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 grub_uint32_t initrd_pages
;
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;
92 static struct idt_descriptor idt_desc
=
99 static inline grub_size_t
100 page_align (grub_size_t size
)
102 return (size
+ (1 << 12) - 1) & (~((1 << 12) - 1));
105 #ifdef GRUB_MACHINE_EFI
106 /* Find the optimal number of pages for the memory map. Is it better to
107 move this code to efi/mm.c? */
108 static grub_efi_uintn_t
109 find_efi_mmap_size (void)
111 static grub_efi_uintn_t mmap_size
= 0;
116 mmap_size
= (1 << 12);
120 grub_efi_memory_descriptor_t
*mmap
;
121 grub_efi_uintn_t desc_size
;
122 grub_efi_uintn_t cur_mmap_size
= mmap_size
;
124 mmap
= grub_malloc (cur_mmap_size
);
128 ret
= grub_efi_get_memory_map (&cur_mmap_size
, mmap
, 0, &desc_size
, 0);
133 grub_error (GRUB_ERR_IO
, "cannot get memory map");
139 if (mmap_size
< cur_mmap_size
)
140 mmap_size
= cur_mmap_size
;
141 mmap_size
+= (1 << 12);
144 /* Increase the size a bit for safety, because GRUB allocates more on
145 later, and EFI itself may allocate more. */
146 mmap_size
+= (3 << 12);
148 mmap_size
= page_align (mmap_size
);
154 /* Helper for find_mmap_size. */
156 count_hook (grub_uint64_t addr
__attribute__ ((unused
)),
157 grub_uint64_t size
__attribute__ ((unused
)),
158 grub_memory_type_t type
__attribute__ ((unused
)), void *data
)
160 grub_size_t
*count
= data
;
166 /* Find the optimal number of pages for the memory map. */
168 find_mmap_size (void)
170 grub_size_t count
= 0, mmap_size
;
172 grub_mmap_iterate (count_hook
, &count
);
174 mmap_size
= count
* sizeof (struct grub_e820_mmap
);
176 /* Increase the size a bit for safety, because GRUB allocates more on
178 mmap_size
+= (1 << 12);
180 return page_align (mmap_size
);
186 grub_relocator_unload (relocator
);
188 prot_mode_mem
= initrd_mem
= 0;
189 prot_mode_target
= initrd_mem_target
= 0;
192 /* Allocate pages for the real mode code and the protected mode code
193 for linux as well as a memory map buffer. */
195 allocate_pages (grub_size_t prot_size
, grub_size_t
*align
,
196 grub_size_t min_align
, int relocatable
,
197 grub_uint64_t preferred_address
)
204 prot_size
= page_align (prot_size
);
206 /* Initialize the memory pointers with NULL for convenience. */
209 relocator
= grub_relocator_new ();
216 /* FIXME: Should request low memory from the heap when this feature is
220 grub_relocator_chunk_t ch
;
223 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
227 GRUB_RELOCATOR_PREFERENCE_LOW
,
229 for (; err
&& *align
+ 1 > min_align
; (*align
)--)
231 grub_errno
= GRUB_ERR_NONE
;
232 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
234 0xffffffff & ~prot_size
,
235 prot_size
, 1 << *align
,
236 GRUB_RELOCATOR_PREFERENCE_LOW
,
243 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
248 prot_mode_mem
= get_virtual_current_address (ch
);
249 prot_mode_target
= get_physical_target_address (ch
);
252 grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
253 prot_mode_mem
, (unsigned long) prot_mode_target
,
254 (unsigned) prot_size
);
255 return GRUB_ERR_NONE
;
263 grub_e820_add_region (struct grub_e820_mmap
*e820_map
, int *e820_num
,
264 grub_uint64_t start
, grub_uint64_t size
,
269 if ((n
> 0) && (e820_map
[n
- 1].addr
+ e820_map
[n
- 1].size
== start
) &&
270 (e820_map
[n
- 1].type
== type
))
271 e820_map
[n
- 1].size
+= size
;
274 e820_map
[n
].addr
= start
;
275 e820_map
[n
].size
= size
;
276 e820_map
[n
].type
= type
;
279 return GRUB_ERR_NONE
;
283 grub_linux_setup_video (struct linux_kernel_params
*params
)
285 struct grub_video_mode_info mode_info
;
288 grub_video_driver_id_t driver_id
;
289 const char *gfxlfbvar
= grub_env_get ("gfxpayloadforcelfb");
291 driver_id
= grub_video_get_driver_id ();
293 if (driver_id
== GRUB_VIDEO_DRIVER_NONE
)
296 err
= grub_video_get_info_and_fini (&mode_info
, &framebuffer
);
300 grub_errno
= GRUB_ERR_NONE
;
304 params
->lfb_width
= mode_info
.width
;
305 params
->lfb_height
= mode_info
.height
;
306 params
->lfb_depth
= mode_info
.bpp
;
307 params
->lfb_line_len
= mode_info
.pitch
;
309 params
->lfb_base
= (grub_size_t
) framebuffer
;
310 params
->lfb_size
= ALIGN_UP (params
->lfb_line_len
* params
->lfb_height
, 65536);
312 params
->red_mask_size
= mode_info
.red_mask_size
;
313 params
->red_field_pos
= mode_info
.red_field_pos
;
314 params
->green_mask_size
= mode_info
.green_mask_size
;
315 params
->green_field_pos
= mode_info
.green_field_pos
;
316 params
->blue_mask_size
= mode_info
.blue_mask_size
;
317 params
->blue_field_pos
= mode_info
.blue_field_pos
;
318 params
->reserved_mask_size
= mode_info
.reserved_mask_size
;
319 params
->reserved_field_pos
= mode_info
.reserved_field_pos
;
321 if (gfxlfbvar
&& (gfxlfbvar
[0] == '1' || gfxlfbvar
[0] == 'y'))
322 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
327 case GRUB_VIDEO_DRIVER_VBE
:
328 params
->lfb_size
>>= 16;
329 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_VESA
;
332 case GRUB_VIDEO_DRIVER_EFI_UGA
:
333 case GRUB_VIDEO_DRIVER_EFI_GOP
:
334 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_EFIFB
;
337 /* FIXME: check if better id is available. */
338 case GRUB_VIDEO_DRIVER_SM712
:
339 case GRUB_VIDEO_DRIVER_SIS315PRO
:
340 case GRUB_VIDEO_DRIVER_VGA
:
341 case GRUB_VIDEO_DRIVER_CIRRUS
:
342 case GRUB_VIDEO_DRIVER_BOCHS
:
343 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E
:
344 case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A
:
345 case GRUB_VIDEO_DRIVER_IEEE1275
:
346 case GRUB_VIDEO_DRIVER_COREBOOT
:
347 /* Make gcc happy. */
348 case GRUB_VIDEO_DRIVER_XEN
:
349 case GRUB_VIDEO_DRIVER_SDL
:
350 case GRUB_VIDEO_DRIVER_NONE
:
351 case GRUB_VIDEO_ADAPTER_CAPTURE
:
352 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
357 #ifdef GRUB_MACHINE_PCBIOS
358 /* VESA packed modes may come with zeroed mask sizes, which need
359 to be set here according to DAC Palette width. If we don't,
360 this results in Linux displaying a black screen. */
361 if (driver_id
== GRUB_VIDEO_DRIVER_VBE
&& mode_info
.bpp
<= 8)
363 struct grub_vbe_info_block controller_info
;
367 status
= grub_vbe_bios_get_controller_info (&controller_info
);
369 if (status
== GRUB_VBE_STATUS_OK
&&
370 (controller_info
.capabilities
& GRUB_VBE_CAPABILITY_DACWIDTH
))
371 status
= grub_vbe_bios_set_dac_palette_width (&width
);
373 if (status
!= GRUB_VBE_STATUS_OK
)
374 /* 6 is default after mode reset. */
377 params
->red_mask_size
= params
->green_mask_size
378 = params
->blue_mask_size
= width
;
379 params
->reserved_mask_size
= 0;
383 return GRUB_ERR_NONE
;
386 /* Context for grub_linux_boot. */
387 struct grub_linux_boot_ctx
389 grub_addr_t real_mode_target
;
390 grub_size_t real_size
;
391 struct linux_kernel_params
*params
;
395 /* Helper for grub_linux_boot. */
397 grub_linux_boot_mmap_find (grub_uint64_t addr
, grub_uint64_t size
,
398 grub_memory_type_t type
, void *data
)
400 struct grub_linux_boot_ctx
*ctx
= data
;
402 /* We must put real mode code in the traditional space. */
403 if (type
!= GRUB_MEMORY_AVAILABLE
|| addr
> 0x90000)
406 if (addr
+ size
< 0x10000)
411 size
+= addr
- 0x10000;
415 if (addr
+ size
> 0x90000)
416 size
= 0x90000 - addr
;
418 if (ctx
->real_size
+ efi_mmap_size
> size
)
421 grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
422 (unsigned long) addr
,
424 (unsigned) (ctx
->real_size
+ efi_mmap_size
));
425 ctx
->real_mode_target
= ((addr
+ size
) - (ctx
->real_size
+ efi_mmap_size
));
429 /* GRUB types conveniently match E820 types. */
431 grub_linux_boot_mmap_fill (grub_uint64_t addr
, grub_uint64_t size
,
432 grub_memory_type_t type
, void *data
)
434 struct grub_linux_boot_ctx
*ctx
= data
;
436 if (grub_e820_add_region (ctx
->params
->e820_map
, &ctx
->e820_num
,
444 grub_linux_boot (void)
449 struct grub_relocator32_state state
;
451 struct grub_linux_boot_ctx ctx
= {
452 .real_mode_target
= 0
454 grub_size_t mmap_size
;
455 grub_size_t cl_offset
;
457 #ifdef GRUB_MACHINE_IEEE1275
459 const char *bootpath
;
462 bootpath
= grub_env_get ("root");
464 grub_ieee1275_set_property (grub_ieee1275_chosen
,
465 "bootpath", bootpath
,
466 grub_strlen (bootpath
) + 1,
468 linux_params
.ofw_signature
= GRUB_LINUX_OFW_SIGNATURE
;
469 linux_params
.ofw_num_items
= 1;
470 linux_params
.ofw_cif_handler
= (grub_uint32_t
) grub_ieee1275_entry_fn
;
471 linux_params
.ofw_idt
= 0;
475 modevar
= grub_env_get ("gfxpayload");
477 /* Now all graphical modes are acceptable.
478 May change in future if we have modes without framebuffer. */
479 if (modevar
&& *modevar
!= 0)
481 tmp
= grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE
, modevar
);
484 #if ACCEPTS_PURE_TEXT
485 err
= grub_video_set_mode (tmp
, 0, 0);
487 err
= grub_video_set_mode (tmp
, GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
491 else /* We can't go back to text mode from coreboot fb. */
492 #ifdef GRUB_MACHINE_COREBOOT
493 if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT
)
498 #if ACCEPTS_PURE_TEXT
499 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
, 0, 0);
501 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
,
502 GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
509 grub_puts_ (N_("Booting in blind mode"));
510 grub_errno
= GRUB_ERR_NONE
;
513 if (grub_linux_setup_video (&linux_params
))
515 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
516 linux_params
.have_vga
= GRUB_VIDEO_LINUX_TYPE_TEXT
;
517 linux_params
.video_mode
= 0x3;
519 linux_params
.have_vga
= 0;
520 linux_params
.video_mode
= 0;
521 linux_params
.video_width
= 0;
522 linux_params
.video_height
= 0;
527 #ifndef GRUB_MACHINE_IEEE1275
528 if (linux_params
.have_vga
== GRUB_VIDEO_LINUX_TYPE_TEXT
)
531 grub_term_output_t term
;
533 FOR_ACTIVE_TERM_OUTPUTS(term
)
534 if (grub_strcmp (term
->name
, "vga_text") == 0
535 || grub_strcmp (term
->name
, "console") == 0
536 || grub_strcmp (term
->name
, "ofconsole") == 0)
538 struct grub_term_coordinate pos
= grub_term_getxy (term
);
539 linux_params
.video_cursor_x
= pos
.x
;
540 linux_params
.video_cursor_y
= pos
.y
;
541 linux_params
.video_width
= grub_term_width (term
);
542 linux_params
.video_height
= grub_term_height (term
);
548 linux_params
.video_cursor_x
= 0;
549 linux_params
.video_cursor_y
= 0;
550 linux_params
.video_width
= 80;
551 linux_params
.video_height
= 25;
555 mmap_size
= find_mmap_size ();
556 /* Make sure that each size is aligned to a page boundary. */
557 cl_offset
= ALIGN_UP (mmap_size
+ sizeof (linux_params
), 4096);
558 if (cl_offset
< ((grub_size_t
) linux_params
.setup_sects
<< GRUB_DISK_SECTOR_BITS
))
559 cl_offset
= ALIGN_UP ((grub_size_t
) (linux_params
.setup_sects
560 << GRUB_DISK_SECTOR_BITS
), 4096);
561 ctx
.real_size
= ALIGN_UP (cl_offset
+ maximal_cmdline_size
, 4096);
563 #ifdef GRUB_MACHINE_EFI
564 efi_mmap_size
= find_efi_mmap_size ();
565 if (efi_mmap_size
== 0)
569 grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
570 (unsigned) ctx
.real_size
, (unsigned) mmap_size
);
572 #ifdef GRUB_MACHINE_EFI
573 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 1);
574 if (! ctx
.real_mode_target
)
575 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 0);
577 grub_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
);
579 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
580 (unsigned long) ctx
.real_mode_target
,
581 (unsigned) ctx
.real_size
,
582 (unsigned) efi_mmap_size
);
584 if (! ctx
.real_mode_target
)
585 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate real mode pages");
588 grub_relocator_chunk_t ch
;
589 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
590 ctx
.real_mode_target
,
591 (ctx
.real_size
+ efi_mmap_size
));
594 real_mode_mem
= get_virtual_current_address (ch
);
596 efi_mmap_buf
= (grub_uint8_t
*) real_mode_mem
+ ctx
.real_size
;
598 grub_dprintf ("linux", "real_mode_mem = %p\n",
601 ctx
.params
= real_mode_mem
;
603 *ctx
.params
= linux_params
;
604 ctx
.params
->cmd_line_ptr
= ctx
.real_mode_target
+ cl_offset
;
605 grub_memcpy ((char *) ctx
.params
+ cl_offset
, linux_cmdline
,
606 maximal_cmdline_size
);
608 grub_dprintf ("linux", "code32_start = %x\n",
609 (unsigned) ctx
.params
->code32_start
);
612 if (grub_mmap_iterate (grub_linux_boot_mmap_fill
, &ctx
))
614 ctx
.params
->mmap_size
= ctx
.e820_num
;
616 #ifdef GRUB_MACHINE_EFI
618 grub_efi_uintn_t efi_desc_size
;
619 grub_size_t efi_mmap_target
;
620 grub_efi_uint32_t efi_desc_version
;
621 err
= grub_efi_finish_boot_services (&efi_mmap_size
, efi_mmap_buf
, NULL
,
622 &efi_desc_size
, &efi_desc_version
);
626 /* Note that no boot services are available from here. */
627 efi_mmap_target
= ctx
.real_mode_target
628 + ((grub_uint8_t
*) efi_mmap_buf
- (grub_uint8_t
*) real_mode_mem
);
629 /* Pass EFI parameters. */
630 if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0208)
632 ctx
.params
->v0208
.efi_mem_desc_size
= efi_desc_size
;
633 ctx
.params
->v0208
.efi_mem_desc_version
= efi_desc_version
;
634 ctx
.params
->v0208
.efi_mmap
= efi_mmap_target
;
635 ctx
.params
->v0208
.efi_mmap_size
= efi_mmap_size
;
638 ctx
.params
->v0208
.efi_mmap_hi
= (efi_mmap_target
>> 32);
641 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0206)
643 ctx
.params
->v0206
.efi_mem_desc_size
= efi_desc_size
;
644 ctx
.params
->v0206
.efi_mem_desc_version
= efi_desc_version
;
645 ctx
.params
->v0206
.efi_mmap
= efi_mmap_target
;
646 ctx
.params
->v0206
.efi_mmap_size
= efi_mmap_size
;
648 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0204)
650 ctx
.params
->v0204
.efi_mem_desc_size
= efi_desc_size
;
651 ctx
.params
->v0204
.efi_mem_desc_version
= efi_desc_version
;
652 ctx
.params
->v0204
.efi_mmap
= efi_mmap_target
;
653 ctx
.params
->v0204
.efi_mmap_size
= efi_mmap_size
;
659 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
660 state
.ebp
= state
.edi
= state
.ebx
= 0;
661 state
.esi
= ctx
.real_mode_target
;
662 state
.esp
= ctx
.real_mode_target
;
663 state
.eip
= ctx
.params
->code32_start
;
664 return grub_relocator32_boot (relocator
, state
, 0);
668 grub_linux_unload (void)
670 grub_dl_unref (my_mod
);
672 grub_free (linux_cmdline
);
674 return GRUB_ERR_NONE
;
678 grub_cmd_linux (grub_command_t cmd
__attribute__ ((unused
)),
679 int argc
, char *argv
[])
681 grub_file_t file
= 0;
682 struct linux_kernel_header lh
;
683 grub_uint8_t setup_sects
;
684 grub_size_t real_size
, prot_size
, prot_file_size
;
687 grub_size_t align
, min_align
;
689 grub_uint64_t preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
691 grub_dl_ref (my_mod
);
695 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
699 file
= grub_file_open (argv
[0]);
703 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
706 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
711 if (lh
.boot_flag
!= grub_cpu_to_le16_compile_time (0xaa55))
713 grub_error (GRUB_ERR_BAD_OS
, "invalid magic number");
717 if (lh
.setup_sects
> GRUB_LINUX_MAX_SETUP_SECTS
)
719 grub_error (GRUB_ERR_BAD_OS
, "too many setup sectors");
723 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
724 still not support 32-bit boot. */
725 if (lh
.header
!= grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE
)
726 || grub_le_to_cpu16 (lh
.version
) < 0x0203)
728 grub_error (GRUB_ERR_BAD_OS
, "version too old for 32-bit boot"
729 #ifdef GRUB_MACHINE_PCBIOS
730 " (try with `linux16')"
736 if (! (lh
.loadflags
& GRUB_LINUX_FLAG_BIG_KERNEL
))
738 grub_error (GRUB_ERR_BAD_OS
, "zImage doesn't support 32-bit boot"
739 #ifdef GRUB_MACHINE_PCBIOS
740 " (try with `linux16')"
746 if (grub_le_to_cpu16 (lh
.version
) >= 0x0206)
747 maximal_cmdline_size
= grub_le_to_cpu32 (lh
.cmdline_size
) + 1;
749 maximal_cmdline_size
= 256;
751 if (maximal_cmdline_size
< 128)
752 maximal_cmdline_size
= 128;
754 setup_sects
= lh
.setup_sects
;
756 /* If SETUP_SECTS is not set, set it to the default (4). */
758 setup_sects
= GRUB_LINUX_DEFAULT_SETUP_SECTS
;
760 real_size
= setup_sects
<< GRUB_DISK_SECTOR_BITS
;
761 prot_file_size
= grub_file_size (file
) - real_size
- GRUB_DISK_SECTOR_SIZE
;
763 if (grub_le_to_cpu16 (lh
.version
) >= 0x205
764 && lh
.kernel_alignment
!= 0
765 && ((lh
.kernel_alignment
- 1) & lh
.kernel_alignment
) == 0)
767 for (align
= 0; align
< 32; align
++)
768 if (grub_le_to_cpu32 (lh
.kernel_alignment
) & (1 << align
))
770 relocatable
= grub_le_to_cpu32 (lh
.relocatable
);
778 if (grub_le_to_cpu16 (lh
.version
) >= 0x020a)
780 min_align
= lh
.min_alignment
;
781 prot_size
= grub_le_to_cpu32 (lh
.init_size
);
782 prot_init_space
= page_align (prot_size
);
784 preferred_address
= grub_le_to_cpu64 (lh
.pref_address
);
786 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
791 prot_size
= prot_file_size
;
792 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
793 /* Usually, the compression ratio is about 50%. */
794 prot_init_space
= page_align (prot_size
) * 3;
797 if (allocate_pages (prot_size
, &align
,
798 min_align
, relocatable
,
802 grub_memset (&linux_params
, 0, sizeof (linux_params
));
803 grub_memcpy (&linux_params
.setup_sects
, &lh
.setup_sects
, sizeof (lh
) - 0x1F1);
805 linux_params
.code32_start
= prot_mode_target
+ lh
.code32_start
- GRUB_LINUX_BZIMAGE_ADDR
;
806 linux_params
.kernel_alignment
= (1 << align
);
807 linux_params
.ps_mouse
= linux_params
.padding10
= 0;
809 len
= sizeof (linux_params
) - sizeof (lh
);
810 if (grub_file_read (file
, (char *) &linux_params
+ sizeof (lh
), len
) != len
)
813 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
818 linux_params
.type_of_loader
= GRUB_LINUX_BOOT_LOADER_TYPE
;
820 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
821 and otherwise ignored. */
822 linux_params
.cl_magic
= GRUB_LINUX_CL_MAGIC
;
823 linux_params
.cl_offset
= 0x1000;
825 linux_params
.ramdisk_image
= 0;
826 linux_params
.ramdisk_size
= 0;
828 linux_params
.heap_end_ptr
= GRUB_LINUX_HEAP_END_OFFSET
;
829 linux_params
.loadflags
|= GRUB_LINUX_FLAG_CAN_USE_HEAP
;
831 /* These are not needed to be precise, because Linux uses these values
832 only to raise an error when the decompression code cannot find good
834 linux_params
.ext_mem
= ((32 * 0x100000) >> 10);
835 linux_params
.alt_mem
= ((32 * 0x100000) >> 10);
837 /* Ignored by Linux. */
838 linux_params
.video_page
= 0;
840 /* Only used when `video_mode == 0x7', otherwise ignored. */
841 linux_params
.video_ega_bx
= 0;
843 linux_params
.font_size
= 16; /* XXX */
845 #ifdef GRUB_MACHINE_EFI
847 if (grub_le_to_cpu16 (linux_params
.version
) < 0x0208 &&
848 ((grub_addr_t
) grub_efi_system_table
>> 32) != 0)
849 return grub_error(GRUB_ERR_BAD_OS
,
850 "kernel does not support 64-bit addressing");
853 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0208)
855 linux_params
.v0208
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
856 linux_params
.v0208
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
858 linux_params
.v0208
.efi_system_table_hi
= (grub_uint32_t
) ((grub_uint64_t
) grub_efi_system_table
>> 32);
861 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0206)
863 linux_params
.v0206
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
864 linux_params
.v0206
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
866 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0204)
868 linux_params
.v0204
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE_0204
;
869 linux_params
.v0204
.efi_system_table
= (grub_uint32_t
) (grub_addr_t
) grub_efi_system_table
;
873 /* The other parameters are filled when booting. */
875 grub_file_seek (file
, real_size
+ GRUB_DISK_SECTOR_SIZE
);
877 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
878 (unsigned) real_size
, (unsigned) prot_size
);
880 /* Look for memory size and video mode specified on the command line. */
882 for (i
= 1; i
< argc
; i
++)
883 #ifdef GRUB_MACHINE_PCBIOS
884 if (grub_memcmp (argv
[i
], "vga=", 4) == 0)
886 /* Video mode selection support. */
887 char *val
= argv
[i
] + 4;
888 unsigned vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
889 struct grub_vesa_mode_table_entry
*linux_mode
;
893 grub_dl_load ("vbe");
895 if (grub_strcmp (val
, "normal") == 0)
896 vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
897 else if (grub_strcmp (val
, "ext") == 0)
898 vid_mode
= GRUB_LINUX_VID_MODE_EXTENDED
;
899 else if (grub_strcmp (val
, "ask") == 0)
901 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
903 /* We usually would never do this in a loader, but "vga=ask" means user
904 requested interaction, so it can't hurt to request keyboard input. */
905 grub_wait_after_message ();
910 vid_mode
= (grub_uint16_t
) grub_strtoul (val
, 0, 0);
915 case GRUB_LINUX_VID_MODE_NORMAL
:
916 grub_env_set ("gfxpayload", "text");
917 grub_printf_ (N_("%s is deprecated. "
918 "Use set gfxpayload=%s before "
919 "linux command instead.\n"), "text",
924 case GRUB_LINUX_VID_MODE_EXTENDED
:
925 /* FIXME: support 80x50 text. */
926 grub_env_set ("gfxpayload", "text");
927 grub_printf_ (N_("%s is deprecated. "
928 "Use set gfxpayload=%s before "
929 "linux command instead.\n"), "text",
933 /* Ignore invalid values. */
934 if (vid_mode
< GRUB_VESA_MODE_TABLE_START
||
935 vid_mode
> GRUB_VESA_MODE_TABLE_END
)
937 grub_env_set ("gfxpayload", "text");
938 /* TRANSLATORS: "x" has to be entered in, like an identifier,
939 so please don't use better Unicode codepoints. */
940 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
941 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
942 "before linux command instead.\n"),
947 linux_mode
= &grub_vesa_mode_table
[vid_mode
948 - GRUB_VESA_MODE_TABLE_START
];
950 buf
= grub_xasprintf ("%ux%ux%u,%ux%u",
951 linux_mode
->width
, linux_mode
->height
,
953 linux_mode
->width
, linux_mode
->height
);
957 grub_printf_ (N_("%s is deprecated. "
958 "Use set gfxpayload=%s before "
959 "linux command instead.\n"),
961 err
= grub_env_set ("gfxpayload", buf
);
968 #endif /* GRUB_MACHINE_PCBIOS */
969 if (grub_memcmp (argv
[i
], "mem=", 4) == 0)
971 char *val
= argv
[i
] + 4;
973 linux_mem_size
= grub_strtoul (val
, &val
, 0);
977 grub_errno
= GRUB_ERR_NONE
;
984 switch (grub_tolower (val
[0]))
996 /* Check an overflow. */
997 if (linux_mem_size
> (~0UL >> shift
))
1000 linux_mem_size
<<= shift
;
1003 else if (grub_memcmp (argv
[i
], "quiet", sizeof ("quiet") - 1) == 0)
1005 linux_params
.loadflags
|= GRUB_LINUX_FLAG_QUIET
;
1008 /* Create kernel command line. */
1009 linux_cmdline
= grub_zalloc (maximal_cmdline_size
+ 1);
1012 grub_memcpy (linux_cmdline
, LINUX_IMAGE
, sizeof (LINUX_IMAGE
));
1013 grub_create_loader_cmdline (argc
, argv
,
1015 + sizeof (LINUX_IMAGE
) - 1,
1016 maximal_cmdline_size
1017 - (sizeof (LINUX_IMAGE
) - 1));
1019 len
= prot_file_size
;
1020 if (grub_file_read (file
, prot_mode_mem
, len
) != len
&& !grub_errno
)
1021 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1024 if (grub_errno
== GRUB_ERR_NONE
)
1026 grub_loader_set (grub_linux_boot
, grub_linux_unload
,
1027 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
1034 grub_file_close (file
);
1036 if (grub_errno
!= GRUB_ERR_NONE
)
1038 grub_dl_unref (my_mod
);
1046 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1047 int argc
, char *argv
[])
1049 grub_size_t size
= 0;
1050 grub_addr_t addr_min
, addr_max
;
1053 struct grub_linux_initrd_context initrd_ctx
= { 0, 0, 0 };
1057 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
1063 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("you need to load the kernel first"));
1067 if (grub_initrd_init (argc
, argv
, &initrd_ctx
))
1070 size
= grub_get_initrd_size (&initrd_ctx
);
1072 initrd_pages
= (page_align (size
) >> 12);
1074 /* Get the highest address available for the initrd. */
1075 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0203)
1077 addr_max
= grub_cpu_to_le32 (linux_params
.initrd_addr_max
);
1079 /* XXX in reality, Linux specifies a bogus value, so
1080 it is necessary to make sure that ADDR_MAX does not exceed
1082 if (addr_max
> GRUB_LINUX_INITRD_MAX_ADDRESS
)
1083 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1086 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1088 if (linux_mem_size
!= 0 && linux_mem_size
< addr_max
)
1089 addr_max
= linux_mem_size
;
1091 /* Linux 2.3.xx has a bug in the memory range check, so avoid
1093 Linux 2.2.xx has a bug in the memory range check, which is
1094 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
1095 addr_max
-= 0x10000;
1097 addr_min
= (grub_addr_t
) prot_mode_target
+ prot_init_space
;
1099 /* Put the initrd as high as possible, 4KiB aligned. */
1100 addr
= (addr_max
- size
) & ~0xFFF;
1102 if (addr
< addr_min
)
1104 grub_error (GRUB_ERR_OUT_OF_RANGE
, "the initrd is too big");
1109 grub_relocator_chunk_t ch
;
1110 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
1111 addr_min
, addr
, size
, 0x1000,
1112 GRUB_RELOCATOR_PREFERENCE_HIGH
,
1116 initrd_mem
= get_virtual_current_address (ch
);
1117 initrd_mem_target
= get_physical_target_address (ch
);
1120 if (grub_initrd_load (&initrd_ctx
, argv
, initrd_mem
))
1123 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1124 (unsigned) addr
, (unsigned) size
);
1126 linux_params
.ramdisk_image
= initrd_mem_target
;
1127 linux_params
.ramdisk_size
= size
;
1128 linux_params
.root_dev
= 0x0100; /* XXX */
1131 grub_initrd_close (&initrd_ctx
);
1136 static grub_command_t cmd_linux
, cmd_initrd
;
1138 GRUB_MOD_INIT(linux
)
1140 cmd_linux
= grub_register_command ("linux", grub_cmd_linux
,
1141 0, N_("Load Linux."));
1142 cmd_initrd
= grub_register_command ("initrd", grub_cmd_initrd
,
1143 0, N_("Load initrd."));
1147 GRUB_MOD_FINI(linux
)
1149 grub_unregister_command (cmd_linux
);
1150 grub_unregister_command (cmd_initrd
);