efi_runtime.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * EFI application runtime services
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <efi_loader.h>
  12. #include <rtc.h>
  13. #include <asm/global_data.h>
  14. /* For manual relocation support */
  15. DECLARE_GLOBAL_DATA_PTR;
  16. struct efi_runtime_mmio_list {
  17. struct list_head link;
  18. void **ptr;
  19. u64 paddr;
  20. u64 len;
  21. };
  22. /* This list contains all runtime available mmio regions */
  23. LIST_HEAD(efi_runtime_mmio);
  24. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void);
  25. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void);
  26. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void);
  27. #ifdef CONFIG_SYS_CACHELINE_SIZE
  28. #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
  29. #else
  30. /* Just use the greatest cache flush alignment requirement I'm aware of */
  31. #define EFI_CACHELINE_SIZE 128
  32. #endif
  33. #if defined(CONFIG_ARM64)
  34. #define R_RELATIVE 1027
  35. #define R_MASK 0xffffffffULL
  36. #define IS_RELA 1
  37. #elif defined(CONFIG_ARM)
  38. #define R_RELATIVE 23
  39. #define R_MASK 0xffULL
  40. #else
  41. #error Need to add relocation awareness
  42. #endif
  43. struct elf_rel {
  44. ulong *offset;
  45. ulong info;
  46. };
  47. struct elf_rela {
  48. ulong *offset;
  49. ulong info;
  50. long addend;
  51. };
  52. /*
  53. * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
  54. * payload are running concurrently at the same time. In this mode, we can
  55. * handle a good number of runtime callbacks
  56. */
  57. static void EFIAPI efi_reset_system_boottime(
  58. enum efi_reset_type reset_type,
  59. efi_status_t reset_status,
  60. unsigned long data_size, void *reset_data)
  61. {
  62. EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
  63. reset_data);
  64. switch (reset_type) {
  65. case EFI_RESET_COLD:
  66. case EFI_RESET_WARM:
  67. do_reset(NULL, 0, 0, NULL);
  68. break;
  69. case EFI_RESET_SHUTDOWN:
  70. /* We don't have anything to map this to */
  71. break;
  72. }
  73. while (1) { }
  74. }
  75. static efi_status_t EFIAPI efi_get_time_boottime(
  76. struct efi_time *time,
  77. struct efi_time_cap *capabilities)
  78. {
  79. #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
  80. struct rtc_time tm;
  81. int r;
  82. struct udevice *dev;
  83. EFI_ENTRY("%p %p", time, capabilities);
  84. r = uclass_get_device(UCLASS_RTC, 0, &dev);
  85. if (r)
  86. return EFI_EXIT(EFI_DEVICE_ERROR);
  87. r = dm_rtc_get(dev, &tm);
  88. if (r)
  89. return EFI_EXIT(EFI_DEVICE_ERROR);
  90. memset(time, 0, sizeof(*time));
  91. time->year = tm.tm_year;
  92. time->month = tm.tm_mon;
  93. time->day = tm.tm_mday;
  94. time->hour = tm.tm_hour;
  95. time->minute = tm.tm_min;
  96. time->daylight = tm.tm_isdst;
  97. return EFI_EXIT(EFI_SUCCESS);
  98. #else
  99. return EFI_DEVICE_ERROR;
  100. #endif
  101. }
  102. /* Boards may override the helpers below to implement RTS functionality */
  103. void __weak EFI_RUNTIME_TEXT EFIAPI efi_reset_system(
  104. enum efi_reset_type reset_type,
  105. efi_status_t reset_status,
  106. unsigned long data_size, void *reset_data)
  107. {
  108. /* Nothing we can do */
  109. while (1) { }
  110. }
  111. void __weak efi_reset_system_init(void)
  112. {
  113. }
  114. efi_status_t __weak EFI_RUNTIME_TEXT EFIAPI efi_get_time(
  115. struct efi_time *time,
  116. struct efi_time_cap *capabilities)
  117. {
  118. /* Nothing we can do */
  119. return EFI_DEVICE_ERROR;
  120. }
  121. void __weak efi_get_time_init(void)
  122. {
  123. }
  124. struct efi_runtime_detach_list_struct {
  125. void *ptr;
  126. void *patchto;
  127. };
  128. static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
  129. {
  130. /* do_reset is gone */
  131. .ptr = &efi_runtime_services.reset_system,
  132. .patchto = efi_reset_system,
  133. }, {
  134. /* invalidate_*cache_all are gone */
  135. .ptr = &efi_runtime_services.set_virtual_address_map,
  136. .patchto = &efi_invalid_parameter,
  137. }, {
  138. /* RTC accessors are gone */
  139. .ptr = &efi_runtime_services.get_time,
  140. .patchto = &efi_get_time,
  141. }, {
  142. /* Clean up system table */
  143. .ptr = &systab.con_in,
  144. .patchto = NULL,
  145. }, {
  146. /* Clean up system table */
  147. .ptr = &systab.con_out,
  148. .patchto = NULL,
  149. }, {
  150. /* Clean up system table */
  151. .ptr = &systab.std_err,
  152. .patchto = NULL,
  153. }, {
  154. /* Clean up system table */
  155. .ptr = &systab.boottime,
  156. .patchto = NULL,
  157. },
  158. };
  159. static bool efi_runtime_tobedetached(void *p)
  160. {
  161. int i;
  162. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
  163. if (efi_runtime_detach_list[i].ptr == p)
  164. return true;
  165. return false;
  166. }
  167. static void efi_runtime_detach(ulong offset)
  168. {
  169. int i;
  170. ulong patchoff = offset - (ulong)gd->relocaddr;
  171. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
  172. ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
  173. ulong *p = efi_runtime_detach_list[i].ptr;
  174. ulong newaddr = patchto ? (patchto + patchoff) : 0;
  175. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  176. *p = newaddr;
  177. }
  178. }
  179. /* Relocate EFI runtime to uboot_reloc_base = offset */
  180. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
  181. {
  182. #ifdef IS_RELA
  183. struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
  184. #else
  185. struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
  186. static ulong lastoff = CONFIG_SYS_TEXT_BASE;
  187. #endif
  188. debug("%s: Relocating to offset=%lx\n", __func__, offset);
  189. for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
  190. ulong base = CONFIG_SYS_TEXT_BASE;
  191. ulong *p;
  192. ulong newaddr;
  193. p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
  194. if ((rel->info & R_MASK) != R_RELATIVE) {
  195. continue;
  196. }
  197. #ifdef IS_RELA
  198. newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
  199. #else
  200. newaddr = *p - lastoff + offset;
  201. #endif
  202. /* Check if the relocation is inside bounds */
  203. if (map && ((newaddr < map->virtual_start) ||
  204. newaddr > (map->virtual_start + (map->num_pages << 12)))) {
  205. if (!efi_runtime_tobedetached(p))
  206. printf("U-Boot EFI: Relocation at %p is out of "
  207. "range (%lx)\n", p, newaddr);
  208. continue;
  209. }
  210. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  211. *p = newaddr;
  212. flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
  213. ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
  214. }
  215. #ifndef IS_RELA
  216. lastoff = offset;
  217. #endif
  218. invalidate_icache_all();
  219. }
  220. static efi_status_t EFIAPI efi_set_virtual_address_map(
  221. unsigned long memory_map_size,
  222. unsigned long descriptor_size,
  223. uint32_t descriptor_version,
  224. struct efi_mem_desc *virtmap)
  225. {
  226. ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL;
  227. int n = memory_map_size / descriptor_size;
  228. int i;
  229. EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
  230. descriptor_version, virtmap);
  231. /* Rebind mmio pointers */
  232. for (i = 0; i < n; i++) {
  233. struct efi_mem_desc *map = (void*)virtmap +
  234. (descriptor_size * i);
  235. struct list_head *lhandle;
  236. efi_physical_addr_t map_start = map->physical_start;
  237. efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
  238. efi_physical_addr_t map_end = map_start + map_len;
  239. /* Adjust all mmio pointers in this region */
  240. list_for_each(lhandle, &efi_runtime_mmio) {
  241. struct efi_runtime_mmio_list *lmmio;
  242. lmmio = list_entry(lhandle,
  243. struct efi_runtime_mmio_list,
  244. link);
  245. if ((map_start <= lmmio->paddr) &&
  246. (map_end >= lmmio->paddr)) {
  247. u64 off = map->virtual_start - map_start;
  248. uintptr_t new_addr = lmmio->paddr + off;
  249. *lmmio->ptr = (void *)new_addr;
  250. }
  251. }
  252. }
  253. /* Move the actual runtime code over */
  254. for (i = 0; i < n; i++) {
  255. struct efi_mem_desc *map;
  256. map = (void*)virtmap + (descriptor_size * i);
  257. if (map->type == EFI_RUNTIME_SERVICES_CODE) {
  258. ulong new_offset = map->virtual_start -
  259. (runtime_start - gd->relocaddr);
  260. efi_runtime_relocate(new_offset, map);
  261. /* Once we're virtual, we can no longer handle
  262. complex callbacks */
  263. efi_runtime_detach(new_offset);
  264. return EFI_EXIT(EFI_SUCCESS);
  265. }
  266. }
  267. return EFI_EXIT(EFI_INVALID_PARAMETER);
  268. }
  269. void efi_add_runtime_mmio(void *mmio_ptr, u64 len)
  270. {
  271. struct efi_runtime_mmio_list *newmmio;
  272. u64 pages = (len + EFI_PAGE_SIZE - 1) >> EFI_PAGE_SHIFT;
  273. efi_add_memory_map(*(uintptr_t *)mmio_ptr, pages, EFI_MMAP_IO, false);
  274. newmmio = calloc(1, sizeof(*newmmio));
  275. newmmio->ptr = mmio_ptr;
  276. newmmio->paddr = *(uintptr_t *)mmio_ptr;
  277. newmmio->len = len;
  278. list_add_tail(&newmmio->link, &efi_runtime_mmio);
  279. }
  280. /*
  281. * In the second stage, U-Boot has disappeared. To isolate our runtime code
  282. * that at this point still exists from the rest, we put it into a special
  283. * section.
  284. *
  285. * !!WARNING!!
  286. *
  287. * This means that we can not rely on any code outside of this file in any
  288. * function or variable below this line.
  289. *
  290. * Please keep everything fully self-contained and annotated with
  291. * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers.
  292. */
  293. /*
  294. * Relocate the EFI runtime stub to a different place. We need to call this
  295. * the first time we expose the runtime interface to a user and on set virtual
  296. * address map calls.
  297. */
  298. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void)
  299. {
  300. return EFI_UNSUPPORTED;
  301. }
  302. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void)
  303. {
  304. return EFI_DEVICE_ERROR;
  305. }
  306. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void)
  307. {
  308. return EFI_INVALID_PARAMETER;
  309. }
  310. struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = {
  311. .hdr = {
  312. .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
  313. .revision = EFI_RUNTIME_SERVICES_REVISION,
  314. .headersize = sizeof(struct efi_table_hdr),
  315. },
  316. .get_time = &efi_get_time_boottime,
  317. .set_time = (void *)&efi_device_error,
  318. .get_wakeup_time = (void *)&efi_unimplemented,
  319. .set_wakeup_time = (void *)&efi_unimplemented,
  320. .set_virtual_address_map = &efi_set_virtual_address_map,
  321. .convert_pointer = (void *)&efi_invalid_parameter,
  322. .get_variable = (void *)&efi_device_error,
  323. .get_next_variable = (void *)&efi_device_error,
  324. .set_variable = (void *)&efi_device_error,
  325. .get_next_high_mono_count = (void *)&efi_device_error,
  326. .reset_system = &efi_reset_system_boottime,
  327. };