1/* Copyright (c) 2011 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.
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 "host_common.h"
14#include "tlcl.h"
15#include "tlcl_tests.h"
16
17#define TPM_MAX_NV_WRITES_NOOWNER 64
18
19int main(int argc, char** argv) {
20  int i;
21
22  uint32_t result;
23
24  TlclLibInit();
25
26  TPM_CHECK(TlclStartupIfNeeded());
27  TPM_CHECK(TlclSelfTestFull());
28  TPM_CHECK(TlclAssertPhysicalPresence());
29  TPM_CHECK(TlclForceClear());
30  TPM_CHECK(TlclSetEnable());
31  TPM_CHECK(TlclSetDeactivated(0));
32
33  for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) {
34    printf("writing %d\n", i);
35    if ((result = TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i))) != TPM_SUCCESS) {
36      switch (result) {
37      case TPM_E_MAXNVWRITES:
38        VbAssert(i >= TPM_MAX_NV_WRITES_NOOWNER);
39        break;
40      default:
41        VbExError("unexpected error code %d (0x%x)\n", result, result);
42      }
43    }
44  }
45
46  /* Reset write count */
47  TPM_CHECK(TlclForceClear());
48  TPM_CHECK(TlclSetEnable());
49  TPM_CHECK(TlclSetDeactivated(0));
50
51  /* Try writing again. */
52  TPM_CHECK(TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i)));
53
54  printf("TEST SUCCEEDED\n");
55  exit(0);
56}
57