efi/main.c: don't close handle early
[syslinux.git] / efi / main.c
blobadfa98b0e6ac734972fa74700202e514bb94cbb3
1 /*
2 * Copyright 2011-2014 Intel Corporation - All Rights Reserved
3 */
5 #include <codepage.h>
6 #include <core.h>
7 #include <fs.h>
8 #include <com32.h>
9 #include <syslinux/memscan.h>
10 #include <syslinux/firmware.h>
11 #include <syslinux/linux.h>
12 #include <sys/ansi.h>
13 #include <setjmp.h>
15 #include "efi.h"
16 #include "fio.h"
17 #include "version.h"
19 __export uint16_t PXERetry;
20 __export char copyright_str[] = "Copyright (C) 2011-" YEAR_STR "\n";
21 uint8_t SerialNotice = 1;
22 __export char syslinux_banner[] = "Syslinux " VERSION_STR " (EFI; " DATE_STR ")\n";
23 char CurrentDirName[CURRENTDIR_MAX];
24 struct com32_sys_args __com32;
26 uint32_t _IdleTimer = 0;
27 char __lowmem_heap[32];
28 uint32_t BIOS_timer_next;
29 uint32_t timer_irq;
30 __export uint8_t KbdMap[256];
31 char aux_seg[256];
33 static jmp_buf load_error_buf;
35 static inline EFI_STATUS
36 efi_close_protocol(EFI_HANDLE handle, EFI_GUID *guid, EFI_HANDLE agent,
37 EFI_HANDLE controller)
39 return uefi_call_wrapper(BS->CloseProtocol, 4, handle,
40 guid, agent, controller);
43 /* As of UEFI-2.4.0, all EFI_SERVICE_BINDINGs are for networking */
44 struct efi_binding *efi_create_binding(EFI_GUID *bguid, EFI_GUID *pguid)
46 EFI_SERVICE_BINDING *sbp;
47 struct efi_binding *b;
48 EFI_STATUS status;
49 EFI_HANDLE protocol, child;
51 b = malloc(sizeof(*b));
52 if (!b)
53 return NULL;
55 status = uefi_call_wrapper(BS->OpenProtocol, 6, pxe_handle,
56 bguid, (void **)&sbp,
57 image_handle, pxe_handle,
58 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
59 if (status != EFI_SUCCESS)
60 goto free_binding;
62 child = NULL;
64 status = uefi_call_wrapper(sbp->CreateChild, 2, sbp, (EFI_HANDLE *)&child);
65 if (status != EFI_SUCCESS)
66 goto close_protocol;
68 status = uefi_call_wrapper(BS->OpenProtocol, 6, child,
69 pguid, (void **)&protocol,
70 image_handle, sbp,
71 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
72 if (status != EFI_SUCCESS)
73 goto destroy_child;
75 b->parent = pxe_handle;
76 b->binding = sbp;
77 b->child = child;
78 b->this = protocol;
80 return b;
82 destroy_child:
83 uefi_call_wrapper(sbp->DestroyChild, 2, sbp, child);
85 close_protocol:
86 uefi_call_wrapper(BS->CloseProtocol, 4, pxe_handle, bguid,
87 image_handle, pxe_handle);
89 free_binding:
90 free(b);
91 return NULL;
94 void efi_destroy_binding(struct efi_binding *b, EFI_GUID *guid)
96 efi_close_protocol(b->child, guid, image_handle, b->binding);
97 uefi_call_wrapper(b->binding->DestroyChild, 2, b->binding, b->child);
98 efi_close_protocol(b->parent, guid, image_handle, b->parent);
100 free(b);
103 #undef kaboom
104 void kaboom(void)
108 void printf_init(void)
112 __export void local_boot(uint16_t ax)
115 * Inform the firmware that we failed to execute correctly, which
116 * will trigger the next entry in the EFI Boot Manager list.
118 longjmp(load_error_buf, 1);
121 void bios_timer_cleanup(void)
125 char trackbuf[4096];
127 void __cdecl core_farcall(uint32_t c, const com32sys_t *a, com32sys_t *b)
131 __export struct firmware *firmware = NULL;
132 __export void *__syslinux_adv_ptr;
133 __export size_t __syslinux_adv_size;
134 char core_xfer_buf[65536];
135 struct iso_boot_info {
136 uint32_t pvd; /* LBA of primary volume descriptor */
137 uint32_t file; /* LBA of boot file */
138 uint32_t length; /* Length of boot file */
139 uint32_t csum; /* Checksum of boot file */
140 uint32_t reserved[10]; /* Currently unused */
141 } iso_boot_info;
143 uint8_t DHCPMagic;
144 uint32_t RebootTime;
146 void pxenv(void)
150 uint16_t BIOS_fbm = 1;
151 far_ptr_t InitStack;
152 far_ptr_t PXEEntry;
154 void gpxe_unload(void)
158 void do_idle(void)
162 void pxe_int1a(void)
166 uint8_t KeepPXE;
168 struct semaphore;
169 mstime_t sem_down(struct semaphore *sem, mstime_t time)
171 /* EFI is single threaded */
172 return 0;
175 void sem_up(struct semaphore *sem)
177 /* EFI is single threaded */
180 __export volatile uint32_t __ms_timer = 0;
181 volatile uint32_t __jiffies = 0;
183 void efi_write_char(uint8_t ch, uint8_t attribute)
185 SIMPLE_TEXT_OUTPUT_INTERFACE *out = ST->ConOut;
186 uint16_t c[2];
188 uefi_call_wrapper(out->SetAttribute, 2, out, attribute);
190 /* Lookup primary Unicode encoding in the system codepage */
191 c[0] = codepage.uni[0][ch];
192 c[1] = '\0';
194 uefi_call_wrapper(out->OutputString, 2, out, c);
197 static void efi_showcursor(const struct term_state *st)
199 SIMPLE_TEXT_OUTPUT_INTERFACE *out = ST->ConOut;
200 bool cursor = st->cursor ? true : false;
202 uefi_call_wrapper(out->EnableCursor, 2, out, cursor);
205 static void efi_set_cursor(int x, int y, bool visible)
207 SIMPLE_TEXT_OUTPUT_INTERFACE *out = ST->ConOut;
209 uefi_call_wrapper(out->SetCursorPosition, 3, out, x, y);
212 static void efi_scroll_up(uint8_t cols, uint8_t rows, uint8_t attribute)
214 efi_write_char('\n', 0);
215 efi_write_char('\r', 0);
218 static void efi_get_mode(int *cols, int *rows)
220 SIMPLE_TEXT_OUTPUT_INTERFACE *out = ST->ConOut;
221 UINTN c, r;
223 uefi_call_wrapper(out->QueryMode, 4, out, out->Mode->Mode, &c, &r);
224 *rows = r;
225 *cols = c;
228 static void efi_erase(int x0, int y0, int x1, int y1, uint8_t attribute)
230 SIMPLE_TEXT_OUTPUT_INTERFACE *out = ST->ConOut;
231 int cols, rows;
233 efi_get_mode(&cols, &rows);
236 * The BIOS version of this function has the ability to erase
237 * parts or all of the screen - the UEFI console doesn't
238 * support this so we just set the cursor position unless
239 * we're clearing the whole screen.
241 if (!x0 && y0 == (cols - 1)) {
242 /* Really clear the screen */
243 uefi_call_wrapper(out->ClearScreen, 1, out);
244 } else {
245 uefi_call_wrapper(out->SetCursorPosition, 3, out, y1, x1);
249 static void efi_text_mode(void)
253 static void efi_get_cursor(uint8_t *x, uint8_t *y)
255 SIMPLE_TEXT_OUTPUT_INTERFACE *out = ST->ConOut;
256 *x = out->Mode->CursorColumn;
257 *y = out->Mode->CursorRow;
260 struct output_ops efi_ops = {
261 .erase = efi_erase,
262 .write_char = efi_write_char,
263 .showcursor = efi_showcursor,
264 .set_cursor = efi_set_cursor,
265 .scroll_up = efi_scroll_up,
266 .get_mode = efi_get_mode,
267 .text_mode = efi_text_mode,
268 .get_cursor = efi_get_cursor,
271 char SubvolName[2];
272 static inline EFI_MEMORY_DESCRIPTOR *
273 get_memory_map(UINTN *nr_entries, UINTN *key, UINTN *desc_sz,
274 uint32_t *desc_ver)
276 return LibMemoryMap(nr_entries, key, desc_sz, desc_ver);
280 int efi_scan_memory(scan_memory_callback_t callback, void *data)
282 UINTN i, nr_entries, key, desc_sz;
283 UINTN buf, bufpos;
284 UINT32 desc_ver;
285 int rv = 0;
287 buf = (UINTN)get_memory_map(&nr_entries, &key, &desc_sz, &desc_ver);
288 if (!buf)
289 return -1;
290 bufpos = buf;
292 for (i = 0; i < nr_entries; bufpos += desc_sz, i++) {
293 EFI_MEMORY_DESCRIPTOR *m;
294 UINT64 region_sz;
295 enum syslinux_memmap_types type;
297 m = (EFI_MEMORY_DESCRIPTOR *)bufpos;
298 region_sz = m->NumberOfPages * EFI_PAGE_SIZE;
300 switch (m->Type) {
301 case EfiConventionalMemory:
302 type = SMT_FREE;
303 break;
304 default:
305 type = SMT_RESERVED;
306 break;
309 rv = callback(data, m->PhysicalStart, region_sz, type);
310 if (rv)
311 break;
314 FreePool((void *)buf);
315 return rv;
318 static struct syslinux_memscan efi_memscan = {
319 .func = efi_scan_memory,
322 extern uint16_t *bios_free_mem;
323 void efi_init(void)
325 /* XXX timer */
326 *bios_free_mem = 0;
327 syslinux_memscan_add(&efi_memscan);
328 mem_init();
331 char efi_getchar(char *hi)
333 SIMPLE_INPUT_INTERFACE *in = ST->ConIn;
334 EFI_INPUT_KEY key;
335 EFI_STATUS status;
337 do {
338 status = uefi_call_wrapper(in->ReadKeyStroke, 2, in, &key);
339 } while (status == EFI_NOT_READY);
341 if (!key.ScanCode)
342 return (char)key.UnicodeChar;
345 * We currently only handle scan codes that fit in 8 bits.
347 *hi = (char)key.ScanCode;
348 return 0;
351 int efi_pollchar(void)
353 SIMPLE_INPUT_INTERFACE *in = ST->ConIn;
354 EFI_STATUS status;
356 status = WaitForSingleEvent(in->WaitForKey, 1);
357 return status != EFI_TIMEOUT;
360 struct input_ops efi_iops = {
361 .getchar = efi_getchar,
362 .pollchar = efi_pollchar,
365 extern void efi_adv_init(void);
366 extern int efi_adv_write(void);
368 struct adv_ops efi_adv_ops = {
369 .init = efi_adv_init,
370 .write = efi_adv_write,
373 struct efi_info {
374 uint32_t load_signature;
375 uint32_t systab;
376 uint32_t desc_size;
377 uint32_t desc_version;
378 uint32_t memmap;
379 uint32_t memmap_size;
380 uint32_t systab_hi;
381 uint32_t memmap_hi;
384 #define E820MAX 128
385 #define E820_RAM 1
386 #define E820_RESERVED 2
387 #define E820_ACPI 3
388 #define E820_NVS 4
389 #define E820_UNUSABLE 5
391 #define BOOT_SIGNATURE 0xaa55
392 #define SYSLINUX_EFILDR 0x30 /* Is this published value? */
393 #define DEFAULT_TIMER_TICK_DURATION 500000 /* 500000 == 500000 * 100 * 10^-9 == 50 msec */
394 #define DEFAULT_MSTIMER_INC 0x32 /* 50 msec */
395 struct e820_entry {
396 uint64_t start;
397 uint64_t len;
398 uint32_t type;
399 } __packed;
401 struct boot_params {
402 struct screen_info screen_info;
403 uint8_t _pad[0x1c0 - sizeof(struct screen_info)];
404 struct efi_info efi;
405 uint8_t _pad2[8];
406 uint8_t e820_entries;
407 uint8_t _pad3[0x2d0 - 0x1e8 - sizeof(uint8_t)];
408 struct e820_entry e820_map[E820MAX];
409 } __packed;
411 /* Allocate boot parameter block aligned to page */
412 #define BOOT_PARAM_BLKSIZE EFI_SIZE_TO_PAGES(sizeof(struct boot_params)) * EFI_PAGE_SIZE
414 /* Routines in support of efi boot loader were obtained from
415 * http://git.kernel.org/?p=boot/efilinux/efilinux.git:
416 * kernel_jump(), handover_jump(),
417 * emalloc()/efree, alloc_pages/free_pages
418 * allocate_pool()/free_pool()
419 * memory_map()
421 extern void kernel_jump(EFI_PHYSICAL_ADDRESS kernel_start,
422 struct boot_params *boot_params);
423 #if __SIZEOF_POINTER__ == 4
424 #define EFI_LOAD_SIG "EL32"
425 #elif __SIZEOF_POINTER__ == 8
426 #define EFI_LOAD_SIG "EL64"
427 #else
428 #error "unsupported architecture"
429 #endif
431 struct dt_desc {
432 uint16_t limit;
433 uint64_t *base;
434 } __packed;
436 struct dt_desc gdt = { 0x800, (uint64_t *)0 };
437 struct dt_desc idt = { 0, 0 };
439 static inline EFI_MEMORY_DESCRIPTOR *
440 get_mem_desc(unsigned long memmap, UINTN desc_sz, int i)
442 return (EFI_MEMORY_DESCRIPTOR *)(memmap + (i * desc_sz));
445 EFI_HANDLE image_handle, pxe_handle;
447 static inline UINT64 round_up(UINT64 x, UINT64 y)
449 return (((x - 1) | (y - 1)) + 1);
452 static inline UINT64 round_down(UINT64 x, UINT64 y)
454 return (x & ~(y - 1));
457 static void find_addr(EFI_PHYSICAL_ADDRESS *first,
458 EFI_PHYSICAL_ADDRESS *last,
459 EFI_PHYSICAL_ADDRESS min,
460 EFI_PHYSICAL_ADDRESS max,
461 size_t size, size_t align)
463 EFI_MEMORY_DESCRIPTOR *map;
464 UINT32 desc_ver;
465 UINTN i, nr_entries, key, desc_sz;
467 map = get_memory_map(&nr_entries, &key, &desc_sz, &desc_ver);
468 if (!map)
469 return;
471 for (i = 0; i < nr_entries; i++) {
472 EFI_MEMORY_DESCRIPTOR *m;
473 EFI_PHYSICAL_ADDRESS best;
474 UINT64 start, end;
476 m = get_mem_desc((unsigned long)map, desc_sz, i);
477 if (m->Type != EfiConventionalMemory)
478 continue;
480 if (m->NumberOfPages < EFI_SIZE_TO_PAGES(size))
481 continue;
483 start = m->PhysicalStart;
484 end = m->PhysicalStart + (m->NumberOfPages << EFI_PAGE_SHIFT);
485 if (first) {
486 if (end < min)
487 continue;
489 /* What's the best address? */
490 if (start < min && min < end)
491 best = min;
492 else
493 best = m->PhysicalStart;
495 start = round_up(best, align);
496 if (start > max)
497 continue;
499 /* Have we run out of space in this region? */
500 if (end < start || (start + size) > end)
501 continue;
503 if (start < *first)
504 *first = start;
507 if (last) {
508 if (start > max)
509 continue;
511 /* What's the best address? */
512 if (start < max && max < end)
513 best = max - size;
514 else
515 best = end - size;
517 start = round_down(best, align);
518 if (start < min || start < m->PhysicalStart)
519 continue;
521 if (start > *last)
522 *last = start;
526 FreePool(map);
530 * allocate_pages - Allocate memory pages from the system
531 * @atype: type of allocation to perform
532 * @mtype: type of memory to allocate
533 * @num_pages: number of contiguous 4KB pages to allocate
534 * @memory: used to return the address of allocated pages
536 * Allocate @num_pages physically contiguous pages from the system
537 * memory and return a pointer to the base of the allocation in
538 * @memory if the allocation succeeds. On success, the firmware memory
539 * map is updated accordingly.
541 * If @atype is AllocateAddress then, on input, @memory specifies the
542 * address at which to attempt to allocate the memory pages.
544 static inline EFI_STATUS
545 allocate_pages(EFI_ALLOCATE_TYPE atype, EFI_MEMORY_TYPE mtype,
546 UINTN num_pages, EFI_PHYSICAL_ADDRESS *memory)
548 return uefi_call_wrapper(BS->AllocatePages, 4, atype,
549 mtype, num_pages, memory);
552 * free_pages - Return memory allocated by allocate_pages() to the firmware
553 * @memory: physical base address of the page range to be freed
554 * @num_pages: number of contiguous 4KB pages to free
556 * On success, the firmware memory map is updated accordingly.
558 static inline EFI_STATUS
559 free_pages(EFI_PHYSICAL_ADDRESS memory, UINTN num_pages)
561 return uefi_call_wrapper(BS->FreePages, 2, memory, num_pages);
564 static EFI_STATUS allocate_addr(EFI_PHYSICAL_ADDRESS *addr, size_t size)
566 UINTN npages = EFI_SIZE_TO_PAGES(size);
568 return uefi_call_wrapper(BS->AllocatePages, 4,
569 AllocateAddress,
570 EfiLoaderData, npages,
571 addr);
574 * allocate_pool - Allocate pool memory
575 * @type: the type of pool to allocate
576 * @size: number of bytes to allocate from pool of @type
577 * @buffer: used to return the address of allocated memory
579 * Allocate memory from pool of @type. If the pool needs more memory
580 * pages are allocated from EfiConventionalMemory in order to grow the
581 * pool.
583 * All allocations are eight-byte aligned.
585 static inline EFI_STATUS
586 allocate_pool(EFI_MEMORY_TYPE type, UINTN size, void **buffer)
588 return uefi_call_wrapper(BS->AllocatePool, 3, type, size, buffer);
592 * free_pool - Return pool memory to the system
593 * @buffer: the buffer to free
595 * Return @buffer to the system. The returned memory is marked as
596 * EfiConventionalMemory.
598 static inline EFI_STATUS free_pool(void *buffer)
600 return uefi_call_wrapper(BS->FreePool, 1, buffer);
603 static void free_addr(EFI_PHYSICAL_ADDRESS addr, size_t size)
605 UINTN npages = EFI_SIZE_TO_PAGES(size);
607 uefi_call_wrapper(BS->FreePages, 2, addr, npages);
610 /* cancel the established timer */
611 static EFI_STATUS cancel_timer(EFI_EVENT ev)
613 return uefi_call_wrapper(BS->SetTimer, 3, ev, TimerCancel, 0);
616 /* Check if timer went off and update default timer counter */
617 void timer_handler(EFI_EVENT ev, VOID *ctx)
619 __ms_timer += DEFAULT_MSTIMER_INC;
620 ++__jiffies;
623 /* Setup a default periodic timer */
624 static EFI_STATUS setup_default_timer(EFI_EVENT *ev)
626 EFI_STATUS efi_status;
628 *ev = NULL;
629 efi_status = uefi_call_wrapper( BS->CreateEvent, 5, EVT_TIMER|EVT_NOTIFY_SIGNAL, TPL_NOTIFY, (EFI_EVENT_NOTIFY)timer_handler, NULL, ev);
630 if (efi_status == EFI_SUCCESS) {
631 efi_status = uefi_call_wrapper(BS->SetTimer, 3, *ev, TimerPeriodic, DEFAULT_TIMER_TICK_DURATION);
633 return efi_status;
637 * emalloc - Allocate memory with a strict alignment requirement
638 * @size: size in bytes of the requested allocation
639 * @align: the required alignment of the allocation
640 * @addr: a pointer to the allocated address on success
642 * If we cannot satisfy @align we return 0.
644 EFI_STATUS emalloc(UINTN size, UINTN align, EFI_PHYSICAL_ADDRESS *addr)
646 UINTN i, nr_entries, map_key, desc_size;
647 EFI_MEMORY_DESCRIPTOR *map_buf;
648 UINTN d;
649 UINT32 desc_version;
650 EFI_STATUS err;
651 UINTN nr_pages = EFI_SIZE_TO_PAGES(size);
653 map_buf = get_memory_map(&nr_entries, &map_key,
654 &desc_size, &desc_version);
655 if (!map_buf)
656 goto fail;
658 d = (UINTN)map_buf;
660 for (i = 0; i < nr_entries; i++, d += desc_size) {
661 EFI_MEMORY_DESCRIPTOR *desc;
662 EFI_PHYSICAL_ADDRESS start, end, aligned;
664 desc = (EFI_MEMORY_DESCRIPTOR *)d;
665 if (desc->Type != EfiConventionalMemory)
666 continue;
668 if (desc->NumberOfPages < nr_pages)
669 continue;
671 start = desc->PhysicalStart;
672 end = start + (desc->NumberOfPages << EFI_PAGE_SHIFT);
674 /* Low-memory is super-precious! */
675 if (end <= 1 << 20)
676 continue;
677 if (start < 1 << 20)
678 start = (1 << 20);
680 aligned = (start + align -1) & ~(align -1);
682 if ((aligned + size) <= end) {
683 err = allocate_pages(AllocateAddress, EfiLoaderData,
684 nr_pages, &aligned);
685 if (err == EFI_SUCCESS) {
686 *addr = aligned;
687 break;
692 if (i == nr_entries)
693 err = EFI_OUT_OF_RESOURCES;
695 free_pool(map_buf);
696 fail:
697 return err;
700 * efree - Return memory allocated with emalloc
701 * @memory: the address of the emalloc() allocation
702 * @size: the size of the allocation
704 void efree(EFI_PHYSICAL_ADDRESS memory, UINTN size)
706 UINTN nr_pages = EFI_SIZE_TO_PAGES(size);
708 free_pages(memory, nr_pages);
712 * Check whether 'buf' contains a PE/COFF header and that the PE/COFF
713 * file can be executed by this architecture.
715 static bool valid_pecoff_image(char *buf)
717 struct pe_header {
718 uint16_t signature;
719 uint8_t _pad[0x3a];
720 uint32_t offset;
721 } *pehdr = (struct pe_header *)buf;
722 struct coff_header {
723 uint32_t signature;
724 uint16_t machine;
725 } *chdr;
727 if (pehdr->signature != 0x5a4d) {
728 dprintf("Invalid MS-DOS header signature\n");
729 return false;
732 if (!pehdr->offset || pehdr->offset > 512) {
733 dprintf("Invalid PE header offset\n");
734 return false;
737 chdr = (struct coff_header *)&buf[pehdr->offset];
738 if (chdr->signature != 0x4550) {
739 dprintf("Invalid PE header signature\n");
740 return false;
743 #if defined(__x86_64__)
744 if (chdr->machine != 0x8664) {
745 dprintf("Invalid PE machine field\n");
746 return false;
748 #else
749 if (chdr->machine != 0x14c) {
750 dprintf("Invalid PE machine field\n");
751 return false;
753 #endif
755 return true;
759 * Boot a Linux kernel using the EFI boot stub handover protocol.
761 * This function will not return to its caller if booting the kernel
762 * image succeeds. If booting the kernel image fails, a legacy boot
763 * method should be attempted.
765 static void handover_boot(struct linux_header *hdr, struct boot_params *bp)
767 unsigned long address = hdr->code32_start + hdr->handover_offset;
768 handover_func_t *func = efi_handover;
770 dprintf("Booting kernel using handover protocol\n");
773 * Ensure that the kernel is a valid PE32(+) file and that the
774 * architecture of the file matches this version of Syslinux - we
775 * can't mix firmware and kernel bitness (e.g. 32-bit kernel on
776 * 64-bit EFI firmware) using the handover protocol.
778 if (!valid_pecoff_image((char *)hdr))
779 return;
781 if (hdr->version >= 0x20c) {
782 if (hdr->xloadflags & XLF_EFI_HANDOVER_32)
783 func = efi_handover_32;
785 if (hdr->xloadflags & XLF_EFI_HANDOVER_64)
786 func = efi_handover_64;
789 efi_console_restore();
790 func(image_handle, ST, bp, address);
793 static int check_linux_header(struct linux_header *hdr)
795 if (hdr->version < 0x205)
796 hdr->relocatable_kernel = 0;
798 /* FIXME: check boot sector signature */
799 if (hdr->boot_flag != BOOT_SIGNATURE) {
800 printf("Invalid Boot signature 0x%x, bailing out\n", hdr->boot_flag);
801 return -1;
804 return 0;
807 static char *build_cmdline(char *str)
809 EFI_PHYSICAL_ADDRESS addr;
810 EFI_STATUS status;
811 char *cmdline = NULL; /* internal, in efi_physical below 0x3FFFFFFF */
814 * The kernel expects cmdline to be allocated pretty low,
815 * Documentation/x86/boot.txt says,
817 * "The kernel command line can be located anywhere
818 * between the end of the setup heap and 0xA0000"
820 addr = 0xA0000;
821 status = allocate_pages(AllocateMaxAddress, EfiLoaderData,
822 EFI_SIZE_TO_PAGES(strlen(str) + 1),
823 &addr);
824 if (status != EFI_SUCCESS) {
825 printf("Failed to allocate memory for kernel command line, bailing out\n");
826 return NULL;
828 cmdline = (char *)(UINTN)addr;
829 memcpy(cmdline, str, strlen(str) + 1);
830 return cmdline;
833 static int build_gdt(void)
835 EFI_STATUS status;
837 /* Allocate gdt consistent with the alignment for architecture */
838 status = emalloc(gdt.limit, __SIZEOF_POINTER__ , (EFI_PHYSICAL_ADDRESS *)&gdt.base);
839 if (status != EFI_SUCCESS) {
840 printf("Failed to allocate memory for GDT, bailing out\n");
841 return -1;
843 memset(gdt.base, 0x0, gdt.limit);
846 * 4Gb - (0x100000*0x1000 = 4Gb)
847 * base address=0
848 * code read/exec
849 * granularity=4096, 386 (+5th nibble of limit)
851 gdt.base[2] = 0x00cf9a000000ffff;
854 * 4Gb - (0x100000*0x1000 = 4Gb)
855 * base address=0
856 * data read/write
857 * granularity=4096, 386 (+5th nibble of limit)
859 gdt.base[3] = 0x00cf92000000ffff;
861 /* Task segment value */
862 gdt.base[4] = 0x0080890000000000;
864 return 0;
868 * Callers use ->ramdisk_size to check whether any memory was
869 * allocated (and therefore needs free'ing). The return value indicates
870 * hard error conditions, such as failing to alloc memory for the
871 * ramdisk image. Having no initramfs is not an error.
873 static int handle_ramdisks(struct linux_header *hdr,
874 struct initramfs *initramfs)
876 EFI_PHYSICAL_ADDRESS last;
877 struct initramfs *ip;
878 EFI_STATUS status;
879 addr_t irf_size;
880 addr_t next_addr, len, pad;
882 hdr->ramdisk_image = 0;
883 hdr->ramdisk_size = 0;
886 * Figure out the size of the initramfs, and where to put it.
887 * We should put it at the highest possible address which is
888 * <= hdr->initrd_addr_max, which fits the entire initramfs.
890 irf_size = initramfs_size(initramfs); /* Handles initramfs == NULL */
891 if (!irf_size)
892 return 0;
894 last = 0;
895 find_addr(NULL, &last, 0x1000, hdr->initrd_addr_max,
896 irf_size, INITRAMFS_MAX_ALIGN);
897 if (last)
898 status = allocate_addr(&last, irf_size);
900 if (!last || status != EFI_SUCCESS) {
901 printf("Failed to allocate initramfs memory, bailing out\n");
902 return -1;
905 hdr->ramdisk_image = (uint32_t)last;
906 hdr->ramdisk_size = irf_size;
908 /* Copy initramfs into allocated memory */
909 for (ip = initramfs->next; ip->len; ip = ip->next) {
910 len = ip->len;
911 next_addr = last + len;
914 * If this isn't the last entry, extend the
915 * zero-pad region to enforce the alignment of
916 * the next chunk.
918 if (ip->next->len) {
919 pad = -next_addr & (ip->next->align - 1);
920 len += pad;
921 next_addr += pad;
924 if (ip->data_len)
925 memcpy((void *)(UINTN)last, ip->data, ip->data_len);
927 if (len > ip->data_len)
928 memset((void *)(UINTN)(last + ip->data_len), 0,
929 len - ip->data_len);
931 last = next_addr;
933 return 0;
936 static int exit_boot(struct boot_params *bp)
938 struct e820_entry *e820buf, *e;
939 EFI_MEMORY_DESCRIPTOR *map;
940 EFI_STATUS status;
941 uint32_t e820_type;
942 UINTN i, nr_entries, key, desc_sz;
943 UINT32 desc_ver;
945 /* Build efi memory map */
946 map = get_memory_map(&nr_entries, &key, &desc_sz, &desc_ver);
947 if (!map)
948 return -1;
950 bp->efi.memmap = (uint32_t)(unsigned long)map;
951 bp->efi.memmap_size = nr_entries * desc_sz;
952 bp->efi.systab = (uint32_t)(unsigned long)ST;
953 bp->efi.desc_size = desc_sz;
954 bp->efi.desc_version = desc_ver;
955 #if defined(__x86_64__)
956 bp->efi.systab_hi = ((unsigned long)ST) >> 32;
957 bp->efi.memmap_hi = ((unsigned long)map) >> 32;
958 #endif
962 * Even though 'memmap' contains the memory map we provided
963 * previously in efi_scan_memory(), we should recalculate the
964 * e820 map because it will most likely have changed in the
965 * interim.
967 e = e820buf = bp->e820_map;
968 for (i = 0; i < nr_entries && i < E820MAX; i++) {
969 struct e820_entry *prev = NULL;
971 if (e > e820buf)
972 prev = e - 1;
974 map = get_mem_desc(bp->efi.memmap, desc_sz, i);
975 e->start = map->PhysicalStart;
976 e->len = map->NumberOfPages << EFI_PAGE_SHIFT;
978 switch (map->Type) {
979 case EfiReservedMemoryType:
980 case EfiRuntimeServicesCode:
981 case EfiRuntimeServicesData:
982 case EfiMemoryMappedIO:
983 case EfiMemoryMappedIOPortSpace:
984 case EfiPalCode:
985 e820_type = E820_RESERVED;
986 break;
988 case EfiUnusableMemory:
989 e820_type = E820_UNUSABLE;
990 break;
992 case EfiACPIReclaimMemory:
993 e820_type = E820_ACPI;
994 break;
996 case EfiLoaderCode:
997 case EfiLoaderData:
998 case EfiBootServicesCode:
999 case EfiBootServicesData:
1000 case EfiConventionalMemory:
1001 e820_type = E820_RAM;
1002 break;
1004 case EfiACPIMemoryNVS:
1005 e820_type = E820_NVS;
1006 break;
1007 default:
1008 continue;
1011 e->type = e820_type;
1013 /* Check for adjacent entries we can merge. */
1014 if (prev && (prev->start + prev->len) == e->start &&
1015 prev->type == e->type)
1016 prev->len += e->len;
1017 else
1018 e++;
1021 bp->e820_entries = e - e820buf;
1023 status = uefi_call_wrapper(BS->ExitBootServices, 2, image_handle, key);
1024 if (status != EFI_SUCCESS) {
1025 printf("Failed to exit boot services: 0x%016lx\n", status);
1026 FreePool(map);
1027 return -1;
1030 return 0;
1033 /* efi_boot_linux:
1034 * Boots the linux kernel using the image and parameters to boot with.
1035 * The EFI boot loader is reworked taking the cue from
1036 * http://git.kernel.org/?p=boot/efilinux/efilinux.git on the need to
1037 * cap key kernel data structures at * 0x3FFFFFFF.
1038 * The kernel image, kernel command line and boot parameter block are copied
1039 * into allocated memory areas that honor the address capping requirement
1040 * prior to kernel handoff.
1042 * FIXME
1043 * Can we move this allocation requirement to com32 linux loader in order
1044 * to avoid double copying kernel image?
1046 int efi_boot_linux(void *kernel_buf, size_t kernel_size,
1047 struct initramfs *initramfs,
1048 struct setup_data *setup_data,
1049 char *cmdline)
1051 struct linux_header *hdr;
1052 struct boot_params *bp;
1053 EFI_STATUS status;
1054 EFI_PHYSICAL_ADDRESS addr, pref_address, kernel_start = 0;
1055 UINT64 setup_sz, init_size = 0;
1056 char *_cmdline;
1058 if (check_linux_header(kernel_buf))
1059 goto bail;
1061 /* allocate for boot parameter block */
1062 addr = 0x3FFFFFFF;
1063 status = allocate_pages(AllocateMaxAddress, EfiLoaderData,
1064 BOOT_PARAM_BLKSIZE, &addr);
1065 if (status != EFI_SUCCESS) {
1066 printf("Failed to allocate memory for kernel boot parameter block, bailing out\n");
1067 goto bail;
1070 bp = (struct boot_params *)(UINTN)addr;
1072 memset((void *)bp, 0x0, BOOT_PARAM_BLKSIZE);
1073 /* Copy the first two sectors to boot_params */
1074 memcpy((char *)bp, kernel_buf, 2 * 512);
1075 hdr = (struct linux_header *)bp;
1077 setup_sz = (hdr->setup_sects + 1) * 512;
1078 if (hdr->version >= 0x20a) {
1079 pref_address = hdr->pref_address;
1080 init_size = hdr->init_size;
1081 } else {
1082 pref_address = 0x100000;
1085 * We need to account for the fact that the kernel
1086 * needs room for decompression, otherwise we could
1087 * end up trashing other chunks of allocated memory.
1089 init_size = (kernel_size - setup_sz) * 3;
1091 hdr->type_of_loader = SYSLINUX_EFILDR; /* SYSLINUX boot loader module */
1092 _cmdline = build_cmdline(cmdline);
1093 if (!_cmdline)
1094 goto bail;
1096 hdr->cmd_line_ptr = (UINT32)(UINTN)_cmdline;
1098 addr = pref_address;
1099 status = allocate_pages(AllocateAddress, EfiLoaderData,
1100 EFI_SIZE_TO_PAGES(init_size), &addr);
1101 if (status != EFI_SUCCESS) {
1103 * We failed to allocate the preferred address, so
1104 * just allocate some memory and hope for the best.
1106 if (!hdr->relocatable_kernel) {
1107 printf("Cannot relocate kernel, bailing out\n");
1108 goto bail;
1111 status = emalloc(init_size, hdr->kernel_alignment, &addr);
1112 if (status != EFI_SUCCESS) {
1113 printf("Failed to allocate memory for kernel image, bailing out\n");
1114 goto free_map;
1117 kernel_start = addr;
1118 /* FIXME: we copy the kernel into the physical memory allocated here
1119 * The syslinux kernel image load elsewhere could allocate the EFI memory from here
1120 * prior to copying kernel and save an extra copy
1122 memcpy((void *)(UINTN)kernel_start, kernel_buf+setup_sz, kernel_size-setup_sz);
1124 hdr->code32_start = (UINT32)((UINT64)kernel_start);
1126 dprintf("efi_boot_linux: kernel_start 0x%x kernel_size 0x%x initramfs 0x%x setup_data 0x%x cmdline 0x%x\n",
1127 kernel_start, kernel_size, initramfs, setup_data, _cmdline);
1129 if (handle_ramdisks(hdr, initramfs))
1130 goto free_map;
1132 /* Attempt to use the handover protocol if available */
1133 if (hdr->version >= 0x20b && hdr->handover_offset)
1134 handover_boot(hdr, bp);
1136 setup_screen(&bp->screen_info);
1138 if (build_gdt())
1139 goto free_map;
1141 dprintf("efi_boot_linux: setup_sects %d kernel_size %d\n", hdr->setup_sects, kernel_size);
1143 efi_console_restore();
1145 if (exit_boot(bp))
1146 goto free_map;
1148 memcpy(&bp->efi.load_signature, EFI_LOAD_SIG, sizeof(uint32_t));
1150 asm volatile ("lidt %0" :: "m" (idt));
1151 asm volatile ("lgdt %0" :: "m" (gdt));
1153 kernel_jump(kernel_start, bp);
1155 /* NOTREACHED */
1157 free_map:
1158 if (_cmdline)
1159 efree((EFI_PHYSICAL_ADDRESS)(unsigned long)_cmdline,
1160 strlen(_cmdline) + 1);
1162 if (bp)
1163 efree((EFI_PHYSICAL_ADDRESS)(unsigned long)bp,
1164 BOOT_PARAM_BLKSIZE);
1165 if (kernel_start) efree(kernel_start, init_size);
1166 if (hdr->ramdisk_size)
1167 free_addr(hdr->ramdisk_image, hdr->ramdisk_size);
1168 bail:
1169 return -1;
1172 extern struct disk *efi_disk_init(EFI_HANDLE);
1173 extern void serialcfg(uint16_t *, uint16_t *, uint16_t *);
1175 extern struct vesa_ops efi_vesa_ops;
1177 struct mem_ops efi_mem_ops = {
1178 .malloc = efi_malloc,
1179 .realloc = efi_realloc,
1180 .free = efi_free,
1183 struct firmware efi_fw = {
1184 .init = efi_init,
1185 .disk_init = efi_disk_init,
1186 .o_ops = &efi_ops,
1187 .i_ops = &efi_iops,
1188 .get_serial_console_info = serialcfg,
1189 .adv_ops = &efi_adv_ops,
1190 .boot_linux = efi_boot_linux,
1191 .vesa = &efi_vesa_ops,
1192 .mem = &efi_mem_ops,
1195 static inline void syslinux_register_efi(void)
1197 firmware = &efi_fw;
1200 extern void init(void);
1201 extern const struct fs_ops vfat_fs_ops;
1202 extern const struct fs_ops pxe_fs_ops;
1204 char free_high_memory[4096];
1206 extern char __bss_start[];
1207 extern char __bss_end[];
1209 static void efi_setcwd(CHAR16 *dp)
1211 CHAR16 *c16;
1212 char *c8;
1213 int i, j;
1215 /* Search for the start of the last path component */
1216 for (i = StrLen(dp) - 1; i >= 0; i--) {
1217 if (dp[i] == '\\' || dp[i] == '/')
1218 break;
1221 if (i < 0 || i > CURRENTDIR_MAX) {
1222 dp = L"\\";
1223 i = 1;
1226 c8 = CurrentDirName;
1227 c16 = dp;
1229 for (j = 0; j < i; j++) {
1230 if (*c16 == '\\') {
1231 *c8++ = '/';
1232 c16++;
1233 } else
1234 *c8++ = *c16++;
1237 *c8 = '\0';
1240 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *table)
1242 EFI_PXE_BASE_CODE *pxe;
1243 EFI_LOADED_IMAGE *info;
1244 EFI_STATUS status = EFI_SUCCESS;
1245 const struct fs_ops *ops[] = { NULL, NULL };
1246 unsigned long len = (unsigned long)__bss_end - (unsigned long)__bss_start;
1247 static struct efi_disk_private priv;
1248 SIMPLE_INPUT_INTERFACE *in;
1249 EFI_INPUT_KEY key;
1250 EFI_EVENT timer_ev;
1252 memset(__bss_start, 0, len);
1253 InitializeLib(image, table);
1255 image_handle = image;
1256 syslinux_register_efi();
1258 efi_console_save();
1259 init();
1261 status = uefi_call_wrapper(BS->HandleProtocol, 3, image,
1262 &LoadedImageProtocol, (void **)&info);
1263 if (status != EFI_SUCCESS) {
1264 Print(L"Failed to lookup LoadedImageProtocol\n");
1265 goto out;
1268 status = uefi_call_wrapper(BS->HandleProtocol, 3, info->DeviceHandle,
1269 &PxeBaseCodeProtocol, (void **)&pxe);
1270 if (status != EFI_SUCCESS) {
1272 * Use device handle to set up the volume root to
1273 * proceed with ADV init.
1275 if (EFI_ERROR(efi_set_volroot(info->DeviceHandle))) {
1276 Print(L"Failed to locate root device to prep for ");
1277 Print(L"file operations & ADV initialization\n");
1278 goto out;
1281 efi_derivative(SYSLINUX_FS_SYSLINUX);
1282 ops[0] = &vfat_fs_ops;
1283 } else {
1284 efi_derivative(SYSLINUX_FS_PXELINUX);
1285 ops[0] = &pxe_fs_ops;
1286 pxe_handle = info->DeviceHandle;
1289 /* setup timer for boot menu system support */
1290 status = setup_default_timer(&timer_ev);
1291 if (status != EFI_SUCCESS) {
1292 Print(L"Failed to set up EFI timer support, bailing out\n");
1293 goto out;
1296 /* TODO: once all errors are captured in efi_errno, bail out if necessary */
1298 priv.dev_handle = info->DeviceHandle;
1301 * Set the current working directory, which should be the
1302 * directory that syslinux.efi resides in.
1304 efi_setcwd(DevicePathToStr(info->FilePath));
1306 fs_init(ops, (void *)&priv);
1309 * There may be pending user input that wasn't processed by
1310 * whatever application invoked us. Consume and discard that
1311 * data now.
1313 in = ST->ConIn;
1314 do {
1315 status = uefi_call_wrapper(in->ReadKeyStroke, 2, in, &key);
1316 } while (status != EFI_NOT_READY);
1318 if (!setjmp(load_error_buf))
1319 load_env32(NULL);
1321 /* load_env32() failed.. cancel timer and bailout */
1322 status = cancel_timer(timer_ev);
1323 if (status != EFI_SUCCESS)
1324 Print(L"Failed to cancel EFI timer: %x\n", status);
1327 * Tell the firmware that Syslinux failed to load.
1329 status = EFI_LOAD_ERROR;
1330 out:
1331 efi_console_restore();
1332 return status;