]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/loader/arm64/linux.c
f8dfec6444505cb12008f1fcca3be1b00ec6bec0
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / 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/fdtload.h>
31 #include <grub/efi/memory.h>
32 #include <grub/efi/pe32.h>
33 #include <grub/i18n.h>
34 #include <grub/lib/cmdline.h>
35 #include <grub/verify.h>
36 #include <grub/term.h>
37 #include <grub/env.h>
38
39 GRUB_MOD_LICENSE ("GPLv3+");
40
41 static grub_dl_t my_mod;
42 static int loaded;
43
44 static void *kernel_addr;
45 static grub_uint64_t kernel_size;
46
47 static char *linux_args;
48 static grub_uint32_t cmdline_size;
49
50 static grub_addr_t initrd_start;
51 static grub_addr_t initrd_end;
52
53 #define LINUX_MAX_ARGC 1024
54 static int ventoy_debug = 0;
55 static int ventoy_initrd_called = 0;
56 static int ventoy_linux_argc = 0;
57 static char **ventoy_linux_args = NULL;
58 static int ventoy_extra_initrd_num = 0;
59 static char *ventoy_extra_initrd_list[256];
60 static grub_err_t
61 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
62 int argc, char *argv[]);
63
64 grub_err_t
65 grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh)
66 {
67 if (lh->magic != GRUB_LINUX_ARMXX_MAGIC_SIGNATURE)
68 return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
69
70 if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
71 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
72 N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
73
74 grub_dprintf ("linux", "UEFI stub kernel:\n");
75 grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset);
76
77 return GRUB_ERR_NONE;
78 }
79
80 static grub_err_t
81 finalize_params_linux (void)
82 {
83 int node, retval;
84
85 void *fdt;
86
87 fdt = grub_fdt_load (GRUB_EFI_LINUX_FDT_EXTRA_SPACE);
88
89 if (!fdt)
90 goto failure;
91
92 node = grub_fdt_find_subnode (fdt, 0, "chosen");
93 if (node < 0)
94 node = grub_fdt_add_subnode (fdt, 0, "chosen");
95
96 if (node < 1)
97 goto failure;
98
99 /* Set initrd info */
100 if (initrd_start && initrd_end > initrd_start)
101 {
102 grub_dprintf ("linux", "Initrd @ %p-%p\n",
103 (void *) initrd_start, (void *) initrd_end);
104
105 retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start",
106 initrd_start);
107 if (retval)
108 goto failure;
109 retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end",
110 initrd_end);
111 if (retval)
112 goto failure;
113 }
114
115 if (grub_fdt_install() != GRUB_ERR_NONE)
116 goto failure;
117
118 return GRUB_ERR_NONE;
119
120 failure:
121 grub_fdt_unload();
122 return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT");
123 }
124
125 grub_err_t
126 grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
127 {
128 grub_efi_memory_mapped_device_path_t *mempath;
129 grub_efi_handle_t image_handle;
130 grub_efi_boot_services_t *b;
131 grub_efi_status_t status;
132 grub_efi_loaded_image_t *loaded_image;
133 int len;
134
135 mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
136 if (!mempath)
137 return grub_errno;
138
139 mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE;
140 mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE;
141 mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath));
142 mempath[0].memory_type = GRUB_EFI_LOADER_DATA;
143 mempath[0].start_address = addr;
144 mempath[0].end_address = addr + size;
145
146 mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE;
147 mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
148 mempath[1].header.length = sizeof (grub_efi_device_path_t);
149
150 b = grub_efi_system_table->boot_services;
151 status = b->load_image (0, grub_efi_image_handle,
152 (grub_efi_device_path_t *) mempath,
153 (void *) addr, size, &image_handle);
154 if (status != GRUB_EFI_SUCCESS)
155 return grub_error (GRUB_ERR_BAD_OS, "cannot load image");
156
157 grub_dprintf ("linux", "linux command line: '%s'\n", args);
158
159 /* Convert command line to UCS-2 */
160 loaded_image = grub_efi_get_loaded_image (image_handle);
161 loaded_image->load_options_size = len =
162 (grub_strlen (args) + 1) * sizeof (grub_efi_char16_t);
163 loaded_image->load_options =
164 grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
165 if (!loaded_image->load_options)
166 return grub_errno;
167
168 loaded_image->load_options_size =
169 2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
170 (grub_uint8_t *) args, len, NULL);
171
172 grub_dprintf ("linux", "starting image %p\n", image_handle);
173 status = b->start_image (image_handle, 0, NULL);
174
175 /* When successful, not reached */
176 b->unload_image (image_handle);
177 grub_efi_free_pages ((grub_addr_t) loaded_image->load_options,
178 GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
179
180 return grub_errno;
181 }
182
183
184 static void ventoy_debug_pause(void)
185 {
186 char key;
187
188 if (0 == ventoy_debug)
189 {
190 return;
191 }
192
193 grub_printf("press Enter to continue ......\n");
194 while (1)
195 {
196 key = grub_getkey();
197 if (key == '\n' || key == '\r')
198 {
199 break;
200 }
201 }
202 }
203
204 static int ventoy_preboot(void)
205 {
206 int i;
207 const char *file;
208 char buf[128];
209
210 if (ventoy_debug)
211 {
212 grub_printf("ventoy_preboot %d %d\n", ventoy_linux_argc, ventoy_initrd_called);
213 ventoy_debug_pause();
214 }
215
216 if (ventoy_linux_argc == 0)
217 {
218 return 0;
219 }
220
221 if (ventoy_initrd_called)
222 {
223 ventoy_initrd_called = 0;
224 return 0;
225 }
226
227 grub_snprintf(buf, sizeof(buf), "mem:%s:size:%s", grub_env_get("ventoy_cpio_addr"), grub_env_get("ventoy_cpio_size"));
228
229 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(buf);
230
231 file = grub_env_get("vtoy_img_part_file");
232 if (file)
233 {
234 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(file);
235 }
236
237 if (ventoy_debug)
238 {
239 grub_printf("========== initrd list ==========\n");
240 for (i = 0; i < ventoy_extra_initrd_num; i++)
241 {
242 grub_printf("%s\n", ventoy_extra_initrd_list[i]);
243 }
244 grub_printf("=================================\n");
245
246 ventoy_debug_pause();
247 }
248
249 grub_cmd_initrd(NULL, ventoy_extra_initrd_num, ventoy_extra_initrd_list);
250
251 return 0;
252 }
253
254 static int ventoy_boot_opt_filter(char *opt)
255 {
256 if (grub_strcmp(opt, "noinitrd") == 0)
257 {
258 return 1;
259 }
260
261 if (grub_strcmp(opt, "vga=current") == 0)
262 {
263 return 1;
264 }
265
266 if (grub_strncmp(opt, "rdinit=", 7) == 0)
267 {
268 if (grub_strcmp(opt, "rdinit=/vtoy/vtoy") != 0)
269 {
270 opt[0] = 'v';
271 opt[1] = 't';
272 }
273 return 0;
274 }
275
276 if (grub_strncmp(opt, "init=", 5) == 0)
277 {
278 opt[0] = 'v';
279 opt[1] = 't';
280 return 0;
281 }
282
283 if (ventoy_debug)
284 {
285 if (grub_strcmp(opt, "quiet") == 0)
286 {
287 return 1;
288 }
289
290 if (grub_strncmp(opt, "loglevel=", 9) == 0)
291 {
292 return 1;
293 }
294
295 if (grub_strcmp(opt, "splash") == 0)
296 {
297 return 1;
298 }
299 }
300
301 return 0;
302 }
303
304 static int ventoy_bootopt_hook(int argc, char *argv[])
305 {
306 int i;
307 int TmpIdx;
308 int count = 0;
309 const char *env;
310 char c;
311 char *newenv;
312 char *last, *pos;
313
314 //grub_printf("ventoy_bootopt_hook: %d %d\n", argc, ventoy_linux_argc);
315
316 if (ventoy_linux_argc == 0)
317 {
318 return 0;
319 }
320
321 /* To avoid --- parameter, we split two parts */
322 for (TmpIdx = 0; TmpIdx < argc; TmpIdx++)
323 {
324 if (ventoy_boot_opt_filter(argv[TmpIdx]))
325 {
326 continue;
327 }
328
329 if (grub_strncmp(argv[TmpIdx], "--", 2) == 0)
330 {
331 break;
332 }
333
334 ventoy_linux_args[count++] = grub_strdup(argv[TmpIdx]);
335 }
336
337 for (i = 0; i < ventoy_linux_argc; i++)
338 {
339 ventoy_linux_args[count] = ventoy_linux_args[i + (LINUX_MAX_ARGC / 2)];
340 ventoy_linux_args[i + (LINUX_MAX_ARGC / 2)] = NULL;
341
342 if (ventoy_linux_args[count][0] == '@')
343 {
344 env = grub_env_get(ventoy_linux_args[count] + 1);
345 if (env)
346 {
347 grub_free(ventoy_linux_args[count]);
348
349 newenv = grub_strdup(env);
350 last = newenv;
351
352 while (*last)
353 {
354 while (*last)
355 {
356 if (*last != ' ' && *last != '\t')
357 {
358 break;
359 }
360 last++;
361 }
362
363 if (*last == 0)
364 {
365 break;
366 }
367
368 for (pos = last; *pos; pos++)
369 {
370 if (*pos == ' ' || *pos == '\t')
371 {
372 c = *pos;
373 *pos = 0;
374 if (0 == ventoy_boot_opt_filter(last))
375 {
376 ventoy_linux_args[count++] = grub_strdup(last);
377 }
378 *pos = c;
379 break;
380 }
381 }
382
383 if (*pos == 0)
384 {
385 if (0 == ventoy_boot_opt_filter(last))
386 {
387 ventoy_linux_args[count++] = grub_strdup(last);
388 }
389 break;
390 }
391
392 last = pos + 1;
393 }
394 }
395 else
396 {
397 count++;
398 }
399 }
400 else
401 {
402 count++;
403 }
404 }
405
406 while (TmpIdx < argc)
407 {
408 if (ventoy_boot_opt_filter(argv[TmpIdx]))
409 {
410 continue;
411 }
412
413 ventoy_linux_args[count++] = grub_strdup(argv[TmpIdx]);
414 TmpIdx++;
415 }
416
417 if (ventoy_debug)
418 {
419 ventoy_linux_args[count++] = grub_strdup("loglevel=7");
420 }
421
422 ventoy_linux_argc = count;
423
424 if (ventoy_debug)
425 {
426 grub_printf("========== bootoption ==========\n");
427 for (i = 0; i < count; i++)
428 {
429 grub_printf("%s ", ventoy_linux_args[i]);
430 }
431 grub_printf("\n================================\n");
432 }
433
434 return 0;
435 }
436
437 static grub_err_t
438 grub_cmd_set_boot_opt (grub_command_t cmd __attribute__ ((unused)),
439 int argc, char *argv[])
440 {
441 int i;
442 const char *vtdebug;
443
444 for (i = 0; i < argc; i++)
445 {
446 ventoy_linux_args[ventoy_linux_argc + (LINUX_MAX_ARGC / 2) ] = grub_strdup(argv[i]);
447 ventoy_linux_argc++;
448 }
449
450 vtdebug = grub_env_get("vtdebug_flag");
451 if (vtdebug && vtdebug[0])
452 {
453 ventoy_debug = 1;
454 }
455
456 if (ventoy_debug) grub_printf("ventoy set boot opt %d\n", ventoy_linux_argc);
457
458 return 0;
459 }
460
461 static grub_err_t
462 grub_cmd_unset_boot_opt (grub_command_t cmd __attribute__ ((unused)),
463 int argc, char *argv[])
464 {
465 int i;
466
467 (void)argc;
468 (void)argv;
469
470 for (i = 0; i < LINUX_MAX_ARGC; i++)
471 {
472 if (ventoy_linux_args[i])
473 {
474 grub_free(ventoy_linux_args[i]);
475 }
476 }
477
478 ventoy_debug = 0;
479 ventoy_linux_argc = 0;
480 ventoy_initrd_called = 0;
481 grub_memset(ventoy_linux_args, 0, sizeof(char *) * LINUX_MAX_ARGC);
482 return 0;
483 }
484
485 static grub_err_t
486 grub_cmd_extra_initrd_append (grub_command_t cmd __attribute__ ((unused)),
487 int argc, char *argv[])
488 {
489 int newclen = 0;
490 char *pos = NULL;
491 char *end = NULL;
492 char buf[256] = {0};
493
494 if (argc != 1)
495 {
496 return 1;
497 }
498
499 for (pos = argv[0]; *pos; pos++)
500 {
501 if (*pos == '/')
502 {
503 end = pos;
504 }
505 }
506
507 if (end)
508 {
509 /* grub2 newc bug workaround */
510 newclen = (int)grub_strlen(end + 1);
511 if ((110 + newclen) % 4 == 0)
512 {
513 grub_snprintf(buf, sizeof(buf), "newc:.%s:%s", end + 1, argv[0]);
514 }
515 else
516 {
517 grub_snprintf(buf, sizeof(buf), "newc:%s:%s", end + 1, argv[0]);
518 }
519
520 if (ventoy_extra_initrd_num < 256)
521 {
522 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(buf);
523 }
524 }
525
526 return 0;
527 }
528
529 static grub_err_t
530 grub_cmd_extra_initrd_reset (grub_command_t cmd __attribute__ ((unused)),
531 int argc, char *argv[])
532 {
533 int i;
534
535 (void)argc;
536 (void)argv;
537
538 for (i = 0; i < ventoy_extra_initrd_num; i++)
539 {
540 if (ventoy_extra_initrd_list[i])
541 {
542 grub_free(ventoy_extra_initrd_list[i]);
543 }
544 }
545
546 grub_memset(ventoy_extra_initrd_list, 0, sizeof(ventoy_extra_initrd_list));
547
548 return 0;
549 }
550
551
552 static grub_err_t
553 grub_linux_boot (void)
554 {
555 ventoy_preboot();
556
557 if (finalize_params_linux () != GRUB_ERR_NONE)
558 return grub_errno;
559
560 return (grub_arch_efi_linux_boot_image((grub_addr_t)kernel_addr,
561 kernel_size, linux_args));
562 }
563
564 static grub_err_t
565 grub_linux_unload (void)
566 {
567 grub_dl_unref (my_mod);
568 loaded = 0;
569 if (initrd_start)
570 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
571 GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
572 initrd_start = initrd_end = 0;
573 grub_free (linux_args);
574 if (kernel_addr)
575 grub_efi_free_pages ((grub_addr_t) kernel_addr,
576 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
577 grub_fdt_unload ();
578 return GRUB_ERR_NONE;
579 }
580
581 /*
582 * As per linux/Documentation/arm/Booting
583 * ARM initrd needs to be covered by kernel linear mapping,
584 * so place it in the first 512MB of DRAM.
585 *
586 * As per linux/Documentation/arm64/booting.txt
587 * ARM64 initrd needs to be contained entirely within a 1GB aligned window
588 * of up to 32GB of size that covers the kernel image as well.
589 * Since the EFI stub loader will attempt to load the kernel near start of
590 * RAM, place the buffer in the first 32GB of RAM.
591 */
592 #ifdef __arm__
593 #define INITRD_MAX_ADDRESS_OFFSET (512U * 1024 * 1024)
594 #else /* __aarch64__ */
595 #define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024)
596 #endif
597
598 /*
599 * This function returns a pointer to a legally allocated initrd buffer,
600 * or NULL if unsuccessful
601 */
602 static void *
603 allocate_initrd_mem (int initrd_pages)
604 {
605 grub_addr_t max_addr;
606
607 if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE)
608 return NULL;
609
610 max_addr += INITRD_MAX_ADDRESS_OFFSET - 1;
611
612 return grub_efi_allocate_pages_real (max_addr, initrd_pages,
613 GRUB_EFI_ALLOCATE_MAX_ADDRESS,
614 GRUB_EFI_LOADER_DATA);
615 }
616
617 static grub_err_t
618 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
619 int argc, char *argv[])
620 {
621 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
622 int initrd_size, initrd_pages;
623 void *initrd_mem = NULL;
624
625 if (argc == 0)
626 {
627 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
628 goto fail;
629 }
630
631 if (!loaded)
632 {
633 grub_error (GRUB_ERR_BAD_ARGUMENT,
634 N_("you need to load the kernel first"));
635 goto fail;
636 }
637
638 if (grub_initrd_init (argc, argv, &initrd_ctx))
639 goto fail;
640
641 initrd_size = grub_get_initrd_size (&initrd_ctx);
642 grub_dprintf ("linux", "Loading initrd\n");
643
644 initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
645 initrd_mem = allocate_initrd_mem (initrd_pages);
646
647 if (!initrd_mem)
648 {
649 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
650 goto fail;
651 }
652
653 if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
654 goto fail;
655
656 initrd_start = (grub_addr_t) initrd_mem;
657 initrd_end = initrd_start + initrd_size;
658 grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
659 (void *) initrd_start, initrd_size);
660
661 fail:
662 grub_initrd_close (&initrd_ctx);
663 if (initrd_mem && !initrd_start)
664 grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
665
666 return grub_errno;
667 }
668
669 static grub_err_t
670 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
671 int argc, char *argv[])
672 {
673 grub_file_t file = 0;
674 struct linux_arch_kernel_header lh;
675 grub_err_t err;
676
677 grub_dl_ref (my_mod);
678
679 if (argc == 0)
680 {
681 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
682 goto fail;
683 }
684
685 file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
686 if (!file)
687 goto fail;
688
689 kernel_size = grub_file_size (file);
690
691 if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
692 return grub_errno;
693
694 if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
695 goto fail;
696
697 grub_loader_unset();
698
699 grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
700 kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
701 grub_dprintf ("linux", "kernel numpages: %lld\n",
702 (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
703 if (!kernel_addr)
704 {
705 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
706 goto fail;
707 }
708
709 grub_file_seek (file, 0);
710 if (grub_file_read (file, kernel_addr, kernel_size)
711 < (grub_int64_t) kernel_size)
712 {
713 if (!grub_errno)
714 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
715 goto fail;
716 }
717
718 grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
719
720 cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
721 linux_args = grub_malloc (cmdline_size);
722 if (!linux_args)
723 {
724 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
725 goto fail;
726 }
727 grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
728
729 if (ventoy_linux_argc)
730 {
731 ventoy_bootopt_hook(argc, argv);
732 err = grub_create_loader_cmdline (ventoy_linux_argc, ventoy_linux_args,
733 linux_args + sizeof (LINUX_IMAGE) - 1,
734 cmdline_size,
735 GRUB_VERIFY_KERNEL_CMDLINE); }
736 else
737 {
738 err = grub_create_loader_cmdline (argc, argv,
739 linux_args + sizeof (LINUX_IMAGE) - 1,
740 cmdline_size,
741 GRUB_VERIFY_KERNEL_CMDLINE);
742 }
743
744 if (err)
745 goto fail;
746
747 if (grub_errno == GRUB_ERR_NONE)
748 {
749 grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
750 loaded = 1;
751 }
752
753 fail:
754 if (file)
755 grub_file_close (file);
756
757 if (grub_errno != GRUB_ERR_NONE)
758 {
759 grub_dl_unref (my_mod);
760 loaded = 0;
761 }
762
763 if (linux_args && !loaded)
764 grub_free (linux_args);
765
766 if (kernel_addr && !loaded)
767 grub_efi_free_pages ((grub_addr_t) kernel_addr,
768 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
769
770 return grub_errno;
771 }
772
773 static grub_err_t
774 ventoy_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
775 int argc, char *argv[])
776 {
777 int i;
778 const char *file;
779 char buf[64];
780
781 if (ventoy_debug) grub_printf("ventoy_cmd_initrd %d\n", ventoy_linux_argc);
782
783 if (ventoy_linux_argc == 0)
784 {
785 return grub_cmd_initrd(cmd, argc, argv);
786 }
787
788 grub_snprintf(buf, sizeof(buf), "mem:%s:size:%s", grub_env_get("ventoy_cpio_addr"), grub_env_get("ventoy_cpio_size"));
789
790 if (ventoy_debug) grub_printf("membuf=%s\n", buf);
791
792 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(buf);
793
794 file = grub_env_get("vtoy_img_part_file");
795 if (file)
796 {
797 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(file);
798 }
799
800 for (i = 0; i < argc; i++)
801 {
802 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(argv[i]);
803 }
804
805 ventoy_initrd_called = 1;
806
807 if (ventoy_debug)
808 {
809 grub_printf("========== initrd list ==========\n");
810 for (i = 0; i < ventoy_extra_initrd_num; i++)
811 {
812 grub_printf("%s\n", ventoy_extra_initrd_list[i]);
813 }
814 grub_printf("=================================\n");
815 }
816
817 return grub_cmd_initrd(cmd, ventoy_extra_initrd_num, ventoy_extra_initrd_list);
818 }
819
820
821 static grub_command_t cmd_linux, cmd_initrd, cmd_linuxefi, cmd_initrdefi;
822 static grub_command_t cmd_set_bootopt, cmd_unset_bootopt, cmd_extra_initrd_append, cmd_extra_initrd_reset;
823
824
825 GRUB_MOD_INIT (linux)
826 {
827 cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
828 N_("Load Linux."));
829 cmd_initrd = grub_register_command ("initrd", ventoy_cmd_initrd, 0,
830 N_("Load initrd."));
831
832 cmd_linuxefi = grub_register_command ("linuxefi", grub_cmd_linux,
833 0, N_("Load Linux."));
834 cmd_initrdefi = grub_register_command ("initrdefi", ventoy_cmd_initrd,
835 0, N_("Load initrd."));
836
837 cmd_set_bootopt = grub_register_command ("vt_set_boot_opt", grub_cmd_set_boot_opt, 0, N_("set ext boot opt"));
838 cmd_unset_bootopt = grub_register_command ("vt_unset_boot_opt", grub_cmd_unset_boot_opt, 0, N_("unset ext boot opt"));
839
840 cmd_extra_initrd_append = grub_register_command ("vt_img_extra_initrd_append", grub_cmd_extra_initrd_append, 0, N_(""));
841 cmd_extra_initrd_reset = grub_register_command ("vt_img_extra_initrd_reset", grub_cmd_extra_initrd_reset, 0, N_(""));
842
843 ventoy_linux_args = grub_zalloc(sizeof(char *) * LINUX_MAX_ARGC);
844
845 my_mod = mod;
846 }
847
848 GRUB_MOD_FINI (linux)
849 {
850 grub_unregister_command (cmd_linux);
851 grub_unregister_command (cmd_initrd);
852 }