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 a 10 * 32-bit EFI application. 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 DECLARE_GLOBAL_DATA_PTR; 25 26 #ifndef CONFIG_X86 27 /* 28 * Problem areas: 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 32 */ 33 #error "This file needs to be ported for use on architectures" 34 #endif 35 36 static struct efi_priv *global_priv; 37 static bool use_uart; 38 39 struct __packed desctab_info { 40 uint16_t limit; 41 uint64_t addr; 42 uint16_t pad; 43 }; 44 45 /* 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 52 * all. 53 * 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. 61 */ 62 void debug_uart_init(void) 63 { 64 } 65 66 void putc(const char ch) 67 { 68 if (use_uart) { 69 NS16550_t com_port = (NS16550_t)0x3f8; 70 71 while ((inb((ulong)&com_port->lsr) & UART_LSR_THRE) == 0) 72 ; 73 outb(ch, (ulong)&com_port->thr); 74 } else { 75 efi_putc(global_priv, ch); 76 } 77 if (ch == '\n') 78 putc('\r'); 79 } 80 81 void puts(const char *str) 82 { 83 while (*str) 84 putc(*str++); 85 } 86 87 static void _debug_uart_putc(int ch) 88 { 89 putc(ch); 90 } 91 92 DEBUG_UART_FUNCS 93 94 void *memcpy(void *dest, const void *src, size_t size) 95 { 96 unsigned char *dptr = dest; 97 const unsigned char *ptr = src; 98 const unsigned char *end = src + size; 99 100 while (ptr < end) 101 *dptr++ = *ptr++; 102 103 return dest; 104 } 105 106 void *memset(void *inptr, int ch, size_t size) 107 { 108 char *ptr = inptr; 109 char *end = ptr + size; 110 111 while (ptr < end) 112 *ptr++ = ch; 113 114 return ptr; 115 } 116 117 static void jump_to_uboot(ulong cs32, ulong addr, ulong info) 118 { 119 #ifdef CONFIG_EFI_STUB_32BIT 120 /* 121 * U-Boot requires these parameters in registers, not on the stack. 122 * See _x86boot_start() for this code. 123 */ 124 typedef void (*func_t)(int bist, int unused, ulong info) 125 __attribute__((regparm(3))); 126 127 ((func_t)addr)(0, 0, info); 128 #else 129 /* TODO: Implement this */ 130 #endif 131 } 132 133 static void get_gdt(struct desctab_info *info) 134 { 135 asm volatile ("sgdt %0" : : "m"(*info) : "memory"); 136 } 137 138 static inline unsigned long read_cr3(void) 139 { 140 unsigned long val; 141 142 asm volatile("mov %%cr3,%0" : "=r" (val) : : "memory"); 143 return val; 144 } 145 146 /** 147 * get_codeseg32() - Find the code segment to use for 32-bit code 148 * 149 * U-Boot only works in 32-bit mode at present, so when booting from 64-bit 150 * EFI we must first change to 32-bit mode. To do this we need to find the 151 * correct code segment to use (an entry in the Global Descriptor Table). 152 * 153 * @return code segment GDT offset, or 0 for 32-bit EFI, -ENOENT if not found 154 */ 155 static int get_codeseg32(void) 156 { 157 int cs32 = 0; 158 159 /* TODO(sjg): Implement this for 64-bit mode */ 160 return cs32; 161 } 162 163 static int setup_info_table(struct efi_priv *priv, int size) 164 { 165 struct efi_info_hdr *info; 166 efi_status_t ret; 167 168 /* Get some memory for our info table */ 169 priv->info_size = size; 170 info = efi_malloc(priv, priv->info_size, &ret); 171 if (ret) { 172 printhex2(ret); 173 puts(" No memory for info table: "); 174 return ret; 175 } 176 177 memset(info, '\0', sizeof(*info)); 178 info->version = EFI_TABLE_VERSION; 179 info->hdr_size = sizeof(*info); 180 priv->info = info; 181 priv->next_hdr = (char *)info + info->hdr_size; 182 183 return 0; 184 } 185 186 static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type, 187 void *ptr1, int size1, void *ptr2, int size2) 188 { 189 struct efi_entry_hdr *hdr = priv->next_hdr; 190 191 hdr->type = type; 192 hdr->size = size1 + size2; 193 hdr->addr = 0; 194 hdr->link = ALIGN(sizeof(*hdr) + hdr->size, 16); 195 priv->next_hdr += hdr->link; 196 memcpy(hdr + 1, ptr1, size1); 197 memcpy((void *)(hdr + 1) + size1, ptr2, size2); 198 priv->info->total_size = (ulong)priv->next_hdr - (ulong)priv->info; 199 } 200 201 /** 202 * efi_main() - Start an EFI image 203 * 204 * This function is called by our EFI start-up code. It handles running 205 * U-Boot. If it returns, EFI will continue. 206 */ 207 efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table) 208 { 209 struct efi_priv local_priv, *priv = &local_priv; 210 struct efi_boot_services *boot = sys_table->boottime; 211 struct efi_mem_desc *desc; 212 struct efi_entry_memmap map; 213 ulong key, desc_size, size; 214 efi_status_t ret; 215 u32 version; 216 int cs32; 217 218 ret = efi_init(priv, "Payload", image, sys_table); 219 if (ret) { 220 printhex2(ret); puts(" efi_init() failed\n"); 221 return ret; 222 } 223 global_priv = priv; 224 225 cs32 = get_codeseg32(); 226 if (cs32 < 0) 227 return EFI_UNSUPPORTED; 228 229 /* Get the memory map so we can switch off EFI */ 230 size = 0; 231 ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version); 232 if (ret != EFI_BUFFER_TOO_SMALL) { 233 printhex2(BITS_PER_LONG); 234 printhex2(ret); 235 puts(" No memory map\n"); 236 return ret; 237 } 238 size += 1024; /* Since doing a malloc() may change the memory map! */ 239 desc = efi_malloc(priv, size, &ret); 240 if (!desc) { 241 printhex2(ret); 242 puts(" No memory for memory descriptor: "); 243 return ret; 244 } 245 ret = setup_info_table(priv, size + 128); 246 if (ret) 247 return ret; 248 249 ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version); 250 if (ret) { 251 printhex2(ret); 252 puts(" Can't get memory map\n"); 253 return ret; 254 } 255 256 ret = boot->exit_boot_services(image, key); 257 if (ret) { 258 /* 259 * Unfortunately it happens that we cannot exit boot services 260 * the first time. But the second time it work. I don't know 261 * why but this seems to be a repeatable problem. To get 262 * around it, just try again. 263 */ 264 printhex2(ret); 265 puts(" Can't exit boot services\n"); 266 size = sizeof(desc); 267 ret = boot->get_memory_map(&size, desc, &key, &desc_size, 268 &version); 269 if (ret) { 270 printhex2(ret); 271 puts(" Can't get memory map\n"); 272 return ret; 273 } 274 ret = boot->exit_boot_services(image, key); 275 if (ret) { 276 printhex2(ret); 277 puts(" Can't exit boot services 2\n"); 278 return ret; 279 } 280 } 281 282 map.version = version; 283 map.desc_size = desc_size; 284 add_entry_addr(priv, EFIET_MEMORY_MAP, &map, sizeof(map), desc, size); 285 add_entry_addr(priv, EFIET_END, NULL, 0, 0, 0); 286 287 /* The EFI UART won't work now, switch to a debug one */ 288 use_uart = true; 289 290 memcpy((void *)CONFIG_SYS_TEXT_BASE, _binary_u_boot_dtb_bin_start, 291 (ulong)_binary_u_boot_dtb_bin_end - 292 (ulong)_binary_u_boot_dtb_bin_start); 293 294 #ifdef DEBUG 295 puts("EFI table at "); 296 printhex8((ulong)priv->info); 297 puts(" size "); 298 printhex8(priv->info->total_size); 299 #endif 300 putc('\n'); 301 jump_to_uboot(cs32, CONFIG_SYS_TEXT_BASE, (ulong)priv->info); 302 303 return EFI_LOAD_ERROR; 304 } 305