efi_runtime.c 11 KB

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