arm64: Share some functions and variables in linux.c to multiboot.c Move some #define from grub-core/loader/arm64/linux.c to include/grub/arm64/linux.h Reason: (1)make code more orderly (2)these #define can be used by other files which includs include/grub/arm64/linux.h
Signed-off-by: Fu Wei fu.wei@linaro.org --- grub-core/loader/arm64/linux.c | 83 +++++++++++++++++++++++------------------- include/grub/arm64/linux.h | 18 +++++++++ 2 files changed, 63 insertions(+), 38 deletions(-)
diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index 564a75a..4f2fa74 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -26,6 +26,7 @@ #include <grub/mm.h> #include <grub/types.h> #include <grub/cpu/linux.h> +#include <grub/cpu/multiboot.h> #include <grub/efi/efi.h> #include <grub/efi/pe32.h> #include <grub/i18n.h> @@ -33,19 +34,15 @@
GRUB_MOD_LICENSE ("GPLv3+");
-#define GRUB_EFI_PAGE_SHIFT 12 -#define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> GRUB_EFI_PAGE_SHIFT) -#define GRUB_EFI_PE_MAGIC 0x5A4D - static grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
static grub_dl_t my_mod; -static int loaded; +loaded_type_t grub_linux_loaded;
-static void *kernel_addr; -static grub_uint64_t kernel_size; +void *kernel_addr; +grub_uint64_t kernel_size;
-static char *linux_args; +char *linux_args; static grub_uint32_t cmdline_size;
static grub_addr_t initrd_start; @@ -54,6 +51,8 @@ static grub_addr_t initrd_end; static void *loaded_fdt; static void *fdt;
+finalize_params_t finalize_params; + static void * get_firmware_fdt (void) { @@ -75,8 +74,8 @@ get_firmware_fdt (void) return firmware_fdt; }
-static void -get_fdt (void) +void * +grub_linux_get_fdt (void) { void *raw_fdt; grub_size_t size; @@ -99,7 +98,7 @@ get_fdt (void) grub_dprintf ("linux", "allocating %ld bytes for fdt\n", size); fdt = grub_efi_allocate_pages (0, BYTES_TO_PAGES (size)); if (!fdt) - return; + return NULL;
if (raw_fdt) { @@ -110,10 +109,11 @@ get_fdt (void) { grub_fdt_create_empty_tree (fdt, size); } + return (fdt); }
-static grub_err_t -check_kernel (struct grub_arm64_linux_kernel_header *lh) +grub_err_t +grub_linux_check_kernel (struct grub_arm64_linux_kernel_header *lh) { if (lh->magic != GRUB_ARM64_LINUX_MAGIC) return grub_error(GRUB_ERR_BAD_OS, "invalid magic number"); @@ -131,14 +131,13 @@ check_kernel (struct grub_arm64_linux_kernel_header *lh) }
static grub_err_t -finalize_params (void) +finalize_params_linux (void) { grub_efi_boot_services_t *b; grub_efi_status_t status; int node, retval;
- get_fdt (); - if (!fdt) + if (!grub_linux_get_fdt ()) goto failure;
node = grub_fdt_find_subnode (fdt, 0, "chosen"); @@ -189,7 +188,7 @@ grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)), void *blob = NULL; int size;
- if (!loaded) + if (!grub_linux_loaded) { grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); @@ -240,7 +239,7 @@ out: return grub_errno; }
-static grub_err_t +grub_err_t grub_linux_boot (void) { grub_efi_memory_mapped_device_path_t *mempath; @@ -304,22 +303,28 @@ grub_linux_boot (void) return grub_errno; }
-static grub_err_t +grub_err_t grub_linux_unload (void) { grub_dl_unref (my_mod); - loaded = 0; - if (initrd_start) - grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start, - BYTES_TO_PAGES (initrd_end - initrd_start)); - initrd_start = initrd_end = 0; - grub_free (linux_args); - if (kernel_addr) - grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr, - BYTES_TO_PAGES (kernel_size)); - if (fdt) - grub_efi_free_pages ((grub_efi_physical_address_t) fdt, - BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); + + if (grub_linux_loaded == LOADED_LINUX) + { + if (kernel_addr) + grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr, + BYTES_TO_PAGES (kernel_size)); + if (linux_args) + grub_free (linux_args); + if (initrd_start) + grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start, + BYTES_TO_PAGES (initrd_end - initrd_start)); + initrd_start = initrd_end = 0; + if (fdt) + grub_efi_free_pages ((grub_efi_physical_address_t) fdt, + BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); + fdt = NULL; + } + grub_linux_loaded = 0;
return GRUB_ERR_NONE; } @@ -338,10 +343,10 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), goto fail; }
- if (!loaded) + if (grub_linux_loaded != LOADED_LINUX) { grub_error (GRUB_ERR_BAD_ARGUMENT, - N_("you need to load the kernel first")); + N_("you need to load the Linux kernel first")); goto fail; }
@@ -400,7 +405,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh)) return grub_errno;
- if (check_kernel (&lh) != GRUB_ERR_NONE) + if (grub_linux_check_kernel (&lh) != GRUB_ERR_NONE) goto fail;
grub_loader_unset(); @@ -440,8 +445,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
if (grub_errno == GRUB_ERR_NONE) { + finalize_params = finalize_params_linux; grub_loader_set (grub_linux_boot, grub_linux_unload, 0); - loaded = 1; + grub_linux_loaded = LOADED_LINUX; }
fail: @@ -451,13 +457,13 @@ fail: if (grub_errno != GRUB_ERR_NONE) { grub_dl_unref (my_mod); - loaded = 0; + grub_linux_loaded = 0; }
- if (linux_args && !loaded) + if (linux_args && !grub_linux_loaded) grub_free (linux_args);
- if (kernel_addr && !loaded) + if (kernel_addr && !grub_linux_loaded) grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr, BYTES_TO_PAGES (kernel_size));
@@ -476,6 +482,7 @@ GRUB_MOD_INIT (linux) cmd_devicetree = grub_register_command ("devicetree", grub_cmd_devicetree, 0, N_("Load DTB file.")); + my_mod = mod; }
diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h index 864e5dc..0695ff6 100644 --- a/include/grub/arm64/linux.h +++ b/include/grub/arm64/linux.h @@ -23,6 +23,10 @@
#define GRUB_ARM64_LINUX_MAGIC 0x644d5241 /* 'ARM\x64' */
+#define GRUB_EFI_PAGE_SHIFT 12 +#define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> GRUB_EFI_PAGE_SHIFT) +#define GRUB_EFI_PE_MAGIC 0x5A4D + /* From linux/Documentation/arm64/booting.txt */ struct grub_arm64_linux_kernel_header { @@ -38,4 +42,18 @@ struct grub_arm64_linux_kernel_header grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ };
+/* Declare the accessor functions for dtb and the "loaded" flag */ +enum loaded_type +{ + LOADED_LINUX = 1, + LOADED_MULTIBOOT +}; +typedef enum loaded_type loaded_type_t; + +typedef grub_err_t (*finalize_params_t) (void); +void *grub_linux_get_fdt (void); +grub_err_t grub_linux_check_kernel (struct grub_arm64_linux_kernel_header *lh); +grub_err_t grub_linux_unload (void); +grub_err_t grub_linux_boot (void); + #endif /* ! GRUB_LINUX_CPU_HEADER */