1
/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2
* Use of this source code is governed by a BSD-style license that can be
3
* found in the LICENSE file.
6
/* This program mimicks the TPM usage from read-only firmware. It exercises
7
* the TPM functionality needed in the read-only firmware. It is meant to be
8
* integrated with the rest of the read-only firmware. It is also provided as
19
/* These index values are used to create NVRAM spaces. They only need to be
27
#define INDEX_INITIALIZED 0xda80
29
/* This is called once at initialization time. It may be called again from
30
* recovery mode to rebuild the spaces if something incomprehensible happened
31
* and the spaces are gone or messed up. This is called after TPM_Startup and
32
* before the spaces are write-locked, so there is a chance that they can be
33
* recreated (but who knows---if anything can happen, there are plenty of ways
34
* of making this FUBAR).
36
void InitializeSpaces(void) {
38
uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE;
40
printf("Initializing spaces\n");
41
TlclSetNvLocked(); /* useful only the first time */
43
TlclDefineSpace(INDEX0, perm, 4);
44
TlclWrite(INDEX0, (uint8_t *) &zero, 4);
45
TlclDefineSpace(INDEX1, perm, 4);
46
TlclWrite(INDEX1, (uint8_t *) &zero, 4);
47
TlclDefineSpace(INDEX2, perm, 4);
48
TlclWrite(INDEX2, (uint8_t *) &zero, 4);
49
TlclDefineSpace(INDEX3, perm, 4);
50
TlclWrite(INDEX3, (uint8_t *) &zero, 4);
52
perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR |
54
TlclDefineSpace(INDEX_INITIALIZED, perm, 1);
58
void EnterRecoveryMode(void) {
59
printf("entering recovery mode");
64
int main(int argc, char** argv) {
66
uint32_t index_0, index_1, index_2, index_3;
73
TlclAssertPhysicalPresence();
75
/* Checks if initialization has completed by trying to read-lock a space
76
* that's created at the end of initialization.
78
if (TlclRead(INDEX_INITIALIZED, &c, 0) == TPM_E_BADINDEX) {
79
/* The initialization did not complete.
84
/* Checks if spaces are OK or messed up.
86
if (TlclRead(INDEX0, (uint8_t*) &index_0, sizeof(index_0)) != TPM_SUCCESS ||
87
TlclRead(INDEX1, (uint8_t*) &index_1, sizeof(index_1)) != TPM_SUCCESS ||
88
TlclRead(INDEX2, (uint8_t*) &index_2, sizeof(index_2)) != TPM_SUCCESS ||
89
TlclRead(INDEX3, (uint8_t*) &index_3, sizeof(index_3)) != TPM_SUCCESS) {
93
/* Writes space, and locks it. Then attempts to write again. I really wish
94
* I could use the imperative.
97
if (TlclWrite(INDEX0, (uint8_t*) &index_0, sizeof(index_0) != TPM_SUCCESS)) {
98
error("could not write index 0\n");
100
TlclWriteLock(INDEX0);
101
if (TlclWrite(INDEX0, (uint8_t*) &index_0, sizeof(index_0)) == TPM_SUCCESS) {
102
error("index 0 is not locked\n");
107
printf("TEST SUCCEEDED\n");