efi_runtime.c 12 KB

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