2 * Copyright 2011-2014 Intel Corporation - All Rights Reserved
9 #include <syslinux/memscan.h>
10 #include <syslinux/firmware.h>
11 #include <syslinux/linux.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
;
30 __export
uint8_t KbdMap
[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 struct efi_binding
*efi_create_binding(EFI_GUID
*bguid
, EFI_GUID
*pguid
)
45 EFI_SERVICE_BINDING
*sbp
;
46 struct efi_binding
*b
;
48 EFI_HANDLE protocol
, child
, *handles
= NULL
;
49 UINTN i
, nr_handles
= 0;
51 b
= malloc(sizeof(*b
));
55 status
= LibLocateHandle(ByProtocol
, bguid
, NULL
, &nr_handles
, &handles
);
56 if (status
!= EFI_SUCCESS
)
59 for (i
= 0; i
< nr_handles
; i
++) {
60 status
= uefi_call_wrapper(BS
->OpenProtocol
, 6, handles
[i
],
62 image_handle
, handles
[i
],
63 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
64 if (status
== EFI_SUCCESS
)
67 uefi_call_wrapper(BS
->CloseProtocol
, 4, handles
[i
], bguid
,
68 image_handle
, handles
[i
]);
76 status
= uefi_call_wrapper(sbp
->CreateChild
, 2, sbp
, (EFI_HANDLE
*)&child
);
77 if (status
!= EFI_SUCCESS
)
80 status
= uefi_call_wrapper(BS
->OpenProtocol
, 6, child
,
81 pguid
, (void **)&protocol
,
83 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
84 if (status
!= EFI_SUCCESS
)
87 b
->parent
= handles
[i
];
95 uefi_call_wrapper(sbp
->DestroyChild
, 2, sbp
, child
);
98 uefi_call_wrapper(BS
->CloseProtocol
, 4, handles
[i
], bguid
,
99 image_handle
, handles
[i
]);
106 void efi_destroy_binding(struct efi_binding
*b
, EFI_GUID
*guid
)
108 efi_close_protocol(b
->child
, guid
, image_handle
, b
->binding
);
109 uefi_call_wrapper(b
->binding
->DestroyChild
, 2, b
->binding
, b
->child
);
110 efi_close_protocol(b
->parent
, guid
, image_handle
, b
->parent
);
120 void printf_init(void)
124 __export
void local_boot(uint16_t ax
)
127 * Inform the firmware that we failed to execute correctly, which
128 * will trigger the next entry in the EFI Boot Manager list.
130 longjmp(load_error_buf
, 1);
133 void bios_timer_cleanup(void)
139 void __cdecl
core_farcall(uint32_t c
, const com32sys_t
*a
, com32sys_t
*b
)
143 __export
struct firmware
*firmware
= NULL
;
144 void *__syslinux_adv_ptr
;
145 size_t __syslinux_adv_size
;
146 char core_xfer_buf
[65536];
147 struct iso_boot_info
{
148 uint32_t pvd
; /* LBA of primary volume descriptor */
149 uint32_t file
; /* LBA of boot file */
150 uint32_t length
; /* Length of boot file */
151 uint32_t csum
; /* Checksum of boot file */
152 uint32_t reserved
[10]; /* Currently unused */
162 uint16_t BIOS_fbm
= 1;
166 void gpxe_unload(void)
181 mstime_t
sem_down(struct semaphore
*sem
, mstime_t time
)
183 /* EFI is single threaded */
187 void sem_up(struct semaphore
*sem
)
189 /* EFI is single threaded */
192 __export
volatile uint32_t __ms_timer
= 0;
193 volatile uint32_t __jiffies
= 0;
195 void efi_write_char(uint8_t ch
, uint8_t attribute
)
197 SIMPLE_TEXT_OUTPUT_INTERFACE
*out
= ST
->ConOut
;
200 uefi_call_wrapper(out
->SetAttribute
, 2, out
, attribute
);
202 /* Lookup primary Unicode encoding in the system codepage */
203 c
[0] = codepage
.uni
[0][ch
];
206 uefi_call_wrapper(out
->OutputString
, 2, out
, c
);
209 static void efi_showcursor(const struct term_state
*st
)
211 SIMPLE_TEXT_OUTPUT_INTERFACE
*out
= ST
->ConOut
;
212 bool cursor
= st
->cursor
? true : false;
214 uefi_call_wrapper(out
->EnableCursor
, 2, out
, cursor
);
217 static void efi_set_cursor(int x
, int y
, bool visible
)
219 SIMPLE_TEXT_OUTPUT_INTERFACE
*out
= ST
->ConOut
;
221 uefi_call_wrapper(out
->SetCursorPosition
, 3, out
, x
, y
);
224 static void efi_scroll_up(uint8_t cols
, uint8_t rows
, uint8_t attribute
)
226 efi_write_char('\n', 0);
227 efi_write_char('\r', 0);
230 static void efi_get_mode(int *cols
, int *rows
)
232 SIMPLE_TEXT_OUTPUT_INTERFACE
*out
= ST
->ConOut
;
235 uefi_call_wrapper(out
->QueryMode
, 4, out
, out
->Mode
->Mode
, &c
, &r
);
240 static void efi_erase(int x0
, int y0
, int x1
, int y1
, uint8_t attribute
)
242 SIMPLE_TEXT_OUTPUT_INTERFACE
*out
= ST
->ConOut
;
245 efi_get_mode(&cols
, &rows
);
248 * The BIOS version of this function has the ability to erase
249 * parts or all of the screen - the UEFI console doesn't
250 * support this so we just set the cursor position unless
251 * we're clearing the whole screen.
253 if (!x0
&& y0
== (cols
- 1)) {
254 /* Really clear the screen */
255 uefi_call_wrapper(out
->ClearScreen
, 1, out
);
257 uefi_call_wrapper(out
->SetCursorPosition
, 3, out
, y1
, x1
);
261 static void efi_text_mode(void)
265 static void efi_get_cursor(uint8_t *x
, uint8_t *y
)
267 SIMPLE_TEXT_OUTPUT_INTERFACE
*out
= ST
->ConOut
;
268 *x
= out
->Mode
->CursorColumn
;
269 *y
= out
->Mode
->CursorRow
;
272 struct output_ops efi_ops
= {
274 .write_char
= efi_write_char
,
275 .showcursor
= efi_showcursor
,
276 .set_cursor
= efi_set_cursor
,
277 .scroll_up
= efi_scroll_up
,
278 .get_mode
= efi_get_mode
,
279 .text_mode
= efi_text_mode
,
280 .get_cursor
= efi_get_cursor
,
284 static inline EFI_MEMORY_DESCRIPTOR
*
285 get_memory_map(UINTN
*nr_entries
, UINTN
*key
, UINTN
*desc_sz
,
288 return LibMemoryMap(nr_entries
, key
, desc_sz
, desc_ver
);
292 int efi_scan_memory(scan_memory_callback_t callback
, void *data
)
294 UINTN i
, nr_entries
, key
, desc_sz
;
299 buf
= (UINTN
)get_memory_map(&nr_entries
, &key
, &desc_sz
, &desc_ver
);
304 for (i
= 0; i
< nr_entries
; bufpos
+= desc_sz
, i
++) {
305 EFI_MEMORY_DESCRIPTOR
*m
;
307 enum syslinux_memmap_types type
;
309 m
= (EFI_MEMORY_DESCRIPTOR
*)bufpos
;
310 region_sz
= m
->NumberOfPages
* EFI_PAGE_SIZE
;
313 case EfiConventionalMemory
:
321 rv
= callback(data
, m
->PhysicalStart
, region_sz
, type
);
326 FreePool((void *)buf
);
330 static struct syslinux_memscan efi_memscan
= {
331 .func
= efi_scan_memory
,
334 extern uint16_t *bios_free_mem
;
339 syslinux_memscan_add(&efi_memscan
);
343 char efi_getchar(char *hi
)
345 SIMPLE_INPUT_INTERFACE
*in
= ST
->ConIn
;
350 status
= uefi_call_wrapper(in
->ReadKeyStroke
, 2, in
, &key
);
351 } while (status
== EFI_NOT_READY
);
354 return (char)key
.UnicodeChar
;
357 * We currently only handle scan codes that fit in 8 bits.
359 *hi
= (char)key
.ScanCode
;
363 int efi_pollchar(void)
365 SIMPLE_INPUT_INTERFACE
*in
= ST
->ConIn
;
368 status
= WaitForSingleEvent(in
->WaitForKey
, 1);
369 return status
!= EFI_TIMEOUT
;
372 struct input_ops efi_iops
= {
373 .getchar
= efi_getchar
,
374 .pollchar
= efi_pollchar
,
377 extern void efi_adv_init(void);
378 extern int efi_adv_write(void);
380 struct adv_ops efi_adv_ops
= {
381 .init
= efi_adv_init
,
382 .write
= efi_adv_write
,
386 uint32_t load_signature
;
389 uint32_t desc_version
;
391 uint32_t memmap_size
;
398 #define E820_RESERVED 2
401 #define E820_UNUSABLE 5
403 #define BOOT_SIGNATURE 0xaa55
404 #define SYSLINUX_EFILDR 0x30 /* Is this published value? */
405 #define DEFAULT_TIMER_TICK_DURATION 500000 /* 500000 == 500000 * 100 * 10^-9 == 50 msec */
406 #define DEFAULT_MSTIMER_INC 0x32 /* 50 msec */
414 struct screen_info screen_info
;
415 uint8_t _pad
[0x1c0 - sizeof(struct screen_info
)];
418 uint8_t e820_entries
;
419 uint8_t _pad3
[0x2d0 - 0x1e8 - sizeof(uint8_t)];
420 struct e820_entry e820_map
[E820MAX
];
423 /* Allocate boot parameter block aligned to page */
424 #define BOOT_PARAM_BLKSIZE EFI_SIZE_TO_PAGES(sizeof(struct boot_params)) * EFI_PAGE_SIZE
426 /* Routines in support of efi boot loader were obtained from
427 * http://git.kernel.org/?p=boot/efilinux/efilinux.git:
428 * kernel_jump(), handover_jump(),
429 * emalloc()/efree, alloc_pages/free_pages
430 * allocate_pool()/free_pool()
433 extern void kernel_jump(EFI_PHYSICAL_ADDRESS kernel_start
,
434 struct boot_params
*boot_params
);
435 #if __SIZEOF_POINTER__ == 4
436 #define EFI_LOAD_SIG "EL32"
437 #elif __SIZEOF_POINTER__ == 8
438 #define EFI_LOAD_SIG "EL64"
440 #error "unsupported architecture"
448 struct dt_desc gdt
= { 0x800, (uint64_t *)0 };
449 struct dt_desc idt
= { 0, 0 };
451 static inline EFI_MEMORY_DESCRIPTOR
*
452 get_mem_desc(unsigned long memmap
, UINTN desc_sz
, int i
)
454 return (EFI_MEMORY_DESCRIPTOR
*)(memmap
+ (i
* desc_sz
));
457 EFI_HANDLE image_handle
;
459 static inline UINT64
round_up(UINT64 x
, UINT64 y
)
461 return (((x
- 1) | (y
- 1)) + 1);
464 static inline UINT64
round_down(UINT64 x
, UINT64 y
)
466 return (x
& ~(y
- 1));
469 static void find_addr(EFI_PHYSICAL_ADDRESS
*first
,
470 EFI_PHYSICAL_ADDRESS
*last
,
471 EFI_PHYSICAL_ADDRESS min
,
472 EFI_PHYSICAL_ADDRESS max
,
473 size_t size
, size_t align
)
475 EFI_MEMORY_DESCRIPTOR
*map
;
477 UINTN i
, nr_entries
, key
, desc_sz
;
479 map
= get_memory_map(&nr_entries
, &key
, &desc_sz
, &desc_ver
);
483 for (i
= 0; i
< nr_entries
; i
++) {
484 EFI_MEMORY_DESCRIPTOR
*m
;
485 EFI_PHYSICAL_ADDRESS best
;
488 m
= get_mem_desc((unsigned long)map
, desc_sz
, i
);
489 if (m
->Type
!= EfiConventionalMemory
)
492 if (m
->NumberOfPages
< EFI_SIZE_TO_PAGES(size
))
495 start
= m
->PhysicalStart
;
496 end
= m
->PhysicalStart
+ (m
->NumberOfPages
<< EFI_PAGE_SHIFT
);
501 /* What's the best address? */
502 if (start
< min
&& min
< end
)
505 best
= m
->PhysicalStart
;
507 start
= round_up(best
, align
);
511 /* Have we run out of space in this region? */
512 if (end
< start
|| (start
+ size
) > end
)
523 /* What's the best address? */
524 if (start
< max
&& max
< end
)
529 start
= round_down(best
, align
);
530 if (start
< min
|| start
< m
->PhysicalStart
)
542 * allocate_pages - Allocate memory pages from the system
543 * @atype: type of allocation to perform
544 * @mtype: type of memory to allocate
545 * @num_pages: number of contiguous 4KB pages to allocate
546 * @memory: used to return the address of allocated pages
548 * Allocate @num_pages physically contiguous pages from the system
549 * memory and return a pointer to the base of the allocation in
550 * @memory if the allocation succeeds. On success, the firmware memory
551 * map is updated accordingly.
553 * If @atype is AllocateAddress then, on input, @memory specifies the
554 * address at which to attempt to allocate the memory pages.
556 static inline EFI_STATUS
557 allocate_pages(EFI_ALLOCATE_TYPE atype
, EFI_MEMORY_TYPE mtype
,
558 UINTN num_pages
, EFI_PHYSICAL_ADDRESS
*memory
)
560 return uefi_call_wrapper(BS
->AllocatePages
, 4, atype
,
561 mtype
, num_pages
, memory
);
564 * free_pages - Return memory allocated by allocate_pages() to the firmware
565 * @memory: physical base address of the page range to be freed
566 * @num_pages: number of contiguous 4KB pages to free
568 * On success, the firmware memory map is updated accordingly.
570 static inline EFI_STATUS
571 free_pages(EFI_PHYSICAL_ADDRESS memory
, UINTN num_pages
)
573 return uefi_call_wrapper(BS
->FreePages
, 2, memory
, num_pages
);
576 static EFI_STATUS
allocate_addr(EFI_PHYSICAL_ADDRESS
*addr
, size_t size
)
578 UINTN npages
= EFI_SIZE_TO_PAGES(size
);
580 return uefi_call_wrapper(BS
->AllocatePages
, 4,
582 EfiLoaderData
, npages
,
586 * allocate_pool - Allocate pool memory
587 * @type: the type of pool to allocate
588 * @size: number of bytes to allocate from pool of @type
589 * @buffer: used to return the address of allocated memory
591 * Allocate memory from pool of @type. If the pool needs more memory
592 * pages are allocated from EfiConventionalMemory in order to grow the
595 * All allocations are eight-byte aligned.
597 static inline EFI_STATUS
598 allocate_pool(EFI_MEMORY_TYPE type
, UINTN size
, void **buffer
)
600 return uefi_call_wrapper(BS
->AllocatePool
, 3, type
, size
, buffer
);
604 * free_pool - Return pool memory to the system
605 * @buffer: the buffer to free
607 * Return @buffer to the system. The returned memory is marked as
608 * EfiConventionalMemory.
610 static inline EFI_STATUS
free_pool(void *buffer
)
612 return uefi_call_wrapper(BS
->FreePool
, 1, buffer
);
615 static void free_addr(EFI_PHYSICAL_ADDRESS addr
, size_t size
)
617 UINTN npages
= EFI_SIZE_TO_PAGES(size
);
619 uefi_call_wrapper(BS
->FreePages
, 2, addr
, npages
);
622 /* cancel the established timer */
623 static EFI_STATUS
cancel_timer(EFI_EVENT ev
)
625 return uefi_call_wrapper(BS
->SetTimer
, 3, ev
, TimerCancel
, 0);
628 /* Check if timer went off and update default timer counter */
629 void timer_handler(EFI_EVENT ev
, VOID
*ctx
)
631 __ms_timer
+= DEFAULT_MSTIMER_INC
;
635 /* Setup a default periodic timer */
636 static EFI_STATUS
setup_default_timer(EFI_EVENT
*ev
)
638 EFI_STATUS efi_status
;
641 efi_status
= uefi_call_wrapper( BS
->CreateEvent
, 5, EVT_TIMER
|EVT_NOTIFY_SIGNAL
, TPL_NOTIFY
, (EFI_EVENT_NOTIFY
)timer_handler
, NULL
, ev
);
642 if (efi_status
== EFI_SUCCESS
) {
643 efi_status
= uefi_call_wrapper(BS
->SetTimer
, 3, *ev
, TimerPeriodic
, DEFAULT_TIMER_TICK_DURATION
);
649 * emalloc - Allocate memory with a strict alignment requirement
650 * @size: size in bytes of the requested allocation
651 * @align: the required alignment of the allocation
652 * @addr: a pointer to the allocated address on success
654 * If we cannot satisfy @align we return 0.
656 EFI_STATUS
emalloc(UINTN size
, UINTN align
, EFI_PHYSICAL_ADDRESS
*addr
)
658 UINTN i
, nr_entries
, map_key
, desc_size
;
659 EFI_MEMORY_DESCRIPTOR
*map_buf
;
663 UINTN nr_pages
= EFI_SIZE_TO_PAGES(size
);
665 map_buf
= get_memory_map(&nr_entries
, &map_key
,
666 &desc_size
, &desc_version
);
672 for (i
= 0; i
< nr_entries
; i
++, d
+= desc_size
) {
673 EFI_MEMORY_DESCRIPTOR
*desc
;
674 EFI_PHYSICAL_ADDRESS start
, end
, aligned
;
676 desc
= (EFI_MEMORY_DESCRIPTOR
*)d
;
677 if (desc
->Type
!= EfiConventionalMemory
)
680 if (desc
->NumberOfPages
< nr_pages
)
683 start
= desc
->PhysicalStart
;
684 end
= start
+ (desc
->NumberOfPages
<< EFI_PAGE_SHIFT
);
686 /* Low-memory is super-precious! */
692 aligned
= (start
+ align
-1) & ~(align
-1);
694 if ((aligned
+ size
) <= end
) {
695 err
= allocate_pages(AllocateAddress
, EfiLoaderData
,
697 if (err
== EFI_SUCCESS
) {
705 err
= EFI_OUT_OF_RESOURCES
;
712 * efree - Return memory allocated with emalloc
713 * @memory: the address of the emalloc() allocation
714 * @size: the size of the allocation
716 void efree(EFI_PHYSICAL_ADDRESS memory
, UINTN size
)
718 UINTN nr_pages
= EFI_SIZE_TO_PAGES(size
);
720 free_pages(memory
, nr_pages
);
724 * Check whether 'buf' contains a PE/COFF header and that the PE/COFF
725 * file can be executed by this architecture.
727 static bool valid_pecoff_image(char *buf
)
733 } *pehdr
= (struct pe_header
*)buf
;
739 if (pehdr
->signature
!= 0x5a4d) {
740 dprintf("Invalid MS-DOS header signature\n");
744 if (!pehdr
->offset
|| pehdr
->offset
> 512) {
745 dprintf("Invalid PE header offset\n");
749 chdr
= (struct coff_header
*)&buf
[pehdr
->offset
];
750 if (chdr
->signature
!= 0x4550) {
751 dprintf("Invalid PE header signature\n");
755 #if defined(__x86_64__)
756 if (chdr
->machine
!= 0x8664) {
757 dprintf("Invalid PE machine field\n");
761 if (chdr
->machine
!= 0x14c) {
762 dprintf("Invalid PE machine field\n");
771 * Boot a Linux kernel using the EFI boot stub handover protocol.
773 * This function will not return to its caller if booting the kernel
774 * image succeeds. If booting the kernel image fails, a legacy boot
775 * method should be attempted.
777 static void handover_boot(struct linux_header
*hdr
, struct boot_params
*bp
)
779 unsigned long address
= hdr
->code32_start
+ hdr
->handover_offset
;
780 handover_func_t
*func
= efi_handover
;
782 dprintf("Booting kernel using handover protocol\n");
785 * Ensure that the kernel is a valid PE32(+) file and that the
786 * architecture of the file matches this version of Syslinux - we
787 * can't mix firmware and kernel bitness (e.g. 32-bit kernel on
788 * 64-bit EFI firmware) using the handover protocol.
790 if (!valid_pecoff_image((char *)hdr
))
793 if (hdr
->version
>= 0x20c) {
794 if (hdr
->xloadflags
& XLF_EFI_HANDOVER_32
)
795 func
= efi_handover_32
;
797 if (hdr
->xloadflags
& XLF_EFI_HANDOVER_64
)
798 func
= efi_handover_64
;
801 efi_console_restore();
802 func(image_handle
, ST
, bp
, address
);
805 static int check_linux_header(struct linux_header
*hdr
)
807 if (hdr
->version
< 0x205)
808 hdr
->relocatable_kernel
= 0;
810 /* FIXME: check boot sector signature */
811 if (hdr
->boot_flag
!= BOOT_SIGNATURE
) {
812 printf("Invalid Boot signature 0x%x, bailing out\n", hdr
->boot_flag
);
819 static char *build_cmdline(char *str
)
821 EFI_PHYSICAL_ADDRESS addr
;
823 char *cmdline
= NULL
; /* internal, in efi_physical below 0x3FFFFFFF */
826 * The kernel expects cmdline to be allocated pretty low,
827 * Documentation/x86/boot.txt says,
829 * "The kernel command line can be located anywhere
830 * between the end of the setup heap and 0xA0000"
833 status
= allocate_pages(AllocateMaxAddress
, EfiLoaderData
,
834 EFI_SIZE_TO_PAGES(strlen(str
) + 1),
836 if (status
!= EFI_SUCCESS
) {
837 printf("Failed to allocate memory for kernel command line, bailing out\n");
840 cmdline
= (char *)(UINTN
)addr
;
841 memcpy(cmdline
, str
, strlen(str
) + 1);
845 static int build_gdt(void)
849 /* Allocate gdt consistent with the alignment for architecture */
850 status
= emalloc(gdt
.limit
, __SIZEOF_POINTER__
, (EFI_PHYSICAL_ADDRESS
*)&gdt
.base
);
851 if (status
!= EFI_SUCCESS
) {
852 printf("Failed to allocate memory for GDT, bailing out\n");
855 memset(gdt
.base
, 0x0, gdt
.limit
);
858 * 4Gb - (0x100000*0x1000 = 4Gb)
861 * granularity=4096, 386 (+5th nibble of limit)
863 gdt
.base
[2] = 0x00cf9a000000ffff;
866 * 4Gb - (0x100000*0x1000 = 4Gb)
869 * granularity=4096, 386 (+5th nibble of limit)
871 gdt
.base
[3] = 0x00cf92000000ffff;
873 /* Task segment value */
874 gdt
.base
[4] = 0x0080890000000000;
880 * Callers use ->ramdisk_size to check whether any memory was
881 * allocated (and therefore needs free'ing). The return value indicates
882 * hard error conditions, such as failing to alloc memory for the
883 * ramdisk image. Having no initramfs is not an error.
885 static int handle_ramdisks(struct linux_header
*hdr
,
886 struct initramfs
*initramfs
)
888 EFI_PHYSICAL_ADDRESS last
;
889 struct initramfs
*ip
;
892 addr_t next_addr
, len
, pad
;
894 hdr
->ramdisk_image
= 0;
895 hdr
->ramdisk_size
= 0;
898 * Figure out the size of the initramfs, and where to put it.
899 * We should put it at the highest possible address which is
900 * <= hdr->initrd_addr_max, which fits the entire initramfs.
902 irf_size
= initramfs_size(initramfs
); /* Handles initramfs == NULL */
907 find_addr(NULL
, &last
, 0x1000, hdr
->initrd_addr_max
,
908 irf_size
, INITRAMFS_MAX_ALIGN
);
910 status
= allocate_addr(&last
, irf_size
);
912 if (!last
|| status
!= EFI_SUCCESS
) {
913 printf("Failed to allocate initramfs memory, bailing out\n");
917 hdr
->ramdisk_image
= (uint32_t)last
;
918 hdr
->ramdisk_size
= irf_size
;
920 /* Copy initramfs into allocated memory */
921 for (ip
= initramfs
->next
; ip
->len
; ip
= ip
->next
) {
923 next_addr
= last
+ len
;
926 * If this isn't the last entry, extend the
927 * zero-pad region to enforce the alignment of
931 pad
= -next_addr
& (ip
->next
->align
- 1);
937 memcpy((void *)(UINTN
)last
, ip
->data
, ip
->data_len
);
939 if (len
> ip
->data_len
)
940 memset((void *)(UINTN
)(last
+ ip
->data_len
), 0,
948 static int exit_boot(struct boot_params
*bp
)
950 struct e820_entry
*e820buf
, *e
;
951 EFI_MEMORY_DESCRIPTOR
*map
;
954 UINTN i
, nr_entries
, key
, desc_sz
;
957 /* Build efi memory map */
958 map
= get_memory_map(&nr_entries
, &key
, &desc_sz
, &desc_ver
);
962 bp
->efi
.memmap
= (uint32_t)(unsigned long)map
;
963 bp
->efi
.memmap_size
= nr_entries
* desc_sz
;
964 bp
->efi
.systab
= (uint32_t)(unsigned long)ST
;
965 bp
->efi
.desc_size
= desc_sz
;
966 bp
->efi
.desc_version
= desc_ver
;
967 #if defined(__x86_64__)
968 bp
->efi
.systab_hi
= ((unsigned long)ST
) >> 32;
969 bp
->efi
.memmap_hi
= ((unsigned long)map
) >> 32;
974 * Even though 'memmap' contains the memory map we provided
975 * previously in efi_scan_memory(), we should recalculate the
976 * e820 map because it will most likely have changed in the
979 e
= e820buf
= bp
->e820_map
;
980 for (i
= 0; i
< nr_entries
&& i
< E820MAX
; i
++) {
981 struct e820_entry
*prev
= NULL
;
986 map
= get_mem_desc(bp
->efi
.memmap
, desc_sz
, i
);
987 e
->start
= map
->PhysicalStart
;
988 e
->len
= map
->NumberOfPages
<< EFI_PAGE_SHIFT
;
991 case EfiReservedMemoryType
:
992 case EfiRuntimeServicesCode
:
993 case EfiRuntimeServicesData
:
994 case EfiMemoryMappedIO
:
995 case EfiMemoryMappedIOPortSpace
:
997 e820_type
= E820_RESERVED
;
1000 case EfiUnusableMemory
:
1001 e820_type
= E820_UNUSABLE
;
1004 case EfiACPIReclaimMemory
:
1005 e820_type
= E820_ACPI
;
1010 case EfiBootServicesCode
:
1011 case EfiBootServicesData
:
1012 case EfiConventionalMemory
:
1013 e820_type
= E820_RAM
;
1016 case EfiACPIMemoryNVS
:
1017 e820_type
= E820_NVS
;
1023 e
->type
= e820_type
;
1025 /* Check for adjacent entries we can merge. */
1026 if (prev
&& (prev
->start
+ prev
->len
) == e
->start
&&
1027 prev
->type
== e
->type
)
1028 prev
->len
+= e
->len
;
1033 bp
->e820_entries
= e
- e820buf
;
1035 status
= uefi_call_wrapper(BS
->ExitBootServices
, 2, image_handle
, key
);
1036 if (status
!= EFI_SUCCESS
) {
1037 printf("Failed to exit boot services: 0x%016lx\n", status
);
1046 * Boots the linux kernel using the image and parameters to boot with.
1047 * The EFI boot loader is reworked taking the cue from
1048 * http://git.kernel.org/?p=boot/efilinux/efilinux.git on the need to
1049 * cap key kernel data structures at * 0x3FFFFFFF.
1050 * The kernel image, kernel command line and boot parameter block are copied
1051 * into allocated memory areas that honor the address capping requirement
1052 * prior to kernel handoff.
1055 * Can we move this allocation requirement to com32 linux loader in order
1056 * to avoid double copying kernel image?
1058 int efi_boot_linux(void *kernel_buf
, size_t kernel_size
,
1059 struct initramfs
*initramfs
,
1060 struct setup_data
*setup_data
,
1063 struct linux_header
*hdr
;
1064 struct boot_params
*bp
;
1066 EFI_PHYSICAL_ADDRESS addr
, pref_address
, kernel_start
= 0;
1067 UINT64 setup_sz
, init_size
= 0;
1070 if (check_linux_header(kernel_buf
))
1073 /* allocate for boot parameter block */
1075 status
= allocate_pages(AllocateMaxAddress
, EfiLoaderData
,
1076 BOOT_PARAM_BLKSIZE
, &addr
);
1077 if (status
!= EFI_SUCCESS
) {
1078 printf("Failed to allocate memory for kernel boot parameter block, bailing out\n");
1082 bp
= (struct boot_params
*)(UINTN
)addr
;
1084 memset((void *)bp
, 0x0, BOOT_PARAM_BLKSIZE
);
1085 /* Copy the first two sectors to boot_params */
1086 memcpy((char *)bp
, kernel_buf
, 2 * 512);
1087 hdr
= (struct linux_header
*)bp
;
1089 setup_sz
= (hdr
->setup_sects
+ 1) * 512;
1090 if (hdr
->version
>= 0x20a) {
1091 pref_address
= hdr
->pref_address
;
1092 init_size
= hdr
->init_size
;
1094 pref_address
= 0x100000;
1097 * We need to account for the fact that the kernel
1098 * needs room for decompression, otherwise we could
1099 * end up trashing other chunks of allocated memory.
1101 init_size
= (kernel_size
- setup_sz
) * 3;
1103 hdr
->type_of_loader
= SYSLINUX_EFILDR
; /* SYSLINUX boot loader module */
1104 _cmdline
= build_cmdline(cmdline
);
1108 hdr
->cmd_line_ptr
= (UINT32
)(UINTN
)_cmdline
;
1110 addr
= pref_address
;
1111 status
= allocate_pages(AllocateAddress
, EfiLoaderData
,
1112 EFI_SIZE_TO_PAGES(init_size
), &addr
);
1113 if (status
!= EFI_SUCCESS
) {
1115 * We failed to allocate the preferred address, so
1116 * just allocate some memory and hope for the best.
1118 if (!hdr
->relocatable_kernel
) {
1119 printf("Cannot relocate kernel, bailing out\n");
1123 status
= emalloc(init_size
, hdr
->kernel_alignment
, &addr
);
1124 if (status
!= EFI_SUCCESS
) {
1125 printf("Failed to allocate memory for kernel image, bailing out\n");
1129 kernel_start
= addr
;
1130 /* FIXME: we copy the kernel into the physical memory allocated here
1131 * The syslinux kernel image load elsewhere could allocate the EFI memory from here
1132 * prior to copying kernel and save an extra copy
1134 memcpy((void *)(UINTN
)kernel_start
, kernel_buf
+setup_sz
, kernel_size
-setup_sz
);
1136 hdr
->code32_start
= (UINT32
)((UINT64
)kernel_start
);
1138 dprintf("efi_boot_linux: kernel_start 0x%x kernel_size 0x%x initramfs 0x%x setup_data 0x%x cmdline 0x%x\n",
1139 kernel_start
, kernel_size
, initramfs
, setup_data
, _cmdline
);
1141 if (handle_ramdisks(hdr
, initramfs
))
1144 /* Attempt to use the handover protocol if available */
1145 if (hdr
->version
>= 0x20b && hdr
->handover_offset
)
1146 handover_boot(hdr
, bp
);
1148 setup_screen(&bp
->screen_info
);
1153 dprintf("efi_boot_linux: setup_sects %d kernel_size %d\n", hdr
->setup_sects
, kernel_size
);
1155 efi_console_restore();
1160 memcpy(&bp
->efi
.load_signature
, EFI_LOAD_SIG
, sizeof(uint32_t));
1162 asm volatile ("lidt %0" :: "m" (idt
));
1163 asm volatile ("lgdt %0" :: "m" (gdt
));
1165 kernel_jump(kernel_start
, bp
);
1171 efree((EFI_PHYSICAL_ADDRESS
)(unsigned long)_cmdline
,
1172 strlen(_cmdline
) + 1);
1175 efree((EFI_PHYSICAL_ADDRESS
)(unsigned long)bp
,
1176 BOOT_PARAM_BLKSIZE
);
1177 if (kernel_start
) efree(kernel_start
, init_size
);
1178 if (hdr
->ramdisk_size
)
1179 free_addr(hdr
->ramdisk_image
, hdr
->ramdisk_size
);
1184 extern struct disk
*efi_disk_init(EFI_HANDLE
);
1185 extern void serialcfg(uint16_t *, uint16_t *, uint16_t *);
1187 extern struct vesa_ops efi_vesa_ops
;
1189 struct mem_ops efi_mem_ops
= {
1190 .malloc
= efi_malloc
,
1191 .realloc
= efi_realloc
,
1195 struct firmware efi_fw
= {
1197 .disk_init
= efi_disk_init
,
1200 .get_serial_console_info
= serialcfg
,
1201 .adv_ops
= &efi_adv_ops
,
1202 .boot_linux
= efi_boot_linux
,
1203 .vesa
= &efi_vesa_ops
,
1204 .mem
= &efi_mem_ops
,
1207 static inline void syslinux_register_efi(void)
1212 extern void init(void);
1213 extern const struct fs_ops vfat_fs_ops
;
1214 extern const struct fs_ops pxe_fs_ops
;
1216 char free_high_memory
[4096];
1218 extern char __bss_start
[];
1219 extern char __bss_end
[];
1221 static void efi_setcwd(CHAR16
*dp
)
1227 /* Search for the start of the last path component */
1228 for (i
= StrLen(dp
) - 1; i
>= 0; i
--) {
1229 if (dp
[i
] == '\\' || dp
[i
] == '/')
1233 if (i
< 0 || i
> CURRENTDIR_MAX
) {
1238 c8
= CurrentDirName
;
1241 for (j
= 0; j
< i
; j
++) {
1252 EFI_STATUS
efi_main(EFI_HANDLE image
, EFI_SYSTEM_TABLE
*table
)
1254 EFI_PXE_BASE_CODE
*pxe
;
1255 EFI_LOADED_IMAGE
*info
;
1256 EFI_STATUS status
= EFI_SUCCESS
;
1257 const struct fs_ops
*ops
[] = { NULL
, NULL
};
1258 unsigned long len
= (unsigned long)__bss_end
- (unsigned long)__bss_start
;
1259 static struct efi_disk_private priv
;
1260 SIMPLE_INPUT_INTERFACE
*in
;
1264 memset(__bss_start
, 0, len
);
1265 InitializeLib(image
, table
);
1267 image_handle
= image
;
1268 syslinux_register_efi();
1273 status
= uefi_call_wrapper(BS
->HandleProtocol
, 3, image
,
1274 &LoadedImageProtocol
, (void **)&info
);
1275 if (status
!= EFI_SUCCESS
) {
1276 Print(L
"Failed to lookup LoadedImageProtocol\n");
1280 status
= uefi_call_wrapper(BS
->HandleProtocol
, 3, info
->DeviceHandle
,
1281 &PxeBaseCodeProtocol
, (void **)&pxe
);
1282 if (status
!= EFI_SUCCESS
) {
1284 * Use device handle to set up the volume root to
1285 * proceed with ADV init.
1287 if (EFI_ERROR(efi_set_volroot(info
->DeviceHandle
))) {
1288 Print(L
"Failed to locate root device to prep for ");
1289 Print(L
"file operations & ADV initialization\n");
1293 efi_derivative(SYSLINUX_FS_SYSLINUX
);
1294 ops
[0] = &vfat_fs_ops
;
1296 efi_derivative(SYSLINUX_FS_PXELINUX
);
1297 ops
[0] = &pxe_fs_ops
;
1300 /* setup timer for boot menu system support */
1301 status
= setup_default_timer(&timer_ev
);
1302 if (status
!= EFI_SUCCESS
) {
1303 Print(L
"Failed to set up EFI timer support, bailing out\n");
1307 /* TODO: once all errors are captured in efi_errno, bail out if necessary */
1309 priv
.dev_handle
= info
->DeviceHandle
;
1312 * Set the current working directory, which should be the
1313 * directory that syslinux.efi resides in.
1315 efi_setcwd(DevicePathToStr(info
->FilePath
));
1317 fs_init(ops
, (void *)&priv
);
1320 * There may be pending user input that wasn't processed by
1321 * whatever application invoked us. Consume and discard that
1326 status
= uefi_call_wrapper(in
->ReadKeyStroke
, 2, in
, &key
);
1327 } while (status
!= EFI_NOT_READY
);
1329 if (!setjmp(load_error_buf
))
1332 /* load_env32() failed.. cancel timer and bailout */
1333 status
= cancel_timer(timer_ev
);
1334 if (status
!= EFI_SUCCESS
)
1335 Print(L
"Failed to cancel EFI timer: %x\n", status
);
1338 * Tell the firmware that Syslinux failed to load.
1340 status
= EFI_LOAD_ERROR
;
1342 efi_console_restore();