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