]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/efi/efi_stub.c
2 * Copyright (c) 2015 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
6 * EFI information obtained here:
7 * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
9 * Loads a payload (U-Boot) within the EFI environment. This is built as an
10 * EFI application. It can be built either in 32-bit or 64-bit mode.
14 #include <debug_uart.h>
21 #include <linux/err.h>
22 #include <linux/types.h>
24 DECLARE_GLOBAL_DATA_PTR
;
29 * - putc() uses the ns16550 address directly and assumed I/O access. Many
30 * platforms will use memory access
31 * get_codeseg32() is only meaningful on x86
33 #error "This file needs to be ported for use on architectures"
36 static struct efi_priv
*global_priv
;
39 struct __packed desctab_info
{
46 * EFI uses Unicode and we don't. The easiest way to get a sensible output
47 * function is to use the U-Boot debug UART. We use EFI's console output
48 * function where available, and assume the built-in UART after that. We rely
49 * on EFI to set up the UART for us and just bring in the functions here.
50 * This last bit is a bit icky, but it's only for debugging anyway. We could
51 * build in ns16550.c with some effort, but this is a payload loader after
54 * Note: We avoid using printf() so we don't need to bring in lib/vsprintf.c.
55 * That would require some refactoring since we already build this for U-Boot.
56 * Building an EFI shared library version would have to be a separate stem.
57 * That might push us to using the SPL framework to build this stub. However
58 * that would involve a round of EFI-specific changes in SPL. Worth
59 * considering if we start needing more U-Boot functionality. Note that we
60 * could then move get_codeseg32() to arch/x86/cpu/cpu.c.
62 void _debug_uart_init(void)
66 void putc(const char ch
)
72 NS16550_t com_port
= (NS16550_t
)0x3f8;
74 while ((inb((ulong
)&com_port
->lsr
) & UART_LSR_THRE
) == 0)
76 outb(ch
, (ulong
)&com_port
->thr
);
78 efi_putc(global_priv
, ch
);
82 void puts(const char *str
)
88 static void _debug_uart_putc(int ch
)
95 void *memcpy(void *dest
, const void *src
, size_t size
)
97 unsigned char *dptr
= dest
;
98 const unsigned char *ptr
= src
;
99 const unsigned char *end
= src
+ size
;
107 void *memset(void *inptr
, int ch
, size_t size
)
110 char *end
= ptr
+ size
;
118 static void jump_to_uboot(ulong cs32
, ulong addr
, ulong info
)
120 #ifdef CONFIG_EFI_STUB_32BIT
122 * U-Boot requires these parameters in registers, not on the stack.
123 * See _x86boot_start() for this code.
125 typedef void (*func_t
)(int bist
, int unused
, ulong info
)
126 __attribute__((regparm(3)));
128 ((func_t
)addr
)(0, 0, info
);
130 cpu_call32(cs32
, CONFIG_SYS_TEXT_BASE
, info
);
134 #ifdef CONFIG_EFI_STUB_64BIT
135 static void get_gdt(struct desctab_info
*info
)
137 asm volatile ("sgdt %0" : : "m"(*info
) : "memory");
141 static inline unsigned long read_cr3(void)
145 asm volatile("mov %%cr3,%0" : "=r" (val
) : : "memory");
150 * get_codeseg32() - Find the code segment to use for 32-bit code
152 * U-Boot only works in 32-bit mode at present, so when booting from 64-bit
153 * EFI we must first change to 32-bit mode. To do this we need to find the
154 * correct code segment to use (an entry in the Global Descriptor Table).
156 * @return code segment GDT offset, or 0 for 32-bit EFI, -ENOENT if not found
158 static int get_codeseg32(void)
162 #ifdef CONFIG_EFI_STUB_64BIT
163 struct desctab_info gdt
;
168 for (ptr
= (uint64_t *)(unsigned long)gdt
.addr
, i
= 0; i
< gdt
.limit
;
170 uint64_t desc
= *ptr
;
171 uint64_t base
, limit
;
174 * Check that the target U-Boot jump address is within the
175 * selector and that the selector is of the right type.
177 base
= ((desc
>> GDT_BASE_LOW_SHIFT
) & GDT_BASE_LOW_MASK
) |
178 ((desc
>> GDT_BASE_HIGH_SHIFT
) & GDT_BASE_HIGH_MASK
)
180 limit
= ((desc
>> GDT_LIMIT_LOW_SHIFT
) & GDT_LIMIT_LOW_MASK
) |
181 ((desc
>> GDT_LIMIT_HIGH_SHIFT
) & GDT_LIMIT_HIGH_MASK
)
183 base
<<= 12; /* 4KB granularity */
185 if ((desc
& GDT_PRESENT
) && (desc
&& GDT_NOTSYS
) &&
186 !(desc
& GDT_LONG
) && (desc
& GDT_4KB
) &&
187 (desc
& GDT_32BIT
) && (desc
& GDT_CODE
) &&
188 CONFIG_SYS_TEXT_BASE
> base
&&
189 CONFIG_SYS_TEXT_BASE
+ CONFIG_SYS_MONITOR_LEN
< limit
198 printhex8(gdt
.limit
);
200 printhex8(gdt
.addr
>> 32);
202 for (i
= 0; i
< gdt
.limit
; i
+= 8) {
203 uint32_t *ptr
= (uint32_t *)((unsigned long)gdt
.addr
+ i
);
213 puts("32-bit code segment: ");
217 puts("page_table: ");
218 printhex8(read_cr3());
222 puts("Can't find 32-bit code segment\n");
230 static int setup_info_table(struct efi_priv
*priv
, int size
)
232 struct efi_info_hdr
*info
;
235 /* Get some memory for our info table */
236 priv
->info_size
= size
;
237 info
= efi_malloc(priv
, priv
->info_size
, &ret
);
240 puts(" No memory for info table: ");
244 memset(info
, '\0', sizeof(*info
));
245 info
->version
= EFI_TABLE_VERSION
;
246 info
->hdr_size
= sizeof(*info
);
248 priv
->next_hdr
= (char *)info
+ info
->hdr_size
;
253 static void add_entry_addr(struct efi_priv
*priv
, enum efi_entry_t type
,
254 void *ptr1
, int size1
, void *ptr2
, int size2
)
256 struct efi_entry_hdr
*hdr
= priv
->next_hdr
;
259 hdr
->size
= size1
+ size2
;
261 hdr
->link
= ALIGN(sizeof(*hdr
) + hdr
->size
, 16);
262 priv
->next_hdr
+= hdr
->link
;
263 memcpy(hdr
+ 1, ptr1
, size1
);
264 memcpy((void *)(hdr
+ 1) + size1
, ptr2
, size2
);
265 priv
->info
->total_size
= (ulong
)priv
->next_hdr
- (ulong
)priv
->info
;
269 * efi_main() - Start an EFI image
271 * This function is called by our EFI start-up code. It handles running
272 * U-Boot. If it returns, EFI will continue.
274 efi_status_t
efi_main(efi_handle_t image
, struct efi_system_table
*sys_table
)
276 struct efi_priv local_priv
, *priv
= &local_priv
;
277 struct efi_boot_services
*boot
= sys_table
->boottime
;
278 struct efi_mem_desc
*desc
;
279 struct efi_entry_memmap map
;
280 ulong key
, desc_size
, size
;
285 ret
= efi_init(priv
, "Payload", image
, sys_table
);
287 printhex2(ret
); puts(" efi_init() failed\n");
292 cs32
= get_codeseg32();
294 return EFI_UNSUPPORTED
;
296 /* Get the memory map so we can switch off EFI */
298 ret
= boot
->get_memory_map(&size
, NULL
, &key
, &desc_size
, &version
);
299 if (ret
!= EFI_BUFFER_TOO_SMALL
) {
300 printhex2(BITS_PER_LONG
);
302 puts(" No memory map\n");
305 size
+= 1024; /* Since doing a malloc() may change the memory map! */
306 desc
= efi_malloc(priv
, size
, &ret
);
309 puts(" No memory for memory descriptor: ");
312 ret
= setup_info_table(priv
, size
+ 128);
316 ret
= boot
->get_memory_map(&size
, desc
, &key
, &desc_size
, &version
);
319 puts(" Can't get memory map\n");
323 ret
= boot
->exit_boot_services(image
, key
);
326 * Unfortunately it happens that we cannot exit boot services
327 * the first time. But the second time it work. I don't know
328 * why but this seems to be a repeatable problem. To get
329 * around it, just try again.
332 puts(" Can't exit boot services\n");
334 ret
= boot
->get_memory_map(&size
, desc
, &key
, &desc_size
,
338 puts(" Can't get memory map\n");
341 ret
= boot
->exit_boot_services(image
, key
);
344 puts(" Can't exit boot services 2\n");
349 map
.version
= version
;
350 map
.desc_size
= desc_size
;
351 add_entry_addr(priv
, EFIET_MEMORY_MAP
, &map
, sizeof(map
), desc
, size
);
352 add_entry_addr(priv
, EFIET_END
, NULL
, 0, 0, 0);
354 /* The EFI UART won't work now, switch to a debug one */
357 memcpy((void *)CONFIG_SYS_TEXT_BASE
, _binary_u_boot_bin_start
,
358 (ulong
)_binary_u_boot_bin_end
-
359 (ulong
)_binary_u_boot_bin_start
);
362 puts("EFI table at ");
363 printhex8((ulong
)priv
->info
);
365 printhex8(priv
->info
->total_size
);
368 jump_to_uboot(cs32
, CONFIG_SYS_TEXT_BASE
, (ulong
)priv
->info
);
370 return EFI_LOAD_ERROR
;