Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * |
Simon Glass | 476476e | 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 | 96a8d40 | 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 | 476476e | 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> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 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 | |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 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 | |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 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 | 97b0597 | 2015-10-18 19:51:23 -0600 | [diff] [blame] | 59 | void _debug_uart_init(void) |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
| 63 | void putc(const char ch) |
| 64 | { |
Simon Glass | 2a1cf03 | 2021-12-29 11:57:45 -0700 | [diff] [blame] | 65 | struct efi_priv *priv = efi_get_priv(); |
| 66 | |
Bin Meng | 075bb5c | 2016-03-17 23:59:03 -0700 | [diff] [blame] | 67 | if (ch == '\n') |
| 68 | putc('\r'); |
| 69 | |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 70 | if (use_uart) { |
Simon Glass | d30c720 | 2020-12-22 19:30:18 -0700 | [diff] [blame] | 71 | struct ns16550 *com_port = (struct ns16550 *)0x3f8; |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 72 | |
| 73 | while ((inb((ulong)&com_port->lsr) & UART_LSR_THRE) == 0) |
| 74 | ; |
| 75 | outb(ch, (ulong)&com_port->thr); |
| 76 | } else { |
Simon Glass | 2a1cf03 | 2021-12-29 11:57:45 -0700 | [diff] [blame] | 77 | efi_putc(priv, ch); |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 78 | } |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 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 |
Simon Glass | 96a8d40 | 2015-08-04 12:33:56 -0600 | [diff] [blame] | 129 | cpu_call32(cs32, CONFIG_SYS_TEXT_BASE, info); |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 130 | #endif |
| 131 | } |
| 132 | |
Simon Glass | 96a8d40 | 2015-08-04 12:33:56 -0600 | [diff] [blame] | 133 | #ifdef CONFIG_EFI_STUB_64BIT |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 134 | static void get_gdt(struct desctab_info *info) |
| 135 | { |
| 136 | asm volatile ("sgdt %0" : : "m"(*info) : "memory"); |
| 137 | } |
Simon Glass | 96a8d40 | 2015-08-04 12:33:56 -0600 | [diff] [blame] | 138 | #endif |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 139 | |
| 140 | static inline unsigned long read_cr3(void) |
| 141 | { |
| 142 | unsigned long val; |
| 143 | |
| 144 | asm volatile("mov %%cr3,%0" : "=r" (val) : : "memory"); |
| 145 | return val; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * get_codeseg32() - Find the code segment to use for 32-bit code |
| 150 | * |
| 151 | * U-Boot only works in 32-bit mode at present, so when booting from 64-bit |
| 152 | * EFI we must first change to 32-bit mode. To do this we need to find the |
| 153 | * correct code segment to use (an entry in the Global Descriptor Table). |
| 154 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 155 | * Return: code segment GDT offset, or 0 for 32-bit EFI, -ENOENT if not found |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 156 | */ |
| 157 | static int get_codeseg32(void) |
| 158 | { |
| 159 | int cs32 = 0; |
| 160 | |
Simon Glass | 96a8d40 | 2015-08-04 12:33:56 -0600 | [diff] [blame] | 161 | #ifdef CONFIG_EFI_STUB_64BIT |
| 162 | struct desctab_info gdt; |
| 163 | uint64_t *ptr; |
| 164 | int i; |
| 165 | |
| 166 | get_gdt(&gdt); |
| 167 | for (ptr = (uint64_t *)(unsigned long)gdt.addr, i = 0; i < gdt.limit; |
| 168 | i += 8, ptr++) { |
| 169 | uint64_t desc = *ptr; |
| 170 | uint64_t base, limit; |
| 171 | |
| 172 | /* |
| 173 | * Check that the target U-Boot jump address is within the |
| 174 | * selector and that the selector is of the right type. |
| 175 | */ |
| 176 | base = ((desc >> GDT_BASE_LOW_SHIFT) & GDT_BASE_LOW_MASK) | |
| 177 | ((desc >> GDT_BASE_HIGH_SHIFT) & GDT_BASE_HIGH_MASK) |
| 178 | << 16; |
| 179 | limit = ((desc >> GDT_LIMIT_LOW_SHIFT) & GDT_LIMIT_LOW_MASK) | |
| 180 | ((desc >> GDT_LIMIT_HIGH_SHIFT) & GDT_LIMIT_HIGH_MASK) |
| 181 | << 16; |
| 182 | base <<= 12; /* 4KB granularity */ |
| 183 | limit <<= 12; |
Alexander Graf | 14d61d4 | 2017-12-04 16:33:26 +0100 | [diff] [blame] | 184 | if ((desc & GDT_PRESENT) && (desc & GDT_NOTSYS) && |
Simon Glass | 96a8d40 | 2015-08-04 12:33:56 -0600 | [diff] [blame] | 185 | !(desc & GDT_LONG) && (desc & GDT_4KB) && |
| 186 | (desc & GDT_32BIT) && (desc & GDT_CODE) && |
| 187 | CONFIG_SYS_TEXT_BASE > base && |
| 188 | CONFIG_SYS_TEXT_BASE + CONFIG_SYS_MONITOR_LEN < limit |
| 189 | ) { |
| 190 | cs32 = i; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | #ifdef DEBUG |
| 196 | puts("\ngdt: "); |
| 197 | printhex8(gdt.limit); |
| 198 | puts(", addr: "); |
| 199 | printhex8(gdt.addr >> 32); |
| 200 | printhex8(gdt.addr); |
| 201 | for (i = 0; i < gdt.limit; i += 8) { |
| 202 | uint32_t *ptr = (uint32_t *)((unsigned long)gdt.addr + i); |
| 203 | |
| 204 | puts("\n"); |
| 205 | printhex2(i); |
| 206 | puts(": "); |
| 207 | printhex8(ptr[1]); |
| 208 | puts(" "); |
| 209 | printhex8(ptr[0]); |
| 210 | } |
| 211 | puts("\n "); |
| 212 | puts("32-bit code segment: "); |
| 213 | printhex2(cs32); |
| 214 | puts("\n "); |
| 215 | |
| 216 | puts("page_table: "); |
| 217 | printhex8(read_cr3()); |
| 218 | puts("\n "); |
| 219 | #endif |
| 220 | if (!cs32) { |
| 221 | puts("Can't find 32-bit code segment\n"); |
| 222 | return -ENOENT; |
| 223 | } |
| 224 | #endif |
| 225 | |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 226 | return cs32; |
| 227 | } |
| 228 | |
Simon Glass | 184be59 | 2021-12-29 11:57:44 -0700 | [diff] [blame] | 229 | /** |
| 230 | * setup_info_table() - sets up a table containing information from EFI |
| 231 | * |
| 232 | * We must call exit_boot_services() before jumping out of the stub into U-Boot |
| 233 | * proper, so that U-Boot has full control of peripherals, memory, etc. |
| 234 | * |
| 235 | * Once we do this, we cannot call any boot-services functions so we must find |
| 236 | * out everything we need to before doing that. |
| 237 | * |
| 238 | * Set up a struct efi_info_hdr table which can hold various records (e.g. |
| 239 | * struct efi_entry_memmap) with information obtained from EFI. |
| 240 | * |
| 241 | * @priv: Pointer to our private information which contains the list |
| 242 | * @size: Size of the table to allocate |
| 243 | * Return: 0 if OK, non-zero on error |
| 244 | */ |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 245 | static int setup_info_table(struct efi_priv *priv, int size) |
| 246 | { |
| 247 | struct efi_info_hdr *info; |
| 248 | efi_status_t ret; |
| 249 | |
| 250 | /* Get some memory for our info table */ |
| 251 | priv->info_size = size; |
| 252 | info = efi_malloc(priv, priv->info_size, &ret); |
| 253 | if (ret) { |
| 254 | printhex2(ret); |
| 255 | puts(" No memory for info table: "); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | memset(info, '\0', sizeof(*info)); |
| 260 | info->version = EFI_TABLE_VERSION; |
| 261 | info->hdr_size = sizeof(*info); |
| 262 | priv->info = info; |
| 263 | priv->next_hdr = (char *)info + info->hdr_size; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Simon Glass | 184be59 | 2021-12-29 11:57:44 -0700 | [diff] [blame] | 268 | /** |
| 269 | * add_entry_addr() - Add a new entry to the efi_info list |
| 270 | * |
| 271 | * This adds an entry, consisting of a tag and two lots of data. This avoids the |
| 272 | * caller having to coalesce the data first |
| 273 | * |
| 274 | * @priv: Pointer to our private information which contains the list |
| 275 | * @type: Type of the entry to add |
| 276 | * @ptr1: Pointer to first data block to add |
| 277 | * @size1: Size of first data block in bytes (can be 0) |
| 278 | * @ptr2: Pointer to second data block to add |
| 279 | * @size2: Size of second data block in bytes (can be 0) |
| 280 | */ |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 281 | static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type, |
| 282 | void *ptr1, int size1, void *ptr2, int size2) |
| 283 | { |
| 284 | struct efi_entry_hdr *hdr = priv->next_hdr; |
| 285 | |
| 286 | hdr->type = type; |
| 287 | hdr->size = size1 + size2; |
| 288 | hdr->addr = 0; |
| 289 | hdr->link = ALIGN(sizeof(*hdr) + hdr->size, 16); |
| 290 | priv->next_hdr += hdr->link; |
| 291 | memcpy(hdr + 1, ptr1, size1); |
| 292 | memcpy((void *)(hdr + 1) + size1, ptr2, size2); |
| 293 | priv->info->total_size = (ulong)priv->next_hdr - (ulong)priv->info; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * efi_main() - Start an EFI image |
| 298 | * |
| 299 | * This function is called by our EFI start-up code. It handles running |
| 300 | * U-Boot. If it returns, EFI will continue. |
| 301 | */ |
Ivan Gorinov | 9f0b011 | 2018-06-13 17:27:39 -0700 | [diff] [blame] | 302 | efi_status_t EFIAPI efi_main(efi_handle_t image, |
| 303 | struct efi_system_table *sys_table) |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 304 | { |
| 305 | struct efi_priv local_priv, *priv = &local_priv; |
| 306 | struct efi_boot_services *boot = sys_table->boottime; |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 307 | struct efi_entry_memmap map; |
Bin Meng | d1fe992 | 2018-06-12 08:36:21 -0700 | [diff] [blame] | 308 | struct efi_gop *gop; |
| 309 | struct efi_entry_gopmode mode; |
Bin Meng | cbe503f | 2018-08-23 08:24:09 -0700 | [diff] [blame] | 310 | struct efi_entry_systable table; |
Heinrich Schuchardt | dec88e4 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 311 | efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 312 | efi_status_t ret; |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 313 | int cs32; |
| 314 | |
| 315 | ret = efi_init(priv, "Payload", image, sys_table); |
| 316 | if (ret) { |
Bin Meng | 558f3ed | 2018-06-10 06:25:09 -0700 | [diff] [blame] | 317 | printhex2(ret); |
| 318 | puts(" efi_init() failed\n"); |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 319 | return ret; |
| 320 | } |
Simon Glass | 2a1cf03 | 2021-12-29 11:57:45 -0700 | [diff] [blame] | 321 | efi_set_priv(priv); |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 322 | |
| 323 | cs32 = get_codeseg32(); |
| 324 | if (cs32 < 0) |
| 325 | return EFI_UNSUPPORTED; |
| 326 | |
Simon Glass | 866e2ac | 2022-01-04 03:51:10 -0700 | [diff] [blame] | 327 | ret = efi_store_memory_map(priv); |
| 328 | if (ret) |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 329 | return ret; |
Simon Glass | 866e2ac | 2022-01-04 03:51:10 -0700 | [diff] [blame] | 330 | |
| 331 | ret = setup_info_table(priv, priv->memmap_size + 128); |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 332 | if (ret) |
| 333 | return ret; |
| 334 | |
Bin Meng | d1fe992 | 2018-06-12 08:36:21 -0700 | [diff] [blame] | 335 | ret = boot->locate_protocol(&efi_gop_guid, NULL, (void **)&gop); |
| 336 | if (ret) { |
| 337 | puts(" GOP unavailable\n"); |
| 338 | } else { |
| 339 | mode.fb_base = gop->mode->fb_base; |
| 340 | mode.fb_size = gop->mode->fb_size; |
| 341 | mode.info_size = gop->mode->info_size; |
| 342 | add_entry_addr(priv, EFIET_GOP_MODE, &mode, sizeof(mode), |
| 343 | gop->mode->info, |
| 344 | sizeof(struct efi_gop_mode_info)); |
| 345 | } |
| 346 | |
Bin Meng | cbe503f | 2018-08-23 08:24:09 -0700 | [diff] [blame] | 347 | table.sys_table = (ulong)sys_table; |
| 348 | add_entry_addr(priv, EFIET_SYS_TABLE, &table, sizeof(table), NULL, 0); |
| 349 | |
Simon Glass | 866e2ac | 2022-01-04 03:51:10 -0700 | [diff] [blame] | 350 | ret = efi_call_exit_boot_services(); |
| 351 | if (ret) |
| 352 | return ret; |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 353 | |
Bin Meng | 7c98ca1 | 2018-06-22 01:38:28 -0700 | [diff] [blame] | 354 | /* The EFI UART won't work now, switch to a debug one */ |
| 355 | use_uart = true; |
| 356 | |
Simon Glass | 866e2ac | 2022-01-04 03:51:10 -0700 | [diff] [blame] | 357 | map.version = priv->memmap_version; |
| 358 | map.desc_size = priv->memmap_desc_size; |
| 359 | add_entry_addr(priv, EFIET_MEMORY_MAP, &map, sizeof(map), |
| 360 | priv->memmap_desc, priv->memmap_size); |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 361 | add_entry_addr(priv, EFIET_END, NULL, 0, 0, 0); |
| 362 | |
Bin Meng | 3dc51ab | 2016-08-25 01:47:17 -0700 | [diff] [blame] | 363 | memcpy((void *)CONFIG_SYS_TEXT_BASE, _binary_u_boot_bin_start, |
| 364 | (ulong)_binary_u_boot_bin_end - |
| 365 | (ulong)_binary_u_boot_bin_start); |
Simon Glass | 476476e | 2015-08-04 12:33:52 -0600 | [diff] [blame] | 366 | |
| 367 | #ifdef DEBUG |
| 368 | puts("EFI table at "); |
| 369 | printhex8((ulong)priv->info); |
| 370 | puts(" size "); |
| 371 | printhex8(priv->info->total_size); |
| 372 | #endif |
| 373 | putc('\n'); |
| 374 | jump_to_uboot(cs32, CONFIG_SYS_TEXT_BASE, (ulong)priv->info); |
| 375 | |
| 376 | return EFI_LOAD_ERROR; |
| 377 | } |