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
;
90 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)), int argc
, char *argv
[]);
100 static struct idt_descriptor idt_desc
=
107 static inline grub_size_t
108 page_align (grub_size_t size
)
110 return (size
+ (1 << 12) - 1) & (~((1 << 12) - 1));
113 /* Helper for find_mmap_size. */
115 count_hook (grub_uint64_t addr
__attribute__ ((unused
)),
116 grub_uint64_t size
__attribute__ ((unused
)),
117 grub_memory_type_t type
__attribute__ ((unused
)), void *data
)
119 grub_size_t
*count
= data
;
125 /* Find the optimal number of pages for the memory map. */
127 find_mmap_size (void)
129 grub_size_t count
= 0, mmap_size
;
131 grub_mmap_iterate (count_hook
, &count
);
133 mmap_size
= count
* sizeof (struct grub_e820_mmap
);
135 /* Increase the size a bit for safety, because GRUB allocates more on
137 mmap_size
+= (1 << 12);
139 return page_align (mmap_size
);
145 grub_relocator_unload (relocator
);
147 prot_mode_mem
= initrd_mem
= 0;
148 prot_mode_target
= initrd_mem_target
= 0;
151 /* Allocate pages for the real mode code and the protected mode code
152 for linux as well as a memory map buffer. */
154 allocate_pages (grub_size_t prot_size
, grub_size_t
*align
,
155 grub_size_t min_align
, int relocatable
,
156 grub_uint64_t preferred_address
)
163 prot_size
= page_align (prot_size
);
165 /* Initialize the memory pointers with NULL for convenience. */
168 relocator
= grub_relocator_new ();
175 /* FIXME: Should request low memory from the heap when this feature is
179 grub_relocator_chunk_t ch
;
182 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
186 GRUB_RELOCATOR_PREFERENCE_LOW
,
188 for (; err
&& *align
+ 1 > min_align
; (*align
)--)
190 grub_errno
= GRUB_ERR_NONE
;
191 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
193 0xffffffff & ~prot_size
,
194 prot_size
, 1 << *align
,
195 GRUB_RELOCATOR_PREFERENCE_LOW
,
202 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
207 prot_mode_mem
= get_virtual_current_address (ch
);
208 prot_mode_target
= get_physical_target_address (ch
);
211 grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
212 prot_mode_mem
, (unsigned long) prot_mode_target
,
213 (unsigned) prot_size
);
214 return GRUB_ERR_NONE
;
222 grub_e820_add_region (struct grub_e820_mmap
*e820_map
, int *e820_num
,
223 grub_uint64_t start
, grub_uint64_t size
,
228 if ((n
> 0) && (e820_map
[n
- 1].addr
+ e820_map
[n
- 1].size
== start
) &&
229 (e820_map
[n
- 1].type
== type
))
230 e820_map
[n
- 1].size
+= size
;
233 e820_map
[n
].addr
= start
;
234 e820_map
[n
].size
= size
;
235 e820_map
[n
].type
= type
;
238 return GRUB_ERR_NONE
;
242 grub_linux_setup_video (struct linux_kernel_params
*params
)
244 struct grub_video_mode_info mode_info
;
247 grub_video_driver_id_t driver_id
;
248 const char *gfxlfbvar
= grub_env_get ("gfxpayloadforcelfb");
250 driver_id
= grub_video_get_driver_id ();
252 if (driver_id
== GRUB_VIDEO_DRIVER_NONE
)
255 err
= grub_video_get_info_and_fini (&mode_info
, &framebuffer
);
259 grub_errno
= GRUB_ERR_NONE
;
263 params
->lfb_width
= mode_info
.width
;
264 params
->lfb_height
= mode_info
.height
;
265 params
->lfb_depth
= mode_info
.bpp
;
266 params
->lfb_line_len
= mode_info
.pitch
;
268 params
->lfb_base
= (grub_size_t
) framebuffer
;
270 #if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
271 params
->ext_lfb_base
= (grub_size_t
) (((grub_uint64_t
)(grub_size_t
) framebuffer
) >> 32);
272 params
->capabilities
|= VIDEO_CAPABILITY_64BIT_BASE
;
275 params
->lfb_size
= ALIGN_UP (params
->lfb_line_len
* params
->lfb_height
, 65536);
277 params
->red_mask_size
= mode_info
.red_mask_size
;
278 params
->red_field_pos
= mode_info
.red_field_pos
;
279 params
->green_mask_size
= mode_info
.green_mask_size
;
280 params
->green_field_pos
= mode_info
.green_field_pos
;
281 params
->blue_mask_size
= mode_info
.blue_mask_size
;
282 params
->blue_field_pos
= mode_info
.blue_field_pos
;
283 params
->reserved_mask_size
= mode_info
.reserved_mask_size
;
284 params
->reserved_field_pos
= mode_info
.reserved_field_pos
;
286 if (gfxlfbvar
&& (gfxlfbvar
[0] == '1' || gfxlfbvar
[0] == 'y'))
287 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
292 case GRUB_VIDEO_DRIVER_VBE
:
293 params
->lfb_size
>>= 16;
294 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_VESA
;
297 case GRUB_VIDEO_DRIVER_EFI_UGA
:
298 case GRUB_VIDEO_DRIVER_EFI_GOP
:
299 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_EFIFB
;
302 /* FIXME: check if better id is available. */
303 case GRUB_VIDEO_DRIVER_SM712
:
304 case GRUB_VIDEO_DRIVER_SIS315PRO
:
305 case GRUB_VIDEO_DRIVER_VGA
:
306 case GRUB_VIDEO_DRIVER_CIRRUS
:
307 case GRUB_VIDEO_DRIVER_BOCHS
:
308 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E
:
309 case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A
:
310 case GRUB_VIDEO_DRIVER_IEEE1275
:
311 case GRUB_VIDEO_DRIVER_COREBOOT
:
312 /* Make gcc happy. */
313 case GRUB_VIDEO_DRIVER_XEN
:
314 case GRUB_VIDEO_DRIVER_SDL
:
315 case GRUB_VIDEO_DRIVER_NONE
:
316 case GRUB_VIDEO_ADAPTER_CAPTURE
:
317 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
322 #ifdef GRUB_MACHINE_PCBIOS
323 /* VESA packed modes may come with zeroed mask sizes, which need
324 to be set here according to DAC Palette width. If we don't,
325 this results in Linux displaying a black screen. */
326 if (driver_id
== GRUB_VIDEO_DRIVER_VBE
&& mode_info
.bpp
<= 8)
328 struct grub_vbe_info_block controller_info
;
332 status
= grub_vbe_bios_get_controller_info (&controller_info
);
334 if (status
== GRUB_VBE_STATUS_OK
&&
335 (controller_info
.capabilities
& GRUB_VBE_CAPABILITY_DACWIDTH
))
336 status
= grub_vbe_bios_set_dac_palette_width (&width
);
338 if (status
!= GRUB_VBE_STATUS_OK
)
339 /* 6 is default after mode reset. */
342 params
->red_mask_size
= params
->green_mask_size
343 = params
->blue_mask_size
= width
;
344 params
->reserved_mask_size
= 0;
348 return GRUB_ERR_NONE
;
351 /* Context for grub_linux_boot. */
352 struct grub_linux_boot_ctx
354 grub_addr_t real_mode_target
;
355 grub_size_t real_size
;
356 struct linux_kernel_params
*params
;
360 /* Helper for grub_linux_boot. */
362 grub_linux_boot_mmap_find (grub_uint64_t addr
, grub_uint64_t size
,
363 grub_memory_type_t type
, void *data
)
365 struct grub_linux_boot_ctx
*ctx
= data
;
367 /* We must put real mode code in the traditional space. */
368 if (type
!= GRUB_MEMORY_AVAILABLE
|| addr
> 0x90000)
371 if (addr
+ size
< 0x10000)
376 size
+= addr
- 0x10000;
380 if (addr
+ size
> 0x90000)
381 size
= 0x90000 - addr
;
383 if (ctx
->real_size
+ efi_mmap_size
> size
)
386 grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
387 (unsigned long) addr
,
389 (unsigned) (ctx
->real_size
+ efi_mmap_size
));
390 ctx
->real_mode_target
= ((addr
+ size
) - (ctx
->real_size
+ efi_mmap_size
));
394 /* GRUB types conveniently match E820 types. */
396 grub_linux_boot_mmap_fill (grub_uint64_t addr
, grub_uint64_t size
,
397 grub_memory_type_t type
, void *data
)
399 struct grub_linux_boot_ctx
*ctx
= data
;
401 if (grub_e820_add_region (ctx
->params
->e820_map
, &ctx
->e820_num
,
408 static void ventoy_debug_pause(void)
412 if (0 == ventoy_debug
)
417 grub_printf("press Enter to continue ......\n");
421 if (key
== '\n' || key
== '\r')
428 static int ventoy_preboot(void)
435 grub_printf("ventoy_preboot %d %d\n", ventoy_linux_argc
, ventoy_initrd_called
);
436 ventoy_debug_pause();
439 if (ventoy_linux_argc
== 0)
444 if (ventoy_initrd_called
)
446 ventoy_initrd_called
= 0;
450 grub_snprintf(buf
, sizeof(buf
), "mem:%s:size:%s", grub_env_get("ventoy_cpio_addr"), grub_env_get("ventoy_cpio_size"));
454 grub_cmd_initrd(NULL
, 1, argv
);
458 grub_printf("add initrd %s\n", buf
);
459 ventoy_debug_pause();
465 static int ventoy_boot_opt_filter(char *opt
)
467 if (grub_strcmp(opt
, "noinitrd") == 0)
472 if (grub_strncmp(opt
, "rdinit=", 7) == 0)
474 if (grub_strcmp(opt
, "rdinit=/vtoy/vtoy") != 0)
484 if (grub_strcmp(opt
, "quiet") == 0)
489 if (grub_strncmp(opt
, "loglevel=", 9) == 0)
494 if (grub_strcmp(opt
, "splash") == 0)
503 static int ventoy_bootopt_hook(int argc
, char *argv
[])
512 //grub_printf("ventoy_bootopt_hook: %d %d\n", argc, ventoy_linux_argc);
514 if (ventoy_linux_argc
== 0)
519 for (i
= 0; i
< argc
; i
++)
521 if (ventoy_boot_opt_filter(argv
[i
]))
526 ventoy_linux_args
[count
++] = grub_strdup(argv
[i
]);
529 for (i
= 0; i
< ventoy_linux_argc
; i
++)
531 ventoy_linux_args
[count
] = ventoy_linux_args
[i
+ (LINUX_MAX_ARGC
/ 2)];
532 ventoy_linux_args
[i
+ (LINUX_MAX_ARGC
/ 2)] = NULL
;
534 if (ventoy_linux_args
[count
][0] == '@')
536 env
= grub_env_get(ventoy_linux_args
[count
] + 1);
539 grub_free(ventoy_linux_args
[count
]);
541 newenv
= grub_strdup(env
);
548 if (*last
!= ' ' && *last
!= '\t')
560 for (pos
= last
; *pos
; pos
++)
562 if (*pos
== ' ' || *pos
== '\t')
566 if (0 == ventoy_boot_opt_filter(last
))
568 ventoy_linux_args
[count
++] = grub_strdup(last
);
577 if (0 == ventoy_boot_opt_filter(last
))
579 ventoy_linux_args
[count
++] = grub_strdup(last
);
600 ventoy_linux_args
[count
++] = grub_strdup("loglevel=10");
603 ventoy_linux_argc
= count
;
607 grub_printf("========== bootoption ==========\n");
608 for (i
= 0; i
< count
; i
++)
610 grub_printf("%s ", ventoy_linux_args
[i
]);
612 grub_printf("\n================================\n");
619 grub_cmd_set_boot_opt (grub_command_t cmd
__attribute__ ((unused
)),
620 int argc
, char *argv
[])
625 for (i
= 0; i
< argc
; i
++)
627 ventoy_linux_args
[ventoy_linux_argc
+ (LINUX_MAX_ARGC
/ 2) ] = grub_strdup(argv
[i
]);
631 vtdebug
= grub_env_get("vtdebug_flag");
632 if (vtdebug
&& vtdebug
[0])
637 if (ventoy_debug
) grub_printf("ventoy set boot opt %d\n", ventoy_linux_argc
);
643 grub_cmd_unset_boot_opt (grub_command_t cmd
__attribute__ ((unused
)),
644 int argc
, char *argv
[])
651 for (i
= 0; i
< LINUX_MAX_ARGC
; i
++)
653 if (ventoy_linux_args
[i
])
655 grub_free(ventoy_linux_args
[i
]);
660 ventoy_linux_argc
= 0;
661 ventoy_initrd_called
= 0;
662 grub_memset(ventoy_linux_args
, 0, sizeof(char *) * LINUX_MAX_ARGC
);
668 grub_linux_boot (void)
673 struct grub_relocator32_state state
;
675 struct grub_linux_boot_ctx ctx
= {
676 .real_mode_target
= 0
678 grub_size_t mmap_size
;
679 grub_size_t cl_offset
;
683 #ifdef GRUB_MACHINE_IEEE1275
685 const char *bootpath
;
688 bootpath
= grub_env_get ("root");
690 grub_ieee1275_set_property (grub_ieee1275_chosen
,
691 "bootpath", bootpath
,
692 grub_strlen (bootpath
) + 1,
694 linux_params
.ofw_signature
= GRUB_LINUX_OFW_SIGNATURE
;
695 linux_params
.ofw_num_items
= 1;
696 linux_params
.ofw_cif_handler
= (grub_uint32_t
) grub_ieee1275_entry_fn
;
697 linux_params
.ofw_idt
= 0;
701 modevar
= grub_env_get ("gfxpayload");
703 /* Now all graphical modes are acceptable.
704 May change in future if we have modes without framebuffer. */
705 if (modevar
&& *modevar
!= 0)
707 tmp
= grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE
, modevar
);
710 #if ACCEPTS_PURE_TEXT
711 err
= grub_video_set_mode (tmp
, 0, 0);
713 err
= grub_video_set_mode (tmp
, GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
717 else /* We can't go back to text mode from coreboot fb. */
718 #ifdef GRUB_MACHINE_COREBOOT
719 if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT
)
724 #if ACCEPTS_PURE_TEXT
725 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
, 0, 0);
727 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
,
728 GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
735 grub_puts_ (N_("Booting in blind mode"));
736 grub_errno
= GRUB_ERR_NONE
;
739 if (grub_linux_setup_video (&linux_params
))
741 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
742 linux_params
.have_vga
= GRUB_VIDEO_LINUX_TYPE_TEXT
;
743 linux_params
.video_mode
= 0x3;
745 linux_params
.have_vga
= 0;
746 linux_params
.video_mode
= 0;
747 linux_params
.video_width
= 0;
748 linux_params
.video_height
= 0;
753 #ifndef GRUB_MACHINE_IEEE1275
754 if (linux_params
.have_vga
== GRUB_VIDEO_LINUX_TYPE_TEXT
)
757 grub_term_output_t term
;
759 FOR_ACTIVE_TERM_OUTPUTS(term
)
760 if (grub_strcmp (term
->name
, "vga_text") == 0
761 || grub_strcmp (term
->name
, "console") == 0
762 || grub_strcmp (term
->name
, "ofconsole") == 0)
764 struct grub_term_coordinate pos
= grub_term_getxy (term
);
765 linux_params
.video_cursor_x
= pos
.x
;
766 linux_params
.video_cursor_y
= pos
.y
;
767 linux_params
.video_width
= grub_term_width (term
);
768 linux_params
.video_height
= grub_term_height (term
);
774 linux_params
.video_cursor_x
= 0;
775 linux_params
.video_cursor_y
= 0;
776 linux_params
.video_width
= 80;
777 linux_params
.video_height
= 25;
781 #ifdef GRUB_KERNEL_USE_RSDP_ADDR
782 linux_params
.acpi_rsdp_addr
= grub_le_to_cpu64 (grub_rsdp_addr
);
785 mmap_size
= find_mmap_size ();
786 /* Make sure that each size is aligned to a page boundary. */
787 cl_offset
= ALIGN_UP (mmap_size
+ sizeof (linux_params
), 4096);
788 if (cl_offset
< ((grub_size_t
) linux_params
.setup_sects
<< GRUB_DISK_SECTOR_BITS
))
789 cl_offset
= ALIGN_UP ((grub_size_t
) (linux_params
.setup_sects
790 << GRUB_DISK_SECTOR_BITS
), 4096);
791 ctx
.real_size
= ALIGN_UP (cl_offset
+ maximal_cmdline_size
, 4096);
793 #ifdef GRUB_MACHINE_EFI
794 efi_mmap_size
= grub_efi_find_mmap_size ();
795 if (efi_mmap_size
== 0)
799 grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
800 (unsigned) ctx
.real_size
, (unsigned) mmap_size
);
802 #ifdef GRUB_MACHINE_EFI
803 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 1);
804 if (! ctx
.real_mode_target
)
805 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 0);
807 grub_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
);
809 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
810 (unsigned long) ctx
.real_mode_target
,
811 (unsigned) ctx
.real_size
,
812 (unsigned) efi_mmap_size
);
814 if (! ctx
.real_mode_target
)
815 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate real mode pages");
818 grub_relocator_chunk_t ch
;
819 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
820 ctx
.real_mode_target
,
821 (ctx
.real_size
+ efi_mmap_size
));
824 real_mode_mem
= get_virtual_current_address (ch
);
826 efi_mmap_buf
= (grub_uint8_t
*) real_mode_mem
+ ctx
.real_size
;
828 grub_dprintf ("linux", "real_mode_mem = %p\n",
831 ctx
.params
= real_mode_mem
;
833 *ctx
.params
= linux_params
;
834 ctx
.params
->cmd_line_ptr
= ctx
.real_mode_target
+ cl_offset
;
835 grub_memcpy ((char *) ctx
.params
+ cl_offset
, linux_cmdline
,
836 maximal_cmdline_size
);
838 grub_dprintf ("linux", "code32_start = %x\n",
839 (unsigned) ctx
.params
->code32_start
);
842 if (grub_mmap_iterate (grub_linux_boot_mmap_fill
, &ctx
))
844 ctx
.params
->mmap_size
= ctx
.e820_num
;
846 #ifdef GRUB_MACHINE_EFI
848 grub_efi_uintn_t efi_desc_size
;
849 grub_size_t efi_mmap_target
;
850 grub_efi_uint32_t efi_desc_version
;
851 err
= grub_efi_finish_boot_services (&efi_mmap_size
, efi_mmap_buf
, NULL
,
852 &efi_desc_size
, &efi_desc_version
);
856 /* Note that no boot services are available from here. */
857 efi_mmap_target
= ctx
.real_mode_target
858 + ((grub_uint8_t
*) efi_mmap_buf
- (grub_uint8_t
*) real_mode_mem
);
859 /* Pass EFI parameters. */
860 if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0208)
862 ctx
.params
->v0208
.efi_mem_desc_size
= efi_desc_size
;
863 ctx
.params
->v0208
.efi_mem_desc_version
= efi_desc_version
;
864 ctx
.params
->v0208
.efi_mmap
= efi_mmap_target
;
865 ctx
.params
->v0208
.efi_mmap_size
= efi_mmap_size
;
868 ctx
.params
->v0208
.efi_mmap_hi
= (efi_mmap_target
>> 32);
871 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0206)
873 ctx
.params
->v0206
.efi_mem_desc_size
= efi_desc_size
;
874 ctx
.params
->v0206
.efi_mem_desc_version
= efi_desc_version
;
875 ctx
.params
->v0206
.efi_mmap
= efi_mmap_target
;
876 ctx
.params
->v0206
.efi_mmap_size
= efi_mmap_size
;
878 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0204)
880 ctx
.params
->v0204
.efi_mem_desc_size
= efi_desc_size
;
881 ctx
.params
->v0204
.efi_mem_desc_version
= efi_desc_version
;
882 ctx
.params
->v0204
.efi_mmap
= efi_mmap_target
;
883 ctx
.params
->v0204
.efi_mmap_size
= efi_mmap_size
;
889 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
890 state
.ebp
= state
.edi
= state
.ebx
= 0;
891 state
.esi
= ctx
.real_mode_target
;
892 state
.esp
= ctx
.real_mode_target
;
893 state
.eip
= ctx
.params
->code32_start
;
894 return grub_relocator32_boot (relocator
, state
, 0);
898 grub_linux_unload (void)
900 grub_dl_unref (my_mod
);
902 grub_free (linux_cmdline
);
904 return GRUB_ERR_NONE
;
908 grub_cmd_linux (grub_command_t cmd
__attribute__ ((unused
)),
909 int argc
, char *argv
[])
911 grub_file_t file
= 0;
912 struct linux_i386_kernel_header lh
;
913 grub_uint8_t setup_sects
;
914 grub_size_t real_size
, prot_size
, prot_file_size
;
917 grub_size_t align
, min_align
;
919 grub_uint64_t preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
921 grub_dl_ref (my_mod
);
925 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
929 file
= grub_file_open (argv
[0], GRUB_FILE_TYPE_LINUX_KERNEL
);
933 if (ventoy_linux_argc
)
935 const char *tip
= grub_env_get("ventoy_loading_tip");
938 grub_printf("%s\n", tip
);
943 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
946 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
951 if (lh
.boot_flag
!= grub_cpu_to_le16_compile_time (0xaa55))
953 grub_error (GRUB_ERR_BAD_OS
, "invalid magic number");
957 if (lh
.setup_sects
> GRUB_LINUX_MAX_SETUP_SECTS
)
959 grub_error (GRUB_ERR_BAD_OS
, "too many setup sectors");
963 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
964 still not support 32-bit boot. */
965 if (lh
.header
!= grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE
)
966 || grub_le_to_cpu16 (lh
.version
) < 0x0203)
968 grub_error (GRUB_ERR_BAD_OS
, "version too old for 32-bit boot"
969 #ifdef GRUB_MACHINE_PCBIOS
970 " (try with `linux16')"
976 if (! (lh
.loadflags
& GRUB_LINUX_FLAG_BIG_KERNEL
))
978 grub_error (GRUB_ERR_BAD_OS
, "zImage doesn't support 32-bit boot"
979 #ifdef GRUB_MACHINE_PCBIOS
980 " (try with `linux16')"
986 if (grub_le_to_cpu16 (lh
.version
) >= 0x0206)
987 maximal_cmdline_size
= grub_le_to_cpu32 (lh
.cmdline_size
) + 1;
989 maximal_cmdline_size
= 256;
991 if (maximal_cmdline_size
< 128)
992 maximal_cmdline_size
= 128;
994 setup_sects
= lh
.setup_sects
;
996 /* If SETUP_SECTS is not set, set it to the default (4). */
998 setup_sects
= GRUB_LINUX_DEFAULT_SETUP_SECTS
;
1000 real_size
= setup_sects
<< GRUB_DISK_SECTOR_BITS
;
1001 prot_file_size
= grub_file_size (file
) - real_size
- GRUB_DISK_SECTOR_SIZE
;
1003 if (grub_le_to_cpu16 (lh
.version
) >= 0x205
1004 && lh
.kernel_alignment
!= 0
1005 && ((lh
.kernel_alignment
- 1) & lh
.kernel_alignment
) == 0)
1007 for (align
= 0; align
< 32; align
++)
1008 if (grub_le_to_cpu32 (lh
.kernel_alignment
) & (1 << align
))
1010 relocatable
= grub_le_to_cpu32 (lh
.relocatable
);
1018 if (grub_le_to_cpu16 (lh
.version
) >= 0x020a)
1020 min_align
= lh
.min_alignment
;
1021 prot_size
= grub_le_to_cpu32 (lh
.init_size
);
1022 prot_init_space
= page_align (prot_size
);
1024 preferred_address
= grub_le_to_cpu64 (lh
.pref_address
);
1026 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
1031 prot_size
= prot_file_size
;
1032 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
1033 /* Usually, the compression ratio is about 50%. */
1034 prot_init_space
= page_align (prot_size
) * 3;
1037 if (allocate_pages (prot_size
, &align
,
1038 min_align
, relocatable
,
1042 grub_memset (&linux_params
, 0, sizeof (linux_params
));
1043 grub_memcpy (&linux_params
.setup_sects
, &lh
.setup_sects
, sizeof (lh
) - 0x1F1);
1045 linux_params
.code32_start
= prot_mode_target
+ lh
.code32_start
- GRUB_LINUX_BZIMAGE_ADDR
;
1046 linux_params
.kernel_alignment
= (1 << align
);
1047 linux_params
.ps_mouse
= linux_params
.padding10
= 0;
1050 * The Linux 32-bit boot protocol defines the setup header end
1051 * to be at 0x202 + the byte value at 0x201.
1053 len
= 0x202 + *((char *) &linux_params
.jump
+ 1);
1055 /* Verify the struct is big enough so we do not write past the end. */
1056 if (len
> (char *) &linux_params
.edd_mbr_sig_buffer
- (char *) &linux_params
) {
1057 grub_error (GRUB_ERR_BAD_OS
, "Linux setup header too big");
1061 /* We've already read lh so there is no need to read it second time. */
1064 if (grub_file_read (file
, (char *) &linux_params
+ sizeof (lh
), len
) != len
)
1067 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1072 linux_params
.type_of_loader
= GRUB_LINUX_BOOT_LOADER_TYPE
;
1074 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
1075 and otherwise ignored. */
1076 linux_params
.cl_magic
= GRUB_LINUX_CL_MAGIC
;
1077 linux_params
.cl_offset
= 0x1000;
1079 linux_params
.ramdisk_image
= 0;
1080 linux_params
.ramdisk_size
= 0;
1082 linux_params
.heap_end_ptr
= GRUB_LINUX_HEAP_END_OFFSET
;
1083 linux_params
.loadflags
|= GRUB_LINUX_FLAG_CAN_USE_HEAP
;
1085 /* These are not needed to be precise, because Linux uses these values
1086 only to raise an error when the decompression code cannot find good
1088 linux_params
.ext_mem
= ((32 * 0x100000) >> 10);
1089 linux_params
.alt_mem
= ((32 * 0x100000) >> 10);
1091 /* Ignored by Linux. */
1092 linux_params
.video_page
= 0;
1094 /* Only used when `video_mode == 0x7', otherwise ignored. */
1095 linux_params
.video_ega_bx
= 0;
1097 linux_params
.font_size
= 16; /* XXX */
1099 #ifdef GRUB_MACHINE_EFI
1101 if (grub_le_to_cpu16 (linux_params
.version
) < 0x0208 &&
1102 ((grub_addr_t
) grub_efi_system_table
>> 32) != 0)
1103 return grub_error(GRUB_ERR_BAD_OS
,
1104 "kernel does not support 64-bit addressing");
1107 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0208)
1109 linux_params
.v0208
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
1110 linux_params
.v0208
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
1112 linux_params
.v0208
.efi_system_table_hi
= (grub_uint32_t
) ((grub_uint64_t
) grub_efi_system_table
>> 32);
1115 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0206)
1117 linux_params
.v0206
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
1118 linux_params
.v0206
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
1120 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0204)
1122 linux_params
.v0204
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE_0204
;
1123 linux_params
.v0204
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
1127 /* The other parameters are filled when booting. */
1129 grub_file_seek (file
, real_size
+ GRUB_DISK_SECTOR_SIZE
);
1131 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
1132 (unsigned) real_size
, (unsigned) prot_size
);
1134 /* Look for memory size and video mode specified on the command line. */
1136 for (i
= 1; i
< argc
; i
++)
1137 #ifdef GRUB_MACHINE_PCBIOS
1138 if (grub_memcmp (argv
[i
], "vga=", 4) == 0)
1140 /* Video mode selection support. */
1141 char *val
= argv
[i
] + 4;
1142 unsigned vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
1143 struct grub_vesa_mode_table_entry
*linux_mode
;
1147 grub_dl_load ("vbe");
1149 if (grub_strcmp (val
, "normal") == 0)
1150 vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
1151 else if (grub_strcmp (val
, "ext") == 0)
1152 vid_mode
= GRUB_LINUX_VID_MODE_EXTENDED
;
1153 else if (grub_strcmp (val
, "ask") == 0)
1155 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
1157 /* We usually would never do this in a loader, but "vga=ask" means user
1158 requested interaction, so it can't hurt to request keyboard input. */
1159 grub_wait_after_message ();
1164 vid_mode
= (grub_uint16_t
) grub_strtoul (val
, 0, 0);
1169 case GRUB_LINUX_VID_MODE_NORMAL
:
1170 grub_env_set ("gfxpayload", "text");
1171 grub_printf_ (N_("%s is deprecated. "
1172 "Use set gfxpayload=%s before "
1173 "linux command instead.\n"),
1178 case GRUB_LINUX_VID_MODE_EXTENDED
:
1179 /* FIXME: support 80x50 text. */
1180 grub_env_set ("gfxpayload", "text");
1181 grub_printf_ (N_("%s is deprecated. "
1182 "Use set gfxpayload=%s before "
1183 "linux command instead.\n"),
1187 /* Ignore invalid values. */
1188 if (vid_mode
< GRUB_VESA_MODE_TABLE_START
||
1189 vid_mode
> GRUB_VESA_MODE_TABLE_END
)
1191 grub_env_set ("gfxpayload", "text");
1192 /* TRANSLATORS: "x" has to be entered in, like an identifier,
1193 so please don't use better Unicode codepoints. */
1194 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
1195 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
1196 "before linux command instead.\n"),
1201 linux_mode
= &grub_vesa_mode_table
[vid_mode
1202 - GRUB_VESA_MODE_TABLE_START
];
1204 buf
= grub_xasprintf ("%ux%ux%u,%ux%u",
1205 linux_mode
->width
, linux_mode
->height
,
1207 linux_mode
->width
, linux_mode
->height
);
1211 grub_printf_ (N_("%s is deprecated. "
1212 "Use set gfxpayload=%s before "
1213 "linux command instead.\n"),
1215 err
= grub_env_set ("gfxpayload", buf
);
1222 #endif /* GRUB_MACHINE_PCBIOS */
1223 if (grub_memcmp (argv
[i
], "mem=", 4) == 0)
1225 char *val
= argv
[i
] + 4;
1227 linux_mem_size
= grub_strtoul (val
, &val
, 0);
1231 grub_errno
= GRUB_ERR_NONE
;
1238 switch (grub_tolower (val
[0]))
1253 /* Check an overflow. */
1254 if (linux_mem_size
> (~0UL >> shift
))
1257 linux_mem_size
<<= shift
;
1260 else if (grub_memcmp (argv
[i
], "quiet", sizeof ("quiet") - 1) == 0)
1262 linux_params
.loadflags
|= GRUB_LINUX_FLAG_QUIET
;
1265 /* Create kernel command line. */
1266 linux_cmdline
= grub_zalloc (maximal_cmdline_size
+ 1);
1269 grub_memcpy (linux_cmdline
, LINUX_IMAGE
, sizeof (LINUX_IMAGE
));
1273 if (ventoy_linux_argc
)
1275 ventoy_bootopt_hook(argc
, argv
);
1276 err
= grub_create_loader_cmdline (ventoy_linux_argc
, ventoy_linux_args
,
1278 + sizeof (LINUX_IMAGE
) - 1,
1279 maximal_cmdline_size
1280 - (sizeof (LINUX_IMAGE
) - 1),
1281 GRUB_VERIFY_KERNEL_CMDLINE
);
1285 err
= grub_create_loader_cmdline (argc
, argv
,
1287 + sizeof (LINUX_IMAGE
) - 1,
1288 maximal_cmdline_size
1289 - (sizeof (LINUX_IMAGE
) - 1),
1290 GRUB_VERIFY_KERNEL_CMDLINE
);
1297 len
= prot_file_size
;
1298 if (grub_file_read (file
, prot_mode_mem
, len
) != len
&& !grub_errno
)
1299 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1302 if (grub_errno
== GRUB_ERR_NONE
)
1304 grub_loader_set (grub_linux_boot
, grub_linux_unload
,
1305 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
1312 grub_file_close (file
);
1314 if (grub_errno
!= GRUB_ERR_NONE
)
1316 grub_dl_unref (my_mod
);
1324 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1325 int argc
, char *argv
[])
1327 grub_size_t size
= 0, aligned_size
= 0;
1328 grub_addr_t addr_min
, addr_max
;
1331 struct grub_linux_initrd_context initrd_ctx
= { 0, 0, 0 };
1335 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
1341 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("you need to load the kernel first"));
1345 if (grub_initrd_init (argc
, argv
, &initrd_ctx
))
1348 size
= grub_get_initrd_size (&initrd_ctx
);
1349 aligned_size
= ALIGN_UP (size
, 4096);
1351 /* Get the highest address available for the initrd. */
1352 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0203)
1354 addr_max
= grub_cpu_to_le32 (linux_params
.initrd_addr_max
);
1356 /* XXX in reality, Linux specifies a bogus value, so
1357 it is necessary to make sure that ADDR_MAX does not exceed
1359 if (addr_max
> GRUB_LINUX_INITRD_MAX_ADDRESS
)
1360 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1363 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1365 if (linux_mem_size
!= 0 && linux_mem_size
< addr_max
)
1366 addr_max
= linux_mem_size
;
1368 /* Linux 2.3.xx has a bug in the memory range check, so avoid
1370 Linux 2.2.xx has a bug in the memory range check, which is
1371 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
1372 addr_max
-= 0x10000;
1374 addr_min
= (grub_addr_t
) prot_mode_target
+ prot_init_space
;
1376 /* Put the initrd as high as possible, 4KiB aligned. */
1377 addr
= (addr_max
- aligned_size
) & ~0xFFF;
1379 if (addr
< addr_min
)
1381 grub_error (GRUB_ERR_OUT_OF_RANGE
, "the initrd is too big");
1386 grub_relocator_chunk_t ch
;
1387 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
1388 addr_min
, addr
, aligned_size
,
1390 GRUB_RELOCATOR_PREFERENCE_HIGH
,
1394 initrd_mem
= get_virtual_current_address (ch
);
1395 initrd_mem_target
= get_physical_target_address (ch
);
1398 if (grub_initrd_load (&initrd_ctx
, argv
, initrd_mem
))
1401 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1402 (unsigned) addr
, (unsigned) size
);
1404 linux_params
.ramdisk_image
= initrd_mem_target
;
1405 linux_params
.ramdisk_size
= size
;
1406 linux_params
.root_dev
= 0x0100; /* XXX */
1409 grub_initrd_close (&initrd_ctx
);
1415 ventoy_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1416 int argc
, char *argv
[])
1420 char *newargv
[32] = {NULL
};
1422 if (ventoy_debug
) grub_printf("ventoy_cmd_initrd %d\n", ventoy_linux_argc
);
1424 if (ventoy_linux_argc
== 0)
1426 return grub_cmd_initrd(cmd
, argc
, argv
);
1429 grub_snprintf(buf
, sizeof(buf
), "mem:%s:size:%s", grub_env_get("ventoy_cpio_addr"), grub_env_get("ventoy_cpio_size"));
1431 if (ventoy_debug
) grub_printf("membuf=%s\n", buf
);
1434 for (i
= 0; i
< argc
; i
++)
1436 newargv
[i
+ 1] = argv
[i
];
1439 ventoy_initrd_called
= 1;
1441 return grub_cmd_initrd(cmd
, argc
+ 1, newargv
);
1445 static grub_command_t cmd_linux
, cmd_initrd
, cmd_linuxefi
, cmd_initrdefi
, cmd_set_bootopt
, cmd_unset_bootopt
;
1447 GRUB_MOD_INIT(linux
)
1449 cmd_linux
= grub_register_command ("linux", grub_cmd_linux
,
1450 0, N_("Load Linux."));
1451 cmd_initrd
= grub_register_command ("initrd", ventoy_cmd_initrd
,
1452 0, N_("Load initrd."));
1454 cmd_linuxefi
= grub_register_command ("linuxefi", grub_cmd_linux
,
1455 0, N_("Load Linux."));
1456 cmd_initrdefi
= grub_register_command ("initrdefi", ventoy_cmd_initrd
,
1457 0, N_("Load initrd."));
1458 cmd_set_bootopt
= grub_register_command ("vt_set_boot_opt", grub_cmd_set_boot_opt
, 0, N_("set ext boot opt"));
1459 cmd_unset_bootopt
= grub_register_command ("vt_unset_boot_opt", grub_cmd_unset_boot_opt
, 0, N_("unset ext boot opt"));
1461 ventoy_linux_args
= grub_zalloc(sizeof(char *) * LINUX_MAX_ARGC
);
1466 GRUB_MOD_FINI(linux
)
1468 grub_unregister_command (cmd_linux
);
1469 grub_unregister_command (cmd_initrd
);