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 / nvidia / native / gsp / fwimage.om

536 lines · 10190 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note fwimage.om: read-only parser for the NVIDIA GSP firmware container (gsp_ga10x.bin, ELF64 RISC-V).
3note Format and driver behaviour: native/gsp/README.md. This file reads one firmware file and writes
4note only stdout and stderr. It never opens a device, never maps memory and never writes a file.
5note
6note Build: this unit is OM-2 and calls sha256_init/update/final from omega-mv om/tls/sha2.om.
7note The compiled unit is this file followed by sha2.om, fed to the pinned seeded omc:
8note cat fwimage.om omega-mv/om/tls/sha2.om | omc > fwimage
9note Usage: fwimage FILE [gb20y|gb10y]
10note
11note Report (stdout, only after every check passed; numbers lowercase hex with 0x, no padding):
12note SECTION <name> off=<hex> size=<hex> every section header except index 0, in table order
13note FWVERSION <text> .fwversion bytes before its terminating zero byte
14note SHA256 <name> <64 hex> .fwimage and every .fwsignature_* section, table order
15note SELECT family=<f> image_off=<hex> image_size=<hex> sig_section=.fwsignature_<f> sig_sha256=<hex>
16note only with a family argument
17note
18note Exit codes (a refusal writes one line "FWIMAGE REFUSE <reason>" to stderr and nothing to stdout):
19note 0 report written
20note 2 usage: wrong argument count or unreadable path argument
21note 3 I/O: open, seek, short read or stdout write failed
22note 10 not ELF: the first four bytes are not 7f 45 4c 46
23note 11 not an ELF64 little-endian RISC-V (e_machine 243) version-1 file
24note 12 truncated: file shorter than the 64-byte ELF header, or section header table past end of file
25note 13 malformed header: e_ehsize or e_shentsize not 64, e_shnum 0, e_shstrndx 0 or not below
26note e_shnum, or the name table section is not SHT_STRTAB or is empty
27note 14 out of bounds: a section's offset + size passes end of file (overflow-checked), a name offset
28note lies outside the name table, or the name table does not end with a zero byte
29note 15 missing .fwimage
30note 16 missing .fwversion
31note 17 unknown family argument (only gb20y and gb10y are accepted)
32note 18 duplicate section names
33note 19 capacity: more than 1024 sections or a name table above 65536 bytes
34note 20 the selected .fwsignature_<family> section is absent
35note 21 .fwimage or a .fwsignature_* section is SHT_NOBITS or empty
36note 22 .fwversion malformed: size 0 or above 63, last byte not zero, or a byte before it not 32..126
37note Runtime traps exit 100 + trap code (Omegese 9.1); none is expected on any input.
38om2
39
40text tpre ".fwsignature_"
41text timg ".fwimage"
42text tver ".fwversion"
43text tf20 "gb20y"
44text tf10 "gb10y"
45text tsec "SECTION "
46text toff " off="
47text tsize " size="
48text tfv "FWVERSION "
49text tsha "SHA256 "
50text tsel "SELECT family="
51text timo " image_off="
52text tims " image_size="
53text tsgs " sig_section="
54text tsgh " sig_sha256="
55text tsp " "
56text tnl "\n"
57text trf "FWIMAGE REFUSE "
58text tuse "usage: fwimage FILE [gb20y|gb10y]"
59text tio "I/O error"
60text tnelf "not an ELF file"
61text tkind "not ELF64 little-endian RISC-V version 1"
62text ttrunc "truncated header or section header table"
63text tshdr "malformed ELF header or name table section"
64text tbnd "section out of bounds"
65text tname "section name out of bounds"
66text tnoimg "missing .fwimage"
67text tnover "missing .fwversion"
68text tfam "unknown family"
69text tdup "duplicate section names"
70text tcap "capacity exceeded"
71text tnosig "missing selected signature section"
72text tempty "image or signature section has no file bytes"
73text tbadv "malformed .fwversion"
74
75global gap uchar 4096
76global gfam uchar 16
77global ghdr uchar 64
78global gsh uchar 65536
79global gss uchar 65536
80global gfv uchar 64
81global gob uchar 4096
82global gdg uchar 32
83global gfd int 1
84note gst: 0 file size, 1 e_shnum, 2 e_shstrndx, 3 output fill, 4 family length (0 none),
85note 5 name table size, 6 .fwimage index, 7 .fwversion index, 8 selected signature index, 9 e_shoff
86global gst ulong 16
87
88fn refuse code int msg view uchar * to void
89n = write 2 trf 0 len trf
90n = write 2 msg 0 len msg
91n = write 2 tnl 0 len tnl
92exit code
93end
94
95fn oflush to void
96n = get gst 3
97if gt n 0
98w = write 1 gob 0 n
99if ne w long n
100refuse 3 tio
101end
102put gst 3 0
103end
104end
105
106fn oc b uchar to void
107if ge get gst 3 len gob
108oflush
109end
110put gob get gst 3 b
111put gst 3 add get gst 3 1
112end
113
114fn ot t view uchar * to void
115for i ulong len t
116oc get t i
117end
118end
119
120fn ohd n ulong to uchar
121if lt n 10
122ret uchar add n 48
123end
124ret uchar add n 87
125end
126
127fn ohx v ulong to void
128oc 48
129oc 120
130on = ulong 0
131for i ulong 16
132nb = and shr v sub 60 mul i 4 15
133if or ne nb 0 or ne on 0 eq i 15
134oc ohd nb
135on = 1
136end
137end
138end
139
140fn odg to void
141for i ulong 32
142b = ulong get gdg i
143oc ohd shr b 4
144oc ohd and b 15
145end
146end
147
148fn hle o ulong w ulong to ulong
149v = ulong 0
150for i ulong w
151v = or v shl ulong get ghdr add o i mul i 8
152end
153ret v
154end
155
156fn sle o ulong w ulong to ulong
157v = ulong 0
158for i ulong w
159v = or v shl ulong get gsh add o i mul i 8
160end
161ret v
162end
163
164fn secnm i ulong to ulong
165ret sle mul i 64 4
166end
167
168fn sectyp i ulong to ulong
169ret sle add mul i 64 4 4
170end
171
172fn secoff i ulong to ulong
173ret sle add mul i 64 24 8
174end
175
176fn secsz i ulong to ulong
177ret sle add mul i 64 32 8
178end
179
180note Name of section i, which ldnames proved ends with a zero byte inside the name table.
181fn oname i ulong to void
182o = secnm i
183for k ulong get gst 5
184c = get gss add o k
185if eq c 0
186ret
187end
188oc c
189end
190end
191
192note Section i's name starts with t and has a zero byte after t inside the table.
193fn nmpre i ulong t view uchar * to bool
194o = secnm i
195if gt len t sub sub get gst 5 o 1
196ret false
197end
198for k ulong len t
199if ne get gss add o k get t k
200ret false
201end
202end
203ret true
204end
205
206fn nmis i ulong t view uchar * to bool
207if not nmpre i t
208ret false
209end
210ret eq get gss add secnm i len t 0
211end
212
213note Section i is .fwsignature_<family argument>.
214fn nmsel i ulong to bool
215if not nmpre i tpre
216ret false
217end
218o = add secnm i len tpre
219fl = get gst 4
220if gt fl sub sub get gst 5 o 1
221ret false
222end
223for k ulong fl
224if ne get gss add o k get gfam k
225ret false
226end
227end
228ret eq get gss add o fl 0
229end
230
231fn streq a ulong b ulong to bool
232for k ulong get gst 5
233x = get gss add a k
234if ne x get gss add b k
235ret false
236end
237if eq x 0
238ret true
239end
240end
241ret false
242end
243
244fn fameq t view uchar * to bool
245if ne get gst 4 len t
246ret false
247end
248for k ulong len t
249if ne get gfam k get t k
250ret false
251end
252end
253ret true
254end
255
256fn find t view uchar * to ulong
257for i ulong sub get gst 1 1
258if nmis add i 1 t
259ret add i 1
260end
261end
262ret 0
263end
264
265fn chkhdr to void
266fs = get gst 0
267if lt fs 64
268refuse 12 ttrunc
269end
270r = pread get gfd 0 ghdr 0 64 0
271if ne r 64
272refuse 3 tio
273end
274if ne hle 0 4 0x464c457f
275refuse 10 tnelf
276end
277if ne hle 4 3 0x010102
278refuse 11 tkind
279end
280if or ne hle 18 2 243 ne hle 20 4 1
281refuse 11 tkind
282end
283if or ne hle 52 2 64 ne hle 58 2 64
284refuse 13 tshdr
285end
286nsec = hle 60 2
287idx = hle 62 2
288if eq nsec 0
289refuse 13 tshdr
290end
291if gt nsec 1024
292refuse 19 tcap
293end
294if or eq idx 0 ge idx nsec
295refuse 13 tshdr
296end
297off = hle 40 8
298tl = mul nsec 64
299if or gt off fs gt tl sub fs off
300refuse 12 ttrunc
301end
302put gst 1 nsec
303put gst 2 idx
304put gst 9 off
305end
306
307fn ldsh to void
308tl = mul get gst 1 64
309r = pread get gfd 0 gsh 0 tl long get gst 9
310if ne r long tl
311refuse 3 tio
312end
313fs = get gst 0
314for i ulong get gst 1
315if ne sectyp i 8
316o = secoff i
317z = secsz i
318if or gt o fs gt z sub fs o
319refuse 14 tbnd
320end
321end
322end
323end
324
325fn ldnames to void
326k = get gst 2
327if ne sectyp k 3
328refuse 13 tshdr
329end
330z = secsz k
331if eq z 0
332refuse 13 tshdr
333end
334if gt z len gss
335refuse 19 tcap
336end
337r = pread get gfd 0 gss 0 z long secoff k
338if ne r long z
339refuse 3 tio
340end
341put gst 5 z
342if ne get gss sub z 1 0
343refuse 14 tname
344end
345for i ulong get gst 1
346if ge secnm i z
347refuse 14 tname
348end
349end
350end
351
352fn chkdup to void
353n = get gst 1
354for i ulong n
355for j ulong sub n add i 1
356a = secnm i
357b = secnm add add i 1 j
358if ne get gss a 0
359if streq a b
360refuse 18 tdup
361end
362end
363end
364end
365end
366
367fn hashable i ulong to void
368if or eq sectyp i 8 eq secsz i 0
369refuse 21 tempty
370end
371end
372
373fn findreq to void
374img = find timg
375if eq img 0
376refuse 15 tnoimg
377end
378hashable img
379ver = find tver
380if eq ver 0
381refuse 16 tnover
382end
383for i ulong get gst 1
384if nmpre i tpre
385hashable i
386end
387end
388z = secsz ver
389if or or eq z 0 gt z 63 eq sectyp ver 8
390refuse 22 tbadv
391end
392r = pread get gfd 0 gfv 0 z long secoff ver
393if ne r long z
394refuse 3 tio
395end
396if ne get gfv sub z 1 0
397refuse 22 tbadv
398end
399for i ulong sub z 1
400c = get gfv i
401if or lt c 32 gt c 126
402refuse 22 tbadv
403end
404end
405put gst 6 img
406put gst 7 ver
407if gt get gst 4 0
408sel = ulong 0
409for i ulong get gst 1
410if nmsel i
411sel = i
412end
413end
414if eq sel 0
415refuse 20 tnosig
416end
417hashable sel
418put gst 8 sel
419end
420end
421
422note SHA-256 of section i's file bytes into dg, read in 1 MiB slices (om/tls/sha2.om, FIPS 180-4).
423fn hashsec i ulong to void
424buf = new uchar 1048576 0
425c = new ulong 40 0
426d = new uchar 32 0
427sha256_init c
428pos = secoff i
429left = secsz i
430fd = get gfd 0
431while gt left 0 bound 999999
432k = left
433if gt k len buf
434k = len buf
435end
436r = pread fd buf 0 k long pos
437if le r 0
438refuse 3 tio
439end
440sha256_update c part buf 0 ulong r
441pos = add pos ulong r
442left = sub left ulong r
443end
444sha256_final c d
445for j ulong 32
446put gdg j get d j
447end
448end
449
450fn report to void
451n = get gst 1
452for i ulong sub n 1
453s = add i 1
454ot tsec
455oname s
456ot toff
457ohx secoff s
458ot tsize
459ohx secsz s
460ot tnl
461end
462ot tfv
463for i ulong sub secsz get gst 7 1
464oc get gfv i
465end
466ot tnl
467img = get gst 6
468for i ulong sub n 1
469s = add i 1
470if or eq s img nmpre s tpre
471hashsec s
472ot tsha
473oname s
474ot tsp
475odg
476ot tnl
477end
478end
479if gt get gst 4 0
480sel = get gst 8
481hashsec sel
482ot tsel
483for i ulong get gst 4
484oc get gfam i
485end
486ot timo
487ohx secoff img
488ot tims
489ohx secsz img
490ot tsgs
491oname sel
492ot tsgh
493odg
494ot tnl
495end
496oflush
497end
498
499fn main to int
500ac = argcnt
501if or lt ac 2 gt ac 3
502refuse 2 tuse
503end
504n = argread 1 gap 0 len gap
505if le n 0
506refuse 2 tuse
507end
508fd = fopen gap 0 ulong n 0 0
509if lt fd 0
510refuse 3 tio
511end
512put gfd 0 int fd
513sz = fseek int fd 0 2
514if lt sz 0
515refuse 3 tio
516end
517put gst 0 ulong sz
518if eq ac 3
519f = argread 2 gfam 0 len gfam
520if le f 0
521refuse 17 tfam
522end
523put gst 4 ulong f
524if not or fameq tf20 fameq tf10
525refuse 17 tfam
526end
527end
528chkhdr
529ldsh
530ldnames
531chkdup
532findreq
533report
534r = fclose int fd
535ret 0
536end