/* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2013 Free Software Foundation, Inc. * * VasEBoot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VasEBoot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VasEBoot. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include VasEBoot_MOD_LICENSE ("GPLv3+"); static VasEBoot_dl_t my_mod; static int loaded; static void *kernel_addr; static VasEBoot_uint64_t kernel_size; static char *linux_args; static VasEBoot_uint32_t cmdline_size; static VasEBoot_addr_t initrd_start; static VasEBoot_addr_t initrd_end; VasEBoot_err_t VasEBoot_arm64_uefi_check_image (struct VasEBoot_arm64_linux_kernel_header * lh) { if (lh->magic != VasEBoot_ARM64_LINUX_MAGIC) return VasEBoot_error(VasEBoot_ERR_BAD_OS, "invalid magic number"); if ((lh->code0 & 0xffff) != VasEBoot_EFI_PE_MAGIC) return VasEBoot_error (VasEBoot_ERR_NOT_IMPLEMENTED_YET, N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled")); VasEBoot_dprintf ("linux", "UEFI stub kernel:\n"); VasEBoot_dprintf ("linux", "text_offset = 0x%012llx\n", (long long unsigned) lh->text_offset); VasEBoot_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset); return VasEBoot_ERR_NONE; } static VasEBoot_err_t finalize_params_linux (void) { int node, retval; void *fdt; fdt = VasEBoot_fdt_load (0x400); if (!fdt) goto failure; node = VasEBoot_fdt_find_subnode (fdt, 0, "chosen"); if (node < 0) node = VasEBoot_fdt_add_subnode (fdt, 0, "chosen"); if (node < 1) goto failure; /* Set initrd info */ if (initrd_start && initrd_end > initrd_start) { VasEBoot_dprintf ("linux", "Initrd @ 0x%012lx-0x%012lx\n", initrd_start, initrd_end); retval = VasEBoot_fdt_set_prop64 (fdt, node, "linux,initrd-start", initrd_start); if (retval) goto failure; retval = VasEBoot_fdt_set_prop64 (fdt, node, "linux,initrd-end", initrd_end); if (retval) goto failure; } if (VasEBoot_fdt_install() != VasEBoot_ERR_NONE) goto failure; return VasEBoot_ERR_NONE; failure: VasEBoot_fdt_unload(); return VasEBoot_error(VasEBoot_ERR_BAD_OS, "failed to install/update FDT"); } VasEBoot_err_t VasEBoot_arm64_uefi_boot_image (VasEBoot_addr_t addr, VasEBoot_size_t size, char *args) { VasEBoot_efi_memory_mapped_device_path_t *mempath; VasEBoot_efi_handle_t image_handle; VasEBoot_efi_boot_services_t *b; VasEBoot_efi_status_t status; VasEBoot_efi_loaded_image_t *loaded_image; int len; mempath = VasEBoot_malloc (2 * sizeof (VasEBoot_efi_memory_mapped_device_path_t)); if (!mempath) return VasEBoot_errno; mempath[0].header.type = VasEBoot_EFI_HARDWARE_DEVICE_PATH_TYPE; mempath[0].header.subtype = VasEBoot_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE; mempath[0].header.length = VasEBoot_cpu_to_le16_compile_time (sizeof (*mempath)); mempath[0].memory_type = VasEBoot_EFI_LOADER_DATA; mempath[0].start_address = addr; mempath[0].end_address = addr + size; mempath[1].header.type = VasEBoot_EFI_END_DEVICE_PATH_TYPE; mempath[1].header.subtype = VasEBoot_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE; mempath[1].header.length = sizeof (VasEBoot_efi_device_path_t); b = VasEBoot_efi_system_table->boot_services; status = b->load_image (0, VasEBoot_efi_image_handle, (VasEBoot_efi_device_path_t *) mempath, (void *) addr, size, &image_handle); if (status != VasEBoot_EFI_SUCCESS) return VasEBoot_error (VasEBoot_ERR_BAD_OS, "cannot load image"); VasEBoot_dprintf ("linux", "linux command line: '%s'\n", args); /* Convert command line to UCS-2 */ loaded_image = VasEBoot_efi_get_loaded_image (image_handle); loaded_image->load_options_size = len = (VasEBoot_strlen (args) + 1) * sizeof (VasEBoot_efi_char16_t); loaded_image->load_options = VasEBoot_efi_allocate_pages (0, VasEBoot_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); if (!loaded_image->load_options) return VasEBoot_errno; loaded_image->load_options_size = 2 * VasEBoot_utf8_to_utf16 (loaded_image->load_options, len, (VasEBoot_uint8_t *) args, len, NULL); VasEBoot_dprintf ("linux", "starting image %p\n", image_handle); status = b->start_image (image_handle, 0, NULL); /* When successful, not reached */ b->unload_image (image_handle); VasEBoot_efi_free_pages ((VasEBoot_efi_physical_address_t) loaded_image->load_options, VasEBoot_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); return VasEBoot_errno; } static VasEBoot_err_t VasEBoot_linux_boot (void) { if (finalize_params_linux () != VasEBoot_ERR_NONE) return VasEBoot_errno; return (VasEBoot_arm64_uefi_boot_image((VasEBoot_addr_t)kernel_addr, kernel_size, linux_args)); } static VasEBoot_err_t VasEBoot_linux_unload (void) { VasEBoot_dl_unref (my_mod); loaded = 0; if (initrd_start) VasEBoot_efi_free_pages ((VasEBoot_efi_physical_address_t) initrd_start, VasEBoot_EFI_BYTES_TO_PAGES (initrd_end - initrd_start)); initrd_start = initrd_end = 0; VasEBoot_free (linux_args); if (kernel_addr) VasEBoot_efi_free_pages ((VasEBoot_efi_physical_address_t) kernel_addr, VasEBoot_EFI_BYTES_TO_PAGES (kernel_size)); VasEBoot_fdt_unload (); return VasEBoot_ERR_NONE; } static VasEBoot_err_t VasEBoot_cmd_initrd (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { struct VasEBoot_linux_initrd_context initrd_ctx = { 0, 0, 0 }; int initrd_size, initrd_pages; void *initrd_mem = NULL; if (argc == 0) { VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("filename expected")); goto fail; } if (!loaded) { VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); goto fail; } if (VasEBoot_initrd_init (argc, argv, &initrd_ctx)) goto fail; initrd_size = VasEBoot_get_initrd_size (&initrd_ctx); VasEBoot_dprintf ("linux", "Loading initrd\n"); initrd_pages = (VasEBoot_EFI_BYTES_TO_PAGES (initrd_size)); initrd_mem = VasEBoot_efi_allocate_pages (0, initrd_pages); if (!initrd_mem) { VasEBoot_error (VasEBoot_ERR_OUT_OF_MEMORY, N_("out of memory")); goto fail; } if (VasEBoot_initrd_load (&initrd_ctx, argv, initrd_mem)) goto fail; initrd_start = (VasEBoot_addr_t) initrd_mem; initrd_end = initrd_start + initrd_size; VasEBoot_dprintf ("linux", "[addr=%p, size=0x%x]\n", (void *) initrd_start, initrd_size); fail: VasEBoot_initrd_close (&initrd_ctx); if (initrd_mem && !initrd_start) VasEBoot_efi_free_pages ((VasEBoot_efi_physical_address_t) initrd_mem, initrd_pages); return VasEBoot_errno; } static VasEBoot_err_t VasEBoot_cmd_linux (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { VasEBoot_file_t file = 0; struct VasEBoot_arm64_linux_kernel_header lh; VasEBoot_dl_ref (my_mod); if (argc == 0) { VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("filename expected")); goto fail; } file = VasEBoot_file_open (argv[0]); if (!file) goto fail; kernel_size = VasEBoot_file_size (file); if (VasEBoot_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh)) return VasEBoot_errno; if (VasEBoot_arm64_uefi_check_image (&lh) != VasEBoot_ERR_NONE) goto fail; VasEBoot_loader_unset(); VasEBoot_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size); kernel_addr = VasEBoot_efi_allocate_pages (0, VasEBoot_EFI_BYTES_TO_PAGES (kernel_size)); VasEBoot_dprintf ("linux", "kernel numpages: %lld\n", (long long) VasEBoot_EFI_BYTES_TO_PAGES (kernel_size)); if (!kernel_addr) { VasEBoot_error (VasEBoot_ERR_OUT_OF_MEMORY, N_("out of memory")); goto fail; } VasEBoot_file_seek (file, 0); if (VasEBoot_file_read (file, kernel_addr, kernel_size) < (VasEBoot_int64_t) kernel_size) { if (!VasEBoot_errno) VasEBoot_error (VasEBoot_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); goto fail; } VasEBoot_dprintf ("linux", "kernel @ %p\n", kernel_addr); cmdline_size = VasEBoot_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE) + VERITY_CMDLINE_LENGTH; linux_args = VasEBoot_malloc (cmdline_size); if (!linux_args) { VasEBoot_error (VasEBoot_ERR_OUT_OF_MEMORY, N_("out of memory")); goto fail; } VasEBoot_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); VasEBoot_create_loader_cmdline (argc, argv, linux_args + sizeof (LINUX_IMAGE) - 1, cmdline_size); if (VasEBoot_errno == VasEBoot_ERR_NONE) { VasEBoot_pass_verity_hash (kernel_addr, linux_args, cmdline_size); VasEBoot_loader_set (VasEBoot_linux_boot, VasEBoot_linux_unload, 0); loaded = 1; } fail: if (file) VasEBoot_file_close (file); if (VasEBoot_errno != VasEBoot_ERR_NONE) { VasEBoot_dl_unref (my_mod); loaded = 0; } if (linux_args && !loaded) VasEBoot_free (linux_args); if (kernel_addr && !loaded) VasEBoot_efi_free_pages ((VasEBoot_efi_physical_address_t) kernel_addr, VasEBoot_EFI_BYTES_TO_PAGES (kernel_size)); return VasEBoot_errno; } static VasEBoot_command_t cmd_linux, cmd_initrd; VasEBoot_MOD_INIT (linux) { cmd_linux = VasEBoot_register_command ("linux", VasEBoot_cmd_linux, 0, N_("Load Linux.")); cmd_initrd = VasEBoot_register_command ("initrd", VasEBoot_cmd_initrd, 0, N_("Load initrd.")); my_mod = mod; } VasEBoot_MOD_FINI (linux) { VasEBoot_unregister_command (cmd_linux); VasEBoot_unregister_command (cmd_initrd); }