2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG
5 * Author: Corvin Köhne <corvink@FreeBSD.org>
13 #include <malloc_np.h>
18 #include "tpm_device.h"
32 tpm_passthru_init(void **sc, nvlist_t *nvl)
34 struct tpm_passthru *tpm;
37 tpm = calloc(1, sizeof(struct tpm_passthru));
39 warnx("%s: failed to allocate tpm passthru", __func__);
43 path = get_config_value_node(nvl, "path");
44 tpm->fd = open(path, O_RDWR);
46 warnx("%s: unable to open tpm device \"%s\"", __func__, path);
56 tpm_passthru_execute_cmd(void *sc, void *cmd, uint32_t cmd_size, void *rsp,
59 struct tpm_passthru *tpm;
62 if (rsp_size < (ssize_t)sizeof(struct tpm_resp_hdr)) {
63 warn("%s: rsp_size of %u is too small", __func__, rsp_size);
69 len = write(tpm->fd, cmd, cmd_size);
70 if (len != cmd_size) {
71 warn("%s: cmd write failed (bytes written: %zd / %d)", __func__,
76 len = read(tpm->fd, rsp, rsp_size);
77 if (len < (ssize_t)sizeof(struct tpm_resp_hdr)) {
78 warn("%s: rsp read failed (bytes read: %zd / %d)", __func__,
87 tpm_passthru_deinit(void *sc)
89 struct tpm_passthru *tpm;
101 static const struct tpm_emul tpm_emul_passthru = {
103 .init = tpm_passthru_init,
104 .deinit = tpm_passthru_deinit,
105 .execute_cmd = tpm_passthru_execute_cmd,
107 TPM_EMUL_SET(tpm_emul_passthru);