efi_capsule.c 28 KB

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