#include <common.h>
#include <dm.h>
+#include <log.h>
#include <asm/unaligned.h>
#include <tpm-common.h>
#include "tpm-utils.h"
+enum tpm_version tpm_get_version(struct udevice *dev)
+{
+ struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
+
+ return priv->version;
+}
+
int pack_byte_string(u8 *str, size_t size, const char *format, ...)
{
va_list args;
if (offset + length > size) {
va_end(args);
- log_err("Failed to read: size=%d, offset=%x, len=%x\n",
+ log_err("Failed to read: size=%zd, offset=%zx, len=%zx\n",
size, offset, length);
return -1;
}
return get_unaligned_be32(response + return_code_offset);
}
-u32 tpm_sendrecv_command(const void *command, void *response, size_t *size_ptr)
+u32 tpm_sendrecv_command(struct udevice *dev, const void *command,
+ void *response, size_t *size_ptr)
{
- struct udevice *dev;
int err, ret;
u8 response_buffer[COMMAND_BUFFER_SIZE];
size_t response_length;
int i;
+ uint size;
if (response) {
response_length = *size_ptr;
response_length = sizeof(response_buffer);
}
- ret = uclass_first_device_err(UCLASS_TPM, &dev);
- if (ret)
- return ret;
- err = tpm_xfer(dev, command, tpm_command_size(command),
- response, &response_length);
+ size = tpm_command_size(command);
+
+ /* sanity check, which also helps coverity */
+ if (size > COMMAND_BUFFER_SIZE)
+ return log_msg_ret("size", -E2BIG);
+
+ log_debug("TPM request [size:%d]: ", size);
+ for (i = 0; i < size; i++)
+ log_debug("%02x ", ((u8 *)command)[i]);
+ log_debug("\n");
+
+ err = tpm_xfer(dev, command, size, response, &response_length);
if (err < 0)
return err;
return ret;
}
-int tpm_init(void)
+int tpm_init(struct udevice *dev)
{
- struct udevice *dev;
- int err;
-
- err = uclass_first_device_err(UCLASS_TPM, &dev);
- if (err)
- return err;
-
return tpm_open(dev);
}