efi_stub.c 9.5 KB

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