efi_runtime.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. };
  111. static bool efi_runtime_tobedetached(void *p)
  112. {
  113. int i;
  114. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
  115. if (efi_runtime_detach_list[i].ptr == p)
  116. return true;
  117. return false;
  118. }
  119. static void efi_runtime_detach(ulong offset)
  120. {
  121. int i;
  122. ulong patchoff = offset - (ulong)gd->relocaddr;
  123. for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
  124. ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
  125. ulong *p = efi_runtime_detach_list[i].ptr;
  126. ulong newaddr = patchto ? (patchto + patchoff) : 0;
  127. #ifdef DEBUG_EFI
  128. printf("%s: Setting %p to %lx\n", __func__, p, newaddr);
  129. #endif
  130. *p = newaddr;
  131. }
  132. }
  133. /* Relocate EFI runtime to uboot_reloc_base = offset */
  134. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
  135. {
  136. #ifdef IS_RELA
  137. struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
  138. #else
  139. struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
  140. static ulong lastoff = CONFIG_SYS_TEXT_BASE;
  141. #endif
  142. #ifdef DEBUG_EFI
  143. printf("%s: Relocating to offset=%lx\n", __func__, offset);
  144. #endif
  145. for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
  146. ulong base = CONFIG_SYS_TEXT_BASE;
  147. ulong *p;
  148. ulong newaddr;
  149. p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
  150. if ((rel->info & R_MASK) != R_RELATIVE) {
  151. continue;
  152. }
  153. #ifdef IS_RELA
  154. newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
  155. #else
  156. newaddr = *p - lastoff + offset;
  157. #endif
  158. /* Check if the relocation is inside bounds */
  159. if (map && ((newaddr < map->virtual_start) ||
  160. newaddr > (map->virtual_start + (map->num_pages << 12)))) {
  161. if (!efi_runtime_tobedetached(p))
  162. printf("U-Boot EFI: Relocation at %p is out of "
  163. "range (%lx)\n", p, newaddr);
  164. continue;
  165. }
  166. #ifdef DEBUG_EFI
  167. printf("%s: Setting %p to %lx\n", __func__, p, newaddr);
  168. #endif
  169. *p = newaddr;
  170. flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
  171. ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
  172. }
  173. #ifndef IS_RELA
  174. lastoff = offset;
  175. #endif
  176. invalidate_icache_all();
  177. }
  178. static efi_status_t EFIAPI efi_set_virtual_address_map(
  179. unsigned long memory_map_size,
  180. unsigned long descriptor_size,
  181. uint32_t descriptor_version,
  182. struct efi_mem_desc *virtmap)
  183. {
  184. ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL;
  185. int n = memory_map_size / descriptor_size;
  186. int i;
  187. EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
  188. descriptor_version, virtmap);
  189. for (i = 0; i < n; i++) {
  190. struct efi_mem_desc *map;
  191. map = (void*)virtmap + (descriptor_size * i);
  192. if (map->type == EFI_RUNTIME_SERVICES_CODE) {
  193. ulong new_offset = map->virtual_start - (runtime_start - gd->relocaddr);
  194. efi_runtime_relocate(new_offset, map);
  195. /* Once we're virtual, we can no longer handle
  196. complex callbacks */
  197. efi_runtime_detach(new_offset);
  198. return EFI_EXIT(EFI_SUCCESS);
  199. }
  200. }
  201. return EFI_EXIT(EFI_INVALID_PARAMETER);
  202. }
  203. /*
  204. * In the second stage, U-Boot has disappeared. To isolate our runtime code
  205. * that at this point still exists from the rest, we put it into a special
  206. * section.
  207. *
  208. * !!WARNING!!
  209. *
  210. * This means that we can not rely on any code outside of this file in any
  211. * function or variable below this line.
  212. *
  213. * Please keep everything fully self-contained and annotated with
  214. * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers.
  215. */
  216. /*
  217. * Relocate the EFI runtime stub to a different place. We need to call this
  218. * the first time we expose the runtime interface to a user and on set virtual
  219. * address map calls.
  220. */
  221. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void)
  222. {
  223. return EFI_UNSUPPORTED;
  224. }
  225. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void)
  226. {
  227. return EFI_DEVICE_ERROR;
  228. }
  229. static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void)
  230. {
  231. return EFI_INVALID_PARAMETER;
  232. }
  233. struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = {
  234. .hdr = {
  235. .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
  236. .revision = EFI_RUNTIME_SERVICES_REVISION,
  237. .headersize = sizeof(struct efi_table_hdr),
  238. },
  239. .get_time = &efi_get_time,
  240. .set_time = (void *)&efi_device_error,
  241. .get_wakeup_time = (void *)&efi_unimplemented,
  242. .set_wakeup_time = (void *)&efi_unimplemented,
  243. .set_virtual_address_map = &efi_set_virtual_address_map,
  244. .convert_pointer = (void *)&efi_invalid_parameter,
  245. .get_variable = (void *)&efi_device_error,
  246. .get_next_variable = (void *)&efi_device_error,
  247. .set_variable = (void *)&efi_device_error,
  248. .get_next_high_mono_count = (void *)&efi_device_error,
  249. .reset_system = &efi_reset_system,
  250. };