efi_runtime.c 24 KB

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