2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009,2010 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/loader.h>
20 #include <grub/memory.h>
21 #include <grub/normal.h>
22 #include <grub/file.h>
23 #include <grub/disk.h>
25 #include <grub/misc.h>
26 #include <grub/types.h>
29 #include <grub/term.h>
30 #include <grub/cpu/linux.h>
31 #include <grub/video.h>
32 #include <grub/video_fb.h>
33 #include <grub/command.h>
34 #include <grub/i386/relocator.h>
35 #include <grub/i18n.h>
36 #include <grub/lib/cmdline.h>
37 #include <grub/linux.h>
38 #include <grub/machine/kernel.h>
40 GRUB_MOD_LICENSE ("GPLv3+");
42 #ifdef GRUB_MACHINE_PCBIOS
43 #include <grub/i386/pc/vesa_modes_table.h>
46 #ifdef GRUB_MACHINE_EFI
47 #include <grub/efi/efi.h>
48 #define HAS_VGA_TEXT 0
49 #define DEFAULT_VIDEO_MODE "auto"
50 #define ACCEPTS_PURE_TEXT 0
51 #elif defined (GRUB_MACHINE_IEEE1275)
52 #include <grub/ieee1275/ieee1275.h>
53 #define HAS_VGA_TEXT 0
54 #define DEFAULT_VIDEO_MODE "text"
55 #define ACCEPTS_PURE_TEXT 1
57 #include <grub/i386/pc/vbe.h>
58 #include <grub/i386/pc/console.h>
59 #define HAS_VGA_TEXT 1
60 #define DEFAULT_VIDEO_MODE "text"
61 #define ACCEPTS_PURE_TEXT 1
64 static grub_dl_t my_mod
;
66 static grub_size_t linux_mem_size
;
68 static void *prot_mode_mem
;
69 static grub_addr_t prot_mode_target
;
70 static void *initrd_mem
;
71 static grub_addr_t initrd_mem_target
;
72 static grub_size_t prot_init_space
;
73 static struct grub_relocator
*relocator
= NULL
;
74 static void *efi_mmap_buf
;
75 static grub_size_t maximal_cmdline_size
;
76 static struct linux_kernel_params linux_params
;
77 static char *linux_cmdline
;
78 #ifdef GRUB_MACHINE_EFI
79 static grub_efi_uintn_t efi_mmap_size
;
81 static const grub_size_t efi_mmap_size
= 0;
84 #define LINUX_MAX_ARGC 1024
85 static int ventoy_debug
= 0;
86 static int ventoy_initrd_called
= 0;
87 static int ventoy_linux_argc
= 0;
88 static char **ventoy_linux_args
= NULL
;
89 static int ventoy_extra_initrd_num
= 0;
90 static char *ventoy_extra_initrd_list
[256];
93 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)), int argc
, char *argv
[]);
103 static struct idt_descriptor idt_desc
=
110 static inline grub_size_t
111 page_align (grub_size_t size
)
113 return (size
+ (1 << 12) - 1) & (~((1 << 12) - 1));
116 /* Helper for find_mmap_size. */
118 count_hook (grub_uint64_t addr
__attribute__ ((unused
)),
119 grub_uint64_t size
__attribute__ ((unused
)),
120 grub_memory_type_t type
__attribute__ ((unused
)), void *data
)
122 grub_size_t
*count
= data
;
128 /* Find the optimal number of pages for the memory map. */
130 find_mmap_size (void)
132 grub_size_t count
= 0, mmap_size
;
134 grub_mmap_iterate (count_hook
, &count
);
136 mmap_size
= count
* sizeof (struct grub_e820_mmap
);
138 /* Increase the size a bit for safety, because GRUB allocates more on
140 mmap_size
+= (1 << 12);
142 return page_align (mmap_size
);
148 grub_relocator_unload (relocator
);
150 prot_mode_mem
= initrd_mem
= 0;
151 prot_mode_target
= initrd_mem_target
= 0;
154 /* Allocate pages for the real mode code and the protected mode code
155 for linux as well as a memory map buffer. */
157 allocate_pages (grub_size_t prot_size
, grub_size_t
*align
,
158 grub_size_t min_align
, int relocatable
,
159 grub_uint64_t preferred_address
)
166 prot_size
= page_align (prot_size
);
168 /* Initialize the memory pointers with NULL for convenience. */
171 relocator
= grub_relocator_new ();
178 /* FIXME: Should request low memory from the heap when this feature is
182 grub_relocator_chunk_t ch
;
185 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
189 GRUB_RELOCATOR_PREFERENCE_LOW
,
191 for (; err
&& *align
+ 1 > min_align
; (*align
)--)
193 grub_errno
= GRUB_ERR_NONE
;
194 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
196 0xffffffff & ~prot_size
,
197 prot_size
, 1 << *align
,
198 GRUB_RELOCATOR_PREFERENCE_LOW
,
205 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
210 prot_mode_mem
= get_virtual_current_address (ch
);
211 prot_mode_target
= get_physical_target_address (ch
);
214 grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
215 prot_mode_mem
, (unsigned long) prot_mode_target
,
216 (unsigned) prot_size
);
217 return GRUB_ERR_NONE
;
225 grub_e820_add_region (struct grub_e820_mmap
*e820_map
, int *e820_num
,
226 grub_uint64_t start
, grub_uint64_t size
,
231 if ((n
> 0) && (e820_map
[n
- 1].addr
+ e820_map
[n
- 1].size
== start
) &&
232 (e820_map
[n
- 1].type
== type
))
233 e820_map
[n
- 1].size
+= size
;
236 e820_map
[n
].addr
= start
;
237 e820_map
[n
].size
= size
;
238 e820_map
[n
].type
= type
;
241 return GRUB_ERR_NONE
;
245 grub_linux_setup_video (struct linux_kernel_params
*params
)
247 struct grub_video_mode_info mode_info
;
250 grub_video_driver_id_t driver_id
;
251 const char *gfxlfbvar
= grub_env_get ("gfxpayloadforcelfb");
253 driver_id
= grub_video_get_driver_id ();
255 if (driver_id
== GRUB_VIDEO_DRIVER_NONE
)
258 err
= grub_video_get_info_and_fini (&mode_info
, &framebuffer
);
262 grub_errno
= GRUB_ERR_NONE
;
266 params
->lfb_width
= mode_info
.width
;
267 params
->lfb_height
= mode_info
.height
;
268 params
->lfb_depth
= mode_info
.bpp
;
269 params
->lfb_line_len
= mode_info
.pitch
;
271 params
->lfb_base
= (grub_size_t
) framebuffer
;
273 #if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
274 params
->ext_lfb_base
= (grub_size_t
) (((grub_uint64_t
)(grub_size_t
) framebuffer
) >> 32);
275 params
->capabilities
|= VIDEO_CAPABILITY_64BIT_BASE
;
278 params
->lfb_size
= ALIGN_UP (params
->lfb_line_len
* params
->lfb_height
, 65536);
280 params
->red_mask_size
= mode_info
.red_mask_size
;
281 params
->red_field_pos
= mode_info
.red_field_pos
;
282 params
->green_mask_size
= mode_info
.green_mask_size
;
283 params
->green_field_pos
= mode_info
.green_field_pos
;
284 params
->blue_mask_size
= mode_info
.blue_mask_size
;
285 params
->blue_field_pos
= mode_info
.blue_field_pos
;
286 params
->reserved_mask_size
= mode_info
.reserved_mask_size
;
287 params
->reserved_field_pos
= mode_info
.reserved_field_pos
;
289 if (gfxlfbvar
&& (gfxlfbvar
[0] == '1' || gfxlfbvar
[0] == 'y'))
290 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
295 case GRUB_VIDEO_DRIVER_VBE
:
296 params
->lfb_size
>>= 16;
297 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_VESA
;
300 case GRUB_VIDEO_DRIVER_EFI_UGA
:
301 case GRUB_VIDEO_DRIVER_EFI_GOP
:
302 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_EFIFB
;
305 /* FIXME: check if better id is available. */
306 case GRUB_VIDEO_DRIVER_SM712
:
307 case GRUB_VIDEO_DRIVER_SIS315PRO
:
308 case GRUB_VIDEO_DRIVER_VGA
:
309 case GRUB_VIDEO_DRIVER_CIRRUS
:
310 case GRUB_VIDEO_DRIVER_BOCHS
:
311 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E
:
312 case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A
:
313 case GRUB_VIDEO_DRIVER_IEEE1275
:
314 case GRUB_VIDEO_DRIVER_COREBOOT
:
315 /* Make gcc happy. */
316 case GRUB_VIDEO_DRIVER_XEN
:
317 case GRUB_VIDEO_DRIVER_SDL
:
318 case GRUB_VIDEO_DRIVER_NONE
:
319 case GRUB_VIDEO_ADAPTER_CAPTURE
:
320 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
325 #ifdef GRUB_MACHINE_PCBIOS
326 /* VESA packed modes may come with zeroed mask sizes, which need
327 to be set here according to DAC Palette width. If we don't,
328 this results in Linux displaying a black screen. */
329 if (driver_id
== GRUB_VIDEO_DRIVER_VBE
&& mode_info
.bpp
<= 8)
331 struct grub_vbe_info_block controller_info
;
335 status
= grub_vbe_bios_get_controller_info (&controller_info
);
337 if (status
== GRUB_VBE_STATUS_OK
&&
338 (controller_info
.capabilities
& GRUB_VBE_CAPABILITY_DACWIDTH
))
339 status
= grub_vbe_bios_set_dac_palette_width (&width
);
341 if (status
!= GRUB_VBE_STATUS_OK
)
342 /* 6 is default after mode reset. */
345 params
->red_mask_size
= params
->green_mask_size
346 = params
->blue_mask_size
= width
;
347 params
->reserved_mask_size
= 0;
351 return GRUB_ERR_NONE
;
354 /* Context for grub_linux_boot. */
355 struct grub_linux_boot_ctx
357 grub_addr_t real_mode_target
;
358 grub_size_t real_size
;
359 struct linux_kernel_params
*params
;
363 /* Helper for grub_linux_boot. */
365 grub_linux_boot_mmap_find (grub_uint64_t addr
, grub_uint64_t size
,
366 grub_memory_type_t type
, void *data
)
368 struct grub_linux_boot_ctx
*ctx
= data
;
370 /* We must put real mode code in the traditional space. */
371 if (type
!= GRUB_MEMORY_AVAILABLE
|| addr
> 0x90000)
374 if (addr
+ size
< 0x10000)
379 size
+= addr
- 0x10000;
383 if (addr
+ size
> 0x90000)
384 size
= 0x90000 - addr
;
386 if (ctx
->real_size
+ efi_mmap_size
> size
)
389 grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
390 (unsigned long) addr
,
392 (unsigned) (ctx
->real_size
+ efi_mmap_size
));
393 ctx
->real_mode_target
= ((addr
+ size
) - (ctx
->real_size
+ efi_mmap_size
));
397 /* GRUB types conveniently match E820 types. */
399 grub_linux_boot_mmap_fill (grub_uint64_t addr
, grub_uint64_t size
,
400 grub_memory_type_t type
, void *data
)
402 struct grub_linux_boot_ctx
*ctx
= data
;
404 if (grub_e820_add_region (ctx
->params
->e820_map
, &ctx
->e820_num
,
411 static void ventoy_debug_pause(void)
415 if (0 == ventoy_debug
)
420 grub_printf("press Enter to continue ......\n");
424 if (key
== '\n' || key
== '\r')
431 static int ventoy_preboot(void)
439 grub_printf("ventoy_preboot %d %d\n", ventoy_linux_argc
, ventoy_initrd_called
);
440 ventoy_debug_pause();
443 if (ventoy_linux_argc
== 0)
448 if (ventoy_initrd_called
)
450 ventoy_initrd_called
= 0;
454 grub_snprintf(buf
, sizeof(buf
), "mem:%s:size:%s", grub_env_get("ventoy_cpio_addr"), grub_env_get("ventoy_cpio_size"));
456 ventoy_extra_initrd_list
[ventoy_extra_initrd_num
++] = grub_strdup(buf
);
458 file
= grub_env_get("vtoy_img_part_file");
461 ventoy_extra_initrd_list
[ventoy_extra_initrd_num
++] = grub_strdup(file
);
466 grub_printf("========== initrd list ==========\n");
467 for (i
= 0; i
< ventoy_extra_initrd_num
; i
++)
469 grub_printf("%s\n", ventoy_extra_initrd_list
[i
]);
471 grub_printf("=================================\n");
473 ventoy_debug_pause();
476 grub_cmd_initrd(NULL
, ventoy_extra_initrd_num
, ventoy_extra_initrd_list
);
481 static int ventoy_boot_opt_filter(char *opt
)
483 if (grub_strcmp(opt
, "noinitrd") == 0)
488 if (grub_strcmp(opt
, "vga=current") == 0)
493 if (grub_strncmp(opt
, "rdinit=", 7) == 0)
495 if (grub_strcmp(opt
, "rdinit=/vtoy/vtoy") != 0)
503 if (grub_strncmp(opt
, "init=", 5) == 0)
512 if (grub_strcmp(opt
, "quiet") == 0)
517 if (grub_strncmp(opt
, "loglevel=", 9) == 0)
522 if (grub_strcmp(opt
, "splash") == 0)
531 static int ventoy_bootopt_hook(int argc
, char *argv
[])
540 //grub_printf("ventoy_bootopt_hook: %d %d\n", argc, ventoy_linux_argc);
542 if (ventoy_linux_argc
== 0)
547 for (i
= 0; i
< argc
; i
++)
549 if (ventoy_boot_opt_filter(argv
[i
]))
554 ventoy_linux_args
[count
++] = grub_strdup(argv
[i
]);
557 for (i
= 0; i
< ventoy_linux_argc
; i
++)
559 ventoy_linux_args
[count
] = ventoy_linux_args
[i
+ (LINUX_MAX_ARGC
/ 2)];
560 ventoy_linux_args
[i
+ (LINUX_MAX_ARGC
/ 2)] = NULL
;
562 if (ventoy_linux_args
[count
][0] == '@')
564 env
= grub_env_get(ventoy_linux_args
[count
] + 1);
567 grub_free(ventoy_linux_args
[count
]);
569 newenv
= grub_strdup(env
);
576 if (*last
!= ' ' && *last
!= '\t')
588 for (pos
= last
; *pos
; pos
++)
590 if (*pos
== ' ' || *pos
== '\t')
594 if (0 == ventoy_boot_opt_filter(last
))
596 ventoy_linux_args
[count
++] = grub_strdup(last
);
605 if (0 == ventoy_boot_opt_filter(last
))
607 ventoy_linux_args
[count
++] = grub_strdup(last
);
628 ventoy_linux_args
[count
++] = grub_strdup("loglevel=7");
631 ventoy_linux_argc
= count
;
635 grub_printf("========== bootoption ==========\n");
636 for (i
= 0; i
< count
; i
++)
638 grub_printf("%s ", ventoy_linux_args
[i
]);
640 grub_printf("\n================================\n");
647 grub_cmd_set_boot_opt (grub_command_t cmd
__attribute__ ((unused
)),
648 int argc
, char *argv
[])
653 for (i
= 0; i
< argc
; i
++)
655 ventoy_linux_args
[ventoy_linux_argc
+ (LINUX_MAX_ARGC
/ 2) ] = grub_strdup(argv
[i
]);
659 vtdebug
= grub_env_get("vtdebug_flag");
660 if (vtdebug
&& vtdebug
[0])
665 if (ventoy_debug
) grub_printf("ventoy set boot opt %d\n", ventoy_linux_argc
);
671 grub_cmd_unset_boot_opt (grub_command_t cmd
__attribute__ ((unused
)),
672 int argc
, char *argv
[])
679 for (i
= 0; i
< LINUX_MAX_ARGC
; i
++)
681 if (ventoy_linux_args
[i
])
683 grub_free(ventoy_linux_args
[i
]);
688 ventoy_linux_argc
= 0;
689 ventoy_initrd_called
= 0;
690 grub_memset(ventoy_linux_args
, 0, sizeof(char *) * LINUX_MAX_ARGC
);
695 grub_cmd_extra_initrd_append (grub_command_t cmd
__attribute__ ((unused
)),
696 int argc
, char *argv
[])
708 for (pos
= argv
[0]; *pos
; pos
++)
718 /* grub2 newc bug workaround */
719 newclen
= (int)grub_strlen(end
+ 1);
720 if ((110 + newclen
) % 4 == 0)
722 grub_snprintf(buf
, sizeof(buf
), "newc:.%s:%s", end
+ 1, argv
[0]);
726 grub_snprintf(buf
, sizeof(buf
), "newc:%s:%s", end
+ 1, argv
[0]);
729 if (ventoy_extra_initrd_num
< 256)
731 ventoy_extra_initrd_list
[ventoy_extra_initrd_num
++] = grub_strdup(buf
);
739 grub_cmd_extra_initrd_reset (grub_command_t cmd
__attribute__ ((unused
)),
740 int argc
, char *argv
[])
747 for (i
= 0; i
< ventoy_extra_initrd_num
; i
++)
749 if (ventoy_extra_initrd_list
[i
])
751 grub_free(ventoy_extra_initrd_list
[i
]);
755 grub_memset(ventoy_extra_initrd_list
, 0, sizeof(ventoy_extra_initrd_list
));
762 grub_linux_boot (void)
767 struct grub_relocator32_state state
;
769 struct grub_linux_boot_ctx ctx
= {
770 .real_mode_target
= 0
772 grub_size_t mmap_size
;
773 grub_size_t cl_offset
;
777 #ifdef GRUB_MACHINE_IEEE1275
779 const char *bootpath
;
782 bootpath
= grub_env_get ("root");
784 grub_ieee1275_set_property (grub_ieee1275_chosen
,
785 "bootpath", bootpath
,
786 grub_strlen (bootpath
) + 1,
788 linux_params
.ofw_signature
= GRUB_LINUX_OFW_SIGNATURE
;
789 linux_params
.ofw_num_items
= 1;
790 linux_params
.ofw_cif_handler
= (grub_uint32_t
) grub_ieee1275_entry_fn
;
791 linux_params
.ofw_idt
= 0;
795 modevar
= grub_env_get ("gfxpayload");
797 /* Now all graphical modes are acceptable.
798 May change in future if we have modes without framebuffer. */
799 if (modevar
&& *modevar
!= 0)
801 tmp
= grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE
, modevar
);
804 #if ACCEPTS_PURE_TEXT
805 err
= grub_video_set_mode (tmp
, 0, 0);
807 err
= grub_video_set_mode (tmp
, GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
811 else /* We can't go back to text mode from coreboot fb. */
812 #ifdef GRUB_MACHINE_COREBOOT
813 if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT
)
818 #if ACCEPTS_PURE_TEXT
819 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
, 0, 0);
821 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
,
822 GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
829 grub_puts_ (N_("Booting in blind mode"));
830 grub_errno
= GRUB_ERR_NONE
;
833 if (grub_linux_setup_video (&linux_params
))
835 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
836 linux_params
.have_vga
= GRUB_VIDEO_LINUX_TYPE_TEXT
;
837 linux_params
.video_mode
= 0x3;
839 linux_params
.have_vga
= 0;
840 linux_params
.video_mode
= 0;
841 linux_params
.video_width
= 0;
842 linux_params
.video_height
= 0;
847 #ifndef GRUB_MACHINE_IEEE1275
848 if (linux_params
.have_vga
== GRUB_VIDEO_LINUX_TYPE_TEXT
)
851 grub_term_output_t term
;
853 FOR_ACTIVE_TERM_OUTPUTS(term
)
854 if (grub_strcmp (term
->name
, "vga_text") == 0
855 || grub_strcmp (term
->name
, "console") == 0
856 || grub_strcmp (term
->name
, "ofconsole") == 0)
858 struct grub_term_coordinate pos
= grub_term_getxy (term
);
859 linux_params
.video_cursor_x
= pos
.x
;
860 linux_params
.video_cursor_y
= pos
.y
;
861 linux_params
.video_width
= grub_term_width (term
);
862 linux_params
.video_height
= grub_term_height (term
);
868 linux_params
.video_cursor_x
= 0;
869 linux_params
.video_cursor_y
= 0;
870 linux_params
.video_width
= 80;
871 linux_params
.video_height
= 25;
875 #ifdef GRUB_KERNEL_USE_RSDP_ADDR
876 linux_params
.acpi_rsdp_addr
= grub_le_to_cpu64 (grub_rsdp_addr
);
879 mmap_size
= find_mmap_size ();
880 /* Make sure that each size is aligned to a page boundary. */
881 cl_offset
= ALIGN_UP (mmap_size
+ sizeof (linux_params
), 4096);
882 if (cl_offset
< ((grub_size_t
) linux_params
.setup_sects
<< GRUB_DISK_SECTOR_BITS
))
883 cl_offset
= ALIGN_UP ((grub_size_t
) (linux_params
.setup_sects
884 << GRUB_DISK_SECTOR_BITS
), 4096);
885 ctx
.real_size
= ALIGN_UP (cl_offset
+ maximal_cmdline_size
, 4096);
887 #ifdef GRUB_MACHINE_EFI
888 efi_mmap_size
= grub_efi_find_mmap_size ();
889 if (efi_mmap_size
== 0)
893 grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
894 (unsigned) ctx
.real_size
, (unsigned) mmap_size
);
896 #ifdef GRUB_MACHINE_EFI
897 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 1);
898 if (! ctx
.real_mode_target
)
899 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 0);
901 grub_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
);
903 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
904 (unsigned long) ctx
.real_mode_target
,
905 (unsigned) ctx
.real_size
,
906 (unsigned) efi_mmap_size
);
908 if (! ctx
.real_mode_target
)
909 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate real mode pages");
912 grub_relocator_chunk_t ch
;
913 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
914 ctx
.real_mode_target
,
915 (ctx
.real_size
+ efi_mmap_size
));
918 real_mode_mem
= get_virtual_current_address (ch
);
920 efi_mmap_buf
= (grub_uint8_t
*) real_mode_mem
+ ctx
.real_size
;
922 grub_dprintf ("linux", "real_mode_mem = %p\n",
925 ctx
.params
= real_mode_mem
;
927 *ctx
.params
= linux_params
;
928 ctx
.params
->cmd_line_ptr
= ctx
.real_mode_target
+ cl_offset
;
929 grub_memcpy ((char *) ctx
.params
+ cl_offset
, linux_cmdline
,
930 maximal_cmdline_size
);
932 grub_dprintf ("linux", "code32_start = %x\n",
933 (unsigned) ctx
.params
->code32_start
);
936 if (grub_mmap_iterate (grub_linux_boot_mmap_fill
, &ctx
))
938 ctx
.params
->mmap_size
= ctx
.e820_num
;
940 #ifdef GRUB_MACHINE_EFI
942 grub_efi_uintn_t efi_desc_size
;
943 grub_size_t efi_mmap_target
;
944 grub_efi_uint32_t efi_desc_version
;
945 err
= grub_efi_finish_boot_services (&efi_mmap_size
, efi_mmap_buf
, NULL
,
946 &efi_desc_size
, &efi_desc_version
);
950 /* Note that no boot services are available from here. */
951 efi_mmap_target
= ctx
.real_mode_target
952 + ((grub_uint8_t
*) efi_mmap_buf
- (grub_uint8_t
*) real_mode_mem
);
953 /* Pass EFI parameters. */
954 if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0208)
956 ctx
.params
->v0208
.efi_mem_desc_size
= efi_desc_size
;
957 ctx
.params
->v0208
.efi_mem_desc_version
= efi_desc_version
;
958 ctx
.params
->v0208
.efi_mmap
= efi_mmap_target
;
959 ctx
.params
->v0208
.efi_mmap_size
= efi_mmap_size
;
962 ctx
.params
->v0208
.efi_mmap_hi
= (efi_mmap_target
>> 32);
965 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0206)
967 ctx
.params
->v0206
.efi_mem_desc_size
= efi_desc_size
;
968 ctx
.params
->v0206
.efi_mem_desc_version
= efi_desc_version
;
969 ctx
.params
->v0206
.efi_mmap
= efi_mmap_target
;
970 ctx
.params
->v0206
.efi_mmap_size
= efi_mmap_size
;
972 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0204)
974 ctx
.params
->v0204
.efi_mem_desc_size
= efi_desc_size
;
975 ctx
.params
->v0204
.efi_mem_desc_version
= efi_desc_version
;
976 ctx
.params
->v0204
.efi_mmap
= efi_mmap_target
;
977 ctx
.params
->v0204
.efi_mmap_size
= efi_mmap_size
;
983 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
984 state
.ebp
= state
.edi
= state
.ebx
= 0;
985 state
.esi
= ctx
.real_mode_target
;
986 state
.esp
= ctx
.real_mode_target
;
987 state
.eip
= ctx
.params
->code32_start
;
988 return grub_relocator32_boot (relocator
, state
, 0);
992 grub_linux_unload (void)
994 grub_dl_unref (my_mod
);
996 grub_free (linux_cmdline
);
998 return GRUB_ERR_NONE
;
1002 grub_cmd_linux (grub_command_t cmd
__attribute__ ((unused
)),
1003 int argc
, char *argv
[])
1005 grub_file_t file
= 0;
1006 struct linux_i386_kernel_header lh
;
1007 grub_uint8_t setup_sects
;
1008 grub_size_t real_size
, prot_size
, prot_file_size
;
1011 grub_size_t align
, min_align
;
1013 grub_uint64_t preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
1015 grub_dl_ref (my_mod
);
1019 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
1023 file
= grub_file_open (argv
[0], GRUB_FILE_TYPE_LINUX_KERNEL
);
1027 if (ventoy_linux_argc
)
1029 const char *tip
= grub_env_get("ventoy_loading_tip");
1032 grub_printf("%s\n", tip
);
1037 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
1040 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1045 if (lh
.boot_flag
!= grub_cpu_to_le16_compile_time (0xaa55))
1047 grub_error (GRUB_ERR_BAD_OS
, "invalid magic number");
1051 if (lh
.setup_sects
> GRUB_LINUX_MAX_SETUP_SECTS
)
1053 grub_error (GRUB_ERR_BAD_OS
, "too many setup sectors");
1057 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
1058 still not support 32-bit boot. */
1059 if (lh
.header
!= grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE
)
1060 || grub_le_to_cpu16 (lh
.version
) < 0x0203)
1062 grub_error (GRUB_ERR_BAD_OS
, "version too old for 32-bit boot"
1063 #ifdef GRUB_MACHINE_PCBIOS
1064 " (try with `linux16')"
1070 if (! (lh
.loadflags
& GRUB_LINUX_FLAG_BIG_KERNEL
))
1072 grub_error (GRUB_ERR_BAD_OS
, "zImage doesn't support 32-bit boot"
1073 #ifdef GRUB_MACHINE_PCBIOS
1074 " (try with `linux16')"
1080 if (grub_le_to_cpu16 (lh
.version
) >= 0x0206)
1081 maximal_cmdline_size
= grub_le_to_cpu32 (lh
.cmdline_size
) + 1;
1083 maximal_cmdline_size
= 256;
1085 if (maximal_cmdline_size
< 128)
1086 maximal_cmdline_size
= 128;
1088 setup_sects
= lh
.setup_sects
;
1090 /* If SETUP_SECTS is not set, set it to the default (4). */
1092 setup_sects
= GRUB_LINUX_DEFAULT_SETUP_SECTS
;
1094 real_size
= setup_sects
<< GRUB_DISK_SECTOR_BITS
;
1095 prot_file_size
= grub_file_size (file
) - real_size
- GRUB_DISK_SECTOR_SIZE
;
1097 if (grub_le_to_cpu16 (lh
.version
) >= 0x205
1098 && lh
.kernel_alignment
!= 0
1099 && ((lh
.kernel_alignment
- 1) & lh
.kernel_alignment
) == 0)
1101 for (align
= 0; align
< 32; align
++)
1102 if (grub_le_to_cpu32 (lh
.kernel_alignment
) & (1 << align
))
1104 relocatable
= grub_le_to_cpu32 (lh
.relocatable
);
1112 if (grub_le_to_cpu16 (lh
.version
) >= 0x020a)
1114 min_align
= lh
.min_alignment
;
1115 prot_size
= grub_le_to_cpu32 (lh
.init_size
);
1116 prot_init_space
= page_align (prot_size
);
1118 preferred_address
= grub_le_to_cpu64 (lh
.pref_address
);
1120 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
1125 prot_size
= prot_file_size
;
1126 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
1127 /* Usually, the compression ratio is about 50%. */
1128 prot_init_space
= page_align (prot_size
) * 3;
1131 if (allocate_pages (prot_size
, &align
,
1132 min_align
, relocatable
,
1136 grub_memset (&linux_params
, 0, sizeof (linux_params
));
1137 grub_memcpy (&linux_params
.setup_sects
, &lh
.setup_sects
, sizeof (lh
) - 0x1F1);
1139 linux_params
.code32_start
= prot_mode_target
+ lh
.code32_start
- GRUB_LINUX_BZIMAGE_ADDR
;
1140 linux_params
.kernel_alignment
= (1 << align
);
1141 linux_params
.ps_mouse
= linux_params
.padding10
= 0;
1144 * The Linux 32-bit boot protocol defines the setup header end
1145 * to be at 0x202 + the byte value at 0x201.
1147 len
= 0x202 + *((char *) &linux_params
.jump
+ 1);
1149 /* Verify the struct is big enough so we do not write past the end. */
1150 if (len
> (char *) &linux_params
.edd_mbr_sig_buffer
- (char *) &linux_params
) {
1151 grub_error (GRUB_ERR_BAD_OS
, "Linux setup header too big");
1155 /* We've already read lh so there is no need to read it second time. */
1158 if (grub_file_read (file
, (char *) &linux_params
+ sizeof (lh
), len
) != len
)
1161 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1166 linux_params
.type_of_loader
= GRUB_LINUX_BOOT_LOADER_TYPE
;
1168 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
1169 and otherwise ignored. */
1170 linux_params
.cl_magic
= GRUB_LINUX_CL_MAGIC
;
1171 linux_params
.cl_offset
= 0x1000;
1173 linux_params
.ramdisk_image
= 0;
1174 linux_params
.ramdisk_size
= 0;
1176 linux_params
.heap_end_ptr
= GRUB_LINUX_HEAP_END_OFFSET
;
1177 linux_params
.loadflags
|= GRUB_LINUX_FLAG_CAN_USE_HEAP
;
1179 /* These are not needed to be precise, because Linux uses these values
1180 only to raise an error when the decompression code cannot find good
1182 linux_params
.ext_mem
= ((32 * 0x100000) >> 10);
1183 linux_params
.alt_mem
= ((32 * 0x100000) >> 10);
1185 /* Ignored by Linux. */
1186 linux_params
.video_page
= 0;
1188 /* Only used when `video_mode == 0x7', otherwise ignored. */
1189 linux_params
.video_ega_bx
= 0;
1191 linux_params
.font_size
= 16; /* XXX */
1193 #ifdef GRUB_MACHINE_EFI
1195 if (grub_le_to_cpu16 (linux_params
.version
) < 0x0208 &&
1196 ((grub_addr_t
) grub_efi_system_table
>> 32) != 0)
1197 return grub_error(GRUB_ERR_BAD_OS
,
1198 "kernel does not support 64-bit addressing");
1201 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0208)
1203 linux_params
.v0208
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
1204 linux_params
.v0208
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
1206 linux_params
.v0208
.efi_system_table_hi
= (grub_uint32_t
) ((grub_uint64_t
) grub_efi_system_table
>> 32);
1209 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0206)
1211 linux_params
.v0206
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
1212 linux_params
.v0206
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
1214 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0204)
1216 linux_params
.v0204
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE_0204
;
1217 linux_params
.v0204
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
1221 /* The other parameters are filled when booting. */
1223 grub_file_seek (file
, real_size
+ GRUB_DISK_SECTOR_SIZE
);
1225 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
1226 (unsigned) real_size
, (unsigned) prot_size
);
1228 /* Look for memory size and video mode specified on the command line. */
1230 for (i
= 1; i
< argc
; i
++)
1231 #ifdef GRUB_MACHINE_PCBIOS
1232 if (grub_memcmp (argv
[i
], "vga=", 4) == 0 && (grub_memcmp (argv
[i
], "vga=current", 11) != 0))
1234 /* Video mode selection support. */
1235 char *val
= argv
[i
] + 4;
1236 unsigned vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
1237 struct grub_vesa_mode_table_entry
*linux_mode
;
1241 grub_dl_load ("vbe");
1243 if (grub_strcmp (val
, "normal") == 0)
1244 vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
1245 else if (grub_strcmp (val
, "ext") == 0)
1246 vid_mode
= GRUB_LINUX_VID_MODE_EXTENDED
;
1247 else if (grub_strcmp (val
, "ask") == 0)
1249 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
1251 /* We usually would never do this in a loader, but "vga=ask" means user
1252 requested interaction, so it can't hurt to request keyboard input. */
1253 grub_wait_after_message ();
1258 vid_mode
= (grub_uint16_t
) grub_strtoul (val
, 0, 0);
1263 case GRUB_LINUX_VID_MODE_NORMAL
:
1264 grub_env_set ("gfxpayload", "text");
1265 grub_printf_ (N_("%s is deprecated. "
1266 "Use set gfxpayload=%s before "
1267 "linux command instead.\n"),
1272 case GRUB_LINUX_VID_MODE_EXTENDED
:
1273 /* FIXME: support 80x50 text. */
1274 grub_env_set ("gfxpayload", "text");
1275 grub_printf_ (N_("%s is deprecated. "
1276 "Use set gfxpayload=%s before "
1277 "linux command instead.\n"),
1281 /* Ignore invalid values. */
1282 if (vid_mode
< GRUB_VESA_MODE_TABLE_START
||
1283 vid_mode
> GRUB_VESA_MODE_TABLE_END
)
1285 grub_env_set ("gfxpayload", "text");
1286 /* TRANSLATORS: "x" has to be entered in, like an identifier,
1287 so please don't use better Unicode codepoints. */
1288 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
1289 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
1290 "before linux command instead.\n"),
1295 linux_mode
= &grub_vesa_mode_table
[vid_mode
1296 - GRUB_VESA_MODE_TABLE_START
];
1298 buf
= grub_xasprintf ("%ux%ux%u,%ux%u",
1299 linux_mode
->width
, linux_mode
->height
,
1301 linux_mode
->width
, linux_mode
->height
);
1305 grub_printf_ (N_("%s is deprecated. "
1306 "Use set gfxpayload=%s before "
1307 "linux command instead.\n"),
1309 err
= grub_env_set ("gfxpayload", buf
);
1316 #endif /* GRUB_MACHINE_PCBIOS */
1317 if (grub_memcmp (argv
[i
], "mem=", 4) == 0)
1319 char *val
= argv
[i
] + 4;
1321 linux_mem_size
= grub_strtoul (val
, &val
, 0);
1325 grub_errno
= GRUB_ERR_NONE
;
1332 switch (grub_tolower (val
[0]))
1347 /* Check an overflow. */
1348 if (linux_mem_size
> (~0UL >> shift
))
1351 linux_mem_size
<<= shift
;
1354 else if (grub_memcmp (argv
[i
], "quiet", sizeof ("quiet") - 1) == 0)
1356 linux_params
.loadflags
|= GRUB_LINUX_FLAG_QUIET
;
1359 /* Create kernel command line. */
1360 linux_cmdline
= grub_zalloc (maximal_cmdline_size
+ 1);
1363 grub_memcpy (linux_cmdline
, LINUX_IMAGE
, sizeof (LINUX_IMAGE
));
1367 if (ventoy_linux_argc
)
1369 ventoy_bootopt_hook(argc
, argv
);
1370 err
= grub_create_loader_cmdline (ventoy_linux_argc
, ventoy_linux_args
,
1372 + sizeof (LINUX_IMAGE
) - 1,
1373 maximal_cmdline_size
1374 - (sizeof (LINUX_IMAGE
) - 1),
1375 GRUB_VERIFY_KERNEL_CMDLINE
);
1379 err
= grub_create_loader_cmdline (argc
, argv
,
1381 + sizeof (LINUX_IMAGE
) - 1,
1382 maximal_cmdline_size
1383 - (sizeof (LINUX_IMAGE
) - 1),
1384 GRUB_VERIFY_KERNEL_CMDLINE
);
1391 len
= prot_file_size
;
1392 if (grub_file_read (file
, prot_mode_mem
, len
) != len
&& !grub_errno
)
1393 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1396 if (grub_errno
== GRUB_ERR_NONE
)
1398 grub_loader_set (grub_linux_boot
, grub_linux_unload
,
1399 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
1406 grub_file_close (file
);
1408 if (grub_errno
!= GRUB_ERR_NONE
)
1410 grub_dl_unref (my_mod
);
1418 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1419 int argc
, char *argv
[])
1421 grub_size_t size
= 0, aligned_size
= 0;
1422 grub_addr_t addr_min
, addr_max
;
1425 struct grub_linux_initrd_context initrd_ctx
= { 0, 0, 0 };
1429 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
1435 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("you need to load the kernel first"));
1439 if (grub_initrd_init (argc
, argv
, &initrd_ctx
))
1442 size
= grub_get_initrd_size (&initrd_ctx
);
1443 aligned_size
= ALIGN_UP (size
, 4096);
1445 /* Get the highest address available for the initrd. */
1446 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0203)
1448 addr_max
= grub_cpu_to_le32 (linux_params
.initrd_addr_max
);
1450 /* XXX in reality, Linux specifies a bogus value, so
1451 it is necessary to make sure that ADDR_MAX does not exceed
1453 if (addr_max
> GRUB_LINUX_INITRD_MAX_ADDRESS
)
1454 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1457 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1459 if (linux_mem_size
!= 0 && linux_mem_size
< addr_max
)
1460 addr_max
= linux_mem_size
;
1462 /* Linux 2.3.xx has a bug in the memory range check, so avoid
1464 Linux 2.2.xx has a bug in the memory range check, which is
1465 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
1466 addr_max
-= 0x10000;
1468 addr_min
= (grub_addr_t
) prot_mode_target
+ prot_init_space
;
1470 /* Put the initrd as high as possible, 4KiB aligned. */
1471 addr
= (addr_max
- aligned_size
) & ~0xFFF;
1473 if (addr
< addr_min
)
1475 grub_error (GRUB_ERR_OUT_OF_RANGE
, "the initrd is too big");
1480 grub_relocator_chunk_t ch
;
1481 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
1482 addr_min
, addr
, aligned_size
,
1484 GRUB_RELOCATOR_PREFERENCE_HIGH
,
1488 initrd_mem
= get_virtual_current_address (ch
);
1489 initrd_mem_target
= get_physical_target_address (ch
);
1492 if (grub_initrd_load (&initrd_ctx
, argv
, initrd_mem
))
1495 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1496 (unsigned) addr
, (unsigned) size
);
1498 linux_params
.ramdisk_image
= initrd_mem_target
;
1499 linux_params
.ramdisk_size
= size
;
1500 linux_params
.root_dev
= 0x0100; /* XXX */
1503 grub_initrd_close (&initrd_ctx
);
1509 ventoy_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1510 int argc
, char *argv
[])
1516 if (ventoy_debug
) grub_printf("ventoy_cmd_initrd %d\n", ventoy_linux_argc
);
1518 if (ventoy_linux_argc
== 0)
1520 return grub_cmd_initrd(cmd
, argc
, argv
);
1523 grub_snprintf(buf
, sizeof(buf
), "mem:%s:size:%s", grub_env_get("ventoy_cpio_addr"), grub_env_get("ventoy_cpio_size"));
1525 if (ventoy_debug
) grub_printf("membuf=%s\n", buf
);
1527 ventoy_extra_initrd_list
[ventoy_extra_initrd_num
++] = grub_strdup(buf
);
1529 file
= grub_env_get("vtoy_img_part_file");
1532 ventoy_extra_initrd_list
[ventoy_extra_initrd_num
++] = grub_strdup(file
);
1535 for (i
= 0; i
< argc
; i
++)
1537 ventoy_extra_initrd_list
[ventoy_extra_initrd_num
++] = grub_strdup(argv
[i
]);
1540 ventoy_initrd_called
= 1;
1544 grub_printf("========== initrd list ==========\n");
1545 for (i
= 0; i
< ventoy_extra_initrd_num
; i
++)
1547 grub_printf("%s\n", ventoy_extra_initrd_list
[i
]);
1549 grub_printf("=================================\n");
1552 return grub_cmd_initrd(cmd
, ventoy_extra_initrd_num
, ventoy_extra_initrd_list
);
1556 static grub_command_t cmd_linux
, cmd_initrd
, cmd_linuxefi
, cmd_initrdefi
;
1557 static grub_command_t cmd_set_bootopt
, cmd_unset_bootopt
, cmd_extra_initrd_append
, cmd_extra_initrd_reset
;
1559 GRUB_MOD_INIT(linux
)
1561 cmd_linux
= grub_register_command ("linux", grub_cmd_linux
,
1562 0, N_("Load Linux."));
1563 cmd_initrd
= grub_register_command ("initrd", ventoy_cmd_initrd
,
1564 0, N_("Load initrd."));
1566 cmd_linuxefi
= grub_register_command ("linuxefi", grub_cmd_linux
,
1567 0, N_("Load Linux."));
1568 cmd_initrdefi
= grub_register_command ("initrdefi", ventoy_cmd_initrd
,
1569 0, N_("Load initrd."));
1570 cmd_set_bootopt
= grub_register_command ("vt_set_boot_opt", grub_cmd_set_boot_opt
, 0, N_("set ext boot opt"));
1571 cmd_unset_bootopt
= grub_register_command ("vt_unset_boot_opt", grub_cmd_unset_boot_opt
, 0, N_("unset ext boot opt"));
1573 cmd_extra_initrd_append
= grub_register_command ("vt_img_extra_initrd_append", grub_cmd_extra_initrd_append
, 0, N_(""));
1574 cmd_extra_initrd_reset
= grub_register_command ("vt_img_extra_initrd_reset", grub_cmd_extra_initrd_reset
, 0, N_(""));
1576 ventoy_linux_args
= grub_zalloc(sizeof(char *) * LINUX_MAX_ARGC
);
1581 GRUB_MOD_FINI(linux
)
1583 grub_unregister_command (cmd_linux
);
1584 grub_unregister_command (cmd_initrd
);