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/machine/loader.h>
21
#include <grub/file.h>
22
#include <grub/disk.h>
24
#include <grub/misc.h>
25
#include <grub/types.h>
28
#include <grub/term.h>
29
#include <grub/cpu/linux.h>
30
#include <grub/efi/api.h>
31
#include <grub/efi/efi.h>
32
#include <grub/command.h>
33
#include <grub/memory.h>
35
#include <grub/video.h>
36
#include <grub/time.h>
37
#include <grub/i18n.h>
39
#define GRUB_LINUX_CL_OFFSET 0x1000
40
#define GRUB_LINUX_CL_END_OFFSET 0x2000
42
#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
43
((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
45
static grub_dl_t my_mod;
47
static grub_size_t linux_mem_size;
49
static void *real_mode_mem;
50
static void *prot_mode_mem;
51
static void *initrd_mem;
52
static grub_efi_uintn_t real_mode_pages;
53
static grub_efi_uintn_t prot_mode_pages;
54
static grub_efi_uintn_t initrd_pages;
55
static void *mmap_buf;
57
static grub_uint8_t gdt[] __attribute__ ((aligned(16))) =
60
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00,
66
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
73
} __attribute__ ((packed));
75
static struct gdt_descriptor gdt_desc =
85
} __attribute__ ((packed));
87
static struct idt_descriptor idt_desc =
93
static inline grub_size_t
94
page_align (grub_size_t size)
96
return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
99
/* Find the optimal number of pages for the memory map. Is it better to
100
move this code to efi/mm.c? */
101
static grub_efi_uintn_t
102
find_mmap_size (void)
104
static grub_efi_uintn_t mmap_size = 0;
109
mmap_size = (1 << 12);
113
grub_efi_memory_descriptor_t *mmap;
114
grub_efi_uintn_t desc_size;
116
mmap = grub_malloc (mmap_size);
120
ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0);
124
grub_fatal ("cannot get memory map");
128
mmap_size += (1 << 12);
131
/* Increase the size a bit for safety, because GRUB allocates more on
132
later, and EFI itself may allocate more. */
133
mmap_size += (1 << 12);
135
return page_align (mmap_size);
143
grub_efi_free_pages ((grub_addr_t) real_mode_mem, real_mode_pages);
149
grub_efi_free_pages ((grub_addr_t) prot_mode_mem, prot_mode_pages);
155
grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
160
/* Allocate pages for the real mode code and the protected mode code
161
for linux as well as a memory map buffer. */
163
allocate_pages (grub_size_t prot_size)
165
grub_efi_uintn_t desc_size;
166
grub_efi_memory_descriptor_t *mmap, *mmap_end;
167
grub_efi_uintn_t mmap_size, tmp_mmap_size;
168
grub_efi_memory_descriptor_t *desc;
169
grub_size_t real_size;
171
/* Make sure that each size is aligned to a page boundary. */
172
real_size = GRUB_LINUX_CL_END_OFFSET;
173
prot_size = page_align (prot_size);
174
mmap_size = find_mmap_size ();
176
grub_dprintf ("linux", "real_size = %x, prot_size = %x, mmap_size = %x\n",
177
(unsigned) real_size, (unsigned) prot_size, (unsigned) mmap_size);
179
/* Calculate the number of pages; Combine the real mode code with
180
the memory map buffer for simplicity. */
181
real_mode_pages = ((real_size + mmap_size) >> 12);
182
prot_mode_pages = (prot_size >> 12);
184
/* Initialize the memory pointers with NULL for convenience. */
188
/* Read the memory map temporarily, to find free space. */
189
mmap = grub_malloc (mmap_size);
193
tmp_mmap_size = mmap_size;
194
if (grub_efi_get_memory_map (&tmp_mmap_size, mmap, 0, &desc_size, 0) <= 0)
195
grub_fatal ("cannot get memory map");
197
mmap_end = NEXT_MEMORY_DESCRIPTOR (mmap, tmp_mmap_size);
199
/* First, find free pages for the real mode code
200
and the memory map buffer. */
203
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
205
/* Probably it is better to put the real mode code in the traditional
207
if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
208
&& desc->physical_start <= 0x90000
209
&& desc->num_pages >= real_mode_pages)
211
grub_efi_physical_address_t physical_end;
212
grub_efi_physical_address_t addr;
214
physical_end = desc->physical_start + (desc->num_pages << 12);
215
if (physical_end > 0x90000)
216
physical_end = 0x90000;
218
grub_dprintf ("linux", "physical_start = %x, physical_end = %x\n",
219
(unsigned) desc->physical_start,
220
(unsigned) physical_end);
221
addr = physical_end - real_size - mmap_size;
225
grub_dprintf ("linux", "trying to allocate %u pages at %lx\n",
226
(unsigned) real_mode_pages, (unsigned long) addr);
227
real_mode_mem = grub_efi_allocate_pages (addr, real_mode_pages);
229
grub_fatal ("cannot allocate pages");
231
desc->num_pages -= real_mode_pages;
238
grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
242
mmap_buf = (void *) ((char *) real_mode_mem + real_size);
244
/* Next, find free pages for the protected mode code. */
245
/* XXX what happens if anything is using this address? */
246
prot_mode_mem = grub_efi_allocate_pages (0x100000, prot_mode_pages + 1);
249
grub_error (GRUB_ERR_OUT_OF_MEMORY,
250
"cannot allocate protected mode pages");
254
grub_dprintf ("linux", "real_mode_mem = %lx, real_mode_pages = %x, "
255
"prot_mode_mem = %lx, prot_mode_pages = %x\n",
256
(unsigned long) real_mode_mem, (unsigned) real_mode_pages,
257
(unsigned long) prot_mode_mem, (unsigned) prot_mode_pages);
269
grub_e820_add_region (struct grub_e820_mmap *e820_map, int *e820_num,
270
grub_uint64_t start, grub_uint64_t size,
275
if (n >= GRUB_E820_MAX_ENTRY)
276
grub_fatal ("Too many e820 memory map entries");
278
if ((n > 0) && (e820_map[n - 1].addr + e820_map[n - 1].size == start) &&
279
(e820_map[n - 1].type == type))
280
e820_map[n - 1].size += size;
283
e820_map[n].addr = start;
284
e820_map[n].size = size;
285
e820_map[n].type = type;
291
grub_linux_setup_video (struct linux_kernel_params *params)
293
struct grub_video_mode_info mode_info;
297
err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
302
params->lfb_width = mode_info.width;
303
params->lfb_height = mode_info.height;
304
params->lfb_depth = mode_info.bpp;
305
params->lfb_line_len = mode_info.pitch;
307
params->lfb_base = (grub_size_t) framebuffer;
308
params->lfb_size = (params->lfb_line_len * params->lfb_height + 65535) >> 16;
310
params->red_mask_size = mode_info.red_mask_size;
311
params->red_field_pos = mode_info.red_field_pos;
312
params->green_mask_size = mode_info.green_mask_size;
313
params->green_field_pos = mode_info.green_field_pos;
314
params->blue_mask_size = mode_info.blue_mask_size;
315
params->blue_field_pos = mode_info.blue_field_pos;
316
params->reserved_mask_size = mode_info.reserved_mask_size;
317
params->reserved_field_pos = mode_info.reserved_field_pos;
319
params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE;
321
#ifdef GRUB_MACHINE_PCBIOS
322
/* VESA packed modes may come with zeroed mask sizes, which need
323
to be set here according to DAC Palette width. If we don't,
324
this results in Linux displaying a black screen. */
325
if (mode_info.bpp <= 8)
327
struct grub_vbe_info_block controller_info;
331
status = grub_vbe_bios_get_controller_info (&controller_info);
333
if (status == GRUB_VBE_STATUS_OK &&
334
(controller_info.capabilities & GRUB_VBE_CAPABILITY_DACWIDTH))
335
status = grub_vbe_bios_set_dac_palette_width (&width);
337
if (status != GRUB_VBE_STATUS_OK)
338
/* 6 is default after mode reset. */
341
params->red_mask_size = params->green_mask_size
342
= params->blue_mask_size = width;
343
params->reserved_mask_size = 0;
351
extern grub_uint8_t grub_linux_trampoline_start[];
352
extern grub_uint8_t grub_linux_trampoline_end[];
356
grub_linux_boot (void)
358
struct linux_kernel_params *params;
359
grub_efi_uintn_t mmap_size;
360
grub_efi_uintn_t map_key;
361
grub_efi_uintn_t desc_size;
362
grub_efi_uint32_t desc_version;
368
params = real_mode_mem;
370
grub_printf ("%d\n", __LINE__);
372
grub_dprintf ("linux", "code32_start = %x, idt_desc = %lx, gdt_desc = %lx\n",
373
(unsigned) params->code32_start,
374
(unsigned long) &(idt_desc.limit),
375
(unsigned long) &(gdt_desc.limit));
376
grub_dprintf ("linux", "idt = %x:%lx, gdt = %x:%lx\n",
377
(unsigned) idt_desc.limit, (unsigned long) idt_desc.base,
378
(unsigned) gdt_desc.limit, (unsigned long) gdt_desc.base);
380
grub_printf ("%d\n", __LINE__);
382
auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
383
int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type)
387
case GRUB_MACHINE_MEMORY_AVAILABLE:
388
grub_e820_add_region (params->e820_map, &e820_num,
389
addr, size, GRUB_E820_RAM);
392
#ifdef GRUB_MACHINE_MEMORY_ACPI
393
case GRUB_MACHINE_MEMORY_ACPI:
394
grub_e820_add_region (params->e820_map, &e820_num,
395
addr, size, GRUB_E820_ACPI);
399
#ifdef GRUB_MACHINE_MEMORY_NVS
400
case GRUB_MACHINE_MEMORY_NVS:
401
grub_e820_add_region (params->e820_map, &e820_num,
402
addr, size, GRUB_E820_NVS);
406
#ifdef GRUB_MACHINE_MEMORY_CODE
407
case GRUB_MACHINE_MEMORY_CODE:
408
grub_e820_add_region (params->e820_map, &e820_num,
409
addr, size, GRUB_E820_EXEC_CODE);
414
grub_e820_add_region (params->e820_map, &e820_num,
415
addr, size, GRUB_E820_RESERVED);
420
grub_printf ("%d\n", __LINE__);
423
grub_mmap_iterate (hook);
424
params->mmap_size = e820_num;
426
grub_printf ("Trampoline at %p. code32=%x, real_mode_mem=%p\n",
427
((char *) prot_mode_mem + (prot_mode_pages << 12)),
428
(unsigned) params->code32_start, real_mode_mem);
430
modevar = grub_env_get ("gfxpayload");
432
/* Now all graphical modes are acceptable.
433
May change in future if we have modes without framebuffer. */
434
if (modevar && *modevar != 0)
436
tmp = grub_xasprintf ("%s;auto", modevar);
439
err = grub_video_set_mode (tmp, GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
443
err = grub_video_set_mode ("auto", GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
446
err = grub_linux_setup_video (params);
451
grub_printf ("Booting however\n");
452
grub_errno = GRUB_ERR_NONE;
455
mmap_size = find_mmap_size ();
456
if (grub_efi_get_memory_map (&mmap_size, mmap_buf, &map_key,
457
&desc_size, &desc_version) <= 0)
458
grub_fatal ("cannot get memory map");
460
if (! grub_efi_exit_boot_services (map_key))
461
grub_fatal ("cannot exit boot services");
463
/* Note that no boot services are available from here. */
465
/* Pass EFI parameters. */
466
if (grub_le_to_cpu16 (params->version) >= 0x0206)
468
params->v0206.efi_mem_desc_size = desc_size;
469
params->v0206.efi_mem_desc_version = desc_version;
470
params->v0206.efi_mmap = (grub_uint32_t) (unsigned long) mmap_buf;
471
params->v0206.efi_mmap_size = mmap_size;
473
params->v0206.efi_mmap_hi = (grub_uint32_t) ((grub_uint64_t) mmap_buf >> 32);
476
else if (grub_le_to_cpu16 (params->version) >= 0x0204)
478
params->v0204.efi_mem_desc_size = desc_size;
479
params->v0204.efi_mem_desc_version = desc_version;
480
params->v0204.efi_mmap = (grub_uint32_t) (unsigned long) mmap_buf;
481
params->v0204.efi_mmap_size = mmap_size;
486
grub_memcpy ((char *) prot_mode_mem + (prot_mode_pages << 12),
487
grub_linux_trampoline_start,
488
grub_linux_trampoline_end - grub_linux_trampoline_start);
490
((void (*) (unsigned long, void *)) ((char *) prot_mode_mem
491
+ (prot_mode_pages << 12)))
492
(params->code32_start, real_mode_mem);
496
/* Hardware interrupts are not safe any longer. */
497
asm volatile ("cli" : : );
499
/* Load the IDT and the GDT for the bootstrap. */
500
asm volatile ("lidt %0" : : "m" (idt_desc));
501
asm volatile ("lgdt %0" : : "m" (gdt_desc));
503
/* Pass parameters. */
504
asm volatile ("movl %0, %%ecx" : : "m" (params->code32_start));
505
asm volatile ("movl %0, %%esi" : : "m" (real_mode_mem));
507
asm volatile ("xorl %%ebx, %%ebx" : : );
510
asm volatile ("jmp *%%ecx" : : );
514
/* Never reach here. */
515
return GRUB_ERR_NONE;
519
grub_linux_unload (void)
522
grub_dl_unref (my_mod);
524
return GRUB_ERR_NONE;
528
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
529
int argc, char *argv[])
531
grub_file_t file = 0;
532
struct linux_kernel_header lh;
533
struct linux_kernel_params *params;
534
grub_uint8_t setup_sects;
535
grub_size_t real_size, prot_size;
540
grub_dl_ref (my_mod);
544
grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
548
file = grub_file_open (argv[0]);
552
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
554
grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
558
if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
560
grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
564
if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
566
grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
570
/* EFI support is quite new, so reject old versions. */
571
if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
572
|| grub_le_to_cpu16 (lh.version) < 0x0203)
574
grub_error (GRUB_ERR_BAD_OS, "too old version");
578
/* I'm not sure how to support zImage on EFI. */
579
if (! (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
581
grub_error (GRUB_ERR_BAD_OS, "zImage is not supported");
585
setup_sects = lh.setup_sects;
587
/* If SETUP_SECTS is not set, set it to the default (4). */
589
setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
591
real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
592
prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
594
if (! allocate_pages (prot_size))
597
params = (struct linux_kernel_params *) real_mode_mem;
598
grub_memset (params, 0, GRUB_LINUX_CL_END_OFFSET);
599
grub_memcpy (¶ms->setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
601
params->ps_mouse = params->padding10 = 0;
603
len = 0x400 - sizeof (lh);
604
if (grub_file_read (file, (char *) real_mode_mem + sizeof (lh), len) != len)
606
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
610
params->type_of_loader = (LINUX_LOADER_ID_GRUB << 4);
612
params->cl_magic = GRUB_LINUX_CL_MAGIC;
613
params->cl_offset = 0x1000;
614
params->cmd_line_ptr = (unsigned long) real_mode_mem + 0x1000;
615
params->ramdisk_image = 0;
616
params->ramdisk_size = 0;
618
params->heap_end_ptr = GRUB_LINUX_HEAP_END_OFFSET;
619
params->loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
621
/* These are not needed to be precise, because Linux uses these values
622
only to raise an error when the decompression code cannot find good
624
params->ext_mem = ((32 * 0x100000) >> 10);
625
params->alt_mem = ((32 * 0x100000) >> 10);
628
grub_term_output_t term;
630
FOR_ACTIVE_TERM_OUTPUTS(term)
631
if (grub_strcmp (term->name, "vga_text") == 0
632
|| grub_strcmp (term->name, "console") == 0)
634
grub_uint16_t pos = grub_term_getxy (term);
635
params->video_cursor_x = pos >> 8;
636
params->video_cursor_y = pos & 0xff;
637
params->video_width = grub_term_width (term);
638
params->video_height = grub_term_height (term);
644
params->video_cursor_x = 0;
645
params->video_cursor_y = 0;
646
params->video_width = 80;
647
params->video_height = 25;
650
params->video_page = 0; /* ??? */
651
params->video_mode = grub_efi_system_table->con_out->mode->mode;
652
params->video_ega_bx = 0;
653
params->have_vga = 0;
654
params->font_size = 16; /* XXX */
656
if (grub_le_to_cpu16 (params->version) >= 0x0206)
658
params->v0206.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
659
params->v0206.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table;
661
params->v0206.efi_system_table_hi = (grub_uint32_t) ((grub_uint64_t) grub_efi_system_table >> 32);
664
else if (grub_le_to_cpu16 (params->version) >= 0x0204)
666
params->v0204.efi_signature = GRUB_LINUX_EFI_SIGNATURE_0204;
667
params->v0204.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table;
671
/* The structure is zeroed already. */
674
params->lfb_width = 0;
675
params->lfb_height = 0;
676
params->lfb_depth = 0;
677
params->lfb_base = 0;
678
params->lfb_size = 0;
679
params->lfb_line_len = 0;
680
params->red_mask_size = 0;
681
params->red_field_pos = 0;
682
params->green_mask_size = 0;
683
params->green_field_pos = 0;
684
params->blue_mask_size = 0;
685
params->blue_field_pos = 0;
686
params->reserved_mask_size = 0;
687
params->reserved_field_pos = 0;
688
params->vesapm_segment = 0;
689
params->vesapm_offset = 0;
690
params->lfb_pages = 0;
691
params->vesa_attrib = 0;
694
params->apm_version = 0;
695
params->apm_code_segment = 0;
696
params->apm_entry = 0;
697
params->apm_16bit_code_segment = 0;
698
params->apm_data_segment = 0;
699
params->apm_flags = 0;
700
params->apm_code_len = 0;
701
params->apm_data_len = 0;
703
/* XXX is there any way to use SpeedStep on EFI? */
704
params->ist_signature = 0;
705
params->ist_command = 0;
706
params->ist_event = 0;
707
params->ist_perf_level = 0;
709
/* Let the kernel probe the information. */
710
grub_memset (params->hd0_drive_info, 0, sizeof (params->hd0_drive_info));
711
grub_memset (params->hd1_drive_info, 0, sizeof (params->hd1_drive_info));
714
params->rom_config_len = 0;
716
/* No need to fake the BIOS's memory map. */
717
params->mmap_size = 0;
719
/* Let the kernel probe the information. */
720
params->ps_mouse = 0;
722
/* Clear padding for future compatibility. */
723
grub_memset (params->padding1, 0, sizeof (params->padding1));
724
grub_memset (params->padding2, 0, sizeof (params->padding2));
725
grub_memset (params->padding3, 0, sizeof (params->padding3));
726
grub_memset (params->padding4, 0, sizeof (params->padding4));
727
grub_memset (params->padding5, 0, sizeof (params->padding5));
728
grub_memset (params->padding6, 0, sizeof (params->padding6));
729
grub_memset (params->padding7, 0, sizeof (params->padding7));
730
grub_memset (params->padding8, 0, sizeof (params->padding8));
731
grub_memset (params->padding9, 0, sizeof (params->padding9));
735
/* The other EFI parameters are filled when booting. */
737
grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
739
/* XXX there is no way to know if the kernel really supports EFI. */
740
grub_printf (" [Linux-bzImage, setup=0x%x, size=0x%x]\n",
741
(unsigned) real_size, (unsigned) prot_size);
743
/* Detect explicitly specified memory size, if any. */
745
for (i = 1; i < argc; i++)
746
if (grub_memcmp (argv[i], "mem=", 4) == 0)
748
char *val = argv[i] + 4;
750
linux_mem_size = grub_strtoul (val, &val, 0);
754
grub_errno = GRUB_ERR_NONE;
761
switch (grub_tolower (val[0]))
773
/* Check an overflow. */
774
if (linux_mem_size > (~0UL >> shift))
777
linux_mem_size <<= shift;
780
else if (grub_memcmp (argv[i], "video=efifb", 11) == 0)
782
if (params->have_vga)
783
params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE;
786
/* Specify the boot file. */
787
dest = grub_stpcpy ((char *) real_mode_mem + GRUB_LINUX_CL_OFFSET,
789
dest = grub_stpcpy (dest, argv[0]);
791
/* Copy kernel parameters. */
794
&& dest + grub_strlen (argv[i]) + 1 < ((char *) real_mode_mem
795
+ GRUB_LINUX_CL_END_OFFSET);
799
dest = grub_stpcpy (dest, argv[i]);
803
if (grub_file_read (file, (void *) GRUB_LINUX_BZIMAGE_ADDR, len) != len)
804
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
806
if (grub_errno == GRUB_ERR_NONE)
808
grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
815
grub_file_close (file);
817
if (grub_errno != GRUB_ERR_NONE)
819
grub_dl_unref (my_mod);
827
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
828
int argc, char *argv[])
830
grub_file_t file = 0;
832
grub_addr_t addr_min, addr_max;
834
grub_efi_uintn_t mmap_size;
835
grub_efi_memory_descriptor_t *desc;
836
grub_efi_uintn_t desc_size;
837
struct linux_kernel_header *lh;
841
grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
847
grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
851
file = grub_file_open (argv[0]);
855
size = grub_file_size (file);
856
initrd_pages = (page_align (size) >> 12);
858
lh = (struct linux_kernel_header *) real_mode_mem;
860
addr_max = (grub_cpu_to_le32 (lh->initrd_addr_max) << 10);
861
if (linux_mem_size != 0 && linux_mem_size < addr_max)
862
addr_max = linux_mem_size;
864
/* Linux 2.3.xx has a bug in the memory range check, so avoid
866
Linux 2.2.xx has a bug in the memory range check, which is
867
worse than that of Linux 2.3.xx, so avoid the last 64kb. */
870
/* Usually, the compression ratio is about 50%. */
871
addr_min = (grub_addr_t) prot_mode_mem + ((prot_mode_pages * 3) << 12)
874
/* Find the highest address to put the initrd. */
875
mmap_size = find_mmap_size ();
876
if (grub_efi_get_memory_map (&mmap_size, mmap_buf, 0, &desc_size, 0) <= 0)
877
grub_fatal ("cannot get memory map");
880
for (desc = mmap_buf;
881
desc < NEXT_MEMORY_DESCRIPTOR (mmap_buf, mmap_size);
882
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
884
if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
885
&& desc->num_pages >= initrd_pages)
887
grub_efi_physical_address_t physical_end;
889
physical_end = desc->physical_start + (desc->num_pages << 12);
890
if (physical_end > addr_max)
891
physical_end = addr_max;
893
if (physical_end < page_align (size))
896
physical_end -= page_align (size);
898
if ((physical_end >= addr_min) &&
899
(physical_end >= desc->physical_start) &&
900
(physical_end > addr))
907
grub_error (GRUB_ERR_OUT_OF_MEMORY, "no free pages available");
911
initrd_mem = grub_efi_allocate_pages (addr, initrd_pages);
913
grub_fatal ("cannot allocate pages");
915
if (grub_file_read (file, initrd_mem, size) != size)
917
grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
921
grub_printf (" [Initrd, addr=0x%x, size=0x%x]\n",
922
(unsigned) addr, (unsigned) size);
924
lh->ramdisk_image = addr;
925
lh->ramdisk_size = size;
926
lh->root_dev = 0x0100; /* XXX */
930
grub_file_close (file);
935
static grub_command_t cmd_linux, cmd_initrd;
939
cmd_linux = grub_register_command ("linux", grub_cmd_linux,
940
0, N_("Load Linux."));
941
cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
942
0, N_("Load initrd."));
948
grub_unregister_command (cmd_linux);
949
grub_unregister_command (cmd_initrd);