efi_esrt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * EFI application ESRT tables support
  4. *
  5. * Copyright (C) 2021 Arm Ltd.
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. #include <log.h>
  10. #include <efi_api.h>
  11. #include <malloc.h>
  12. const efi_guid_t efi_esrt_guid = EFI_SYSTEM_RESOURCE_TABLE_GUID;
  13. static struct efi_system_resource_table *esrt;
  14. #define EFI_ESRT_VERSION 1
  15. /**
  16. * efi_esrt_image_info_to_entry() - copy the information present in a fw image
  17. * descriptor to a ESRT entry.
  18. * The function ensures the ESRT entry matches the image_type_id in @img_info.
  19. * In case of a mismatch we leave the entry unchanged.
  20. *
  21. * @img_info: the source image info descriptor
  22. * @entry: pointer to the ESRT entry to be filled
  23. * @desc_version: the version of the elements in img_info
  24. * @image_type: the image type value to be set in the ESRT entry
  25. * @flags: the capsule flags value to be set in the ESRT entry
  26. *
  27. * Return:
  28. * - EFI_SUCCESS if the entry is correctly updated
  29. * - EFI_INVALID_PARAMETER if entry does not match image_type_id in @img_info.
  30. */
  31. static efi_status_t
  32. efi_esrt_image_info_to_entry(struct efi_firmware_image_descriptor *img_info,
  33. struct efi_system_resource_entry *entry,
  34. u32 desc_version, u32 image_type, u32 flags)
  35. {
  36. if (guidcmp(&entry->fw_class, &img_info->image_type_id)) {
  37. EFI_PRINT("ESRT entry %pUL mismatches img_type_id %pUL\n",
  38. &entry->fw_class, &img_info->image_type_id);
  39. return EFI_INVALID_PARAMETER;
  40. }
  41. entry->fw_version = img_info->version;
  42. entry->fw_type = image_type;
  43. entry->capsule_flags = flags;
  44. /*
  45. * The field lowest_supported_image_version is only present
  46. * on image info structure of version 2 or greater.
  47. * See the EFI_FIRMWARE_IMAGE_DESCRIPTOR definition in UEFI.
  48. */
  49. if (desc_version >= 2)
  50. entry->lowest_supported_fw_version =
  51. img_info->lowest_supported_image_version;
  52. else
  53. entry->lowest_supported_fw_version = 0;
  54. /*
  55. * The fields last_attempt_version and last_attempt_status
  56. * are only present on image info structure of version 3 or
  57. * greater.
  58. * See the EFI_FIRMWARE_IMAGE_DESCRIPTOR definition in UEFI.
  59. */
  60. if (desc_version >= 3) {
  61. entry->last_attempt_version =
  62. img_info->last_attempt_version;
  63. entry->last_attempt_status =
  64. img_info->last_attempt_status;
  65. } else {
  66. entry->last_attempt_version = 0;
  67. entry->last_attempt_status = LAST_ATTEMPT_STATUS_SUCCESS;
  68. }
  69. return EFI_SUCCESS;
  70. }
  71. /**
  72. * efi_esrt_entries_to_size() - Obtain the bytes used by an ESRT
  73. * datastructure with @num_entries.
  74. *
  75. * @num_entries: the number of entries in the ESRT.
  76. *
  77. * Return: the number of bytes an ESRT with @num_entries occupies in memory.
  78. */
  79. static
  80. inline u32 efi_esrt_entries_to_size(u32 num_entries)
  81. {
  82. u32 esrt_size = sizeof(struct efi_system_resource_table) +
  83. num_entries * sizeof(struct efi_system_resource_entry);
  84. return esrt_size;
  85. }
  86. /**
  87. * efi_esrt_allocate_install() - Allocates @num_entries for the ESRT and
  88. * performs basic ESRT initialization.
  89. *
  90. * @num_entries: the number of entries that the ESRT will hold.
  91. *
  92. * Return:
  93. * - pointer to the ESRT if successful.
  94. * - NULL otherwise.
  95. */
  96. static
  97. efi_status_t efi_esrt_allocate_install(u32 num_entries)
  98. {
  99. efi_status_t ret;
  100. struct efi_system_resource_table *new_esrt;
  101. u32 size = efi_esrt_entries_to_size(num_entries);
  102. efi_guid_t esrt_guid = efi_esrt_guid;
  103. /* Reserve num_pages for ESRT */
  104. ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, size,
  105. (void **)&new_esrt);
  106. if (ret != EFI_SUCCESS) {
  107. EFI_PRINT("ESRT cannot allocate memory for %u entries (%u bytes)\n",
  108. num_entries, size);
  109. return ret;
  110. }
  111. new_esrt->fw_resource_count_max = num_entries;
  112. new_esrt->fw_resource_count = 0;
  113. new_esrt->fw_resource_version = EFI_ESRT_VERSION;
  114. /* Install the ESRT in the system configuration table. */
  115. ret = efi_install_configuration_table(&esrt_guid, (void *)new_esrt);
  116. if (ret != EFI_SUCCESS) {
  117. EFI_PRINT("ESRT failed to install the ESRT in the system table\n");
  118. return ret;
  119. }
  120. /* If there was a previous ESRT, deallocate its memory now. */
  121. if (esrt)
  122. ret = efi_free_pool(esrt);
  123. esrt = new_esrt;
  124. return EFI_SUCCESS;
  125. }
  126. /**
  127. * esrt_find_entry() - Obtain the ESRT entry for the image with GUID
  128. * @img_fw_class.
  129. *
  130. * If the img_fw_class is not yet present in the ESRT, this function
  131. * reserves the tail element of the current ESRT as the entry for that fw_class.
  132. * The number of elements in the ESRT is updated in that case.
  133. *
  134. * @img_fw_class: the GUID of the FW image which ESRT entry we want to obtain.
  135. *
  136. * Return:
  137. * - A pointer to the ESRT entry for the image with GUID img_fw_class,
  138. * - NULL if:
  139. * - there is no more space in the ESRT,
  140. * - ESRT is not initialized,
  141. */
  142. static
  143. struct efi_system_resource_entry *esrt_find_entry(efi_guid_t *img_fw_class)
  144. {
  145. u32 filled_entries;
  146. u32 max_entries;
  147. struct efi_system_resource_entry *entry;
  148. if (!esrt) {
  149. EFI_PRINT("ESRT access before initialized\n");
  150. return NULL;
  151. }
  152. filled_entries = esrt->fw_resource_count;
  153. entry = esrt->entries;
  154. /* Check if the image with img_fw_class is already in the ESRT. */
  155. for (u32 idx = 0; idx < filled_entries; idx++) {
  156. if (!guidcmp(&entry[idx].fw_class, img_fw_class)) {
  157. EFI_PRINT("ESRT found entry for image %pUs at index %u\n",
  158. img_fw_class, idx);
  159. return &entry[idx];
  160. }
  161. }
  162. max_entries = esrt->fw_resource_count_max;
  163. /*
  164. * Since the image with img_fw_class is not present in the ESRT, check
  165. * if ESRT is full before appending the new entry to it.
  166. */
  167. if (filled_entries == max_entries) {
  168. EFI_PRINT("ESRT full, this should not happen\n");
  169. return NULL;
  170. }
  171. /*
  172. * This is a new entry for a fw image, increment the element
  173. * number in the table and set the fw_class field.
  174. */
  175. esrt->fw_resource_count++;
  176. entry[filled_entries].fw_class = *img_fw_class;
  177. EFI_PRINT("ESRT allocated new entry for image %pUs at index %u\n",
  178. img_fw_class, filled_entries);
  179. return &entry[filled_entries];
  180. }
  181. /**
  182. * efi_esrt_add_from_fmp() - Populates a sequence of ESRT entries from the FW
  183. * images in the FMP.
  184. *
  185. * @fmp: the FMP instance from which FW images are added to the ESRT
  186. *
  187. * Return:
  188. * - EFI_SUCCESS if all the FW images in the FMP are added to the ESRT
  189. * - Error status otherwise
  190. */
  191. static
  192. efi_status_t efi_esrt_add_from_fmp(struct efi_firmware_management_protocol *fmp)
  193. {
  194. struct efi_system_resource_entry *entry = NULL;
  195. size_t info_size = 0;
  196. struct efi_firmware_image_descriptor *img_info = NULL;
  197. u32 desc_version;
  198. u8 desc_count;
  199. size_t desc_size;
  200. u32 package_version;
  201. u16 *package_version_name;
  202. efi_status_t ret = EFI_SUCCESS;
  203. /*
  204. * TODO: set the field image_type depending on the FW image type
  205. * defined in a platform basis.
  206. */
  207. u32 image_type = ESRT_FW_TYPE_UNKNOWN;
  208. /* TODO: set the capsule flags as a function of the FW image type. */
  209. u32 flags = 0;
  210. ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, img_info,
  211. &desc_version, &desc_count,
  212. &desc_size, NULL, NULL));
  213. if (ret != EFI_BUFFER_TOO_SMALL) {
  214. /*
  215. * An input of info_size=0 should always lead
  216. * fmp->get_image_info to return BUFFER_TO_SMALL.
  217. */
  218. EFI_PRINT("Erroneous FMP implementation\n");
  219. return EFI_INVALID_PARAMETER;
  220. }
  221. ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, info_size,
  222. (void **)&img_info);
  223. if (ret != EFI_SUCCESS) {
  224. EFI_PRINT("ESRT failed to allocate memory for image info.\n");
  225. return ret;
  226. }
  227. ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, img_info,
  228. &desc_version, &desc_count,
  229. &desc_size, &package_version,
  230. &package_version_name));
  231. if (ret != EFI_SUCCESS) {
  232. EFI_PRINT("ESRT failed to obtain the FMP image info\n");
  233. goto out;
  234. }
  235. /*
  236. * Iterate over all the FW images in the FMP.
  237. */
  238. for (u32 desc_idx = 0; desc_idx < desc_count; desc_idx++) {
  239. struct efi_firmware_image_descriptor *cur_img_info =
  240. (struct efi_firmware_image_descriptor *)
  241. ((uintptr_t)img_info + desc_idx * desc_size);
  242. /*
  243. * Obtain the ESRT entry for the FW image with fw_class
  244. * equal to cur_img_info->image_type_id.
  245. */
  246. entry = esrt_find_entry(&cur_img_info->image_type_id);
  247. if (entry) {
  248. ret = efi_esrt_image_info_to_entry(cur_img_info, entry,
  249. desc_version,
  250. image_type, flags);
  251. if (ret != EFI_SUCCESS)
  252. EFI_PRINT("ESRT entry mismatches image_type\n");
  253. } else {
  254. EFI_PRINT("ESRT failed to add entry for %pUs\n",
  255. &cur_img_info->image_type_id);
  256. continue;
  257. }
  258. }
  259. out:
  260. efi_free_pool(img_info);
  261. return EFI_SUCCESS;
  262. }
  263. /**
  264. * efi_esrt_populate() - Populates the ESRT entries from the FMP instances
  265. * present in the system.
  266. * If an ESRT already exists, the old ESRT is replaced in the system table.
  267. * The memory of the old ESRT is deallocated.
  268. *
  269. * Return:
  270. * - EFI_SUCCESS if the ESRT is correctly created
  271. * - error code otherwise.
  272. */
  273. efi_status_t efi_esrt_populate(void)
  274. {
  275. efi_handle_t *base_handle = NULL;
  276. efi_handle_t *it_handle;
  277. efi_uintn_t no_handles = 0;
  278. struct efi_firmware_management_protocol *fmp;
  279. efi_status_t ret;
  280. u32 num_entries = 0;
  281. struct efi_handler *handler;
  282. /*
  283. * Obtain the number of registered FMP handles.
  284. */
  285. ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
  286. &efi_guid_firmware_management_protocol,
  287. NULL, &no_handles,
  288. (efi_handle_t **)&base_handle));
  289. if (ret != EFI_SUCCESS) {
  290. EFI_PRINT("ESRT There are no FMP instances\n");
  291. ret = efi_esrt_allocate_install(0);
  292. if (ret != EFI_SUCCESS) {
  293. EFI_PRINT("ESRT failed to create table with 0 entries\n");
  294. return ret;
  295. }
  296. return EFI_SUCCESS;
  297. }
  298. EFI_PRINT("ESRT populate esrt from (%zd) available FMP handles\n",
  299. no_handles);
  300. /*
  301. * Iterate over all FMPs to determine an upper bound on the number of
  302. * ESRT entries.
  303. */
  304. it_handle = base_handle;
  305. for (u32 idx = 0; idx < no_handles; idx++, it_handle++) {
  306. struct efi_firmware_image_descriptor *img_info = NULL;
  307. size_t info_size = 0;
  308. u32 desc_version = 0;
  309. u8 desc_count = 0;
  310. size_t desc_size = 0;
  311. u32 package_version;
  312. u16 *package_version_name;
  313. ret = efi_search_protocol(*it_handle,
  314. &efi_guid_firmware_management_protocol,
  315. &handler);
  316. if (ret != EFI_SUCCESS) {
  317. EFI_PRINT("ESRT Unable to find FMP handle (%u)\n",
  318. idx);
  319. goto out;
  320. }
  321. fmp = handler->protocol_interface;
  322. ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, NULL,
  323. &desc_version, &desc_count,
  324. &desc_size, &package_version,
  325. &package_version_name));
  326. if (ret != EFI_BUFFER_TOO_SMALL) {
  327. /*
  328. * An input of info_size=0 should always lead
  329. * fmp->get_image_info to return BUFFER_TO_SMALL.
  330. */
  331. EFI_PRINT("ESRT erroneous FMP implementation\n");
  332. ret = EFI_INVALID_PARAMETER;
  333. goto out;
  334. }
  335. ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, info_size,
  336. (void **)&img_info);
  337. if (ret != EFI_SUCCESS) {
  338. EFI_PRINT("ESRT failed to allocate memory for image info\n");
  339. goto out;
  340. }
  341. /*
  342. * Calls to a FMP get_image_info method do not return the
  343. * desc_count value if the return status differs from EFI_SUCCESS.
  344. * We need to repeat the call to get_image_info with a properly
  345. * sized buffer in order to obtain the real number of images
  346. * handled by the FMP.
  347. */
  348. ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, img_info,
  349. &desc_version, &desc_count,
  350. &desc_size, &package_version,
  351. &package_version_name));
  352. if (ret != EFI_SUCCESS) {
  353. EFI_PRINT("ESRT failed to obtain image info from FMP\n");
  354. efi_free_pool(img_info);
  355. goto out;
  356. }
  357. num_entries += desc_count;
  358. efi_free_pool(img_info);
  359. }
  360. EFI_PRINT("ESRT create table with %u entries\n", num_entries);
  361. /*
  362. * Allocate an ESRT with the sufficient number of entries to accommodate
  363. * all the FMPs in the system.
  364. */
  365. ret = efi_esrt_allocate_install(num_entries);
  366. if (ret != EFI_SUCCESS) {
  367. EFI_PRINT("ESRT failed to initialize table\n");
  368. goto out;
  369. }
  370. /*
  371. * Populate the ESRT entries with all existing FMP.
  372. */
  373. it_handle = base_handle;
  374. for (u32 idx = 0; idx < no_handles; idx++, it_handle++) {
  375. ret = efi_search_protocol(*it_handle,
  376. &efi_guid_firmware_management_protocol,
  377. &handler);
  378. if (ret != EFI_SUCCESS) {
  379. EFI_PRINT("ESRT unable to find FMP handle (%u)\n",
  380. idx);
  381. break;
  382. }
  383. fmp = handler->protocol_interface;
  384. ret = efi_esrt_add_from_fmp(fmp);
  385. if (ret != EFI_SUCCESS)
  386. EFI_PRINT("ESRT failed to add FMP to the table\n");
  387. }
  388. out:
  389. efi_free_pool(base_handle);
  390. return ret;
  391. }
  392. /**
  393. * efi_esrt_new_fmp_notify() - Callback for the EVT_NOTIFY_SIGNAL event raised
  394. * when a new FMP protocol instance is registered in the system.
  395. */
  396. static void EFIAPI efi_esrt_new_fmp_notify(struct efi_event *event,
  397. void *context)
  398. {
  399. efi_status_t ret;
  400. EFI_ENTRY();
  401. ret = efi_esrt_populate();
  402. if (ret != EFI_SUCCESS)
  403. EFI_PRINT("ESRT failed to populate ESRT entry\n");
  404. EFI_EXIT(ret);
  405. }
  406. /**
  407. * efi_esrt_register() - Install the ESRT system table.
  408. *
  409. * Return: status code
  410. */
  411. efi_status_t efi_esrt_register(void)
  412. {
  413. struct efi_event *ev = NULL;
  414. void *registration;
  415. efi_status_t ret;
  416. EFI_PRINT("ESRT creation start\n");
  417. ret = efi_esrt_populate();
  418. if (ret != EFI_SUCCESS) {
  419. EFI_PRINT("ESRT failed to initiate the table\n");
  420. return ret;
  421. }
  422. ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  423. efi_esrt_new_fmp_notify, NULL, NULL, &ev);
  424. if (ret != EFI_SUCCESS) {
  425. EFI_PRINT("ESRT failed to create event\n");
  426. return ret;
  427. }
  428. ret = EFI_CALL(efi_register_protocol_notify(&efi_guid_firmware_management_protocol,
  429. ev, &registration));
  430. if (ret != EFI_SUCCESS) {
  431. EFI_PRINT("ESRT failed to register FMP callback\n");
  432. return ret;
  433. }
  434. EFI_PRINT("ESRT table created\n");
  435. return ret;
  436. }