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_pages (0,
152 GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
153 if (!loaded_image->load_options)
156 loaded_image->load_options_size =
157 2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
158 (grub_uint8_t *) args, len, NULL);
160 grub_dprintf ("linux", "starting image %p\n", image_handle);
161 status = b->start_image (image_handle, 0, NULL);
163 /* When successful, not reached */
164 b->unload_image (image_handle);
165 grub_efi_free_pages ((grub_efi_physical_address_t) loaded_image->load_options,
166 GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
172 grub_linux_boot (void)
174 if (finalize_params_linux () != GRUB_ERR_NONE)
177 return (grub_arm64_uefi_boot_image((grub_addr_t)kernel_addr,
178 kernel_size, linux_args));
182 grub_linux_unload (void)
184 grub_dl_unref (my_mod);
187 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
188 GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
189 initrd_start = initrd_end = 0;
190 grub_free (linux_args);
192 grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
193 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
195 return GRUB_ERR_NONE;
199 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
200 int argc, char *argv[])
202 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
203 int initrd_size, initrd_pages;
204 void *initrd_mem = NULL;
208 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
214 grub_error (GRUB_ERR_BAD_ARGUMENT,
215 N_("you need to load the kernel first"));
219 if (grub_initrd_init (argc, argv, &initrd_ctx))
222 initrd_size = grub_get_initrd_size (&initrd_ctx);
223 grub_dprintf ("linux", "Loading initrd\n");
225 initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
226 initrd_mem = grub_efi_allocate_pages (0, initrd_pages);
229 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
233 if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
236 initrd_start = (grub_addr_t) initrd_mem;
237 initrd_end = initrd_start + initrd_size;
238 grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
239 (void *) initrd_start, initrd_size);
242 grub_initrd_close (&initrd_ctx);
243 if (initrd_mem && !initrd_start)
244 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_mem,
251 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
252 int argc, char *argv[])
254 grub_file_t file = 0;
255 struct grub_arm64_linux_kernel_header lh;
257 grub_dl_ref (my_mod);
261 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
265 file = grub_file_open (argv[0]);
269 kernel_size = grub_file_size (file);
271 if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
274 if (grub_arm64_uefi_check_image (&lh) != GRUB_ERR_NONE)
279 grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
280 kernel_addr = grub_efi_allocate_pages (0, GRUB_EFI_BYTES_TO_PAGES (kernel_size));
281 grub_dprintf ("linux", "kernel numpages: %lld\n",
282 (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
285 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
289 grub_file_seek (file, 0);
290 if (grub_file_read (file, kernel_addr, kernel_size)
291 < (grub_int64_t) kernel_size)
294 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
298 grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
300 cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
301 linux_args = grub_malloc (cmdline_size);
304 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
307 grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
308 grub_create_loader_cmdline (argc, argv,
309 linux_args + sizeof (LINUX_IMAGE) - 1,
312 if (grub_errno == GRUB_ERR_NONE)
314 grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
320 grub_file_close (file);
322 if (grub_errno != GRUB_ERR_NONE)
324 grub_dl_unref (my_mod);
328 if (linux_args && !loaded)
329 grub_free (linux_args);
331 if (kernel_addr && !loaded)
332 grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
333 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
339 static grub_command_t cmd_linux, cmd_initrd;
341 GRUB_MOD_INIT (linux)
343 cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
345 cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
350 GRUB_MOD_FINI (linux)
352 grub_unregister_command (cmd_linux);
353 grub_unregister_command (cmd_initrd);