1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG 5 * Author: Corvin Köhne <corvink@FreeBSD.org> 6 */ 7 8 #include <sys/types.h> 9 10 #include <err.h> 11 #include <errno.h> 12 #include <fcntl.h> 13 #include <malloc_np.h> 14 #include <stdlib.h> 15 #include <unistd.h> 16 17 #include "config.h" 18 #include "tpm_device.h" 19 #include "tpm_emul.h" 20 21 struct tpm_passthru { 22 int fd; 23 }; 24 25 static int 26 tpm_passthru_init(void **sc, nvlist_t *nvl) 27 { 28 struct tpm_passthru *tpm; 29 const char *path; 30 31 tpm = calloc(1, sizeof(struct tpm_passthru)); 32 if (tpm == NULL) { 33 warnx("%s: failed to allocate tpm passthru", __func__); 34 return (ENOMEM); 35 } 36 37 path = get_config_value_node(nvl, "path"); 38 tpm->fd = open(path, O_RDWR); 39 if (tpm->fd < 0) { 40 warnx("%s: unable to open tpm device \"%s\"", __func__, path); 41 return (ENOENT); 42 } 43 44 *sc = tpm; 45 46 return (0); 47 } 48 49 static void 50 tpm_passthru_deinit(void *sc) 51 { 52 struct tpm_passthru *tpm; 53 54 tpm = sc; 55 if (tpm == NULL) 56 return; 57 58 if (tpm->fd >= 0) 59 close(tpm->fd); 60 61 free(tpm); 62 } 63 64 static const struct tpm_emul tpm_emul_passthru = { 65 .name = "passthru", 66 .init = tpm_passthru_init, 67 .deinit = tpm_passthru_deinit, 68 }; 69 TPM_EMUL_SET(tpm_emul_passthru); 70