efi_runtime.c 22 KB

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