efi_runtime.c 24 KB

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