efi_capsule.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI Capsule
  4. *
  5. * Copyright (c) 2018 Linaro Limited
  6. * Author: AKASHI Takahiro
  7. */
  8. #define LOG_CATEGORY LOGC_EFI
  9. #include <common.h>
  10. #include <efi_loader.h>
  11. #include <efi_variable.h>
  12. #include <fs.h>
  13. #include <malloc.h>
  14. #include <mapmem.h>
  15. #include <sort.h>
  16. #include <crypto/pkcs7.h>
  17. #include <crypto/pkcs7_parser.h>
  18. #include <linux/err.h>
  19. const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
  20. static const efi_guid_t efi_guid_firmware_management_capsule_id =
  21. EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
  22. const efi_guid_t efi_guid_firmware_management_protocol =
  23. EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
  24. #ifdef CONFIG_EFI_CAPSULE_ON_DISK
  25. /* for file system access */
  26. static struct efi_file_handle *bootdev_root;
  27. #endif
  28. /**
  29. * get_last_capsule - get the last capsule index
  30. *
  31. * Retrieve the index of the capsule invoked last time from "CapsuleLast"
  32. * variable.
  33. *
  34. * Return:
  35. * * > 0 - the last capsule index invoked
  36. * * 0xffff - on error, or no capsule invoked yet
  37. */
  38. static __maybe_unused unsigned int get_last_capsule(void)
  39. {
  40. u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
  41. char value[5];
  42. efi_uintn_t size;
  43. unsigned long index = 0xffff;
  44. efi_status_t ret;
  45. int i;
  46. size = sizeof(value16);
  47. ret = efi_get_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
  48. NULL, &size, value16, NULL);
  49. if (ret != EFI_SUCCESS || size != 22 ||
  50. u16_strncmp(value16, L"Capsule", 7))
  51. goto err;
  52. for (i = 0; i < 4; ++i) {
  53. u16 c = value16[i + 7];
  54. if (!c || c > 0x7f)
  55. goto err;
  56. value[i] = c;
  57. }
  58. value[4] = 0;
  59. if (strict_strtoul(value, 16, &index))
  60. index = 0xffff;
  61. err:
  62. return index;
  63. }
  64. /**
  65. * set_capsule_result - set a result variable
  66. * @capsule: Capsule
  67. * @return_status: Return status
  68. *
  69. * Create and set a result variable, "CapsuleXXXX", for the capsule,
  70. * @capsule.
  71. */
  72. static __maybe_unused
  73. void set_capsule_result(int index, struct efi_capsule_header *capsule,
  74. efi_status_t return_status)
  75. {
  76. u16 variable_name16[12];
  77. struct efi_capsule_result_variable_header result;
  78. struct efi_time time;
  79. efi_status_t ret;
  80. efi_create_indexed_name(variable_name16, sizeof(variable_name16),
  81. "Capsule", index);
  82. result.variable_total_size = sizeof(result);
  83. result.capsule_guid = capsule->capsule_guid;
  84. ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
  85. if (ret == EFI_SUCCESS)
  86. memcpy(&result.capsule_processed, &time, sizeof(time));
  87. else
  88. memset(&result.capsule_processed, 0, sizeof(time));
  89. result.capsule_status = return_status;
  90. ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report,
  91. EFI_VARIABLE_NON_VOLATILE |
  92. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  93. EFI_VARIABLE_RUNTIME_ACCESS,
  94. sizeof(result), &result, false);
  95. if (ret != EFI_SUCCESS) {
  96. log_err("Setting %ls failed\n", variable_name16);
  97. return;
  98. }
  99. /* Variable CapsuleLast must not include terminating 0x0000 */
  100. ret = efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
  101. EFI_VARIABLE_READ_ONLY |
  102. EFI_VARIABLE_NON_VOLATILE |
  103. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  104. EFI_VARIABLE_RUNTIME_ACCESS,
  105. 22, variable_name16, false);
  106. if (ret != EFI_SUCCESS)
  107. log_err("Setting %ls failed\n", L"CapsuleLast");
  108. }
  109. #ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
  110. /**
  111. * efi_fmp_find - search for Firmware Management Protocol drivers
  112. * @image_type: Image type guid
  113. * @instance: Instance number
  114. * @handles: Handles of FMP drivers
  115. * @no_handles: Number of handles
  116. *
  117. * Search for Firmware Management Protocol drivers, matching the image
  118. * type, @image_type and the machine instance, @instance, from the list,
  119. * @handles.
  120. *
  121. * Return:
  122. * * Protocol instance - on success
  123. * * NULL - on failure
  124. */
  125. static struct efi_firmware_management_protocol *
  126. efi_fmp_find(efi_guid_t *image_type, u64 instance, efi_handle_t *handles,
  127. efi_uintn_t no_handles)
  128. {
  129. efi_handle_t *handle;
  130. struct efi_firmware_management_protocol *fmp;
  131. struct efi_firmware_image_descriptor *image_info, *desc;
  132. efi_uintn_t info_size, descriptor_size;
  133. u32 descriptor_version;
  134. u8 descriptor_count;
  135. u32 package_version;
  136. u16 *package_version_name;
  137. bool found = false;
  138. int i, j;
  139. efi_status_t ret;
  140. for (i = 0, handle = handles; i < no_handles; i++, handle++) {
  141. ret = EFI_CALL(efi_handle_protocol(
  142. *handle,
  143. &efi_guid_firmware_management_protocol,
  144. (void **)&fmp));
  145. if (ret != EFI_SUCCESS)
  146. continue;
  147. /* get device's image info */
  148. info_size = 0;
  149. image_info = NULL;
  150. descriptor_version = 0;
  151. descriptor_count = 0;
  152. descriptor_size = 0;
  153. package_version = 0;
  154. package_version_name = NULL;
  155. ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
  156. image_info,
  157. &descriptor_version,
  158. &descriptor_count,
  159. &descriptor_size,
  160. &package_version,
  161. &package_version_name));
  162. if (ret != EFI_BUFFER_TOO_SMALL)
  163. goto skip;
  164. image_info = malloc(info_size);
  165. if (!image_info)
  166. goto skip;
  167. ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
  168. image_info,
  169. &descriptor_version,
  170. &descriptor_count,
  171. &descriptor_size,
  172. &package_version,
  173. &package_version_name));
  174. if (ret != EFI_SUCCESS ||
  175. descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION)
  176. goto skip;
  177. /* matching */
  178. for (j = 0, desc = image_info; j < descriptor_count;
  179. j++, desc = (void *)desc + descriptor_size) {
  180. log_debug("+++ desc[%d] index: %d, name: %ls\n",
  181. j, desc->image_index, desc->image_id_name);
  182. if (!guidcmp(&desc->image_type_id, image_type) &&
  183. (!instance ||
  184. !desc->hardware_instance ||
  185. desc->hardware_instance == instance))
  186. found = true;
  187. }
  188. skip:
  189. efi_free_pool(package_version_name);
  190. free(image_info);
  191. EFI_CALL(efi_close_protocol(
  192. (efi_handle_t)fmp,
  193. &efi_guid_firmware_management_protocol,
  194. NULL, NULL));
  195. if (found)
  196. return fmp;
  197. }
  198. return NULL;
  199. }
  200. /**
  201. * efi_remove_auth_hdr - remove authentication data from image
  202. * @image: Pointer to pointer to Image
  203. * @image_size: Pointer to Image size
  204. *
  205. * Remove the authentication data from image if possible.
  206. * Update @image and @image_size.
  207. *
  208. * Return: status code
  209. */
  210. static efi_status_t efi_remove_auth_hdr(void **image, efi_uintn_t *image_size)
  211. {
  212. struct efi_firmware_image_authentication *auth_hdr;
  213. efi_status_t ret = EFI_INVALID_PARAMETER;
  214. auth_hdr = (struct efi_firmware_image_authentication *)*image;
  215. if (*image_size < sizeof(*auth_hdr))
  216. goto out;
  217. if (auth_hdr->auth_info.hdr.dwLength <=
  218. offsetof(struct win_certificate_uefi_guid, cert_data))
  219. goto out;
  220. *image = (uint8_t *)*image + sizeof(auth_hdr->monotonic_count) +
  221. auth_hdr->auth_info.hdr.dwLength;
  222. *image_size = *image_size - auth_hdr->auth_info.hdr.dwLength -
  223. sizeof(auth_hdr->monotonic_count);
  224. ret = EFI_SUCCESS;
  225. out:
  226. return ret;
  227. }
  228. #if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
  229. efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
  230. void **image, efi_uintn_t *image_size)
  231. {
  232. u8 *buf;
  233. int ret;
  234. void *fdt_pkey, *pkey;
  235. efi_uintn_t pkey_len;
  236. uint64_t monotonic_count;
  237. struct efi_signature_store *truststore;
  238. struct pkcs7_message *capsule_sig;
  239. struct efi_image_regions *regs;
  240. struct efi_firmware_image_authentication *auth_hdr;
  241. efi_status_t status;
  242. status = EFI_SECURITY_VIOLATION;
  243. capsule_sig = NULL;
  244. truststore = NULL;
  245. regs = NULL;
  246. /* Sanity checks */
  247. if (capsule == NULL || capsule_size == 0)
  248. goto out;
  249. *image = (uint8_t *)capsule;
  250. *image_size = capsule_size;
  251. if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS)
  252. goto out;
  253. auth_hdr = (struct efi_firmware_image_authentication *)capsule;
  254. if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
  255. goto out;
  256. memcpy(&monotonic_count, &auth_hdr->monotonic_count,
  257. sizeof(monotonic_count));
  258. /* data to be digested */
  259. regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1);
  260. if (!regs)
  261. goto out;
  262. regs->max = 2;
  263. efi_image_region_add(regs, (uint8_t *)*image,
  264. (uint8_t *)*image + *image_size, 1);
  265. efi_image_region_add(regs, (uint8_t *)&monotonic_count,
  266. (uint8_t *)&monotonic_count + sizeof(monotonic_count),
  267. 1);
  268. capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data,
  269. auth_hdr->auth_info.hdr.dwLength
  270. - sizeof(auth_hdr->auth_info),
  271. &buf);
  272. if (IS_ERR(capsule_sig)) {
  273. debug("Parsing variable's pkcs7 header failed\n");
  274. capsule_sig = NULL;
  275. goto out;
  276. }
  277. ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
  278. if (ret < 0)
  279. goto out;
  280. pkey = malloc(pkey_len);
  281. if (!pkey)
  282. goto out;
  283. memcpy(pkey, fdt_pkey, pkey_len);
  284. truststore = efi_build_signature_store(pkey, pkey_len);
  285. if (!truststore)
  286. goto out;
  287. /* verify signature */
  288. if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
  289. debug("Verified\n");
  290. } else {
  291. debug("Verifying variable's signature failed\n");
  292. goto out;
  293. }
  294. status = EFI_SUCCESS;
  295. out:
  296. efi_sigstore_free(truststore);
  297. pkcs7_free_message(capsule_sig);
  298. free(regs);
  299. return status;
  300. }
  301. #else
  302. efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
  303. void **image, efi_uintn_t *image_size)
  304. {
  305. return EFI_UNSUPPORTED;
  306. }
  307. #endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
  308. /**
  309. * efi_capsule_update_firmware - update firmware from capsule
  310. * @capsule_data: Capsule
  311. *
  312. * Update firmware, using a capsule, @capsule_data. Loading any FMP
  313. * drivers embedded in a capsule is not supported.
  314. *
  315. * Return: status code
  316. */
  317. static efi_status_t efi_capsule_update_firmware(
  318. struct efi_capsule_header *capsule_data)
  319. {
  320. struct efi_firmware_management_capsule_header *capsule;
  321. struct efi_firmware_management_capsule_image_header *image;
  322. size_t capsule_size, image_binary_size;
  323. void *image_binary, *vendor_code;
  324. efi_handle_t *handles;
  325. efi_uintn_t no_handles;
  326. int item;
  327. struct efi_firmware_management_protocol *fmp;
  328. u16 *abort_reason;
  329. efi_status_t ret = EFI_SUCCESS;
  330. /* sanity check */
  331. if (capsule_data->header_size < sizeof(*capsule) ||
  332. capsule_data->header_size >= capsule_data->capsule_image_size)
  333. return EFI_INVALID_PARAMETER;
  334. capsule = (void *)capsule_data + capsule_data->header_size;
  335. capsule_size = capsule_data->capsule_image_size
  336. - capsule_data->header_size;
  337. if (capsule->version != 0x00000001)
  338. return EFI_UNSUPPORTED;
  339. handles = NULL;
  340. ret = EFI_CALL(efi_locate_handle_buffer(
  341. BY_PROTOCOL,
  342. &efi_guid_firmware_management_protocol,
  343. NULL, &no_handles, (efi_handle_t **)&handles));
  344. if (ret != EFI_SUCCESS)
  345. return EFI_UNSUPPORTED;
  346. /* Payload */
  347. for (item = capsule->embedded_driver_count;
  348. item < capsule->embedded_driver_count
  349. + capsule->payload_item_count; item++) {
  350. /* sanity check */
  351. if ((capsule->item_offset_list[item] + sizeof(*image)
  352. >= capsule_size)) {
  353. log_err("Capsule does not have enough data\n");
  354. ret = EFI_INVALID_PARAMETER;
  355. goto out;
  356. }
  357. image = (void *)capsule + capsule->item_offset_list[item];
  358. if (image->version != 0x00000003) {
  359. ret = EFI_UNSUPPORTED;
  360. goto out;
  361. }
  362. /* find a device for update firmware */
  363. /* TODO: should we pass index as well, or nothing but type? */
  364. fmp = efi_fmp_find(&image->update_image_type_id,
  365. image->update_hardware_instance,
  366. handles, no_handles);
  367. if (!fmp) {
  368. log_err("FMP driver not found for firmware type %pUl, hardware instance %lld\n",
  369. &image->update_image_type_id,
  370. image->update_hardware_instance);
  371. ret = EFI_UNSUPPORTED;
  372. goto out;
  373. }
  374. /* do update */
  375. if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
  376. !(image->image_capsule_support &
  377. CAPSULE_SUPPORT_AUTHENTICATION)) {
  378. /* no signature */
  379. ret = EFI_SECURITY_VIOLATION;
  380. goto out;
  381. }
  382. image_binary = (void *)image + sizeof(*image);
  383. image_binary_size = image->update_image_size;
  384. vendor_code = image_binary + image_binary_size;
  385. if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
  386. (image->image_capsule_support &
  387. CAPSULE_SUPPORT_AUTHENTICATION)) {
  388. ret = efi_remove_auth_hdr(&image_binary,
  389. &image_binary_size);
  390. if (ret != EFI_SUCCESS)
  391. goto out;
  392. }
  393. abort_reason = NULL;
  394. ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
  395. image_binary,
  396. image_binary_size,
  397. vendor_code, NULL,
  398. &abort_reason));
  399. if (ret != EFI_SUCCESS) {
  400. log_err("Firmware update failed: %ls\n",
  401. abort_reason);
  402. efi_free_pool(abort_reason);
  403. goto out;
  404. }
  405. }
  406. out:
  407. efi_free_pool(handles);
  408. return ret;
  409. }
  410. #else
  411. static efi_status_t efi_capsule_update_firmware(
  412. struct efi_capsule_header *capsule_data)
  413. {
  414. return EFI_UNSUPPORTED;
  415. }
  416. #endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
  417. /**
  418. * efi_update_capsule() - process information from operating system
  419. * @capsule_header_array: Array of virtual address pointers
  420. * @capsule_count: Number of pointers in capsule_header_array
  421. * @scatter_gather_list: Array of physical address pointers
  422. *
  423. * This function implements the UpdateCapsule() runtime service.
  424. *
  425. * See the Unified Extensible Firmware Interface (UEFI) specification for
  426. * details.
  427. *
  428. * Return: status code
  429. */
  430. efi_status_t EFIAPI efi_update_capsule(
  431. struct efi_capsule_header **capsule_header_array,
  432. efi_uintn_t capsule_count,
  433. u64 scatter_gather_list)
  434. {
  435. struct efi_capsule_header *capsule;
  436. unsigned int i;
  437. efi_status_t ret;
  438. EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
  439. scatter_gather_list);
  440. if (!capsule_count) {
  441. ret = EFI_INVALID_PARAMETER;
  442. goto out;
  443. }
  444. ret = EFI_SUCCESS;
  445. for (i = 0, capsule = *capsule_header_array; i < capsule_count;
  446. i++, capsule = *(++capsule_header_array)) {
  447. /* sanity check */
  448. if (capsule->header_size < sizeof(*capsule) ||
  449. capsule->capsule_image_size < sizeof(*capsule)) {
  450. log_err("Capsule does not have enough data\n");
  451. continue;
  452. }
  453. log_debug("Capsule[%d] (guid:%pUl)\n",
  454. i, &capsule->capsule_guid);
  455. if (!guidcmp(&capsule->capsule_guid,
  456. &efi_guid_firmware_management_capsule_id)) {
  457. ret = efi_capsule_update_firmware(capsule);
  458. } else {
  459. log_err("Unsupported capsule type: %pUl\n",
  460. &capsule->capsule_guid);
  461. ret = EFI_UNSUPPORTED;
  462. }
  463. if (ret != EFI_SUCCESS)
  464. goto out;
  465. }
  466. if (IS_ENABLED(CONFIG_EFI_ESRT)) {
  467. /* Rebuild the ESRT to reflect any updated FW images. */
  468. ret = efi_esrt_populate();
  469. if (ret != EFI_SUCCESS)
  470. log_warning("ESRT update failed\n");
  471. }
  472. out:
  473. return EFI_EXIT(ret);
  474. }
  475. /**
  476. * efi_query_capsule_caps() - check if capsule is supported
  477. * @capsule_header_array: Array of virtual pointers
  478. * @capsule_count: Number of pointers in capsule_header_array
  479. * @maximum_capsule_size: Maximum capsule size
  480. * @reset_type: Type of reset needed for capsule update
  481. *
  482. * This function implements the QueryCapsuleCapabilities() runtime service.
  483. *
  484. * See the Unified Extensible Firmware Interface (UEFI) specification for
  485. * details.
  486. *
  487. * Return: status code
  488. */
  489. efi_status_t EFIAPI efi_query_capsule_caps(
  490. struct efi_capsule_header **capsule_header_array,
  491. efi_uintn_t capsule_count,
  492. u64 *maximum_capsule_size,
  493. u32 *reset_type)
  494. {
  495. struct efi_capsule_header *capsule __attribute__((unused));
  496. unsigned int i;
  497. efi_status_t ret;
  498. EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
  499. maximum_capsule_size, reset_type);
  500. if (!maximum_capsule_size) {
  501. ret = EFI_INVALID_PARAMETER;
  502. goto out;
  503. }
  504. *maximum_capsule_size = U64_MAX;
  505. *reset_type = EFI_RESET_COLD;
  506. ret = EFI_SUCCESS;
  507. for (i = 0, capsule = *capsule_header_array; i < capsule_count;
  508. i++, capsule = *(++capsule_header_array)) {
  509. /* TODO */
  510. }
  511. out:
  512. return EFI_EXIT(ret);
  513. }
  514. #ifdef CONFIG_EFI_CAPSULE_ON_DISK
  515. /**
  516. * get_dp_device - retrieve a device path from boot variable
  517. * @boot_var: Boot variable name
  518. * @device_dp Device path
  519. *
  520. * Retrieve a device patch from boot variable, @boot_var.
  521. *
  522. * Return: status code
  523. */
  524. static efi_status_t get_dp_device(u16 *boot_var,
  525. struct efi_device_path **device_dp)
  526. {
  527. void *buf = NULL;
  528. efi_uintn_t size;
  529. struct efi_load_option lo;
  530. struct efi_device_path *file_dp;
  531. efi_status_t ret;
  532. size = 0;
  533. ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
  534. NULL, &size, NULL, NULL);
  535. if (ret == EFI_BUFFER_TOO_SMALL) {
  536. buf = malloc(size);
  537. if (!buf)
  538. return EFI_OUT_OF_RESOURCES;
  539. ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
  540. NULL, &size, buf, NULL);
  541. }
  542. if (ret != EFI_SUCCESS)
  543. return ret;
  544. efi_deserialize_load_option(&lo, buf, &size);
  545. if (lo.attributes & LOAD_OPTION_ACTIVE) {
  546. efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
  547. efi_free_pool(file_dp);
  548. ret = EFI_SUCCESS;
  549. } else {
  550. ret = EFI_NOT_FOUND;
  551. }
  552. free(buf);
  553. return ret;
  554. }
  555. /**
  556. * device_is_present_and_system_part - check if a device exists
  557. * @dp Device path
  558. *
  559. * Check if a device pointed to by the device path, @dp, exists and is
  560. * located in UEFI system partition.
  561. *
  562. * Return: true - yes, false - no
  563. */
  564. static bool device_is_present_and_system_part(struct efi_device_path *dp)
  565. {
  566. efi_handle_t handle;
  567. handle = efi_dp_find_obj(dp, NULL);
  568. if (!handle)
  569. return false;
  570. return efi_disk_is_system_part(handle);
  571. }
  572. /**
  573. * find_boot_device - identify the boot device
  574. *
  575. * Identify the boot device from boot-related variables as UEFI
  576. * specification describes and put its handle into bootdev_root.
  577. *
  578. * Return: status code
  579. */
  580. static efi_status_t find_boot_device(void)
  581. {
  582. char boot_var[9];
  583. u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
  584. efi_uintn_t size;
  585. int i, num;
  586. struct efi_simple_file_system_protocol *volume;
  587. struct efi_device_path *boot_dev = NULL;
  588. efi_status_t ret;
  589. /* find active boot device in BootNext */
  590. bootnext = 0;
  591. size = sizeof(bootnext);
  592. ret = efi_get_variable_int(L"BootNext",
  593. (efi_guid_t *)&efi_global_variable_guid,
  594. NULL, &size, &bootnext, NULL);
  595. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  596. /* BootNext does exist here */
  597. if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
  598. log_err("BootNext must be 16-bit integer\n");
  599. goto skip;
  600. }
  601. sprintf((char *)boot_var, "Boot%04X", bootnext);
  602. p = boot_var16;
  603. utf8_utf16_strcpy(&p, boot_var);
  604. ret = get_dp_device(boot_var16, &boot_dev);
  605. if (ret == EFI_SUCCESS) {
  606. if (device_is_present_and_system_part(boot_dev)) {
  607. goto found;
  608. } else {
  609. efi_free_pool(boot_dev);
  610. boot_dev = NULL;
  611. }
  612. }
  613. }
  614. skip:
  615. /* find active boot device in BootOrder */
  616. size = 0;
  617. ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid,
  618. NULL, &size, NULL, NULL);
  619. if (ret == EFI_BUFFER_TOO_SMALL) {
  620. boot_order = malloc(size);
  621. if (!boot_order) {
  622. ret = EFI_OUT_OF_RESOURCES;
  623. goto out;
  624. }
  625. ret = efi_get_variable_int(L"BootOrder",
  626. &efi_global_variable_guid,
  627. NULL, &size, boot_order, NULL);
  628. }
  629. if (ret != EFI_SUCCESS)
  630. goto out;
  631. /* check in higher order */
  632. num = size / sizeof(u16);
  633. for (i = 0; i < num; i++) {
  634. sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
  635. p = boot_var16;
  636. utf8_utf16_strcpy(&p, boot_var);
  637. ret = get_dp_device(boot_var16, &boot_dev);
  638. if (ret != EFI_SUCCESS)
  639. continue;
  640. if (device_is_present_and_system_part(boot_dev))
  641. break;
  642. efi_free_pool(boot_dev);
  643. boot_dev = NULL;
  644. }
  645. found:
  646. if (boot_dev) {
  647. log_debug("Boot device %pD\n", boot_dev);
  648. volume = efi_fs_from_path(boot_dev);
  649. if (!volume)
  650. ret = EFI_DEVICE_ERROR;
  651. else
  652. ret = EFI_CALL(volume->open_volume(volume,
  653. &bootdev_root));
  654. efi_free_pool(boot_dev);
  655. } else {
  656. ret = EFI_NOT_FOUND;
  657. }
  658. out:
  659. free(boot_order);
  660. return ret;
  661. }
  662. /**
  663. * efi_capsule_scan_dir - traverse a capsule directory in boot device
  664. * @files: Array of file names
  665. * @num: Number of elements in @files
  666. *
  667. * Traverse a capsule directory in boot device.
  668. * Called by initialization code, and returns an array of capsule file
  669. * names in @files.
  670. *
  671. * Return: status code
  672. */
  673. static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
  674. {
  675. struct efi_file_handle *dirh;
  676. struct efi_file_info *dirent;
  677. efi_uintn_t dirent_size, tmp_size;
  678. unsigned int count;
  679. u16 **tmp_files;
  680. efi_status_t ret;
  681. ret = find_boot_device();
  682. if (ret == EFI_NOT_FOUND) {
  683. log_debug("Boot device is not set\n");
  684. *num = 0;
  685. return EFI_SUCCESS;
  686. } else if (ret != EFI_SUCCESS) {
  687. return EFI_DEVICE_ERROR;
  688. }
  689. /* count capsule files */
  690. ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
  691. EFI_CAPSULE_DIR,
  692. EFI_FILE_MODE_READ, 0));
  693. if (ret != EFI_SUCCESS) {
  694. *num = 0;
  695. return EFI_SUCCESS;
  696. }
  697. dirent_size = 256;
  698. dirent = malloc(dirent_size);
  699. if (!dirent)
  700. return EFI_OUT_OF_RESOURCES;
  701. count = 0;
  702. while (1) {
  703. tmp_size = dirent_size;
  704. ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
  705. if (ret == EFI_BUFFER_TOO_SMALL) {
  706. struct efi_file_info *old_dirent = dirent;
  707. dirent = realloc(dirent, tmp_size);
  708. if (!dirent) {
  709. dirent = old_dirent;
  710. ret = EFI_OUT_OF_RESOURCES;
  711. goto err;
  712. }
  713. dirent_size = tmp_size;
  714. ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
  715. }
  716. if (ret != EFI_SUCCESS)
  717. goto err;
  718. if (!tmp_size)
  719. break;
  720. if (!(dirent->attribute & EFI_FILE_DIRECTORY))
  721. count++;
  722. }
  723. ret = EFI_CALL((*dirh->setpos)(dirh, 0));
  724. if (ret != EFI_SUCCESS)
  725. goto err;
  726. /* make a list */
  727. tmp_files = malloc(count * sizeof(*tmp_files));
  728. if (!tmp_files) {
  729. ret = EFI_OUT_OF_RESOURCES;
  730. goto err;
  731. }
  732. count = 0;
  733. while (1) {
  734. tmp_size = dirent_size;
  735. ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
  736. if (ret != EFI_SUCCESS)
  737. goto err;
  738. if (!tmp_size)
  739. break;
  740. if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
  741. u16_strcmp(dirent->file_name, L".") &&
  742. u16_strcmp(dirent->file_name, L".."))
  743. tmp_files[count++] = u16_strdup(dirent->file_name);
  744. }
  745. /* ignore an error */
  746. EFI_CALL((*dirh->close)(dirh));
  747. /* in ascii order */
  748. /* FIXME: u16 version of strcasecmp */
  749. qsort(tmp_files, count, sizeof(*tmp_files),
  750. (int (*)(const void *, const void *))strcasecmp);
  751. *files = tmp_files;
  752. *num = count;
  753. ret = EFI_SUCCESS;
  754. err:
  755. free(dirent);
  756. return ret;
  757. }
  758. /**
  759. * efi_capsule_read_file - read in a capsule file
  760. * @filename: File name
  761. * @capsule: Pointer to buffer for capsule
  762. *
  763. * Read a capsule file and put its content in @capsule.
  764. *
  765. * Return: status code
  766. */
  767. static efi_status_t efi_capsule_read_file(const u16 *filename,
  768. struct efi_capsule_header **capsule)
  769. {
  770. struct efi_file_handle *dirh, *fh;
  771. struct efi_file_info *file_info = NULL;
  772. struct efi_capsule_header *buf = NULL;
  773. efi_uintn_t size;
  774. efi_status_t ret;
  775. ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
  776. EFI_CAPSULE_DIR,
  777. EFI_FILE_MODE_READ, 0));
  778. if (ret != EFI_SUCCESS)
  779. return ret;
  780. ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
  781. EFI_FILE_MODE_READ, 0));
  782. /* ignore an error */
  783. EFI_CALL((*dirh->close)(dirh));
  784. if (ret != EFI_SUCCESS)
  785. return ret;
  786. /* file size */
  787. size = 0;
  788. ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
  789. &size, file_info));
  790. if (ret == EFI_BUFFER_TOO_SMALL) {
  791. file_info = malloc(size);
  792. if (!file_info) {
  793. ret = EFI_OUT_OF_RESOURCES;
  794. goto err;
  795. }
  796. ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
  797. &size, file_info));
  798. }
  799. if (ret != EFI_SUCCESS)
  800. goto err;
  801. size = file_info->file_size;
  802. free(file_info);
  803. buf = malloc(size);
  804. if (!buf) {
  805. ret = EFI_OUT_OF_RESOURCES;
  806. goto err;
  807. }
  808. /* fetch data */
  809. ret = EFI_CALL((*fh->read)(fh, &size, buf));
  810. if (ret == EFI_SUCCESS) {
  811. if (size >= buf->capsule_image_size) {
  812. *capsule = buf;
  813. } else {
  814. free(buf);
  815. ret = EFI_INVALID_PARAMETER;
  816. }
  817. } else {
  818. free(buf);
  819. }
  820. err:
  821. EFI_CALL((*fh->close)(fh));
  822. return ret;
  823. }
  824. /**
  825. * efi_capsule_delete_file - delete a capsule file
  826. * @filename: File name
  827. *
  828. * Delete a capsule file from capsule directory.
  829. *
  830. * Return: status code
  831. */
  832. static efi_status_t efi_capsule_delete_file(const u16 *filename)
  833. {
  834. struct efi_file_handle *dirh, *fh;
  835. efi_status_t ret;
  836. ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
  837. EFI_CAPSULE_DIR,
  838. EFI_FILE_MODE_READ, 0));
  839. if (ret != EFI_SUCCESS)
  840. return ret;
  841. ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
  842. EFI_FILE_MODE_READ, 0));
  843. /* ignore an error */
  844. EFI_CALL((*dirh->close)(dirh));
  845. if (ret == EFI_SUCCESS)
  846. ret = EFI_CALL((*fh->delete)(fh));
  847. return ret;
  848. }
  849. /**
  850. * efi_capsule_scan_done - reset a scan help function
  851. *
  852. * Reset a scan help function
  853. */
  854. static void efi_capsule_scan_done(void)
  855. {
  856. EFI_CALL((*bootdev_root->close)(bootdev_root));
  857. bootdev_root = NULL;
  858. }
  859. /**
  860. * efi_load_capsule_drivers - initialize capsule drivers
  861. *
  862. * Generic FMP drivers backed by DFU
  863. *
  864. * Return: status code
  865. */
  866. efi_status_t __weak efi_load_capsule_drivers(void)
  867. {
  868. __maybe_unused efi_handle_t handle;
  869. efi_status_t ret = EFI_SUCCESS;
  870. if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
  871. handle = NULL;
  872. ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
  873. &handle, &efi_guid_firmware_management_protocol,
  874. &efi_fmp_fit, NULL));
  875. }
  876. if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
  877. handle = NULL;
  878. ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
  879. &handle,
  880. &efi_guid_firmware_management_protocol,
  881. &efi_fmp_raw, NULL));
  882. }
  883. return ret;
  884. }
  885. /**
  886. * check_run_capsules - Check whether capsule update should run
  887. *
  888. * The spec says OsIndications must be set in order to run the capsule update
  889. * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
  890. * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
  891. */
  892. static bool check_run_capsules(void)
  893. {
  894. u64 os_indications;
  895. efi_uintn_t size;
  896. efi_status_t ret;
  897. if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS))
  898. return true;
  899. size = sizeof(os_indications);
  900. ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
  901. NULL, &size, &os_indications, NULL);
  902. if (ret == EFI_SUCCESS &&
  903. (os_indications
  904. & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
  905. return true;
  906. return false;
  907. }
  908. /**
  909. * efi_launch_capsule - launch capsules
  910. *
  911. * Launch all the capsules in system at boot time.
  912. * Called by efi init code
  913. *
  914. * Return: status codde
  915. */
  916. efi_status_t efi_launch_capsules(void)
  917. {
  918. struct efi_capsule_header *capsule = NULL;
  919. u16 **files;
  920. unsigned int nfiles, index, i;
  921. efi_status_t ret;
  922. if (!check_run_capsules())
  923. return EFI_SUCCESS;
  924. index = get_last_capsule();
  925. /*
  926. * Find capsules on disk.
  927. * All the capsules are collected at the beginning because
  928. * capsule files will be removed instantly.
  929. */
  930. nfiles = 0;
  931. files = NULL;
  932. ret = efi_capsule_scan_dir(&files, &nfiles);
  933. if (ret != EFI_SUCCESS)
  934. return ret;
  935. if (!nfiles)
  936. return EFI_SUCCESS;
  937. /* Launch capsules */
  938. for (i = 0, ++index; i < nfiles; i++, index++) {
  939. log_debug("Applying %ls\n", files[i]);
  940. if (index > 0xffff)
  941. index = 0;
  942. ret = efi_capsule_read_file(files[i], &capsule);
  943. if (ret == EFI_SUCCESS) {
  944. ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
  945. if (ret != EFI_SUCCESS)
  946. log_err("Applying capsule %ls failed\n",
  947. files[i]);
  948. free(capsule);
  949. } else {
  950. log_err("Reading capsule %ls failed\n", files[i]);
  951. }
  952. /* create CapsuleXXXX */
  953. set_capsule_result(index, capsule, ret);
  954. /* delete a capsule either in case of success or failure */
  955. ret = efi_capsule_delete_file(files[i]);
  956. if (ret != EFI_SUCCESS)
  957. log_err("Deleting capsule %ls failed\n",
  958. files[i]);
  959. }
  960. efi_capsule_scan_done();
  961. for (i = 0; i < nfiles; i++)
  962. free(files[i]);
  963. free(files);
  964. return ret;
  965. }
  966. #endif /* CONFIG_EFI_CAPSULE_ON_DISK */