efi_runtime.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 <cpu_func.h>
  10. #include <dm.h>
  11. #include <elf.h>
  12. #include <efi_loader.h>
  13. #include <log.h>
  14. #include <malloc.h>
  15. #include <rtc.h>
  16. #include <asm/global_data.h>
  17. #include <u-boot/crc.h>
  18. /* For manual relocation support */
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /* GUID of the runtime properties table */
  21. static const efi_guid_t efi_rt_properties_table_guid =
  22. EFI_RT_PROPERTIES_TABLE_GUID;
  23. struct efi_runtime_mmio_list {
  24. struct list_head link;
  25. void **ptr;
  26. u64 paddr;
  27. u64 len;
  28. };
  29. /* This list contains all runtime available mmio regions */
  30. LIST_HEAD(efi_runtime_mmio);
  31. static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
  32. /*
  33. * TODO(sjg@chromium.org): These defines and structures should come from the ELF
  34. * header for each architecture (or a generic header) rather than being repeated
  35. * here.
  36. */
  37. #if defined(__aarch64__)
  38. #define R_RELATIVE R_AARCH64_RELATIVE
  39. #define R_MASK 0xffffffffULL
  40. #define IS_RELA 1
  41. #elif defined(__arm__)
  42. #define R_RELATIVE R_ARM_RELATIVE
  43. #define R_MASK 0xffULL
  44. #elif defined(__i386__)
  45. #define R_RELATIVE R_386_RELATIVE
  46. #define R_MASK 0xffULL
  47. #elif defined(__x86_64__)
  48. #define R_RELATIVE R_X86_64_RELATIVE
  49. #define R_MASK 0xffffffffULL
  50. #define IS_RELA 1
  51. #elif defined(__riscv)
  52. #define R_RELATIVE R_RISCV_RELATIVE
  53. #define R_MASK 0xffULL
  54. #define IS_RELA 1
  55. struct dyn_sym {
  56. ulong foo1;
  57. ulong addr;
  58. u32 foo2;
  59. u32 foo3;
  60. };
  61. #if (__riscv_xlen == 32)
  62. #define R_ABSOLUTE R_RISCV_32
  63. #define SYM_INDEX 8
  64. #elif (__riscv_xlen == 64)
  65. #define R_ABSOLUTE R_RISCV_64
  66. #define SYM_INDEX 32
  67. #else
  68. #error unknown riscv target
  69. #endif
  70. #else
  71. #error Need to add relocation awareness
  72. #endif
  73. struct elf_rel {
  74. ulong *offset;
  75. ulong info;
  76. };
  77. struct elf_rela {
  78. ulong *offset;
  79. ulong info;
  80. long addend;
  81. };
  82. static __efi_runtime_data struct efi_mem_desc *efi_virtmap;
  83. static __efi_runtime_data efi_uintn_t efi_descriptor_count;
  84. static __efi_runtime_data efi_uintn_t efi_descriptor_size;
  85. /*
  86. * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
  87. * payload are running concurrently at the same time. In this mode, we can
  88. * handle a good number of runtime callbacks
  89. */
  90. /**
  91. * efi_init_runtime_supported() - create runtime properties table
  92. *
  93. * Create a configuration table specifying which services are available at
  94. * runtime.
  95. *
  96. * Return: status code
  97. */
  98. efi_status_t efi_init_runtime_supported(void)
  99. {
  100. efi_status_t ret;
  101. struct efi_rt_properties_table *rt_table;
  102. ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
  103. sizeof(struct efi_rt_properties_table),
  104. (void **)&rt_table);
  105. if (ret != EFI_SUCCESS)
  106. return ret;
  107. rt_table->version = EFI_RT_PROPERTIES_TABLE_VERSION;
  108. rt_table->length = sizeof(struct efi_rt_properties_table);
  109. rt_table->runtime_services_supported =
  110. EFI_RT_SUPPORTED_GET_VARIABLE |
  111. EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME |
  112. EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
  113. EFI_RT_SUPPORTED_CONVERT_POINTER;
  114. /*
  115. * This value must be synced with efi_runtime_detach_list
  116. * as well as efi_runtime_services.
  117. */
  118. #ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
  119. rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
  120. #endif
  121. ret = efi_install_configuration_table(&efi_rt_properties_table_guid,
  122. rt_table);
  123. return ret;
  124. }
  125. /**
  126. * efi_memcpy_runtime() - copy memory area
  127. *
  128. * At runtime memcpy() is not available.
  129. *
  130. * Overlapping memory areas can be copied safely if src >= dest.
  131. *
  132. * @dest: destination buffer
  133. * @src: source buffer
  134. * @n: number of bytes to copy
  135. * Return: pointer to destination buffer
  136. */
  137. void __efi_runtime efi_memcpy_runtime(void *dest, const void *src, size_t n)
  138. {
  139. u8 *d = dest;
  140. const u8 *s = src;
  141. for (; n; --n)
  142. *d++ = *s++;
  143. }
  144. /**
  145. * efi_update_table_header_crc32() - Update crc32 in table header
  146. *
  147. * @table: EFI table
  148. */
  149. void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
  150. {
  151. table->crc32 = 0;
  152. table->crc32 = crc32(0, (const unsigned char *)table,
  153. table->headersize);
  154. }
  155. /**
  156. * efi_reset_system_boottime() - reset system at boot time
  157. *
  158. * This function implements the ResetSystem() runtime service before
  159. * SetVirtualAddressMap() is called.
  160. *
  161. * See the Unified Extensible Firmware Interface (UEFI) specification for
  162. * details.
  163. *
  164. * @reset_type: type of reset to perform
  165. * @reset_status: status code for the reset
  166. * @data_size: size of reset_data
  167. * @reset_data: information about the reset
  168. */
  169. static void EFIAPI efi_reset_system_boottime(
  170. enum efi_reset_type reset_type,
  171. efi_status_t reset_status,
  172. unsigned long data_size, void *reset_data)
  173. {
  174. struct efi_event *evt;
  175. EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
  176. reset_data);
  177. /* Notify reset */
  178. list_for_each_entry(evt, &efi_events, link) {
  179. if (evt->group &&
  180. !guidcmp(evt->group,
  181. &efi_guid_event_group_reset_system)) {
  182. efi_signal_event(evt);
  183. break;
  184. }
  185. }
  186. switch (reset_type) {
  187. case EFI_RESET_COLD:
  188. case EFI_RESET_WARM:
  189. case EFI_RESET_PLATFORM_SPECIFIC:
  190. do_reset(NULL, 0, 0, NULL);
  191. break;
  192. case EFI_RESET_SHUTDOWN:
  193. #ifdef CONFIG_CMD_POWEROFF
  194. do_poweroff(NULL, 0, 0, NULL);
  195. #endif
  196. break;
  197. }
  198. while (1) { }
  199. }
  200. /**
  201. * efi_get_time_boottime() - get current time at boot time
  202. *
  203. * This function implements the GetTime runtime service before
  204. * SetVirtualAddressMap() is called.
  205. *
  206. * See the Unified Extensible Firmware Interface (UEFI) specification
  207. * for details.
  208. *
  209. * @time: pointer to structure to receive current time
  210. * @capabilities: pointer to structure to receive RTC properties
  211. * Returns: status code
  212. */
  213. static efi_status_t EFIAPI efi_get_time_boottime(
  214. struct efi_time *time,
  215. struct efi_time_cap *capabilities)
  216. {
  217. #ifdef CONFIG_EFI_GET_TIME
  218. efi_status_t ret = EFI_SUCCESS;
  219. struct rtc_time tm;
  220. struct udevice *dev;
  221. EFI_ENTRY("%p %p", time, capabilities);
  222. if (!time) {
  223. ret = EFI_INVALID_PARAMETER;
  224. goto out;
  225. }
  226. if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
  227. dm_rtc_get(dev, &tm)) {
  228. ret = EFI_UNSUPPORTED;
  229. goto out;
  230. }
  231. if (dm_rtc_get(dev, &tm)) {
  232. ret = EFI_DEVICE_ERROR;
  233. goto out;
  234. }
  235. memset(time, 0, sizeof(*time));
  236. time->year = tm.tm_year;
  237. time->month = tm.tm_mon;
  238. time->day = tm.tm_mday;
  239. time->hour = tm.tm_hour;
  240. time->minute = tm.tm_min;
  241. time->second = tm.tm_sec;
  242. if (tm.tm_isdst > 0)
  243. time->daylight =
  244. EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
  245. else if (!tm.tm_isdst)
  246. time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
  247. else
  248. time->daylight = 0;
  249. time->timezone = EFI_UNSPECIFIED_TIMEZONE;
  250. if (capabilities) {
  251. /* Set reasonable dummy values */
  252. capabilities->resolution = 1; /* 1 Hz */
  253. capabilities->accuracy = 100000000; /* 100 ppm */
  254. capabilities->sets_to_zero = false;
  255. }
  256. out:
  257. return EFI_EXIT(ret);
  258. #else
  259. EFI_ENTRY("%p %p", time, capabilities);
  260. return EFI_EXIT(EFI_UNSUPPORTED);
  261. #endif
  262. }
  263. #ifdef CONFIG_EFI_SET_TIME
  264. /**
  265. * efi_validate_time() - checks if timestamp is valid
  266. *
  267. * @time: timestamp to validate
  268. * Returns: 0 if timestamp is valid, 1 otherwise
  269. */
  270. static int efi_validate_time(struct efi_time *time)
  271. {
  272. return (!time ||
  273. time->year < 1900 || time->year > 9999 ||
  274. !time->month || time->month > 12 || !time->day ||
  275. time->day > rtc_month_days(time->month - 1, time->year) ||
  276. time->hour > 23 || time->minute > 59 || time->second > 59 ||
  277. time->nanosecond > 999999999 ||
  278. time->daylight &
  279. ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
  280. ((time->timezone < -1440 || time->timezone > 1440) &&
  281. time->timezone != EFI_UNSPECIFIED_TIMEZONE));
  282. }
  283. #endif
  284. /**
  285. * efi_set_time_boottime() - set current time
  286. *
  287. * This function implements the SetTime() runtime service before
  288. * SetVirtualAddressMap() is called.
  289. *
  290. * See the Unified Extensible Firmware Interface (UEFI) specification
  291. * for details.
  292. *
  293. * @time: pointer to structure to with current time
  294. * Returns: status code
  295. */
  296. static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
  297. {
  298. #ifdef CONFIG_EFI_SET_TIME
  299. efi_status_t ret = EFI_SUCCESS;
  300. struct rtc_time tm;
  301. struct udevice *dev;
  302. EFI_ENTRY("%p", time);
  303. if (efi_validate_time(time)) {
  304. ret = EFI_INVALID_PARAMETER;
  305. goto out;
  306. }
  307. if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
  308. ret = EFI_UNSUPPORTED;
  309. goto out;
  310. }
  311. memset(&tm, 0, sizeof(tm));
  312. tm.tm_year = time->year;
  313. tm.tm_mon = time->month;
  314. tm.tm_mday = time->day;
  315. tm.tm_hour = time->hour;
  316. tm.tm_min = time->minute;
  317. tm.tm_sec = time->second;
  318. switch (time->daylight) {
  319. case EFI_TIME_ADJUST_DAYLIGHT:
  320. tm.tm_isdst = 0;
  321. break;
  322. case EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT:
  323. tm.tm_isdst = 1;
  324. break;
  325. default:
  326. tm.tm_isdst = -1;
  327. break;
  328. }
  329. /* Calculate day of week */
  330. rtc_calc_weekday(&tm);
  331. if (dm_rtc_set(dev, &tm))
  332. ret = EFI_DEVICE_ERROR;
  333. out:
  334. return EFI_EXIT(ret);
  335. #else
  336. EFI_ENTRY("%p", time);
  337. return EFI_EXIT(EFI_UNSUPPORTED);
  338. #endif
  339. }
  340. /**
  341. * efi_reset_system() - reset system
  342. *
  343. * This function implements the ResetSystem() runtime service after
  344. * SetVirtualAddressMap() is called. As this placeholder cannot reset the
  345. * system it simply return to the caller.
  346. *
  347. * Boards may override the helpers below to implement reset functionality.
  348. *
  349. * See the Unified Extensible Firmware Interface (UEFI) specification for
  350. * details.
  351. *
  352. * @reset_type: type of reset to perform
  353. * @reset_status: status code for the reset
  354. * @data_size: size of reset_data
  355. * @reset_data: information about the reset
  356. */
  357. void __weak __efi_runtime EFIAPI efi_reset_system(
  358. enum efi_reset_type reset_type,
  359. efi_status_t reset_status,
  360. unsigned long data_size, void *reset_data)
  361. {
  362. return;
  363. }
  364. /**
  365. * efi_reset_system_init() - initialize the reset driver
  366. *
  367. * Boards may override this function to initialize the reset driver.
  368. */
  369. efi_status_t __weak efi_reset_system_init(void)
  370. {
  371. return EFI_SUCCESS;
  372. }
  373. /**
  374. * efi_get_time() - get current time
  375. *
  376. * This function implements the GetTime runtime service after
  377. * SetVirtualAddressMap() is called. As the U-Boot driver are not available
  378. * anymore only an error code is returned.
  379. *
  380. * See the Unified Extensible Firmware Interface (UEFI) specification
  381. * for details.
  382. *
  383. * @time: pointer to structure to receive current time
  384. * @capabilities: pointer to structure to receive RTC properties
  385. * Returns: status code
  386. */
  387. efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
  388. struct efi_time *time,
  389. struct efi_time_cap *capabilities)
  390. {
  391. return EFI_UNSUPPORTED;
  392. }
  393. /**
  394. * efi_set_time() - set current time
  395. *
  396. * This function implements the SetTime runtime service after
  397. * SetVirtualAddressMap() is called. As the U-Boot driver are not available
  398. * anymore only an error code is returned.
  399. *
  400. * See the Unified Extensible Firmware Interface (UEFI) specification
  401. * for details.
  402. *
  403. * @time: pointer to structure to with current time
  404. * Returns: status code
  405. */
  406. efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
  407. {
  408. return EFI_UNSUPPORTED;
  409. }
  410. /**
  411. * efi_update_capsule_unsupported() - process information from operating system
  412. *
  413. * This function implements the UpdateCapsule() runtime service.
  414. *
  415. * See the Unified Extensible Firmware Interface (UEFI) specification for
  416. * details.
  417. *
  418. * @capsule_header_array: pointer to array of virtual pointers
  419. * @capsule_count: number of pointers in capsule_header_array
  420. * @scatter_gather_list: pointer to array of physical pointers
  421. * Returns: status code
  422. */
  423. efi_status_t __efi_runtime EFIAPI efi_update_capsule_unsupported(
  424. struct efi_capsule_header **capsule_header_array,
  425. efi_uintn_t capsule_count,
  426. u64 scatter_gather_list)
  427. {
  428. return EFI_UNSUPPORTED;
  429. }
  430. /**
  431. * efi_query_capsule_caps_unsupported() - check if capsule is supported
  432. *
  433. * This function implements the QueryCapsuleCapabilities() runtime service.
  434. *
  435. * See the Unified Extensible Firmware Interface (UEFI) specification for
  436. * details.
  437. *
  438. * @capsule_header_array: pointer to array of virtual pointers
  439. * @capsule_count: number of pointers in capsule_header_array
  440. * @maximum_capsule_size: maximum capsule size
  441. * @reset_type: type of reset needed for capsule update
  442. * Returns: status code
  443. */
  444. efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps_unsupported(
  445. struct efi_capsule_header **capsule_header_array,
  446. efi_uintn_t capsule_count,
  447. u64 *maximum_capsule_size,
  448. u32 *reset_type)
  449. {
  450. return EFI_UNSUPPORTED;
  451. }
  452. /**
  453. * efi_is_runtime_service_pointer() - check if pointer points to runtime table
  454. *
  455. * @p: pointer to check
  456. * Return: true if the pointer points to a service function pointer in the
  457. * runtime table
  458. */
  459. static bool efi_is_runtime_service_pointer(void *p)
  460. {
  461. return (p >= (void *)&efi_runtime_services.get_time &&
  462. p <= (void *)&efi_runtime_services.query_variable_info) ||
  463. p == (void *)&efi_events.prev ||
  464. p == (void *)&efi_events.next;
  465. }
  466. /**
  467. * efi_runtime_detach() - detach unimplemented runtime functions
  468. */
  469. void efi_runtime_detach(void)
  470. {
  471. efi_runtime_services.reset_system = efi_reset_system;
  472. efi_runtime_services.get_time = efi_get_time;
  473. efi_runtime_services.set_time = efi_set_time;
  474. if (IS_ENABLED(CONFIG_EFI_RUNTIME_UPDATE_CAPSULE)) {
  475. /* won't support at runtime */
  476. efi_runtime_services.update_capsule =
  477. efi_update_capsule_unsupported;
  478. efi_runtime_services.query_capsule_caps =
  479. efi_query_capsule_caps_unsupported;
  480. }
  481. /* Update CRC32 */
  482. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  483. }
  484. /**
  485. * efi_set_virtual_address_map_runtime() - change from physical to virtual
  486. * mapping
  487. *
  488. * This function implements the SetVirtualAddressMap() runtime service after
  489. * it is first called.
  490. *
  491. * See the Unified Extensible Firmware Interface (UEFI) specification for
  492. * details.
  493. *
  494. * @memory_map_size: size of the virtual map
  495. * @descriptor_size: size of an entry in the map
  496. * @descriptor_version: version of the map entries
  497. * @virtmap: virtual address mapping information
  498. * Return: status code EFI_UNSUPPORTED
  499. */
  500. static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
  501. efi_uintn_t memory_map_size,
  502. efi_uintn_t descriptor_size,
  503. uint32_t descriptor_version,
  504. struct efi_mem_desc *virtmap)
  505. {
  506. return EFI_UNSUPPORTED;
  507. }
  508. /**
  509. * efi_convert_pointer_runtime() - convert from physical to virtual pointer
  510. *
  511. * This function implements the ConvertPointer() runtime service after
  512. * the first call to SetVirtualAddressMap().
  513. *
  514. * See the Unified Extensible Firmware Interface (UEFI) specification for
  515. * details.
  516. *
  517. * @debug_disposition: indicates if pointer may be converted to NULL
  518. * @address: pointer to be converted
  519. * Return: status code EFI_UNSUPPORTED
  520. */
  521. static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
  522. efi_uintn_t debug_disposition, void **address)
  523. {
  524. return EFI_UNSUPPORTED;
  525. }
  526. /**
  527. * efi_convert_pointer() - convert from physical to virtual pointer
  528. *
  529. * This function implements the ConvertPointer() runtime service until
  530. * the first call to SetVirtualAddressMap().
  531. *
  532. * See the Unified Extensible Firmware Interface (UEFI) specification for
  533. * details.
  534. *
  535. * @debug_disposition: indicates if pointer may be converted to NULL
  536. * @address: pointer to be converted
  537. * Return: status code
  538. */
  539. __efi_runtime efi_status_t EFIAPI
  540. efi_convert_pointer(efi_uintn_t debug_disposition, void **address)
  541. {
  542. efi_physical_addr_t addr;
  543. efi_uintn_t i;
  544. efi_status_t ret = EFI_NOT_FOUND;
  545. if (!efi_virtmap) {
  546. ret = EFI_UNSUPPORTED;
  547. goto out;
  548. }
  549. if (!address) {
  550. ret = EFI_INVALID_PARAMETER;
  551. goto out;
  552. }
  553. if (!*address) {
  554. if (debug_disposition & EFI_OPTIONAL_PTR)
  555. return EFI_SUCCESS;
  556. else
  557. return EFI_INVALID_PARAMETER;
  558. }
  559. addr = (uintptr_t)*address;
  560. for (i = 0; i < efi_descriptor_count; i++) {
  561. struct efi_mem_desc *map = (void *)efi_virtmap +
  562. (efi_descriptor_size * i);
  563. if (addr >= map->physical_start &&
  564. (addr < map->physical_start
  565. + (map->num_pages << EFI_PAGE_SHIFT))) {
  566. *address = (void *)(uintptr_t)
  567. (addr + map->virtual_start -
  568. map->physical_start);
  569. ret = EFI_SUCCESS;
  570. break;
  571. }
  572. }
  573. out:
  574. return ret;
  575. }
  576. static __efi_runtime void efi_relocate_runtime_table(ulong offset)
  577. {
  578. ulong patchoff;
  579. void **pos;
  580. /* Relocate the runtime services pointers */
  581. patchoff = offset - gd->relocaddr;
  582. for (pos = (void **)&efi_runtime_services.get_time;
  583. pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
  584. if (*pos)
  585. *pos += patchoff;
  586. }
  587. /*
  588. * The entry for SetVirtualAddress() must point to a physical address.
  589. * After the first execution the service must return EFI_UNSUPPORTED.
  590. */
  591. efi_runtime_services.set_virtual_address_map =
  592. &efi_set_virtual_address_map_runtime;
  593. /*
  594. * The entry for ConvertPointer() must point to a physical address.
  595. * The service is not usable after SetVirtualAddress().
  596. */
  597. efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
  598. /*
  599. * TODO: Update UEFI variable RuntimeServicesSupported removing flags
  600. * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
  601. * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
  602. */
  603. /* Update CRC32 */
  604. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  605. }
  606. /* Relocate EFI runtime to uboot_reloc_base = offset */
  607. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
  608. {
  609. #ifdef IS_RELA
  610. struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
  611. #else
  612. struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
  613. static ulong lastoff = CONFIG_SYS_TEXT_BASE;
  614. #endif
  615. debug("%s: Relocating to offset=%lx\n", __func__, offset);
  616. for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
  617. ulong base = CONFIG_SYS_TEXT_BASE;
  618. ulong *p;
  619. ulong newaddr;
  620. p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
  621. /*
  622. * The runtime services table is updated in
  623. * efi_relocate_runtime_table()
  624. */
  625. if (map && efi_is_runtime_service_pointer(p))
  626. continue;
  627. debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
  628. rel->info, *p, rel->offset);
  629. switch (rel->info & R_MASK) {
  630. case R_RELATIVE:
  631. #ifdef IS_RELA
  632. newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
  633. #else
  634. newaddr = *p - lastoff + offset;
  635. #endif
  636. break;
  637. #ifdef R_ABSOLUTE
  638. case R_ABSOLUTE: {
  639. ulong symidx = rel->info >> SYM_INDEX;
  640. extern struct dyn_sym __dyn_sym_start[];
  641. newaddr = __dyn_sym_start[symidx].addr + offset;
  642. #ifdef IS_RELA
  643. newaddr -= CONFIG_SYS_TEXT_BASE;
  644. #endif
  645. break;
  646. }
  647. #endif
  648. default:
  649. printf("%s: Unknown relocation type %llx\n",
  650. __func__, rel->info & R_MASK);
  651. continue;
  652. }
  653. /* Check if the relocation is inside bounds */
  654. if (map && ((newaddr < map->virtual_start) ||
  655. newaddr > (map->virtual_start +
  656. (map->num_pages << EFI_PAGE_SHIFT)))) {
  657. printf("%s: Relocation at %p is out of range (%lx)\n",
  658. __func__, p, newaddr);
  659. continue;
  660. }
  661. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  662. *p = newaddr;
  663. flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
  664. ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
  665. }
  666. #ifndef IS_RELA
  667. lastoff = offset;
  668. #endif
  669. invalidate_icache_all();
  670. }
  671. /**
  672. * efi_set_virtual_address_map() - change from physical to virtual mapping
  673. *
  674. * This function implements the SetVirtualAddressMap() runtime service.
  675. *
  676. * See the Unified Extensible Firmware Interface (UEFI) specification for
  677. * details.
  678. *
  679. * @memory_map_size: size of the virtual map
  680. * @descriptor_size: size of an entry in the map
  681. * @descriptor_version: version of the map entries
  682. * @virtmap: virtual address mapping information
  683. * Return: status code
  684. */
  685. static efi_status_t EFIAPI efi_set_virtual_address_map(
  686. efi_uintn_t memory_map_size,
  687. efi_uintn_t descriptor_size,
  688. uint32_t descriptor_version,
  689. struct efi_mem_desc *virtmap)
  690. {
  691. efi_uintn_t n = memory_map_size / descriptor_size;
  692. efi_uintn_t i;
  693. efi_status_t ret = EFI_INVALID_PARAMETER;
  694. int rt_code_sections = 0;
  695. struct efi_event *event;
  696. EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
  697. descriptor_version, virtmap);
  698. if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
  699. descriptor_size < sizeof(struct efi_mem_desc))
  700. goto out;
  701. efi_virtmap = virtmap;
  702. efi_descriptor_size = descriptor_size;
  703. efi_descriptor_count = n;
  704. /*
  705. * TODO:
  706. * Further down we are cheating. While really we should implement
  707. * SetVirtualAddressMap() events and ConvertPointer() to allow
  708. * dynamically loaded drivers to expose runtime services, we don't
  709. * today.
  710. *
  711. * So let's ensure we see exactly one single runtime section, as
  712. * that is the built-in one. If we see more (or less), someone must
  713. * have tried adding or removing to that which we don't support yet.
  714. * In that case, let's better fail rather than expose broken runtime
  715. * services.
  716. */
  717. for (i = 0; i < n; i++) {
  718. struct efi_mem_desc *map = (void*)virtmap +
  719. (descriptor_size * i);
  720. if (map->type == EFI_RUNTIME_SERVICES_CODE)
  721. rt_code_sections++;
  722. }
  723. if (rt_code_sections != 1) {
  724. /*
  725. * We expose exactly one single runtime code section, so
  726. * something is definitely going wrong.
  727. */
  728. goto out;
  729. }
  730. /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
  731. list_for_each_entry(event, &efi_events, link) {
  732. if (event->notify_function)
  733. EFI_CALL_VOID(event->notify_function(
  734. event, event->notify_context));
  735. }
  736. /* Rebind mmio pointers */
  737. for (i = 0; i < n; i++) {
  738. struct efi_mem_desc *map = (void*)virtmap +
  739. (descriptor_size * i);
  740. struct list_head *lhandle;
  741. efi_physical_addr_t map_start = map->physical_start;
  742. efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
  743. efi_physical_addr_t map_end = map_start + map_len;
  744. u64 off = map->virtual_start - map_start;
  745. /* Adjust all mmio pointers in this region */
  746. list_for_each(lhandle, &efi_runtime_mmio) {
  747. struct efi_runtime_mmio_list *lmmio;
  748. lmmio = list_entry(lhandle,
  749. struct efi_runtime_mmio_list,
  750. link);
  751. if ((map_start <= lmmio->paddr) &&
  752. (map_end >= lmmio->paddr)) {
  753. uintptr_t new_addr = lmmio->paddr + off;
  754. *lmmio->ptr = (void *)new_addr;
  755. }
  756. }
  757. if ((map_start <= (uintptr_t)systab.tables) &&
  758. (map_end >= (uintptr_t)systab.tables)) {
  759. char *ptr = (char *)systab.tables;
  760. ptr += off;
  761. systab.tables = (struct efi_configuration_table *)ptr;
  762. }
  763. }
  764. /* Relocate the runtime. See TODO above */
  765. for (i = 0; i < n; i++) {
  766. struct efi_mem_desc *map;
  767. map = (void*)virtmap + (descriptor_size * i);
  768. if (map->type == EFI_RUNTIME_SERVICES_CODE) {
  769. ulong new_offset = map->virtual_start -
  770. map->physical_start + gd->relocaddr;
  771. efi_relocate_runtime_table(new_offset);
  772. efi_runtime_relocate(new_offset, map);
  773. ret = EFI_SUCCESS;
  774. goto out;
  775. }
  776. }
  777. out:
  778. return EFI_EXIT(ret);
  779. }
  780. /**
  781. * efi_add_runtime_mmio() - add memory-mapped IO region
  782. *
  783. * This function adds a memory-mapped IO region to the memory map to make it
  784. * available at runtime.
  785. *
  786. * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
  787. * IO region
  788. * @len: size of the memory-mapped IO region
  789. * Returns: status code
  790. */
  791. efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
  792. {
  793. struct efi_runtime_mmio_list *newmmio;
  794. uint64_t addr = *(uintptr_t *)mmio_ptr;
  795. efi_status_t ret;
  796. ret = efi_add_memory_map(addr, len, EFI_MMAP_IO);
  797. if (ret != EFI_SUCCESS)
  798. return EFI_OUT_OF_RESOURCES;
  799. newmmio = calloc(1, sizeof(*newmmio));
  800. if (!newmmio)
  801. return EFI_OUT_OF_RESOURCES;
  802. newmmio->ptr = mmio_ptr;
  803. newmmio->paddr = *(uintptr_t *)mmio_ptr;
  804. newmmio->len = len;
  805. list_add_tail(&newmmio->link, &efi_runtime_mmio);
  806. return EFI_SUCCESS;
  807. }
  808. /*
  809. * In the second stage, U-Boot has disappeared. To isolate our runtime code
  810. * that at this point still exists from the rest, we put it into a special
  811. * section.
  812. *
  813. * !!WARNING!!
  814. *
  815. * This means that we can not rely on any code outside of this file in any
  816. * function or variable below this line.
  817. *
  818. * Please keep everything fully self-contained and annotated with
  819. * __efi_runtime and __efi_runtime_data markers.
  820. */
  821. /*
  822. * Relocate the EFI runtime stub to a different place. We need to call this
  823. * the first time we expose the runtime interface to a user and on set virtual
  824. * address map calls.
  825. */
  826. /**
  827. * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
  828. *
  829. * This function is used after SetVirtualAddressMap() is called as replacement
  830. * for services that are not available anymore due to constraints of the U-Boot
  831. * implementation.
  832. *
  833. * Return: EFI_UNSUPPORTED
  834. */
  835. static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
  836. {
  837. return EFI_UNSUPPORTED;
  838. }
  839. struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
  840. .hdr = {
  841. .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
  842. .revision = EFI_SPECIFICATION_VERSION,
  843. .headersize = sizeof(struct efi_runtime_services),
  844. },
  845. .get_time = &efi_get_time_boottime,
  846. .set_time = &efi_set_time_boottime,
  847. .get_wakeup_time = (void *)&efi_unimplemented,
  848. .set_wakeup_time = (void *)&efi_unimplemented,
  849. .set_virtual_address_map = &efi_set_virtual_address_map,
  850. .convert_pointer = efi_convert_pointer,
  851. .get_variable = efi_get_variable,
  852. .get_next_variable_name = efi_get_next_variable_name,
  853. .set_variable = efi_set_variable,
  854. .get_next_high_mono_count = (void *)&efi_unimplemented,
  855. .reset_system = &efi_reset_system_boottime,
  856. #ifdef CONFIG_EFI_RUNTIME_UPDATE_CAPSULE
  857. .update_capsule = efi_update_capsule,
  858. .query_capsule_caps = efi_query_capsule_caps,
  859. #else
  860. .update_capsule = efi_update_capsule_unsupported,
  861. .query_capsule_caps = efi_query_capsule_caps_unsupported,
  862. #endif
  863. .query_variable_info = efi_query_variable_info,
  864. };