1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <security/tpm/tspi.h>
4 #include <security/tpm/tss.h>
5 #include <security/vboot/antirollback.h>
8 #include "secdata_tpm_private.h"
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.
16 static tpm_result_t
safe_define_space(uint32_t index
, uint32_t perm
, uint32_t size
)
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
);
27 tpm_result_t
factory_initialize_tpm1(struct vb2_context
*ctx
)
29 TPM_PERMANENT_FLAGS pflags
;
32 vb2api_secdata_firmware_create(ctx
);
33 vb2api_secdata_kernel_create_v0(ctx
);
35 rc
= tlcl1_get_permanent_flags(&pflags
);
36 if (rc
!= TPM_SUCCESS
)
40 * TPM may come from the factory without physical presence finalized.
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());
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
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());
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());
65 /* Define and write secdata_kernel space. */
66 RETURN_ON_FAILURE(safe_define_space(KERNEL_NV_INDEX
,
68 VB2_SECDATA_KERNEL_SIZE_V02
));
69 RETURN_ON_FAILURE(safe_write(KERNEL_NV_INDEX
,
71 VB2_SECDATA_KERNEL_SIZE_V02
));
73 /* Define and write secdata_firmware space. */
74 RETURN_ON_FAILURE(safe_define_space(FIRMWARE_NV_INDEX
,
75 TPM_NV_PER_GLOBALLOCK
|
77 VB2_SECDATA_FIRMWARE_SIZE
));
78 RETURN_ON_FAILURE(safe_write(FIRMWARE_NV_INDEX
,
79 ctx
->secdata_firmware
,
80 VB2_SECDATA_FIRMWARE_SIZE
));