efi_stub.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <ns16550.h>
  17. #include <asm/cpu.h>
  18. #include <asm/io.h>
  19. #include <linux/err.h>
  20. #include <linux/types.h>
  21. #ifndef CONFIG_X86
  22. /*
  23. * Problem areas:
  24. * - putc() uses the ns16550 address directly and assumed I/O access. Many
  25. * platforms will use memory access
  26. * get_codeseg32() is only meaningful on x86
  27. */
  28. #error "This file needs to be ported for use on architectures"
  29. #endif
  30. static struct efi_priv *global_priv;
  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. if (ch == '\n')
  60. putc('\r');
  61. if (use_uart) {
  62. NS16550_t com_port = (NS16550_t)0x3f8;
  63. while ((inb((ulong)&com_port->lsr) & UART_LSR_THRE) == 0)
  64. ;
  65. outb(ch, (ulong)&com_port->thr);
  66. } else {
  67. efi_putc(global_priv, ch);
  68. }
  69. }
  70. void puts(const char *str)
  71. {
  72. while (*str)
  73. putc(*str++);
  74. }
  75. static void _debug_uart_putc(int ch)
  76. {
  77. putc(ch);
  78. }
  79. DEBUG_UART_FUNCS
  80. void *memcpy(void *dest, const void *src, size_t size)
  81. {
  82. unsigned char *dptr = dest;
  83. const unsigned char *ptr = src;
  84. const unsigned char *end = src + size;
  85. while (ptr < end)
  86. *dptr++ = *ptr++;
  87. return dest;
  88. }
  89. void *memset(void *inptr, int ch, size_t size)
  90. {
  91. char *ptr = inptr;
  92. char *end = ptr + size;
  93. while (ptr < end)
  94. *ptr++ = ch;
  95. return ptr;
  96. }
  97. static void jump_to_uboot(ulong cs32, ulong addr, ulong info)
  98. {
  99. #ifdef CONFIG_EFI_STUB_32BIT
  100. /*
  101. * U-Boot requires these parameters in registers, not on the stack.
  102. * See _x86boot_start() for this code.
  103. */
  104. typedef void (*func_t)(int bist, int unused, ulong info)
  105. __attribute__((regparm(3)));
  106. ((func_t)addr)(0, 0, info);
  107. #else
  108. cpu_call32(cs32, CONFIG_SYS_TEXT_BASE, info);
  109. #endif
  110. }
  111. #ifdef CONFIG_EFI_STUB_64BIT
  112. static void get_gdt(struct desctab_info *info)
  113. {
  114. asm volatile ("sgdt %0" : : "m"(*info) : "memory");
  115. }
  116. #endif
  117. static inline unsigned long read_cr3(void)
  118. {
  119. unsigned long val;
  120. asm volatile("mov %%cr3,%0" : "=r" (val) : : "memory");
  121. return val;
  122. }
  123. /**
  124. * get_codeseg32() - Find the code segment to use for 32-bit code
  125. *
  126. * U-Boot only works in 32-bit mode at present, so when booting from 64-bit
  127. * EFI we must first change to 32-bit mode. To do this we need to find the
  128. * correct code segment to use (an entry in the Global Descriptor Table).
  129. *
  130. * @return code segment GDT offset, or 0 for 32-bit EFI, -ENOENT if not found
  131. */
  132. static int get_codeseg32(void)
  133. {
  134. int cs32 = 0;
  135. #ifdef CONFIG_EFI_STUB_64BIT
  136. struct desctab_info gdt;
  137. uint64_t *ptr;
  138. int i;
  139. get_gdt(&gdt);
  140. for (ptr = (uint64_t *)(unsigned long)gdt.addr, i = 0; i < gdt.limit;
  141. i += 8, ptr++) {
  142. uint64_t desc = *ptr;
  143. uint64_t base, limit;
  144. /*
  145. * Check that the target U-Boot jump address is within the
  146. * selector and that the selector is of the right type.
  147. */
  148. base = ((desc >> GDT_BASE_LOW_SHIFT) & GDT_BASE_LOW_MASK) |
  149. ((desc >> GDT_BASE_HIGH_SHIFT) & GDT_BASE_HIGH_MASK)
  150. << 16;
  151. limit = ((desc >> GDT_LIMIT_LOW_SHIFT) & GDT_LIMIT_LOW_MASK) |
  152. ((desc >> GDT_LIMIT_HIGH_SHIFT) & GDT_LIMIT_HIGH_MASK)
  153. << 16;
  154. base <<= 12; /* 4KB granularity */
  155. limit <<= 12;
  156. if ((desc & GDT_PRESENT) && (desc & GDT_NOTSYS) &&
  157. !(desc & GDT_LONG) && (desc & GDT_4KB) &&
  158. (desc & GDT_32BIT) && (desc & GDT_CODE) &&
  159. CONFIG_SYS_TEXT_BASE > base &&
  160. CONFIG_SYS_TEXT_BASE + CONFIG_SYS_MONITOR_LEN < limit
  161. ) {
  162. cs32 = i;
  163. break;
  164. }
  165. }
  166. #ifdef DEBUG
  167. puts("\ngdt: ");
  168. printhex8(gdt.limit);
  169. puts(", addr: ");
  170. printhex8(gdt.addr >> 32);
  171. printhex8(gdt.addr);
  172. for (i = 0; i < gdt.limit; i += 8) {
  173. uint32_t *ptr = (uint32_t *)((unsigned long)gdt.addr + i);
  174. puts("\n");
  175. printhex2(i);
  176. puts(": ");
  177. printhex8(ptr[1]);
  178. puts(" ");
  179. printhex8(ptr[0]);
  180. }
  181. puts("\n ");
  182. puts("32-bit code segment: ");
  183. printhex2(cs32);
  184. puts("\n ");
  185. puts("page_table: ");
  186. printhex8(read_cr3());
  187. puts("\n ");
  188. #endif
  189. if (!cs32) {
  190. puts("Can't find 32-bit code segment\n");
  191. return -ENOENT;
  192. }
  193. #endif
  194. return cs32;
  195. }
  196. static int setup_info_table(struct efi_priv *priv, int size)
  197. {
  198. struct efi_info_hdr *info;
  199. efi_status_t ret;
  200. /* Get some memory for our info table */
  201. priv->info_size = size;
  202. info = efi_malloc(priv, priv->info_size, &ret);
  203. if (ret) {
  204. printhex2(ret);
  205. puts(" No memory for info table: ");
  206. return ret;
  207. }
  208. memset(info, '\0', sizeof(*info));
  209. info->version = EFI_TABLE_VERSION;
  210. info->hdr_size = sizeof(*info);
  211. priv->info = info;
  212. priv->next_hdr = (char *)info + info->hdr_size;
  213. return 0;
  214. }
  215. static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type,
  216. void *ptr1, int size1, void *ptr2, int size2)
  217. {
  218. struct efi_entry_hdr *hdr = priv->next_hdr;
  219. hdr->type = type;
  220. hdr->size = size1 + size2;
  221. hdr->addr = 0;
  222. hdr->link = ALIGN(sizeof(*hdr) + hdr->size, 16);
  223. priv->next_hdr += hdr->link;
  224. memcpy(hdr + 1, ptr1, size1);
  225. memcpy((void *)(hdr + 1) + size1, ptr2, size2);
  226. priv->info->total_size = (ulong)priv->next_hdr - (ulong)priv->info;
  227. }
  228. /**
  229. * efi_main() - Start an EFI image
  230. *
  231. * This function is called by our EFI start-up code. It handles running
  232. * U-Boot. If it returns, EFI will continue.
  233. */
  234. efi_status_t EFIAPI efi_main(efi_handle_t image,
  235. struct efi_system_table *sys_table)
  236. {
  237. struct efi_priv local_priv, *priv = &local_priv;
  238. struct efi_boot_services *boot = sys_table->boottime;
  239. struct efi_mem_desc *desc;
  240. struct efi_entry_memmap map;
  241. struct efi_gop *gop;
  242. struct efi_entry_gopmode mode;
  243. struct efi_entry_systable table;
  244. efi_guid_t efi_gop_guid = EFI_GOP_GUID;
  245. efi_uintn_t key, desc_size, size;
  246. efi_status_t ret;
  247. u32 version;
  248. int cs32;
  249. ret = efi_init(priv, "Payload", image, sys_table);
  250. if (ret) {
  251. printhex2(ret);
  252. puts(" efi_init() failed\n");
  253. return ret;
  254. }
  255. global_priv = priv;
  256. cs32 = get_codeseg32();
  257. if (cs32 < 0)
  258. return EFI_UNSUPPORTED;
  259. /* Get the memory map so we can switch off EFI */
  260. size = 0;
  261. ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version);
  262. if (ret != EFI_BUFFER_TOO_SMALL) {
  263. printhex2(EFI_BITS_PER_LONG);
  264. putc(' ');
  265. printhex2(ret);
  266. puts(" No memory map\n");
  267. return ret;
  268. }
  269. size += 1024; /* Since doing a malloc() may change the memory map! */
  270. desc = efi_malloc(priv, size, &ret);
  271. if (!desc) {
  272. printhex2(ret);
  273. puts(" No memory for memory descriptor\n");
  274. return ret;
  275. }
  276. ret = setup_info_table(priv, size + 128);
  277. if (ret)
  278. return ret;
  279. ret = boot->locate_protocol(&efi_gop_guid, NULL, (void **)&gop);
  280. if (ret) {
  281. puts(" GOP unavailable\n");
  282. } else {
  283. mode.fb_base = gop->mode->fb_base;
  284. mode.fb_size = gop->mode->fb_size;
  285. mode.info_size = gop->mode->info_size;
  286. add_entry_addr(priv, EFIET_GOP_MODE, &mode, sizeof(mode),
  287. gop->mode->info,
  288. sizeof(struct efi_gop_mode_info));
  289. }
  290. ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version);
  291. if (ret) {
  292. printhex2(ret);
  293. puts(" Can't get memory map\n");
  294. return ret;
  295. }
  296. table.sys_table = (ulong)sys_table;
  297. add_entry_addr(priv, EFIET_SYS_TABLE, &table, sizeof(table), NULL, 0);
  298. ret = boot->exit_boot_services(image, key);
  299. if (ret) {
  300. /*
  301. * Unfortunately it happens that we cannot exit boot services
  302. * the first time. But the second time it work. I don't know
  303. * why but this seems to be a repeatable problem. To get
  304. * around it, just try again.
  305. */
  306. printhex2(ret);
  307. puts(" Can't exit boot services\n");
  308. size = sizeof(desc);
  309. ret = boot->get_memory_map(&size, desc, &key, &desc_size,
  310. &version);
  311. if (ret) {
  312. printhex2(ret);
  313. puts(" Can't get memory map\n");
  314. return ret;
  315. }
  316. ret = boot->exit_boot_services(image, key);
  317. if (ret) {
  318. printhex2(ret);
  319. puts(" Can't exit boot services 2\n");
  320. return ret;
  321. }
  322. }
  323. /* The EFI UART won't work now, switch to a debug one */
  324. use_uart = true;
  325. map.version = version;
  326. map.desc_size = desc_size;
  327. add_entry_addr(priv, EFIET_MEMORY_MAP, &map, sizeof(map), desc, size);
  328. add_entry_addr(priv, EFIET_END, NULL, 0, 0, 0);
  329. memcpy((void *)CONFIG_SYS_TEXT_BASE, _binary_u_boot_bin_start,
  330. (ulong)_binary_u_boot_bin_end -
  331. (ulong)_binary_u_boot_bin_start);
  332. #ifdef DEBUG
  333. puts("EFI table at ");
  334. printhex8((ulong)priv->info);
  335. puts(" size ");
  336. printhex8(priv->info->total_size);
  337. #endif
  338. putc('\n');
  339. jump_to_uboot(cs32, CONFIG_SYS_TEXT_BASE, (ulong)priv->info);
  340. return EFI_LOAD_ERROR;
  341. }