efi_stub.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 struct efi_priv *global_priv;
  32. static bool use_uart;
  33. struct __packed desctab_info {
  34. uint16_t limit;
  35. uint64_t addr;
  36. uint16_t pad;
  37. };
  38. /*
  39. * EFI uses Unicode and we don't. The easiest way to get a sensible output
  40. * function is to use the U-Boot debug UART. We use EFI's console output
  41. * function where available, and assume the built-in UART after that. We rely
  42. * on EFI to set up the UART for us and just bring in the functions here.
  43. * This last bit is a bit icky, but it's only for debugging anyway. We could
  44. * build in ns16550.c with some effort, but this is a payload loader after
  45. * all.
  46. *
  47. * Note: We avoid using printf() so we don't need to bring in lib/vsprintf.c.
  48. * That would require some refactoring since we already build this for U-Boot.
  49. * Building an EFI shared library version would have to be a separate stem.
  50. * That might push us to using the SPL framework to build this stub. However
  51. * that would involve a round of EFI-specific changes in SPL. Worth
  52. * considering if we start needing more U-Boot functionality. Note that we
  53. * could then move get_codeseg32() to arch/x86/cpu/cpu.c.
  54. */
  55. void _debug_uart_init(void)
  56. {
  57. }
  58. void putc(const char ch)
  59. {
  60. if (ch == '\n')
  61. putc('\r');
  62. if (use_uart) {
  63. NS16550_t com_port = (NS16550_t)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(global_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_SYS_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_SYS_TEXT_BASE > base &&
  161. CONFIG_SYS_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. static int setup_info_table(struct efi_priv *priv, int size)
  198. {
  199. struct efi_info_hdr *info;
  200. efi_status_t ret;
  201. /* Get some memory for our info table */
  202. priv->info_size = size;
  203. info = efi_malloc(priv, priv->info_size, &ret);
  204. if (ret) {
  205. printhex2(ret);
  206. puts(" No memory for info table: ");
  207. return ret;
  208. }
  209. memset(info, '\0', sizeof(*info));
  210. info->version = EFI_TABLE_VERSION;
  211. info->hdr_size = sizeof(*info);
  212. priv->info = info;
  213. priv->next_hdr = (char *)info + info->hdr_size;
  214. return 0;
  215. }
  216. static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type,
  217. void *ptr1, int size1, void *ptr2, int size2)
  218. {
  219. struct efi_entry_hdr *hdr = priv->next_hdr;
  220. hdr->type = type;
  221. hdr->size = size1 + size2;
  222. hdr->addr = 0;
  223. hdr->link = ALIGN(sizeof(*hdr) + hdr->size, 16);
  224. priv->next_hdr += hdr->link;
  225. memcpy(hdr + 1, ptr1, size1);
  226. memcpy((void *)(hdr + 1) + size1, ptr2, size2);
  227. priv->info->total_size = (ulong)priv->next_hdr - (ulong)priv->info;
  228. }
  229. /**
  230. * efi_main() - Start an EFI image
  231. *
  232. * This function is called by our EFI start-up code. It handles running
  233. * U-Boot. If it returns, EFI will continue.
  234. */
  235. efi_status_t EFIAPI efi_main(efi_handle_t image,
  236. struct efi_system_table *sys_table)
  237. {
  238. struct efi_priv local_priv, *priv = &local_priv;
  239. struct efi_boot_services *boot = sys_table->boottime;
  240. struct efi_mem_desc *desc;
  241. struct efi_entry_memmap map;
  242. struct efi_gop *gop;
  243. struct efi_entry_gopmode mode;
  244. struct efi_entry_systable table;
  245. efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  246. efi_uintn_t key, desc_size, size;
  247. efi_status_t ret;
  248. u32 version;
  249. int cs32;
  250. ret = efi_init(priv, "Payload", image, sys_table);
  251. if (ret) {
  252. printhex2(ret);
  253. puts(" efi_init() failed\n");
  254. return ret;
  255. }
  256. global_priv = priv;
  257. cs32 = get_codeseg32();
  258. if (cs32 < 0)
  259. return EFI_UNSUPPORTED;
  260. /* Get the memory map so we can switch off EFI */
  261. size = 0;
  262. ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version);
  263. if (ret != EFI_BUFFER_TOO_SMALL) {
  264. printhex2(EFI_BITS_PER_LONG);
  265. putc(' ');
  266. printhex2(ret);
  267. puts(" No memory map\n");
  268. return ret;
  269. }
  270. size += 1024; /* Since doing a malloc() may change the memory map! */
  271. desc = efi_malloc(priv, size, &ret);
  272. if (!desc) {
  273. printhex2(ret);
  274. puts(" No memory for memory descriptor\n");
  275. return ret;
  276. }
  277. ret = setup_info_table(priv, size + 128);
  278. if (ret)
  279. return ret;
  280. ret = boot->locate_protocol(&efi_gop_guid, NULL, (void **)&gop);
  281. if (ret) {
  282. puts(" GOP unavailable\n");
  283. } else {
  284. mode.fb_base = gop->mode->fb_base;
  285. mode.fb_size = gop->mode->fb_size;
  286. mode.info_size = gop->mode->info_size;
  287. add_entry_addr(priv, EFIET_GOP_MODE, &mode, sizeof(mode),
  288. gop->mode->info,
  289. sizeof(struct efi_gop_mode_info));
  290. }
  291. ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version);
  292. if (ret) {
  293. printhex2(ret);
  294. puts(" Can't get memory map\n");
  295. return ret;
  296. }
  297. table.sys_table = (ulong)sys_table;
  298. add_entry_addr(priv, EFIET_SYS_TABLE, &table, sizeof(table), NULL, 0);
  299. ret = boot->exit_boot_services(image, key);
  300. if (ret) {
  301. /*
  302. * Unfortunately it happens that we cannot exit boot services
  303. * the first time. But the second time it work. I don't know
  304. * why but this seems to be a repeatable problem. To get
  305. * around it, just try again.
  306. */
  307. printhex2(ret);
  308. puts(" Can't exit boot services\n");
  309. size = sizeof(desc);
  310. ret = boot->get_memory_map(&size, desc, &key, &desc_size,
  311. &version);
  312. if (ret) {
  313. printhex2(ret);
  314. puts(" Can't get memory map\n");
  315. return ret;
  316. }
  317. ret = boot->exit_boot_services(image, key);
  318. if (ret) {
  319. printhex2(ret);
  320. puts(" Can't exit boot services 2\n");
  321. return ret;
  322. }
  323. }
  324. /* The EFI UART won't work now, switch to a debug one */
  325. use_uart = true;
  326. map.version = version;
  327. map.desc_size = desc_size;
  328. add_entry_addr(priv, EFIET_MEMORY_MAP, &map, sizeof(map), desc, size);
  329. add_entry_addr(priv, EFIET_END, NULL, 0, 0, 0);
  330. memcpy((void *)CONFIG_SYS_TEXT_BASE, _binary_u_boot_bin_start,
  331. (ulong)_binary_u_boot_bin_end -
  332. (ulong)_binary_u_boot_bin_start);
  333. #ifdef DEBUG
  334. puts("EFI table at ");
  335. printhex8((ulong)priv->info);
  336. puts(" size ");
  337. printhex8(priv->info->total_size);
  338. #endif
  339. putc('\n');
  340. jump_to_uboot(cs32, CONFIG_SYS_TEXT_BASE, (ulong)priv->info);
  341. return EFI_LOAD_ERROR;
  342. }