tpm.c revision 1.21 1 1.21 riastrad /* $NetBSD: tpm.c,v 1.21 2021/05/29 08:45:29 riastradh Exp $ */
2 1.13 maxv
3 1.13 maxv /*
4 1.13 maxv * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.13 maxv * All rights reserved.
6 1.13 maxv *
7 1.13 maxv * This code is derived from software contributed to The NetBSD Foundation
8 1.13 maxv * by Maxime Villard.
9 1.13 maxv *
10 1.13 maxv * Redistribution and use in source and binary forms, with or without
11 1.13 maxv * modification, are permitted provided that the following conditions
12 1.13 maxv * are met:
13 1.13 maxv * 1. Redistributions of source code must retain the above copyright
14 1.13 maxv * notice, this list of conditions and the following disclaimer.
15 1.13 maxv * 2. Redistributions in binary form must reproduce the above copyright
16 1.13 maxv * notice, this list of conditions and the following disclaimer in the
17 1.13 maxv * documentation and/or other materials provided with the distribution.
18 1.13 maxv *
19 1.13 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.13 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.13 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.13 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.13 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.13 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.13 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.13 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.13 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.13 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.13 maxv * POSSIBILITY OF SUCH DAMAGE.
30 1.13 maxv */
31 1.13 maxv
32 1.1 christos /*
33 1.1 christos * Copyright (c) 2008, 2009 Michael Shalayeff
34 1.13 maxv * Copyright (c) 2009, 2010 Hans-Joerg Hoexer
35 1.1 christos * All rights reserved.
36 1.1 christos *
37 1.1 christos * Permission to use, copy, modify, and distribute this software for any
38 1.1 christos * purpose with or without fee is hereby granted, provided that the above
39 1.1 christos * copyright notice and this permission notice appear in all copies.
40 1.1 christos *
41 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
46 1.1 christos * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
47 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 1.1 christos */
49 1.1 christos
50 1.1 christos #include <sys/cdefs.h>
51 1.21 riastrad __KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.21 2021/05/29 08:45:29 riastradh Exp $");
52 1.1 christos
53 1.1 christos #include <sys/param.h>
54 1.17 riastrad #include <sys/types.h>
55 1.17 riastrad
56 1.19 riastrad #include <sys/atomic.h>
57 1.17 riastrad #include <sys/bus.h>
58 1.17 riastrad #include <sys/conf.h>
59 1.17 riastrad #include <sys/device.h>
60 1.1 christos #include <sys/kernel.h>
61 1.1 christos #include <sys/malloc.h>
62 1.17 riastrad #include <sys/pmf.h>
63 1.1 christos #include <sys/proc.h>
64 1.17 riastrad #include <sys/systm.h>
65 1.19 riastrad #include <sys/workqueue.h>
66 1.1 christos
67 1.1 christos #include <dev/ic/tpmreg.h>
68 1.1 christos #include <dev/ic/tpmvar.h>
69 1.1 christos
70 1.12 riastrad #include "ioconf.h"
71 1.12 riastrad
72 1.15 maxv CTASSERT(sizeof(struct tpm_header) == 10);
73 1.15 maxv
74 1.13 maxv #define TPM_BUFSIZ 1024
75 1.14 maxv
76 1.13 maxv #define TPM_PARAM_SIZE 0x0001 /* that's a flag */
77 1.13 maxv
78 1.13 maxv /* Timeouts. */
79 1.13 maxv #define TPM_ACCESS_TMO 2000 /* 2sec */
80 1.13 maxv #define TPM_READY_TMO 2000 /* 2sec */
81 1.13 maxv #define TPM_READ_TMO 2000 /* 2sec */
82 1.13 maxv #define TPM_BURST_TMO 2000 /* 2sec */
83 1.13 maxv
84 1.13 maxv #define TPM_CAPS_REQUIRED \
85 1.13 maxv (TPM_INTF_DATA_AVAIL_INT|TPM_INTF_LOCALITY_CHANGE_INT| \
86 1.13 maxv TPM_INTF_INT_LEVEL_LOW)
87 1.1 christos
88 1.13 maxv static inline int
89 1.13 maxv tpm_tmotohz(int tmo)
90 1.1 christos {
91 1.13 maxv struct timeval tv;
92 1.1 christos
93 1.13 maxv tv.tv_sec = tmo / 1000;
94 1.13 maxv tv.tv_usec = 1000 * (tmo % 1000);
95 1.1 christos
96 1.13 maxv return tvtohz(&tv);
97 1.1 christos }
98 1.1 christos
99 1.13 maxv static int
100 1.1 christos tpm_getburst(struct tpm_softc *sc)
101 1.1 christos {
102 1.1 christos int burst, to, rv;
103 1.1 christos
104 1.1 christos to = tpm_tmotohz(TPM_BURST_TMO);
105 1.1 christos
106 1.13 maxv while (to--) {
107 1.1 christos /*
108 1.13 maxv * Burst count is in bits 23:8, so read the two higher bytes.
109 1.1 christos */
110 1.1 christos burst = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 1);
111 1.1 christos burst |= bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 2)
112 1.1 christos << 8;
113 1.13 maxv
114 1.1 christos if (burst)
115 1.1 christos return burst;
116 1.1 christos
117 1.14 maxv rv = tsleep(sc, PCATCH, "tpm_getburst", 1);
118 1.1 christos if (rv && rv != EWOULDBLOCK) {
119 1.1 christos return 0;
120 1.1 christos }
121 1.1 christos }
122 1.1 christos
123 1.1 christos return 0;
124 1.1 christos }
125 1.1 christos
126 1.13 maxv static inline uint8_t
127 1.1 christos tpm_status(struct tpm_softc *sc)
128 1.1 christos {
129 1.13 maxv return bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS) &
130 1.13 maxv TPM_STS_STATUS_BITS;
131 1.1 christos }
132 1.1 christos
133 1.13 maxv /* -------------------------------------------------------------------------- */
134 1.1 christos
135 1.15 maxv static bool
136 1.15 maxv tpm12_suspend(struct tpm_softc *sc)
137 1.15 maxv {
138 1.15 maxv static const uint8_t command[10] = {
139 1.15 maxv 0x00, 0xC1, /* TPM_TAG_RQU_COMMAND */
140 1.15 maxv 0x00, 0x00, 0x00, 10, /* Length in bytes */
141 1.15 maxv 0x00, 0x00, 0x00, 0x98 /* TPM_ORD_SaveState */
142 1.15 maxv };
143 1.15 maxv struct tpm_header response;
144 1.15 maxv
145 1.16 maxv if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
146 1.15 maxv return false;
147 1.16 maxv if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
148 1.15 maxv return false;
149 1.15 maxv if (TPM_BE32(response.code) != 0)
150 1.15 maxv return false;
151 1.15 maxv
152 1.15 maxv return true;
153 1.15 maxv }
154 1.1 christos
155 1.15 maxv static bool
156 1.15 maxv tpm20_suspend(struct tpm_softc *sc)
157 1.1 christos {
158 1.15 maxv static const uint8_t command[12] = {
159 1.15 maxv 0x80, 0x01, /* TPM_ST_NO_SESSIONS */
160 1.15 maxv 0x00, 0x00, 0x00, 12, /* Length in bytes */
161 1.15 maxv 0x00, 0x00, 0x01, 0x45, /* TPM_CC_Shutdown */
162 1.15 maxv 0x00, 0x01 /* TPM_SU_STATE */
163 1.1 christos };
164 1.15 maxv struct tpm_header response;
165 1.1 christos
166 1.16 maxv if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
167 1.15 maxv return false;
168 1.16 maxv if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
169 1.15 maxv return false;
170 1.15 maxv if (TPM_BE32(response.code) != 0)
171 1.15 maxv return false;
172 1.13 maxv
173 1.7 christos return true;
174 1.1 christos }
175 1.1 christos
176 1.1 christos bool
177 1.15 maxv tpm_suspend(device_t dev, const pmf_qual_t *qual)
178 1.1 christos {
179 1.15 maxv struct tpm_softc *sc = device_private(dev);
180 1.15 maxv
181 1.15 maxv switch (sc->sc_ver) {
182 1.15 maxv case TPM_1_2:
183 1.15 maxv return tpm12_suspend(sc);
184 1.15 maxv case TPM_2_0:
185 1.15 maxv return tpm20_suspend(sc);
186 1.15 maxv default:
187 1.15 maxv panic("%s: impossible", __func__);
188 1.15 maxv }
189 1.15 maxv }
190 1.15 maxv
191 1.15 maxv bool
192 1.15 maxv tpm_resume(device_t dev, const pmf_qual_t *qual)
193 1.15 maxv {
194 1.15 maxv /*
195 1.15 maxv * Don't do anything, the BIOS is supposed to restore the previously
196 1.15 maxv * saved state.
197 1.15 maxv */
198 1.7 christos return true;
199 1.1 christos }
200 1.1 christos
201 1.13 maxv /* -------------------------------------------------------------------------- */
202 1.13 maxv
203 1.13 maxv static int
204 1.14 maxv tpm_poll(struct tpm_softc *sc, uint8_t mask, int to, wchan_t chan)
205 1.1 christos {
206 1.1 christos int rv;
207 1.1 christos
208 1.13 maxv while (((sc->sc_status = tpm_status(sc)) & mask) != mask && to--) {
209 1.14 maxv rv = tsleep(chan, PCATCH, "tpm_poll", 1);
210 1.1 christos if (rv && rv != EWOULDBLOCK) {
211 1.1 christos return rv;
212 1.1 christos }
213 1.1 christos }
214 1.1 christos
215 1.1 christos return 0;
216 1.1 christos }
217 1.1 christos
218 1.13 maxv static int
219 1.13 maxv tpm_waitfor(struct tpm_softc *sc, uint8_t bits, int tmo, wchan_t chan)
220 1.1 christos {
221 1.13 maxv int retry, to, rv;
222 1.13 maxv uint8_t todo;
223 1.1 christos
224 1.14 maxv to = tpm_tmotohz(tmo);
225 1.13 maxv retry = 3;
226 1.13 maxv
227 1.1 christos restart:
228 1.14 maxv todo = bits;
229 1.14 maxv
230 1.1 christos /*
231 1.14 maxv * TPM_STS_VALID has priority over the others.
232 1.1 christos */
233 1.14 maxv if (todo & TPM_STS_VALID) {
234 1.14 maxv if ((rv = tpm_poll(sc, TPM_STS_VALID, to+1, chan)) != 0)
235 1.14 maxv return rv;
236 1.14 maxv todo &= ~TPM_STS_VALID;
237 1.14 maxv }
238 1.14 maxv
239 1.14 maxv if ((rv = tpm_poll(sc, todo, to, chan)) != 0)
240 1.1 christos return rv;
241 1.1 christos
242 1.13 maxv if ((todo & sc->sc_status) != todo) {
243 1.14 maxv if ((retry-- > 0) && (bits & TPM_STS_VALID)) {
244 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
245 1.1 christos TPM_STS_RESP_RETRY);
246 1.1 christos goto restart;
247 1.1 christos }
248 1.1 christos return EIO;
249 1.1 christos }
250 1.1 christos
251 1.1 christos return 0;
252 1.1 christos }
253 1.1 christos
254 1.13 maxv /* -------------------------------------------------------------------------- */
255 1.13 maxv
256 1.13 maxv /*
257 1.16 maxv * TPM using the TIS 1.2 interface.
258 1.13 maxv */
259 1.13 maxv
260 1.16 maxv static int
261 1.16 maxv tpm12_request_locality(struct tpm_softc *sc, int l)
262 1.16 maxv {
263 1.16 maxv uint32_t r;
264 1.16 maxv int to, rv;
265 1.16 maxv
266 1.16 maxv if (l != 0)
267 1.16 maxv return EINVAL;
268 1.16 maxv
269 1.16 maxv if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
270 1.16 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
271 1.16 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
272 1.16 maxv return 0;
273 1.16 maxv
274 1.16 maxv bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
275 1.16 maxv TPM_ACCESS_REQUEST_USE);
276 1.16 maxv
277 1.16 maxv to = tpm_tmotohz(TPM_ACCESS_TMO);
278 1.16 maxv
279 1.16 maxv while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
280 1.16 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
281 1.16 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
282 1.16 maxv rv = tsleep(sc->sc_intf->init, PCATCH, "tpm_locality", 1);
283 1.16 maxv if (rv && rv != EWOULDBLOCK) {
284 1.16 maxv return rv;
285 1.16 maxv }
286 1.16 maxv }
287 1.16 maxv
288 1.16 maxv if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
289 1.16 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
290 1.16 maxv return EBUSY;
291 1.16 maxv }
292 1.16 maxv
293 1.16 maxv return 0;
294 1.16 maxv }
295 1.16 maxv
296 1.16 maxv static int
297 1.13 maxv tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
298 1.13 maxv {
299 1.13 maxv uint32_t cap;
300 1.13 maxv uint8_t reg;
301 1.13 maxv int tmo;
302 1.13 maxv
303 1.13 maxv cap = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITY);
304 1.13 maxv if (cap == 0xffffffff)
305 1.16 maxv return EINVAL;
306 1.13 maxv if ((cap & TPM_CAPS_REQUIRED) != TPM_CAPS_REQUIRED)
307 1.16 maxv return ENOTSUP;
308 1.13 maxv
309 1.13 maxv /* Request locality 0. */
310 1.13 maxv bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
311 1.13 maxv
312 1.13 maxv /* Wait for it to become active. */
313 1.13 maxv tmo = TPM_ACCESS_TMO; /* Milliseconds. */
314 1.13 maxv while ((reg = bus_space_read_1(bt, bh, TPM_ACCESS) &
315 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
316 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && tmo--) {
317 1.13 maxv DELAY(1000); /* 1 millisecond. */
318 1.13 maxv }
319 1.13 maxv if ((reg & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
320 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
321 1.16 maxv return ETIMEDOUT;
322 1.13 maxv }
323 1.13 maxv
324 1.13 maxv if (bus_space_read_4(bt, bh, TPM_ID) == 0xffffffff)
325 1.16 maxv return EINVAL;
326 1.13 maxv
327 1.16 maxv return 0;
328 1.13 maxv }
329 1.13 maxv
330 1.21 riastrad static int
331 1.21 riastrad tpm12_rng(struct tpm_softc *sc, unsigned *entropybitsp)
332 1.19 riastrad {
333 1.19 riastrad /*
334 1.19 riastrad * TPM Specification Version 1.2, Main Part 3: Commands,
335 1.19 riastrad * Sec. 13.6 TPM_GetRandom
336 1.19 riastrad */
337 1.19 riastrad struct {
338 1.19 riastrad struct tpm_header hdr;
339 1.19 riastrad uint32_t bytesRequested;
340 1.19 riastrad } __packed command;
341 1.19 riastrad struct response {
342 1.19 riastrad struct tpm_header hdr;
343 1.19 riastrad uint32_t randomBytesSize;
344 1.19 riastrad uint8_t bytes[64];
345 1.19 riastrad } __packed response;
346 1.21 riastrad bool endwrite = false, endread = false;
347 1.19 riastrad size_t nread;
348 1.19 riastrad uint16_t tag;
349 1.21 riastrad uint32_t pktlen, code, nbytes, entropybits = 0;
350 1.19 riastrad int rv;
351 1.19 riastrad
352 1.19 riastrad /* Encode the command. */
353 1.19 riastrad memset(&command, 0, sizeof(command));
354 1.19 riastrad command.hdr.tag = htobe16(TPM_TAG_RQU_COMMAND);
355 1.19 riastrad command.hdr.length = htobe32(sizeof(command));
356 1.19 riastrad command.hdr.code = htobe32(TPM_ORD_GetRandom);
357 1.19 riastrad command.bytesRequested = htobe32(sizeof(response.bytes));
358 1.19 riastrad
359 1.19 riastrad /* Write the command. */
360 1.19 riastrad if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE)) != 0) {
361 1.19 riastrad device_printf(sc->sc_dev, "start write failed, error=%d\n",
362 1.19 riastrad rv);
363 1.19 riastrad goto out;
364 1.19 riastrad }
365 1.19 riastrad endwrite = true;
366 1.19 riastrad if ((rv = (*sc->sc_intf->write)(sc, &command, sizeof(command))) != 0) {
367 1.19 riastrad device_printf(sc->sc_dev, "write failed, error=%d\n", rv);
368 1.19 riastrad goto out;
369 1.19 riastrad }
370 1.19 riastrad rv = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
371 1.19 riastrad endwrite = false;
372 1.19 riastrad if (rv) {
373 1.19 riastrad device_printf(sc->sc_dev, "end write failed, error=%d\n", rv);
374 1.19 riastrad goto out;
375 1.19 riastrad }
376 1.19 riastrad
377 1.19 riastrad /* Read the response header. */
378 1.19 riastrad if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)) != 0) {
379 1.19 riastrad device_printf(sc->sc_dev, "start write failed, error=%d\n",
380 1.19 riastrad rv);
381 1.19 riastrad goto out;
382 1.19 riastrad }
383 1.19 riastrad endread = true;
384 1.19 riastrad if ((rv = (*sc->sc_intf->read)(sc, &response.hdr, sizeof(response.hdr),
385 1.19 riastrad &nread, 0)) != 0) {
386 1.19 riastrad device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
387 1.19 riastrad goto out;
388 1.19 riastrad }
389 1.19 riastrad
390 1.19 riastrad /* Verify the response header looks sensible. */
391 1.19 riastrad if (nread != sizeof(response.hdr)) {
392 1.19 riastrad device_printf(sc->sc_dev, "read %zu bytes, expected %zu",
393 1.19 riastrad nread, sizeof(response.hdr));
394 1.19 riastrad goto out;
395 1.19 riastrad }
396 1.19 riastrad tag = be16toh(response.hdr.tag);
397 1.19 riastrad pktlen = be32toh(response.hdr.length);
398 1.19 riastrad code = be32toh(response.hdr.code);
399 1.19 riastrad if (tag != TPM_TAG_RSP_COMMAND ||
400 1.19 riastrad pktlen < offsetof(struct response, bytes) ||
401 1.19 riastrad pktlen > sizeof(response) ||
402 1.19 riastrad code != 0) {
403 1.19 riastrad /*
404 1.19 riastrad * If the tpm itself is busy (e.g., it has yet to run a
405 1.19 riastrad * self-test, or it's in a timeout period to defend
406 1.19 riastrad * against brute force attacks), then we can try again
407 1.19 riastrad * later. Otherwise, give up.
408 1.19 riastrad */
409 1.19 riastrad if (code & TPM_NON_FATAL) {
410 1.19 riastrad aprint_debug_dev(sc->sc_dev, "%s: tpm busy, code=%u\n",
411 1.19 riastrad __func__, code & ~TPM_NON_FATAL);
412 1.19 riastrad rv = 0;
413 1.19 riastrad } else if (code == TPM_DEACTIVATED) {
414 1.19 riastrad device_printf(sc->sc_dev, "tpm is deactivated\n");
415 1.19 riastrad rv = ENXIO;
416 1.19 riastrad } else {
417 1.19 riastrad device_printf(sc->sc_dev, "bad tpm response:"
418 1.19 riastrad " tag=%u len=%u code=%u\n", tag, pktlen, code);
419 1.19 riastrad hexdump(aprint_debug, "tpm response header",
420 1.19 riastrad (const void *)&response.hdr,
421 1.19 riastrad sizeof(response.hdr));
422 1.19 riastrad rv = EIO;
423 1.19 riastrad }
424 1.19 riastrad goto out;
425 1.19 riastrad }
426 1.19 riastrad
427 1.19 riastrad /* Read the response payload. */
428 1.19 riastrad if ((rv = (*sc->sc_intf->read)(sc,
429 1.19 riastrad (char *)&response + nread, pktlen - nread,
430 1.19 riastrad NULL, TPM_PARAM_SIZE)) != 0) {
431 1.19 riastrad device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
432 1.19 riastrad goto out;
433 1.19 riastrad }
434 1.19 riastrad endread = false;
435 1.19 riastrad if ((rv = (*sc->sc_intf->end)(sc, UIO_READ, 0)) != 0) {
436 1.19 riastrad device_printf(sc->sc_dev, "end read failed, error=%d\n", rv);
437 1.19 riastrad goto out;
438 1.19 riastrad }
439 1.19 riastrad
440 1.19 riastrad /* Verify the number of bytes read looks sensible. */
441 1.19 riastrad nbytes = be32toh(response.randomBytesSize);
442 1.19 riastrad if (nbytes > pktlen - offsetof(struct response, bytes)) {
443 1.19 riastrad device_printf(sc->sc_dev, "overlong GetRandom length:"
444 1.19 riastrad " %u, max %zu\n",
445 1.19 riastrad nbytes, pktlen - offsetof(struct response, bytes));
446 1.19 riastrad nbytes = pktlen - offsetof(struct response, bytes);
447 1.19 riastrad }
448 1.19 riastrad
449 1.19 riastrad /*
450 1.19 riastrad * Enter the data into the entropy pool. Conservatively (or,
451 1.19 riastrad * perhaps, cargocultily) estimate half a bit of entropy per
452 1.19 riastrad * bit of data.
453 1.19 riastrad */
454 1.21 riastrad CTASSERT(sizeof(response.bytes) <= UINT_MAX/(NBBY/2));
455 1.21 riastrad entropybits = (NBBY/2)*nbytes;
456 1.21 riastrad rnd_add_data(&sc->sc_rnd, response.bytes, nbytes, entropybits);
457 1.21 riastrad
458 1.21 riastrad out: /* End the read or write if still ongoing. */
459 1.21 riastrad if (endread)
460 1.21 riastrad rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
461 1.21 riastrad if (endwrite)
462 1.21 riastrad rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
463 1.21 riastrad
464 1.21 riastrad *entropybitsp = entropybits;
465 1.21 riastrad return rv;
466 1.21 riastrad }
467 1.21 riastrad
468 1.21 riastrad static int
469 1.21 riastrad tpm20_rng(struct tpm_softc *sc, unsigned *entropybitsp)
470 1.21 riastrad {
471 1.21 riastrad /*
472 1.21 riastrad * Trusted Platform Module Library, Family "2.0", Level 00
473 1.21 riastrad * Revision 01.38, Part 3: Commands, Sec. 16.1 `TPM2_GetRandom'
474 1.21 riastrad *
475 1.21 riastrad * https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-3-Commands-01.38.pdf#page=133
476 1.21 riastrad */
477 1.21 riastrad struct {
478 1.21 riastrad struct tpm_header hdr;
479 1.21 riastrad uint16_t bytesRequested;
480 1.21 riastrad } __packed command;
481 1.21 riastrad struct response {
482 1.21 riastrad struct tpm_header hdr;
483 1.21 riastrad uint16_t randomBytesSize;
484 1.21 riastrad uint8_t bytes[64];
485 1.21 riastrad } __packed response;
486 1.21 riastrad bool endwrite = false, endread = false;
487 1.21 riastrad size_t nread;
488 1.21 riastrad uint16_t tag;
489 1.21 riastrad uint32_t pktlen, code, nbytes, entropybits = 0;
490 1.21 riastrad int rv;
491 1.21 riastrad
492 1.21 riastrad /* Encode the command. */
493 1.21 riastrad memset(&command, 0, sizeof(command));
494 1.21 riastrad command.hdr.tag = htobe16(TPM2_ST_NO_SESSIONS);
495 1.21 riastrad command.hdr.length = htobe32(sizeof(command));
496 1.21 riastrad command.hdr.code = htobe32(TPM2_CC_GetRandom);
497 1.21 riastrad command.bytesRequested = htobe16(sizeof(response.bytes));
498 1.21 riastrad
499 1.21 riastrad /* Write the command. */
500 1.21 riastrad if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE)) != 0) {
501 1.21 riastrad device_printf(sc->sc_dev, "start write failed, error=%d\n",
502 1.21 riastrad rv);
503 1.21 riastrad goto out;
504 1.21 riastrad }
505 1.21 riastrad endwrite = true;
506 1.21 riastrad if ((rv = (*sc->sc_intf->write)(sc, &command, sizeof(command))) != 0) {
507 1.21 riastrad device_printf(sc->sc_dev, "write failed, error=%d\n", rv);
508 1.21 riastrad goto out;
509 1.21 riastrad }
510 1.21 riastrad rv = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
511 1.21 riastrad endwrite = false;
512 1.21 riastrad if (rv) {
513 1.21 riastrad device_printf(sc->sc_dev, "end write failed, error=%d\n", rv);
514 1.21 riastrad goto out;
515 1.21 riastrad }
516 1.21 riastrad
517 1.21 riastrad /* Read the response header. */
518 1.21 riastrad if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)) != 0) {
519 1.21 riastrad device_printf(sc->sc_dev, "start write failed, error=%d\n",
520 1.21 riastrad rv);
521 1.21 riastrad goto out;
522 1.21 riastrad }
523 1.21 riastrad endread = true;
524 1.21 riastrad if ((rv = (*sc->sc_intf->read)(sc, &response.hdr, sizeof(response.hdr),
525 1.21 riastrad &nread, 0)) != 0) {
526 1.21 riastrad device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
527 1.21 riastrad goto out;
528 1.21 riastrad }
529 1.21 riastrad
530 1.21 riastrad /* Verify the response header looks sensible. */
531 1.21 riastrad if (nread != sizeof(response.hdr)) {
532 1.21 riastrad device_printf(sc->sc_dev, "read %zu bytes, expected %zu",
533 1.21 riastrad nread, sizeof(response.hdr));
534 1.21 riastrad goto out;
535 1.21 riastrad }
536 1.21 riastrad tag = be16toh(response.hdr.tag);
537 1.21 riastrad pktlen = be32toh(response.hdr.length);
538 1.21 riastrad code = be32toh(response.hdr.code);
539 1.21 riastrad if (tag != TPM2_ST_NO_SESSIONS ||
540 1.21 riastrad pktlen < offsetof(struct response, bytes) ||
541 1.21 riastrad pktlen > sizeof(response) ||
542 1.21 riastrad code != 0) {
543 1.21 riastrad /*
544 1.21 riastrad * If the tpm itself is busy (e.g., it has yet to run a
545 1.21 riastrad * self-test, or it's in a timeout period to defend
546 1.21 riastrad * against brute force attacks), then we can try again
547 1.21 riastrad * later. Otherwise, give up.
548 1.21 riastrad */
549 1.21 riastrad if (code & TPM2_RC_WARN) {
550 1.21 riastrad aprint_debug_dev(sc->sc_dev, "%s: tpm busy,"
551 1.21 riastrad " code=TPM_RC_WARN+0x%x\n",
552 1.21 riastrad __func__, code & ~TPM2_RC_WARN);
553 1.21 riastrad rv = 0;
554 1.21 riastrad } else {
555 1.21 riastrad device_printf(sc->sc_dev, "bad tpm response:"
556 1.21 riastrad " tag=%u len=%u code=0x%x\n", tag, pktlen, code);
557 1.21 riastrad hexdump(aprint_debug, "tpm response header",
558 1.21 riastrad (const void *)&response.hdr,
559 1.21 riastrad sizeof(response.hdr));
560 1.21 riastrad rv = EIO;
561 1.21 riastrad }
562 1.21 riastrad goto out;
563 1.21 riastrad }
564 1.21 riastrad
565 1.21 riastrad /* Read the response payload. */
566 1.21 riastrad if ((rv = (*sc->sc_intf->read)(sc,
567 1.21 riastrad (char *)&response + nread, pktlen - nread,
568 1.21 riastrad NULL, TPM_PARAM_SIZE)) != 0) {
569 1.21 riastrad device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
570 1.21 riastrad goto out;
571 1.21 riastrad }
572 1.21 riastrad endread = false;
573 1.21 riastrad if ((rv = (*sc->sc_intf->end)(sc, UIO_READ, 0)) != 0) {
574 1.21 riastrad device_printf(sc->sc_dev, "end read failed, error=%d\n", rv);
575 1.21 riastrad goto out;
576 1.21 riastrad }
577 1.21 riastrad
578 1.21 riastrad /* Verify the number of bytes read looks sensible. */
579 1.21 riastrad nbytes = be16toh(response.randomBytesSize);
580 1.21 riastrad if (nbytes > pktlen - offsetof(struct response, bytes)) {
581 1.21 riastrad device_printf(sc->sc_dev, "overlong GetRandom length:"
582 1.21 riastrad " %u, max %zu\n",
583 1.21 riastrad nbytes, pktlen - offsetof(struct response, bytes));
584 1.21 riastrad nbytes = pktlen - offsetof(struct response, bytes);
585 1.21 riastrad }
586 1.21 riastrad
587 1.21 riastrad /*
588 1.21 riastrad * Enter the data into the entropy pool. Conservatively (or,
589 1.21 riastrad * perhaps, cargocultily) estimate half a bit of entropy per
590 1.21 riastrad * bit of data.
591 1.21 riastrad */
592 1.21 riastrad CTASSERT(sizeof(response.bytes) <= UINT_MAX/(NBBY/2));
593 1.21 riastrad entropybits = (NBBY/2)*nbytes;
594 1.21 riastrad rnd_add_data(&sc->sc_rnd, response.bytes, nbytes, entropybits);
595 1.21 riastrad
596 1.21 riastrad out: /* End the read or write if still ongoing. */
597 1.21 riastrad if (endread)
598 1.21 riastrad rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
599 1.21 riastrad if (endwrite)
600 1.21 riastrad rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
601 1.21 riastrad
602 1.21 riastrad *entropybitsp = entropybits;
603 1.21 riastrad return rv;
604 1.21 riastrad }
605 1.21 riastrad
606 1.21 riastrad static void
607 1.21 riastrad tpm_rng_work(struct work *wk, void *cookie)
608 1.21 riastrad {
609 1.21 riastrad struct tpm_softc *sc = cookie;
610 1.21 riastrad unsigned nbytes, entropybits;
611 1.21 riastrad bool busy;
612 1.21 riastrad int rv;
613 1.21 riastrad
614 1.21 riastrad /* Acknowledge the request. */
615 1.21 riastrad nbytes = atomic_swap_uint(&sc->sc_rndpending, 0);
616 1.21 riastrad
617 1.21 riastrad /* Lock userland out of the tpm, or fail if it's already open. */
618 1.21 riastrad mutex_enter(&sc->sc_lock);
619 1.21 riastrad busy = sc->sc_busy;
620 1.21 riastrad sc->sc_busy = true;
621 1.21 riastrad mutex_exit(&sc->sc_lock);
622 1.21 riastrad if (busy) { /* tough */
623 1.21 riastrad aprint_debug_dev(sc->sc_dev, "%s: device in use\n", __func__);
624 1.21 riastrad return;
625 1.21 riastrad }
626 1.21 riastrad
627 1.21 riastrad /*
628 1.21 riastrad * Issue as many commands as needed to fulfill the request, but
629 1.21 riastrad * stop if anything fails.
630 1.21 riastrad */
631 1.21 riastrad for (; nbytes; nbytes -= MIN(nbytes, MAX(1, entropybits/NBBY))) {
632 1.21 riastrad switch (sc->sc_ver) {
633 1.21 riastrad case TPM_1_2:
634 1.21 riastrad rv = tpm12_rng(sc, &entropybits);
635 1.21 riastrad break;
636 1.21 riastrad case TPM_2_0:
637 1.21 riastrad rv = tpm20_rng(sc, &entropybits);
638 1.21 riastrad break;
639 1.21 riastrad default:
640 1.21 riastrad panic("bad tpm version: %d", sc->sc_ver);
641 1.21 riastrad }
642 1.21 riastrad if (rv)
643 1.21 riastrad break;
644 1.21 riastrad }
645 1.19 riastrad
646 1.21 riastrad /*
647 1.19 riastrad * If the tpm is busted, no sense in trying again -- most
648 1.19 riastrad * likely, it is deactivated, and by the spec it cannot be
649 1.19 riastrad * reactivated until after a reboot.
650 1.19 riastrad */
651 1.19 riastrad if (rv) {
652 1.19 riastrad device_printf(sc->sc_dev, "deactivating entropy source\n");
653 1.19 riastrad rnd_detach_source(&sc->sc_rnd);
654 1.19 riastrad /* XXX worker thread can't workqueue_destroy its own queue */
655 1.19 riastrad }
656 1.19 riastrad
657 1.19 riastrad /* Relinquish the tpm back to userland. */
658 1.19 riastrad mutex_enter(&sc->sc_lock);
659 1.19 riastrad KASSERT(sc->sc_busy);
660 1.19 riastrad sc->sc_busy = false;
661 1.19 riastrad mutex_exit(&sc->sc_lock);
662 1.19 riastrad }
663 1.19 riastrad
664 1.19 riastrad static void
665 1.21 riastrad tpm_rng_get(size_t nbytes, void *cookie)
666 1.19 riastrad {
667 1.19 riastrad struct tpm_softc *sc = cookie;
668 1.19 riastrad
669 1.21 riastrad if (atomic_swap_uint(&sc->sc_rndpending, MIN(nbytes, UINT_MAX/NBBY))
670 1.21 riastrad == 0)
671 1.19 riastrad workqueue_enqueue(sc->sc_rndwq, &sc->sc_rndwk, NULL);
672 1.19 riastrad }
673 1.19 riastrad
674 1.16 maxv static int
675 1.14 maxv tpm_tis12_init(struct tpm_softc *sc)
676 1.13 maxv {
677 1.16 maxv int rv;
678 1.16 maxv
679 1.14 maxv sc->sc_caps = bus_space_read_4(sc->sc_bt, sc->sc_bh,
680 1.13 maxv TPM_INTF_CAPABILITY);
681 1.13 maxv sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
682 1.13 maxv sc->sc_rev = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_REV);
683 1.13 maxv
684 1.14 maxv aprint_normal_dev(sc->sc_dev, "device 0x%08x rev 0x%x\n",
685 1.14 maxv sc->sc_devid, sc->sc_rev);
686 1.13 maxv
687 1.16 maxv if ((rv = tpm12_request_locality(sc, 0)) != 0)
688 1.16 maxv return rv;
689 1.13 maxv
690 1.13 maxv /* Abort whatever it thought it was doing. */
691 1.13 maxv bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
692 1.13 maxv
693 1.19 riastrad /* XXX Run this at higher priority? */
694 1.19 riastrad if ((rv = workqueue_create(&sc->sc_rndwq, device_xname(sc->sc_dev),
695 1.21 riastrad tpm_rng_work, sc, PRI_NONE, IPL_VM, WQ_MPSAFE)) != 0)
696 1.19 riastrad return rv;
697 1.21 riastrad rndsource_setcb(&sc->sc_rnd, tpm_rng_get, sc);
698 1.19 riastrad rnd_attach_source(&sc->sc_rnd, device_xname(sc->sc_dev),
699 1.19 riastrad RND_TYPE_RNG,
700 1.19 riastrad RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_VALUE|RND_FLAG_HASCB);
701 1.19 riastrad
702 1.13 maxv return 0;
703 1.13 maxv }
704 1.13 maxv
705 1.16 maxv static int
706 1.14 maxv tpm_tis12_start(struct tpm_softc *sc, int rw)
707 1.1 christos {
708 1.1 christos int rv;
709 1.1 christos
710 1.14 maxv if (rw == UIO_READ) {
711 1.1 christos rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
712 1.16 maxv TPM_READ_TMO, sc->sc_intf->read);
713 1.1 christos return rv;
714 1.1 christos }
715 1.1 christos
716 1.13 maxv /* Request the 0th locality. */
717 1.16 maxv if ((rv = tpm12_request_locality(sc, 0)) != 0)
718 1.1 christos return rv;
719 1.1 christos
720 1.13 maxv sc->sc_status = tpm_status(sc);
721 1.13 maxv if (sc->sc_status & TPM_STS_CMD_READY)
722 1.1 christos return 0;
723 1.1 christos
724 1.1 christos /* Abort previous and restart. */
725 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
726 1.16 maxv rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_intf->write);
727 1.13 maxv if (rv)
728 1.1 christos return rv;
729 1.1 christos
730 1.1 christos return 0;
731 1.1 christos }
732 1.1 christos
733 1.16 maxv static int
734 1.3 christos tpm_tis12_read(struct tpm_softc *sc, void *buf, size_t len, size_t *count,
735 1.1 christos int flags)
736 1.1 christos {
737 1.1 christos uint8_t *p = buf;
738 1.1 christos size_t cnt;
739 1.14 maxv int rv, n;
740 1.1 christos
741 1.1 christos cnt = 0;
742 1.1 christos while (len > 0) {
743 1.13 maxv rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
744 1.16 maxv TPM_READ_TMO, sc->sc_intf->read);
745 1.13 maxv if (rv)
746 1.1 christos return rv;
747 1.1 christos
748 1.14 maxv n = MIN(len, tpm_getburst(sc));
749 1.14 maxv while (n > 0) {
750 1.1 christos *p++ = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_DATA);
751 1.1 christos cnt++;
752 1.14 maxv len--;
753 1.14 maxv n--;
754 1.1 christos }
755 1.1 christos
756 1.1 christos if ((flags & TPM_PARAM_SIZE) == 0 && cnt >= 6)
757 1.1 christos break;
758 1.1 christos }
759 1.1 christos
760 1.1 christos if (count)
761 1.1 christos *count = cnt;
762 1.1 christos
763 1.1 christos return 0;
764 1.1 christos }
765 1.1 christos
766 1.16 maxv static int
767 1.3 christos tpm_tis12_write(struct tpm_softc *sc, const void *buf, size_t len)
768 1.1 christos {
769 1.3 christos const uint8_t *p = buf;
770 1.1 christos size_t cnt;
771 1.1 christos int rv, r;
772 1.1 christos
773 1.3 christos if (len == 0)
774 1.3 christos return 0;
775 1.16 maxv if ((rv = tpm12_request_locality(sc, 0)) != 0)
776 1.1 christos return rv;
777 1.1 christos
778 1.1 christos cnt = 0;
779 1.1 christos while (cnt < len - 1) {
780 1.1 christos for (r = tpm_getburst(sc); r > 0 && cnt < len - 1; r--) {
781 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
782 1.1 christos cnt++;
783 1.1 christos }
784 1.1 christos if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
785 1.1 christos return rv;
786 1.1 christos }
787 1.13 maxv sc->sc_status = tpm_status(sc);
788 1.13 maxv if (!(sc->sc_status & TPM_STS_DATA_EXPECT)) {
789 1.1 christos return EIO;
790 1.1 christos }
791 1.1 christos }
792 1.1 christos
793 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
794 1.1 christos cnt++;
795 1.1 christos
796 1.1 christos if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
797 1.1 christos return rv;
798 1.1 christos }
799 1.13 maxv if ((sc->sc_status & TPM_STS_DATA_EXPECT) != 0) {
800 1.1 christos return EIO;
801 1.1 christos }
802 1.1 christos
803 1.1 christos return 0;
804 1.1 christos }
805 1.1 christos
806 1.16 maxv static int
807 1.14 maxv tpm_tis12_end(struct tpm_softc *sc, int rw, int err)
808 1.1 christos {
809 1.1 christos int rv = 0;
810 1.1 christos
811 1.14 maxv if (rw == UIO_READ) {
812 1.16 maxv rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc->sc_intf->read);
813 1.13 maxv if (rv)
814 1.1 christos return rv;
815 1.1 christos
816 1.1 christos /* Still more data? */
817 1.13 maxv sc->sc_status = tpm_status(sc);
818 1.14 maxv if (!err && (sc->sc_status & TPM_STS_DATA_AVAIL)) {
819 1.1 christos rv = EIO;
820 1.1 christos }
821 1.1 christos
822 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
823 1.1 christos TPM_STS_CMD_READY);
824 1.1 christos
825 1.13 maxv /* Release the 0th locality. */
826 1.13 maxv bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
827 1.1 christos TPM_ACCESS_ACTIVE_LOCALITY);
828 1.1 christos } else {
829 1.1 christos /* Hungry for more? */
830 1.13 maxv sc->sc_status = tpm_status(sc);
831 1.13 maxv if (!err && (sc->sc_status & TPM_STS_DATA_EXPECT)) {
832 1.1 christos rv = EIO;
833 1.1 christos }
834 1.1 christos
835 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
836 1.1 christos err ? TPM_STS_CMD_READY : TPM_STS_GO);
837 1.1 christos }
838 1.1 christos
839 1.1 christos return rv;
840 1.1 christos }
841 1.1 christos
842 1.16 maxv const struct tpm_intf tpm_intf_tis12 = {
843 1.16 maxv .version = TIS_1_2,
844 1.16 maxv .probe = tpm_tis12_probe,
845 1.16 maxv .init = tpm_tis12_init,
846 1.16 maxv .start = tpm_tis12_start,
847 1.16 maxv .read = tpm_tis12_read,
848 1.16 maxv .write = tpm_tis12_write,
849 1.16 maxv .end = tpm_tis12_end
850 1.16 maxv };
851 1.16 maxv
852 1.13 maxv /* -------------------------------------------------------------------------- */
853 1.1 christos
854 1.13 maxv static dev_type_open(tpmopen);
855 1.13 maxv static dev_type_close(tpmclose);
856 1.13 maxv static dev_type_read(tpmread);
857 1.13 maxv static dev_type_write(tpmwrite);
858 1.13 maxv static dev_type_ioctl(tpmioctl);
859 1.1 christos
860 1.13 maxv const struct cdevsw tpm_cdevsw = {
861 1.13 maxv .d_open = tpmopen,
862 1.13 maxv .d_close = tpmclose,
863 1.13 maxv .d_read = tpmread,
864 1.13 maxv .d_write = tpmwrite,
865 1.13 maxv .d_ioctl = tpmioctl,
866 1.13 maxv .d_stop = nostop,
867 1.13 maxv .d_tty = notty,
868 1.13 maxv .d_poll = nopoll,
869 1.13 maxv .d_mmap = nommap,
870 1.13 maxv .d_kqfilter = nokqfilter,
871 1.13 maxv .d_discard = nodiscard,
872 1.14 maxv .d_flag = D_OTHER | D_MPSAFE,
873 1.13 maxv };
874 1.1 christos
875 1.13 maxv static int
876 1.1 christos tpmopen(dev_t dev, int flag, int mode, struct lwp *l)
877 1.1 christos {
878 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
879 1.14 maxv int ret = 0;
880 1.1 christos
881 1.13 maxv if (sc == NULL)
882 1.1 christos return ENXIO;
883 1.1 christos
884 1.14 maxv mutex_enter(&sc->sc_lock);
885 1.14 maxv if (sc->sc_busy) {
886 1.14 maxv ret = EBUSY;
887 1.14 maxv } else {
888 1.14 maxv sc->sc_busy = true;
889 1.14 maxv }
890 1.14 maxv mutex_exit(&sc->sc_lock);
891 1.1 christos
892 1.14 maxv return ret;
893 1.1 christos }
894 1.1 christos
895 1.13 maxv static int
896 1.1 christos tpmclose(dev_t dev, int flag, int mode, struct lwp *l)
897 1.1 christos {
898 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
899 1.14 maxv int ret = 0;
900 1.1 christos
901 1.13 maxv if (sc == NULL)
902 1.1 christos return ENXIO;
903 1.1 christos
904 1.14 maxv mutex_enter(&sc->sc_lock);
905 1.14 maxv if (!sc->sc_busy) {
906 1.14 maxv ret = EINVAL;
907 1.14 maxv } else {
908 1.14 maxv sc->sc_busy = false;
909 1.14 maxv }
910 1.14 maxv mutex_exit(&sc->sc_lock);
911 1.1 christos
912 1.14 maxv return ret;
913 1.1 christos }
914 1.1 christos
915 1.13 maxv static int
916 1.1 christos tpmread(dev_t dev, struct uio *uio, int flags)
917 1.1 christos {
918 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
919 1.15 maxv struct tpm_header hdr;
920 1.14 maxv uint8_t buf[TPM_BUFSIZ];
921 1.1 christos size_t cnt, len, n;
922 1.14 maxv int rv;
923 1.1 christos
924 1.13 maxv if (sc == NULL)
925 1.1 christos return ENXIO;
926 1.1 christos
927 1.16 maxv if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)))
928 1.16 maxv return rv;
929 1.1 christos
930 1.14 maxv /* Get the header. */
931 1.16 maxv if ((rv = (*sc->sc_intf->read)(sc, &hdr, sizeof(hdr), &cnt, 0))) {
932 1.3 christos goto out;
933 1.1 christos }
934 1.15 maxv len = TPM_BE32(hdr.length);
935 1.14 maxv if (len > uio->uio_resid || len < cnt) {
936 1.1 christos rv = EIO;
937 1.3 christos goto out;
938 1.1 christos }
939 1.1 christos
940 1.14 maxv /* Copy out the header. */
941 1.15 maxv if ((rv = uiomove(&hdr, cnt, uio))) {
942 1.3 christos goto out;
943 1.1 christos }
944 1.1 christos
945 1.14 maxv /* Process the rest. */
946 1.14 maxv len -= cnt;
947 1.14 maxv while (len > 0) {
948 1.14 maxv n = MIN(sizeof(buf), len);
949 1.16 maxv if ((rv = (*sc->sc_intf->read)(sc, buf, n, NULL, TPM_PARAM_SIZE))) {
950 1.3 christos goto out;
951 1.1 christos }
952 1.14 maxv if ((rv = uiomove(buf, n, uio))) {
953 1.3 christos goto out;
954 1.1 christos }
955 1.14 maxv len -= n;
956 1.1 christos }
957 1.1 christos
958 1.3 christos out:
959 1.16 maxv rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
960 1.1 christos return rv;
961 1.1 christos }
962 1.1 christos
963 1.13 maxv static int
964 1.1 christos tpmwrite(dev_t dev, struct uio *uio, int flags)
965 1.1 christos {
966 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
967 1.1 christos uint8_t buf[TPM_BUFSIZ];
968 1.14 maxv int n, rv;
969 1.1 christos
970 1.13 maxv if (sc == NULL)
971 1.1 christos return ENXIO;
972 1.1 christos
973 1.1 christos n = MIN(sizeof(buf), uio->uio_resid);
974 1.1 christos if ((rv = uiomove(buf, n, uio))) {
975 1.13 maxv goto out;
976 1.1 christos }
977 1.16 maxv if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE))) {
978 1.13 maxv goto out;
979 1.1 christos }
980 1.16 maxv if ((rv = (*sc->sc_intf->write)(sc, buf, n))) {
981 1.13 maxv goto out;
982 1.1 christos }
983 1.1 christos
984 1.16 maxv rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
985 1.13 maxv out:
986 1.1 christos return rv;
987 1.1 christos }
988 1.1 christos
989 1.13 maxv static int
990 1.13 maxv tpmioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
991 1.1 christos {
992 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
993 1.13 maxv struct tpm_ioc_getinfo *info;
994 1.13 maxv
995 1.13 maxv if (sc == NULL)
996 1.13 maxv return ENXIO;
997 1.13 maxv
998 1.13 maxv switch (cmd) {
999 1.13 maxv case TPM_IOC_GETINFO:
1000 1.13 maxv info = addr;
1001 1.13 maxv info->api_version = TPM_API_VERSION;
1002 1.13 maxv info->tpm_version = sc->sc_ver;
1003 1.16 maxv info->itf_version = sc->sc_intf->version;
1004 1.13 maxv info->device_id = sc->sc_devid;
1005 1.13 maxv info->device_rev = sc->sc_rev;
1006 1.14 maxv info->device_caps = sc->sc_caps;
1007 1.13 maxv return 0;
1008 1.13 maxv default:
1009 1.13 maxv break;
1010 1.13 maxv }
1011 1.13 maxv
1012 1.1 christos return ENOTTY;
1013 1.1 christos }
1014