summaryrefslogtreecommitdiff
path: root/utility
diff options
context:
space:
mode:
authorLuigi Semenzato <semenzato@google.com>2010-09-21 14:12:15 -0700
committerLuigi Semenzato <semenzato@google.com>2010-09-21 14:12:15 -0700
commita8cba996b793547b2dab92c9808cf97b98e2a555 (patch)
treed0ceb47f927038054639c048b264c96df4307848 /utility
parente19da8b8183edbbc41cbc651277b07c2362c6236 (diff)
downloadvboot-a8cba996b793547b2dab92c9808cf97b98e2a555.tar.gz
Utility to measure the available size of a TPM NVRAM.
Also change tpmc to return the TPM error code, or 255. Change-Id: Ie5fc107ff50efd4480c2a47b91f3b8a93b4f95e3 BUG=none TEST=ran it on a TPM Review URL: http://codereview.chromium.org/3479003
Diffstat (limited to 'utility')
-rwxr-xr-xutility/tpm-nvsize50
-rw-r--r--utility/tpmc.c37
2 files changed, 71 insertions, 16 deletions
diff --git a/utility/tpm-nvsize b/utility/tpm-nvsize
new file mode 100755
index 00000000..ba5b46ff
--- /dev/null
+++ b/utility/tpm-nvsize
@@ -0,0 +1,50 @@
+#! /bin/sh -e
+# Copyright (c) 2010 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.
+#
+# Finds the largest NV space that can be defined on the TPM in this state
+# (i.e. without removing existing spaces).
+#
+# The TPM must be unowned, and physical presence must be on.
+
+low=1
+high=1500
+try=$high
+
+# Binary search with no upper bound
+while true; do
+ ## echo trying $try [ $low $high ]
+ if /usr/bin/tpmc definespace 0xf004 $(printf "0x%x" $try) 0x1 \
+ > dev/null 2>&1; then
+ # definespace success: end, or $try must grow
+ if [ $try -eq $low ]; then
+ echo $low
+ exit 0
+ elif [ $try -lt $high ]; then
+ low=$try
+ try=$(( ( $high + $low ) / 2 ))
+ else
+ # special case: when try == high, expand the search
+ low=$try
+ try=$(( $try * 2 ))
+ high=$try
+ fi
+ else
+ # check for unexpected errors
+ result=$?
+ if [ $result -ne 17 ]; then
+ echo running tpmc definespace 0xf004 0x1 0x1
+ /usr/bin/tpmc definespace 0xf004 0x1 0x1
+ echo please correct this condition and try again
+ exit 1
+ fi
+ # definespace failure: end, or $try must shrink
+ if [ $try -eq $low ]; then
+ echo 0
+ exit 0
+ fi
+ high=$try
+ try=$(( ( $high + $low ) / 2 ))
+ fi
+done
diff --git a/utility/tpmc.c b/utility/tpmc.c
index 2c23aa68..2bec9c38 100644
--- a/utility/tpmc.c
+++ b/utility/tpmc.c
@@ -4,6 +4,9 @@
*
* TPM command utility. Runs simple TPM commands. Mostly useful when physical
* presence has not been locked.
+ *
+ * The exit code is 0 for success, the TPM error code for TPM errors, and 255
+ * for other errors.
*/
#include <stdio.h>
@@ -14,6 +17,8 @@
#include "tpm_error_messages.h"
#include "tss_constants.h"
+#define OTHER_ERROR 255
+
typedef struct command_record {
const char* name;
const char* abbr;
@@ -67,11 +72,11 @@ uint32_t ErrorCheck(uint32_t result, const char* cmd) {
if (tpm_error_table[i].code == result) {
fprintf(stderr, "%s\n%s\n", tpm_error_table[i].name,
tpm_error_table[i].description);
- return 1;
+ return result;
}
}
fprintf(stderr, "the TPM error code is unknown to this program\n");
- return 1;
+ return result;
}
}
@@ -101,14 +106,14 @@ static uint32_t HandlerDefineSpace(void) {
uint32_t index, size, perm;
if (nargs != 5) {
fprintf(stderr, "usage: tpmc def <index> <size> <perm>\n");
- exit(1);
+ exit(OTHER_ERROR);
}
if (HexStringToUint32(args[2], &index) != 0 ||
HexStringToUint32(args[3], &size) != 0 ||
HexStringToUint32(args[4], &perm) != 0) {
fprintf(stderr, "<index>, <size>, and <perm> must be "
"32-bit hex (0x[0-9a-f]+)\n");
- exit(1);
+ exit(OTHER_ERROR);
}
return TlclDefineSpace(index, perm, size);
}
@@ -120,16 +125,16 @@ static uint32_t HandlerWrite(void) {
int i;
if (nargs < 3) {
fprintf(stderr, "usage: tpmc write <index> [<byte0> <byte1> ...]\n");
- exit(1);
+ exit(OTHER_ERROR);
}
if (HexStringToUint32(args[2], &index) != 0) {
fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
- exit(1);
+ exit(OTHER_ERROR);
}
size = nargs - 3;
if (size > sizeof(value)) {
fprintf(stderr, "byte array too large\n");
- exit(1);
+ exit(OTHER_ERROR);
}
byteargs = args + 3;
@@ -137,7 +142,7 @@ static uint32_t HandlerWrite(void) {
if (HexStringToUint8(byteargs[i], &value[i]) != 0) {
fprintf(stderr, "invalid byte %s, should be [0-9a-f][0-9a-f]?\n",
byteargs[i]);
- exit(1);
+ exit(OTHER_ERROR);
}
}
@@ -145,7 +150,7 @@ static uint32_t HandlerWrite(void) {
if (index == TPM_NV_INDEX_LOCK) {
fprintf(stderr, "This would set the nvLocked bit. "
"Use \"tpmc setnv\" instead.\n");
- exit(1);
+ exit(OTHER_ERROR);
}
printf("warning: zero-length write\n");
} else {
@@ -162,16 +167,16 @@ static uint32_t HandlerRead(void) {
int i;
if (nargs != 4) {
fprintf(stderr, "usage: tpmc read <index> <size>\n");
- exit(1);
+ exit(OTHER_ERROR);
}
if (HexStringToUint32(args[2], &index) != 0 ||
HexStringToUint32(args[3], &size) != 0) {
fprintf(stderr, "<index> and <size> must be 32-bit hex (0x[0-9a-f]+)\n");
- exit(1);
+ exit(OTHER_ERROR);
}
if (size > sizeof(value)) {
fprintf(stderr, "size of read (0x%x) is too big\n", size);
- exit(1);
+ exit(OTHER_ERROR);
}
result = TlclRead(index, value, size);
if (result == 0 && size > 0) {
@@ -187,11 +192,11 @@ static uint32_t HandlerGetPermissions(void) {
uint32_t index, permissions, result;
if (nargs != 3) {
fprintf(stderr, "usage: tpmc getp <index>\n");
- exit(1);
+ exit(OTHER_ERROR);
}
if (HexStringToUint32(args[2], &index) != 0) {
fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
- exit(1);
+ exit(OTHER_ERROR);
}
result = TlclGetPermissions(index, &permissions);
if (result == 0) {
@@ -292,7 +297,7 @@ int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "usage: %s <TPM command> [args]\n or: %s help\n",
argv[0], argv[0]);
- exit(1);
+ return OTHER_ERROR;
} else {
command_record* c;
const char* cmd = argv[1];
@@ -317,6 +322,6 @@ int main(int argc, char* argv[]) {
/* No command matched. */
fprintf(stderr, "%s: unknown command: %s\n", argv[0], cmd);
- return 1;
+ return OTHER_ERROR;
}
}