vboot: Move TPM disabling call to depthcharge

Introduce a new vboot context flag VB2_CONTEXT_DISABLE_TPM to indicate
whether TPM should be disabled before jumping to kernel. This allows us
to move the vb2ex_tpm_set_mode(VB2_TPM_MODE_DISABLED) call from vboot to
depthcharge. See CL:3653659 for details.

BUG=b:223662000, b:232743820
TEST=make runtests
TEST=emerge-cherry depthcharge
BRANCH=cherry

Cq-Depend: chromium:3653659
Change-Id: Ie7bcc3c7bf01346a3bc1f9e14b30017a4c3148ac
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3653207
Reviewed-by: Julius Werner <jwerner@chromium.org>
diff --git a/firmware/2lib/include/2api.h b/firmware/2lib/include/2api.h
index 3e5ecac..4b68c81 100644
--- a/firmware/2lib/include/2api.h
+++ b/firmware/2lib/include/2api.h
@@ -257,6 +257,12 @@
 	 * nvdata, FWMP or GBB flags.
 	 */
 	VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED = (1 << 27),
+
+	/*
+	 * If this is set after kernel verification, caller should disable the
+	 * TPM before jumping to kernel.
+	 */
+	VB2_CONTEXT_DISABLE_TPM = (1 << 28),
 };
 
 /* Boot mode decided in vb2api_fw_phase1.
diff --git a/firmware/lib/vboot_api_kernel.c b/firmware/lib/vboot_api_kernel.c
index 9e89620..3029852 100644
--- a/firmware/lib/vboot_api_kernel.c
+++ b/firmware/lib/vboot_api_kernel.c
@@ -143,6 +143,7 @@
 test_mockable
 vb2_error_t VbTryLoadKernel(struct vb2_context *ctx, uint32_t disk_flags)
 {
+	ctx->flags &= ~VB2_CONTEXT_DISABLE_TPM;
 	return VbTryLoadKernelImpl(ctx, disk_flags, 0, 0);
 }
 
@@ -150,7 +151,9 @@
 vb2_error_t VbTryLoadMiniOsKernel(struct vb2_context *ctx,
 				  uint32_t minios_flags)
 {
-	return VbTryLoadKernelImpl(ctx, VB_DISK_FLAG_FIXED, 1, minios_flags);
+	VB2_TRY(VbTryLoadKernelImpl(ctx, VB_DISK_FLAG_FIXED, 1, minios_flags));
+	ctx->flags |= VB2_CONTEXT_DISABLE_TPM;
+	return VB2_SUCCESS;
 }
 
 vb2_error_t VbSelectAndLoadKernel(struct vb2_context *ctx,
diff --git a/firmware/lib/vboot_kernel.c b/firmware/lib/vboot_kernel.c
index 1edf4a5..44bcf29 100644
--- a/firmware/lib/vboot_kernel.c
+++ b/firmware/lib/vboot_kernel.c
@@ -593,12 +593,6 @@
 	if (rv)
 		rv = try_minios_sector_region(ctx, params, disk_info,
 					      !end_region_first);
-	if (rv)
-		return rv;
-
-	rv = vb2ex_tpm_set_mode(VB2_TPM_MODE_DISABLED);
-	if (rv)
-		VB2_DEBUG("Failed to disable TPM\n");
 
 	return rv;
 }
diff --git a/tests/vboot_api_kernel_tests.c b/tests/vboot_api_kernel_tests.c
index b66c432..b2d4236 100644
--- a/tests/vboot_api_kernel_tests.c
+++ b/tests/vboot_api_kernel_tests.c
@@ -602,6 +602,8 @@
 				    "  load disk");
 		}
 		TEST_EQ(got_external_mismatch, 0, "  external GPT errors");
+		TEST_EQ(!!(ctx->flags & VB2_CONTEXT_DISABLE_TPM), 0,
+			"  ctx flag disable_tpm");
 	}
 	TEST_EQ(lk_normal_calls, load_kernel_calls, "  LoadKernel called");
 	TEST_EQ(lk_minios_calls, 0, "  LoadMiniOsKernel not called");
@@ -627,6 +629,8 @@
 				    "  load disk");
 		}
 		TEST_EQ(got_external_mismatch, 0, "  external GPT errors");
+		TEST_EQ(!!(ctx->flags & VB2_CONTEXT_DISABLE_TPM), 1,
+			"  ctx flag disable_tpm");
 	}
 	TEST_EQ(lk_normal_calls, 0, "  LoadKernel not called");
 	TEST_EQ(lk_minios_calls, load_kernel_calls,
diff --git a/tests/vboot_kernel2_tests.c b/tests/vboot_kernel2_tests.c
index 5fc66f1..bbebf38 100644
--- a/tests/vboot_kernel2_tests.c
+++ b/tests/vboot_kernel2_tests.c
@@ -59,8 +59,6 @@
 static int kernel_count;
 static struct mock_kernel *cur_kernel;
 
-static int mock_tpm_set_mode_calls;
-
 static void add_mock_kernel(uint64_t sector, vb2_error_t rv)
 {
 	if (kernel_count >= ARRAY_SIZE(kernels)) {
@@ -118,8 +116,6 @@
 	memset(&kernels, 0, sizeof(kernels));
 	kernel_count = 0;
 	cur_kernel = NULL;
-
-	mock_tpm_set_mode_calls = 0;
 }
 
 /* Mocks */
@@ -248,12 +244,6 @@
 	return cur_kernel->rv;
 }
 
-vb2_error_t vb2ex_tpm_set_mode(enum vb2_tpm_mode mode_val)
-{
-	mock_tpm_set_mode_calls++;
-	return VB2_SUCCESS;
-}
-
 /* Make sure nothing tested here ever calls this directly. */
 void vb2api_fail(struct vb2_context *c, uint8_t reason, uint8_t subcode)
 {
@@ -270,16 +260,12 @@
 	add_mock_kernel(0, VB2_SUCCESS);
 	TEST_SUCC(LoadMiniOsKernel(ctx, &lkp, &disk_info, 0),
 		  "{valid kernel}");
-	TEST_EQ(mock_tpm_set_mode_calls, 1,
-		"  TPM disabled");
 
 	reset_common_data();
 	disk_info.bytes_per_lba = KBUF_SIZE;
 	disk_info.lba_count = 1;
 	TEST_EQ(LoadMiniOsKernel(ctx, &lkp, &disk_info, 0),
 		VB2_ERROR_LK_NO_KERNEL_FOUND, "{no kernel}");
-	TEST_EQ(mock_tpm_set_mode_calls, 0,
-		"  TPM not disabled");
 
 	reset_common_data();
 	disk_info.bytes_per_lba = KBUF_SIZE;
@@ -306,8 +292,6 @@
 	TEST_EQ(LoadMiniOsKernel(ctx, &lkp, &disk_info, 0),
 		VB2_ERROR_LK_NO_KERNEL_FOUND,
 		"{invalid kernel, invalid kernel}");
-	TEST_EQ(mock_tpm_set_mode_calls, 0,
-		"  TPM not disabled");
 
 	reset_common_data();
 	disk_info.bytes_per_lba = KBUF_SIZE;