efi_runtime.c 12 KB

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