efi_runtime.c 23 KB

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