]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/loader/arm64/linux.c
c5f82927ad0ba82075dc5948438334ab62e523af
[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 count = 0;
308 const char *env;
309 char c;
310 char *newenv;
311 char *last, *pos;
312
313 //grub_printf("ventoy_bootopt_hook: %d %d\n", argc, ventoy_linux_argc);
314
315 if (ventoy_linux_argc == 0)
316 {
317 return 0;
318 }
319
320 /* the 1st parameter is BOOT_IMAGE=xxxx */
321 if (argc > 0 && 0 == ventoy_boot_opt_filter(argv[0]))
322 {
323 ventoy_linux_args[count++] = grub_strdup(argv[0]);
324 }
325
326 for (i = 0; i < ventoy_linux_argc; i++)
327 {
328 ventoy_linux_args[count] = ventoy_linux_args[i + (LINUX_MAX_ARGC / 2)];
329 ventoy_linux_args[i + (LINUX_MAX_ARGC / 2)] = NULL;
330
331 if (ventoy_linux_args[count][0] == '@')
332 {
333 env = grub_env_get(ventoy_linux_args[count] + 1);
334 if (env)
335 {
336 grub_free(ventoy_linux_args[count]);
337
338 newenv = grub_strdup(env);
339 last = newenv;
340
341 while (*last)
342 {
343 while (*last)
344 {
345 if (*last != ' ' && *last != '\t')
346 {
347 break;
348 }
349 last++;
350 }
351
352 if (*last == 0)
353 {
354 break;
355 }
356
357 for (pos = last; *pos; pos++)
358 {
359 if (*pos == ' ' || *pos == '\t')
360 {
361 c = *pos;
362 *pos = 0;
363 if (0 == ventoy_boot_opt_filter(last))
364 {
365 ventoy_linux_args[count++] = grub_strdup(last);
366 }
367 *pos = c;
368 break;
369 }
370 }
371
372 if (*pos == 0)
373 {
374 if (0 == ventoy_boot_opt_filter(last))
375 {
376 ventoy_linux_args[count++] = grub_strdup(last);
377 }
378 break;
379 }
380
381 last = pos + 1;
382 }
383 }
384 else
385 {
386 count++;
387 }
388 }
389 else
390 {
391 count++;
392 }
393 }
394
395 /* We have processed the 1st parameter before, so start from 1 */
396 for (i = 1; i < argc; i++)
397 {
398 if (ventoy_boot_opt_filter(argv[i]))
399 {
400 continue;
401 }
402
403 ventoy_linux_args[count++] = grub_strdup(argv[i]);
404 }
405
406 if (ventoy_debug)
407 {
408 ventoy_linux_args[count++] = grub_strdup("loglevel=7");
409 }
410
411 ventoy_linux_argc = count;
412
413 if (ventoy_debug)
414 {
415 grub_printf("========== bootoption ==========\n");
416 for (i = 0; i < count; i++)
417 {
418 grub_printf("%s ", ventoy_linux_args[i]);
419 }
420 grub_printf("\n================================\n");
421 }
422
423 return 0;
424 }
425
426 static grub_err_t
427 grub_cmd_set_boot_opt (grub_command_t cmd __attribute__ ((unused)),
428 int argc, char *argv[])
429 {
430 int i;
431 const char *vtdebug;
432
433 for (i = 0; i < argc; i++)
434 {
435 ventoy_linux_args[ventoy_linux_argc + (LINUX_MAX_ARGC / 2) ] = grub_strdup(argv[i]);
436 ventoy_linux_argc++;
437 }
438
439 vtdebug = grub_env_get("vtdebug_flag");
440 if (vtdebug && vtdebug[0])
441 {
442 ventoy_debug = 1;
443 }
444
445 if (ventoy_debug) grub_printf("ventoy set boot opt %d\n", ventoy_linux_argc);
446
447 return 0;
448 }
449
450 static grub_err_t
451 grub_cmd_unset_boot_opt (grub_command_t cmd __attribute__ ((unused)),
452 int argc, char *argv[])
453 {
454 int i;
455
456 (void)argc;
457 (void)argv;
458
459 for (i = 0; i < LINUX_MAX_ARGC; i++)
460 {
461 if (ventoy_linux_args[i])
462 {
463 grub_free(ventoy_linux_args[i]);
464 }
465 }
466
467 ventoy_debug = 0;
468 ventoy_linux_argc = 0;
469 ventoy_initrd_called = 0;
470 grub_memset(ventoy_linux_args, 0, sizeof(char *) * LINUX_MAX_ARGC);
471 return 0;
472 }
473
474 static grub_err_t
475 grub_cmd_extra_initrd_append (grub_command_t cmd __attribute__ ((unused)),
476 int argc, char *argv[])
477 {
478 int newclen = 0;
479 char *pos = NULL;
480 char *end = NULL;
481 char buf[256] = {0};
482
483 if (argc != 1)
484 {
485 return 1;
486 }
487
488 for (pos = argv[0]; *pos; pos++)
489 {
490 if (*pos == '/')
491 {
492 end = pos;
493 }
494 }
495
496 if (end)
497 {
498 /* grub2 newc bug workaround */
499 newclen = (int)grub_strlen(end + 1);
500 if ((110 + newclen) % 4 == 0)
501 {
502 grub_snprintf(buf, sizeof(buf), "newc:.%s:%s", end + 1, argv[0]);
503 }
504 else
505 {
506 grub_snprintf(buf, sizeof(buf), "newc:%s:%s", end + 1, argv[0]);
507 }
508
509 if (ventoy_extra_initrd_num < 256)
510 {
511 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(buf);
512 }
513 }
514
515 return 0;
516 }
517
518 static grub_err_t
519 grub_cmd_extra_initrd_reset (grub_command_t cmd __attribute__ ((unused)),
520 int argc, char *argv[])
521 {
522 int i;
523
524 (void)argc;
525 (void)argv;
526
527 for (i = 0; i < ventoy_extra_initrd_num; i++)
528 {
529 if (ventoy_extra_initrd_list[i])
530 {
531 grub_free(ventoy_extra_initrd_list[i]);
532 }
533 }
534
535 grub_memset(ventoy_extra_initrd_list, 0, sizeof(ventoy_extra_initrd_list));
536
537 return 0;
538 }
539
540
541 static grub_err_t
542 grub_linux_boot (void)
543 {
544 ventoy_preboot();
545
546 if (finalize_params_linux () != GRUB_ERR_NONE)
547 return grub_errno;
548
549 return (grub_arch_efi_linux_boot_image((grub_addr_t)kernel_addr,
550 kernel_size, linux_args));
551 }
552
553 static grub_err_t
554 grub_linux_unload (void)
555 {
556 grub_dl_unref (my_mod);
557 loaded = 0;
558 if (initrd_start)
559 grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
560 GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
561 initrd_start = initrd_end = 0;
562 grub_free (linux_args);
563 if (kernel_addr)
564 grub_efi_free_pages ((grub_addr_t) kernel_addr,
565 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
566 grub_fdt_unload ();
567 return GRUB_ERR_NONE;
568 }
569
570 /*
571 * As per linux/Documentation/arm/Booting
572 * ARM initrd needs to be covered by kernel linear mapping,
573 * so place it in the first 512MB of DRAM.
574 *
575 * As per linux/Documentation/arm64/booting.txt
576 * ARM64 initrd needs to be contained entirely within a 1GB aligned window
577 * of up to 32GB of size that covers the kernel image as well.
578 * Since the EFI stub loader will attempt to load the kernel near start of
579 * RAM, place the buffer in the first 32GB of RAM.
580 */
581 #ifdef __arm__
582 #define INITRD_MAX_ADDRESS_OFFSET (512U * 1024 * 1024)
583 #else /* __aarch64__ */
584 #define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024)
585 #endif
586
587 /*
588 * This function returns a pointer to a legally allocated initrd buffer,
589 * or NULL if unsuccessful
590 */
591 static void *
592 allocate_initrd_mem (int initrd_pages)
593 {
594 grub_addr_t max_addr;
595
596 if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE)
597 return NULL;
598
599 max_addr += INITRD_MAX_ADDRESS_OFFSET - 1;
600
601 return grub_efi_allocate_pages_real (max_addr, initrd_pages,
602 GRUB_EFI_ALLOCATE_MAX_ADDRESS,
603 GRUB_EFI_LOADER_DATA);
604 }
605
606 static grub_err_t
607 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
608 int argc, char *argv[])
609 {
610 struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
611 int initrd_size, initrd_pages;
612 void *initrd_mem = NULL;
613
614 if (argc == 0)
615 {
616 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
617 goto fail;
618 }
619
620 if (!loaded)
621 {
622 grub_error (GRUB_ERR_BAD_ARGUMENT,
623 N_("you need to load the kernel first"));
624 goto fail;
625 }
626
627 if (grub_initrd_init (argc, argv, &initrd_ctx))
628 goto fail;
629
630 initrd_size = grub_get_initrd_size (&initrd_ctx);
631 grub_dprintf ("linux", "Loading initrd\n");
632
633 initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
634 initrd_mem = allocate_initrd_mem (initrd_pages);
635
636 if (!initrd_mem)
637 {
638 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
639 goto fail;
640 }
641
642 if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
643 goto fail;
644
645 initrd_start = (grub_addr_t) initrd_mem;
646 initrd_end = initrd_start + initrd_size;
647 grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
648 (void *) initrd_start, initrd_size);
649
650 fail:
651 grub_initrd_close (&initrd_ctx);
652 if (initrd_mem && !initrd_start)
653 grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
654
655 return grub_errno;
656 }
657
658 static grub_err_t
659 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
660 int argc, char *argv[])
661 {
662 grub_file_t file = 0;
663 struct linux_arch_kernel_header lh;
664 grub_err_t err;
665
666 grub_dl_ref (my_mod);
667
668 if (argc == 0)
669 {
670 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
671 goto fail;
672 }
673
674 file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
675 if (!file)
676 goto fail;
677
678 kernel_size = grub_file_size (file);
679
680 if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
681 return grub_errno;
682
683 if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
684 goto fail;
685
686 grub_loader_unset();
687
688 grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
689 kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
690 grub_dprintf ("linux", "kernel numpages: %lld\n",
691 (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
692 if (!kernel_addr)
693 {
694 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
695 goto fail;
696 }
697
698 grub_file_seek (file, 0);
699 if (grub_file_read (file, kernel_addr, kernel_size)
700 < (grub_int64_t) kernel_size)
701 {
702 if (!grub_errno)
703 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
704 goto fail;
705 }
706
707 grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
708
709 cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
710 linux_args = grub_malloc (cmdline_size);
711 if (!linux_args)
712 {
713 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
714 goto fail;
715 }
716 grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
717
718 if (ventoy_linux_argc)
719 {
720 ventoy_bootopt_hook(argc, argv);
721 err = grub_create_loader_cmdline (ventoy_linux_argc, ventoy_linux_args,
722 linux_args + sizeof (LINUX_IMAGE) - 1,
723 cmdline_size,
724 GRUB_VERIFY_KERNEL_CMDLINE); }
725 else
726 {
727 err = grub_create_loader_cmdline (argc, argv,
728 linux_args + sizeof (LINUX_IMAGE) - 1,
729 cmdline_size,
730 GRUB_VERIFY_KERNEL_CMDLINE);
731 }
732
733 if (err)
734 goto fail;
735
736 if (grub_errno == GRUB_ERR_NONE)
737 {
738 grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
739 loaded = 1;
740 }
741
742 fail:
743 if (file)
744 grub_file_close (file);
745
746 if (grub_errno != GRUB_ERR_NONE)
747 {
748 grub_dl_unref (my_mod);
749 loaded = 0;
750 }
751
752 if (linux_args && !loaded)
753 grub_free (linux_args);
754
755 if (kernel_addr && !loaded)
756 grub_efi_free_pages ((grub_addr_t) kernel_addr,
757 GRUB_EFI_BYTES_TO_PAGES (kernel_size));
758
759 return grub_errno;
760 }
761
762 static grub_err_t
763 ventoy_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
764 int argc, char *argv[])
765 {
766 int i;
767 const char *file;
768 char buf[64];
769
770 if (ventoy_debug) grub_printf("ventoy_cmd_initrd %d\n", ventoy_linux_argc);
771
772 if (ventoy_linux_argc == 0)
773 {
774 return grub_cmd_initrd(cmd, argc, argv);
775 }
776
777 grub_snprintf(buf, sizeof(buf), "mem:%s:size:%s", grub_env_get("ventoy_cpio_addr"), grub_env_get("ventoy_cpio_size"));
778
779 if (ventoy_debug) grub_printf("membuf=%s\n", buf);
780
781 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(buf);
782
783 file = grub_env_get("vtoy_img_part_file");
784 if (file)
785 {
786 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(file);
787 }
788
789 for (i = 0; i < argc; i++)
790 {
791 ventoy_extra_initrd_list[ventoy_extra_initrd_num++] = grub_strdup(argv[i]);
792 }
793
794 ventoy_initrd_called = 1;
795
796 if (ventoy_debug)
797 {
798 grub_printf("========== initrd list ==========\n");
799 for (i = 0; i < ventoy_extra_initrd_num; i++)
800 {
801 grub_printf("%s\n", ventoy_extra_initrd_list[i]);
802 }
803 grub_printf("=================================\n");
804 }
805
806 return grub_cmd_initrd(cmd, ventoy_extra_initrd_num, ventoy_extra_initrd_list);
807 }
808
809
810 static grub_command_t cmd_linux, cmd_initrd, cmd_linuxefi, cmd_initrdefi;
811 static grub_command_t cmd_set_bootopt, cmd_unset_bootopt, cmd_extra_initrd_append, cmd_extra_initrd_reset;
812
813
814 GRUB_MOD_INIT (linux)
815 {
816 cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
817 N_("Load Linux."));
818 cmd_initrd = grub_register_command ("initrd", ventoy_cmd_initrd, 0,
819 N_("Load initrd."));
820
821 cmd_linuxefi = grub_register_command ("linuxefi", grub_cmd_linux,
822 0, N_("Load Linux."));
823 cmd_initrdefi = grub_register_command ("initrdefi", ventoy_cmd_initrd,
824 0, N_("Load initrd."));
825
826 cmd_set_bootopt = grub_register_command ("vt_set_boot_opt", grub_cmd_set_boot_opt, 0, N_("set ext boot opt"));
827 cmd_unset_bootopt = grub_register_command ("vt_unset_boot_opt", grub_cmd_unset_boot_opt, 0, N_("unset ext boot opt"));
828
829 cmd_extra_initrd_append = grub_register_command ("vt_img_extra_initrd_append", grub_cmd_extra_initrd_append, 0, N_(""));
830 cmd_extra_initrd_reset = grub_register_command ("vt_img_extra_initrd_reset", grub_cmd_extra_initrd_reset, 0, N_(""));
831
832 ventoy_linux_args = grub_zalloc(sizeof(char *) * LINUX_MAX_ARGC);
833
834 my_mod = mod;
835 }
836
837 GRUB_MOD_FINI (linux)
838 {
839 grub_unregister_command (cmd_linux);
840 grub_unregister_command (cmd_initrd);
841 }