efi_runtime.c 7.0 KB

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