interplane.Get Omega
Read in your language

omega-gpu-driver

aien-dev/omega-gpu-driver

Brought by drakestapleton

branch main Branches Tags History

omega-gpu-driver / gsp / fw.om

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