summaryrefslogtreecommitdiff
path: root/tests/tpm_lite/tpmtest_writelimit.c
blob: 952033cb5e58040534e35fab3732ce5ed1428000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Test of recovery when we hit the NVRAM write limit for an unowned TPM.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "host_common.h"
#include "tlcl.h"
#include "tlcl_tests.h"

#define TPM_MAX_NV_WRITES_NOOWNER 64

int main(int argc, char** argv) {
  int i;

  uint32_t result;

  TlclLibInit();

  TPM_CHECK(TlclStartupIfNeeded());
  TPM_CHECK(TlclSelfTestFull());
  TPM_CHECK(TlclAssertPhysicalPresence());
  TPM_CHECK(TlclForceClear());
  TPM_CHECK(TlclSetEnable());
  TPM_CHECK(TlclSetDeactivated(0));

  for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) {
    printf("writing %d\n", i);
    if ((result = TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i))) != TPM_SUCCESS) {
      switch (result) {
      case TPM_E_MAXNVWRITES:
        VbAssert(i >= TPM_MAX_NV_WRITES_NOOWNER);
        break;
      default:
        VbExError("unexpected error code %d (0x%x)\n", result, result);
      }
    }
  }

  /* Reset write count */
  TPM_CHECK(TlclForceClear());
  TPM_CHECK(TlclSetEnable());
  TPM_CHECK(TlclSetDeactivated(0));

  /* Try writing again. */
  TPM_CHECK(TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i)));

  printf("TEST SUCCEEDED\n");
  exit(0);
}