efi_capsule.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI Capsule
  4. *
  5. * Copyright (c) 2018 Linaro Limited
  6. * Author: AKASHI Takahiro
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <efi_variable.h>
  11. #include <fs.h>
  12. #include <malloc.h>
  13. #include <mapmem.h>
  14. #include <sort.h>
  15. const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
  16. #ifdef CONFIG_EFI_CAPSULE_ON_DISK
  17. /* for file system access */
  18. static struct efi_file_handle *bootdev_root;
  19. #endif
  20. /**
  21. * get_last_capsule - get the last capsule index
  22. *
  23. * Retrieve the index of the capsule invoked last time from "CapsuleLast"
  24. * variable.
  25. *
  26. * Return:
  27. * * > 0 - the last capsule index invoked
  28. * * 0xffff - on error, or no capsule invoked yet
  29. */
  30. static __maybe_unused unsigned int get_last_capsule(void)
  31. {
  32. u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
  33. char value[11], *p;
  34. efi_uintn_t size;
  35. unsigned long index = 0xffff;
  36. efi_status_t ret;
  37. size = sizeof(value16);
  38. ret = efi_get_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
  39. NULL, &size, value16, NULL);
  40. if (ret != EFI_SUCCESS || u16_strncmp(value16, L"Capsule", 7))
  41. goto err;
  42. p = value;
  43. utf16_utf8_strcpy(&p, value16);
  44. strict_strtoul(&value[7], 16, &index);
  45. err:
  46. return index;
  47. }
  48. /**
  49. * set_capsule_result - set a result variable
  50. * @capsule: Capsule
  51. * @return_status: Return status
  52. *
  53. * Create and set a result variable, "CapsuleXXXX", for the capsule,
  54. * @capsule.
  55. */
  56. static __maybe_unused
  57. void set_capsule_result(int index, struct efi_capsule_header *capsule,
  58. efi_status_t return_status)
  59. {
  60. u16 variable_name16[12];
  61. struct efi_capsule_result_variable_header result;
  62. struct efi_time time;
  63. efi_status_t ret;
  64. efi_create_indexed_name(variable_name16, "Capsule", index);
  65. result.variable_total_size = sizeof(result);
  66. result.capsule_guid = capsule->capsule_guid;
  67. ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
  68. if (ret == EFI_SUCCESS)
  69. memcpy(&result.capsule_processed, &time, sizeof(time));
  70. else
  71. memset(&result.capsule_processed, 0, sizeof(time));
  72. result.capsule_status = return_status;
  73. ret = efi_set_variable(variable_name16, &efi_guid_capsule_report,
  74. EFI_VARIABLE_NON_VOLATILE |
  75. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  76. EFI_VARIABLE_RUNTIME_ACCESS,
  77. sizeof(result), &result);
  78. if (ret)
  79. printf("EFI: creating %ls failed\n", variable_name16);
  80. }
  81. /**
  82. * efi_update_capsule() - process information from operating system
  83. * @capsule_header_array: Array of virtual address pointers
  84. * @capsule_count: Number of pointers in capsule_header_array
  85. * @scatter_gather_list: Array of physical address pointers
  86. *
  87. * This function implements the UpdateCapsule() runtime service.
  88. *
  89. * See the Unified Extensible Firmware Interface (UEFI) specification for
  90. * details.
  91. *
  92. * Return: status code
  93. */
  94. efi_status_t EFIAPI efi_update_capsule(
  95. struct efi_capsule_header **capsule_header_array,
  96. efi_uintn_t capsule_count,
  97. u64 scatter_gather_list)
  98. {
  99. struct efi_capsule_header *capsule;
  100. unsigned int i;
  101. efi_status_t ret;
  102. EFI_ENTRY("%p, %lu, %llu\n", capsule_header_array, capsule_count,
  103. scatter_gather_list);
  104. if (!capsule_count) {
  105. ret = EFI_INVALID_PARAMETER;
  106. goto out;
  107. }
  108. ret = EFI_UNSUPPORTED;
  109. for (i = 0, capsule = *capsule_header_array; i < capsule_count;
  110. i++, capsule = *(++capsule_header_array)) {
  111. }
  112. out:
  113. return EFI_EXIT(ret);
  114. }
  115. /**
  116. * efi_query_capsule_caps() - check if capsule is supported
  117. * @capsule_header_array: Array of virtual pointers
  118. * @capsule_count: Number of pointers in capsule_header_array
  119. * @maximum_capsule_size: Maximum capsule size
  120. * @reset_type: Type of reset needed for capsule update
  121. *
  122. * This function implements the QueryCapsuleCapabilities() runtime service.
  123. *
  124. * See the Unified Extensible Firmware Interface (UEFI) specification for
  125. * details.
  126. *
  127. * Return: status code
  128. */
  129. efi_status_t EFIAPI efi_query_capsule_caps(
  130. struct efi_capsule_header **capsule_header_array,
  131. efi_uintn_t capsule_count,
  132. u64 *maximum_capsule_size,
  133. u32 *reset_type)
  134. {
  135. struct efi_capsule_header *capsule __attribute__((unused));
  136. unsigned int i;
  137. efi_status_t ret;
  138. EFI_ENTRY("%p, %lu, %p, %p\n", capsule_header_array, capsule_count,
  139. maximum_capsule_size, reset_type);
  140. if (!maximum_capsule_size) {
  141. ret = EFI_INVALID_PARAMETER;
  142. goto out;
  143. }
  144. *maximum_capsule_size = U64_MAX;
  145. *reset_type = EFI_RESET_COLD;
  146. ret = EFI_SUCCESS;
  147. for (i = 0, capsule = *capsule_header_array; i < capsule_count;
  148. i++, capsule = *(++capsule_header_array)) {
  149. /* TODO */
  150. }
  151. out:
  152. return EFI_EXIT(ret);
  153. }
  154. #ifdef CONFIG_EFI_CAPSULE_ON_DISK
  155. /**
  156. * get_dp_device - retrieve a device path from boot variable
  157. * @boot_var: Boot variable name
  158. * @device_dp Device path
  159. *
  160. * Retrieve a device patch from boot variable, @boot_var.
  161. *
  162. * Return: status code
  163. */
  164. static efi_status_t get_dp_device(u16 *boot_var,
  165. struct efi_device_path **device_dp)
  166. {
  167. void *buf = NULL;
  168. efi_uintn_t size;
  169. struct efi_load_option lo;
  170. struct efi_device_path *file_dp;
  171. efi_status_t ret;
  172. size = 0;
  173. ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
  174. NULL, &size, NULL, NULL);
  175. if (ret == EFI_BUFFER_TOO_SMALL) {
  176. buf = malloc(size);
  177. if (!buf)
  178. return EFI_OUT_OF_RESOURCES;
  179. ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
  180. NULL, &size, buf, NULL);
  181. }
  182. if (ret != EFI_SUCCESS)
  183. return ret;
  184. efi_deserialize_load_option(&lo, buf, &size);
  185. if (lo.attributes & LOAD_OPTION_ACTIVE) {
  186. efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
  187. efi_free_pool(file_dp);
  188. ret = EFI_SUCCESS;
  189. } else {
  190. ret = EFI_NOT_FOUND;
  191. }
  192. free(buf);
  193. return ret;
  194. }
  195. /**
  196. * device_is_present_and_system_part - check if a device exists
  197. * @dp Device path
  198. *
  199. * Check if a device pointed to by the device path, @dp, exists and is
  200. * located in UEFI system partition.
  201. *
  202. * Return: true - yes, false - no
  203. */
  204. static bool device_is_present_and_system_part(struct efi_device_path *dp)
  205. {
  206. efi_handle_t handle;
  207. handle = efi_dp_find_obj(dp, NULL);
  208. if (!handle)
  209. return false;
  210. return efi_disk_is_system_part(handle);
  211. }
  212. /**
  213. * find_boot_device - identify the boot device
  214. *
  215. * Identify the boot device from boot-related variables as UEFI
  216. * specification describes and put its handle into bootdev_root.
  217. *
  218. * Return: status code
  219. */
  220. static efi_status_t find_boot_device(void)
  221. {
  222. char boot_var[9];
  223. u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
  224. efi_uintn_t size;
  225. int i, num;
  226. struct efi_simple_file_system_protocol *volume;
  227. struct efi_device_path *boot_dev = NULL;
  228. efi_status_t ret;
  229. /* find active boot device in BootNext */
  230. bootnext = 0;
  231. size = sizeof(bootnext);
  232. ret = efi_get_variable_int(L"BootNext",
  233. (efi_guid_t *)&efi_global_variable_guid,
  234. NULL, &size, &bootnext, NULL);
  235. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  236. /* BootNext does exist here */
  237. if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
  238. printf("BootNext must be 16-bit integer\n");
  239. goto skip;
  240. }
  241. sprintf((char *)boot_var, "Boot%04X", bootnext);
  242. p = boot_var16;
  243. utf8_utf16_strcpy(&p, boot_var);
  244. ret = get_dp_device(boot_var16, &boot_dev);
  245. if (ret == EFI_SUCCESS) {
  246. if (device_is_present_and_system_part(boot_dev)) {
  247. goto out;
  248. } else {
  249. efi_free_pool(boot_dev);
  250. boot_dev = NULL;
  251. }
  252. }
  253. }
  254. skip:
  255. /* find active boot device in BootOrder */
  256. size = 0;
  257. ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid,
  258. NULL, &size, NULL, NULL);
  259. if (ret == EFI_BUFFER_TOO_SMALL) {
  260. boot_order = malloc(size);
  261. if (!boot_order) {
  262. ret = EFI_OUT_OF_RESOURCES;
  263. goto out;
  264. }
  265. ret = efi_get_variable_int(L"BootOrder",
  266. &efi_global_variable_guid,
  267. NULL, &size, boot_order, NULL);
  268. }
  269. if (ret != EFI_SUCCESS)
  270. goto out;
  271. /* check in higher order */
  272. num = size / sizeof(u16);
  273. for (i = 0; i < num; i++) {
  274. sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
  275. p = boot_var16;
  276. utf8_utf16_strcpy(&p, boot_var);
  277. ret = get_dp_device(boot_var16, &boot_dev);
  278. if (ret != EFI_SUCCESS)
  279. continue;
  280. if (device_is_present_and_system_part(boot_dev))
  281. break;
  282. efi_free_pool(boot_dev);
  283. boot_dev = NULL;
  284. }
  285. out:
  286. if (boot_dev) {
  287. u16 *path_str;
  288. path_str = efi_dp_str(boot_dev);
  289. EFI_PRINT("EFI Capsule: bootdev is %ls\n", path_str);
  290. efi_free_pool(path_str);
  291. volume = efi_fs_from_path(boot_dev);
  292. if (!volume)
  293. ret = EFI_DEVICE_ERROR;
  294. else
  295. ret = EFI_CALL(volume->open_volume(volume,
  296. &bootdev_root));
  297. efi_free_pool(boot_dev);
  298. } else {
  299. ret = EFI_NOT_FOUND;
  300. }
  301. free(boot_order);
  302. return ret;
  303. }
  304. /**
  305. * efi_capsule_scan_dir - traverse a capsule directory in boot device
  306. * @files: Array of file names
  307. * @num: Number of elements in @files
  308. *
  309. * Traverse a capsule directory in boot device.
  310. * Called by initialization code, and returns an array of capsule file
  311. * names in @files.
  312. *
  313. * Return: status code
  314. */
  315. static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
  316. {
  317. struct efi_file_handle *dirh;
  318. struct efi_file_info *dirent;
  319. efi_uintn_t dirent_size, tmp_size;
  320. unsigned int count;
  321. u16 **tmp_files;
  322. efi_status_t ret;
  323. ret = find_boot_device();
  324. if (ret == EFI_NOT_FOUND) {
  325. EFI_PRINT("EFI Capsule: bootdev is not set\n");
  326. *num = 0;
  327. return EFI_SUCCESS;
  328. } else if (ret != EFI_SUCCESS) {
  329. return EFI_DEVICE_ERROR;
  330. }
  331. /* count capsule files */
  332. ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
  333. EFI_CAPSULE_DIR,
  334. EFI_FILE_MODE_READ, 0));
  335. if (ret != EFI_SUCCESS) {
  336. *num = 0;
  337. return EFI_SUCCESS;
  338. }
  339. dirent_size = 256;
  340. dirent = malloc(dirent_size);
  341. if (!dirent)
  342. return EFI_OUT_OF_RESOURCES;
  343. count = 0;
  344. while (1) {
  345. tmp_size = dirent_size;
  346. ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
  347. if (ret == EFI_BUFFER_TOO_SMALL) {
  348. dirent = realloc(dirent, tmp_size);
  349. if (!dirent) {
  350. ret = EFI_OUT_OF_RESOURCES;
  351. goto err;
  352. }
  353. dirent_size = tmp_size;
  354. ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
  355. }
  356. if (ret != EFI_SUCCESS)
  357. goto err;
  358. if (!tmp_size)
  359. break;
  360. if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
  361. u16_strcmp(dirent->file_name, L".") &&
  362. u16_strcmp(dirent->file_name, L".."))
  363. count++;
  364. }
  365. ret = EFI_CALL((*dirh->setpos)(dirh, 0));
  366. if (ret != EFI_SUCCESS)
  367. goto err;
  368. /* make a list */
  369. tmp_files = malloc(count * sizeof(*files));
  370. if (!tmp_files) {
  371. ret = EFI_OUT_OF_RESOURCES;
  372. goto err;
  373. }
  374. count = 0;
  375. while (1) {
  376. tmp_size = dirent_size;
  377. ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
  378. if (ret != EFI_SUCCESS)
  379. goto err;
  380. if (!tmp_size)
  381. break;
  382. if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
  383. u16_strcmp(dirent->file_name, L".") &&
  384. u16_strcmp(dirent->file_name, L".."))
  385. tmp_files[count++] = u16_strdup(dirent->file_name);
  386. }
  387. /* ignore an error */
  388. EFI_CALL((*dirh->close)(dirh));
  389. /* in ascii order */
  390. /* FIXME: u16 version of strcasecmp */
  391. qsort(tmp_files, count, sizeof(*tmp_files),
  392. (int (*)(const void *, const void *))strcasecmp);
  393. *files = tmp_files;
  394. *num = count;
  395. ret = EFI_SUCCESS;
  396. err:
  397. free(dirent);
  398. return ret;
  399. }
  400. /**
  401. * efi_capsule_read_file - read in a capsule file
  402. * @filename: File name
  403. * @capsule: Pointer to buffer for capsule
  404. *
  405. * Read a capsule file and put its content in @capsule.
  406. *
  407. * Return: status code
  408. */
  409. static efi_status_t efi_capsule_read_file(const u16 *filename,
  410. struct efi_capsule_header **capsule)
  411. {
  412. struct efi_file_handle *dirh, *fh;
  413. struct efi_file_info *file_info = NULL;
  414. struct efi_capsule_header *buf = NULL;
  415. efi_uintn_t size;
  416. efi_status_t ret;
  417. ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
  418. EFI_CAPSULE_DIR,
  419. EFI_FILE_MODE_READ, 0));
  420. if (ret != EFI_SUCCESS)
  421. return ret;
  422. ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
  423. EFI_FILE_MODE_READ, 0));
  424. /* ignore an error */
  425. EFI_CALL((*dirh->close)(dirh));
  426. if (ret != EFI_SUCCESS)
  427. return ret;
  428. /* file size */
  429. size = 0;
  430. ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
  431. &size, file_info));
  432. if (ret == EFI_BUFFER_TOO_SMALL) {
  433. file_info = malloc(size);
  434. if (!file_info) {
  435. ret = EFI_OUT_OF_RESOURCES;
  436. goto err;
  437. }
  438. ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
  439. &size, file_info));
  440. }
  441. if (ret != EFI_SUCCESS)
  442. goto err;
  443. size = file_info->file_size;
  444. free(file_info);
  445. buf = malloc(size);
  446. if (!buf) {
  447. ret = EFI_OUT_OF_RESOURCES;
  448. goto err;
  449. }
  450. /* fetch data */
  451. ret = EFI_CALL((*fh->read)(fh, &size, buf));
  452. if (ret == EFI_SUCCESS) {
  453. if (size >= buf->capsule_image_size) {
  454. *capsule = buf;
  455. } else {
  456. free(buf);
  457. ret = EFI_INVALID_PARAMETER;
  458. }
  459. } else {
  460. free(buf);
  461. }
  462. err:
  463. EFI_CALL((*fh->close)(fh));
  464. return ret;
  465. }
  466. /**
  467. * efi_capsule_delete_file - delete a capsule file
  468. * @filename: File name
  469. *
  470. * Delete a capsule file from capsule directory.
  471. *
  472. * Return: status code
  473. */
  474. static efi_status_t efi_capsule_delete_file(const u16 *filename)
  475. {
  476. struct efi_file_handle *dirh, *fh;
  477. efi_status_t ret;
  478. ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
  479. EFI_CAPSULE_DIR,
  480. EFI_FILE_MODE_READ, 0));
  481. if (ret != EFI_SUCCESS)
  482. return ret;
  483. ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
  484. EFI_FILE_MODE_READ, 0));
  485. /* ignore an error */
  486. EFI_CALL((*dirh->close)(dirh));
  487. ret = EFI_CALL((*fh->delete)(fh));
  488. return ret;
  489. }
  490. /**
  491. * efi_capsule_scan_done - reset a scan help function
  492. *
  493. * Reset a scan help function
  494. */
  495. static void efi_capsule_scan_done(void)
  496. {
  497. EFI_CALL((*bootdev_root->close)(bootdev_root));
  498. bootdev_root = NULL;
  499. }
  500. /**
  501. * arch_efi_load_capsule_drivers - initialize capsule drivers
  502. *
  503. * Architecture or board specific initialization routine
  504. *
  505. * Return: status code
  506. */
  507. efi_status_t __weak arch_efi_load_capsule_drivers(void)
  508. {
  509. return EFI_SUCCESS;
  510. }
  511. /**
  512. * efi_launch_capsule - launch capsules
  513. *
  514. * Launch all the capsules in system at boot time.
  515. * Called by efi init code
  516. *
  517. * Return: status codde
  518. */
  519. efi_status_t efi_launch_capsules(void)
  520. {
  521. u64 os_indications;
  522. efi_uintn_t size;
  523. struct efi_capsule_header *capsule = NULL;
  524. u16 **files;
  525. unsigned int nfiles, index, i;
  526. u16 variable_name16[12];
  527. efi_status_t ret;
  528. size = sizeof(os_indications);
  529. ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
  530. NULL, &size, &os_indications, NULL);
  531. if (ret != EFI_SUCCESS ||
  532. !(os_indications
  533. & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
  534. return EFI_SUCCESS;
  535. index = get_last_capsule();
  536. /* Load capsule drivers */
  537. ret = arch_efi_load_capsule_drivers();
  538. if (ret != EFI_SUCCESS)
  539. return ret;
  540. /*
  541. * Find capsules on disk.
  542. * All the capsules are collected at the beginning because
  543. * capsule files will be removed instantly.
  544. */
  545. nfiles = 0;
  546. files = NULL;
  547. ret = efi_capsule_scan_dir(&files, &nfiles);
  548. if (ret != EFI_SUCCESS)
  549. return ret;
  550. if (!nfiles)
  551. return EFI_SUCCESS;
  552. /* Launch capsules */
  553. for (i = 0, ++index; i < nfiles; i++, index++) {
  554. EFI_PRINT("capsule from %ls ...\n", files[i]);
  555. if (index > 0xffff)
  556. index = 0;
  557. ret = efi_capsule_read_file(files[i], &capsule);
  558. if (ret == EFI_SUCCESS) {
  559. ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
  560. if (ret != EFI_SUCCESS)
  561. printf("EFI Capsule update failed at %ls\n",
  562. files[i]);
  563. free(capsule);
  564. } else {
  565. printf("EFI: reading capsule failed: %ls\n",
  566. files[i]);
  567. }
  568. /* create CapsuleXXXX */
  569. set_capsule_result(index, capsule, ret);
  570. /* delete a capsule either in case of success or failure */
  571. ret = efi_capsule_delete_file(files[i]);
  572. if (ret != EFI_SUCCESS)
  573. printf("EFI: deleting a capsule file failed: %ls\n",
  574. files[i]);
  575. }
  576. efi_capsule_scan_done();
  577. for (i = 0; i < nfiles; i++)
  578. free(files[i]);
  579. free(files);
  580. /* CapsuleLast */
  581. efi_create_indexed_name(variable_name16, "Capsule", index - 1);
  582. efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
  583. EFI_VARIABLE_READ_ONLY |
  584. EFI_VARIABLE_NON_VOLATILE |
  585. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  586. EFI_VARIABLE_RUNTIME_ACCESS,
  587. 22, variable_name16, false);
  588. return ret;
  589. }
  590. #endif /* CONFIG_EFI_CAPSULE_ON_DISK */