remove all trailing whitespace
[grub2/phcoder/solaris.git] / loader / i386 / efi / linux.c
blob97a693e96384cd07d5399d2ce8aeaeb2e019abaa
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009 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>
23 #include <grub/err.h>
24 #include <grub/misc.h>
25 #include <grub/types.h>
26 #include <grub/dl.h>
27 #include <grub/mm.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/efi/uga_draw.h>
33 #include <grub/pci.h>
34 #include <grub/command.h>
35 #include <grub/memory.h>
37 #define GRUB_LINUX_CL_OFFSET 0x1000
38 #define GRUB_LINUX_CL_END_OFFSET 0x2000
40 #define NEXT_MEMORY_DESCRIPTOR(desc, size) \
41 ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
43 static grub_dl_t my_mod;
45 static grub_size_t linux_mem_size;
46 static int loaded;
47 static void *real_mode_mem;
48 static void *prot_mode_mem;
49 static void *initrd_mem;
50 static grub_efi_uintn_t real_mode_pages;
51 static grub_efi_uintn_t prot_mode_pages;
52 static grub_efi_uintn_t initrd_pages;
53 static void *mmap_buf;
55 static grub_uint8_t gdt[] __attribute__ ((aligned(16))) =
57 /* NULL. */
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 /* Reserved. */
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 /* Code segment. */
62 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00,
63 /* Data segment. */
64 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
67 struct gdt_descriptor
69 grub_uint16_t limit;
70 void *base;
71 } __attribute__ ((packed));
73 static struct gdt_descriptor gdt_desc =
75 sizeof (gdt) - 1,
76 gdt
79 struct idt_descriptor
81 grub_uint16_t limit;
82 void *base;
83 } __attribute__ ((packed));
85 static struct idt_descriptor idt_desc =
91 static inline grub_size_t
92 page_align (grub_size_t size)
94 return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
97 /* Find the optimal number of pages for the memory map. Is it better to
98 move this code to efi/mm.c? */
99 static grub_efi_uintn_t
100 find_mmap_size (void)
102 static grub_efi_uintn_t mmap_size = 0;
104 if (mmap_size != 0)
105 return mmap_size;
107 mmap_size = (1 << 12);
108 while (1)
110 int ret;
111 grub_efi_memory_descriptor_t *mmap;
112 grub_efi_uintn_t desc_size;
114 mmap = grub_malloc (mmap_size);
115 if (! mmap)
116 return 0;
118 ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0);
119 grub_free (mmap);
121 if (ret < 0)
122 grub_fatal ("cannot get memory map");
123 else if (ret > 0)
124 break;
126 mmap_size += (1 << 12);
129 /* Increase the size a bit for safety, because GRUB allocates more on
130 later, and EFI itself may allocate more. */
131 mmap_size += (1 << 12);
133 return page_align (mmap_size);
136 static void
137 free_pages (void)
139 if (real_mode_mem)
141 grub_efi_free_pages ((grub_addr_t) real_mode_mem, real_mode_pages);
142 real_mode_mem = 0;
145 if (prot_mode_mem)
147 grub_efi_free_pages ((grub_addr_t) prot_mode_mem, prot_mode_pages);
148 prot_mode_mem = 0;
151 if (initrd_mem)
153 grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
154 initrd_mem = 0;
158 /* Allocate pages for the real mode code and the protected mode code
159 for linux as well as a memory map buffer. */
160 static int
161 allocate_pages (grub_size_t prot_size)
163 grub_efi_uintn_t desc_size;
164 grub_efi_memory_descriptor_t *mmap, *mmap_end;
165 grub_efi_uintn_t mmap_size, tmp_mmap_size;
166 grub_efi_memory_descriptor_t *desc;
167 grub_size_t real_size;
169 /* Make sure that each size is aligned to a page boundary. */
170 real_size = GRUB_LINUX_CL_END_OFFSET;
171 prot_size = page_align (prot_size);
172 mmap_size = find_mmap_size ();
174 grub_dprintf ("linux", "real_size = %x, prot_size = %x, mmap_size = %x\n",
175 (unsigned) real_size, (unsigned) prot_size, (unsigned) mmap_size);
177 /* Calculate the number of pages; Combine the real mode code with
178 the memory map buffer for simplicity. */
179 real_mode_pages = ((real_size + mmap_size) >> 12);
180 prot_mode_pages = (prot_size >> 12);
182 /* Initialize the memory pointers with NULL for convenience. */
183 real_mode_mem = 0;
184 prot_mode_mem = 0;
186 /* Read the memory map temporarily, to find free space. */
187 mmap = grub_malloc (mmap_size);
188 if (! mmap)
189 return 0;
191 tmp_mmap_size = mmap_size;
192 if (grub_efi_get_memory_map (&tmp_mmap_size, mmap, 0, &desc_size, 0) <= 0)
193 grub_fatal ("cannot get memory map");
195 mmap_end = NEXT_MEMORY_DESCRIPTOR (mmap, tmp_mmap_size);
197 /* First, find free pages for the real mode code
198 and the memory map buffer. */
199 for (desc = mmap;
200 desc < mmap_end;
201 desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
203 /* Probably it is better to put the real mode code in the traditional
204 space for safety. */
205 if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
206 && desc->physical_start <= 0x90000
207 && desc->num_pages >= real_mode_pages)
209 grub_efi_physical_address_t physical_end;
210 grub_efi_physical_address_t addr;
212 physical_end = desc->physical_start + (desc->num_pages << 12);
213 if (physical_end > 0x90000)
214 physical_end = 0x90000;
216 grub_dprintf ("linux", "physical_start = %x, physical_end = %x\n",
217 (unsigned) desc->physical_start,
218 (unsigned) physical_end);
219 addr = physical_end - real_size - mmap_size;
220 if (addr < 0x10000)
221 continue;
223 grub_dprintf ("linux", "trying to allocate %u pages at %lx\n",
224 (unsigned) real_mode_pages, (unsigned long) addr);
225 real_mode_mem = grub_efi_allocate_pages (addr, real_mode_pages);
226 if (! real_mode_mem)
227 grub_fatal ("cannot allocate pages");
229 desc->num_pages -= real_mode_pages;
230 break;
234 if (! real_mode_mem)
236 grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
237 goto fail;
240 mmap_buf = (void *) ((char *) real_mode_mem + real_size);
242 /* Next, find free pages for the protected mode code. */
243 /* XXX what happens if anything is using this address? */
244 prot_mode_mem = grub_efi_allocate_pages (0x100000, prot_mode_pages + 1);
245 if (! prot_mode_mem)
247 grub_error (GRUB_ERR_OUT_OF_MEMORY,
248 "cannot allocate protected mode pages");
249 goto fail;
252 grub_dprintf ("linux", "real_mode_mem = %lx, real_mode_pages = %x, "
253 "prot_mode_mem = %lx, prot_mode_pages = %x\n",
254 (unsigned long) real_mode_mem, (unsigned) real_mode_pages,
255 (unsigned long) prot_mode_mem, (unsigned) prot_mode_pages);
257 grub_free (mmap);
258 return 1;
260 fail:
261 grub_free (mmap);
262 free_pages ();
263 return 0;
266 static void
267 grub_e820_add_region (struct grub_e820_mmap *e820_map, int *e820_num,
268 grub_uint64_t start, grub_uint64_t size,
269 grub_uint32_t type)
271 int n = *e820_num;
273 if (n >= GRUB_E820_MAX_ENTRY)
274 grub_fatal ("Too many e820 memory map entries");
276 if ((n > 0) && (e820_map[n - 1].addr + e820_map[n - 1].size == start) &&
277 (e820_map[n - 1].type == type))
278 e820_map[n - 1].size += size;
279 else
281 e820_map[n].addr = start;
282 e820_map[n].size = size;
283 e820_map[n].type = type;
284 (*e820_num)++;
288 #ifdef __x86_64__
289 extern grub_uint8_t grub_linux_trampoline_start[];
290 extern grub_uint8_t grub_linux_trampoline_end[];
291 #endif
293 static grub_err_t
294 grub_linux_boot (void)
296 struct linux_kernel_params *params;
297 grub_efi_uintn_t mmap_size;
298 grub_efi_uintn_t map_key;
299 grub_efi_uintn_t desc_size;
300 grub_efi_uint32_t desc_version;
301 int e820_num;
303 params = real_mode_mem;
305 grub_dprintf ("linux", "code32_start = %x, idt_desc = %lx, gdt_desc = %lx\n",
306 (unsigned) params->code32_start,
307 (unsigned long) &(idt_desc.limit),
308 (unsigned long) &(gdt_desc.limit));
309 grub_dprintf ("linux", "idt = %x:%lx, gdt = %x:%lx\n",
310 (unsigned) idt_desc.limit, (unsigned long) idt_desc.base,
311 (unsigned) gdt_desc.limit, (unsigned long) gdt_desc.base);
313 auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
314 int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type)
316 switch (type)
318 case GRUB_MACHINE_MEMORY_AVAILABLE:
319 grub_e820_add_region (params->e820_map, &e820_num,
320 addr, size, GRUB_E820_RAM);
321 break;
323 #ifdef GRUB_MACHINE_MEMORY_ACPI
324 case GRUB_MACHINE_MEMORY_ACPI:
325 grub_e820_add_region (params->e820_map, &e820_num,
326 addr, size, GRUB_E820_ACPI);
327 break;
328 #endif
330 #ifdef GRUB_MACHINE_MEMORY_NVS
331 case GRUB_MACHINE_MEMORY_NVS:
332 grub_e820_add_region (params->e820_map, &e820_num,
333 addr, size, GRUB_E820_NVS);
334 break;
335 #endif
337 #ifdef GRUB_MACHINE_MEMORY_CODE
338 case GRUB_MACHINE_MEMORY_CODE:
339 grub_e820_add_region (params->e820_map, &e820_num,
340 addr, size, GRUB_E820_EXEC_CODE);
341 break;
342 #endif
344 default:
345 grub_e820_add_region (params->e820_map, &e820_num,
346 addr, size, GRUB_E820_RESERVED);
348 return 0;
351 e820_num = 0;
352 grub_mmap_iterate (hook);
353 params->mmap_size = e820_num;
355 mmap_size = find_mmap_size ();
356 if (grub_efi_get_memory_map (&mmap_size, mmap_buf, &map_key,
357 &desc_size, &desc_version) <= 0)
358 grub_fatal ("cannot get memory map");
360 if (! grub_efi_exit_boot_services (map_key))
361 grub_fatal ("cannot exit boot services");
363 /* Note that no boot services are available from here. */
365 /* Pass EFI parameters. */
366 if (grub_le_to_cpu16 (params->version) >= 0x0206)
368 params->v0206.efi_mem_desc_size = desc_size;
369 params->v0206.efi_mem_desc_version = desc_version;
370 params->v0206.efi_mmap = (grub_uint32_t) (unsigned long) mmap_buf;
371 params->v0206.efi_mmap_size = mmap_size;
372 #ifdef __x86_64__
373 params->v0206.efi_mmap_hi = (grub_uint32_t) ((grub_uint64_t) mmap_buf >> 32);
374 #endif
376 else if (grub_le_to_cpu16 (params->version) >= 0x0204)
378 params->v0204.efi_mem_desc_size = desc_size;
379 params->v0204.efi_mem_desc_version = desc_version;
380 params->v0204.efi_mmap = (grub_uint32_t) (unsigned long) mmap_buf;
381 params->v0204.efi_mmap_size = mmap_size;
384 #ifdef __x86_64__
386 grub_memcpy ((char *) prot_mode_mem + (prot_mode_pages << 12),
387 grub_linux_trampoline_start,
388 grub_linux_trampoline_end - grub_linux_trampoline_start);
390 ((void (*) (unsigned long, void *)) ((char *) prot_mode_mem
391 + (prot_mode_pages << 12)))
392 (params->code32_start, real_mode_mem);
394 #else
396 /* Hardware interrupts are not safe any longer. */
397 asm volatile ("cli" : : );
399 /* Load the IDT and the GDT for the bootstrap. */
400 asm volatile ("lidt %0" : : "m" (idt_desc));
401 asm volatile ("lgdt %0" : : "m" (gdt_desc));
403 /* Pass parameters. */
404 asm volatile ("movl %0, %%ecx" : : "m" (params->code32_start));
405 asm volatile ("movl %0, %%esi" : : "m" (real_mode_mem));
407 asm volatile ("xorl %%ebx, %%ebx" : : );
409 /* Enter Linux. */
410 asm volatile ("jmp *%%ecx" : : );
412 #endif
414 /* Never reach here. */
415 return GRUB_ERR_NONE;
418 static grub_err_t
419 grub_linux_unload (void)
421 free_pages ();
422 grub_dl_unref (my_mod);
423 loaded = 0;
424 return GRUB_ERR_NONE;
427 static grub_efi_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
430 #define RGB_MASK 0xffffff
431 #define RGB_MAGIC 0x121314
432 #define LINE_MIN 800
433 #define LINE_MAX 4096
434 #define FBTEST_STEP (0x10000 >> 2)
435 #define FBTEST_COUNT 8
437 static int
438 find_line_len (grub_uint32_t *fb_base, grub_uint32_t *line_len)
440 grub_uint32_t *base = (grub_uint32_t *) (grub_target_addr_t) *fb_base;
441 int i;
443 for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP)
445 if ((*base & RGB_MASK) == RGB_MAGIC)
447 int j;
449 for (j = LINE_MIN; j <= LINE_MAX; j++)
451 if ((base[j] & RGB_MASK) == RGB_MAGIC)
453 *fb_base = (grub_uint32_t) (grub_target_addr_t) base;
454 *line_len = j << 2;
456 return 1;
460 break;
464 return 0;
467 static int
468 find_framebuf (grub_uint32_t *fb_base, grub_uint32_t *line_len)
470 int found = 0;
472 auto int NESTED_FUNC_ATTR find_card (int bus, int dev, int func,
473 grub_pci_id_t pciid);
475 int NESTED_FUNC_ATTR find_card (int bus, int dev, int func,
476 grub_pci_id_t pciid)
478 grub_pci_address_t addr;
480 addr = grub_pci_make_address (bus, dev, func, 2);
481 if (grub_pci_read (addr) >> 24 == 0x3)
483 int i;
485 grub_printf ("Display controller: %d:%d.%d\nDevice id: %x\n",
486 bus, dev, func, pciid);
487 addr += 8;
488 for (i = 0; i < 6; i++, addr += 4)
490 grub_uint32_t old_bar1, old_bar2, type;
491 grub_uint64_t base64;
493 old_bar1 = grub_pci_read (addr);
494 if ((! old_bar1) || (old_bar1 & GRUB_PCI_ADDR_SPACE_IO))
495 continue;
497 type = old_bar1 & GRUB_PCI_ADDR_MEM_TYPE_MASK;
498 if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
500 if (i == 5)
501 break;
503 old_bar2 = grub_pci_read (addr + 4);
505 else
506 old_bar2 = 0;
508 base64 = old_bar2;
509 base64 <<= 32;
510 base64 |= (old_bar1 & GRUB_PCI_ADDR_MEM_MASK);
512 grub_printf ("%s(%d): 0x%llx\n",
513 ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) ?
514 "VMEM" : "MMIO"), i,
515 (unsigned long long) base64);
517 if ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) && (! found))
519 *fb_base = base64;
520 if (find_line_len (fb_base, line_len))
521 found++;
524 if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
526 i++;
527 addr += 4;
532 return found;
535 grub_pci_iterate (find_card);
536 return found;
539 static int
540 grub_linux_setup_video (struct linux_kernel_params *params)
542 grub_efi_uga_draw_protocol_t *c;
543 grub_uint32_t width, height, depth, rate, pixel, fb_base, line_len;
544 int ret;
546 c = grub_efi_locate_protocol (&uga_draw_guid, 0);
547 if (! c)
548 return 1;
550 if (efi_call_5 (c->get_mode, c, &width, &height, &depth, &rate))
551 return 1;
553 grub_printf ("Video mode: %ux%u-%u@%u\n", width, height, depth, rate);
555 grub_efi_set_text_mode (0);
556 pixel = RGB_MAGIC;
557 efi_call_10 (c->blt, c, (struct grub_efi_uga_pixel *) &pixel,
558 GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0);
559 ret = find_framebuf (&fb_base, &line_len);
560 grub_efi_set_text_mode (1);
562 if (! ret)
564 grub_printf ("Can\'t find frame buffer address\n");
565 return 1;
568 grub_printf ("Frame buffer base: 0x%x\n", fb_base);
569 grub_printf ("Video line length: %d\n", line_len);
571 params->lfb_width = width;
572 params->lfb_height = height;
573 params->lfb_depth = depth;
574 params->lfb_line_len = line_len;
576 params->lfb_base = fb_base;
577 params->lfb_size = (line_len * params->lfb_height + 65535) >> 16;
579 params->red_mask_size = 8;
580 params->red_field_pos = 16;
581 params->green_mask_size = 8;
582 params->green_field_pos = 8;
583 params->blue_mask_size = 8;
584 params->blue_field_pos = 0;
585 params->reserved_mask_size = 8;
586 params->reserved_field_pos = 24;
588 params->have_vga = GRUB_VIDEO_TYPE_VLFB;
589 params->vid_mode = 0x338; /* 1024x768x32 */
591 return 0;
594 static grub_err_t
595 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
596 int argc, char *argv[])
598 grub_file_t file = 0;
599 struct linux_kernel_header lh;
600 struct linux_kernel_params *params;
601 grub_uint8_t setup_sects;
602 grub_size_t real_size, prot_size;
603 grub_ssize_t len;
604 int i;
605 char *dest;
607 grub_dl_ref (my_mod);
609 if (argc == 0)
611 grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
612 goto fail;
615 file = grub_file_open (argv[0]);
616 if (! file)
617 goto fail;
619 if (grub_file_read (file, (char *) &lh, sizeof (lh)) != sizeof (lh))
621 grub_error (GRUB_ERR_READ_ERROR, "cannot read the linux header");
622 goto fail;
625 if (lh.boot_flag != grub_cpu_to_le16 (0xaa55))
627 grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
628 goto fail;
631 if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
633 grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
634 goto fail;
637 /* EFI support is quite new, so reject old versions. */
638 if (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)
639 || grub_le_to_cpu16 (lh.version) < 0x0203)
641 grub_error (GRUB_ERR_BAD_OS, "too old version");
642 goto fail;
645 /* I'm not sure how to support zImage on EFI. */
646 if (! (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
648 grub_error (GRUB_ERR_BAD_OS, "zImage is not supported");
649 goto fail;
652 setup_sects = lh.setup_sects;
654 /* If SETUP_SECTS is not set, set it to the default (4). */
655 if (! setup_sects)
656 setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
658 real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
659 prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
661 if (! allocate_pages (prot_size))
662 goto fail;
664 params = (struct linux_kernel_params *) real_mode_mem;
665 grub_memset (params, 0, GRUB_LINUX_CL_END_OFFSET);
666 grub_memcpy (&params->setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
668 params->ps_mouse = params->padding10 = 0;
670 len = 0x400 - sizeof (lh);
671 if (grub_file_read (file, (char *) real_mode_mem + sizeof (lh), len) != len)
673 grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
674 goto fail;
677 /* XXX Linux assumes that only elilo can boot Linux on EFI!!! */
678 params->type_of_loader = (LINUX_LOADER_ID_ELILO << 4);
680 params->cl_magic = GRUB_LINUX_CL_MAGIC;
681 params->cl_offset = 0x1000;
682 params->cmd_line_ptr = (unsigned long) real_mode_mem + 0x1000;
683 params->ramdisk_image = 0;
684 params->ramdisk_size = 0;
686 params->heap_end_ptr = GRUB_LINUX_HEAP_END_OFFSET;
687 params->loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
689 /* These are not needed to be precise, because Linux uses these values
690 only to raise an error when the decompression code cannot find good
691 space. */
692 params->ext_mem = ((32 * 0x100000) >> 10);
693 params->alt_mem = ((32 * 0x100000) >> 10);
695 params->video_cursor_x = grub_getxy () >> 8;
696 params->video_cursor_y = grub_getxy () & 0xff;
697 params->video_page = 0; /* ??? */
698 params->video_mode = grub_efi_system_table->con_out->mode->mode;
699 params->video_width = (grub_getwh () >> 8);
700 params->video_ega_bx = 0;
701 params->video_height = (grub_getwh () & 0xff);
702 params->have_vga = 0;
703 params->font_size = 16; /* XXX */
705 if (grub_le_to_cpu16 (params->version) >= 0x0206)
707 params->v0206.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
708 params->v0206.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table;
709 #ifdef __x86_64__
710 params->v0206.efi_system_table_hi = (grub_uint32_t) ((grub_uint64_t) grub_efi_system_table >> 32);
711 #endif
713 else if (grub_le_to_cpu16 (params->version) >= 0x0204)
715 params->v0204.efi_signature = GRUB_LINUX_EFI_SIGNATURE_0204;
716 params->v0204.efi_system_table = (grub_uint32_t) (unsigned long) grub_efi_system_table;
719 #if 0
720 /* The structure is zeroed already. */
722 /* No VBE on EFI. */
723 params->lfb_width = 0;
724 params->lfb_height = 0;
725 params->lfb_depth = 0;
726 params->lfb_base = 0;
727 params->lfb_size = 0;
728 params->lfb_line_len = 0;
729 params->red_mask_size = 0;
730 params->red_field_pos = 0;
731 params->green_mask_size = 0;
732 params->green_field_pos = 0;
733 params->blue_mask_size = 0;
734 params->blue_field_pos = 0;
735 params->reserved_mask_size = 0;
736 params->reserved_field_pos = 0;
737 params->vesapm_segment = 0;
738 params->vesapm_offset = 0;
739 params->lfb_pages = 0;
740 params->vesa_attrib = 0;
742 /* No APM on EFI. */
743 params->apm_version = 0;
744 params->apm_code_segment = 0;
745 params->apm_entry = 0;
746 params->apm_16bit_code_segment = 0;
747 params->apm_data_segment = 0;
748 params->apm_flags = 0;
749 params->apm_code_len = 0;
750 params->apm_data_len = 0;
752 /* XXX is there any way to use SpeedStep on EFI? */
753 params->ist_signature = 0;
754 params->ist_command = 0;
755 params->ist_event = 0;
756 params->ist_perf_level = 0;
758 /* Let the kernel probe the information. */
759 grub_memset (params->hd0_drive_info, 0, sizeof (params->hd0_drive_info));
760 grub_memset (params->hd1_drive_info, 0, sizeof (params->hd1_drive_info));
762 /* No MCA on EFI. */
763 params->rom_config_len = 0;
765 /* No need to fake the BIOS's memory map. */
766 params->mmap_size = 0;
768 /* Let the kernel probe the information. */
769 params->ps_mouse = 0;
771 /* Clear padding for future compatibility. */
772 grub_memset (params->padding1, 0, sizeof (params->padding1));
773 grub_memset (params->padding2, 0, sizeof (params->padding2));
774 grub_memset (params->padding3, 0, sizeof (params->padding3));
775 grub_memset (params->padding4, 0, sizeof (params->padding4));
776 grub_memset (params->padding5, 0, sizeof (params->padding5));
777 grub_memset (params->padding6, 0, sizeof (params->padding6));
778 grub_memset (params->padding7, 0, sizeof (params->padding7));
779 grub_memset (params->padding8, 0, sizeof (params->padding8));
780 grub_memset (params->padding9, 0, sizeof (params->padding9));
782 #endif
784 /* The other EFI parameters are filled when booting. */
786 grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
788 /* XXX there is no way to know if the kernel really supports EFI. */
789 grub_printf (" [Linux-bzImage, setup=0x%x, size=0x%x]\n",
790 (unsigned) real_size, (unsigned) prot_size);
792 grub_linux_setup_video (params);
794 /* Detect explicitly specified memory size, if any. */
795 linux_mem_size = 0;
796 for (i = 1; i < argc; i++)
797 if (grub_memcmp (argv[i], "mem=", 4) == 0)
799 char *val = argv[i] + 4;
801 linux_mem_size = grub_strtoul (val, &val, 0);
803 if (grub_errno)
805 grub_errno = GRUB_ERR_NONE;
806 linux_mem_size = 0;
808 else
810 int shift = 0;
812 switch (grub_tolower (val[0]))
814 case 'g':
815 shift += 10;
816 case 'm':
817 shift += 10;
818 case 'k':
819 shift += 10;
820 default:
821 break;
824 /* Check an overflow. */
825 if (linux_mem_size > (~0UL >> shift))
826 linux_mem_size = 0;
827 else
828 linux_mem_size <<= shift;
831 else if (grub_memcmp (argv[i], "video=efifb", 11) == 0)
833 if (params->have_vga)
834 params->have_vga = GRUB_VIDEO_TYPE_EFI;
837 /* Specify the boot file. */
838 dest = grub_stpcpy ((char *) real_mode_mem + GRUB_LINUX_CL_OFFSET,
839 "BOOT_IMAGE=");
840 dest = grub_stpcpy (dest, argv[0]);
842 /* Copy kernel parameters. */
843 for (i = 1;
844 i < argc
845 && dest + grub_strlen (argv[i]) + 1 < ((char *) real_mode_mem
846 + GRUB_LINUX_CL_END_OFFSET);
847 i++)
849 *dest++ = ' ';
850 dest = grub_stpcpy (dest, argv[i]);
853 len = prot_size;
854 if (grub_file_read (file, (char *) GRUB_LINUX_BZIMAGE_ADDR, len) != len)
855 grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
857 if (grub_errno == GRUB_ERR_NONE)
859 grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
860 loaded = 1;
863 fail:
865 if (file)
866 grub_file_close (file);
868 if (grub_errno != GRUB_ERR_NONE)
870 grub_dl_unref (my_mod);
871 loaded = 0;
874 return grub_errno;
877 static grub_err_t
878 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
879 int argc, char *argv[])
881 grub_file_t file = 0;
882 grub_ssize_t size;
883 grub_addr_t addr_min, addr_max;
884 grub_addr_t addr;
885 grub_efi_uintn_t mmap_size;
886 grub_efi_memory_descriptor_t *desc;
887 grub_efi_uintn_t desc_size;
888 struct linux_kernel_header *lh;
890 if (argc == 0)
892 grub_error (GRUB_ERR_BAD_ARGUMENT, "No module specified");
893 goto fail;
896 if (! loaded)
898 grub_error (GRUB_ERR_BAD_ARGUMENT, "You need to load the kernel first.");
899 goto fail;
902 file = grub_file_open (argv[0]);
903 if (! file)
904 goto fail;
906 size = grub_file_size (file);
907 initrd_pages = (page_align (size) >> 12);
909 lh = (struct linux_kernel_header *) real_mode_mem;
911 addr_max = (grub_cpu_to_le32 (lh->initrd_addr_max) << 10);
912 if (linux_mem_size != 0 && linux_mem_size < addr_max)
913 addr_max = linux_mem_size;
915 /* Linux 2.3.xx has a bug in the memory range check, so avoid
916 the last page.
917 Linux 2.2.xx has a bug in the memory range check, which is
918 worse than that of Linux 2.3.xx, so avoid the last 64kb. */
919 addr_max -= 0x10000;
921 /* Usually, the compression ratio is about 50%. */
922 addr_min = (grub_addr_t) prot_mode_mem + ((prot_mode_pages * 3) << 12)
923 + page_align (size);
925 /* Find the highest address to put the initrd. */
926 mmap_size = find_mmap_size ();
927 if (grub_efi_get_memory_map (&mmap_size, mmap_buf, 0, &desc_size, 0) <= 0)
928 grub_fatal ("cannot get memory map");
930 addr = 0;
931 for (desc = mmap_buf;
932 desc < NEXT_MEMORY_DESCRIPTOR (mmap_buf, mmap_size);
933 desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
935 if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
936 && desc->num_pages >= initrd_pages)
938 grub_efi_physical_address_t physical_end;
940 physical_end = desc->physical_start + (desc->num_pages << 12);
941 if (physical_end > addr_max)
942 physical_end = addr_max;
944 if (physical_end < page_align (size))
945 continue;
947 physical_end -= page_align (size);
949 if ((physical_end >= addr_min) &&
950 (physical_end >= desc->physical_start) &&
951 (physical_end > addr))
952 addr = physical_end;
956 if (addr == 0)
958 grub_error (GRUB_ERR_OUT_OF_MEMORY, "no free pages available");
959 goto fail;
962 initrd_mem = grub_efi_allocate_pages (addr, initrd_pages);
963 if (! initrd_mem)
964 grub_fatal ("cannot allocate pages");
966 if (grub_file_read (file, initrd_mem, size) != size)
968 grub_error (GRUB_ERR_FILE_READ_ERROR, "Couldn't read file");
969 goto fail;
972 grub_printf (" [Initrd, addr=0x%x, size=0x%x]\n",
973 (unsigned) addr, (unsigned) size);
975 lh->ramdisk_image = addr;
976 lh->ramdisk_size = size;
977 lh->root_dev = 0x0100; /* XXX */
979 fail:
980 if (file)
981 grub_file_close (file);
983 return grub_errno;
986 static grub_command_t cmd_linux, cmd_initrd;
988 GRUB_MOD_INIT(linux)
990 cmd_linux = grub_register_command ("linux", grub_cmd_linux,
991 0, "load linux");
992 cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
993 0, "load initrd");
994 my_mod = mod;
997 GRUB_MOD_FINI(linux)
999 grub_unregister_command (cmd_linux);
1000 grub_unregister_command (cmd_initrd);