efi_stub.c 8.8 KB

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