efi_runtime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 EFIAPI efi_unimplemented(void);
  25. static efi_status_t __efi_runtime EFIAPI efi_device_error(void);
  26. static efi_status_t __efi_runtime 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. #elif defined(CONFIG_X86)
  41. #include <asm/elf.h>
  42. #define R_RELATIVE R_386_RELATIVE
  43. #define R_MASK 0xffULL
  44. #else
  45. #error Need to add relocation awareness
  46. #endif
  47. struct elf_rel {
  48. ulong *offset;
  49. ulong info;
  50. };
  51. struct elf_rela {
  52. ulong *offset;
  53. ulong info;
  54. long addend;
  55. };
  56. /*
  57. * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
  58. * payload are running concurrently at the same time. In this mode, we can
  59. * handle a good number of runtime callbacks
  60. */
  61. static void EFIAPI efi_reset_system_boottime(
  62. enum efi_reset_type reset_type,
  63. efi_status_t reset_status,
  64. unsigned long data_size, void *reset_data)
  65. {
  66. EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
  67. reset_data);
  68. switch (reset_type) {
  69. case EFI_RESET_COLD:
  70. case EFI_RESET_WARM:
  71. do_reset(NULL, 0, 0, NULL);
  72. break;
  73. case EFI_RESET_SHUTDOWN:
  74. /* We don't have anything to map this to */
  75. break;
  76. }
  77. while (1) { }
  78. }
  79. static efi_status_t EFIAPI efi_get_time_boottime(
  80. struct efi_time *time,
  81. struct efi_time_cap *capabilities)
  82. {
  83. #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
  84. struct rtc_time tm;
  85. int r;
  86. struct udevice *dev;
  87. EFI_ENTRY("%p %p", time, capabilities);
  88. r = uclass_get_device(UCLASS_RTC, 0, &dev);
  89. if (r)
  90. return EFI_EXIT(EFI_DEVICE_ERROR);
  91. r = dm_rtc_get(dev, &tm);
  92. if (r)
  93. return EFI_EXIT(EFI_DEVICE_ERROR);
  94. memset(time, 0, sizeof(*time));
  95. time->year = tm.tm_year;
  96. time->month = tm.tm_mon;
  97. time->day = tm.tm_mday;
  98. time->hour = tm.tm_hour;
  99. time->minute = tm.tm_min;
  100. time->daylight = tm.tm_isdst;
  101. return EFI_EXIT(EFI_SUCCESS);
  102. #else
  103. return EFI_DEVICE_ERROR;
  104. #endif
  105. }
  106. /* Boards may override the helpers below to implement RTS functionality */
  107. void __weak __efi_runtime EFIAPI efi_reset_system(
  108. enum efi_reset_type reset_type,
  109. efi_status_t reset_status,
  110. unsigned long data_size, void *reset_data)
  111. {
  112. /* Nothing we can do */
  113. while (1) { }
  114. }
  115. efi_status_t __weak efi_reset_system_init(void)
  116. {
  117. return EFI_SUCCESS;
  118. }
  119. efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
  120. struct efi_time *time,
  121. struct efi_time_cap *capabilities)
  122. {
  123. /* Nothing we can do */
  124. return EFI_DEVICE_ERROR;
  125. }
  126. efi_status_t __weak efi_get_time_init(void)
  127. {
  128. return EFI_SUCCESS;
  129. }
  130. struct efi_runtime_detach_list_struct {
  131. void *ptr;
  132. void *patchto;
  133. };
  134. static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
  135. {
  136. /* do_reset is gone */
  137. .ptr = &efi_runtime_services.reset_system,
  138. .patchto = efi_reset_system,
  139. }, {
  140. /* invalidate_*cache_all are gone */
  141. .ptr = &efi_runtime_services.set_virtual_address_map,
  142. .patchto = &efi_invalid_parameter,
  143. }, {
  144. /* RTC accessors are gone */
  145. .ptr = &efi_runtime_services.get_time,
  146. .patchto = &efi_get_time,
  147. }, {
  148. /* Clean up system table */
  149. .ptr = &systab.con_in,
  150. .patchto = NULL,
  151. }, {
  152. /* Clean up system table */
  153. .ptr = &systab.con_out,
  154. .patchto = NULL,
  155. }, {
  156. /* Clean up system table */
  157. .ptr = &systab.std_err,
  158. .patchto = NULL,
  159. }, {
  160. /* Clean up system table */
  161. .ptr = &systab.boottime,
  162. .patchto = NULL,
  163. }, {
  164. .ptr = &efi_runtime_services.get_variable,
  165. .patchto = &efi_device_error,
  166. }, {
  167. .ptr = &efi_runtime_services.get_next_variable,
  168. .patchto = &efi_device_error,
  169. }, {
  170. .ptr = &efi_runtime_services.set_variable,
  171. .patchto = &efi_device_error,
  172. }
  173. };
  174. static bool efi_runtime_tobedetached(void *p)
  175. {
  176. int i;
  177. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
  178. if (efi_runtime_detach_list[i].ptr == p)
  179. return true;
  180. return false;
  181. }
  182. static void efi_runtime_detach(ulong offset)
  183. {
  184. int i;
  185. ulong patchoff = offset - (ulong)gd->relocaddr;
  186. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
  187. ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
  188. ulong *p = efi_runtime_detach_list[i].ptr;
  189. ulong newaddr = patchto ? (patchto + patchoff) : 0;
  190. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  191. *p = newaddr;
  192. }
  193. }
  194. /* Relocate EFI runtime to uboot_reloc_base = offset */
  195. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
  196. {
  197. #ifdef IS_RELA
  198. struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
  199. #else
  200. struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
  201. static ulong lastoff = CONFIG_SYS_TEXT_BASE;
  202. #endif
  203. debug("%s: Relocating to offset=%lx\n", __func__, offset);
  204. for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
  205. ulong base = CONFIG_SYS_TEXT_BASE;
  206. ulong *p;
  207. ulong newaddr;
  208. p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
  209. if ((rel->info & R_MASK) != R_RELATIVE) {
  210. continue;
  211. }
  212. #ifdef IS_RELA
  213. newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
  214. #else
  215. newaddr = *p - lastoff + offset;
  216. #endif
  217. /* Check if the relocation is inside bounds */
  218. if (map && ((newaddr < map->virtual_start) ||
  219. newaddr > (map->virtual_start +
  220. (map->num_pages << EFI_PAGE_SHIFT)))) {
  221. if (!efi_runtime_tobedetached(p))
  222. printf("U-Boot EFI: Relocation at %p is out of "
  223. "range (%lx)\n", p, newaddr);
  224. continue;
  225. }
  226. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  227. *p = newaddr;
  228. flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
  229. ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
  230. }
  231. #ifndef IS_RELA
  232. lastoff = offset;
  233. #endif
  234. invalidate_icache_all();
  235. }
  236. static efi_status_t EFIAPI efi_set_virtual_address_map(
  237. unsigned long memory_map_size,
  238. unsigned long descriptor_size,
  239. uint32_t descriptor_version,
  240. struct efi_mem_desc *virtmap)
  241. {
  242. ulong runtime_start = (ulong)&__efi_runtime_start &
  243. ~(ulong)EFI_PAGE_MASK;
  244. int n = memory_map_size / descriptor_size;
  245. int i;
  246. EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
  247. descriptor_version, virtmap);
  248. /* Rebind mmio pointers */
  249. for (i = 0; i < n; i++) {
  250. struct efi_mem_desc *map = (void*)virtmap +
  251. (descriptor_size * i);
  252. struct list_head *lhandle;
  253. efi_physical_addr_t map_start = map->physical_start;
  254. efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
  255. efi_physical_addr_t map_end = map_start + map_len;
  256. /* Adjust all mmio pointers in this region */
  257. list_for_each(lhandle, &efi_runtime_mmio) {
  258. struct efi_runtime_mmio_list *lmmio;
  259. lmmio = list_entry(lhandle,
  260. struct efi_runtime_mmio_list,
  261. link);
  262. if ((map_start <= lmmio->paddr) &&
  263. (map_end >= lmmio->paddr)) {
  264. u64 off = map->virtual_start - map_start;
  265. uintptr_t new_addr = lmmio->paddr + off;
  266. *lmmio->ptr = (void *)new_addr;
  267. }
  268. }
  269. }
  270. /* Move the actual runtime code over */
  271. for (i = 0; i < n; i++) {
  272. struct efi_mem_desc *map;
  273. map = (void*)virtmap + (descriptor_size * i);
  274. if (map->type == EFI_RUNTIME_SERVICES_CODE) {
  275. ulong new_offset = map->virtual_start -
  276. (runtime_start - gd->relocaddr);
  277. efi_runtime_relocate(new_offset, map);
  278. /* Once we're virtual, we can no longer handle
  279. complex callbacks */
  280. efi_runtime_detach(new_offset);
  281. return EFI_EXIT(EFI_SUCCESS);
  282. }
  283. }
  284. return EFI_EXIT(EFI_INVALID_PARAMETER);
  285. }
  286. efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
  287. {
  288. struct efi_runtime_mmio_list *newmmio;
  289. efi_status_t ret;
  290. u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
  291. ret = efi_add_memory_map(*(uintptr_t *)mmio_ptr, pages, EFI_MMAP_IO,
  292. false);
  293. if (ret != EFI_SUCCESS)
  294. return ret;
  295. newmmio = calloc(1, sizeof(*newmmio));
  296. if (!newmmio)
  297. return EFI_OUT_OF_RESOURCES;
  298. newmmio->ptr = mmio_ptr;
  299. newmmio->paddr = *(uintptr_t *)mmio_ptr;
  300. newmmio->len = len;
  301. list_add_tail(&newmmio->link, &efi_runtime_mmio);
  302. return ret;
  303. }
  304. /*
  305. * In the second stage, U-Boot has disappeared. To isolate our runtime code
  306. * that at this point still exists from the rest, we put it into a special
  307. * section.
  308. *
  309. * !!WARNING!!
  310. *
  311. * This means that we can not rely on any code outside of this file in any
  312. * function or variable below this line.
  313. *
  314. * Please keep everything fully self-contained and annotated with
  315. * __efi_runtime and __efi_runtime_data markers.
  316. */
  317. /*
  318. * Relocate the EFI runtime stub to a different place. We need to call this
  319. * the first time we expose the runtime interface to a user and on set virtual
  320. * address map calls.
  321. */
  322. static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
  323. {
  324. return EFI_UNSUPPORTED;
  325. }
  326. static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
  327. {
  328. return EFI_DEVICE_ERROR;
  329. }
  330. static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
  331. {
  332. return EFI_INVALID_PARAMETER;
  333. }
  334. efi_status_t __efi_runtime EFIAPI efi_update_capsule(
  335. struct efi_capsule_header **capsule_header_array,
  336. efi_uintn_t capsule_count,
  337. u64 scatter_gather_list)
  338. {
  339. return EFI_UNSUPPORTED;
  340. }
  341. efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
  342. struct efi_capsule_header **capsule_header_array,
  343. efi_uintn_t capsule_count,
  344. u64 maximum_capsule_size,
  345. u32 reset_type)
  346. {
  347. return EFI_UNSUPPORTED;
  348. }
  349. efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
  350. u32 attributes,
  351. u64 maximum_variable_storage_size,
  352. u64 remaining_variable_storage_size,
  353. u64 maximum_variable_size)
  354. {
  355. return EFI_UNSUPPORTED;
  356. }
  357. struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
  358. .hdr = {
  359. .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
  360. .revision = EFI_RUNTIME_SERVICES_REVISION,
  361. .headersize = sizeof(struct efi_table_hdr),
  362. },
  363. .get_time = &efi_get_time_boottime,
  364. .set_time = (void *)&efi_device_error,
  365. .get_wakeup_time = (void *)&efi_unimplemented,
  366. .set_wakeup_time = (void *)&efi_unimplemented,
  367. .set_virtual_address_map = &efi_set_virtual_address_map,
  368. .convert_pointer = (void *)&efi_invalid_parameter,
  369. .get_variable = efi_get_variable,
  370. .get_next_variable = efi_get_next_variable,
  371. .set_variable = efi_set_variable,
  372. .get_next_high_mono_count = (void *)&efi_device_error,
  373. .reset_system = &efi_reset_system_boottime,
  374. .update_capsule = efi_update_capsule,
  375. .query_capsule_caps = efi_query_capsule_caps,
  376. .query_variable_info = efi_query_variable_info,
  377. };