2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2008,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/file.h>
21 #include <grub/disk.h>
23 #include <grub/misc.h>
24 #include <grub/types.h>
25 #include <grub/command.h>
28 #include <grub/cache.h>
29 #include <grub/kernel.h>
30 #include <grub/efi/api.h>
31 #include <grub/efi/efi.h>
33 #include <grub/i18n.h>
36 GRUB_MOD_LICENSE ("GPLv3+");
38 #pragma GCC diagnostic ignored "-Wcast-align"
40 #define ALIGN_MIN (256*1024*1024)
42 #define GRUB_ELF_SEARCH 1024
44 #define BOOT_PARAM_SIZE 16384
46 struct ia64_boot_param
48 grub_uint64_t command_line
; /* physical address of command line. */
49 grub_uint64_t efi_systab
; /* physical address of EFI system table */
50 grub_uint64_t efi_memmap
; /* physical address of EFI memory map */
51 grub_uint64_t efi_memmap_size
; /* size of EFI memory map */
52 grub_uint64_t efi_memdesc_size
; /* size of an EFI memory map descriptor */
53 grub_uint32_t efi_memdesc_version
; /* memory descriptor version */
56 grub_uint16_t num_cols
; /* number of columns on console output dev */
57 grub_uint16_t num_rows
; /* number of rows on console output device */
58 grub_uint16_t orig_x
; /* cursor's x position */
59 grub_uint16_t orig_y
; /* cursor's y position */
61 grub_uint64_t fpswa
; /* physical address of the fpswa interface */
62 grub_uint64_t initrd_start
;
63 grub_uint64_t initrd_size
;
68 grub_uint32_t revision
;
69 grub_uint32_t reserved
;
72 static fpswa_interface_t
*fpswa
;
74 #define NEXT_MEMORY_DESCRIPTOR(desc, size) \
75 ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
77 static grub_dl_t my_mod
;
81 /* Kernel base and size. */
82 static void *kernel_mem
;
83 static grub_efi_uintn_t kernel_pages
;
84 static grub_uint64_t entry
;
86 /* Initrd base and size. */
87 static void *initrd_mem
;
88 static grub_efi_uintn_t initrd_pages
;
89 static grub_efi_uintn_t initrd_size
;
91 static struct ia64_boot_param
*boot_param
;
92 static grub_efi_uintn_t boot_param_pages
;
94 static inline grub_size_t
95 page_align (grub_size_t size
)
97 return (size
+ (1 << 12) - 1) & (~((1 << 12) - 1));
103 grub_efi_handle_t fpswa_image
;
104 grub_efi_boot_services_t
*bs
;
105 grub_efi_status_t status
;
106 grub_efi_uintn_t size
;
107 static const grub_efi_guid_t fpswa_protocol
=
108 { 0xc41b6531, 0x97b9, 0x11d3,
109 {0x9a, 0x29, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d} };
114 size
= sizeof(grub_efi_handle_t
);
116 bs
= grub_efi_system_table
->boot_services
;
117 status
= bs
->locate_handle (GRUB_EFI_BY_PROTOCOL
,
118 (void *) &fpswa_protocol
,
119 NULL
, &size
, &fpswa_image
);
120 if (status
!= GRUB_EFI_SUCCESS
)
122 grub_printf ("%s\n", _("Could not locate FPSWA driver"));
125 status
= bs
->handle_protocol (fpswa_image
,
126 (void *) &fpswa_protocol
, (void *) &fpswa
);
127 if (status
!= GRUB_EFI_SUCCESS
)
130 _("FPSWA protocol wasn't able to find the interface"));
135 /* Find the optimal number of pages for the memory map. Is it better to
136 move this code to efi/mm.c? */
137 static grub_efi_uintn_t
138 find_mmap_size (void)
140 static grub_efi_uintn_t mmap_size
= 0;
145 mmap_size
= (1 << 12);
149 grub_efi_memory_descriptor_t
*mmap
;
150 grub_efi_uintn_t desc_size
;
152 mmap
= grub_malloc (mmap_size
);
156 ret
= grub_efi_get_memory_map (&mmap_size
, mmap
, 0, &desc_size
, 0);
161 grub_error (GRUB_ERR_IO
, "cannot get memory map");
167 mmap_size
+= (1 << 12);
170 /* Increase the size a bit for safety, because GRUB allocates more on
171 later, and EFI itself may allocate more. */
172 mmap_size
+= (1 << 12);
174 return page_align (mmap_size
);
182 grub_efi_free_pages ((grub_addr_t
) kernel_mem
, kernel_pages
);
188 grub_efi_free_pages ((grub_addr_t
) initrd_mem
, initrd_pages
);
194 /* Free bootparam. */
195 grub_efi_free_pages ((grub_efi_physical_address_t
) boot_param
,
202 allocate_pages (grub_uint64_t align
, grub_uint64_t size_pages
,
203 grub_uint64_t nobase
)
206 grub_efi_uintn_t desc_size
;
207 grub_efi_memory_descriptor_t
*mmap
, *mmap_end
;
208 grub_efi_uintn_t mmap_size
, tmp_mmap_size
;
209 grub_efi_memory_descriptor_t
*desc
;
212 size
= size_pages
<< 12;
214 mmap_size
= find_mmap_size ();
218 /* Read the memory map temporarily, to find free space. */
219 mmap
= grub_malloc (mmap_size
);
223 tmp_mmap_size
= mmap_size
;
224 if (grub_efi_get_memory_map (&tmp_mmap_size
, mmap
, 0, &desc_size
, 0) <= 0)
226 grub_error (GRUB_ERR_IO
, "cannot get memory map");
230 mmap_end
= NEXT_MEMORY_DESCRIPTOR (mmap
, tmp_mmap_size
);
232 /* First, find free pages for the real mode code
233 and the memory map buffer. */
236 desc
= NEXT_MEMORY_DESCRIPTOR (desc
, desc_size
))
238 grub_uint64_t start
, end
;
239 grub_uint64_t aligned_start
;
241 if (desc
->type
!= GRUB_EFI_CONVENTIONAL_MEMORY
)
244 start
= desc
->physical_start
;
245 end
= start
+ (desc
->num_pages
<< 12);
246 /* Align is a power of 2. */
247 aligned_start
= (start
+ align
- 1) & ~(align
- 1);
248 if (aligned_start
+ size
> end
)
250 if (aligned_start
== nobase
)
251 aligned_start
+= align
;
252 if (aligned_start
+ size
> end
)
254 mem
= grub_efi_allocate_pages (aligned_start
, size_pages
);
257 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate memory");
265 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate memory");
279 set_boot_param_console (void)
281 grub_efi_simple_text_output_interface_t
*conout
;
282 grub_efi_uintn_t cols
, rows
;
284 conout
= grub_efi_system_table
->con_out
;
285 if (conout
->query_mode (conout
, conout
->mode
->mode
, &cols
, &rows
)
289 grub_dprintf ("linux",
290 "Console info: cols=%lu rows=%lu x=%u y=%u\n",
292 conout
->mode
->cursor_column
, conout
->mode
->cursor_row
);
294 boot_param
->console_info
.num_cols
= cols
;
295 boot_param
->console_info
.num_rows
= rows
;
296 boot_param
->console_info
.orig_x
= conout
->mode
->cursor_column
;
297 boot_param
->console_info
.orig_y
= conout
->mode
->cursor_row
;
301 grub_linux_boot (void)
303 grub_efi_uintn_t mmap_size
;
304 grub_efi_uintn_t map_key
;
305 grub_efi_uintn_t desc_size
;
306 grub_efi_uint32_t desc_version
;
307 grub_efi_memory_descriptor_t
*mmap_buf
;
312 boot_param
->fpswa
= (grub_uint64_t
)fpswa
;
315 boot_param
->initrd_start
= (grub_uint64_t
)initrd_mem
;
316 boot_param
->initrd_size
= (grub_uint64_t
)initrd_size
;
318 set_boot_param_console ();
320 grub_dprintf ("linux", "Jump to %016lx\n", entry
);
323 Must be done after grub_machine_fini because map_key is used by
324 exit_boot_services. */
325 mmap_size
= find_mmap_size ();
328 mmap_buf
= grub_efi_allocate_pages (0, page_align (mmap_size
) >> 12);
330 return grub_error (GRUB_ERR_IO
, "cannot allocate memory map");
331 err
= grub_efi_finish_boot_services (&mmap_size
, mmap_buf
, &map_key
,
332 &desc_size
, &desc_version
);
336 boot_param
->efi_memmap
= (grub_uint64_t
)mmap_buf
;
337 boot_param
->efi_memmap_size
= mmap_size
;
338 boot_param
->efi_memdesc_size
= desc_size
;
339 boot_param
->efi_memdesc_version
= desc_version
;
341 /* See you next boot. */
342 asm volatile ("mov r28=%1; br.sptk.few %0" :: "b"(entry
),"r"(boot_param
));
344 /* Never reach here. */
345 return GRUB_ERR_NONE
;
349 grub_linux_unload (void)
352 grub_dl_unref (my_mod
);
354 return GRUB_ERR_NONE
;
358 grub_load_elf64 (grub_file_t file
, void *buffer
, const char *filename
)
360 Elf64_Ehdr
*ehdr
= (Elf64_Ehdr
*) buffer
;
363 grub_uint64_t low_addr
;
364 grub_uint64_t high_addr
;
366 grub_uint64_t reloc_offset
;
367 const char *relocate
;
369 if (ehdr
->e_ident
[EI_MAG0
] != ELFMAG0
370 || ehdr
->e_ident
[EI_MAG1
] != ELFMAG1
371 || ehdr
->e_ident
[EI_MAG2
] != ELFMAG2
372 || ehdr
->e_ident
[EI_MAG3
] != ELFMAG3
373 || ehdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
)
374 return grub_error(GRUB_ERR_UNKNOWN_OS
,
375 N_("invalid arch-independent ELF magic"));
377 if (ehdr
->e_ident
[EI_CLASS
] != ELFCLASS64
378 || ehdr
->e_version
!= EV_CURRENT
379 || ehdr
->e_machine
!= EM_IA_64
)
380 return grub_error (GRUB_ERR_UNKNOWN_OS
,
381 N_("invalid arch-dependent ELF magic"));
383 if (ehdr
->e_type
!= ET_EXEC
)
384 return grub_error (GRUB_ERR_UNKNOWN_OS
,
385 N_("this ELF file is not of the right type"));
387 /* FIXME: Should we support program headers at strange locations? */
388 if (ehdr
->e_phoff
+ ehdr
->e_phnum
* ehdr
->e_phentsize
> GRUB_ELF_SEARCH
)
389 return grub_error (GRUB_ERR_BAD_OS
, "program header at a too high offset");
391 entry
= ehdr
->e_entry
;
393 /* Compute low, high and align addresses. */
397 for (i
= 0; i
< ehdr
->e_phnum
; i
++)
399 phdr
= (Elf64_Phdr
*) ((char *) buffer
+ ehdr
->e_phoff
400 + i
* ehdr
->e_phentsize
);
401 if (phdr
->p_type
== PT_LOAD
)
403 if (phdr
->p_paddr
< low_addr
)
404 low_addr
= phdr
->p_paddr
;
405 if (phdr
->p_paddr
+ phdr
->p_memsz
> high_addr
)
406 high_addr
= phdr
->p_paddr
+ phdr
->p_memsz
;
407 if (phdr
->p_align
> align
)
408 align
= phdr
->p_align
;
412 if (align
< ALIGN_MIN
)
416 return grub_error (GRUB_ERR_BAD_OS
, "no program entries");
418 kernel_pages
= page_align (high_addr
- low_addr
) >> 12;
420 /* Undocumented on purpose. */
421 relocate
= grub_env_get ("linux_relocate");
422 if (!relocate
|| grub_strcmp (relocate
, "force") != 0)
424 kernel_mem
= grub_efi_allocate_pages (low_addr
, kernel_pages
);
427 /* Try to relocate. */
428 if (! kernel_mem
&& (!relocate
|| grub_strcmp (relocate
, "off") != 0))
430 kernel_mem
= allocate_pages (align
, kernel_pages
, low_addr
);
433 reloc_offset
= (grub_uint64_t
)kernel_mem
- low_addr
;
434 grub_dprintf ("linux", " Relocated at %p (offset=%016lx)\n",
435 kernel_mem
, reloc_offset
);
436 entry
+= reloc_offset
;
440 return grub_error (GRUB_ERR_OUT_OF_MEMORY
,
441 "cannot allocate memory for OS");
443 /* Load every loadable segment in memory. */
444 for (i
= 0; i
< ehdr
->e_phnum
; i
++)
446 phdr
= (Elf64_Phdr
*) ((char *) buffer
+ ehdr
->e_phoff
447 + i
* ehdr
->e_phentsize
);
448 if (phdr
->p_type
== PT_LOAD
)
450 grub_dprintf ("linux", " [paddr=%lx load=%lx memsz=%08lx "
451 "off=%lx flags=%x]\n",
452 phdr
->p_paddr
, phdr
->p_paddr
+ reloc_offset
,
453 phdr
->p_memsz
, phdr
->p_offset
, phdr
->p_flags
);
455 if (grub_file_seek (file
, phdr
->p_offset
) == (grub_off_t
)-1)
458 if (grub_file_read (file
, (void *) (phdr
->p_paddr
+ reloc_offset
),
460 != (grub_ssize_t
) phdr
->p_filesz
)
463 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
468 if (phdr
->p_filesz
< phdr
->p_memsz
)
470 ((char *)(phdr
->p_paddr
+ reloc_offset
+ phdr
->p_filesz
),
471 0, phdr
->p_memsz
- phdr
->p_filesz
);
473 /* Sync caches if necessary. */
474 if (phdr
->p_flags
& PF_X
)
475 grub_arch_sync_caches
476 ((void *)(phdr
->p_paddr
+ reloc_offset
), phdr
->p_memsz
);
484 grub_cmd_linux (grub_command_t cmd
__attribute__ ((unused
)),
485 int argc
, char *argv
[])
487 grub_file_t file
= 0;
488 char buffer
[GRUB_ELF_SEARCH
];
493 grub_dl_ref (my_mod
);
495 grub_loader_unset ();
499 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
503 file
= grub_file_open (argv
[0]);
507 len
= grub_file_read (file
, buffer
, sizeof (buffer
));
508 if (len
< (grub_ssize_t
) sizeof (Elf64_Ehdr
))
511 grub_error (GRUB_ERR_BAD_OS
, N_("premature end of file %s"),
516 grub_dprintf ("linux", "Loading linux: %s\n", argv
[0]);
518 if (grub_load_elf64 (file
, buffer
, argv
[0]))
521 len
= sizeof("BOOT_IMAGE=") + 8;
522 for (i
= 0; i
< argc
; i
++)
523 len
+= grub_strlen (argv
[i
]) + 1;
524 len
+= sizeof (struct ia64_boot_param
) + 512; /* Room for extensions. */
525 boot_param_pages
= page_align (len
) >> 12;
526 boot_param
= grub_efi_allocate_pages (0, boot_param_pages
);
529 grub_error (GRUB_ERR_OUT_OF_MEMORY
,
530 "cannot allocate memory for bootparams");
534 grub_memset (boot_param
, 0, len
);
535 cmdline
= ((char *)(boot_param
+ 1)) + 256;
538 p
= grub_stpcpy (cmdline
, "BOOT_IMAGE");
539 for (i
= 0; i
< argc
; i
++)
542 p
= grub_stpcpy (p
, argv
[i
]);
546 boot_param
->command_line
= (grub_uint64_t
) cmdline
;
547 boot_param
->efi_systab
= (grub_uint64_t
) grub_efi_system_table
;
549 grub_errno
= GRUB_ERR_NONE
;
551 grub_loader_set (grub_linux_boot
, grub_linux_unload
, 0);
555 grub_file_close (file
);
557 if (grub_errno
!= GRUB_ERR_NONE
)
559 grub_efi_free_pages ((grub_efi_physical_address_t
) boot_param
,
561 grub_dl_unref (my_mod
);
567 grub_cmd_initrd (grub_command_t cmd
__attribute__ ((unused
)),
568 int argc
, char *argv
[])
570 grub_file_t
*files
= 0;
577 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
583 grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("you need to load the kernel first"));
587 files
= grub_zalloc (argc
* sizeof (files
[0]));
592 grub_dprintf ("linux", "Loading initrd\n");
593 for (i
= 0; i
< argc
; i
++)
595 grub_file_filter_disable_compression ();
596 files
[i
] = grub_file_open (argv
[i
]);
600 initrd_size
+= ALIGN_UP (grub_file_size (files
[i
]), 4);
601 grub_dprintf ("linux", "File %d: %s\n", i
, argv
[i
]);
604 initrd_pages
= (page_align (initrd_size
) >> 12);
605 initrd_mem
= grub_efi_allocate_pages (0, initrd_pages
);
608 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "cannot allocate pages");
612 grub_dprintf ("linux", "[addr=0x%lx, size=0x%lx]\n",
613 (grub_uint64_t
) initrd_mem
, initrd_size
);
616 for (i
= 0; i
< nfiles
; i
++)
618 grub_ssize_t cursize
= grub_file_size (files
[i
]);
619 if (grub_file_read (files
[i
], ptr
, cursize
) != cursize
)
622 grub_error (GRUB_ERR_FILE_READ_ERROR
, N_("premature end of file %s"),
627 grub_memset (ptr
, 0, ALIGN_UP_OVERHEAD (cursize
, 4));
628 ptr
+= ALIGN_UP_OVERHEAD (cursize
, 4);
631 for (i
= 0; i
< nfiles
; i
++)
632 grub_file_close (files
[i
]);
638 grub_cmd_fpswa (grub_command_t cmd
__attribute__ ((unused
)),
639 int argc
__attribute__((unused
)),
640 char *argv
[] __attribute__((unused
)))
644 grub_puts_ (N_("No FPSWA found"));
646 grub_printf (_("FPSWA revision: %x\n"), fpswa
->revision
);
647 return GRUB_ERR_NONE
;
650 static grub_command_t cmd_linux
, cmd_initrd
, cmd_fpswa
;
654 cmd_linux
= grub_register_command ("linux", grub_cmd_linux
,
655 N_("FILE [ARGS...]"), N_("Load Linux."));
657 cmd_initrd
= grub_register_command ("initrd", grub_cmd_initrd
,
658 N_("FILE"), N_("Load initrd."));
660 cmd_fpswa
= grub_register_command ("fpswa", grub_cmd_fpswa
,
661 "", N_("Display FPSWA version."));
668 grub_unregister_command (cmd_linux
);
669 grub_unregister_command (cmd_initrd
);
670 grub_unregister_command (cmd_fpswa
);