efi_runtime.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. It only executes an endless loop.
  331. * Boards may override the helpers below to implement reset functionality.
  332. *
  333. * See the Unified Extensible Firmware Interface (UEFI) specification for
  334. * details.
  335. *
  336. * @reset_type: type of reset to perform
  337. * @reset_status: status code for the reset
  338. * @data_size: size of reset_data
  339. * @reset_data: information about the reset
  340. */
  341. void __weak __efi_runtime EFIAPI efi_reset_system(
  342. enum efi_reset_type reset_type,
  343. efi_status_t reset_status,
  344. unsigned long data_size, void *reset_data)
  345. {
  346. /* Nothing we can do */
  347. while (1) { }
  348. }
  349. /**
  350. * efi_reset_system_init() - initialize the reset driver
  351. *
  352. * Boards may override this function to initialize the reset driver.
  353. */
  354. efi_status_t __weak efi_reset_system_init(void)
  355. {
  356. return EFI_SUCCESS;
  357. }
  358. /**
  359. * efi_get_time() - get current time
  360. *
  361. * This function implements the GetTime runtime service after
  362. * SetVirtualAddressMap() is called. As the U-Boot driver are not available
  363. * anymore only an error code is returned.
  364. *
  365. * See the Unified Extensible Firmware Interface (UEFI) specification
  366. * for details.
  367. *
  368. * @time: pointer to structure to receive current time
  369. * @capabilities: pointer to structure to receive RTC properties
  370. * Returns: status code
  371. */
  372. efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
  373. struct efi_time *time,
  374. struct efi_time_cap *capabilities)
  375. {
  376. return EFI_UNSUPPORTED;
  377. }
  378. /**
  379. * efi_set_time() - set current time
  380. *
  381. * This function implements the SetTime runtime service after
  382. * SetVirtualAddressMap() is called. As the U-Boot driver are not available
  383. * anymore only an error code is returned.
  384. *
  385. * See the Unified Extensible Firmware Interface (UEFI) specification
  386. * for details.
  387. *
  388. * @time: pointer to structure to with current time
  389. * Returns: status code
  390. */
  391. efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
  392. {
  393. return EFI_UNSUPPORTED;
  394. }
  395. /**
  396. * efi_is_runtime_service_pointer() - check if pointer points to runtime table
  397. *
  398. * @p: pointer to check
  399. * Return: true if the pointer points to a service function pointer in the
  400. * runtime table
  401. */
  402. static bool efi_is_runtime_service_pointer(void *p)
  403. {
  404. return (p >= (void *)&efi_runtime_services.get_time &&
  405. p <= (void *)&efi_runtime_services.query_variable_info) ||
  406. p == (void *)&efi_events.prev ||
  407. p == (void *)&efi_events.next;
  408. }
  409. /**
  410. * efi_runtime_detach() - detach unimplemented runtime functions
  411. */
  412. void efi_runtime_detach(void)
  413. {
  414. efi_runtime_services.reset_system = efi_reset_system;
  415. efi_runtime_services.get_time = efi_get_time;
  416. efi_runtime_services.set_time = efi_set_time;
  417. /* Update CRC32 */
  418. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  419. }
  420. /**
  421. * efi_set_virtual_address_map_runtime() - change from physical to virtual
  422. * mapping
  423. *
  424. * This function implements the SetVirtualAddressMap() runtime service after
  425. * it is first called.
  426. *
  427. * See the Unified Extensible Firmware Interface (UEFI) specification for
  428. * details.
  429. *
  430. * @memory_map_size: size of the virtual map
  431. * @descriptor_size: size of an entry in the map
  432. * @descriptor_version: version of the map entries
  433. * @virtmap: virtual address mapping information
  434. * Return: status code EFI_UNSUPPORTED
  435. */
  436. static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
  437. efi_uintn_t memory_map_size,
  438. efi_uintn_t descriptor_size,
  439. uint32_t descriptor_version,
  440. struct efi_mem_desc *virtmap)
  441. {
  442. return EFI_UNSUPPORTED;
  443. }
  444. /**
  445. * efi_convert_pointer_runtime() - convert from physical to virtual pointer
  446. *
  447. * This function implements the ConvertPointer() runtime service after
  448. * the first call to SetVirtualAddressMap().
  449. *
  450. * See the Unified Extensible Firmware Interface (UEFI) specification for
  451. * details.
  452. *
  453. * @debug_disposition: indicates if pointer may be converted to NULL
  454. * @address: pointer to be converted
  455. * Return: status code EFI_UNSUPPORTED
  456. */
  457. static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
  458. efi_uintn_t debug_disposition, void **address)
  459. {
  460. return EFI_UNSUPPORTED;
  461. }
  462. /**
  463. * efi_convert_pointer() - convert from physical to virtual pointer
  464. *
  465. * This function implements the ConvertPointer() runtime service until
  466. * the first call to SetVirtualAddressMap().
  467. *
  468. * See the Unified Extensible Firmware Interface (UEFI) specification for
  469. * details.
  470. *
  471. * @debug_disposition: indicates if pointer may be converted to NULL
  472. * @address: pointer to be converted
  473. * Return: status code
  474. */
  475. __efi_runtime efi_status_t EFIAPI
  476. efi_convert_pointer(efi_uintn_t debug_disposition, void **address)
  477. {
  478. efi_physical_addr_t addr;
  479. efi_uintn_t i;
  480. efi_status_t ret = EFI_NOT_FOUND;
  481. if (!efi_virtmap) {
  482. ret = EFI_UNSUPPORTED;
  483. goto out;
  484. }
  485. if (!address) {
  486. ret = EFI_INVALID_PARAMETER;
  487. goto out;
  488. }
  489. if (!*address) {
  490. if (debug_disposition & EFI_OPTIONAL_PTR)
  491. return EFI_SUCCESS;
  492. else
  493. return EFI_INVALID_PARAMETER;
  494. }
  495. addr = (uintptr_t)*address;
  496. for (i = 0; i < efi_descriptor_count; i++) {
  497. struct efi_mem_desc *map = (void *)efi_virtmap +
  498. (efi_descriptor_size * i);
  499. if (addr >= map->physical_start &&
  500. (addr < map->physical_start
  501. + (map->num_pages << EFI_PAGE_SHIFT))) {
  502. *address = (void *)(uintptr_t)
  503. (addr + map->virtual_start -
  504. map->physical_start);
  505. ret = EFI_SUCCESS;
  506. break;
  507. }
  508. }
  509. out:
  510. return ret;
  511. }
  512. static __efi_runtime void efi_relocate_runtime_table(ulong offset)
  513. {
  514. ulong patchoff;
  515. void **pos;
  516. /* Relocate the runtime services pointers */
  517. patchoff = offset - gd->relocaddr;
  518. for (pos = (void **)&efi_runtime_services.get_time;
  519. pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
  520. if (*pos)
  521. *pos += patchoff;
  522. }
  523. /*
  524. * The entry for SetVirtualAddress() must point to a physical address.
  525. * After the first execution the service must return EFI_UNSUPPORTED.
  526. */
  527. efi_runtime_services.set_virtual_address_map =
  528. &efi_set_virtual_address_map_runtime;
  529. /*
  530. * The entry for ConvertPointer() must point to a physical address.
  531. * The service is not usable after SetVirtualAddress().
  532. */
  533. efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
  534. /*
  535. * TODO: Update UEFI variable RuntimeServicesSupported removing flags
  536. * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
  537. * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
  538. */
  539. /* Update CRC32 */
  540. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  541. }
  542. /* Relocate EFI runtime to uboot_reloc_base = offset */
  543. void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
  544. {
  545. #ifdef IS_RELA
  546. struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
  547. #else
  548. struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
  549. static ulong lastoff = CONFIG_SYS_TEXT_BASE;
  550. #endif
  551. debug("%s: Relocating to offset=%lx\n", __func__, offset);
  552. for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
  553. ulong base = CONFIG_SYS_TEXT_BASE;
  554. ulong *p;
  555. ulong newaddr;
  556. p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
  557. /*
  558. * The runtime services table is updated in
  559. * efi_relocate_runtime_table()
  560. */
  561. if (map && efi_is_runtime_service_pointer(p))
  562. continue;
  563. debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
  564. rel->info, *p, rel->offset);
  565. switch (rel->info & R_MASK) {
  566. case R_RELATIVE:
  567. #ifdef IS_RELA
  568. newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
  569. #else
  570. newaddr = *p - lastoff + offset;
  571. #endif
  572. break;
  573. #ifdef R_ABSOLUTE
  574. case R_ABSOLUTE: {
  575. ulong symidx = rel->info >> SYM_INDEX;
  576. extern struct dyn_sym __dyn_sym_start[];
  577. newaddr = __dyn_sym_start[symidx].addr + offset;
  578. #ifdef IS_RELA
  579. newaddr -= CONFIG_SYS_TEXT_BASE;
  580. #endif
  581. break;
  582. }
  583. #endif
  584. default:
  585. printf("%s: Unknown relocation type %llx\n",
  586. __func__, rel->info & R_MASK);
  587. continue;
  588. }
  589. /* Check if the relocation is inside bounds */
  590. if (map && ((newaddr < map->virtual_start) ||
  591. newaddr > (map->virtual_start +
  592. (map->num_pages << EFI_PAGE_SHIFT)))) {
  593. printf("%s: Relocation at %p is out of range (%lx)\n",
  594. __func__, p, newaddr);
  595. continue;
  596. }
  597. debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
  598. *p = newaddr;
  599. flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
  600. ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
  601. }
  602. #ifndef IS_RELA
  603. lastoff = offset;
  604. #endif
  605. invalidate_icache_all();
  606. }
  607. /**
  608. * efi_set_virtual_address_map() - change from physical to virtual mapping
  609. *
  610. * This function implements the SetVirtualAddressMap() runtime service.
  611. *
  612. * See the Unified Extensible Firmware Interface (UEFI) specification for
  613. * details.
  614. *
  615. * @memory_map_size: size of the virtual map
  616. * @descriptor_size: size of an entry in the map
  617. * @descriptor_version: version of the map entries
  618. * @virtmap: virtual address mapping information
  619. * Return: status code
  620. */
  621. static efi_status_t EFIAPI efi_set_virtual_address_map(
  622. efi_uintn_t memory_map_size,
  623. efi_uintn_t descriptor_size,
  624. uint32_t descriptor_version,
  625. struct efi_mem_desc *virtmap)
  626. {
  627. efi_uintn_t n = memory_map_size / descriptor_size;
  628. efi_uintn_t i;
  629. efi_status_t ret = EFI_INVALID_PARAMETER;
  630. int rt_code_sections = 0;
  631. struct efi_event *event;
  632. EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
  633. descriptor_version, virtmap);
  634. if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
  635. descriptor_size < sizeof(struct efi_mem_desc))
  636. goto out;
  637. efi_virtmap = virtmap;
  638. efi_descriptor_size = descriptor_size;
  639. efi_descriptor_count = n;
  640. /*
  641. * TODO:
  642. * Further down we are cheating. While really we should implement
  643. * SetVirtualAddressMap() events and ConvertPointer() to allow
  644. * dynamically loaded drivers to expose runtime services, we don't
  645. * today.
  646. *
  647. * So let's ensure we see exactly one single runtime section, as
  648. * that is the built-in one. If we see more (or less), someone must
  649. * have tried adding or removing to that which we don't support yet.
  650. * In that case, let's better fail rather than expose broken runtime
  651. * services.
  652. */
  653. for (i = 0; i < n; i++) {
  654. struct efi_mem_desc *map = (void*)virtmap +
  655. (descriptor_size * i);
  656. if (map->type == EFI_RUNTIME_SERVICES_CODE)
  657. rt_code_sections++;
  658. }
  659. if (rt_code_sections != 1) {
  660. /*
  661. * We expose exactly one single runtime code section, so
  662. * something is definitely going wrong.
  663. */
  664. goto out;
  665. }
  666. /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
  667. list_for_each_entry(event, &efi_events, link) {
  668. if (event->notify_function)
  669. EFI_CALL_VOID(event->notify_function(
  670. event, event->notify_context));
  671. }
  672. /* Rebind mmio pointers */
  673. for (i = 0; i < n; i++) {
  674. struct efi_mem_desc *map = (void*)virtmap +
  675. (descriptor_size * i);
  676. struct list_head *lhandle;
  677. efi_physical_addr_t map_start = map->physical_start;
  678. efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
  679. efi_physical_addr_t map_end = map_start + map_len;
  680. u64 off = map->virtual_start - map_start;
  681. /* Adjust all mmio pointers in this region */
  682. list_for_each(lhandle, &efi_runtime_mmio) {
  683. struct efi_runtime_mmio_list *lmmio;
  684. lmmio = list_entry(lhandle,
  685. struct efi_runtime_mmio_list,
  686. link);
  687. if ((map_start <= lmmio->paddr) &&
  688. (map_end >= lmmio->paddr)) {
  689. uintptr_t new_addr = lmmio->paddr + off;
  690. *lmmio->ptr = (void *)new_addr;
  691. }
  692. }
  693. if ((map_start <= (uintptr_t)systab.tables) &&
  694. (map_end >= (uintptr_t)systab.tables)) {
  695. char *ptr = (char *)systab.tables;
  696. ptr += off;
  697. systab.tables = (struct efi_configuration_table *)ptr;
  698. }
  699. }
  700. /* Relocate the runtime. See TODO above */
  701. for (i = 0; i < n; i++) {
  702. struct efi_mem_desc *map;
  703. map = (void*)virtmap + (descriptor_size * i);
  704. if (map->type == EFI_RUNTIME_SERVICES_CODE) {
  705. ulong new_offset = map->virtual_start -
  706. map->physical_start + gd->relocaddr;
  707. efi_relocate_runtime_table(new_offset);
  708. efi_runtime_relocate(new_offset, map);
  709. ret = EFI_SUCCESS;
  710. goto out;
  711. }
  712. }
  713. out:
  714. return EFI_EXIT(ret);
  715. }
  716. /**
  717. * efi_add_runtime_mmio() - add memory-mapped IO region
  718. *
  719. * This function adds a memory-mapped IO region to the memory map to make it
  720. * available at runtime.
  721. *
  722. * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
  723. * IO region
  724. * @len: size of the memory-mapped IO region
  725. * Returns: status code
  726. */
  727. efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
  728. {
  729. struct efi_runtime_mmio_list *newmmio;
  730. uint64_t addr = *(uintptr_t *)mmio_ptr;
  731. efi_status_t ret;
  732. ret = efi_add_memory_map(addr, len, EFI_MMAP_IO);
  733. if (ret != EFI_SUCCESS)
  734. return EFI_OUT_OF_RESOURCES;
  735. newmmio = calloc(1, sizeof(*newmmio));
  736. if (!newmmio)
  737. return EFI_OUT_OF_RESOURCES;
  738. newmmio->ptr = mmio_ptr;
  739. newmmio->paddr = *(uintptr_t *)mmio_ptr;
  740. newmmio->len = len;
  741. list_add_tail(&newmmio->link, &efi_runtime_mmio);
  742. return EFI_SUCCESS;
  743. }
  744. /*
  745. * In the second stage, U-Boot has disappeared. To isolate our runtime code
  746. * that at this point still exists from the rest, we put it into a special
  747. * section.
  748. *
  749. * !!WARNING!!
  750. *
  751. * This means that we can not rely on any code outside of this file in any
  752. * function or variable below this line.
  753. *
  754. * Please keep everything fully self-contained and annotated with
  755. * __efi_runtime and __efi_runtime_data markers.
  756. */
  757. /*
  758. * Relocate the EFI runtime stub to a different place. We need to call this
  759. * the first time we expose the runtime interface to a user and on set virtual
  760. * address map calls.
  761. */
  762. /**
  763. * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
  764. *
  765. * This function is used after SetVirtualAddressMap() is called as replacement
  766. * for services that are not available anymore due to constraints of the U-Boot
  767. * implementation.
  768. *
  769. * Return: EFI_UNSUPPORTED
  770. */
  771. static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
  772. {
  773. return EFI_UNSUPPORTED;
  774. }
  775. /**
  776. * efi_update_capsule() - process information from operating system
  777. *
  778. * This function implements the UpdateCapsule() runtime service.
  779. *
  780. * See the Unified Extensible Firmware Interface (UEFI) specification for
  781. * details.
  782. *
  783. * @capsule_header_array: pointer to array of virtual pointers
  784. * @capsule_count: number of pointers in capsule_header_array
  785. * @scatter_gather_list: pointer to arry of physical pointers
  786. * Returns: status code
  787. */
  788. efi_status_t __efi_runtime EFIAPI efi_update_capsule(
  789. struct efi_capsule_header **capsule_header_array,
  790. efi_uintn_t capsule_count,
  791. u64 scatter_gather_list)
  792. {
  793. return EFI_UNSUPPORTED;
  794. }
  795. /**
  796. * efi_query_capsule_caps() - check if capsule is supported
  797. *
  798. * This function implements the QueryCapsuleCapabilities() runtime service.
  799. *
  800. * See the Unified Extensible Firmware Interface (UEFI) specification for
  801. * details.
  802. *
  803. * @capsule_header_array: pointer to array of virtual pointers
  804. * @capsule_count: number of pointers in capsule_header_array
  805. * @maximum_capsule_size: maximum capsule size
  806. * @reset_type: type of reset needed for capsule update
  807. * Returns: status code
  808. */
  809. efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
  810. struct efi_capsule_header **capsule_header_array,
  811. efi_uintn_t capsule_count,
  812. u64 *maximum_capsule_size,
  813. u32 *reset_type)
  814. {
  815. return EFI_UNSUPPORTED;
  816. }
  817. struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
  818. .hdr = {
  819. .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
  820. .revision = EFI_SPECIFICATION_VERSION,
  821. .headersize = sizeof(struct efi_runtime_services),
  822. },
  823. .get_time = &efi_get_time_boottime,
  824. .set_time = &efi_set_time_boottime,
  825. .get_wakeup_time = (void *)&efi_unimplemented,
  826. .set_wakeup_time = (void *)&efi_unimplemented,
  827. .set_virtual_address_map = &efi_set_virtual_address_map,
  828. .convert_pointer = efi_convert_pointer,
  829. .get_variable = efi_get_variable,
  830. .get_next_variable_name = efi_get_next_variable_name,
  831. .set_variable = efi_set_variable,
  832. .get_next_high_mono_count = (void *)&efi_unimplemented,
  833. .reset_system = &efi_reset_system_boottime,
  834. .update_capsule = efi_update_capsule,
  835. .query_capsule_caps = efi_query_capsule_caps,
  836. .query_variable_info = efi_query_variable_info,
  837. };