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 / driver / gsp / fw.om

778 lines · 14451 bytes Raw Blame History
1note gsp/fw.om: GSP firmware container inspection in seeded Omega (OM-1). Ports the inspect path of
2note aienux PR #441 native/kernel/tools/ck_gsp_fw_inspect.c (in_bounds, inspect_buf, print_report,
3note cmd_inspect) for firmware version 580.173.02 (default family gb10y). Library unit: no main.
4note Prefix fw. Needs gsp/sha2.om concatenated first. The file is read with pread from an open fd,
5note never mapped and never a device. Also holds the shared stdout writer (fwo*) that gsp/bindata.om
6note and fwprep.om print through, and the strerror texts the C prints.
7note Deviation (documented): the section string table is copied into a 16 MiB global; a larger one is
8note refused with reason 29 after the C's bounds check and before its NUL check. The C keeps the whole
9note file in memory; this port preads only the header, the section table, the string table, the
10note version and note bytes, and streams the three SHA-256 digests (same results).
11const FWOCAP ulong 65536
12const FWSTMX ulong 16777216
13const FWNONE ulong 999
14const FWFAM10 ulong 1
15const FWFAM20 ulong 2
16global fwob uchar 65536
17global fwon ulong 1
18global fwofd int 1
19global fwoerr ulong 1
20global fwsgb ulong 80
21global fwsgl ulong 80
22global fwok ulong 1
23global fwfam ulong 1
24global fwpath uchar 4096
25global fwpathn ulong 1
26global fwhd uchar 64
27global fwsh uchar 16384
28global fwst uchar 16777216
29global fwstn ulong 1
30global fwbuf uchar 128
31global fwshn ulong 1
32global fwnsec ulong 1
33global fwsn ulong 256
34global fwsty ulong 256
35global fwsof ulong 256
36global fwssz ulong 256
37global fwlen ulong 1
38global fwimx ulong 1
39global fwvx ulong 1
40global fwbx ulong 1
41global fwsx ulong 1
42global fwver uchar 64
43global fwvern ulong 1
44global fwbidh uchar 128
45global fwbidn ulong 1
46global fwhxf uchar 64
47global fwhxi uchar 64
48global fwhxs uchar 64
49note segment k of FWTXT is the text between the k-th and (k+1)-th '|' (segment 0 is empty).
50note 1..28 are the C refusal reasons in check order, 29 the deviation reason, 30.. report pieces,
51note 54.. strerror texts (glibc wording); 68..70 more strerror texts for fwprep's write paths;
52note 71..72 gb20y signature section and family name; 73 gb20y signature size refusal reason.
53text FWTXT "|short header: file smaller than an ELF64 header|ELF magic missing|not ELF64 (EI_CLASS)|not little-endian (EI_DATA)|EI_VERSION is not 1|e_type is not ET_REL (the pinned container is a relocatable object)|e_machine is not RISC-V (243)|e_shentsize is not 64|e_shnum is 0: no section header table|e_shnum above this tool's limit (256)|e_shstrndx out of range|section header table outside the file|section string table outside the file or empty|section string table not NUL-terminated|section name index outside the string table|section bounds: offset+size outside the file|missing section .fwimage|.fwimage empty|missing section .fwversion|missing section .note.gnu.build-id|missing section: no signature section for this chip family|signature section empty|version section size not in 1..63|version string not NUL-terminated|version mismatch against the pinned driver version|build-id note too short|build-id note is not an NT_GNU_BUILD_ID note|build-id note descriptor size out of range|section string table above this port's 16 MiB buffer|file | bytes=| family=| expect_version=|section | type=| offset=| size=|(null)|fwversion |build_id |signature_section |sha256_file |sha256_fwimage |sha256_signature |RESULT OK|RESULT REFUSED |.fwimage|.fwversion|.note.gnu.build-id|.fwsignature_gb10y|580.173.02|gb10y|GNU|No such file or directory|Permission denied|Is a directory|File too large|Input/output error|Not a directory|Too many levels of symbolic links|File name too long|Unknown error |open |: |short read|Too many open files|Cannot allocate memory|File exists|No space left on device|Read-only file system|.fwsignature_gb20y|gb20y|signature section size is not 4096|"
54text FWHEXD "0123456789abcdef"
55note build the segment table once
56fn fwsetup to void
57if eq get fwok 0 1
58ret
59end
60put fwok 0 1
61seg = ulong 0
62put fwsgb 0 0
63put fwsgl 0 0
64for i ulong len FWTXT
65c = get FWTXT i
66if eq c 124
67seg = add seg 1
68put fwsgb seg add i 1
69put fwsgl seg 0
70else
71put fwsgl seg add get fwsgl seg 1
72end
73end
74put fwon 0 0
75put fwofd 0 1
76put fwoerr 0 0
77end
78note explicit chip-family selector: FWFAM10 (gb10y, default) or FWFAM20 (gb20y); returns 0 or -1
79fn fwfamily fam ulong to long
80if or eq fam FWFAM10 eq fam FWFAM20
81put fwfam 0 fam
82ret 0
83end
84ret -1
85end
86fn fwsigseg to ulong
87if eq get fwfam 0 FWFAM20
88ret 71
89end
90ret 50
91end
92fn fwfamseg to ulong
93if eq get fwfam 0 FWFAM20
94ret 72
95end
96ret 52
97end
98note ---- buffered writer: fwob[0..fwon) goes to fd fwofd; a failed write sets fwoerr 1 ----
99fn fwoflush to long
100n = get fwon 0
101p = ulong 0
102while lt p n bound 999999
103r = write get fwofd 0 fwob p sub n p
104if le r 0
105put fwoerr 0 1
106put fwon 0 0
107ret -5
108end
109p = add p ulong r
110end
111put fwon 0 0
112ret 0
113end
114fn fwoc c uchar to void
115if ge get fwon 0 FWOCAP
116z = fwoflush
117end
118n = get fwon 0
119put fwob n c
120put fwon 0 add n 1
121end
122note print segment k of FWTXT
123fn fwos k ulong to void
124b = get fwsgb k
125for i ulong get fwsgl k
126fwoc get FWTXT add b i
127end
128end
129note print v in decimal
130fn fwodec v ulong to void
131d = new uchar 24 0
132n = ulong 0
133x = v
134while lt n 24 bound 24
135put d n add uchar rem x 10 48
136n = add n 1
137x = div x 10
138if eq x 0
139break
140end
141end
142while gt n 0 bound 24
143n = sub n 1
144fwoc get d n
145end
146end
147note print v in lowercase hex without leading zeros (C %x)
148fn fwohex v ulong to void
149d = new uchar 24 0
150n = ulong 0
151x = v
152while lt n 24 bound 24
153put d n get FWHEXD rem x 16
154n = add n 1
155x = div x 16
156if eq x 0
157break
158end
159end
160while gt n 0 bound 24
161n = sub n 1
162fwoc get d n
163end
164end
165note print shhx[0..2n) (call shhex n first)
166fn fwohx n ulong to void
167for i ulong mul n 2
168fwoc get shhx i
169end
170end
171fn fwopath to void
172for i ulong get fwpathn 0
173fwoc get fwpath i
174end
175end
176note print the strerror text of errno e (positive)
177fn fwoerrno e ulong to void
178k = ulong 0
179if eq e 2
180k = 54
181end
182if eq e 13
183k = 55
184end
185if eq e 21
186k = 56
187end
188if eq e 27
189k = 57
190end
191if eq e 5
192k = 58
193end
194if eq e 20
195k = 59
196end
197if eq e 40
198k = 60
199end
200if eq e 36
201k = 61
202end
203if eq e 24
204k = 66
205end
206if eq e 12
207k = 67
208end
209if eq e 17
210k = 68
211end
212if eq e 28
213k = 69
214end
215if eq e 30
216k = 70
217end
218if eq k 0
219fwos 62
220fwodec e
221ret
222end
223fwos k
224end
225note ---- pread loops, one per destination global: 0, -errno, or -5 when the file ends early ----
226fn fwrdhd fd int off ulong n ulong to long
227q = ulong 0
228while lt q n bound 999999
229r = pread fd fwhd q sub n q long add off q
230if lt r 0
231ret r
232end
233if eq r 0
234ret -5
235end
236q = add q ulong r
237end
238ret 0
239end
240fn fwrdsh fd int off ulong n ulong to long
241q = ulong 0
242while lt q n bound 999999
243r = pread fd fwsh q sub n q long add off q
244if lt r 0
245ret r
246end
247if eq r 0
248ret -5
249end
250q = add q ulong r
251end
252ret 0
253end
254fn fwrdst fd int off ulong n ulong to long
255q = ulong 0
256while lt q n bound 999999
257r = pread fd fwst q sub n q long add off q
258if lt r 0
259ret r
260end
261if eq r 0
262ret -5
263end
264q = add q ulong r
265end
266ret 0
267end
268fn fwrdbf fd int off ulong n ulong to long
269q = ulong 0
270while lt q n bound 999999
271r = pread fd fwbuf q sub n q long add off q
272if lt r 0
273ret r
274end
275if eq r 0
276ret -5
277end
278q = add q ulong r
279end
280ret 0
281end
282note ---- little-endian readers (C rd16/rd32/rd64) over fwhd, fwsh, fwbuf ----
283fn fwh16 p ulong to ulong
284ret or ulong get fwhd p shl ulong get fwhd add p 1 8
285end
286fn fwh64 p ulong to ulong
287v = ulong 0
288for i ulong 8
289v = or v shl ulong get fwhd add p i mul i 8
290end
291ret v
292end
293fn fws32 p ulong to ulong
294v = ulong 0
295for i ulong 4
296v = or v shl ulong get fwsh add p i mul i 8
297end
298ret v
299end
300fn fws64 p ulong to ulong
301v = ulong 0
302for i ulong 8
303v = or v shl ulong get fwsh add p i mul i 8
304end
305ret v
306end
307fn fwb32 p ulong to ulong
308v = ulong 0
309for i ulong 4
310v = or v shl ulong get fwbuf add p i mul i 8
311end
312ret v
313end
314note C in_bounds: off + size <= len without overflow
315fn fwinb off ulong size ulong flen ulong to ulong
316if gt off flen
317ret 0
318end
319if le size sub flen off
320ret 1
321end
322ret 0
323end
324note does the NUL-terminated name at fwst[off] equal segment k?
325fn fwnmeq off ulong k ulong to ulong
326b = get fwsgb k
327l = get fwsgl k
328n = get fwstn 0
329for i ulong l
330p = add off i
331if ge p n
332ret 0
333end
334if ne get fwst p get FWTXT add b i
335ret 0
336end
337end
338p2 = add off l
339if ge p2 n
340ret 0
341end
342if ne get fwst p2 0
343ret 0
344end
345ret 1
346end
347note checks 1..12 (C inspect_buf lines 123-138): returns 0, a reason, or 1000 on an I/O error
348fn fwchk1 fd int flen ulong to ulong
349if lt flen 64
350ret 1
351end
352r = fwrdhd fd 0 64
353if ne r 0
354ret 1000
355end
356note magic 0x7f 'E' 'L' 'F' = 127 69 76 70
357m = ulong 0
358if ne get fwhd 0 127
359m = 1
360end
361if ne get fwhd 1 69
362m = 1
363end
364if ne get fwhd 2 76
365m = 1
366end
367if ne get fwhd 3 70
368m = 1
369end
370if eq m 1
371ret 2
372end
373if ne get fwhd 4 2
374ret 3
375end
376if ne get fwhd 5 1
377ret 4
378end
379if ne get fwhd 6 1
380ret 5
381end
382if ne fwh16 16 1
383ret 6
384end
385if ne fwh16 18 243
386ret 7
387end
388if ne fwh16 58 64
389ret 8
390end
391n = fwh16 60
392if eq n 0
393ret 9
394end
395if gt n 256
396ret 10
397end
398if ge fwh16 62 n
399ret 11
400end
401if eq fwinb fwh64 40 mul n 64 flen 0
402ret 12
403end
404put fwshn 0 n
405r = fwrdsh fd fwh64 40 mul n 64
406if ne r 0
407ret 1000
408end
409ret 0
410end
411note checks 13..16 (C lines 139-154): string table, then every section header in order
412fn fwchk2 fd int flen ulong to ulong
413si = mul fwh16 62 64
414so = fws64 add si 24
415ss = fws64 add si 32
416if eq ss 0
417ret 13
418end
419if eq fwinb so ss flen 0
420ret 13
421end
422if gt ss FWSTMX
423ret 29
424end
425r = fwrdst fd so ss
426if ne r 0
427ret 1000
428end
429if ne get fwst sub ss 1 0
430ret 14
431end
432put fwstn 0 ss
433ret fwsecs flen
434end
435note C lines 145-154: every section header in order; records each before checking the next
436fn fwsecs flen ulong to ulong
437for i ulong get fwshn 0
438b = mul i 64
439nm = fws32 b
440ty = fws32 add b 4
441of = fws64 add b 24
442sz = fws64 add b 32
443if ge nm get fwstn 0
444ret 15
445end
446if ne ty 8
447if gt sz 0
448if eq fwinb of sz flen 0
449ret 16
450end
451end
452end
453k = get fwnsec 0
454put fwsn k nm
455put fwsty k ty
456put fwsof k of
457put fwssz k sz
458put fwnsec 0 add k 1
459end
460ret 0
461end
462note checks 17..22 (C lines 155-168): the strcmp chain, last match wins
463fn fwchk3 to ulong
464put fwimx 0 FWNONE
465put fwvx 0 FWNONE
466put fwbx 0 FWNONE
467put fwsx 0 FWNONE
468for i ulong get fwnsec 0
469o = get fwsn i
470if eq fwnmeq o 47 1
471put fwimx 0 i
472else
473if eq fwnmeq o 48 1
474put fwvx 0 i
475else
476if eq fwnmeq o 49 1
477put fwbx 0 i
478else
479if eq fwnmeq o fwsigseg 1
480put fwsx 0 i
481end
482end
483end
484end
485end
486x = get fwimx 0
487if eq x FWNONE
488ret 17
489end
490if eq get fwsty x 8
491ret 18
492end
493if eq get fwssz x 0
494ret 18
495end
496if eq get fwvx 0 FWNONE
497ret 19
498end
499if eq get fwbx 0 FWNONE
500ret 20
501end
502x = get fwsx 0
503if eq x FWNONE
504ret 21
505end
506if eq get fwsty x 8
507ret 22
508end
509if eq get fwssz x 0
510ret 22
511end
512if eq get fwfam 0 FWFAM20
513if ne get fwssz x 4096
514ret 73
515end
516end
517ret 0
518end
519note checks 23..25 (C lines 170-175): .fwversion size, NUL, equality with the pin "580.173.02"
520fn fwchk4 fd int to ulong
521x = get fwvx 0
522sz = get fwssz x
523if eq get fwsty x 8
524ret 23
525end
526if eq sz 0
527ret 23
528end
529if ge sz 64
530ret 23
531end
532r = fwrdbf fd get fwsof x sz
533if ne r 0
534ret 1000
535end
536if ne get fwbuf sub sz 1 0
537ret 24
538end
539n = ulong 0
540while lt n sz bound 64
541if eq get fwbuf n 0
542break
543end
544put fwver n get fwbuf n
545n = add n 1
546end
547put fwvern 0 n
548if ne sz add get fwsgl 51 1
549ret 25
550end
551b = get fwsgb 51
552for i ulong get fwsgl 51
553if ne get fwbuf i get FWTXT add b i
554ret 25
555end
556end
557ret 0
558end
559note checks 26..28 (C lines 177-185): the NT_GNU_BUILD_ID note, then its hex
560fn fwchk5 fd int to ulong
561x = get fwbx 0
562sz = get fwssz x
563if eq get fwsty x 8
564ret 26
565end
566if lt sz 16
567ret 26
568end
569n = sz
570if gt n 80
571n = 80
572end
573r = fwrdbf fd get fwsof x n
574if ne r 0
575ret 1000
576end
577m = ulong 0
578if ne fwb32 0 4
579m = 1
580end
581note "GNU\0" = 71 78 85 0
582if ne get fwbuf 12 71
583m = 1
584end
585if ne get fwbuf 13 78
586m = 1
587end
588if ne get fwbuf 14 85
589m = 1
590end
591if ne get fwbuf 15 0
592m = 1
593end
594if ne fwb32 8 3
595m = 1
596end
597if eq m 1
598ret 27
599end
600d = fwb32 4
601if eq d 0
602ret 28
603end
604if gt d 64
605ret 28
606end
607if gt add 16 d sz
608ret 28
609end
610fwbidhx d
611ret 0
612end
613note lowercase hex of the d note descriptor bytes fwbuf[16..16+d) into fwbidh (C hex)
614fn fwbidhx d ulong to void
615for i ulong d
616c = get fwbuf add 16 i
617put fwbidh mul i 2 get FWHEXD shr c 4
618put fwbidh add mul i 2 1 get FWHEXD and c 15
619end
620put fwbidn 0 mul d 2
621end
622note the three SHA-256 digests as lowercase hex in fwhxf / fwhxi / fwhxs; 0 or 1000 on an I/O error
623fn fwdig fd int flen ulong to ulong
624sh2init 0
625sh2init 1
626sh2init 2
627r = sh2file 0 fd 0 flen
628if ne r 0
629ret 1000
630end
631x = get fwimx 0
632r = sh2file 1 fd get fwsof x get fwssz x
633if ne r 0
634ret 1000
635end
636x = get fwsx 0
637r = sh2file 2 fd get fwsof x get fwssz x
638if ne r 0
639ret 1000
640end
641sh2fin 0
642shhex 32
643for i ulong 64
644put fwhxf i get shhx i
645end
646sh2fin 1
647shhex 32
648for i ulong 64
649put fwhxi i get shhx i
650end
651sh2fin 2
652shhex 32
653for i ulong 64
654put fwhxs i get shhx i
655end
656ret 0
657end
658note all checks in the C order: 0 = every check passed, 1..29 = refusal reason, 1000 = I/O error
659fn fwcheck fd int flen ulong to ulong
660put fwnsec 0 0
661put fwlen 0 flen
662e = fwchk1 fd flen
663if ne e 0
664ret e
665end
666e = fwchk2 fd flen
667if ne e 0
668ret e
669end
670e = fwchk3
671if ne e 0
672ret e
673end
674e = fwchk4 fd
675if ne e 0
676ret e
677end
678e = fwchk5 fd
679if ne e 0
680ret e
681end
682ret fwdig fd flen
683end
684note print the NUL-terminated name at fwst[off], or (null) when it is empty
685fn fwoname off ulong to void
686if eq get fwst off 0
687fwos 38
688ret
689end
690note the string table is NUL-terminated (check 14), so the name ends inside it; chunked for the
691note 999999 bound (a name may run up to 16 MiB)
692p = off
693go = ulong 1
694while eq go 1 bound 20
695while ne get fwst p 0 bound 999999
696fwoc get fwst p
697p = add p 1
698end
699if eq get fwst p 0
700go = 0
701end
702end
703end
704note C cmd_inspect + print_report for an open fd of flen bytes, path in fwpath[0..fwpathn).
705note Returns 0 = RESULT OK, 2 = RESULT REFUSED, 1 = I/O error (the C prints "short read" on
706note stderr and nothing on stdout; here the buffered file line is dropped and the caller reports).
707fn fwinsp fd int flen ulong to ulong
708fwsetup
709mk = get fwon 0
710fwos 30
711fwopath
712fwos 31
713fwodec flen
714fwos 32
715fwos fwfamseg
716fwos 33
717fwos 51
718fwoc 10
719e = fwcheck fd flen
720if eq e 1000
721put fwon 0 mk
722ret 1
723end
724for i ulong get fwnsec 0
725fwos 34
726fwodec i
727fwoc 32
728fwoname get fwsn i
729fwos 35
730fwodec get fwsty i
731fwos 36
732fwodec get fwsof i
733fwos 37
734fwodec get fwssz i
735fwoc 10
736end
737if ne e 0
738fwos 46
739fwos e
740fwoc 10
741ret 2
742end
743fwos 39
744for i ulong get fwvern 0
745fwoc get fwver i
746end
747fwoc 10
748fwos 40
749for i ulong get fwbidn 0
750fwoc get fwbidh i
751end
752fwoc 10
753fwos 41
754fwos fwsigseg
755fwos 37
756fwodec get fwssz get fwsx 0
757fwoc 10
758fwos 42
759for i ulong 64
760fwoc get fwhxf i
761end
762fwoc 10
763fwos 43
764for i ulong 64
765fwoc get fwhxi i
766end
767fwos 37
768fwodec get fwssz get fwimx 0
769fwoc 10
770fwos 44
771for i ulong 64
772fwoc get fwhxs i
773end
774fwoc 10
775fwos 45
776fwoc 10
777ret 0
778end