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;
90 } __attribute__ ((packed
));
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
)
201 prot_size
= page_align (prot_size
);
203 /* Initialize the memory pointers with NULL for convenience. */
206 relocator
= grub_relocator_new ();
213 /* FIXME: Should request low memory from the heap when this feature is
217 grub_relocator_chunk_t ch
;
220 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
224 GRUB_RELOCATOR_PREFERENCE_LOW
,
226 for (; err
&& *align
+ 1 > min_align
; (*align
)--)
228 grub_errno
= GRUB_ERR_NONE
;
229 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
231 0xffffffff & ~prot_size
,
232 prot_size
, 1 << *align
,
233 GRUB_RELOCATOR_PREFERENCE_LOW
,
240 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
245 prot_mode_mem
= get_virtual_current_address (ch
);
246 prot_mode_target
= get_physical_target_address (ch
);
249 grub_dprintf ("linux", "prot_mode_mem = %lx, prot_mode_target = %lx, prot_size = %x\n",
250 (unsigned long) prot_mode_mem
, (unsigned long) prot_mode_target
,
251 (unsigned) prot_size
);
252 return GRUB_ERR_NONE
;
260 grub_e820_add_region (struct grub_e820_mmap
*e820_map
, int *e820_num
,
261 grub_uint64_t start
, grub_uint64_t size
,
266 if ((n
> 0) && (e820_map
[n
- 1].addr
+ e820_map
[n
- 1].size
== start
) &&
267 (e820_map
[n
- 1].type
== type
))
268 e820_map
[n
- 1].size
+= size
;
271 e820_map
[n
].addr
= start
;
272 e820_map
[n
].size
= size
;
273 e820_map
[n
].type
= type
;
276 return GRUB_ERR_NONE
;
280 grub_linux_setup_video (struct linux_kernel_params
*params
)
282 struct grub_video_mode_info mode_info
;
285 grub_video_driver_id_t driver_id
;
286 const char *gfxlfbvar
= grub_env_get ("gfxpayloadforcelfb");
288 driver_id
= grub_video_get_driver_id ();
290 if (driver_id
== GRUB_VIDEO_DRIVER_NONE
)
293 err
= grub_video_get_info_and_fini (&mode_info
, &framebuffer
);
297 grub_errno
= GRUB_ERR_NONE
;
301 params
->lfb_width
= mode_info
.width
;
302 params
->lfb_height
= mode_info
.height
;
303 params
->lfb_depth
= mode_info
.bpp
;
304 params
->lfb_line_len
= mode_info
.pitch
;
306 params
->lfb_base
= (grub_size_t
) framebuffer
;
307 params
->lfb_size
= ALIGN_UP (params
->lfb_line_len
* params
->lfb_height
, 65536);
309 params
->red_mask_size
= mode_info
.red_mask_size
;
310 params
->red_field_pos
= mode_info
.red_field_pos
;
311 params
->green_mask_size
= mode_info
.green_mask_size
;
312 params
->green_field_pos
= mode_info
.green_field_pos
;
313 params
->blue_mask_size
= mode_info
.blue_mask_size
;
314 params
->blue_field_pos
= mode_info
.blue_field_pos
;
315 params
->reserved_mask_size
= mode_info
.reserved_mask_size
;
316 params
->reserved_field_pos
= mode_info
.reserved_field_pos
;
318 if (gfxlfbvar
&& (gfxlfbvar
[0] == '1' || gfxlfbvar
[0] == 'y'))
319 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
324 case GRUB_VIDEO_DRIVER_VBE
:
325 params
->lfb_size
>>= 16;
326 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_VESA
;
329 case GRUB_VIDEO_DRIVER_EFI_UGA
:
330 case GRUB_VIDEO_DRIVER_EFI_GOP
:
331 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_EFIFB
;
334 /* FIXME: check if better id is available. */
335 case GRUB_VIDEO_DRIVER_SM712
:
336 case GRUB_VIDEO_DRIVER_SIS315PRO
:
337 case GRUB_VIDEO_DRIVER_VGA
:
338 case GRUB_VIDEO_DRIVER_CIRRUS
:
339 case GRUB_VIDEO_DRIVER_BOCHS
:
340 case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E
:
341 case GRUB_VIDEO_DRIVER_IEEE1275
:
342 case GRUB_VIDEO_DRIVER_COREBOOT
:
343 /* Make gcc happy. */
344 case GRUB_VIDEO_DRIVER_SDL
:
345 case GRUB_VIDEO_DRIVER_NONE
:
346 case GRUB_VIDEO_ADAPTER_CAPTURE
:
347 params
->have_vga
= GRUB_VIDEO_LINUX_TYPE_SIMPLE
;
352 #ifdef GRUB_MACHINE_PCBIOS
353 /* VESA packed modes may come with zeroed mask sizes, which need
354 to be set here according to DAC Palette width. If we don't,
355 this results in Linux displaying a black screen. */
356 if (driver_id
== GRUB_VIDEO_DRIVER_VBE
&& mode_info
.bpp
<= 8)
358 struct grub_vbe_info_block controller_info
;
362 status
= grub_vbe_bios_get_controller_info (&controller_info
);
364 if (status
== GRUB_VBE_STATUS_OK
&&
365 (controller_info
.capabilities
& GRUB_VBE_CAPABILITY_DACWIDTH
))
366 status
= grub_vbe_bios_set_dac_palette_width (&width
);
368 if (status
!= GRUB_VBE_STATUS_OK
)
369 /* 6 is default after mode reset. */
372 params
->red_mask_size
= params
->green_mask_size
373 = params
->blue_mask_size
= width
;
374 params
->reserved_mask_size
= 0;
378 return GRUB_ERR_NONE
;
381 /* Context for grub_linux_boot. */
382 struct grub_linux_boot_ctx
384 grub_addr_t real_mode_target
;
385 grub_size_t real_size
;
386 struct linux_kernel_params
*params
;
390 /* Helper for grub_linux_boot. */
392 grub_linux_boot_mmap_find (grub_uint64_t addr
, grub_uint64_t size
,
393 grub_memory_type_t type
, void *data
)
395 struct grub_linux_boot_ctx
*ctx
= data
;
397 /* We must put real mode code in the traditional space. */
398 if (type
!= GRUB_MEMORY_AVAILABLE
|| addr
> 0x90000)
401 if (addr
+ size
< 0x10000)
406 size
+= addr
- 0x10000;
410 if (addr
+ size
> 0x90000)
411 size
= 0x90000 - addr
;
413 if (ctx
->real_size
+ efi_mmap_size
> size
)
416 grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
417 (unsigned long) addr
,
419 (unsigned) (ctx
->real_size
+ efi_mmap_size
));
420 ctx
->real_mode_target
= ((addr
+ size
) - (ctx
->real_size
+ efi_mmap_size
));
424 /* GRUB types conveniently match E820 types. */
426 grub_linux_boot_mmap_fill (grub_uint64_t addr
, grub_uint64_t size
,
427 grub_memory_type_t type
, void *data
)
429 struct grub_linux_boot_ctx
*ctx
= data
;
431 if (grub_e820_add_region (ctx
->params
->e820_map
, &ctx
->e820_num
,
439 grub_linux_boot (void)
444 struct grub_relocator32_state state
;
446 struct grub_linux_boot_ctx ctx
= {
447 .real_mode_target
= 0
449 grub_size_t mmap_size
;
450 grub_size_t cl_offset
;
452 #ifdef GRUB_MACHINE_IEEE1275
454 const char *bootpath
;
457 bootpath
= grub_env_get ("root");
459 grub_ieee1275_set_property (grub_ieee1275_chosen
,
460 "bootpath", bootpath
,
461 grub_strlen (bootpath
) + 1,
463 linux_params
.ofw_signature
= GRUB_LINUX_OFW_SIGNATURE
;
464 linux_params
.ofw_num_items
= 1;
465 linux_params
.ofw_cif_handler
= (grub_uint32_t
) grub_ieee1275_entry_fn
;
466 linux_params
.ofw_idt
= 0;
470 modevar
= grub_env_get ("gfxpayload");
472 /* Now all graphical modes are acceptable.
473 May change in future if we have modes without framebuffer. */
474 if (modevar
&& *modevar
!= 0)
476 tmp
= grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE
, modevar
);
479 #if ACCEPTS_PURE_TEXT
480 err
= grub_video_set_mode (tmp
, 0, 0);
482 err
= grub_video_set_mode (tmp
, GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
486 else /* We can't go back to text mode from coreboot fb. */
487 #ifdef GRUB_MACHINE_COREBOOT
488 if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT
)
493 #if ACCEPTS_PURE_TEXT
494 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
, 0, 0);
496 err
= grub_video_set_mode (DEFAULT_VIDEO_MODE
,
497 GRUB_VIDEO_MODE_TYPE_PURE_TEXT
, 0);
504 grub_puts_ (N_("Booting in blind mode"));
505 grub_errno
= GRUB_ERR_NONE
;
508 if (grub_linux_setup_video (&linux_params
))
510 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
511 linux_params
.have_vga
= GRUB_VIDEO_LINUX_TYPE_TEXT
;
512 linux_params
.video_mode
= 0x3;
514 linux_params
.have_vga
= 0;
515 linux_params
.video_mode
= 0;
516 linux_params
.video_width
= 0;
517 linux_params
.video_height
= 0;
522 #ifndef GRUB_MACHINE_IEEE1275
523 if (linux_params
.have_vga
== GRUB_VIDEO_LINUX_TYPE_TEXT
)
526 grub_term_output_t term
;
528 FOR_ACTIVE_TERM_OUTPUTS(term
)
529 if (grub_strcmp (term
->name
, "vga_text") == 0
530 || grub_strcmp (term
->name
, "console") == 0
531 || grub_strcmp (term
->name
, "ofconsole") == 0)
533 struct grub_term_coordinate pos
= grub_term_getxy (term
);
534 linux_params
.video_cursor_x
= pos
.x
;
535 linux_params
.video_cursor_y
= pos
.y
;
536 linux_params
.video_width
= grub_term_width (term
);
537 linux_params
.video_height
= grub_term_height (term
);
543 linux_params
.video_cursor_x
= 0;
544 linux_params
.video_cursor_y
= 0;
545 linux_params
.video_width
= 80;
546 linux_params
.video_height
= 25;
550 mmap_size
= find_mmap_size ();
551 /* Make sure that each size is aligned to a page boundary. */
552 cl_offset
= ALIGN_UP (mmap_size
+ sizeof (linux_params
), 4096);
553 if (cl_offset
< ((grub_size_t
) linux_params
.setup_sects
<< GRUB_DISK_SECTOR_BITS
))
554 cl_offset
= ALIGN_UP ((grub_size_t
) (linux_params
.setup_sects
555 << GRUB_DISK_SECTOR_BITS
), 4096);
556 ctx
.real_size
= ALIGN_UP (cl_offset
+ maximal_cmdline_size
, 4096);
558 #ifdef GRUB_MACHINE_EFI
559 efi_mmap_size
= find_efi_mmap_size ();
560 if (efi_mmap_size
== 0)
564 grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
565 (unsigned) ctx
.real_size
, (unsigned) mmap_size
);
567 #ifdef GRUB_MACHINE_EFI
568 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 1);
569 if (! ctx
.real_mode_target
)
570 grub_efi_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
, 0);
572 grub_mmap_iterate (grub_linux_boot_mmap_find
, &ctx
);
574 grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
575 (unsigned long) ctx
.real_mode_target
,
576 (unsigned) ctx
.real_size
,
577 (unsigned) efi_mmap_size
);
579 if (! ctx
.real_mode_target
)
580 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate real mode pages");
583 grub_relocator_chunk_t ch
;
584 err
= grub_relocator_alloc_chunk_addr (relocator
, &ch
,
585 ctx
.real_mode_target
,
586 (ctx
.real_size
+ efi_mmap_size
));
589 real_mode_mem
= get_virtual_current_address (ch
);
591 efi_mmap_buf
= (grub_uint8_t
*) real_mode_mem
+ ctx
.real_size
;
593 grub_dprintf ("linux", "real_mode_mem = %lx\n",
594 (unsigned long) real_mode_mem
);
596 ctx
.params
= real_mode_mem
;
598 *ctx
.params
= linux_params
;
599 ctx
.params
->cmd_line_ptr
= ctx
.real_mode_target
+ cl_offset
;
600 grub_memcpy ((char *) ctx
.params
+ cl_offset
, linux_cmdline
,
601 maximal_cmdline_size
);
603 grub_dprintf ("linux", "code32_start = %x\n",
604 (unsigned) ctx
.params
->code32_start
);
607 if (grub_mmap_iterate (grub_linux_boot_mmap_fill
, &ctx
))
609 ctx
.params
->mmap_size
= ctx
.e820_num
;
611 #ifdef GRUB_MACHINE_EFI
613 grub_efi_uintn_t efi_desc_size
;
614 grub_size_t efi_mmap_target
;
615 grub_efi_uint32_t efi_desc_version
;
616 err
= grub_efi_finish_boot_services (&efi_mmap_size
, efi_mmap_buf
, NULL
,
617 &efi_desc_size
, &efi_desc_version
);
621 /* Note that no boot services are available from here. */
622 efi_mmap_target
= ctx
.real_mode_target
623 + ((grub_uint8_t
*) efi_mmap_buf
- (grub_uint8_t
*) real_mode_mem
);
624 /* Pass EFI parameters. */
625 if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0208)
627 ctx
.params
->v0208
.efi_mem_desc_size
= efi_desc_size
;
628 ctx
.params
->v0208
.efi_mem_desc_version
= efi_desc_version
;
629 ctx
.params
->v0208
.efi_mmap
= efi_mmap_target
;
630 ctx
.params
->v0208
.efi_mmap_size
= efi_mmap_size
;
633 ctx
.params
->v0208
.efi_mmap_hi
= (efi_mmap_target
>> 32);
636 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0206)
638 ctx
.params
->v0206
.efi_mem_desc_size
= efi_desc_size
;
639 ctx
.params
->v0206
.efi_mem_desc_version
= efi_desc_version
;
640 ctx
.params
->v0206
.efi_mmap
= efi_mmap_target
;
641 ctx
.params
->v0206
.efi_mmap_size
= efi_mmap_size
;
643 else if (grub_le_to_cpu16 (ctx
.params
->version
) >= 0x0204)
645 ctx
.params
->v0204
.efi_mem_desc_size
= efi_desc_size
;
646 ctx
.params
->v0204
.efi_mem_desc_version
= efi_desc_version
;
647 ctx
.params
->v0204
.efi_mmap
= efi_mmap_target
;
648 ctx
.params
->v0204
.efi_mmap_size
= efi_mmap_size
;
654 /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
655 state
.ebp
= state
.edi
= state
.ebx
= 0;
656 state
.esi
= ctx
.real_mode_target
;
657 state
.esp
= ctx
.real_mode_target
;
658 state
.eip
= ctx
.params
->code32_start
;
659 return grub_relocator32_boot (relocator
, state
, 0);
663 grub_linux_unload (void)
665 grub_dl_unref (my_mod
);
667 grub_free (linux_cmdline
);
669 return GRUB_ERR_NONE
;
673 grub_cmd_linux (grub_command_t cmd
__attribute__ ((unused
)),
674 int argc
, char *argv
[])
676 grub_file_t file
= 0;
677 struct linux_kernel_header lh
;
678 grub_uint8_t setup_sects
;
679 grub_size_t real_size
, prot_size
, prot_file_size
;
682 grub_size_t align
, min_align
;
684 grub_uint64_t preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
686 grub_dl_ref (my_mod
);
690 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
694 file
= grub_file_open (argv
[0]);
698 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
701 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
706 if (lh
.boot_flag
!= grub_cpu_to_le16 (0xaa55))
708 grub_error (GRUB_ERR_BAD_OS
, "invalid magic number");
712 if (lh
.setup_sects
> GRUB_LINUX_MAX_SETUP_SECTS
)
714 grub_error (GRUB_ERR_BAD_OS
, "too many setup sectors");
718 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
719 still not support 32-bit boot. */
720 if (lh
.header
!= grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE
)
721 || grub_le_to_cpu16 (lh
.version
) < 0x0203)
723 grub_error (GRUB_ERR_BAD_OS
, "version too old for 32-bit boot"
724 #ifdef GRUB_MACHINE_PCBIOS
725 " (try with `linux16')"
731 if (! (lh
.loadflags
& GRUB_LINUX_FLAG_BIG_KERNEL
))
733 grub_error (GRUB_ERR_BAD_OS
, "zImage doesn't support 32-bit boot"
734 #ifdef GRUB_MACHINE_PCBIOS
735 " (try with `linux16')"
741 if (grub_le_to_cpu16 (lh
.version
) >= 0x0206)
742 maximal_cmdline_size
= grub_le_to_cpu32 (lh
.cmdline_size
) + 1;
744 maximal_cmdline_size
= 256;
746 if (maximal_cmdline_size
< 128)
747 maximal_cmdline_size
= 128;
749 setup_sects
= lh
.setup_sects
;
751 /* If SETUP_SECTS is not set, set it to the default (4). */
753 setup_sects
= GRUB_LINUX_DEFAULT_SETUP_SECTS
;
755 real_size
= setup_sects
<< GRUB_DISK_SECTOR_BITS
;
756 prot_file_size
= grub_file_size (file
) - real_size
- GRUB_DISK_SECTOR_SIZE
;
758 if (grub_le_to_cpu16 (lh
.version
) >= 0x205
759 && lh
.kernel_alignment
!= 0
760 && ((lh
.kernel_alignment
- 1) & lh
.kernel_alignment
) == 0)
762 for (align
= 0; align
< 32; align
++)
763 if (grub_le_to_cpu32 (lh
.kernel_alignment
) & (1 << align
))
765 relocatable
= grub_le_to_cpu32 (lh
.relocatable
);
773 if (grub_le_to_cpu16 (lh
.version
) >= 0x020a)
775 min_align
= lh
.min_alignment
;
776 prot_size
= grub_le_to_cpu32 (lh
.init_size
);
777 prot_init_space
= page_align (prot_size
);
779 preferred_address
= grub_le_to_cpu64 (lh
.pref_address
);
781 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
786 prot_size
= prot_file_size
;
787 preferred_address
= GRUB_LINUX_BZIMAGE_ADDR
;
788 /* Usually, the compression ratio is about 50%. */
789 prot_init_space
= page_align (prot_size
) * 3;
792 if (allocate_pages (prot_size
, &align
,
793 min_align
, relocatable
,
797 grub_memset (&linux_params
, 0, sizeof (linux_params
));
798 grub_memcpy (&linux_params
.setup_sects
, &lh
.setup_sects
, sizeof (lh
) - 0x1F1);
800 linux_params
.code32_start
= prot_mode_target
+ lh
.code32_start
- GRUB_LINUX_BZIMAGE_ADDR
;
801 linux_params
.kernel_alignment
= (1 << align
);
802 linux_params
.ps_mouse
= linux_params
.padding10
= 0;
804 len
= sizeof (linux_params
) - sizeof (lh
);
805 if (grub_file_read (file
, (char *) &linux_params
+ sizeof (lh
), len
) != len
)
808 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
813 linux_params
.type_of_loader
= GRUB_LINUX_BOOT_LOADER_TYPE
;
815 /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
816 and otherwise ignored. */
817 linux_params
.cl_magic
= GRUB_LINUX_CL_MAGIC
;
818 linux_params
.cl_offset
= 0x1000;
820 linux_params
.ramdisk_image
= 0;
821 linux_params
.ramdisk_size
= 0;
823 linux_params
.heap_end_ptr
= GRUB_LINUX_HEAP_END_OFFSET
;
824 linux_params
.loadflags
|= GRUB_LINUX_FLAG_CAN_USE_HEAP
;
826 /* These are not needed to be precise, because Linux uses these values
827 only to raise an error when the decompression code cannot find good
829 linux_params
.ext_mem
= ((32 * 0x100000) >> 10);
830 linux_params
.alt_mem
= ((32 * 0x100000) >> 10);
832 /* Ignored by Linux. */
833 linux_params
.video_page
= 0;
835 /* Only used when `video_mode == 0x7', otherwise ignored. */
836 linux_params
.video_ega_bx
= 0;
838 linux_params
.font_size
= 16; /* XXX */
840 #ifdef GRUB_MACHINE_EFI
842 if (grub_le_to_cpu16 (linux_params
.version
) < 0x0208 &&
843 ((grub_addr_t
) grub_efi_system_table
>> 32) != 0)
844 return grub_error(GRUB_ERR_BAD_OS
,
845 "kernel does not support 64-bit addressing");
848 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0208)
850 linux_params
.v0208
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
851 linux_params
.v0208
.efi_system_table
= (grub_uint32_t
) (unsigned long) grub_efi_system_table
;
853 linux_params
.v0208
.efi_system_table_hi
= (grub_uint32_t
) ((grub_uint64_t
) grub_efi_system_table
>> 32);
856 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0206)
858 linux_params
.v0206
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE
;
859 linux_params
.v0206
.efi_system_table
= (grub_uint32_t
) (unsigned long) grub_efi_system_table
;
861 else if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0204)
863 linux_params
.v0204
.efi_signature
= GRUB_LINUX_EFI_SIGNATURE_0204
;
864 linux_params
.v0204
.efi_system_table
= (grub_uint32_t
) (unsigned long) grub_efi_system_table
;
868 /* The other parameters are filled when booting. */
870 grub_file_seek (file
, real_size
+ GRUB_DISK_SECTOR_SIZE
);
872 grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
873 (unsigned) real_size
, (unsigned) prot_size
);
875 /* Look for memory size and video mode specified on the command line. */
877 for (i
= 1; i
< argc
; i
++)
878 #ifdef GRUB_MACHINE_PCBIOS
879 if (grub_memcmp (argv
[i
], "vga=", 4) == 0)
881 /* Video mode selection support. */
882 char *val
= argv
[i
] + 4;
883 unsigned vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
884 struct grub_vesa_mode_table_entry
*linux_mode
;
888 grub_dl_load ("vbe");
890 if (grub_strcmp (val
, "normal") == 0)
891 vid_mode
= GRUB_LINUX_VID_MODE_NORMAL
;
892 else if (grub_strcmp (val
, "ext") == 0)
893 vid_mode
= GRUB_LINUX_VID_MODE_EXTENDED
;
894 else if (grub_strcmp (val
, "ask") == 0)
896 grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
898 /* We usually would never do this in a loader, but "vga=ask" means user
899 requested interaction, so it can't hurt to request keyboard input. */
900 grub_wait_after_message ();
905 vid_mode
= (grub_uint16_t
) grub_strtoul (val
, 0, 0);
910 case GRUB_LINUX_VID_MODE_NORMAL
:
911 grub_env_set ("gfxpayload", "text");
912 grub_printf_ (N_("%s is deprecated. "
913 "Use set gfxpayload=%s before "
914 "linux command instead.\n"), "text",
919 case GRUB_LINUX_VID_MODE_EXTENDED
:
920 /* FIXME: support 80x50 text. */
921 grub_env_set ("gfxpayload", "text");
922 grub_printf_ (N_("%s is deprecated. "
923 "Use set gfxpayload=%s before "
924 "linux command instead.\n"), "text",
928 /* Ignore invalid values. */
929 if (vid_mode
< GRUB_VESA_MODE_TABLE_START
||
930 vid_mode
> GRUB_VESA_MODE_TABLE_END
)
932 grub_env_set ("gfxpayload", "text");
933 /* TRANSLATORS: "x" has to be entered in, like an identifier,
934 so please don't use better Unicode codepoints. */
935 grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
936 "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
937 "before linux command instead.\n"),
942 linux_mode
= &grub_vesa_mode_table
[vid_mode
943 - GRUB_VESA_MODE_TABLE_START
];
945 buf
= grub_xasprintf ("%ux%ux%u,%ux%u",
946 linux_mode
->width
, linux_mode
->height
,
948 linux_mode
->width
, linux_mode
->height
);
952 grub_printf_ (N_("%s is deprecated. "
953 "Use set gfxpayload=%s before "
954 "linux command instead.\n"),
956 err
= grub_env_set ("gfxpayload", buf
);
963 #endif /* GRUB_MACHINE_PCBIOS */
964 if (grub_memcmp (argv
[i
], "mem=", 4) == 0)
966 char *val
= argv
[i
] + 4;
968 linux_mem_size
= grub_strtoul (val
, &val
, 0);
972 grub_errno
= GRUB_ERR_NONE
;
979 switch (grub_tolower (val
[0]))
991 /* Check an overflow. */
992 if (linux_mem_size
> (~0UL >> shift
))
995 linux_mem_size
<<= shift
;
998 else if (grub_memcmp (argv
[i
], "quiet", sizeof ("quiet") - 1) == 0)
1000 linux_params
.loadflags
|= GRUB_LINUX_FLAG_QUIET
;
1003 /* Create kernel command line. */
1004 linux_cmdline
= grub_zalloc (maximal_cmdline_size
+ 1);
1007 grub_memcpy (linux_cmdline
, LINUX_IMAGE
, sizeof (LINUX_IMAGE
));
1008 grub_create_loader_cmdline (argc
, argv
,
1010 + sizeof (LINUX_IMAGE
) - 1,
1011 maximal_cmdline_size
1012 - (sizeof (LINUX_IMAGE
) - 1));
1014 len
= prot_file_size
;
1015 if (grub_file_read (file
, prot_mode_mem
, len
) != len
&& !grub_errno
)
1016 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
1019 if (grub_errno
== GRUB_ERR_NONE
)
1021 grub_loader_set (grub_linux_boot
, grub_linux_unload
,
1022 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
1029 grub_file_close (file
);
1031 if (grub_errno
!= GRUB_ERR_NONE
)
1033 grub_dl_unref (my_mod
);
1041 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
1042 int argc
, char *argv
[])
1044 grub_size_t size
= 0;
1045 grub_addr_t addr_min
, addr_max
;
1048 struct grub_linux_initrd_context initrd_ctx
;
1052 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
1058 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("you need to load the kernel first"));
1062 if (grub_initrd_init (argc
, argv
, &initrd_ctx
))
1065 size
= grub_get_initrd_size (&initrd_ctx
);
1067 initrd_pages
= (page_align (size
) >> 12);
1069 /* Get the highest address available for the initrd. */
1070 if (grub_le_to_cpu16 (linux_params
.version
) >= 0x0203)
1072 addr_max
= grub_cpu_to_le32 (linux_params
.initrd_addr_max
);
1074 /* XXX in reality, Linux specifies a bogus value, so
1075 it is necessary to make sure that ADDR_MAX does not exceed
1077 if (addr_max
> GRUB_LINUX_INITRD_MAX_ADDRESS
)
1078 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1081 addr_max
= GRUB_LINUX_INITRD_MAX_ADDRESS
;
1083 if (linux_mem_size
!= 0 && linux_mem_size
< addr_max
)
1084 addr_max
= linux_mem_size
;
1086 /* Linux 2.3.xx has a bug in the memory range check, so avoid
1088 Linux 2.2.xx has a bug in the memory range check, which is
1089 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
1090 addr_max
-= 0x10000;
1092 addr_min
= (grub_addr_t
) prot_mode_target
+ prot_init_space
;
1094 /* Put the initrd as high as possible, 4KiB aligned. */
1095 addr
= (addr_max
- size
) & ~0xFFF;
1097 if (addr
< addr_min
)
1099 grub_error (GRUB_ERR_OUT_OF_RANGE
, "the initrd is too big");
1104 grub_relocator_chunk_t ch
;
1105 err
= grub_relocator_alloc_chunk_align (relocator
, &ch
,
1106 addr_min
, addr
, size
, 0x1000,
1107 GRUB_RELOCATOR_PREFERENCE_HIGH
,
1111 initrd_mem
= get_virtual_current_address (ch
);
1112 initrd_mem_target
= get_physical_target_address (ch
);
1115 if (grub_initrd_load (&initrd_ctx
, argv
, initrd_mem
))
1118 grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
1119 (unsigned) addr
, (unsigned) size
);
1121 linux_params
.ramdisk_image
= initrd_mem_target
;
1122 linux_params
.ramdisk_size
= size
;
1123 linux_params
.root_dev
= 0x0100; /* XXX */
1126 grub_initrd_close (&initrd_ctx
);
1131 static grub_command_t cmd_linux
, cmd_initrd
;
1133 GRUB_MOD_INIT(linux
)
1135 cmd_linux
= grub_register_command ("linux", grub_cmd_linux
,
1136 0, N_("Load Linux."));
1137 cmd_initrd
= grub_register_command ("initrd", grub_cmd_initrd
,
1138 0, N_("Load initrd."));
1142 GRUB_MOD_FINI(linux
)
1144 grub_unregister_command (cmd_linux
);
1145 grub_unregister_command (cmd_initrd
);