xref: /aosp_15_r20/external/vboot_reference/tests/tpm_lite/tpmtest_writelimit.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2011 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 /* Test of recovery when we hit the NVRAM write limit for an unowned TPM.
7  */
8 
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 
13 #include "2common.h"
14 #include "host_common.h"
15 #include "common/tests.h"
16 #include "tlcl.h"
17 #include "tlcl_tests.h"
18 
19 #define TPM_MAX_NV_WRITES_NOOWNER 64
20 
main(int argc,char ** argv)21 int main(int argc, char** argv) {
22 	int i;
23 
24 	uint32_t result;
25 
26 	TlclLibInit();
27 
28 	TPM_CHECK(TlclStartupIfNeeded());
29 	TPM_CHECK(TlclSelfTestFull());
30 	TPM_CHECK(TlclAssertPhysicalPresence());
31 	TPM_CHECK(TlclForceClear());
32 	TPM_CHECK(TlclSetEnable());
33 	TPM_CHECK(TlclSetDeactivated(0));
34 
35 	for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) {
36 		printf("writing %d\n", i);
37 		if ((result = TlclWrite(INDEX0, (uint8_t*)&i,
38 					sizeof(i))) != TPM_SUCCESS) {
39 			switch (result) {
40 			case TPM_E_MAXNVWRITES:
41 				TEST_TRUE(i >= TPM_MAX_NV_WRITES_NOOWNER,
42 					  "MAXNVWRITES should only occur after "
43 					  "MAX_NV_WRITES_NOOWNER reached");
44 				break;
45 			default:
46 				VB2_DEBUG("unexpected error code %d (%#x)\n",
47 					  result, result);
48 				exit(1);
49 			}
50 		}
51 	}
52 
53 	/* Reset write count */
54 	TPM_CHECK(TlclForceClear());
55 	TPM_CHECK(TlclSetEnable());
56 	TPM_CHECK(TlclSetDeactivated(0));
57 
58 	/* Try writing again. */
59 	TPM_CHECK(TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i)));
60 
61 	printf("TEST SUCCEEDED\n");
62 	exit(0);
63 }
64