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>
39 #include <grub/safemath.h>
41 GRUB_MOD_LICENSE ("GPLv3+");
43 #ifdef GRUB_MACHINE_PCBIOS
44 #include <grub/i386/pc/vesa_modes_table.h>
47 #ifdef GRUB_MACHINE_EFI
48 #include <grub/efi/efi.h>
49 #include <grub/efi/sb.h>
50 #define HAS_VGA_TEXT 0
51 #define DEFAULT_VIDEO_MODE "auto"
52 #define ACCEPTS_PURE_TEXT 0
53 #elif defined (GRUB_MACHINE_IEEE1275)
54 #include <grub/ieee1275/ieee1275.h>
55 #define HAS_VGA_TEXT 0
56 #define DEFAULT_VIDEO_MODE "text"
57 #define ACCEPTS_PURE_TEXT 1
59 #include <grub/i386/pc/vbe.h>
60 #include <grub/i386/pc/console.h>
61 #define HAS_VGA_TEXT 1
62 #define DEFAULT_VIDEO_MODE "text"
63 #define ACCEPTS_PURE_TEXT 1
66 static grub_dl_t my_mod
;
68 static grub_size_t linux_mem_size
;
70 static void *prot_mode_mem
;
71 static grub_addr_t prot_mode_target
;
72 static void *initrd_mem
;
73 static grub_addr_t initrd_mem_target
;
74 static grub_size_t prot_init_space
;
75 static struct grub_relocator
*relocator
= NULL
;
76 static void *efi_mmap_buf
;
77 static grub_size_t maximal_cmdline_size
;
78 static struct linux_kernel_params linux_params
;
79 static char *linux_cmdline
;
80 #ifdef GRUB_MACHINE_EFI
81 static grub_efi_uintn_t efi_mmap_size
;
83 static const grub_size_t efi_mmap_size
= 0;
94 static struct idt_descriptor idt_desc
=
101 static inline grub_size_t
102 page_align (grub_size_t size
)
104 return (size
+ (1 << 12) - 1) & (~((1 << 12) - 1));
107 /* Helper for find_mmap_size. */
109 count_hook (grub_uint64_t addr
__attribute__ ((unused
)),
110 grub_uint64_t size
__attribute__ ((unused
)),
111 grub_memory_type_t type
__attribute__ ((unused
)), void *data
)
113 grub_size_t
*count
= data
;
119 /* Find the optimal number of pages for the memory map. */
121 find_mmap_size (void)
123 grub_size_t count
= 0, mmap_size
;
125 grub_mmap_iterate (count_hook
, &count
);
127 mmap_size
= count
* sizeof (struct grub_e820_mmap
);
129 /* Increase the size a bit for safety, because GRUB allocates more on
131 mmap_size
+= (1 << 12);
133 return page_align (mmap_size
);
139 grub_relocator_unload (relocator
);
141 prot_mode_mem
= initrd_mem
= 0;
142 prot_mode_target
= initrd_mem_target
= 0;
145 /* Allocate pages for the real mode code and the protected mode code
146 for linux as well as a memory map buffer. */
148 allocate_pages (grub_size_t prot_size
, grub_size_t
*align
,
149 grub_size_t min_align
, int relocatable
,
150 grub_uint64_t preferred_address
)
157 prot_size
= page_align (prot_size
);
159 /* Initialize the memory pointers with NULL for convenience. */
162 relocator
= grub_relocator_new ();
169 /* FIXME: Should request low memory from the heap when this feature is
173 grub_relocator_chunk_t ch
;
176 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
180 GRUB_RELOCATOR_PREFERENCE_LOW
,
182 for (; err
&& *align
+ 1 > min_align
; (*align
)--)
184 grub_errno
= GRUB_ERR_NONE
;
185 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
, 0x1000000,
186 UP_TO_TOP32 (prot_size
),
187 prot_size
, 1 << *align
,
188 GRUB_RELOCATOR_PREFERENCE_LOW
,
195 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
200 prot_mode_mem
= get_virtual_current_address (ch
);
201 prot_mode_target
= get_physical_target_address (ch
);
204 grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
205 prot_mode_mem
, (unsigned long) prot_mode_target
,
206 (unsigned) prot_size
);
207 return GRUB_ERR_NONE
;
215 grub_e820_add_region (struct grub_e820_mmap
*e820_map
, int *e820_num
,
216 grub_uint64_t start
, grub_uint64_t size
,
221 if ((n
> 0) && (e820_map
[n
- 1].addr
+ e820_map
[n
- 1].size
== start
) &&
222 (e820_map
[n
- 1].type
== type
))
223 e820_map
[n
- 1].size
+= size
;
226 e820_map
[n
].addr
= start
;
227 e820_map
[n
].size
= size
;
228 e820_map
[n
].type
= type
;
231 return GRUB_ERR_NONE
;
235 grub_linux_setup_video (struct linux_kernel_params
*params
)
237 struct grub_video_mode_info mode_info
;
240 grub_video_driver_id_t driver_id
;
241 const char *gfxlfbvar
= grub_env_get ("gfxpayloadforcelfb");
243 driver_id
= grub_video_get_driver_id ();
245 if (driver_id
== GRUB_VIDEO_DRIVER_NONE
)
248 err
= grub_video_get_info_and_fini (&mode_info
, &framebuffer
);
252 grub_errno
= GRUB_ERR_NONE
;
256 params
->lfb_width
= mode_info
.width
;
257 params
->lfb_height
= mode_info
.height
;
258 params
->lfb_depth
= mode_info
.bpp
;
259 params
->lfb_line_len
= mode_info
.pitch
;
261 params
->lfb_base
= (grub_size_t
) framebuffer
;
263 #if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
264 params
->ext_lfb_base
= (grub_size_t
) (((grub_uint64_t
)(grub_size_t
) framebuffer
) >> 32);
265 params
->capabilities
|= VIDEO_CAPABILITY_64BIT_BASE
;
268 params
->lfb_size
= ALIGN_UP (params
->lfb_line_len
* params
->lfb_height
, 65536);
270 params
->red_mask_size
= mode_info
.red_mask_size
;
271 params
->red_field_pos
= mode_info
.red_field_pos
;
272 params
->green_mask_size
= mode_info
.green_mask_size
;
273 params
->green_field_pos
= mode_info
.green_field_pos
;
274 params
->blue_mask_size
= mode_info
.blue_mask_size
;
275 params
->blue_field_pos
= mode_info
.blue_field_pos
;
276 params
->reserved_mask_size
= mode_info
.reserved_mask_size
;
277 params
->reserved_field_pos
= mode_info
.reserved_field_pos
;
279 if (gfxlfbvar
&& (gfxlfbvar
[0] == '1' || gfxlfbvar
[0] == 'y'))
280 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
285 case GRUB_VIDEO_DRIVER_VBE
:
286 params
->lfb_size
>>= 16;
287 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_VESA
;
290 case GRUB_VIDEO_DRIVER_EFI_UGA
:
291 case GRUB_VIDEO_DRIVER_EFI_GOP
:
292 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_EFIFB
;
295 /* FIXME: check if better id is available. */
296 case GRUB_VIDEO_DRIVER_SM712
:
297 case GRUB_VIDEO_DRIVER_SIS315PRO
:
298 case GRUB_VIDEO_DRIVER_VGA
:
299 case GRUB_VIDEO_DRIVER_CIRRUS
:
300 case GRUB_VIDEO_DRIVER_BOCHS
:
301 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E
:
302 case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A
:
303 case GRUB_VIDEO_DRIVER_IEEE1275
:
304 case GRUB_VIDEO_DRIVER_COREBOOT
:
305 /* Make gcc happy. */
306 case GRUB_VIDEO_DRIVER_XEN
:
307 case GRUB_VIDEO_DRIVER_SDL
:
308 case GRUB_VIDEO_DRIVER_NONE
:
309 case GRUB_VIDEO_ADAPTER_CAPTURE
:
310 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
315 #ifdef GRUB_MACHINE_PCBIOS
316 /* VESA packed modes may come with zeroed mask sizes, which need
317 to be set here according to DAC Palette width. If we don't,
318 this results in Linux displaying a black screen. */
319 if (driver_id
== GRUB_VIDEO_DRIVER_VBE
&& mode_info
.bpp
<= 8)
321 struct grub_vbe_info_block controller_info
;
325 status
= grub_vbe_bios_get_controller_info (&controller_info
);
327 if (status
== GRUB_VBE_STATUS_OK
&&
328 (controller_info
.capabilities
& GRUB_VBE_CAPABILITY_DACWIDTH
))
329 status
= grub_vbe_bios_set_dac_palette_width (&width
);
331 if (status
!= GRUB_VBE_STATUS_OK
)
332 /* 6 is default after mode reset. */
335 params
->red_mask_size
= params
->green_mask_size
336 = params
->blue_mask_size
= width
;
337 params
->reserved_mask_size
= 0;
341 return GRUB_ERR_NONE
;
344 /* Context for grub_linux_boot. */
345 struct grub_linux_boot_ctx
347 grub_addr_t real_mode_target
;
348 grub_size_t real_size
;
349 struct linux_kernel_params
*params
;
353 /* Helper for grub_linux_boot. */
355 grub_linux_boot_mmap_find (grub_uint64_t addr
, grub_uint64_t size
,
356 grub_memory_type_t type
, void *data
)
358 struct grub_linux_boot_ctx
*ctx
= data
;
360 /* We must put real mode code in the traditional space. */
361 if (type
!= GRUB_MEMORY_AVAILABLE
|| addr
> 0x90000)
364 if (addr
+ size
< 0x10000)
369 size
+= addr
- 0x10000;
373 if (addr
+ size
> 0x90000)
374 size
= 0x90000 - addr
;
376 if (ctx
->real_size
+ efi_mmap_size
> size
)
379 grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
380 (unsigned long) addr
,
382 (unsigned) (ctx
->real_size
+ efi_mmap_size
));
383 ctx
->real_mode_target
= ((addr
+ size
) - (ctx
->real_size
+ efi_mmap_size
));
387 /* GRUB types conveniently match E820 types. */
389 grub_linux_boot_mmap_fill (grub_uint64_t addr
, grub_uint64_t size
,
390 grub_memory_type_t type
, void *data
)
392 struct grub_linux_boot_ctx
*ctx
= data
;
394 if (grub_e820_add_region (ctx
->params
->e820_map
, &ctx
->e820_num
,
402 grub_linux_boot (void)
407 struct grub_relocator32_state state
;
409 struct grub_linux_boot_ctx ctx
= {
410 .real_mode_target
= 0
412 grub_size_t mmap_size
;
413 grub_size_t cl_offset
;
415 #ifdef GRUB_MACHINE_IEEE1275
417 const char *bootpath
;
420 bootpath
= grub_env_get ("root");
422 grub_ieee1275_set_property (grub_ieee1275_chosen
,
423 "bootpath", bootpath
,
424 grub_strlen (bootpath
) + 1,
426 linux_params
.ofw_signature
= GRUB_LINUX_OFW_SIGNATURE
;
427 linux_params
.ofw_num_items
= 1;
428 linux_params
.ofw_cif_handler
= (grub_uint32_t
) grub_ieee1275_entry_fn
;
429 linux_params
.ofw_idt
= 0;
433 modevar
= grub_env_get ("gfxpayload");
435 /* Now all graphical modes are acceptable.
436 May change in future if we have modes without framebuffer. */
437 if (modevar
&& *modevar
!= 0)
439 tmp
= grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE
, modevar
);
442 #if ACCEPTS_PURE_TEXT
443 err
= grub_video_set_mode (tmp
, 0, 0);
445 err
= grub_video_set_mode (tmp
, GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
449 else /* We can't go back to text mode from coreboot fb. */
450 #ifdef GRUB_MACHINE_COREBOOT
451 if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT
)
456 #if ACCEPTS_PURE_TEXT
457 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
, 0, 0);
459 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
,
460 GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
467 grub_puts_ (N_("Booting in blind mode"));
468 grub_errno
= GRUB_ERR_NONE
;
471 if (grub_linux_setup_video (&linux_params
))
473 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
474 linux_params
.have_vga
= GRUB_VIDEO_LINUX_TYPE_TEXT
;
475 linux_params
.video_mode
= 0x3;
477 linux_params
.have_vga
= 0;
478 linux_params
.video_mode
= 0;
479 linux_params
.video_width
= 0;
480 linux_params
.video_height
= 0;
485 #ifndef GRUB_MACHINE_IEEE1275
486 if (linux_params
.have_vga
== GRUB_VIDEO_LINUX_TYPE_TEXT
)
489 grub_term_output_t term
;
491 FOR_ACTIVE_TERM_OUTPUTS(term
)
492 if (grub_strcmp (term
->name
, "vga_text") == 0
493 || grub_strcmp (term
->name
, "console") == 0
494 || grub_strcmp (term
->name
, "ofconsole") == 0)
496 struct grub_term_coordinate pos
= grub_term_getxy (term
);
497 linux_params
.video_cursor_x
= pos
.x
;
498 linux_params
.video_cursor_y
= pos
.y
;
499 linux_params
.video_width
= grub_term_width (term
);
500 linux_params
.video_height
= grub_term_height (term
);
506 linux_params
.video_cursor_x
= 0;
507 linux_params
.video_cursor_y
= 0;
508 linux_params
.video_width
= 80;
509 linux_params
.video_height
= 25;
513 #ifdef GRUB_KERNEL_USE_RSDP_ADDR
514 linux_params
.acpi_rsdp_addr
= grub_le_to_cpu64 (grub_rsdp_addr
);
517 mmap_size
= find_mmap_size ();
518 /* Make sure that each size is aligned to a page boundary. */
519 cl_offset
= ALIGN_UP (mmap_size
+ sizeof (linux_params
), 4096);
520 if (cl_offset
< ((grub_size_t
) linux_params
.setup_sects
<< GRUB_DISK_SECTOR_BITS
))
521 cl_offset
= ALIGN_UP ((grub_size_t
) (linux_params
.setup_sects
522 << GRUB_DISK_SECTOR_BITS
), 4096);
523 ctx
.real_size
= ALIGN_UP (cl_offset
+ maximal_cmdline_size
, 4096);
525 #ifdef GRUB_MACHINE_EFI
526 efi_mmap_size
= grub_efi_find_mmap_size ();
527 if (efi_mmap_size
== 0)
531 grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
532 (unsigned) ctx
.real_size
, (unsigned) mmap_size
);
534 #ifdef GRUB_MACHINE_EFI
535 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 1);
536 if (! ctx
.real_mode_target
)
537 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 0);
539 grub_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
);
541 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
542 (unsigned long) ctx
.real_mode_target
,
543 (unsigned) ctx
.real_size
,
544 (unsigned) efi_mmap_size
);
546 if (! ctx
.real_mode_target
)
547 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate real mode pages");
550 grub_relocator_chunk_t ch
;
553 if (grub_add (ctx
.real_size
, efi_mmap_size
, &sz
))
554 return GRUB_ERR_OUT_OF_RANGE
;
556 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
557 ctx
.real_mode_target
, sz
);
560 real_mode_mem
= get_virtual_current_address (ch
);
562 efi_mmap_buf
= (grub_uint8_t
*) real_mode_mem
+ ctx
.real_size
;
564 grub_dprintf ("linux", "real_mode_mem = %p\n",
567 ctx
.params
= real_mode_mem
;
569 *ctx
.params
= linux_params
;
570 ctx
.params
->cmd_line_ptr
= ctx
.real_mode_target
+ cl_offset
;
571 grub_memcpy ((char *) ctx
.params
+ cl_offset
, linux_cmdline
,
572 maximal_cmdline_size
);
574 grub_dprintf ("linux", "code32_start = %x\n",
575 (unsigned) ctx
.params
->code32_start
);
578 if (grub_mmap_iterate (grub_linux_boot_mmap_fill
, &ctx
))
580 ctx
.params
->mmap_size
= ctx
.e820_num
;
582 #ifdef GRUB_MACHINE_EFI
584 grub_efi_uintn_t efi_desc_size
;
585 grub_size_t efi_mmap_target
;
586 grub_efi_uint32_t efi_desc_version
;
588 ctx
.params
->secure_boot
= grub_efi_get_secureboot ();
590 err
= grub_efi_finish_boot_services (&efi_mmap_size
, efi_mmap_buf
, NULL
,
591 &efi_desc_size
, &efi_desc_version
);
595 /* Note that no boot services are available from here. */
596 efi_mmap_target
= ctx
.real_mode_target
597 + ((grub_uint8_t
*) efi_mmap_buf
- (grub_uint8_t
*) real_mode_mem
);
598 /* Pass EFI parameters. */
599 if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0208)
601 ctx
.params
->v0208
.efi_mem_desc_size
= efi_desc_size
;
602 ctx
.params
->v0208
.efi_mem_desc_version
= efi_desc_version
;
603 ctx
.params
->v0208
.efi_mmap
= efi_mmap_target
;
604 ctx
.params
->v0208
.efi_mmap_size
= efi_mmap_size
;
607 ctx
.params
->v0208
.efi_mmap_hi
= (efi_mmap_target
>> 32);
610 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0206)
612 ctx
.params
->v0206
.efi_mem_desc_size
= efi_desc_size
;
613 ctx
.params
->v0206
.efi_mem_desc_version
= efi_desc_version
;
614 ctx
.params
->v0206
.efi_mmap
= efi_mmap_target
;
615 ctx
.params
->v0206
.efi_mmap_size
= efi_mmap_size
;
617 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0204)
619 ctx
.params
->v0204
.efi_mem_desc_size
= efi_desc_size
;
620 ctx
.params
->v0204
.efi_mem_desc_version
= efi_desc_version
;
621 ctx
.params
->v0204
.efi_mmap
= efi_mmap_target
;
622 ctx
.params
->v0204
.efi_mmap_size
= efi_mmap_size
;
628 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
629 state
.ebp
= state
.edi
= state
.ebx
= 0;
630 state
.esi
= ctx
.real_mode_target
;
631 state
.esp
= ctx
.real_mode_target
;
632 state
.eip
= ctx
.params
->code32_start
;
633 return grub_relocator32_boot (relocator
, state
, 0);
637 grub_linux_unload (void)
639 grub_dl_unref (my_mod
);
641 grub_free (linux_cmdline
);
643 return GRUB_ERR_NONE
;
647 grub_cmd_linux (grub_command_t cmd
__attribute__ ((unused
)),
648 int argc
, char *argv
[])
650 grub_file_t file
= 0;
651 struct linux_i386_kernel_header lh
;
652 grub_uint8_t setup_sects
;
653 grub_size_t real_size
, prot_size
, prot_file_size
;
656 grub_size_t align
, min_align
;
658 grub_uint64_t preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
660 grub_dl_ref (my_mod
);
664 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
668 file
= grub_file_open (argv
[0], GRUB_FILE_TYPE_LINUX_KERNEL
);
672 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
675 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
680 if (lh
.boot_flag
!= grub_cpu_to_le16_compile_time (0xaa55))
682 grub_error (GRUB_ERR_BAD_OS
, "invalid magic number");
686 if (lh
.setup_sects
> GRUB_LINUX_MAX_SETUP_SECTS
)
688 grub_error (GRUB_ERR_BAD_OS
, "too many setup sectors");
692 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
693 still not support 32-bit boot. */
694 if (lh
.header
!= grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE
)
695 || grub_le_to_cpu16 (lh
.version
) < 0x0203)
697 grub_error (GRUB_ERR_BAD_OS
, "version too old for 32-bit boot"
698 #ifdef GRUB_MACHINE_PCBIOS
699 " (try with `linux16')"
705 if (! (lh
.loadflags
& GRUB_LINUX_FLAG_BIG_KERNEL
))
707 grub_error (GRUB_ERR_BAD_OS
, "zImage doesn't support 32-bit boot"
708 #ifdef GRUB_MACHINE_PCBIOS
709 " (try with `linux16')"
715 if (grub_le_to_cpu16 (lh
.version
) >= 0x0206)
716 maximal_cmdline_size
= grub_le_to_cpu32 (lh
.cmdline_size
) + 1;
718 maximal_cmdline_size
= 256;
720 if (maximal_cmdline_size
< 128)
721 maximal_cmdline_size
= 128;
723 setup_sects
= lh
.setup_sects
;
725 /* If SETUP_SECTS is not set, set it to the default (4). */
727 setup_sects
= GRUB_LINUX_DEFAULT_SETUP_SECTS
;
729 real_size
= setup_sects
<< GRUB_DISK_SECTOR_BITS
;
730 prot_file_size
= grub_file_size (file
) - real_size
- GRUB_DISK_SECTOR_SIZE
;
732 if (grub_le_to_cpu16 (lh
.version
) >= 0x205
733 && lh
.kernel_alignment
!= 0
734 && ((lh
.kernel_alignment
- 1) & lh
.kernel_alignment
) == 0)
736 for (align
= 0; align
< 32; align
++)
737 if (grub_le_to_cpu32 (lh
.kernel_alignment
) & (1 << align
))
739 relocatable
= lh
.relocatable
;
747 if (grub_le_to_cpu16 (lh
.version
) >= 0x020a)
749 min_align
= lh
.min_alignment
;
750 prot_size
= grub_le_to_cpu32 (lh
.init_size
);
751 prot_init_space
= page_align (prot_size
);
753 preferred_address
= grub_le_to_cpu64 (lh
.pref_address
);
758 prot_size
= prot_file_size
;
759 /* Usually, the compression ratio is about 50%. */
760 prot_init_space
= page_align (prot_size
) * 3;
763 if (allocate_pages (prot_size
, &align
,
764 min_align
, relocatable
,
768 grub_memset (&linux_params
, 0, sizeof (linux_params
));
771 * The Linux 32-bit boot protocol defines the setup header end
772 * to be at 0x202 + the byte value at 0x201.
774 len
= 0x202 + *((char *) &lh
.jump
+ 1);
776 /* Verify the struct is big enough so we do not write past the end. */
777 if (len
> (char *) &linux_params
.edd_mbr_sig_buffer
- (char *) &linux_params
) {
778 grub_error (GRUB_ERR_BAD_OS
, "Linux setup header too big");
782 grub_memcpy (&linux_params
.setup_sects
, &lh
.setup_sects
, len
- 0x1F1);
784 /* We've already read lh so there is no need to read it second time. */
788 (grub_file_read (file
, (char *) &linux_params
+ sizeof (lh
), len
) != len
))
791 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
796 linux_params
.code32_start
= prot_mode_target
+ lh
.code32_start
- GRUB_LINUX_BZIMAGE_ADDR
;
797 linux_params
.kernel_alignment
= (1 << align
);
798 linux_params
.ps_mouse
= linux_params
.padding11
= 0;
799 linux_params
.type_of_loader
= GRUB_LINUX_BOOT_LOADER_TYPE
;
801 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
802 and otherwise ignored. */
803 linux_params
.cl_magic
= GRUB_LINUX_CL_MAGIC
;
804 linux_params
.cl_offset
= 0x1000;
806 linux_params
.ramdisk_image
= 0;
807 linux_params
.ramdisk_size
= 0;
809 linux_params
.heap_end_ptr
= GRUB_LINUX_HEAP_END_OFFSET
;
810 linux_params
.loadflags
|= GRUB_LINUX_FLAG_CAN_USE_HEAP
;
812 /* These are not needed to be precise, because Linux uses these values
813 only to raise an error when the decompression code cannot find good
815 linux_params
.ext_mem
= ((32 * 0x100000) >> 10);
816 linux_params
.alt_mem
= ((32 * 0x100000) >> 10);
818 /* Ignored by Linux. */
819 linux_params
.video_page
= 0;
821 /* Only used when `video_mode == 0x7', otherwise ignored. */
822 linux_params
.video_ega_bx
= 0;
824 linux_params
.font_size
= 16; /* XXX */
826 #ifdef GRUB_MACHINE_EFI
828 if (grub_le_to_cpu16 (linux_params
.version
) < 0x0208 &&
829 ((grub_addr_t
) grub_efi_system_table
>> 32) != 0)
830 return grub_error(GRUB_ERR_BAD_OS
,
831 "kernel does not support 64-bit addressing");
834 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0208)
836 linux_params
.v0208
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
837 linux_params
.v0208
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
839 linux_params
.v0208
.efi_system_table_hi
= (grub_uint32_t
) ((grub_uint64_t
) grub_efi_system_table
>> 32);
842 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0206)
844 linux_params
.v0206
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
845 linux_params
.v0206
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
847 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0204)
849 linux_params
.v0204
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE_0204
;
850 linux_params
.v0204
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
854 /* The other parameters are filled when booting. */
856 grub_file_seek (file
, real_size
+ GRUB_DISK_SECTOR_SIZE
);
858 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
859 (unsigned) real_size
, (unsigned) prot_size
);
861 /* Look for memory size and video mode specified on the command line. */
863 for (i
= 1; i
< argc
; i
++)
864 #ifdef GRUB_MACHINE_PCBIOS
865 if (grub_memcmp (argv
[i
], "vga=", 4) == 0)
867 /* Video mode selection support. */
868 char *val
= argv
[i
] + 4;
869 unsigned vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
870 struct grub_vesa_mode_table_entry
*linux_mode
;
874 grub_dl_load ("vbe");
876 if (grub_strcmp (val
, "normal") == 0)
877 vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
878 else if (grub_strcmp (val
, "ext") == 0)
879 vid_mode
= GRUB_LINUX_VID_MODE_EXTENDED
;
880 else if (grub_strcmp (val
, "ask") == 0)
882 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
884 /* We usually would never do this in a loader, but "vga=ask" means user
885 requested interaction, so it can't hurt to request keyboard input. */
886 grub_wait_after_message ();
891 vid_mode
= (grub_uint16_t
) grub_strtoul (val
, 0, 0);
896 case GRUB_LINUX_VID_MODE_NORMAL
:
897 grub_env_set ("gfxpayload", "text");
898 grub_printf_ (N_("%s is deprecated. "
899 "Use set gfxpayload=%s before "
900 "linux command instead.\n"),
905 case GRUB_LINUX_VID_MODE_EXTENDED
:
906 /* FIXME: support 80x50 text. */
907 grub_env_set ("gfxpayload", "text");
908 grub_printf_ (N_("%s is deprecated. "
909 "Use set gfxpayload=%s before "
910 "linux command instead.\n"),
914 /* Ignore invalid values. */
915 if (vid_mode
< GRUB_VESA_MODE_TABLE_START
||
916 vid_mode
> GRUB_VESA_MODE_TABLE_END
)
918 grub_env_set ("gfxpayload", "text");
919 /* TRANSLATORS: "x" has to be entered in, like an identifier,
920 so please don't use better Unicode codepoints. */
921 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
922 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
923 "before linux command instead.\n"),
928 linux_mode
= &grub_vesa_mode_table
[vid_mode
929 - GRUB_VESA_MODE_TABLE_START
];
931 buf
= grub_xasprintf ("%ux%ux%u,%ux%u",
932 linux_mode
->width
, linux_mode
->height
,
934 linux_mode
->width
, linux_mode
->height
);
938 grub_printf_ (N_("%s is deprecated. "
939 "Use set gfxpayload=%s before "
940 "linux command instead.\n"),
942 err
= grub_env_set ("gfxpayload", buf
);
949 #endif /* GRUB_MACHINE_PCBIOS */
950 if (grub_memcmp (argv
[i
], "mem=", 4) == 0)
952 const char *val
= argv
[i
] + 4;
954 linux_mem_size
= grub_strtoul (val
, &val
, 0);
958 grub_errno
= GRUB_ERR_NONE
;
965 switch (grub_tolower (val
[0]))
980 /* Check an overflow. */
981 if (linux_mem_size
> (~0UL >> shift
))
984 linux_mem_size
<<= shift
;
987 else if (grub_memcmp (argv
[i
], "quiet", sizeof ("quiet") - 1) == 0)
989 linux_params
.loadflags
|= GRUB_LINUX_FLAG_QUIET
;
992 /* Create kernel command line. */
993 linux_cmdline
= grub_zalloc (maximal_cmdline_size
+ 1);
996 grub_memcpy (linux_cmdline
, LINUX_IMAGE
, sizeof (LINUX_IMAGE
));
999 err
= grub_create_loader_cmdline (argc
, argv
,
1001 + sizeof (LINUX_IMAGE
) - 1,
1002 maximal_cmdline_size
1003 - (sizeof (LINUX_IMAGE
) - 1),
1004 GRUB_VERIFY_KERNEL_CMDLINE
);
1009 len
= prot_file_size
;
1010 if (grub_file_read (file
, prot_mode_mem
, len
) != len
&& !grub_errno
)
1011 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1014 if (grub_errno
== GRUB_ERR_NONE
)
1016 grub_loader_set (grub_linux_boot
, grub_linux_unload
,
1017 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
1024 grub_file_close (file
);
1026 if (grub_errno
!= GRUB_ERR_NONE
)
1028 grub_dl_unref (my_mod
);
1036 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1037 int argc
, char *argv
[])
1039 grub_size_t size
= 0, aligned_size
= 0;
1040 grub_addr_t addr_min
, addr_max
;
1043 struct grub_linux_initrd_context initrd_ctx
= { 0, 0, 0 };
1047 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
1053 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("you need to load the kernel first"));
1057 if (grub_initrd_init (argc
, argv
, &initrd_ctx
))
1060 size
= grub_get_initrd_size (&initrd_ctx
);
1061 aligned_size
= ALIGN_UP (size
, 4096);
1063 /* Get the highest address available for the initrd. */
1064 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0203)
1066 addr_max
= grub_cpu_to_le32 (linux_params
.initrd_addr_max
);
1068 /* XXX in reality, Linux specifies a bogus value, so
1069 it is necessary to make sure that ADDR_MAX does not exceed
1071 if (addr_max
> GRUB_LINUX_INITRD_MAX_ADDRESS
)
1072 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1075 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1077 if (linux_mem_size
!= 0 && linux_mem_size
< addr_max
)
1078 addr_max
= linux_mem_size
;
1080 /* Linux 2.3.xx has a bug in the memory range check, so avoid
1082 Linux 2.2.xx has a bug in the memory range check, which is
1083 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
1084 addr_max
-= 0x10000;
1086 addr_min
= (grub_addr_t
) prot_mode_target
+ prot_init_space
;
1088 /* Put the initrd as high as possible, 4KiB aligned. */
1089 addr
= (addr_max
- aligned_size
) & ~0xFFF;
1091 if (addr
< addr_min
)
1093 grub_error (GRUB_ERR_OUT_OF_RANGE
, "the initrd is too big");
1098 grub_relocator_chunk_t ch
;
1099 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
1100 addr_min
, addr
, aligned_size
,
1102 GRUB_RELOCATOR_PREFERENCE_HIGH
,
1106 initrd_mem
= get_virtual_current_address (ch
);
1107 initrd_mem_target
= get_physical_target_address (ch
);
1110 if (grub_initrd_load (&initrd_ctx
, argv
, initrd_mem
))
1113 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1114 (unsigned) addr
, (unsigned) size
);
1116 linux_params
.ramdisk_image
= initrd_mem_target
;
1117 linux_params
.ramdisk_size
= size
;
1118 linux_params
.root_dev
= 0x0100; /* XXX */
1121 grub_initrd_close (&initrd_ctx
);
1126 static grub_command_t cmd_linux
, cmd_initrd
;
1128 GRUB_MOD_INIT(linux
)
1130 cmd_linux
= grub_register_command ("linux", grub_cmd_linux
,
1131 0, N_("Load Linux."));
1132 cmd_initrd
= grub_register_command ("initrd", grub_cmd_initrd
,
1133 0, N_("Load initrd."));
1137 GRUB_MOD_FINI(linux
)
1139 grub_unregister_command (cmd_linux
);
1140 grub_unregister_command (cmd_initrd
);