[U-Boot] [PATCH 12/15] sandbox: tpm: Tidy up enums and return values

Simon Glass
2018-10-01T17:55:17Z

Use an enum for command values instead of open-coding them. This removes
the need for comments. Also make sure the driver returns proper error
numbers instead of -1.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

drivers/tpm/tpm_tis_sandbox.c | 20 ++++++++++----------
include/tpm-v1.h | 14 ++++++++++++++
include/tpm-v2.h | 1 +
3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/drivers/tpm/tpm_tis_sandbox.c b/drivers/tpm/tpm_tis_sandbox.c
index 8816d55759f..c0b35a06c8a 100644
--- a/drivers/tpm/tpm_tis_sandbox.c
+++ b/drivers/tpm/tpm_tis_sandbox.c
@@ -151,10 +151,10 @@ static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
*recv_len, code);
print_buffer(0, sendbuf, 1, send_size, 0);
switch (code) {
- case 0x65: /* get flags */
+ case TPM_CMD_GET_CAPABILITY:
type = get_unaligned_be32(sendbuf + 14);
switch (type) {
- case 4:
+ case TPM_CAP_FLAG:
index = get_unaligned_be32(sendbuf + 18);
printf("Get flags index %#02x\n", index);
*recv_len = 22;
@@ -173,7 +173,7 @@ static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
break;
}
break;
- case 0x11: /* TPM_CAP_NV_INDEX */
+ case TPM_CAP_NV_INDEX:
index = get_unaligned_be32(sendbuf + 18);
printf("Get cap nv index %#02x\n", index);
put_unaligned_be32(22, recvbuf +
@@ -182,26 +182,26 @@ static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
default:
printf(" ** Unknown 0x65 command type %#02x\n",
type);
- return -1;
+ return -ENOSYS;
}
break;
- case 0xcd: /* nvwrite */
+ case TPM_CMD_NV_WRITE_VALUE:
index = get_unaligned_be32(sendbuf + 10);
length = get_unaligned_be32(sendbuf + 18);
seq = index_to_seq(index);
if (seq < 0)
- return -1;
+ return -EINVAL;
printf("tpm: nvwrite index=%#02x, len=%#02x\n", index, length);
memcpy(&tpm->nvdata[seq], sendbuf + 22, length);
*recv_len = 12;
memset(recvbuf, '\0', *recv_len);
break;
- case 0xcf: /* nvread */
+ case TPM_CMD_NV_READ_VALUE: /* nvread */
index = get_unaligned_be32(sendbuf + 10);
length = get_unaligned_be32(sendbuf + 18);
seq = index_to_seq(index);
if (seq < 0)
- return -1;
+ return -EINVAL;
printf("tpm: nvread index=%#02x, len=%#02x\n", index, length);
*recv_len = TPM_RESPONSE_HEADER_LENGTH + sizeof(uint32_t) +
length;
@@ -225,7 +225,7 @@ static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
sizeof(uint32_t), &tpm->nvdata[seq], length);
}
break;
- case 0x14: /* tpm extend */
+ case TPM_CMD_EXTEND: /* tpm extend */
case 0x15: /* pcr read */
case 0x5d: /* force clear */
case 0x6f: /* physical enable */
@@ -237,7 +237,7 @@ static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
break;
default:
printf("Unknown tpm command %02x\n", code);
- return -1;
+ return -ENOSYS;
}

return 0;
diff --git a/include/tpm-v1.h b/include/tpm-v1.h
index 6b4941ef9a2..29788b5390f 100644
--- a/include/tpm-v1.h
+++ b/include/tpm-v1.h
@@ -81,6 +81,12 @@ enum tpm_capability_areas {
TPM_CAP_VERSION_VAL = 0x0000001A,
};

+enum tmp_cap_flag {
+ TPM_CAP_FLAG_PERMANENT = 0x108,
+};
+
+#define TPM_TAG_PERMANENT_FLAGS 0x001f
+
#define TPM_NV_PER_GLOBALLOCK BIT(15)
#define TPM_NV_PER_PPREAD BIT(16)
#define TPM_NV_PER_PPWRITE BIT(0)
@@ -93,6 +99,14 @@ enum {
TPM_PUBEK_SIZE = 256,
};

+enum {
+ TPM_CMD_EXTEND = 0x14,
+ TPM_CMD_GET_CAPABILITY = 0x65,
+ TPM_CMD_NV_DEFINE_SPACE = 0xcc,
+ TPM_CMD_NV_WRITE_VALUE = 0xcd,
+ TPM_CMD_NV_READ_VALUE = 0xcf,
+};
+
/**
* TPM return codes as defined in the TCG Main specification
* (TPM Main Part 2 Structures; Specification version 1.2)
diff --git a/include/tpm-v2.h b/include/tpm-v2.h
index 780e0619750..c77b416182e 100644
--- a/include/tpm-v2.h
+++ b/include/tpm-v2.h
@@ -83,6 +83,7 @@ enum tpm2_command_codes {
TPM2_CC_PCR_SETAUTHPOL = 0x012C,
TPM2_CC_DAM_RESET = 0x0139,
TPM2_CC_DAM_PARAMETERS = 0x013A,
+ TPM2_CC_NV_READ = 0x014E,
TPM2_CC_GET_CAPABILITY = 0x017A,
TPM2_CC_PCR_READ = 0x017E,
TPM2_CC_PCR_EXTEND = 0x0182,
--
2.19.0.605.g01d371f741-goog