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>
39 GRUB_MOD_LICENSE ("GPLv3+");
41 #ifdef GRUB_MACHINE_PCBIOS
42 #include <grub/i386/pc/vesa_modes_table.h>
45 #ifdef GRUB_MACHINE_EFI
46 #include <grub/efi/efi.h>
47 #define HAS_VGA_TEXT 0
48 #define DEFAULT_VIDEO_MODE "auto"
49 #define ACCEPTS_PURE_TEXT 0
50 #elif defined (GRUB_MACHINE_IEEE1275)
51 #include <grub/ieee1275/ieee1275.h>
52 #define HAS_VGA_TEXT 0
53 #define DEFAULT_VIDEO_MODE "text"
54 #define ACCEPTS_PURE_TEXT 1
56 #include <grub/i386/pc/vbe.h>
57 #include <grub/i386/pc/console.h>
58 #define HAS_VGA_TEXT 1
59 #define DEFAULT_VIDEO_MODE "text"
60 #define ACCEPTS_PURE_TEXT 1
63 static grub_dl_t my_mod
;
65 static grub_size_t linux_mem_size
;
67 static void *prot_mode_mem
;
68 static grub_addr_t prot_mode_target
;
69 static void *initrd_mem
;
70 static grub_addr_t initrd_mem_target
;
71 static grub_size_t prot_init_space
;
72 static struct grub_relocator
*relocator
= NULL
;
73 static void *efi_mmap_buf
;
74 static grub_size_t maximal_cmdline_size
;
75 static struct linux_kernel_params linux_params
;
76 static char *linux_cmdline
;
77 #ifdef GRUB_MACHINE_EFI
78 static grub_efi_uintn_t efi_mmap_size
;
80 static const grub_size_t efi_mmap_size
= 0;
91 static struct idt_descriptor idt_desc
=
98 static inline grub_size_t
99 page_align (grub_size_t size
)
101 return (size
+ (1 << 12) - 1) & (~((1 << 12) - 1));
104 #ifdef GRUB_MACHINE_EFI
105 /* Find the optimal number of pages for the memory map. Is it better to
106 move this code to efi/mm.c? */
107 static grub_efi_uintn_t
108 find_efi_mmap_size (void)
110 static grub_efi_uintn_t mmap_size
= 0;
115 mmap_size
= (1 << 12);
119 grub_efi_memory_descriptor_t
*mmap
;
120 grub_efi_uintn_t desc_size
;
121 grub_efi_uintn_t cur_mmap_size
= mmap_size
;
123 mmap
= grub_malloc (cur_mmap_size
);
127 ret
= grub_efi_get_memory_map (&cur_mmap_size
, mmap
, 0, &desc_size
, 0);
132 grub_error (GRUB_ERR_IO
, "cannot get memory map");
138 if (mmap_size
< cur_mmap_size
)
139 mmap_size
= cur_mmap_size
;
140 mmap_size
+= (1 << 12);
143 /* Increase the size a bit for safety, because GRUB allocates more on
144 later, and EFI itself may allocate more. */
145 mmap_size
+= (3 << 12);
147 mmap_size
= page_align (mmap_size
);
153 /* Helper for find_mmap_size. */
155 count_hook (grub_uint64_t addr
__attribute__ ((unused
)),
156 grub_uint64_t size
__attribute__ ((unused
)),
157 grub_memory_type_t type
__attribute__ ((unused
)), void *data
)
159 grub_size_t
*count
= data
;
165 /* Find the optimal number of pages for the memory map. */
167 find_mmap_size (void)
169 grub_size_t count
= 0, mmap_size
;
171 grub_mmap_iterate (count_hook
, &count
);
173 mmap_size
= count
* sizeof (struct grub_e820_mmap
);
175 /* Increase the size a bit for safety, because GRUB allocates more on
177 mmap_size
+= (1 << 12);
179 return page_align (mmap_size
);
185 grub_relocator_unload (relocator
);
187 prot_mode_mem
= initrd_mem
= 0;
188 prot_mode_target
= initrd_mem_target
= 0;
191 /* Allocate pages for the real mode code and the protected mode code
192 for linux as well as a memory map buffer. */
194 allocate_pages (grub_size_t prot_size
, grub_size_t
*align
,
195 grub_size_t min_align
, int relocatable
,
196 grub_uint64_t preferred_address
)
203 prot_size
= page_align (prot_size
);
205 /* Initialize the memory pointers with NULL for convenience. */
208 relocator
= grub_relocator_new ();
215 /* FIXME: Should request low memory from the heap when this feature is
219 grub_relocator_chunk_t ch
;
222 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
226 GRUB_RELOCATOR_PREFERENCE_LOW
,
228 for (; err
&& *align
+ 1 > min_align
; (*align
)--)
230 grub_errno
= GRUB_ERR_NONE
;
231 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
233 0xffffffff & ~prot_size
,
234 prot_size
, 1 << *align
,
235 GRUB_RELOCATOR_PREFERENCE_LOW
,
242 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
247 prot_mode_mem
= get_virtual_current_address (ch
);
248 prot_mode_target
= get_physical_target_address (ch
);
251 grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
252 prot_mode_mem
, (unsigned long) prot_mode_target
,
253 (unsigned) prot_size
);
254 return GRUB_ERR_NONE
;
262 grub_e820_add_region (struct grub_e820_mmap
*e820_map
, int *e820_num
,
263 grub_uint64_t start
, grub_uint64_t size
,
268 if ((n
> 0) && (e820_map
[n
- 1].addr
+ e820_map
[n
- 1].size
== start
) &&
269 (e820_map
[n
- 1].type
== type
))
270 e820_map
[n
- 1].size
+= size
;
273 e820_map
[n
].addr
= start
;
274 e820_map
[n
].size
= size
;
275 e820_map
[n
].type
= type
;
278 return GRUB_ERR_NONE
;
282 grub_linux_setup_video (struct linux_kernel_params
*params
)
284 struct grub_video_mode_info mode_info
;
287 grub_video_driver_id_t driver_id
;
288 const char *gfxlfbvar
= grub_env_get ("gfxpayloadforcelfb");
290 driver_id
= grub_video_get_driver_id ();
292 if (driver_id
== GRUB_VIDEO_DRIVER_NONE
)
295 err
= grub_video_get_info_and_fini (&mode_info
, &framebuffer
);
299 grub_errno
= GRUB_ERR_NONE
;
303 params
->lfb_width
= mode_info
.width
;
304 params
->lfb_height
= mode_info
.height
;
305 params
->lfb_depth
= mode_info
.bpp
;
306 params
->lfb_line_len
= mode_info
.pitch
;
308 params
->lfb_base
= (grub_size_t
) framebuffer
;
309 params
->lfb_size
= ALIGN_UP (params
->lfb_line_len
* params
->lfb_height
, 65536);
311 params
->red_mask_size
= mode_info
.red_mask_size
;
312 params
->red_field_pos
= mode_info
.red_field_pos
;
313 params
->green_mask_size
= mode_info
.green_mask_size
;
314 params
->green_field_pos
= mode_info
.green_field_pos
;
315 params
->blue_mask_size
= mode_info
.blue_mask_size
;
316 params
->blue_field_pos
= mode_info
.blue_field_pos
;
317 params
->reserved_mask_size
= mode_info
.reserved_mask_size
;
318 params
->reserved_field_pos
= mode_info
.reserved_field_pos
;
320 if (gfxlfbvar
&& (gfxlfbvar
[0] == '1' || gfxlfbvar
[0] == 'y'))
321 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
326 case GRUB_VIDEO_DRIVER_VBE
:
327 params
->lfb_size
>>= 16;
328 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_VESA
;
331 case GRUB_VIDEO_DRIVER_EFI_UGA
:
332 case GRUB_VIDEO_DRIVER_EFI_GOP
:
333 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_EFIFB
;
336 /* FIXME: check if better id is available. */
337 case GRUB_VIDEO_DRIVER_SM712
:
338 case GRUB_VIDEO_DRIVER_SIS315PRO
:
339 case GRUB_VIDEO_DRIVER_VGA
:
340 case GRUB_VIDEO_DRIVER_CIRRUS
:
341 case GRUB_VIDEO_DRIVER_BOCHS
:
342 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E
:
343 case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A
:
344 case GRUB_VIDEO_DRIVER_IEEE1275
:
345 case GRUB_VIDEO_DRIVER_COREBOOT
:
346 /* Make gcc happy. */
347 case GRUB_VIDEO_DRIVER_XEN
:
348 case GRUB_VIDEO_DRIVER_SDL
:
349 case GRUB_VIDEO_DRIVER_NONE
:
350 case GRUB_VIDEO_ADAPTER_CAPTURE
:
351 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
356 #ifdef GRUB_MACHINE_PCBIOS
357 /* VESA packed modes may come with zeroed mask sizes, which need
358 to be set here according to DAC Palette width. If we don't,
359 this results in Linux displaying a black screen. */
360 if (driver_id
== GRUB_VIDEO_DRIVER_VBE
&& mode_info
.bpp
<= 8)
362 struct grub_vbe_info_block controller_info
;
366 status
= grub_vbe_bios_get_controller_info (&controller_info
);
368 if (status
== GRUB_VBE_STATUS_OK
&&
369 (controller_info
.capabilities
& GRUB_VBE_CAPABILITY_DACWIDTH
))
370 status
= grub_vbe_bios_set_dac_palette_width (&width
);
372 if (status
!= GRUB_VBE_STATUS_OK
)
373 /* 6 is default after mode reset. */
376 params
->red_mask_size
= params
->green_mask_size
377 = params
->blue_mask_size
= width
;
378 params
->reserved_mask_size
= 0;
382 return GRUB_ERR_NONE
;
385 /* Context for grub_linux_boot. */
386 struct grub_linux_boot_ctx
388 grub_addr_t real_mode_target
;
389 grub_size_t real_size
;
390 struct linux_kernel_params
*params
;
394 /* Helper for grub_linux_boot. */
396 grub_linux_boot_mmap_find (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 /* We must put real mode code in the traditional space. */
402 if (type
!= GRUB_MEMORY_AVAILABLE
|| addr
> 0x90000)
405 if (addr
+ size
< 0x10000)
410 size
+= addr
- 0x10000;
414 if (addr
+ size
> 0x90000)
415 size
= 0x90000 - addr
;
417 if (ctx
->real_size
+ efi_mmap_size
> size
)
420 grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
421 (unsigned long) addr
,
423 (unsigned) (ctx
->real_size
+ efi_mmap_size
));
424 ctx
->real_mode_target
= ((addr
+ size
) - (ctx
->real_size
+ efi_mmap_size
));
428 /* GRUB types conveniently match E820 types. */
430 grub_linux_boot_mmap_fill (grub_uint64_t addr
, grub_uint64_t size
,
431 grub_memory_type_t type
, void *data
)
433 struct grub_linux_boot_ctx
*ctx
= data
;
435 if (grub_e820_add_region (ctx
->params
->e820_map
, &ctx
->e820_num
,
443 grub_linux_boot (void)
448 struct grub_relocator32_state state
;
450 struct grub_linux_boot_ctx ctx
= {
451 .real_mode_target
= 0
453 grub_size_t mmap_size
;
454 grub_size_t cl_offset
;
456 #ifdef GRUB_MACHINE_IEEE1275
458 const char *bootpath
;
461 bootpath
= grub_env_get ("root");
463 grub_ieee1275_set_property (grub_ieee1275_chosen
,
464 "bootpath", bootpath
,
465 grub_strlen (bootpath
) + 1,
467 linux_params
.ofw_signature
= GRUB_LINUX_OFW_SIGNATURE
;
468 linux_params
.ofw_num_items
= 1;
469 linux_params
.ofw_cif_handler
= (grub_uint32_t
) grub_ieee1275_entry_fn
;
470 linux_params
.ofw_idt
= 0;
474 modevar
= grub_env_get ("gfxpayload");
476 /* Now all graphical modes are acceptable.
477 May change in future if we have modes without framebuffer. */
478 if (modevar
&& *modevar
!= 0)
480 tmp
= grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE
, modevar
);
483 #if ACCEPTS_PURE_TEXT
484 err
= grub_video_set_mode (tmp
, 0, 0);
486 err
= grub_video_set_mode (tmp
, GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
490 else /* We can't go back to text mode from coreboot fb. */
491 #ifdef GRUB_MACHINE_COREBOOT
492 if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT
)
497 #if ACCEPTS_PURE_TEXT
498 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
, 0, 0);
500 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
,
501 GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
508 grub_puts_ (N_("Booting in blind mode"));
509 grub_errno
= GRUB_ERR_NONE
;
512 if (grub_linux_setup_video (&linux_params
))
514 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
515 linux_params
.have_vga
= GRUB_VIDEO_LINUX_TYPE_TEXT
;
516 linux_params
.video_mode
= 0x3;
518 linux_params
.have_vga
= 0;
519 linux_params
.video_mode
= 0;
520 linux_params
.video_width
= 0;
521 linux_params
.video_height
= 0;
526 #ifndef GRUB_MACHINE_IEEE1275
527 if (linux_params
.have_vga
== GRUB_VIDEO_LINUX_TYPE_TEXT
)
530 grub_term_output_t term
;
532 FOR_ACTIVE_TERM_OUTPUTS(term
)
533 if (grub_strcmp (term
->name
, "vga_text") == 0
534 || grub_strcmp (term
->name
, "console") == 0
535 || grub_strcmp (term
->name
, "ofconsole") == 0)
537 struct grub_term_coordinate pos
= grub_term_getxy (term
);
538 linux_params
.video_cursor_x
= pos
.x
;
539 linux_params
.video_cursor_y
= pos
.y
;
540 linux_params
.video_width
= grub_term_width (term
);
541 linux_params
.video_height
= grub_term_height (term
);
547 linux_params
.video_cursor_x
= 0;
548 linux_params
.video_cursor_y
= 0;
549 linux_params
.video_width
= 80;
550 linux_params
.video_height
= 25;
554 mmap_size
= find_mmap_size ();
555 /* Make sure that each size is aligned to a page boundary. */
556 cl_offset
= ALIGN_UP (mmap_size
+ sizeof (linux_params
), 4096);
557 if (cl_offset
< ((grub_size_t
) linux_params
.setup_sects
<< GRUB_DISK_SECTOR_BITS
))
558 cl_offset
= ALIGN_UP ((grub_size_t
) (linux_params
.setup_sects
559 << GRUB_DISK_SECTOR_BITS
), 4096);
560 ctx
.real_size
= ALIGN_UP (cl_offset
+ maximal_cmdline_size
, 4096);
562 #ifdef GRUB_MACHINE_EFI
563 efi_mmap_size
= find_efi_mmap_size ();
564 if (efi_mmap_size
== 0)
568 grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
569 (unsigned) ctx
.real_size
, (unsigned) mmap_size
);
571 #ifdef GRUB_MACHINE_EFI
572 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 1);
573 if (! ctx
.real_mode_target
)
574 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 0);
576 grub_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
);
578 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
579 (unsigned long) ctx
.real_mode_target
,
580 (unsigned) ctx
.real_size
,
581 (unsigned) efi_mmap_size
);
583 if (! ctx
.real_mode_target
)
584 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate real mode pages");
587 grub_relocator_chunk_t ch
;
588 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
589 ctx
.real_mode_target
,
590 (ctx
.real_size
+ efi_mmap_size
));
593 real_mode_mem
= get_virtual_current_address (ch
);
595 efi_mmap_buf
= (grub_uint8_t
*) real_mode_mem
+ ctx
.real_size
;
597 grub_dprintf ("linux", "real_mode_mem = %p\n",
600 ctx
.params
= real_mode_mem
;
602 *ctx
.params
= linux_params
;
603 ctx
.params
->cmd_line_ptr
= ctx
.real_mode_target
+ cl_offset
;
604 grub_memcpy ((char *) ctx
.params
+ cl_offset
, linux_cmdline
,
605 maximal_cmdline_size
);
607 grub_dprintf ("linux", "code32_start = %x\n",
608 (unsigned) ctx
.params
->code32_start
);
611 if (grub_mmap_iterate (grub_linux_boot_mmap_fill
, &ctx
))
613 ctx
.params
->mmap_size
= ctx
.e820_num
;
615 #ifdef GRUB_MACHINE_EFI
617 grub_efi_uintn_t efi_desc_size
;
618 grub_size_t efi_mmap_target
;
619 grub_efi_uint32_t efi_desc_version
;
620 err
= grub_efi_finish_boot_services (&efi_mmap_size
, efi_mmap_buf
, NULL
,
621 &efi_desc_size
, &efi_desc_version
);
625 /* Note that no boot services are available from here. */
626 efi_mmap_target
= ctx
.real_mode_target
627 + ((grub_uint8_t
*) efi_mmap_buf
- (grub_uint8_t
*) real_mode_mem
);
628 /* Pass EFI parameters. */
629 if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0208)
631 ctx
.params
->v0208
.efi_mem_desc_size
= efi_desc_size
;
632 ctx
.params
->v0208
.efi_mem_desc_version
= efi_desc_version
;
633 ctx
.params
->v0208
.efi_mmap
= efi_mmap_target
;
634 ctx
.params
->v0208
.efi_mmap_size
= efi_mmap_size
;
637 ctx
.params
->v0208
.efi_mmap_hi
= (efi_mmap_target
>> 32);
640 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0206)
642 ctx
.params
->v0206
.efi_mem_desc_size
= efi_desc_size
;
643 ctx
.params
->v0206
.efi_mem_desc_version
= efi_desc_version
;
644 ctx
.params
->v0206
.efi_mmap
= efi_mmap_target
;
645 ctx
.params
->v0206
.efi_mmap_size
= efi_mmap_size
;
647 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0204)
649 ctx
.params
->v0204
.efi_mem_desc_size
= efi_desc_size
;
650 ctx
.params
->v0204
.efi_mem_desc_version
= efi_desc_version
;
651 ctx
.params
->v0204
.efi_mmap
= efi_mmap_target
;
652 ctx
.params
->v0204
.efi_mmap_size
= efi_mmap_size
;
658 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
659 state
.ebp
= state
.edi
= state
.ebx
= 0;
660 state
.esi
= ctx
.real_mode_target
;
661 state
.esp
= ctx
.real_mode_target
;
662 state
.eip
= ctx
.params
->code32_start
;
663 return grub_relocator32_boot (relocator
, state
, 0);
667 grub_linux_unload (void)
669 grub_dl_unref (my_mod
);
671 grub_free (linux_cmdline
);
673 return GRUB_ERR_NONE
;
677 grub_cmd_linux (grub_command_t cmd
__attribute__ ((unused
)),
678 int argc
, char *argv
[])
680 grub_file_t file
= 0;
681 struct linux_kernel_header lh
;
682 grub_uint8_t setup_sects
;
683 grub_size_t real_size
, prot_size
, prot_file_size
;
686 grub_size_t align
, min_align
;
688 grub_uint64_t preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
690 grub_dl_ref (my_mod
);
694 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
698 file
= grub_file_open (argv
[0]);
702 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
705 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
710 if (lh
.boot_flag
!= grub_cpu_to_le16_compile_time (0xaa55))
712 grub_error (GRUB_ERR_BAD_OS
, "invalid magic number");
716 if (lh
.setup_sects
> GRUB_LINUX_MAX_SETUP_SECTS
)
718 grub_error (GRUB_ERR_BAD_OS
, "too many setup sectors");
722 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
723 still not support 32-bit boot. */
724 if (lh
.header
!= grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE
)
725 || grub_le_to_cpu16 (lh
.version
) < 0x0203)
727 grub_error (GRUB_ERR_BAD_OS
, "version too old for 32-bit boot"
728 #ifdef GRUB_MACHINE_PCBIOS
729 " (try with `linux16')"
735 if (! (lh
.loadflags
& GRUB_LINUX_FLAG_BIG_KERNEL
))
737 grub_error (GRUB_ERR_BAD_OS
, "zImage doesn't support 32-bit boot"
738 #ifdef GRUB_MACHINE_PCBIOS
739 " (try with `linux16')"
745 if (grub_le_to_cpu16 (lh
.version
) >= 0x0206)
746 maximal_cmdline_size
= grub_le_to_cpu32 (lh
.cmdline_size
) + 1;
748 maximal_cmdline_size
= 256;
750 if (maximal_cmdline_size
< 128)
751 maximal_cmdline_size
= 128;
753 setup_sects
= lh
.setup_sects
;
755 /* If SETUP_SECTS is not set, set it to the default (4). */
757 setup_sects
= GRUB_LINUX_DEFAULT_SETUP_SECTS
;
759 real_size
= setup_sects
<< GRUB_DISK_SECTOR_BITS
;
760 prot_file_size
= grub_file_size (file
) - real_size
- GRUB_DISK_SECTOR_SIZE
;
762 if (grub_le_to_cpu16 (lh
.version
) >= 0x205
763 && lh
.kernel_alignment
!= 0
764 && ((lh
.kernel_alignment
- 1) & lh
.kernel_alignment
) == 0)
766 for (align
= 0; align
< 32; align
++)
767 if (grub_le_to_cpu32 (lh
.kernel_alignment
) & (1 << align
))
769 relocatable
= grub_le_to_cpu32 (lh
.relocatable
);
777 if (grub_le_to_cpu16 (lh
.version
) >= 0x020a)
779 min_align
= lh
.min_alignment
;
780 prot_size
= grub_le_to_cpu32 (lh
.init_size
);
781 prot_init_space
= page_align (prot_size
);
783 preferred_address
= grub_le_to_cpu64 (lh
.pref_address
);
785 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
790 prot_size
= prot_file_size
;
791 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
792 /* Usually, the compression ratio is about 50%. */
793 prot_init_space
= page_align (prot_size
) * 3;
796 if (allocate_pages (prot_size
, &align
,
797 min_align
, relocatable
,
801 grub_memset (&linux_params
, 0, sizeof (linux_params
));
802 grub_memcpy (&linux_params
.setup_sects
, &lh
.setup_sects
, sizeof (lh
) - 0x1F1);
804 linux_params
.code32_start
= prot_mode_target
+ lh
.code32_start
- GRUB_LINUX_BZIMAGE_ADDR
;
805 linux_params
.kernel_alignment
= (1 << align
);
806 linux_params
.ps_mouse
= linux_params
.padding10
= 0;
808 len
= sizeof (linux_params
) - sizeof (lh
);
809 if (grub_file_read (file
, (char *) &linux_params
+ sizeof (lh
), len
) != len
)
812 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
817 linux_params
.type_of_loader
= GRUB_LINUX_BOOT_LOADER_TYPE
;
819 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
820 and otherwise ignored. */
821 linux_params
.cl_magic
= GRUB_LINUX_CL_MAGIC
;
822 linux_params
.cl_offset
= 0x1000;
824 linux_params
.ramdisk_image
= 0;
825 linux_params
.ramdisk_size
= 0;
827 linux_params
.heap_end_ptr
= GRUB_LINUX_HEAP_END_OFFSET
;
828 linux_params
.loadflags
|= GRUB_LINUX_FLAG_CAN_USE_HEAP
;
830 /* These are not needed to be precise, because Linux uses these values
831 only to raise an error when the decompression code cannot find good
833 linux_params
.ext_mem
= ((32 * 0x100000) >> 10);
834 linux_params
.alt_mem
= ((32 * 0x100000) >> 10);
836 /* Ignored by Linux. */
837 linux_params
.video_page
= 0;
839 /* Only used when `video_mode == 0x7', otherwise ignored. */
840 linux_params
.video_ega_bx
= 0;
842 linux_params
.font_size
= 16; /* XXX */
844 #ifdef GRUB_MACHINE_EFI
846 if (grub_le_to_cpu16 (linux_params
.version
) < 0x0208 &&
847 ((grub_addr_t
) grub_efi_system_table
>> 32) != 0)
848 return grub_error(GRUB_ERR_BAD_OS
,
849 "kernel does not support 64-bit addressing");
852 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0208)
854 linux_params
.v0208
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
855 linux_params
.v0208
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
857 linux_params
.v0208
.efi_system_table_hi
= (grub_uint32_t
) ((grub_uint64_t
) grub_efi_system_table
>> 32);
860 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0206)
862 linux_params
.v0206
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
863 linux_params
.v0206
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
865 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0204)
867 linux_params
.v0204
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE_0204
;
868 linux_params
.v0204
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
872 /* The other parameters are filled when booting. */
874 grub_file_seek (file
, real_size
+ GRUB_DISK_SECTOR_SIZE
);
876 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
877 (unsigned) real_size
, (unsigned) prot_size
);
879 /* Look for memory size and video mode specified on the command line. */
881 for (i
= 1; i
< argc
; i
++)
882 #ifdef GRUB_MACHINE_PCBIOS
883 if (grub_memcmp (argv
[i
], "vga=", 4) == 0)
885 /* Video mode selection support. */
886 char *val
= argv
[i
] + 4;
887 unsigned vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
888 struct grub_vesa_mode_table_entry
*linux_mode
;
892 grub_dl_load ("vbe");
894 if (grub_strcmp (val
, "normal") == 0)
895 vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
896 else if (grub_strcmp (val
, "ext") == 0)
897 vid_mode
= GRUB_LINUX_VID_MODE_EXTENDED
;
898 else if (grub_strcmp (val
, "ask") == 0)
900 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
902 /* We usually would never do this in a loader, but "vga=ask" means user
903 requested interaction, so it can't hurt to request keyboard input. */
904 grub_wait_after_message ();
909 vid_mode
= (grub_uint16_t
) grub_strtoul (val
, 0, 0);
914 case GRUB_LINUX_VID_MODE_NORMAL
:
915 grub_env_set ("gfxpayload", "text");
916 grub_printf_ (N_("%s is deprecated. "
917 "Use set gfxpayload=%s before "
918 "linux command instead.\n"),
923 case GRUB_LINUX_VID_MODE_EXTENDED
:
924 /* FIXME: support 80x50 text. */
925 grub_env_set ("gfxpayload", "text");
926 grub_printf_ (N_("%s is deprecated. "
927 "Use set gfxpayload=%s before "
928 "linux command instead.\n"),
932 /* Ignore invalid values. */
933 if (vid_mode
< GRUB_VESA_MODE_TABLE_START
||
934 vid_mode
> GRUB_VESA_MODE_TABLE_END
)
936 grub_env_set ("gfxpayload", "text");
937 /* TRANSLATORS: "x" has to be entered in, like an identifier,
938 so please don't use better Unicode codepoints. */
939 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
940 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
941 "before linux command instead.\n"),
946 linux_mode
= &grub_vesa_mode_table
[vid_mode
947 - GRUB_VESA_MODE_TABLE_START
];
949 buf
= grub_xasprintf ("%ux%ux%u,%ux%u",
950 linux_mode
->width
, linux_mode
->height
,
952 linux_mode
->width
, linux_mode
->height
);
956 grub_printf_ (N_("%s is deprecated. "
957 "Use set gfxpayload=%s before "
958 "linux command instead.\n"),
960 err
= grub_env_set ("gfxpayload", buf
);
967 #endif /* GRUB_MACHINE_PCBIOS */
968 if (grub_memcmp (argv
[i
], "mem=", 4) == 0)
970 char *val
= argv
[i
] + 4;
972 linux_mem_size
= grub_strtoul (val
, &val
, 0);
976 grub_errno
= GRUB_ERR_NONE
;
983 switch (grub_tolower (val
[0]))
998 /* Check an overflow. */
999 if (linux_mem_size
> (~0UL >> shift
))
1002 linux_mem_size
<<= shift
;
1005 else if (grub_memcmp (argv
[i
], "quiet", sizeof ("quiet") - 1) == 0)
1007 linux_params
.loadflags
|= GRUB_LINUX_FLAG_QUIET
;
1010 /* Create kernel command line. */
1011 linux_cmdline
= grub_zalloc (maximal_cmdline_size
+ 1);
1014 grub_memcpy (linux_cmdline
, LINUX_IMAGE
, sizeof (LINUX_IMAGE
));
1015 grub_create_loader_cmdline (argc
, argv
,
1017 + sizeof (LINUX_IMAGE
) - 1,
1018 maximal_cmdline_size
1019 - (sizeof (LINUX_IMAGE
) - 1));
1021 len
= prot_file_size
;
1022 if (grub_file_read (file
, prot_mode_mem
, len
) != len
&& !grub_errno
)
1023 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1026 if (grub_errno
== GRUB_ERR_NONE
)
1028 grub_loader_set (grub_linux_boot
, grub_linux_unload
,
1029 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
1036 grub_file_close (file
);
1038 if (grub_errno
!= GRUB_ERR_NONE
)
1040 grub_dl_unref (my_mod
);
1048 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1049 int argc
, char *argv
[])
1051 grub_size_t size
= 0, aligned_size
= 0;
1052 grub_addr_t addr_min
, addr_max
;
1055 struct grub_linux_initrd_context initrd_ctx
= { 0, 0, 0 };
1059 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
1065 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("you need to load the kernel first"));
1069 if (grub_initrd_init (argc
, argv
, &initrd_ctx
))
1072 size
= grub_get_initrd_size (&initrd_ctx
);
1073 aligned_size
= ALIGN_UP (size
, 4096);
1075 /* Get the highest address available for the initrd. */
1076 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0203)
1078 addr_max
= grub_cpu_to_le32 (linux_params
.initrd_addr_max
);
1080 /* XXX in reality, Linux specifies a bogus value, so
1081 it is necessary to make sure that ADDR_MAX does not exceed
1083 if (addr_max
> GRUB_LINUX_INITRD_MAX_ADDRESS
)
1084 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1087 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1089 if (linux_mem_size
!= 0 && linux_mem_size
< addr_max
)
1090 addr_max
= linux_mem_size
;
1092 /* Linux 2.3.xx has a bug in the memory range check, so avoid
1094 Linux 2.2.xx has a bug in the memory range check, which is
1095 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
1096 addr_max
-= 0x10000;
1098 addr_min
= (grub_addr_t
) prot_mode_target
+ prot_init_space
;
1100 /* Put the initrd as high as possible, 4KiB aligned. */
1101 addr
= (addr_max
- aligned_size
) & ~0xFFF;
1103 if (addr
< addr_min
)
1105 grub_error (GRUB_ERR_OUT_OF_RANGE
, "the initrd is too big");
1110 grub_relocator_chunk_t ch
;
1111 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
1112 addr_min
, addr
, aligned_size
,
1114 GRUB_RELOCATOR_PREFERENCE_HIGH
,
1118 initrd_mem
= get_virtual_current_address (ch
);
1119 initrd_mem_target
= get_physical_target_address (ch
);
1122 if (grub_initrd_load (&initrd_ctx
, argv
, initrd_mem
))
1125 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1126 (unsigned) addr
, (unsigned) size
);
1128 linux_params
.ramdisk_image
= initrd_mem_target
;
1129 linux_params
.ramdisk_size
= size
;
1130 linux_params
.root_dev
= 0x0100; /* XXX */
1133 grub_initrd_close (&initrd_ctx
);
1138 static grub_command_t cmd_linux
, cmd_initrd
;
1140 GRUB_MOD_INIT(linux
)
1142 cmd_linux
= grub_register_command ("linux", grub_cmd_linux
,
1143 0, N_("Load Linux."));
1144 cmd_initrd
= grub_register_command ("initrd", grub_cmd_initrd
,
1145 0, N_("Load initrd."));
1149 GRUB_MOD_FINI(linux
)
1151 grub_unregister_command (cmd_linux
);
1152 grub_unregister_command (cmd_initrd
);