load_vfy_boot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 <bootargs.h>
  49. #include <preload_partition.h>
  50. #include <mt_boot.h>
  51. #include <boot_info.h>
  52. #include <avb_persist.h>
  53. #include <dm_verity_status.h>
  54. #include <boot_opt.h>
  55. #include <avb_RoT.h>
  56. #define KB (1024)
  57. #define MB (1024 * KB)
  58. #define AVB_HEAP_SZ (100 * MB)
  59. #define TMPBUF_SIZE (200)
  60. #define CMDLINE_ROOT_RAM "root=/dev/ram"
  61. #define CMDLINE_ROOT_SYSTEM_COMMON "skip_initramfs ro rootwait init=/init"
  62. extern BOOT_ARGUMENT *g_boot_arg;
  63. extern AvbOps ops;
  64. unsigned long get_timer(unsigned long base);
  65. void *avb_heap;
  66. uint32_t avb_heap_sz;
  67. static const char *boot_partitions[] = {"boot", NULL};
  68. static const char *recovery_partitions[] = {"recovery", NULL};
  69. static uint32_t boot_partitions_sz = sizeof(boot_partitions) / sizeof(char *) - 1;
  70. static uint32_t recovery_partitions_sz = sizeof(recovery_partitions) / sizeof(char *) - 1;
  71. /* please keep avb_cmdline_attrs and avb_dt_attrs sync since we
  72. * use them for cmdline attrs to dt attrs conversion */
  73. static const char *avb_cmdline_attrs[] = {
  74. "androidboot.vbmeta.hash_alg",
  75. "androidboot.vbmeta.size",
  76. "androidboot.vbmeta.digest",
  77. NULL
  78. };
  79. static const char *avb_dt_attrs[] = {
  80. "vbmeta.hash_alg",
  81. "vbmeta.size",
  82. "vbmeta.digest",
  83. NULL
  84. };
  85. uint32_t __attribute__((weak)) collect_rot_info(AvbSlotVerifyData *slot_data)
  86. {
  87. return 0;
  88. }
  89. static uint32_t preload_partitions(uint32_t bootimg_type)
  90. {
  91. uint32_t ret = 0;
  92. uint32_t i;
  93. /* please add load address to load_addrs[] for new partition */
  94. uint32_t load_addrs[] = {0};
  95. uint32_t load_addrs_sz = sizeof(load_addrs) / sizeof(uint32_t);
  96. if (bootimg_type == BOOTIMG_TYPE_BOOT && load_addrs_sz != boot_partitions_sz)
  97. return 0x1;
  98. else if (bootimg_type == BOOTIMG_TYPE_RECOVERY && load_addrs_sz != recovery_partitions_sz)
  99. return 0x2;
  100. load_addrs[0] = (uint32_t)target_get_scratch_address();
  101. i = 0;
  102. switch (bootimg_type) {
  103. case BOOTIMG_TYPE_RECOVERY:
  104. #ifndef RECOVERY_AS_BOOT
  105. while (recovery_partitions[i] != NULL) {
  106. pal_log_err("preloading %s\n", recovery_partitions[i]);
  107. ret = preload_partition(recovery_partitions[i], load_addrs[i]);
  108. if (ret)
  109. return ret;
  110. i++;
  111. }
  112. break;
  113. #endif
  114. case BOOTIMG_TYPE_BOOT:
  115. while (boot_partitions[i] != NULL) {
  116. pal_log_err("preloading %s\n", boot_partitions[i]);
  117. ret = preload_partition(boot_partitions[i], load_addrs[i]);
  118. if (ret)
  119. return ret;
  120. i++;
  121. }
  122. break;
  123. default:
  124. break;
  125. }
  126. return ret;
  127. }
  128. static uint32_t record_avb_version(AvbSlotVerifyData *slot_data)
  129. {
  130. uint32_t ret = 0;
  131. #ifdef MTK_SECURITY_ANTI_ROLLBACK
  132. uint32_t i = 0;
  133. uint64_t min_ver = 0;
  134. if (slot_data == NULL) {
  135. ret = AVB_IO_RESULT_ERROR_IO;
  136. goto end;
  137. }
  138. min_ver = slot_data->rollback_indexes[0];
  139. if (min_ver == AVB_VER_INITIAL_VALUE) {
  140. ret = AVB_IO_RESULT_ERROR_OOM;
  141. goto end;
  142. }
  143. for (i = 0; i < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; i++) {
  144. /* recovery otp group is not handled here */
  145. if (i == RECOVERY_ROLLBACK_INDEX)
  146. continue;
  147. pal_log_info("ver(%d) = %llu\n", i, slot_data->rollback_indexes[i]);
  148. if (slot_data->rollback_indexes[i] != AVB_VER_INITIAL_VALUE)
  149. if (slot_data->rollback_indexes[i] < min_ver)
  150. min_ver = slot_data->rollback_indexes[i];
  151. }
  152. /* MTK_OTP_FRAMEWORK_V2 is used to create AVB and recovery otp field.
  153. * Recovery otp field is used to record recovery image version.
  154. * This option is designed for solving the problem that device are
  155. * not able to boot into recovery mode if recovery image version is
  156. * the minimum version in original non-secure group.
  157. */
  158. #ifdef MTK_OTP_FRAMEWORK_V2
  159. /* write recovery image version to recovery otp field */
  160. if (slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX] != AVB_VER_INITIAL_VALUE) {
  161. ret = set_avb_otp_ver(RECOVERY_GROUP,
  162. (uint32_t)slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX]);
  163. if (ret)
  164. goto end;
  165. }
  166. #else
  167. /* update min_ver if the recovery image version is the minimum */
  168. if (slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX] != AVB_VER_INITIAL_VALUE) {
  169. if (slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX] < min_ver)
  170. min_ver = slot_data->rollback_indexes[RECOVERY_ROLLBACK_INDEX];
  171. }
  172. #endif
  173. pal_log_err("[avb] min_ver = %llu\n", min_ver);
  174. /* update AVB otp field*/
  175. ret = set_avb_otp_ver(AVB_GROUP, (uint32_t)min_ver);
  176. end:
  177. #endif
  178. return ret;
  179. }
  180. static uint32_t boot_authentication(uint32_t bootimg_type)
  181. {
  182. #ifdef MTK_SECURITY_SW_SUPPORT
  183. uint32_t policy_entry_idx = 0;
  184. uint32_t img_auth_required = 0;
  185. switch (bootimg_type) {
  186. case BOOTIMG_TYPE_RECOVERY:
  187. #ifndef RECOVERY_AS_BOOT
  188. policy_entry_idx = get_policy_entry_idx(recovery_partitions[0]);
  189. break;
  190. #endif
  191. case BOOTIMG_TYPE_BOOT:
  192. policy_entry_idx = get_policy_entry_idx(boot_partitions[0]);
  193. break;
  194. default:
  195. pal_log_err("[avb] invalid boot image type\n");
  196. return 1;
  197. }
  198. img_auth_required = get_vfy_policy(policy_entry_idx);
  199. pal_log_err("[avb] img_auth_required = %d\n", img_auth_required);
  200. return img_auth_required;
  201. #else
  202. pal_log_err("[avb] img_auth_required = 0\n");
  203. return 0;
  204. #endif
  205. }
  206. static uint32_t avb_cmdline_postprocessing(char *cmdline, void *fdt)
  207. {
  208. uint32_t ret = 0;
  209. uint32_t i = 0;
  210. int nodeoffset;
  211. int fdt_ret = 0;
  212. char fdt_value[MAX_VALUE_SZ + 1] = {0};
  213. /* extract avb parameters from cmdline */
  214. for (i = 0; ; i++) {
  215. if (avb_cmdline_attrs[i] == NULL)
  216. break;
  217. ret = avb_extract_from_cmdline(cmdline,
  218. strlen(cmdline),
  219. avb_cmdline_attrs[i]);
  220. if (ret) {
  221. pal_log_err("[avb] extract %s fails\n", avb_cmdline_attrs[i]);
  222. return -1;
  223. }
  224. }
  225. /* create /firmware/android if it does not exist */
  226. nodeoffset = fdt_path_offset(fdt, "/firmware/android");
  227. if (nodeoffset < 0) {
  228. nodeoffset = fdt_path_offset(fdt, "/firmware");
  229. if (nodeoffset < 0) {
  230. /* Add subnode "firmware" in root */
  231. nodeoffset = fdt_add_subnode(fdt, 0, "firmware");
  232. if (nodeoffset < 0) {
  233. pal_log_err("Warning: can't add firmware node in device tree\n");
  234. return -1;
  235. }
  236. }
  237. /* Add subnode "android" in "/firmware" */
  238. nodeoffset = fdt_add_subnode(fdt, nodeoffset, "android");
  239. if (nodeoffset < 0) {
  240. pal_log_err("Warning: can't add firmware/android node in device tree\n");
  241. return -1;
  242. }
  243. }
  244. /* set cmdline attributes to device tree */
  245. for (i = 0; ; i++) {
  246. if (avb_dt_attrs[i] == NULL)
  247. break;
  248. ret = avb_get_cmdline(avb_cmdline_attrs[i], fdt_value, MAX_VALUE_SZ);
  249. if (ret == STATUS_OK) {
  250. fdt_ret = fdt_setprop_string(fdt, nodeoffset, avb_dt_attrs[i], fdt_value);
  251. if (fdt_ret) {
  252. pal_log_err("Error: can't set property in device tree\n");
  253. return -1;
  254. }
  255. }
  256. /* we allow the case that entry is not found */
  257. if (ret == ERR_AVB_CMDLINE_ENTRY_NOT_FOUND)
  258. ret = STATUS_OK;
  259. }
  260. return ret;
  261. }
  262. static uint32_t boot_post_processing(AvbOps *ops, uint32_t bootimg_type,
  263. AvbSlotVerifyData *slot_data)
  264. {
  265. uint32_t ret = 0;
  266. uint32_t i = 0;
  267. bool out_verification_enabled = true;
  268. #ifndef PRELOAD_PARTITION_SUPPORT
  269. uint32_t boot_load_addr = 0;
  270. #endif
  271. char *part_name = get_bootimg_partition_name(bootimg_type);
  272. if (part_name == NULL)
  273. return AVB_IO_RESULT_ERROR_IO;
  274. if (slot_data == NULL)
  275. return AVB_IO_RESULT_ERROR_IO;
  276. ret = collect_rot_info(slot_data);
  277. if (ret)
  278. return AVB_IO_RESULT_ERROR_IO;
  279. #ifndef PRELOAD_PARTITION_SUPPORT
  280. boot_load_addr = (uint32_t)target_get_scratch_address();
  281. /* relocate boot image from slot data to its target buffer */
  282. for (i = 0; i < slot_data->num_loaded_partitions; i++) {
  283. if (!strcmp(part_name,
  284. slot_data->loaded_partitions[i].partition_name)) {
  285. memcpy((void *)boot_load_addr,
  286. (void *)((uint8_t *)slot_data->loaded_partitions[i].data),
  287. slot_data->loaded_partitions[i].data_size);
  288. break;
  289. }
  290. }
  291. #endif
  292. load_bootinfo_bootimg((void *)boot_load_addr);
  293. prepare_kernel_dtb();
  294. if (i == slot_data->num_loaded_partitions) {
  295. ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  296. goto end;
  297. }
  298. #ifdef SYSTEM_AS_ROOT
  299. if (bootimg_type == BOOTIMG_TYPE_BOOT) {
  300. cmdline_append(CMDLINE_ROOT_SYSTEM_COMMON);
  301. /* root file system is already in avb slot data when
  302. * BOARD_BUILD_SYSTEM_ROOT_IMAGE is true, so we don't
  303. * have to handle it again here.
  304. */
  305. } else
  306. cmdline_append(CMDLINE_ROOT_RAM);
  307. #else
  308. cmdline_append(CMDLINE_ROOT_RAM);
  309. #endif
  310. if(!avb_user_verification_get(ops, "", &out_verification_enabled)) {
  311. pal_log_err("[avb] Fail to get verification state\n");
  312. out_verification_enabled = true;
  313. }
  314. if (out_verification_enabled) {
  315. ret = avb_cmdline_postprocessing(slot_data->cmdline, get_kernel_fdt());
  316. if (ret)
  317. goto end;
  318. }
  319. pal_log_err("[avb] cmdline = %s\n", slot_data->cmdline);
  320. cmdline_append(slot_data->cmdline);
  321. ret = write_persist_value(ops, PERSIST_PART_NAME);
  322. if (ret)
  323. goto end;
  324. set_bootimg_loaded(boot_load_addr);
  325. end:
  326. return ret;
  327. }
  328. static uint32_t handle_vboot_state(uint32_t bootimg_type)
  329. {
  330. uint32_t ret = STATUS_OK;
  331. int32_t vboot_state_ret = 0;
  332. /* device should not boot if any error occurs
  333. * in this function
  334. */
  335. /* be careful that error code should not be overriden by
  336. * return value of the following error handling functions.
  337. */
  338. vboot_state_ret = print_boot_state();
  339. if (vboot_state_ret)
  340. return ERR_VB_STATE_PRINT_FAIL;
  341. if (bootimg_type == BOOTIMG_TYPE_RECOVERY)
  342. vboot_state_ret = show_warning(recovery_partitions[0]);
  343. else if (bootimg_type == BOOTIMG_TYPE_BOOT)
  344. vboot_state_ret = show_warning(boot_partitions[0]);
  345. else
  346. return ERR_VB_STATE_SHOW_WARNING_FAIL;
  347. if (vboot_state_ret)
  348. return ERR_VB_STATE_SHOW_WARNING_FAIL;
  349. vboot_state_ret = set_boot_state_to_cmdline();
  350. if (vboot_state_ret)
  351. return ERR_VB_STATE_SET_CMDLINE_FAIL;
  352. return ret;
  353. }
  354. int load_vfy_boot(uint32_t bootimg_type, uint32_t addr)
  355. {
  356. int ret = STATUS_OK;
  357. uint32_t img_vfy_time = 0;
  358. AvbSlotVerifyResult avb_ret = AVB_SLOT_VERIFY_RESULT_OK;
  359. AvbSlotVerifyData *slot_data = NULL;
  360. AvbSlotVerifyFlags avb_flag = AVB_SLOT_VERIFY_FLAGS_NONE;
  361. uint32_t lock_state = LKS_DEFAULT;
  362. uint32_t dm_status = 0;
  363. AvbHashtreeErrorMode hashtree_error_mode = AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE;
  364. img_vfy_time = get_timer(0);
  365. g_boot_state = BOOT_STATE_RED;
  366. /* heap initialization for avb */
  367. avb_heap_sz = AVB_HEAP_SZ;
  368. pal_log_debug("[avb] avb heap alloc size 0x%x\n", avb_heap_sz);
  369. avb_heap = (void *)(uint32_t)mblock_reserve_ext(&g_boot_arg->mblock_info,
  370. avb_heap_sz,
  371. 4 * KB,
  372. 0xc0000000,
  373. 0,
  374. "avb");
  375. if (avb_heap == 0) {
  376. pal_log_err("[avb] avb heap alloc fails\n");
  377. avb_heap_sz = 0;
  378. return -1;
  379. } else
  380. pal_log_debug("[avb] avb heap alloc 0x%x\n", (uint32_t)avb_heap);
  381. #ifdef MTK_SEC_FASTBOOT_UNLOCK_SUPPORT
  382. #if defined(MTK_DM_VERITY_OFF) || defined(MTK_BUILD_DEFAULT_UNLOCK) || !defined(MTK_SECURITY_SW_SUPPORT)
  383. /* This feature is for test purpose only.
  384. * we unlock device without wiping userdata, which should not
  385. * happen for MP product. We do this because after userdata
  386. * has been wiped, Android will fail to mount userdata and reboot
  387. * to recovery to format userdata, and then reboot. If we always
  388. * wipe userdata in boot process, Android will never get a properly
  389. * formatted userdata.
  390. */
  391. #ifdef MTK_SECURITY_SW_SUPPORT
  392. /* unlock device */
  393. if (sec_set_device_lock(0) != 0) {
  394. ret = ERR_AVB_UNLOCK_DEVICE_FAILED;
  395. goto end;
  396. }
  397. #endif
  398. /* disable verification */
  399. if (avb_user_verification_set(&ops, "", 0) != true) {
  400. ret = ERR_AVB_SET_VERIFICATION_FAILED;
  401. goto end;
  402. }
  403. #endif
  404. #endif
  405. if (boot_authentication(bootimg_type) == 0)
  406. avb_flag = AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR;
  407. #ifdef PRELOAD_PARTITION_SUPPORT
  408. if (preload_partitions(bootimg_type)) {
  409. avb_ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
  410. goto end;
  411. }
  412. #endif
  413. get_dm_verity_status(&dm_status);
  414. if (dm_status)
  415. avb_flag |= AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION;
  416. get_hash_tree_error_mode((uint32_t *)&hashtree_error_mode);
  417. ret = init_persist_value(&ops, PERSIST_PART_NAME, PERSIST_VALUE_OFFSET);
  418. if (ret) {
  419. pal_log_err("init_persist_value ret = 0x%x\n", ret);
  420. goto end;
  421. }
  422. switch (bootimg_type) {
  423. case BOOTIMG_TYPE_RECOVERY:
  424. #ifndef RECOVERY_AS_BOOT
  425. avb_ret = avb_slot_verify(&ops,
  426. recovery_partitions,
  427. "",
  428. avb_flag,
  429. hashtree_error_mode,
  430. &slot_data);
  431. if (avb_flag & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR)
  432. avb_ret = AVB_SLOT_VERIFY_RESULT_OK;
  433. if (avb_ret != AVB_SLOT_VERIFY_RESULT_OK)
  434. goto end;
  435. break;
  436. #endif
  437. case BOOTIMG_TYPE_BOOT:
  438. avb_ret = avb_slot_verify(&ops,
  439. boot_partitions,
  440. "",
  441. avb_flag,
  442. hashtree_error_mode,
  443. &slot_data);
  444. if (avb_flag & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR)
  445. avb_ret = AVB_SLOT_VERIFY_RESULT_OK;
  446. if (avb_ret != AVB_SLOT_VERIFY_RESULT_OK)
  447. goto end;
  448. break;
  449. default:
  450. avb_ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
  451. goto end;
  452. }
  453. if (avb_ret == AVB_SLOT_VERIFY_RESULT_OK) {
  454. ret = boot_post_processing(&ops, bootimg_type, slot_data);
  455. if (ret)
  456. goto end;
  457. g_boot_state = BOOT_STATE_GREEN;
  458. #ifdef MTK_SECURITY_YELLOW_STATE_SUPPORT
  459. if (vb_custom_key_exist())
  460. g_boot_state = BOOT_STATE_YELLOW;
  461. #endif
  462. if (boot_authentication(bootimg_type) != 0 &&
  463. g_boot_state == BOOT_STATE_GREEN) {
  464. ret = record_avb_version(slot_data);
  465. if (ret != AVB_IO_RESULT_OK) {
  466. pal_log_err("update avb otp version fail.\n");
  467. ret = AVB_IO_RESULT_OK;
  468. }
  469. }
  470. }
  471. end:
  472. pal_log_err("[avb] boot/recovery vfy time = %d ms\n",
  473. (unsigned int)get_timer(img_vfy_time));
  474. if (slot_data != NULL)
  475. hashtree_error_mode = slot_data->resolved_hashtree_error_mode;
  476. /* After this function call, memory is returned to lk.
  477. * Please avoid using avb_heap after this function call
  478. */
  479. mblock_create(&g_boot_arg->mblock_info,
  480. &g_boot_arg->orig_dram_info,
  481. (uint64_t)(uint32_t)avb_heap & 0xffffffff,
  482. (uint64_t)avb_heap_sz & 0xffffffff);
  483. pal_log_err("[avb] avb_ret = %d\n", avb_ret);
  484. pal_log_err("[avb] ret = %d\n", ret);
  485. /* if device state is "unlocked," override
  486. * boot state with "ORANGE."
  487. */
  488. #ifdef MTK_SECURITY_SW_SUPPORT
  489. ret = get_lock_state(&lock_state);
  490. if (ret == 0 && lock_state == LKS_UNLOCK)
  491. g_boot_state = BOOT_STATE_ORANGE;
  492. #endif
  493. dm_verity_handler((uint32_t)hashtree_error_mode);
  494. ret = (int)handle_vboot_state(bootimg_type);
  495. if (ret != STATUS_OK)
  496. mtk_arch_reset(1);
  497. if (avb_ret != AVB_SLOT_VERIFY_RESULT_OK)
  498. ret = ERR_AVB_VERIFICAITON_FAILED;
  499. return ret;
  500. }