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