/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2013 Free Software Foundation, Inc. * * VAS_EBOOT 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. * * VAS_EBOOT 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 VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include VAS_EBOOT_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; static struct VasEBoot_linux_initrd_context initrd_ctx = {0, 0, 0}; static VasEBoot_efi_handle_t initrd_lf2_handle = NULL; static bool initrd_use_loadfile2 = false; static VasEBoot_guid_t load_file2_guid = VAS_EBOOT_EFI_LOAD_FILE2_PROTOCOL_GUID; static VasEBoot_guid_t device_path_guid = VAS_EBOOT_EFI_DEVICE_PATH_GUID; /* * Clang will produce a warning for missing initializer for the * zero-length array "vendor_defined_data" inside this structure. * Suppress this warning which is treated as an error. */ #ifdef __clang__ #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif static initrd_media_device_path_t initrd_lf2_device_path = { { { VAS_EBOOT_EFI_MEDIA_DEVICE_PATH_TYPE, VAS_EBOOT_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE, sizeof(VasEBoot_efi_vendor_media_device_path_t), }, LINUX_EFI_INITRD_MEDIA_GUID }, { VAS_EBOOT_EFI_END_DEVICE_PATH_TYPE, VAS_EBOOT_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE, sizeof(VasEBoot_efi_device_path_t) } }; extern VasEBoot_err_t VasEBoot_cmd_linux_x86_legacy (VasEBoot_command_t cmd, int argc, char *argv[]); extern VasEBoot_err_t VasEBoot_cmd_initrd_x86_legacy (VasEBoot_command_t cmd, int argc, char *argv[]); static VasEBoot_efi_status_t __VasEBoot_efi_api VasEBoot_efi_initrd_load_file2 (VasEBoot_efi_load_file2_t *this, VasEBoot_efi_device_path_t *device_path, VasEBoot_efi_boolean_t boot_policy, VasEBoot_efi_uintn_t *buffer_size, void *buffer); static VasEBoot_efi_load_file2_t initrd_lf2 = { VasEBoot_efi_initrd_load_file2 }; VasEBoot_err_t VasEBoot_arch_efi_linux_load_image_header (VasEBoot_file_t file, struct linux_arch_kernel_header * lh) { VasEBoot_file_seek (file, 0); if (VasEBoot_file_read (file, lh, sizeof (*lh)) < (VasEBoot_ssize_t) sizeof (*lh)) return VasEBoot_error(VAS_EBOOT_ERR_FILE_READ_ERROR, "failed to read Linux image header"); if ((lh->code0 & 0xffff) != VAS_EBOOT_PE32_MAGIC) return VasEBoot_error (VAS_EBOOT_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", "PE/COFF header @ %08x\n", lh->hdr_offset); /* * The PE/COFF spec permits the COFF header to appear anywhere in the file, so * we need to double check whether it was where we expected it, and if not, we * must load it from the correct offset into the pe_image_header field of * struct linux_arch_kernel_header. */ if ((VasEBoot_uint8_t *) lh + lh->hdr_offset != (VasEBoot_uint8_t *) &lh->pe_image_header) { if (VasEBoot_file_seek (file, lh->hdr_offset) == (VasEBoot_off_t) -1 || VasEBoot_file_read (file, &lh->pe_image_header, sizeof (struct VasEBoot_pe_image_header)) != sizeof (struct VasEBoot_pe_image_header)) return VasEBoot_error (VAS_EBOOT_ERR_FILE_READ_ERROR, "failed to read COFF image header"); } if (lh->pe_image_header.optional_header.magic != VAS_EBOOT_PE32_NATIVE_MAGIC) return VasEBoot_error (VAS_EBOOT_ERR_NOT_IMPLEMENTED_YET, "non-native image not supported"); /* * Linux kernels built for any architecture are guaranteed to support the * LoadFile2 based initrd loading protocol if the image version is >= 1. */ if (lh->pe_image_header.optional_header.major_image_version >= 1) initrd_use_loadfile2 = true; else initrd_use_loadfile2 = false; VasEBoot_dprintf ("linux", "LoadFile2 initrd loading %sabled\n", initrd_use_loadfile2 ? "en" : "dis"); return VAS_EBOOT_ERR_NONE; } #if !defined(__i386__) && !defined(__x86_64__) static VasEBoot_err_t finalize_params_linux (void) { int node, retval; void *fdt; /* Set initrd info */ if (initrd_start && initrd_end > initrd_start) { fdt = VasEBoot_fdt_load (VAS_EBOOT_EFI_LINUX_FDT_EXTRA_SPACE); 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; VasEBoot_dprintf ("linux", "Initrd @ %p-%p\n", (void *) initrd_start, (void *) 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() != VAS_EBOOT_ERR_NONE) goto failure; return VAS_EBOOT_ERR_NONE; failure: VasEBoot_fdt_unload(); return VasEBoot_error(VAS_EBOOT_ERR_BAD_OS, "failed to install/update FDT"); } #endif VasEBoot_err_t VasEBoot_arch_efi_linux_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_status_t status; VasEBoot_efi_loaded_image_t *loaded_image; VasEBoot_size_t len; VasEBoot_size_t args_len; mempath = VasEBoot_malloc (2 * sizeof (VasEBoot_efi_memory_mapped_device_path_t)); if (!mempath) return VasEBoot_errno; mempath[0].header.type = VAS_EBOOT_EFI_HARDWARE_DEVICE_PATH_TYPE; mempath[0].header.subtype = VAS_EBOOT_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE; mempath[0].header.length = VasEBoot_cpu_to_le16_compile_time (sizeof (*mempath)); mempath[0].memory_type = VAS_EBOOT_EFI_LOADER_DATA; mempath[0].start_address = addr; mempath[0].end_address = addr + size; mempath[1].header.type = VAS_EBOOT_EFI_END_DEVICE_PATH_TYPE; mempath[1].header.subtype = VAS_EBOOT_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE; mempath[1].header.length = sizeof (VasEBoot_efi_device_path_t); image_handle = VasEBoot_efi_get_last_verified_image_handle (); if (image_handle == NULL) { status = VasEBoot_efi_load_image (0, VasEBoot_efi_image_handle, (VasEBoot_efi_device_path_t *) mempath, (void *) addr, size, &image_handle); if (status != VAS_EBOOT_EFI_SUCCESS) { VasEBoot_free (mempath); return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "cannot load image"); } } VasEBoot_free (mempath); VasEBoot_dprintf ("linux", "linux command line: '%s'\n", args); /* Convert command line to UTF-16. */ loaded_image = VasEBoot_efi_get_loaded_image (image_handle); if (loaded_image == NULL) { VasEBoot_error (VAS_EBOOT_ERR_BAD_FIRMWARE, "missing loaded_image proto"); goto unload; } args_len = VasEBoot_strlen (args); len = (args_len + 1) * sizeof (VasEBoot_efi_char16_t); loaded_image->load_options = VasEBoot_efi_allocate_any_pages (VAS_EBOOT_EFI_BYTES_TO_PAGES (len)); if (!loaded_image->load_options) return VasEBoot_errno; len = VasEBoot_utf8_to_utf16 (loaded_image->load_options, len, (VasEBoot_uint8_t *) args, args_len, NULL); /* NUL terminate. */ ((VasEBoot_efi_char16_t *) loaded_image->load_options)[len++] = 0; loaded_image->load_options_size = len * sizeof (VasEBoot_efi_char16_t); VasEBoot_dprintf ("linux", "starting image %p\n", image_handle); status = VasEBoot_efi_start_image (image_handle, 0, NULL); /* When successful, not reached */ VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "start_image() returned 0x%" PRIxVAS_EBOOT_EFI_UINTN_T, status); VasEBoot_efi_free_pages ((VasEBoot_addr_t) loaded_image->load_options, VAS_EBOOT_EFI_BYTES_TO_PAGES (len)); loaded_image->load_options = NULL; unload: VasEBoot_efi_unload_image (image_handle); return VasEBoot_errno; } static VasEBoot_err_t VasEBoot_linux_boot (void) { #if !defined(__i386__) && !defined(__x86_64__) if (finalize_params_linux () != VAS_EBOOT_ERR_NONE) return VasEBoot_errno; #endif return VasEBoot_arch_efi_linux_boot_image ((VasEBoot_addr_t) kernel_addr, kernel_size, linux_args); } static VasEBoot_err_t VasEBoot_linux_unload (void) { VasEBoot_efi_boot_services_t *b = VasEBoot_efi_system_table->boot_services; VasEBoot_dl_unref (my_mod); loaded = 0; if (initrd_start) VasEBoot_efi_free_pages ((VasEBoot_efi_physical_address_t) initrd_start, VAS_EBOOT_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_addr_t) kernel_addr, VAS_EBOOT_EFI_BYTES_TO_PAGES (kernel_size)); #if !defined(__i386__) && !defined(__x86_64__) VasEBoot_fdt_unload (); #endif if (initrd_lf2_handle != NULL) { b->uninstall_multiple_protocol_interfaces (initrd_lf2_handle, &load_file2_guid, &initrd_lf2, &device_path_guid, &initrd_lf2_device_path, NULL); initrd_lf2_handle = NULL; initrd_use_loadfile2 = false; } return VAS_EBOOT_ERR_NONE; } #if !defined(__i386__) && !defined(__x86_64__) /* * As per linux/Documentation/arm/Booting * ARM initrd needs to be covered by kernel linear mapping, * so place it in the first 512MB of DRAM. * * As per linux/Documentation/arm64/booting.txt * ARM64 initrd needs to be contained entirely within a 1GB aligned window * of up to 32GB of size that covers the kernel image as well. * Since the EFI stub loader will attempt to load the kernel near start of * RAM, place the buffer in the first 32GB of RAM. */ #ifdef __arm__ #define INITRD_MAX_ADDRESS_OFFSET (512U * 1024 * 1024) #else /* __aarch64__ */ #define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024) #endif /* * This function returns a pointer to a legally allocated initrd buffer, * or NULL if unsuccessful */ static void * allocate_initrd_mem (int initrd_pages) { VasEBoot_addr_t max_addr; if (VasEBoot_efi_get_ram_base (&max_addr) != VAS_EBOOT_ERR_NONE) return NULL; max_addr += INITRD_MAX_ADDRESS_OFFSET - 1; return VasEBoot_efi_allocate_pages_real (max_addr, initrd_pages, VAS_EBOOT_EFI_ALLOCATE_MAX_ADDRESS, VAS_EBOOT_EFI_LOADER_DATA); } #endif static VasEBoot_efi_status_t __VasEBoot_efi_api VasEBoot_efi_initrd_load_file2 (VasEBoot_efi_load_file2_t *this, VasEBoot_efi_device_path_t *device_path, VasEBoot_efi_boolean_t boot_policy, VasEBoot_efi_uintn_t *buffer_size, void *buffer) { VasEBoot_efi_status_t status = VAS_EBOOT_EFI_SUCCESS; VasEBoot_efi_uintn_t initrd_size; if (this != &initrd_lf2 || buffer_size == NULL) return VAS_EBOOT_EFI_INVALID_PARAMETER; if (device_path->type != VAS_EBOOT_EFI_END_DEVICE_PATH_TYPE || device_path->subtype != VAS_EBOOT_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE) return VAS_EBOOT_EFI_NOT_FOUND; if (boot_policy) return VAS_EBOOT_EFI_UNSUPPORTED; initrd_size = VasEBoot_get_initrd_size (&initrd_ctx); if (buffer == NULL || *buffer_size < initrd_size) { *buffer_size = initrd_size; return VAS_EBOOT_EFI_BUFFER_TOO_SMALL; } VasEBoot_dprintf ("linux", "Providing initrd via EFI_LOAD_FILE2_PROTOCOL\n"); if (VasEBoot_initrd_load (&initrd_ctx, buffer)) status = VAS_EBOOT_EFI_DEVICE_ERROR; else *buffer_size = initrd_size; VasEBoot_initrd_close (&initrd_ctx); return status; } static VasEBoot_err_t VasEBoot_cmd_initrd (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { int __attribute__ ((unused)) initrd_size, initrd_pages; void *__attribute__ ((unused)) initrd_mem = NULL; VasEBoot_efi_boot_services_t *b = VasEBoot_efi_system_table->boot_services; VasEBoot_efi_status_t status; if (argc == 0) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected")); goto fail; } #if defined(__i386__) || defined(__x86_64__) if (!initrd_use_loadfile2) return VasEBoot_cmd_initrd_x86_legacy (cmd, argc, argv); #endif if (!loaded) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); goto fail; } if (VasEBoot_initrd_init (argc, argv, &initrd_ctx)) goto fail; if (initrd_use_loadfile2) { if (initrd_lf2_handle == NULL) { status = b->install_multiple_protocol_interfaces (&initrd_lf2_handle, &load_file2_guid, &initrd_lf2, &device_path_guid, &initrd_lf2_device_path, NULL); if (status == VAS_EBOOT_EFI_OUT_OF_RESOURCES) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_MEMORY, N_("out of memory")); goto fail; } else if (status != VAS_EBOOT_EFI_SUCCESS) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("failed to install protocols")); goto fail; } } VasEBoot_dprintf ("linux", "Using LoadFile2 initrd loading protocol\n"); return VAS_EBOOT_ERR_NONE; } #if !defined(__i386__) && !defined(__x86_64__) initrd_size = VasEBoot_get_initrd_size (&initrd_ctx); VasEBoot_dprintf ("linux", "Loading initrd\n"); initrd_pages = (VAS_EBOOT_EFI_BYTES_TO_PAGES (initrd_size)); initrd_mem = allocate_initrd_mem (initrd_pages); if (!initrd_mem) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_MEMORY, N_("out of memory")); goto fail; } if (VasEBoot_initrd_load (&initrd_ctx, initrd_mem)) { VasEBoot_efi_free_pages ((VasEBoot_addr_t) initrd_mem, initrd_pages); 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); #endif fail: VasEBoot_initrd_close (&initrd_ctx); 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 linux_arch_kernel_header lh; VasEBoot_err_t err; VasEBoot_dl_ref (my_mod); if (VasEBoot_is_using_legacy_shim_lock_protocol () == true) { #if defined(__i386__) || defined(__x86_64__) VasEBoot_dprintf ("linux", "using legacy shim_lock protocol, falling back to legacy Linux kernel loader\n"); err = VasEBoot_cmd_linux_x86_legacy (cmd, argc, argv); if (err == VAS_EBOOT_ERR_NONE) return VAS_EBOOT_ERR_NONE; else goto fail; #else VasEBoot_dprintf ("linux", "using legacy shim_lock protocol on non-x86, only db verifiable kernels will work\n"); #endif } if (argc == 0) { VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected")); goto fail; } file = VasEBoot_file_open (argv[0], VAS_EBOOT_FILE_TYPE_LINUX_KERNEL); if (!file) goto fail; VasEBoot_loader_unset(); kernel_size = VasEBoot_file_size (file); if (VasEBoot_arch_efi_linux_load_image_header (file, &lh) != VAS_EBOOT_ERR_NONE) #if !defined(__i386__) && !defined(__x86_64__) goto fail; #else goto fallback; if (!initrd_use_loadfile2) { /* * This is a EFI stub image but it is too old to implement the LoadFile2 * based initrd loading scheme, and Linux/x86 does not support the DT * based method either. So fall back to the x86-specific loader that * enters Linux in EFI mode but without going through its EFI stub. */ fallback: VasEBoot_file_close (file); return VasEBoot_cmd_linux_x86_legacy (cmd, argc, argv); } #endif VasEBoot_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size); kernel_addr = VasEBoot_efi_allocate_any_pages (VAS_EBOOT_EFI_BYTES_TO_PAGES (kernel_size)); VasEBoot_dprintf ("linux", "kernel numpages: %lld\n", (long long) VAS_EBOOT_EFI_BYTES_TO_PAGES (kernel_size)); if (!kernel_addr) { VasEBoot_error (VAS_EBOOT_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 (VAS_EBOOT_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); linux_args = VasEBoot_malloc (cmdline_size); if (!linux_args) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_MEMORY, N_("out of memory")); goto fail; } VasEBoot_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); err = VasEBoot_create_loader_cmdline (argc, argv, linux_args + sizeof (LINUX_IMAGE) - 1, cmdline_size, VAS_EBOOT_VERIFY_KERNEL_CMDLINE); if (err) goto fail; if (VasEBoot_errno == VAS_EBOOT_ERR_NONE) { VasEBoot_loader_set (VasEBoot_linux_boot, VasEBoot_linux_unload, 0); loaded = 1; } fail: if (file) VasEBoot_file_close (file); if (VasEBoot_errno != VAS_EBOOT_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_addr_t) kernel_addr, VAS_EBOOT_EFI_BYTES_TO_PAGES (kernel_size)); return VasEBoot_errno; } static VasEBoot_command_t cmd_linux, cmd_initrd; VAS_EBOOT_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; } VAS_EBOOT_MOD_FINI (linux) { VasEBoot_unregister_command (cmd_linux); VasEBoot_unregister_command (cmd_initrd); }