2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/charset.h>
20 #include <grub/command.h>
22 #include <grub/file.h>
24 #include <grub/linux.h>
25 #include <grub/loader.h>
27 #include <grub/types.h>
28 #include <grub/cpu/linux.h>
29 #include <grub/cpu/fdtload.h>
30 #include <grub/efi/efi.h>
31 #include <grub/efi/pe32.h>
32 #include <grub/i18n.h>
33 #include <grub/lib/cmdline.h>
35 GRUB_MOD_LICENSE ("GPLv3+");
37 static grub_dl_t my_mod;
40 static void *kernel_addr;
41 static grub_uint64_t kernel_size;
43 static char *linux_args;
44 static grub_uint32_t cmdline_size;
46 static grub_addr_t initrd_start;
47 static grub_addr_t initrd_end;
50 grub_arm64_uefi_check_image (struct grub_arm64_linux_kernel_header * lh)
52 if (lh->magic != GRUB_ARM64_LINUX_MAGIC)
53 return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
55 if ((lh->code0 & 0xffff) != GRUB_EFI_PE_MAGIC)
56 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
57 N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
59 grub_dprintf ("linux", "UEFI stub kernel:\n");
60 grub_dprintf ("linux", "text_offset = 0x%012llx\n",
61 (long long unsigned) lh->text_offset);
62 grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset);
68 finalize_params_linux (void)
74 fdt = grub_fdt_load (0x400);
79 node = grub_fdt_find_subnode (fdt, 0, "chosen");
81 node = grub_fdt_add_subnode (fdt, 0, "chosen");
87 if (initrd_start && initrd_end > initrd_start)
89 grub_dprintf ("linux", "Initrd @ 0x%012lx-0x%012lx\n",
90 initrd_start, initrd_end);
92 retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start",
96 retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end",
102 if (grub_fdt_install() != GRUB_ERR_NONE)
105 return GRUB_ERR_NONE;
109 return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT");
113 grub_arm64_uefi_boot_image (grub_addr_t addr, grub_size_t size, char *args)
115 grub_efi_memory_mapped_device_path_t *mempath;
116 grub_efi_handle_t image_handle;
117 grub_efi_boot_services_t *b;
118 grub_efi_status_t status;
119 grub_efi_loaded_image_t *loaded_image;
122 mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
126 mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE;
127 mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE;
128 mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath));
129 mempath[0].memory_type = GRUB_EFI_LOADER_DATA;
130 mempath[0].start_address = addr;
131 mempath[0].end_address = addr + size;
133 mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE;
134 mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
135 mempath[1].header.length = sizeof (grub_efi_device_path_t);
137 b = grub_efi_system_table->boot_services;
138 status = b->load_image (0, grub_efi_image_handle,
139 (grub_efi_device_path_t *) mempath,
140 (void *) addr, size, &image_handle);
141 if (status != GRUB_EFI_SUCCESS)
142 return grub_error (GRUB_ERR_BAD_OS, "cannot load image");
144 grub_dprintf ("linux", "linux command line: '%s'\n", args);
146 /* Convert command line to UCS-2 */
147 loaded_image = grub_efi_get_loaded_image (image_handle);
148 loaded_image->load_options_size = len =
149 (grub_strlen (args) + 1) * sizeof (grub_efi_char16_t);
150 loaded_image->load_options =
151 grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
152 if (!loaded_image->load_options)
155 loaded_image->load_options_size =
156 2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
157 (grub_uint8_t *) args, len, NULL);
159 grub_dprintf ("linux", "starting image %p\n", image_handle);
160 status = b->start_image (image_handle, 0, NULL);
162 /* When successful, not reached */
163 b->unload_image (image_handle);
164 grub_efi_free_pages ((grub_efi_physical_address_t) loaded_image->load_options,
165 GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
171 grub_linux_boot (void)
173 if (finalize_params_linux () != GRUB_ERR_NONE)
176 return (grub_arm64_uefi_boot_image((grub_addr_t)kernel_addr,
177 kernel_size, linux_args));
181 grub_linux_unload (void)
183 grub_dl_unref (my_mod);
186 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
187 GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
188 initrd_start = initrd_end = 0;
189 grub_free (linux_args);
191 grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
192 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
194 return GRUB_ERR_NONE;
198 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
199 int argc, char *argv[])
201 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
202 int initrd_size, initrd_pages;
203 void *initrd_mem = NULL;
207 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
213 grub_error (GRUB_ERR_BAD_ARGUMENT,
214 N_("you need to load the kernel first"));
218 if (grub_initrd_init (argc, argv, &initrd_ctx))
221 initrd_size = grub_get_initrd_size (&initrd_ctx);
222 grub_dprintf ("linux", "Loading initrd\n");
224 initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
225 initrd_mem = grub_efi_allocate_any_pages (initrd_pages);
228 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
232 if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
235 initrd_start = (grub_addr_t) initrd_mem;
236 initrd_end = initrd_start + initrd_size;
237 grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
238 (void *) initrd_start, initrd_size);
241 grub_initrd_close (&initrd_ctx);
242 if (initrd_mem && !initrd_start)
243 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_mem,
250 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
251 int argc, char *argv[])
253 grub_file_t file = 0;
254 struct grub_arm64_linux_kernel_header lh;
256 grub_dl_ref (my_mod);
260 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
264 file = grub_file_open (argv[0]);
268 kernel_size = grub_file_size (file);
270 if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
273 if (grub_arm64_uefi_check_image (&lh) != GRUB_ERR_NONE)
278 grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
279 kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
280 grub_dprintf ("linux", "kernel numpages: %lld\n",
281 (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
284 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
288 grub_file_seek (file, 0);
289 if (grub_file_read (file, kernel_addr, kernel_size)
290 < (grub_int64_t) kernel_size)
293 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
297 grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
299 cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
300 linux_args = grub_malloc (cmdline_size);
303 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
306 grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
307 grub_create_loader_cmdline (argc, argv,
308 linux_args + sizeof (LINUX_IMAGE) - 1,
311 if (grub_errno == GRUB_ERR_NONE)
313 grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
319 grub_file_close (file);
321 if (grub_errno != GRUB_ERR_NONE)
323 grub_dl_unref (my_mod);
327 if (linux_args && !loaded)
328 grub_free (linux_args);
330 if (kernel_addr && !loaded)
331 grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
332 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
338 static grub_command_t cmd_linux, cmd_initrd;
340 GRUB_MOD_INIT (linux)
342 cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
344 cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
349 GRUB_MOD_FINI (linux)
351 grub_unregister_command (cmd_linux);
352 grub_unregister_command (cmd_initrd);