xref: /aosp_15_r20/external/coreboot/src/security/vboot/secdata_tpm1.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 
3 #include <security/tpm/tspi.h>
4 #include <security/tpm/tss.h>
5 #include <security/vboot/antirollback.h>
6 #include <vb2_api.h>
7 
8 #include "secdata_tpm_private.h"
9 
10 /**
11  * Similarly to safe_write(), this ensures we don't fail a DefineSpace because
12  * we hit the TPM write limit. This is even less likely to happen than with
13  * writes because we only define spaces once at initialization, but we'd
14  * rather be paranoid about this.
15  */
safe_define_space(uint32_t index,uint32_t perm,uint32_t size)16 static tpm_result_t safe_define_space(uint32_t index, uint32_t perm, uint32_t size)
17 {
18 	tpm_result_t rc = tlcl1_define_space(index, perm, size);
19 	if (rc == TPM_MAXNVWRITES) {
20 		RETURN_ON_FAILURE(tpm_clear_and_reenable());
21 		return tlcl1_define_space(index, perm, size);
22 	} else {
23 		return rc;
24 	}
25 }
26 
factory_initialize_tpm1(struct vb2_context * ctx)27 tpm_result_t factory_initialize_tpm1(struct vb2_context *ctx)
28 {
29 	TPM_PERMANENT_FLAGS pflags;
30 	tpm_result_t rc;
31 
32 	vb2api_secdata_firmware_create(ctx);
33 	vb2api_secdata_kernel_create_v0(ctx);
34 
35 	rc = tlcl1_get_permanent_flags(&pflags);
36 	if (rc != TPM_SUCCESS)
37 		return rc;
38 
39 	/*
40 	 * TPM may come from the factory without physical presence finalized.
41 	 * Fix if necessary.
42 	 */
43 	VBDEBUG("TPM: physicalPresenceLifetimeLock=%d\n",
44 		 pflags.physicalPresenceLifetimeLock);
45 	if (!pflags.physicalPresenceLifetimeLock) {
46 		VBDEBUG("TPM: Finalizing physical presence\n");
47 		RETURN_ON_FAILURE(tlcl_finalize_physical_presence());
48 	}
49 
50 	/*
51 	 * The TPM will not enforce the NV authorization restrictions until the
52 	 * execution of a TPM_NV_DefineSpace with the handle of
53 	 * TPM_NV_INDEX_LOCK.  Here we create that space if it doesn't already
54 	 * exist. */
55 	VBDEBUG("TPM: nvLocked=%d\n", pflags.nvLocked);
56 	if (!pflags.nvLocked) {
57 		VBDEBUG("TPM: Enabling NV locking\n");
58 		RETURN_ON_FAILURE(tlcl1_set_nv_locked());
59 	}
60 
61 	/* Clear TPM owner, in case the TPM is already owned for some reason. */
62 	VBDEBUG("TPM: Clearing owner\n");
63 	RETURN_ON_FAILURE(tpm_clear_and_reenable());
64 
65 	/* Define and write secdata_kernel space. */
66 	RETURN_ON_FAILURE(safe_define_space(KERNEL_NV_INDEX,
67 					    TPM_NV_PER_PPWRITE,
68 					    VB2_SECDATA_KERNEL_SIZE_V02));
69 	RETURN_ON_FAILURE(safe_write(KERNEL_NV_INDEX,
70 				     ctx->secdata_kernel,
71 				     VB2_SECDATA_KERNEL_SIZE_V02));
72 
73 	/* Define and write secdata_firmware space. */
74 	RETURN_ON_FAILURE(safe_define_space(FIRMWARE_NV_INDEX,
75 					    TPM_NV_PER_GLOBALLOCK |
76 					    TPM_NV_PER_PPWRITE,
77 					    VB2_SECDATA_FIRMWARE_SIZE));
78 	RETURN_ON_FAILURE(safe_write(FIRMWARE_NV_INDEX,
79 				     ctx->secdata_firmware,
80 					VB2_SECDATA_FIRMWARE_SIZE));
81 
82 	return TPM_SUCCESS;
83 }
84