load_vfy_boot.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <pal_typedefs.h>
  32. #include <pal_log.h>
  33. #include <platform/verified_boot.h>
  34. #include <platform/boot_mode.h>
  35. #include <platform/mtk_wdt.h>
  36. #include <bootimg.h>
  37. #include <load_vfy_boot.h>
  38. #include <verified_boot_error.h>
  39. #include <verified_boot_common.h>
  40. #include <avb_slot_verify.h>
  41. #include <avb_ops.h>
  42. #include <string.h>
  43. #include <vboot_state.h>
  44. #include <avb_cmdline.h>
  45. #include <libfdt.h>
  46. #include <target.h>
  47. #include <avb_user_verification.h>
  48. #include <bootctrl.h>
  49. #include <bootargs.h>
  50. #include <preload_partition.h>
  51. #include <mt_boot.h>
  52. #include <boot_info.h>
  53. #include <avb_persist.h>
  54. #include <dm_verity_status.h>
  55. #include <boot_opt.h>
  56. #include <avb_RoT.h>
  57. #include <img_workbuf.h>
  58. #include <part_interface.h>
  59. #include <platform/errno.h>
  60. #define KB (1024)
  61. #define MB (1024 * KB)
  62. #define AVB_HEAP_SZ (140 * MB)
  63. #define TMPBUF_SIZE (200)
  64. #define CMDLINE_ROOT_RAM "root=/dev/ram"
  65. #define CMDLINE_ROOT_SYSTEM_COMMON "skip_initramfs ro rootwait init=/init"
  66. extern BOOT_ARGUMENT *g_boot_arg;
  67. extern AvbOps ops;
  68. extern int sec_set_device_lock(int do_lock);
  69. unsigned long get_timer(unsigned long base);
  70. void *avb_heap;
  71. uint32_t avb_heap_sz;
  72. static const char *boot_partitions[] = {"boot", NULL, NULL};
  73. static const char *recovery_partitions[] = {"recovery", NULL};
  74. static uint32_t boot_partitions_sz = sizeof(boot_partitions) / sizeof(char *) - 1;
  75. static uint32_t recovery_partitions_sz = sizeof(recovery_partitions) / sizeof(char *) - 1;
  76. /* please keep avb_cmdline_attrs and avb_dt_attrs sync since we
  77. * use them for cmdline attrs to dt attrs conversion */
  78. static const char *avb_cmdline_attrs[] = {
  79. "androidboot.vbmeta.hash_alg",
  80. "androidboot.vbmeta.size",
  81. "androidboot.vbmeta.digest",
  82. NULL
  83. };
  84. static const char *avb_dt_attrs[] = {
  85. "vbmeta.hash_alg",
  86. "vbmeta.size",
  87. "vbmeta.digest",
  88. NULL
  89. };
  90. static int32_t build_up_boot_partitions(const char **req_parts, uint32_t parts_sz)
  91. {
  92. uint32_t i;
  93. int32_t ret = 0;
  94. /* add vendor_boot in request partitions */
  95. if (partition_exists("vendor_boot") == PART_OK) {
  96. for (i = 0 ; i < parts_sz ; i++) {
  97. if (req_parts[i] == NULL) {
  98. req_parts[i] = "vendor_boot";
  99. break;
  100. }
  101. }
  102. if (i >= parts_sz) {
  103. pal_log_err("Not enough space to add vendor_boot to request partitions\n");
  104. ret = -ENOMEM;
  105. }
  106. }
  107. return ret;
  108. }
  109. uint32_t __attribute__((weak)) collect_rot_info(AvbSlotVerifyData *slot_data)
  110. {
  111. return 0;
  112. }
  113. static uint32_t preload_partitions(uint32_t bootimg_type)
  114. {
  115. uint32_t ret = 0;
  116. uint32_t i;
  117. /* please add load address to load_addrs[] for new partition */
  118. uint32_t load_addrs[] = {0};
  119. uint32_t load_addrs_sz = sizeof(load_addrs) / sizeof(uint32_t);
  120. if (bootimg_type == BOOTIMG_TYPE_BOOT && load_addrs_sz != boot_partitions_sz)
  121. return 0x1;
  122. else if (bootimg_type == BOOTIMG_TYPE_RECOVERY && load_addrs_sz != recovery_partitions_sz)
  123. return 0x2;
  124. load_addrs[0] = (uint32_t)target_get_scratch_address();
  125. i = 0;
  126. switch (bootimg_type) {
  127. case BOOTIMG_TYPE_RECOVERY:
  128. #ifndef RECOVERY_AS_BOOT
  129. while (recovery_partitions[i] != NULL) {
  130. pal_log_err("preloading %s\n", recovery_partitions[i]);
  131. ret = preload_partition(recovery_partitions[i], load_addrs[i]);
  132. if (ret)
  133. return ret;
  134. i++;
  135. }
  136. break;
  137. #endif
  138. case BOOTIMG_TYPE_BOOT:
  139. while (boot_partitions[i] != NULL) {
  140. pal_log_err("preloading %s\n", boot_partitions[i]);
  141. ret = preload_partition(boot_partitions[i], load_addrs[i]);
  142. if (ret)
  143. return ret;
  144. i++;
  145. }
  146. break;
  147. default:
  148. break;
  149. }
  150. return ret;
  151. }
  152. static uint32_t record_avb_version(AvbSlotVerifyData *slot_data)
  153. {
  154. uint32_t ret = 0;
  155. #ifdef MTK_SECURITY_ANTI_ROLLBACK
  156. uint32_t i = 0;
  157. uint64_t min_ver = 0;
  158. if (slot_data == NULL) {
  159. ret = AVB_IO_RESULT_ERROR_IO;
  160. goto end;
  161. }
  162. min_ver = slot_data->rollback_indexes[0];
  163. if (min_ver == AVB_VER_INITIAL_VALUE) {
  164. ret = AVB_IO_RESULT_ERROR_OOM;
  165. goto end;
  166. }
  167. for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) {
  168. #ifndef RECOVERY_AS_BOOT
  169. /* recovery otp group is not handled here */
  170. if (i == RECOVERY_ROLLBACK_INDEX)
  171. continue;
  172. #endif
  173. pal_log_info("ver(%d) = %llu\n", i, slot_data->rollback_indexes[i]);
  174. if (slot_data->rollback_indexes[i] != AVB_VER_INITIAL_VALUE)
  175. if (slot_data->rollback_indexes[i] < min_ver)
  176. min_ver = slot_data->rollback_indexes[i];
  177. }
  178. #ifndef RECOVERY_AS_BOOT
  179. /* MTK_OTP_FRAMEWORK_V2 is used to create AVB and recovery otp field.
  180. * Recovery otp field is used to record recovery image version.
  181. * This option is designed for solving the problem that device are
  182. * not able to boot into recovery mode if recovery image version is
  183. * the minimum version in original non-secure group.
  184. */
  185. #ifdef MTK_OTP_FRAMEWORK_V2
  186. /* write recovery image version to recovery otp field */
  187. if (slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX] != AVB_VER_INITIAL_VALUE) {
  188. ret = set_avb_otp_ver(RECOVERY_GROUP,
  189. (uint32_t)slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX]);
  190. if (ret)
  191. goto end;
  192. }
  193. #else
  194. /* update min_ver if the recovery image version is the minimum */
  195. if (slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX] != AVB_VER_INITIAL_VALUE) {
  196. if (slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX] < min_ver)
  197. min_ver = slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX];
  198. }
  199. #endif
  200. #endif
  201. pal_log_err("[avb] min_ver = %llu\n", min_ver);
  202. /* update AVB otp field*/
  203. ret = set_avb_otp_ver(AVB_GROUP, (uint32_t)min_ver);
  204. end:
  205. #endif
  206. return ret;
  207. }
  208. static uint32_t boot_authentication(uint32_t bootimg_type)
  209. {
  210. #ifdef MTK_SECURITY_SW_SUPPORT
  211. uint32_t policy_entry_idx = 0;
  212. uint32_t img_auth_required = 0;
  213. switch (bootimg_type) {
  214. case BOOTIMG_TYPE_RECOVERY:
  215. #ifndef RECOVERY_AS_BOOT
  216. policy_entry_idx = get_policy_entry_idx(recovery_partitions[0]);
  217. break;
  218. #endif
  219. case BOOTIMG_TYPE_BOOT:
  220. policy_entry_idx = get_policy_entry_idx(boot_partitions[0]);
  221. break;
  222. default:
  223. pal_log_err("[avb] invalid boot image type\n");
  224. return 1;
  225. }
  226. img_auth_required = get_vfy_policy(policy_entry_idx);
  227. pal_log_err("[avb] img_auth_required = %d\n", img_auth_required);
  228. return img_auth_required;
  229. #else
  230. pal_log_err("[avb] img_auth_required = 0\n");
  231. return 0;
  232. #endif
  233. }
  234. static uint32_t avb_cmdline_postprocessing(char *cmdline, void *fdt)
  235. {
  236. uint32_t ret = 0;
  237. uint32_t i = 0;
  238. int nodeoffset;
  239. int fdt_ret = 0;
  240. char fdt_value[MAX_VALUE_SZ + 1] = {0};
  241. /* extract avb parameters from cmdline */
  242. for (i = 0; ; i++) {
  243. if (avb_cmdline_attrs[i] == NULL)
  244. break;
  245. ret = avb_extract_from_cmdline(cmdline,
  246. strlen(cmdline),
  247. avb_cmdline_attrs[i]);
  248. if (ret) {
  249. pal_log_err("[avb] extract %s fails\n", avb_cmdline_attrs[i]);
  250. return -1;
  251. }
  252. }
  253. /* create /firmware/android if it does not exist */
  254. nodeoffset = fdt_path_offset(fdt, "/firmware/android");
  255. if (nodeoffset < 0) {
  256. nodeoffset = fdt_path_offset(fdt, "/firmware");
  257. if (nodeoffset < 0) {
  258. /* Add subnode "firmware" in root */
  259. nodeoffset = fdt_add_subnode(fdt, 0, "firmware");
  260. if (nodeoffset < 0) {
  261. pal_log_err("Warning: can't add firmware node in device tree\n");
  262. return -1;
  263. }
  264. }
  265. /* Add subnode "android" in "/firmware" */
  266. nodeoffset = fdt_add_subnode(fdt, nodeoffset, "android");
  267. if (nodeoffset < 0) {
  268. pal_log_err("Warning: can't add firmware/android node in device tree\n");
  269. return -1;
  270. }
  271. }
  272. /* set cmdline attributes to device tree */
  273. for (i = 0; ; i++) {
  274. if (avb_dt_attrs[i] == NULL)
  275. break;
  276. ret = avb_get_cmdline(avb_cmdline_attrs[i], fdt_value, MAX_VALUE_SZ);
  277. if (ret == STATUS_OK) {
  278. fdt_ret = fdt_setprop_string(fdt, nodeoffset, avb_dt_attrs[i], fdt_value);
  279. if (fdt_ret) {
  280. pal_log_err("Error: can't set property in device tree\n");
  281. return -1;
  282. }
  283. }
  284. /* we allow the case that entry is not found */
  285. if (ret == ERR_AVB_CMDLINE_ENTRY_NOT_FOUND)
  286. ret = STATUS_OK;
  287. }
  288. return ret;
  289. }
  290. static uint32_t boot_post_processing(AvbOps *ops, uint32_t bootimg_type,
  291. AvbSlotVerifyData *slot_data)
  292. {
  293. uint32_t ret = 0;
  294. uint32_t i = 0;
  295. const char *suffix = NULL;
  296. bool out_verification_enabled = true;
  297. #ifndef PRELOAD_PARTITION_SUPPORT
  298. void *boot_load_addr = 0;
  299. #endif
  300. char *part_name = get_bootimg_partition_name(bootimg_type);
  301. #ifdef MTK_AB_OTA_UPDATER
  302. char vboot_cmdline_ab[TMPBUF_SIZE] = {0};
  303. #endif
  304. if (part_name == NULL)
  305. return AVB_IO_RESULT_ERROR_IO;
  306. if (slot_data == NULL)
  307. return AVB_IO_RESULT_ERROR_IO;
  308. ret = collect_rot_info(slot_data);
  309. if (ret)
  310. return AVB_IO_RESULT_ERROR_IO;
  311. #ifndef PRELOAD_PARTITION_SUPPORT
  312. /* relocate boot image from slot data to its target buffer */
  313. for (i = 0; i < slot_data->num_loaded_partitions; i++) {
  314. boot_load_addr = bootimg_alloc_work_buf(slot_data->loaded_partitions[i].partition_name,
  315. slot_data->loaded_partitions[i].data_size);
  316. if (!boot_load_addr)
  317. panic("fail to allocate boot img workbuf\n");
  318. memcpy((void *)boot_load_addr,
  319. (void *)((uint8_t *)slot_data->loaded_partitions[i].data),
  320. slot_data->loaded_partitions[i].data_size);
  321. }
  322. #endif
  323. load_bootinfo_bootimg(boot_load_addr);
  324. prepare_kernel_dtb();
  325. #ifdef SYSTEM_AS_ROOT
  326. if (bootimg_type == BOOTIMG_TYPE_BOOT) {
  327. cmdline_append(CMDLINE_ROOT_SYSTEM_COMMON);
  328. /* root file system is already in avb slot data when
  329. * BOARD_BUILD_SYSTEM_ROOT_IMAGE is true, so we don't
  330. * have to handle it again here.
  331. */
  332. } else
  333. cmdline_append(CMDLINE_ROOT_RAM);
  334. #else
  335. cmdline_append(CMDLINE_ROOT_RAM);
  336. #endif
  337. suffix = get_suffix();
  338. if (suffix == NULL) {
  339. pal_log_err("[avb] Fail to get suffix\n");
  340. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  341. goto end;
  342. }
  343. if(!avb_user_verification_get(ops, suffix, &out_verification_enabled)) {
  344. pal_log_err("[avb] Fail to get verification state\n");
  345. out_verification_enabled = true;
  346. }
  347. if (out_verification_enabled) {
  348. ret = avb_cmdline_postprocessing(slot_data->cmdline, get_kernel_fdt());
  349. if (ret)
  350. goto end;
  351. }
  352. pal_log_err("[avb] cmdline = %s\n", slot_data->cmdline);
  353. cmdline_append(slot_data->cmdline);
  354. ret = write_persist_value(ops, PERSIST_PART_NAME);
  355. if (ret)
  356. goto end;
  357. #ifdef MTK_AB_OTA_UPDATER
  358. if (snprintf(vboot_cmdline_ab, TMPBUF_SIZE,
  359. "androidboot.slot_suffix=%s androidboot.slot=%s",
  360. suffix, suffix + 1) > 0) {
  361. cmdline_append(vboot_cmdline_ab);
  362. }
  363. else {
  364. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  365. goto end;
  366. }
  367. #endif
  368. set_bootimg_loaded((uint32_t)boot_load_addr);
  369. end:
  370. return ret;
  371. }
  372. uint32_t handle_vboot_state(uint32_t bootimg_type)
  373. {
  374. uint32_t ret = STATUS_OK;
  375. int32_t vboot_state_ret = 0;
  376. /* device should not boot if any error occurs
  377. * in this function
  378. */
  379. /* be careful that error code should not be overriden by
  380. * return value of the following error handling functions.
  381. */
  382. vboot_state_ret = print_boot_state();
  383. if (vboot_state_ret)
  384. return ERR_VB_STATE_PRINT_FAIL;
  385. switch (bootimg_type) {
  386. case BOOTIMG_TYPE_RECOVERY:
  387. #ifndef RECOVERY_AS_BOOT
  388. vboot_state_ret = show_warning(recovery_partitions[0]);
  389. break;
  390. #endif
  391. case BOOTIMG_TYPE_BOOT:
  392. vboot_state_ret = show_warning(boot_partitions[0]);
  393. break;
  394. default:
  395. pal_log_err("[avb] invalid boot image type\n");
  396. return ERR_VB_STATE_SHOW_WARNING_FAIL;
  397. }
  398. if (vboot_state_ret)
  399. return ERR_VB_STATE_SHOW_WARNING_FAIL;
  400. vboot_state_ret = set_boot_state_to_cmdline();
  401. if (vboot_state_ret)
  402. return ERR_VB_STATE_SET_CMDLINE_FAIL;
  403. return ret;
  404. }
  405. int load_vfy_boot(uint32_t bootimg_type, uint32_t addr)
  406. {
  407. int ret = STATUS_OK;
  408. uint32_t img_vfy_time = 0;
  409. AvbSlotVerifyResult avb_ret = AVB_SLOT_VERIFY_RESULT_OK;
  410. AvbSlotVerifyData *slot_data = NULL;
  411. AvbSlotVerifyFlags avb_flag = AVB_SLOT_VERIFY_FLAGS_NONE;
  412. uint32_t lock_state = LKS_DEFAULT;
  413. uint32_t dm_status = 0;
  414. AvbHashtreeErrorMode hashtree_error_mode = AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE;
  415. img_vfy_time = get_timer(0);
  416. build_up_boot_partitions(boot_partitions, boot_partitions_sz);
  417. g_boot_state = BOOT_STATE_RED;
  418. /* heap initialization for avb */
  419. avb_heap_sz = AVB_HEAP_SZ;
  420. pal_log_debug("[avb] avb heap alloc size 0x%x\n", avb_heap_sz);
  421. avb_heap = (void *)(uint32_t)mblock_reserve_ext(&g_boot_arg->mblock_info,
  422. avb_heap_sz,
  423. 4 * KB,
  424. 0xc0000000,
  425. 0,
  426. "avb");
  427. if (avb_heap == 0) {
  428. pal_log_err("[avb] avb heap alloc fails\n");
  429. avb_heap_sz = 0;
  430. return -1;
  431. } else
  432. pal_log_debug("[avb] avb heap alloc 0x%x\n", (uint32_t)avb_heap);
  433. #ifdef MTK_SEC_FASTBOOT_UNLOCK_SUPPORT
  434. #if defined(MTK_DM_VERITY_OFF) || defined(MTK_BUILD_DEFAULT_UNLOCK) || !defined(MTK_SECURITY_SW_SUPPORT)
  435. /* This feature is for test purpose only.
  436. * we unlock device without wiping userdata, which should not
  437. * happen for MP product. We do this because after userdata
  438. * has been wiped, Android will fail to mount userdata and reboot
  439. * to recovery to format userdata, and then reboot. If we always
  440. * wipe userdata in boot process, Android will never get a properly
  441. * formatted userdata.
  442. */
  443. #ifdef MTK_SECURITY_SW_SUPPORT
  444. /* unlock device */
  445. if (sec_set_device_lock(0) != 0) {
  446. ret = ERR_AVB_UNLOCK_DEVICE_FAILED;
  447. goto end;
  448. }
  449. #endif
  450. /* disable verification */
  451. if (avb_user_verification_set(&ops, get_suffix(), 0) != true) {
  452. ret = ERR_AVB_SET_VERIFICATION_FAILED;
  453. goto end;
  454. }
  455. #endif
  456. #endif
  457. if (boot_authentication(bootimg_type) == 0)
  458. avb_flag = AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR;
  459. #ifdef PRELOAD_PARTITION_SUPPORT
  460. if (preload_partitions(bootimg_type)) {
  461. avb_ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  462. goto end;
  463. }
  464. #endif
  465. get_dm_verity_status(&dm_status);
  466. if (dm_status)
  467. avb_flag |= AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION;
  468. get_hash_tree_error_mode((uint32_t *)&hashtree_error_mode);
  469. ret = init_persist_value(&ops, PERSIST_PART_NAME, PERSIST_VALUE_OFFSET);
  470. if (ret) {
  471. pal_log_err("init_persist_value ret = 0x%x\n", ret);
  472. goto end;
  473. }
  474. switch (bootimg_type) {
  475. case BOOTIMG_TYPE_RECOVERY:
  476. #ifndef RECOVERY_AS_BOOT
  477. avb_flag |= AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION;
  478. avb_ret = avb_slot_verify(&ops,
  479. recovery_partitions,
  480. get_suffix(),
  481. avb_flag,
  482. hashtree_error_mode,
  483. &slot_data);
  484. if (avb_flag & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR)
  485. avb_ret = AVB_SLOT_VERIFY_RESULT_OK;
  486. if (avb_ret != AVB_SLOT_VERIFY_RESULT_OK)
  487. goto end;
  488. break;
  489. #endif
  490. case BOOTIMG_TYPE_BOOT:
  491. avb_ret = avb_slot_verify(&ops,
  492. boot_partitions,
  493. get_suffix(),
  494. avb_flag,
  495. hashtree_error_mode,
  496. &slot_data);
  497. if (avb_flag & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR)
  498. avb_ret = AVB_SLOT_VERIFY_RESULT_OK;
  499. if (avb_ret != AVB_SLOT_VERIFY_RESULT_OK)
  500. goto end;
  501. break;
  502. default:
  503. avb_ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  504. goto end;
  505. }
  506. if (avb_ret == AVB_SLOT_VERIFY_RESULT_OK) {
  507. ret = boot_post_processing(&ops, bootimg_type, slot_data);
  508. if (ret)
  509. goto end;
  510. g_boot_state = BOOT_STATE_GREEN;
  511. #ifdef MTK_SECURITY_YELLOW_STATE_SUPPORT
  512. if (vb_custom_key_exist())
  513. g_boot_state = BOOT_STATE_YELLOW;
  514. #endif
  515. if (boot_authentication(bootimg_type) != 0 &&
  516. g_boot_state == BOOT_STATE_GREEN) {
  517. ret = record_avb_version(slot_data);
  518. if (ret != AVB_IO_RESULT_OK) {
  519. pal_log_err("update avb otp version fail.\n");
  520. ret = AVB_IO_RESULT_OK;
  521. }
  522. }
  523. }
  524. end:
  525. pal_log_err("[avb] boot/recovery vfy time = %d ms\n",
  526. (unsigned int)get_timer(img_vfy_time));
  527. if (slot_data != NULL)
  528. hashtree_error_mode = slot_data->resolved_hashtree_error_mode;
  529. /* After this function call, memory is returned to lk.
  530. * Please avoid using avb_heap after this function call
  531. */
  532. mblock_create(&g_boot_arg->mblock_info,
  533. &g_boot_arg->orig_dram_info,
  534. (uint64_t)(uint32_t)avb_heap & 0xffffffff,
  535. (uint64_t)avb_heap_sz & 0xffffffff);
  536. pal_log_err("[avb] avb_ret = %d\n", avb_ret);
  537. pal_log_err("[avb] ret = %d\n", ret);
  538. /* if device state is "unlocked," override
  539. * boot state with "ORANGE."
  540. */
  541. #ifdef MTK_SECURITY_SW_SUPPORT
  542. ret = get_lock_state(&lock_state);
  543. if (ret == 0 && lock_state == LKS_UNLOCK)
  544. g_boot_state = BOOT_STATE_ORANGE;
  545. #endif
  546. dm_verity_handler((uint32_t)hashtree_error_mode);
  547. ret = vboot_state_result(bootimg_type);
  548. if (ret != STATUS_OK)
  549. return ret;
  550. if (avb_ret != AVB_SLOT_VERIFY_RESULT_OK)
  551. ret = ERR_AVB_VERIFICAITON_FAILED;
  552. return ret;
  553. }