interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / hw / tpm.om

200 lines · 5047 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note tpm.om: hwman unit 3, the TPM 2.0 DIRECT_DEVICE runner (runner hwman-tpm). It writes complete
3note TPM 2.0 command frames to the kernel resource-manager node named in the row (/dev/tpmrm0) and reads
4note the response frame back; there is no TSS library. Frame layout and constants, TCG TPM 2.0 Library
5note Part 2/3, as transcribed in tpm2-tss 4.0.1 include/tss2/tss2_tpm2_types.h and Linux
6note include/linux/tpm.h:
7note header tag UINT16 | commandSize UINT32 | commandCode UINT32 (big-endian, 10 bytes)
8note tag TPM2_ST_NO_SESSIONS 0x8001
9note GetCapability 0x0000017a: capability UINT32, property UINT32, propertyCount UINT32
10note response: moreData BYTE, capability UINT32, count UINT32, count x (property UINT32, value UINT32)
11note TPM2_CAP_TPM_PROPERTIES 6; TPM2_PT_FIXED 0x100; TPM2_PT_REVISION 0x102;
12note TPM2_PT_MANUFACTURER 0x105; TPM2_PT_FIRMWARE_VERSION_1 0x10b, _2 0x10c
13note GetRandom 0x0000017b: bytesRequested UINT16; response TPM2B_DIGEST (size UINT16, bytes)
14note TPM2_RC_SUCCESS 0; TPM2_RC_COMMAND_CODE 0x143; TPM2_RC_COMMAND_SIZE 0x142
15note TSS2_RESMGR_TPM_RC_LAYER 11 << 16: Linux tpm-dev-common.c synthesizes TPM2_RC_COMMAND_CODE |
16note TSS2_RESMGR_TPM_RC_LAYER = 0x000b0143 for a command code the chip does not list
17note (tpm2_prepare_space -EOPNOTSUPP); that frame never reaches the chip
18note Every response is checked: responseSize equals the bytes read, tag 0x8001, then per command the
19note exact parameter layout. The response read waits at most the row's timeout_ms (waitio).
20note Operations: props (identity and firmware), getrandom N (1..32 bytes). Fault frames (fault command):
21note badsize a GetRandom frame whose commandSize says 13 but 12 bytes are written
22note badcc a 10-byte frame with commandCode 0x11e, below TPM2_CC_FIRST 0x11f (no such command);
23note /dev/tpmrm0 answers it in the kernel resource manager with rc 0x000b0143
24
25text TPMPROPS "props"
26text TPMRAND "getrandom"
27text TPMBSZ "badsize"
28text TPMBADCC "badcc"
29const TPMMAXR ulong 32
30note responses that are not TPM frames, and a response that did not arrive in time
31const TPMEFRM long -1000
32const TPMETIME long -1001
33
34global gcmd uchar 64
35global gresp uchar 4096
36
37fn tpmp16 o ulong v ulong to void
38put gcmd o uchar and shr v 8 255
39put gcmd add o 1 uchar and v 255
40end
41
42fn tpmp32 o ulong v ulong to void
43tpmp16 o shr v 16
44tpmp16 add o 2 and v 65535
45end
46
47fn tpmg16 o ulong to ulong
48ret or shl ulong get gresp o 8 ulong get gresp add o 1
49end
50
51fn tpmg32 o ulong to ulong
52ret or shl tpmg16 o 16 tpmg16 add o 2
53end
54
55fn tpmhdr size ulong cc ulong to void
56tpmp16 0 0x8001
57tpmp32 2 size
58tpmp32 6 cc
59end
60
61note open the row's device node read-write: descriptor or -errno
62fn tpmopen to long
63pclear
64pfield CNODE
65ret fopen gpath 0 get gst 5 2 0
66end
67
68note write n command bytes, wait up to tmo nanoseconds, read and frame-check the response.
69note Returns the response length; -errno when the kernel refused the write or read; TPMETIME; TPMEFRM.
70fn tpmxfer fd int n ulong tmo long to long
71w = write fd gcmd 0 n
72if lt w 0
73ret w
74end
75if ne w long n
76ret TPMEFRM
77end
78p = waitio fd 1 tmo
79if lt p 0
80ret p
81end
82if eq p 0
83ret TPMETIME
84end
85r = read fd gresp 0 len gresp
86if lt r 0
87ret r
88end
89if lt r 10
90ret TPMEFRM
91end
92if ne tpmg32 2 ulong r
93ret TPMEFRM
94end
95if ne tpmg16 0 0x8001
96ret TPMEFRM
97end
98put gst 14 tpmg32 6
99ret r
100end
101
102note TPM2_GetCapability(TPM_PROPERTIES, PT_FIXED, 64): manufacturer gst 10, firmware gst 11/12,
103note revision gst 13. Returns 0, a negative transfer result, or the TPM response code (> 0).
104fn tpmprops fd int tmo long to long
105tpmhdr 22 0x17a
106tpmp32 10 6
107tpmp32 14 0x100
108tpmp32 18 64
109r = tpmxfer fd 22 tmo
110if lt r 0
111ret r
112end
113rc = get gst 14
114if ne rc 0
115ret long rc
116end
117n = ulong r
118if lt n 19
119ret TPMEFRM
120end
121if ne tpmg32 11 6
122ret TPMEFRM
123end
124c = tpmg32 15
125if ne add 19 mul c 8 n
126ret TPMEFRM
127end
128seen = ulong 0
129for i ulong c
130p = tpmg32 add 19 mul i 8
131v = tpmg32 add 23 mul i 8
132if eq p 0x105
133put gst 10 v
134seen = or seen 1
135end
136if eq p 0x10b
137put gst 11 v
138seen = or seen 2
139end
140if eq p 0x10c
141put gst 12 v
142seen = or seen 4
143end
144if eq p 0x102
145put gst 13 v
146seen = or seen 8
147end
148end
149if ne seen 15
150ret TPMEFRM
151end
152ret 0
153end
154
155note TPM2_GetRandom(n): the n bytes are left at gresp[12..12+n). Returns 0, a negative transfer
156note result, or the TPM response code.
157fn tpmrand fd int n ulong tmo long to long
158tpmhdr 12 0x17b
159tpmp16 10 n
160r = tpmxfer fd 12 tmo
161if lt r 0
162ret r
163end
164rc = get gst 14
165if ne rc 0
166ret long rc
167end
168if ne ulong r add 12 n
169ret TPMEFRM
170end
171if ne tpmg16 10 n
172ret TPMEFRM
173end
174ret 0
175end
176
177note fault frame argument a: returns the transfer result (negative) or the TPM response code;
178note gst 9 is 1 when a names no fault frame
179fn tpmfault fd int a ulong tmo long to long
180put gst 9 0
181if argeq a TPMBSZ
182tpmhdr 13 0x17b
183tpmp16 10 16
184r = tpmxfer fd 12 tmo
185if lt r 0
186ret r
187end
188ret long get gst 14
189end
190if argeq a TPMBADCC
191tpmhdr 10 0x11e
192r = tpmxfer fd 10 tmo
193if lt r 0
194ret r
195end
196ret long get gst 14
197end
198put gst 9 1
199ret 0
200end