efi_runtime.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void);
  17. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void);
  18. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void);
  19. #ifdef CONFIG_SYS_CACHELINE_SIZE
  20. #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
  21. #else
  22. /* Just use the greatest cache flush alignment requirement I'm aware of */
  23. #define EFI_CACHELINE_SIZE 128
  24. #endif
  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. #else
  33. #error Need to add relocation awareness
  34. #endif
  35. struct elf_rel {
  36. ulong *offset;
  37. ulong info;
  38. };
  39. struct elf_rela {
  40. ulong *offset;
  41. ulong info;
  42. long addend;
  43. };
  44. /*
  45. * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
  46. * payload are running concurrently at the same time. In this mode, we can
  47. * handle a good number of runtime callbacks
  48. */
  49. static void EFIAPI efi_reset_system(enum efi_reset_type reset_type,
  50. efi_status_t reset_status,
  51. unsigned long data_size, void *reset_data)
  52. {
  53. EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
  54. reset_data);
  55. switch (reset_type) {
  56. case EFI_RESET_COLD:
  57. case EFI_RESET_WARM:
  58. do_reset(NULL, 0, 0, NULL);
  59. break;
  60. case EFI_RESET_SHUTDOWN:
  61. /* We don't have anything to map this to */
  62. break;
  63. }
  64. EFI_EXIT(EFI_SUCCESS);
  65. }
  66. static efi_status_t EFIAPI efi_get_time(struct efi_time *time,
  67. struct efi_time_cap *capabilities)
  68. {
  69. #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
  70. struct rtc_time tm;
  71. int r;
  72. struct udevice *dev;
  73. EFI_ENTRY("%p %p", time, capabilities);
  74. r = uclass_get_device(UCLASS_RTC, 0, &dev);
  75. if (r)
  76. return EFI_EXIT(EFI_DEVICE_ERROR);
  77. r = dm_rtc_get(dev, &tm);
  78. if (r)
  79. return EFI_EXIT(EFI_DEVICE_ERROR);
  80. memset(time, 0, sizeof(*time));
  81. time->year = tm.tm_year;
  82. time->month = tm.tm_mon;
  83. time->day = tm.tm_mday;
  84. time->hour = tm.tm_hour;
  85. time->minute = tm.tm_min;
  86. time->daylight = tm.tm_isdst;
  87. return EFI_EXIT(EFI_SUCCESS);
  88. #else
  89. return EFI_DEVICE_ERROR;
  90. #endif
  91. }
  92. struct efi_runtime_detach_list_struct {
  93. void *ptr;
  94. void *patchto;
  95. };
  96. static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
  97. {
  98. /* do_reset is gone */
  99. .ptr = &efi_runtime_services.reset_system,
  100. .patchto = NULL,
  101. }, {
  102. /* invalidate_*cache_all are gone */
  103. .ptr = &efi_runtime_services.set_virtual_address_map,
  104. .patchto = &efi_invalid_parameter,
  105. }, {
  106. /* RTC accessors are gone */
  107. .ptr = &efi_runtime_services.get_time,
  108. .patchto = &efi_device_error,
  109. }, {
  110. /* Clean up system table */
  111. .ptr = &systab.con_in,
  112. .patchto = NULL,
  113. }, {
  114. /* Clean up system table */
  115. .ptr = &systab.con_out,
  116. .patchto = NULL,
  117. }, {
  118. /* Clean up system table */
  119. .ptr = &systab.std_err,
  120. .patchto = NULL,
  121. }, {
  122. /* Clean up system table */
  123. .ptr = &systab.boottime,
  124. .patchto = NULL,
  125. },
  126. };
  127. static bool efi_runtime_tobedetached(void *p)
  128. {
  129. int i;
  130. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
  131. if (efi_runtime_detach_list[i].ptr == p)
  132. return true;
  133. return false;
  134. }
  135. static void efi_runtime_detach(ulong offset)
  136. {
  137. int i;
  138. ulong patchoff = offset - (ulong)gd->relocaddr;
  139. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
  140. ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
  141. ulong *p = efi_runtime_detach_list[i].ptr;
  142. ulong newaddr = patchto ? (patchto + patchoff) : 0;
  143. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  144. *p = newaddr;
  145. }
  146. }
  147. /* Relocate EFI runtime to uboot_reloc_base = offset */
  148. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
  149. {
  150. #ifdef IS_RELA
  151. struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
  152. #else
  153. struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
  154. static ulong lastoff = CONFIG_SYS_TEXT_BASE;
  155. #endif
  156. debug("%s: Relocating to offset=%lx\n", __func__, offset);
  157. for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
  158. ulong base = CONFIG_SYS_TEXT_BASE;
  159. ulong *p;
  160. ulong newaddr;
  161. p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
  162. if ((rel->info & R_MASK) != R_RELATIVE) {
  163. continue;
  164. }
  165. #ifdef IS_RELA
  166. newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
  167. #else
  168. newaddr = *p - lastoff + offset;
  169. #endif
  170. /* Check if the relocation is inside bounds */
  171. if (map && ((newaddr < map->virtual_start) ||
  172. newaddr > (map->virtual_start + (map->num_pages << 12)))) {
  173. if (!efi_runtime_tobedetached(p))
  174. printf("U-Boot EFI: Relocation at %p is out of "
  175. "range (%lx)\n", p, newaddr);
  176. continue;
  177. }
  178. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  179. *p = newaddr;
  180. flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
  181. ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
  182. }
  183. #ifndef IS_RELA
  184. lastoff = offset;
  185. #endif
  186. invalidate_icache_all();
  187. }
  188. static efi_status_t EFIAPI efi_set_virtual_address_map(
  189. unsigned long memory_map_size,
  190. unsigned long descriptor_size,
  191. uint32_t descriptor_version,
  192. struct efi_mem_desc *virtmap)
  193. {
  194. ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL;
  195. int n = memory_map_size / descriptor_size;
  196. int i;
  197. EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
  198. descriptor_version, virtmap);
  199. for (i = 0; i < n; i++) {
  200. struct efi_mem_desc *map;
  201. map = (void*)virtmap + (descriptor_size * i);
  202. if (map->type == EFI_RUNTIME_SERVICES_CODE) {
  203. ulong new_offset = map->virtual_start - (runtime_start - gd->relocaddr);
  204. efi_runtime_relocate(new_offset, map);
  205. /* Once we're virtual, we can no longer handle
  206. complex callbacks */
  207. efi_runtime_detach(new_offset);
  208. return EFI_EXIT(EFI_SUCCESS);
  209. }
  210. }
  211. return EFI_EXIT(EFI_INVALID_PARAMETER);
  212. }
  213. /*
  214. * In the second stage, U-Boot has disappeared. To isolate our runtime code
  215. * that at this point still exists from the rest, we put it into a special
  216. * section.
  217. *
  218. * !!WARNING!!
  219. *
  220. * This means that we can not rely on any code outside of this file in any
  221. * function or variable below this line.
  222. *
  223. * Please keep everything fully self-contained and annotated with
  224. * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers.
  225. */
  226. /*
  227. * Relocate the EFI runtime stub to a different place. We need to call this
  228. * the first time we expose the runtime interface to a user and on set virtual
  229. * address map calls.
  230. */
  231. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void)
  232. {
  233. return EFI_UNSUPPORTED;
  234. }
  235. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void)
  236. {
  237. return EFI_DEVICE_ERROR;
  238. }
  239. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void)
  240. {
  241. return EFI_INVALID_PARAMETER;
  242. }
  243. struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = {
  244. .hdr = {
  245. .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
  246. .revision = EFI_RUNTIME_SERVICES_REVISION,
  247. .headersize = sizeof(struct efi_table_hdr),
  248. },
  249. .get_time = &efi_get_time,
  250. .set_time = (void *)&efi_device_error,
  251. .get_wakeup_time = (void *)&efi_unimplemented,
  252. .set_wakeup_time = (void *)&efi_unimplemented,
  253. .set_virtual_address_map = &efi_set_virtual_address_map,
  254. .convert_pointer = (void *)&efi_invalid_parameter,
  255. .get_variable = (void *)&efi_device_error,
  256. .get_next_variable = (void *)&efi_device_error,
  257. .set_variable = (void *)&efi_device_error,
  258. .get_next_high_mono_count = (void *)&efi_device_error,
  259. .reset_system = &efi_reset_system,
  260. };