xref: /openbmc/u-boot/lib/efi/efi_stub.c (revision 3ce88cd7)
1 /*
2  * Copyright (c) 2015 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  *
6  * EFI information obtained here:
7  * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
8  *
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.
11  */
12 
13 #include <common.h>
14 #include <debug_uart.h>
15 #include <efi.h>
16 #include <efi_api.h>
17 #include <errno.h>
18 #include <ns16550.h>
19 #include <asm/cpu.h>
20 #include <asm/io.h>
21 #include <linux/err.h>
22 #include <linux/types.h>
23 
24 #ifndef CONFIG_X86
25 /*
26  * Problem areas:
27  * - putc() uses the ns16550 address directly and assumed I/O access. Many
28  *	platforms will use memory access
29  * get_codeseg32() is only meaningful on x86
30  */
31 #error "This file needs to be ported for use on architectures"
32 #endif
33 
34 static struct efi_priv *global_priv;
35 static bool use_uart;
36 
37 struct __packed desctab_info {
38 	uint16_t limit;
39 	uint64_t addr;
40 	uint16_t pad;
41 };
42 
43 /*
44  * EFI uses Unicode and we don't. The easiest way to get a sensible output
45  * function is to use the U-Boot debug UART. We use EFI's console output
46  * function where available, and assume the built-in UART after that. We rely
47  * on EFI to set up the UART for us and just bring in the functions here.
48  * This last bit is a bit icky, but it's only for debugging anyway. We could
49  * build in ns16550.c with some effort, but this is a payload loader after
50  * all.
51  *
52  * Note: We avoid using printf() so we don't need to bring in lib/vsprintf.c.
53  * That would require some refactoring since we already build this for U-Boot.
54  * Building an EFI shared library version would have to be a separate stem.
55  * That might push us to using the SPL framework to build this stub. However
56  * that would involve a round of EFI-specific changes in SPL. Worth
57  * considering if we start needing more U-Boot functionality. Note that we
58  * could then move get_codeseg32() to arch/x86/cpu/cpu.c.
59  */
60 void _debug_uart_init(void)
61 {
62 }
63 
64 void putc(const char ch)
65 {
66 	if (ch == '\n')
67 		putc('\r');
68 
69 	if (use_uart) {
70 		NS16550_t com_port = (NS16550_t)0x3f8;
71 
72 		while ((inb((ulong)&com_port->lsr) & UART_LSR_THRE) == 0)
73 			;
74 		outb(ch, (ulong)&com_port->thr);
75 	} else {
76 		efi_putc(global_priv, ch);
77 	}
78 }
79 
80 void puts(const char *str)
81 {
82 	while (*str)
83 		putc(*str++);
84 }
85 
86 static void _debug_uart_putc(int ch)
87 {
88 	putc(ch);
89 }
90 
91 DEBUG_UART_FUNCS
92 
93 void *memcpy(void *dest, const void *src, size_t size)
94 {
95 	unsigned char *dptr = dest;
96 	const unsigned char *ptr = src;
97 	const unsigned char *end = src + size;
98 
99 	while (ptr < end)
100 		*dptr++ = *ptr++;
101 
102 	return dest;
103 }
104 
105 void *memset(void *inptr, int ch, size_t size)
106 {
107 	char *ptr = inptr;
108 	char *end = ptr + size;
109 
110 	while (ptr < end)
111 		*ptr++ = ch;
112 
113 	return ptr;
114 }
115 
116 static void jump_to_uboot(ulong cs32, ulong addr, ulong info)
117 {
118 #ifdef CONFIG_EFI_STUB_32BIT
119 	/*
120 	 * U-Boot requires these parameters in registers, not on the stack.
121 	 * See _x86boot_start() for this code.
122 	 */
123 	typedef void (*func_t)(int bist, int unused, ulong info)
124 		__attribute__((regparm(3)));
125 
126 	((func_t)addr)(0, 0, info);
127 #else
128 	cpu_call32(cs32, CONFIG_SYS_TEXT_BASE, info);
129 #endif
130 }
131 
132 #ifdef CONFIG_EFI_STUB_64BIT
133 static void get_gdt(struct desctab_info *info)
134 {
135 	asm volatile ("sgdt %0" : : "m"(*info) : "memory");
136 }
137 #endif
138 
139 static inline unsigned long read_cr3(void)
140 {
141 	unsigned long val;
142 
143 	asm volatile("mov %%cr3,%0" : "=r" (val) : : "memory");
144 	return val;
145 }
146 
147 /**
148  * get_codeseg32() - Find the code segment to use for 32-bit code
149  *
150  * U-Boot only works in 32-bit mode at present, so when booting from 64-bit
151  * EFI we must first change to 32-bit mode. To do this we need to find the
152  * correct code segment to use (an entry in the Global Descriptor Table).
153  *
154  * @return code segment GDT offset, or 0 for 32-bit EFI, -ENOENT if not found
155  */
156 static int get_codeseg32(void)
157 {
158 	int cs32 = 0;
159 
160 #ifdef CONFIG_EFI_STUB_64BIT
161 	struct desctab_info gdt;
162 	uint64_t *ptr;
163 	int i;
164 
165 	get_gdt(&gdt);
166 	for (ptr = (uint64_t *)(unsigned long)gdt.addr, i = 0; i < gdt.limit;
167 	     i += 8, ptr++) {
168 		uint64_t desc = *ptr;
169 		uint64_t base, limit;
170 
171 		/*
172 		 * Check that the target U-Boot jump address is within the
173 		 * selector and that the selector is of the right type.
174 		 */
175 		base = ((desc >> GDT_BASE_LOW_SHIFT) & GDT_BASE_LOW_MASK) |
176 			((desc >> GDT_BASE_HIGH_SHIFT) & GDT_BASE_HIGH_MASK)
177 				<< 16;
178 		limit = ((desc >> GDT_LIMIT_LOW_SHIFT) & GDT_LIMIT_LOW_MASK) |
179 			((desc >> GDT_LIMIT_HIGH_SHIFT) & GDT_LIMIT_HIGH_MASK)
180 				<< 16;
181 		base <<= 12;	/* 4KB granularity */
182 		limit <<= 12;
183 		if ((desc & GDT_PRESENT) && (desc & GDT_NOTSYS) &&
184 		    !(desc & GDT_LONG) && (desc & GDT_4KB) &&
185 		    (desc & GDT_32BIT) && (desc & GDT_CODE) &&
186 		    CONFIG_SYS_TEXT_BASE > base &&
187 		    CONFIG_SYS_TEXT_BASE + CONFIG_SYS_MONITOR_LEN < limit
188 		) {
189 			cs32 = i;
190 			break;
191 		}
192 	}
193 
194 #ifdef DEBUG
195 	puts("\ngdt: ");
196 	printhex8(gdt.limit);
197 	puts(", addr: ");
198 	printhex8(gdt.addr >> 32);
199 	printhex8(gdt.addr);
200 	for (i = 0; i < gdt.limit; i += 8) {
201 		uint32_t *ptr = (uint32_t *)((unsigned long)gdt.addr + i);
202 
203 		puts("\n");
204 		printhex2(i);
205 		puts(": ");
206 		printhex8(ptr[1]);
207 		puts("  ");
208 		printhex8(ptr[0]);
209 	}
210 	puts("\n ");
211 	puts("32-bit code segment: ");
212 	printhex2(cs32);
213 	puts("\n ");
214 
215 	puts("page_table: ");
216 	printhex8(read_cr3());
217 	puts("\n ");
218 #endif
219 	if (!cs32) {
220 		puts("Can't find 32-bit code segment\n");
221 		return -ENOENT;
222 	}
223 #endif
224 
225 	return cs32;
226 }
227 
228 static int setup_info_table(struct efi_priv *priv, int size)
229 {
230 	struct efi_info_hdr *info;
231 	efi_status_t ret;
232 
233 	/* Get some memory for our info table */
234 	priv->info_size = size;
235 	info = efi_malloc(priv, priv->info_size, &ret);
236 	if (ret) {
237 		printhex2(ret);
238 		puts(" No memory for info table: ");
239 		return ret;
240 	}
241 
242 	memset(info, '\0', sizeof(*info));
243 	info->version = EFI_TABLE_VERSION;
244 	info->hdr_size = sizeof(*info);
245 	priv->info = info;
246 	priv->next_hdr = (char *)info + info->hdr_size;
247 
248 	return 0;
249 }
250 
251 static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type,
252 			   void *ptr1, int size1, void *ptr2, int size2)
253 {
254 	struct efi_entry_hdr *hdr = priv->next_hdr;
255 
256 	hdr->type = type;
257 	hdr->size = size1 + size2;
258 	hdr->addr = 0;
259 	hdr->link = ALIGN(sizeof(*hdr) + hdr->size, 16);
260 	priv->next_hdr += hdr->link;
261 	memcpy(hdr + 1, ptr1, size1);
262 	memcpy((void *)(hdr + 1) + size1, ptr2, size2);
263 	priv->info->total_size = (ulong)priv->next_hdr - (ulong)priv->info;
264 }
265 
266 /**
267  * efi_main() - Start an EFI image
268  *
269  * This function is called by our EFI start-up code. It handles running
270  * U-Boot. If it returns, EFI will continue.
271  */
272 efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table)
273 {
274 	struct efi_priv local_priv, *priv = &local_priv;
275 	struct efi_boot_services *boot = sys_table->boottime;
276 	struct efi_mem_desc *desc;
277 	struct efi_entry_memmap map;
278 	efi_uintn_t key, desc_size, size;
279 	efi_status_t ret;
280 	u32 version;
281 	int cs32;
282 
283 	ret = efi_init(priv, "Payload", image, sys_table);
284 	if (ret) {
285 		printhex2(ret); puts(" efi_init() failed\n");
286 		return ret;
287 	}
288 	global_priv = priv;
289 
290 	cs32 = get_codeseg32();
291 	if (cs32 < 0)
292 		return EFI_UNSUPPORTED;
293 
294 	/* Get the memory map so we can switch off EFI */
295 	size = 0;
296 	ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version);
297 	if (ret != EFI_BUFFER_TOO_SMALL) {
298 		printhex2(BITS_PER_LONG);
299 		printhex2(ret);
300 		puts(" No memory map\n");
301 		return ret;
302 	}
303 	size += 1024;	/* Since doing a malloc() may change the memory map! */
304 	desc = efi_malloc(priv, size, &ret);
305 	if (!desc) {
306 		printhex2(ret);
307 		puts(" No memory for memory descriptor: ");
308 		return ret;
309 	}
310 	ret = setup_info_table(priv, size + 128);
311 	if (ret)
312 		return ret;
313 
314 	ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version);
315 	if (ret) {
316 		printhex2(ret);
317 		puts(" Can't get memory map\n");
318 		return ret;
319 	}
320 
321 	ret = boot->exit_boot_services(image, key);
322 	if (ret) {
323 		/*
324 		 * Unfortunately it happens that we cannot exit boot services
325 		 * the first time. But the second time it work. I don't know
326 		 * why but this seems to be a repeatable problem. To get
327 		 * around it, just try again.
328 		 */
329 		printhex2(ret);
330 		puts(" Can't exit boot services\n");
331 		size = sizeof(desc);
332 		ret = boot->get_memory_map(&size, desc, &key, &desc_size,
333 					   &version);
334 		if (ret) {
335 			printhex2(ret);
336 			puts(" Can't get memory map\n");
337 			return ret;
338 		}
339 		ret = boot->exit_boot_services(image, key);
340 		if (ret) {
341 			printhex2(ret);
342 			puts(" Can't exit boot services 2\n");
343 			return ret;
344 		}
345 	}
346 
347 	map.version = version;
348 	map.desc_size = desc_size;
349 	add_entry_addr(priv, EFIET_MEMORY_MAP, &map, sizeof(map), desc, size);
350 	add_entry_addr(priv, EFIET_END, NULL, 0, 0, 0);
351 
352 	/* The EFI UART won't work now, switch to a debug one */
353 	use_uart = true;
354 
355 	memcpy((void *)CONFIG_SYS_TEXT_BASE, _binary_u_boot_bin_start,
356 	       (ulong)_binary_u_boot_bin_end -
357 	       (ulong)_binary_u_boot_bin_start);
358 
359 #ifdef DEBUG
360 	puts("EFI table at ");
361 	printhex8((ulong)priv->info);
362 	puts(" size ");
363 	printhex8(priv->info->total_size);
364 #endif
365 	putc('\n');
366 	jump_to_uboot(cs32, CONFIG_SYS_TEXT_BASE, (ulong)priv->info);
367 
368 	return EFI_LOAD_ERROR;
369 }
370