efi_capsule.c 34 KB

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