]> git.proxmox.com Git - grub2.git/blob - grub-core/loader/arm64/linux.c
Use full initializer for initrd_ctx to avoid fatal warnings with older GCC
[grub2.git] / grub-core / loader / arm64 / linux.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19 #include <grub/charset.h>
20 #include <grub/command.h>
21 #include <grub/err.h>
22 #include <grub/file.h>
23 #include <grub/fdt.h>
24 #include <grub/linux.h>
25 #include <grub/loader.h>
26 #include <grub/mm.h>
27 #include <grub/types.h>
28 #include <grub/cpu/linux.h>
29 #include <grub/efi/efi.h>
30 #include <grub/efi/pe32.h>
31 #include <grub/i18n.h>
32 #include <grub/lib/cmdline.h>
33
34 GRUB_MOD_LICENSE ("GPLv3+");
35
36 #define GRUB_EFI_PAGE_SHIFT 12
37 #define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> GRUB_EFI_PAGE_SHIFT)
38 #define GRUB_EFI_PE_MAGIC 0x5A4D
39
40 static grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
41
42 static grub_dl_t my_mod;
43 static int loaded;
44
45 static void *kernel_addr;
46 static grub_uint64_t kernel_size;
47
48 static char *linux_args;
49 static grub_uint32_t cmdline_size;
50
51 static grub_addr_t initrd_start;
52 static grub_addr_t initrd_end;
53
54 static void *loaded_fdt;
55 static void *fdt;
56
57 static void *
58 get_firmware_fdt (void)
59 {
60 grub_efi_configuration_table_t *tables;
61 void *firmware_fdt = NULL;
62 unsigned int i;
63
64 /* Look for FDT in UEFI config tables. */
65 tables = grub_efi_system_table->configuration_table;
66
67 for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
68 if (grub_memcmp (&tables[i].vendor_guid, &fdt_guid, sizeof (fdt_guid)) == 0)
69 {
70 firmware_fdt = tables[i].vendor_table;
71 grub_dprintf ("linux", "found registered FDT @ %p\n", firmware_fdt);
72 break;
73 }
74
75 return firmware_fdt;
76 }
77
78 static void
79 get_fdt (void)
80 {
81 void *raw_fdt;
82 grub_size_t size;
83
84 if (fdt)
85 {
86 size = BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt));
87 grub_efi_free_pages ((grub_efi_physical_address_t) fdt, size);
88 }
89
90 if (loaded_fdt)
91 raw_fdt = loaded_fdt;
92 else
93 raw_fdt = get_firmware_fdt();
94
95 size =
96 raw_fdt ? grub_fdt_get_totalsize (raw_fdt) : GRUB_FDT_EMPTY_TREE_SZ;
97 size += 0x400;
98
99 grub_dprintf ("linux", "allocating %ld bytes for fdt\n", size);
100 fdt = grub_efi_allocate_pages (0, BYTES_TO_PAGES (size));
101 if (!fdt)
102 return;
103
104 if (raw_fdt)
105 {
106 grub_memmove (fdt, raw_fdt, size);
107 grub_fdt_set_totalsize (fdt, size);
108 }
109 else
110 {
111 grub_fdt_create_empty_tree (fdt, size);
112 }
113 }
114
115 static grub_err_t
116 check_kernel (struct grub_arm64_linux_kernel_header *lh)
117 {
118 if (lh->magic != GRUB_ARM64_LINUX_MAGIC)
119 return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
120
121 if ((lh->code0 & 0xffff) != GRUB_EFI_PE_MAGIC)
122 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
123 N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
124
125 grub_dprintf ("linux", "UEFI stub kernel:\n");
126 grub_dprintf ("linux", "text_offset = 0x%012llx\n",
127 (long long unsigned) lh->text_offset);
128 grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset);
129
130 return GRUB_ERR_NONE;
131 }
132
133 static grub_err_t
134 finalize_params (void)
135 {
136 grub_efi_boot_services_t *b;
137 grub_efi_status_t status;
138 int node, retval;
139
140 get_fdt ();
141 if (!fdt)
142 goto failure;
143
144 node = grub_fdt_find_subnode (fdt, 0, "chosen");
145 if (node < 0)
146 node = grub_fdt_add_subnode (fdt, 0, "chosen");
147
148 if (node < 1)
149 goto failure;
150
151 /* Set initrd info */
152 if (initrd_start && initrd_end > initrd_start)
153 {
154 grub_dprintf ("linux", "Initrd @ 0x%012lx-0x%012lx\n",
155 initrd_start, initrd_end);
156
157 retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start",
158 initrd_start);
159 if (retval)
160 goto failure;
161 retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end",
162 initrd_end);
163 if (retval)
164 goto failure;
165 }
166
167 b = grub_efi_system_table->boot_services;
168 status = b->install_configuration_table (&fdt_guid, fdt);
169 if (status != GRUB_EFI_SUCCESS)
170 goto failure;
171
172 grub_dprintf ("linux", "Installed/updated FDT configuration table @ %p\n",
173 fdt);
174
175 return GRUB_ERR_NONE;
176
177 failure:
178 grub_efi_free_pages ((grub_efi_physical_address_t) fdt,
179 BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)));
180 fdt = NULL;
181 return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT");
182 }
183
184 static grub_err_t
185 grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
186 int argc, char *argv[])
187 {
188 grub_file_t dtb;
189 void *blob = NULL;
190 int size;
191
192 if (!loaded)
193 {
194 grub_error (GRUB_ERR_BAD_ARGUMENT,
195 N_("you need to load the kernel first"));
196 return GRUB_ERR_BAD_OS;
197 }
198
199 if (argc != 1)
200 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
201
202 if (loaded_fdt)
203 grub_free (loaded_fdt);
204 loaded_fdt = NULL;
205
206 dtb = grub_file_open (argv[0]);
207 if (!dtb)
208 goto out;
209
210 size = grub_file_size (dtb);
211 blob = grub_malloc (size);
212 if (!blob)
213 goto out;
214
215 if (grub_file_read (dtb, blob, size) < size)
216 {
217 if (!grub_errno)
218 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
219 goto out;
220 }
221
222 if (grub_fdt_check_header (blob, size) != 0)
223 {
224 grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree"));
225 goto out;
226 }
227
228 out:
229 if (dtb)
230 grub_file_close (dtb);
231
232 if (blob)
233 {
234 if (grub_errno == GRUB_ERR_NONE)
235 loaded_fdt = blob;
236 else
237 grub_free (blob);
238 }
239
240 return grub_errno;
241 }
242
243 static grub_err_t
244 grub_linux_boot (void)
245 {
246 grub_efi_memory_mapped_device_path_t *mempath;
247 grub_efi_handle_t image_handle;
248 grub_efi_boot_services_t *b;
249 grub_efi_status_t status;
250 grub_err_t retval;
251 grub_efi_loaded_image_t *loaded_image;
252 int len;
253
254 retval = finalize_params();
255 if (retval != GRUB_ERR_NONE)
256 return retval;
257
258 mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
259 if (!mempath)
260 return grub_errno;
261
262 mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE;
263 mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE;
264 mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath));
265 mempath[0].memory_type = GRUB_EFI_LOADER_DATA;
266 mempath[0].start_address = (grub_addr_t) kernel_addr;
267 mempath[0].end_address = (grub_addr_t) kernel_addr + kernel_size;
268
269 mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE;
270 mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
271 mempath[1].header.length = sizeof (grub_efi_device_path_t);
272
273 b = grub_efi_system_table->boot_services;
274 status = b->load_image (0, grub_efi_image_handle,
275 (grub_efi_device_path_t *) mempath,
276 kernel_addr, kernel_size, &image_handle);
277 if (status != GRUB_EFI_SUCCESS)
278 return grub_error (GRUB_ERR_BAD_OS, "cannot load image");
279
280 grub_dprintf ("linux", "linux command line: '%s'\n", linux_args);
281
282 /* Convert command line to UCS-2 */
283 loaded_image = grub_efi_get_loaded_image (image_handle);
284 loaded_image->load_options_size = len =
285 (grub_strlen (linux_args) + 1) * sizeof (grub_efi_char16_t);
286 loaded_image->load_options =
287 grub_efi_allocate_pages (0,
288 BYTES_TO_PAGES (loaded_image->load_options_size));
289 if (!loaded_image->load_options)
290 return grub_errno;
291
292 loaded_image->load_options_size =
293 2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
294 (grub_uint8_t *) linux_args, len, NULL);
295
296 grub_dprintf("linux", "starting image %p\n", image_handle);
297 status = b->start_image (image_handle, 0, NULL);
298
299 /* When successful, not reached */
300 b->unload_image (image_handle);
301 grub_efi_free_pages ((grub_efi_physical_address_t) loaded_image->load_options,
302 BYTES_TO_PAGES (loaded_image->load_options_size));
303
304 return grub_errno;
305 }
306
307 static grub_err_t
308 grub_linux_unload (void)
309 {
310 grub_dl_unref (my_mod);
311 loaded = 0;
312 if (initrd_start)
313 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
314 BYTES_TO_PAGES (initrd_end - initrd_start));
315 initrd_start = initrd_end = 0;
316 grub_free (linux_args);
317 if (kernel_addr)
318 grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
319 BYTES_TO_PAGES (kernel_size));
320 if (fdt)
321 grub_efi_free_pages ((grub_efi_physical_address_t) fdt,
322 BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)));
323
324 return GRUB_ERR_NONE;
325 }
326
327 static grub_err_t
328 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
329 int argc, char *argv[])
330 {
331 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
332 int initrd_size, initrd_pages;
333 void *initrd_mem = NULL;
334
335 if (argc == 0)
336 {
337 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
338 goto fail;
339 }
340
341 if (!loaded)
342 {
343 grub_error (GRUB_ERR_BAD_ARGUMENT,
344 N_("you need to load the kernel first"));
345 goto fail;
346 }
347
348 if (grub_initrd_init (argc, argv, &initrd_ctx))
349 goto fail;
350
351 initrd_size = grub_get_initrd_size (&initrd_ctx);
352 grub_dprintf ("linux", "Loading initrd\n");
353
354 initrd_pages = (BYTES_TO_PAGES (initrd_size));
355 initrd_mem = grub_efi_allocate_pages (0, initrd_pages);
356 if (!initrd_mem)
357 {
358 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
359 goto fail;
360 }
361
362 if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
363 goto fail;
364
365 initrd_start = (grub_addr_t) initrd_mem;
366 initrd_end = initrd_start + initrd_size;
367 grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
368 (void *) initrd_start, initrd_size);
369
370 fail:
371 grub_initrd_close (&initrd_ctx);
372 if (initrd_mem && !initrd_start)
373 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_mem,
374 initrd_pages);
375
376 return grub_errno;
377 }
378
379 static grub_err_t
380 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
381 int argc, char *argv[])
382 {
383 grub_file_t file = 0;
384 struct grub_arm64_linux_kernel_header lh;
385
386 grub_dl_ref (my_mod);
387
388 if (argc == 0)
389 {
390 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
391 goto fail;
392 }
393
394 file = grub_file_open (argv[0]);
395 if (!file)
396 goto fail;
397
398 kernel_size = grub_file_size (file);
399
400 if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
401 return grub_errno;
402
403 if (check_kernel (&lh) != GRUB_ERR_NONE)
404 goto fail;
405
406 grub_loader_unset();
407
408 grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
409 kernel_addr = grub_efi_allocate_pages (0, BYTES_TO_PAGES (kernel_size));
410 grub_dprintf ("linux", "kernel numpages: %lld\n",
411 (long long) BYTES_TO_PAGES (kernel_size));
412 if (!kernel_addr)
413 {
414 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
415 goto fail;
416 }
417
418 grub_file_seek (file, 0);
419 if (grub_file_read (file, kernel_addr, kernel_size)
420 < (grub_int64_t) kernel_size)
421 {
422 if (!grub_errno)
423 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
424 goto fail;
425 }
426
427 grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
428
429 cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
430 linux_args = grub_malloc (cmdline_size);
431 if (!linux_args)
432 {
433 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
434 goto fail;
435 }
436 grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
437 grub_create_loader_cmdline (argc, argv,
438 linux_args + sizeof (LINUX_IMAGE) - 1,
439 cmdline_size);
440
441 if (grub_errno == GRUB_ERR_NONE)
442 {
443 grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
444 loaded = 1;
445 }
446
447 fail:
448 if (file)
449 grub_file_close (file);
450
451 if (grub_errno != GRUB_ERR_NONE)
452 {
453 grub_dl_unref (my_mod);
454 loaded = 0;
455 }
456
457 if (linux_args && !loaded)
458 grub_free (linux_args);
459
460 if (kernel_addr && !loaded)
461 grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
462 BYTES_TO_PAGES (kernel_size));
463
464 return grub_errno;
465 }
466
467
468 static grub_command_t cmd_linux, cmd_initrd, cmd_devicetree;
469
470 GRUB_MOD_INIT (linux)
471 {
472 cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
473 N_("Load Linux."));
474 cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
475 N_("Load initrd."));
476 cmd_devicetree =
477 grub_register_command ("devicetree", grub_cmd_devicetree, 0,
478 N_("Load DTB file."));
479 my_mod = mod;
480 }
481
482 GRUB_MOD_FINI (linux)
483 {
484 grub_unregister_command (cmd_linux);
485 grub_unregister_command (cmd_initrd);
486 grub_unregister_command (cmd_devicetree);
487 }