efi_runtime.c 24 KB

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