Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / loader / ia64 / efi / linux.c
blob17843fdbf912b8d627310dccf97c5211521b95f7
1 /*
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>
22 #include <grub/err.h>
23 #include <grub/misc.h>
24 #include <grub/types.h>
25 #include <grub/command.h>
26 #include <grub/dl.h>
27 #include <grub/mm.h>
28 #include <grub/cache.h>
29 #include <grub/kernel.h>
30 #include <grub/efi/api.h>
31 #include <grub/efi/efi.h>
32 #include <grub/elf.h>
33 #include <grub/i18n.h>
34 #include <grub/env.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 */
54 struct
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 */
60 } console_info;
61 grub_uint64_t fpswa; /* physical address of the fpswa interface */
62 grub_uint64_t initrd_start;
63 grub_uint64_t initrd_size;
66 typedef struct
68 grub_uint32_t revision;
69 grub_uint32_t reserved;
70 void *fpswa;
71 } fpswa_interface_t;
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;
79 static int loaded;
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));
100 static void
101 query_fpswa (void)
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} };
111 if (fpswa != NULL)
112 return;
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"));
123 return;
125 status = bs->handle_protocol (fpswa_image,
126 (void *) &fpswa_protocol, (void *) &fpswa);
127 if (status != GRUB_EFI_SUCCESS)
129 grub_printf ("%s\n",
130 _("FPSWA protocol wasn't able to find the interface"));
131 return;
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;
142 if (mmap_size != 0)
143 return mmap_size;
145 mmap_size = (1 << 12);
146 while (1)
148 int ret;
149 grub_efi_memory_descriptor_t *mmap;
150 grub_efi_uintn_t desc_size;
152 mmap = grub_malloc (mmap_size);
153 if (! mmap)
154 return 0;
156 ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0);
157 grub_free (mmap);
159 if (ret < 0)
161 grub_error (GRUB_ERR_IO, "cannot get memory map");
162 return 0;
164 else if (ret > 0)
165 break;
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);
177 static void
178 free_pages (void)
180 if (kernel_mem)
182 grub_efi_free_pages ((grub_addr_t) kernel_mem, kernel_pages);
183 kernel_mem = 0;
186 if (initrd_mem)
188 grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
189 initrd_mem = 0;
192 if (boot_param)
194 /* Free bootparam. */
195 grub_efi_free_pages ((grub_efi_physical_address_t) boot_param,
196 boot_param_pages);
197 boot_param = 0;
201 static void *
202 allocate_pages (grub_uint64_t align, grub_uint64_t size_pages,
203 grub_uint64_t nobase)
205 grub_uint64_t size;
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;
210 void *mem = NULL;
212 size = size_pages << 12;
214 mmap_size = find_mmap_size ();
215 if (!mmap_size)
216 return 0;
218 /* Read the memory map temporarily, to find free space. */
219 mmap = grub_malloc (mmap_size);
220 if (! mmap)
221 return 0;
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");
227 goto fail;
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. */
234 for (desc = mmap;
235 desc < mmap_end;
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)
242 continue;
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)
249 continue;
250 if (aligned_start == nobase)
251 aligned_start += align;
252 if (aligned_start + size > end)
253 continue;
254 mem = grub_efi_allocate_pages (aligned_start, size_pages);
255 if (! mem)
257 grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");
258 goto fail;
260 break;
263 if (! mem)
265 grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");
266 goto fail;
269 grub_free (mmap);
270 return mem;
272 fail:
273 grub_free (mmap);
274 free_pages ();
275 return 0;
278 static void
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)
286 != GRUB_EFI_SUCCESS)
287 return;
289 grub_dprintf ("linux",
290 "Console info: cols=%lu rows=%lu x=%u y=%u\n",
291 cols, rows,
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;
300 static grub_err_t
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;
308 grub_err_t err;
310 /* FPSWA. */
311 query_fpswa ();
312 boot_param->fpswa = (grub_uint64_t)fpswa;
314 /* Initrd. */
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);
322 /* MDT.
323 Must be done after grub_machine_fini because map_key is used by
324 exit_boot_services. */
325 mmap_size = find_mmap_size ();
326 if (! mmap_size)
327 return grub_errno;
328 mmap_buf = grub_efi_allocate_pages (0, page_align (mmap_size) >> 12);
329 if (! mmap_buf)
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);
333 if (err)
334 return err;
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;
348 static grub_err_t
349 grub_linux_unload (void)
351 free_pages ();
352 grub_dl_unref (my_mod);
353 loaded = 0;
354 return GRUB_ERR_NONE;
357 static grub_err_t
358 grub_load_elf64 (grub_file_t file, void *buffer, const char *filename)
360 Elf64_Ehdr *ehdr = (Elf64_Ehdr *) buffer;
361 Elf64_Phdr *phdr;
362 int i;
363 grub_uint64_t low_addr;
364 grub_uint64_t high_addr;
365 grub_uint64_t align;
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. */
394 low_addr = ~0UL;
395 high_addr = 0;
396 align = 0;
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)
413 align = ALIGN_MIN;
415 if (high_addr == 0)
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);
425 reloc_offset = 0;
427 /* Try to relocate. */
428 if (! kernel_mem && (!relocate || grub_strcmp (relocate, "off") != 0))
430 kernel_mem = allocate_pages (align, kernel_pages, low_addr);
431 if (kernel_mem)
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;
439 if (! kernel_mem)
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)
456 return grub_errno;
458 if (grub_file_read (file, (void *) (phdr->p_paddr + reloc_offset),
459 phdr->p_filesz)
460 != (grub_ssize_t) phdr->p_filesz)
462 if (!grub_errno)
463 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
464 filename);
465 return grub_errno;
468 if (phdr->p_filesz < phdr->p_memsz)
469 grub_memset
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);
479 loaded = 1;
480 return 0;
483 static grub_err_t
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];
489 char *cmdline, *p;
490 grub_ssize_t len;
491 int i;
493 grub_dl_ref (my_mod);
495 grub_loader_unset ();
497 if (argc == 0)
499 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
500 goto fail;
503 file = grub_file_open (argv[0]);
504 if (! file)
505 goto fail;
507 len = grub_file_read (file, buffer, sizeof (buffer));
508 if (len < (grub_ssize_t) sizeof (Elf64_Ehdr))
510 if (!grub_errno)
511 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
512 argv[0]);
513 goto fail;
516 grub_dprintf ("linux", "Loading linux: %s\n", argv[0]);
518 if (grub_load_elf64 (file, buffer, argv[0]))
519 goto fail;
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);
527 if (boot_param == 0)
529 grub_error (GRUB_ERR_OUT_OF_MEMORY,
530 "cannot allocate memory for bootparams");
531 goto fail;
534 grub_memset (boot_param, 0, len);
535 cmdline = ((char *)(boot_param + 1)) + 256;
537 /* Build cmdline. */
538 p = grub_stpcpy (cmdline, "BOOT_IMAGE");
539 for (i = 0; i < argc; i++)
541 *p++ = ' ';
542 p = grub_stpcpy (p, argv[i]);
544 cmdline[10] = '=';
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);
553 fail:
554 if (file)
555 grub_file_close (file);
557 if (grub_errno != GRUB_ERR_NONE)
559 grub_efi_free_pages ((grub_efi_physical_address_t) boot_param,
560 boot_param_pages);
561 grub_dl_unref (my_mod);
563 return grub_errno;
566 static grub_err_t
567 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
568 int argc, char *argv[])
570 grub_file_t *files = 0;
571 int i;
572 int nfiles = 0;
573 grub_uint8_t *ptr;
575 if (argc == 0)
577 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
578 goto fail;
581 if (! loaded)
583 grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
584 goto fail;
587 files = grub_zalloc (argc * sizeof (files[0]));
588 if (!files)
589 goto fail;
591 initrd_size = 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]);
597 if (! files[i])
598 goto fail;
599 nfiles++;
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);
606 if (! initrd_mem)
608 grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate pages");
609 goto fail;
612 grub_dprintf ("linux", "[addr=0x%lx, size=0x%lx]\n",
613 (grub_uint64_t) initrd_mem, initrd_size);
615 ptr = initrd_mem;
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)
621 if (!grub_errno)
622 grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
623 argv[i]);
624 goto fail;
626 ptr += cursize;
627 grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
628 ptr += ALIGN_UP_OVERHEAD (cursize, 4);
630 fail:
631 for (i = 0; i < nfiles; i++)
632 grub_file_close (files[i]);
633 grub_free (files);
634 return grub_errno;
637 static grub_err_t
638 grub_cmd_fpswa (grub_command_t cmd __attribute__ ((unused)),
639 int argc __attribute__((unused)),
640 char *argv[] __attribute__((unused)))
642 query_fpswa ();
643 if (fpswa == NULL)
644 grub_puts_ (N_("No FPSWA found"));
645 else
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;
652 GRUB_MOD_INIT(linux)
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."));
663 my_mod = mod;
666 GRUB_MOD_FINI(linux)
668 grub_unregister_command (cmd_linux);
669 grub_unregister_command (cmd_initrd);
670 grub_unregister_command (cmd_fpswa);