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 / bindata.om

1526 lines · 28406 bytes Raw Blame History
1note gsp/bindata.om: the NVIDIA generated bindata reader in seeded Omega (OM-1). Ports the extract path
2note of aienux PR #441 native/kernel/tools/ck_gsp_bindata_extract.c: refuse, next_line, after,
3note parse_u32, copy_field, parse_hex_array, parse_pvt, parse_archive, parse_file, find_label,
4note decode_desc, region_ok, check_semantics, report (stdout lines; no --out writing) and the read
5note step of cmd_extract. Every refusal text is the C's, "line N: " prefix included.
6note Library unit: no main. Prefix bd. Needs gsp/sha2.om, gsp/inflate.om and gsp/fw.om first
7note (SHA-256/384, DEFLATE + CRC-32 + ISIZE, and the fwo* stdout writer).
8note The text is read through a 1 MiB pread window (the C reads the whole file, at most 64 MiB, first;
9note this port hashes the whole file first, so a read failure is still reported before any parse).
10note Deviations (documented): arrays live in two 16 MiB pools, bdraw for compressed arrays as stored and
11note bdpl for plain bytes (uncompressed arrays are parsed straight into bdpl). Running out of a pool is
12note the C's own "out of memory for LABEL" refusal; the C would malloc up to MAX_ARRAY per array and
13note DATA SIZE per inflated array. A directory path is refused as "File too large", the text the C
14note prints on this host (glibc ftell on a directory).
15const BDMAXB ulong 16
16const BDPOOL ulong 16777216
17const BDMAXA ulong 8388608
18const BDNONE ulong 999
19const BDUNS ulong 4294967295
20const BDWIN ulong 1048576
21global bdok ulong 1
22global bdsgb ulong 160
23global bdsgl ulong 160
24global bdpath uchar 4096
25global bdpathn ulong 1
26global bdfd int 1
27global bdst uchar 128
28global bdfsz ulong 1
29global bdfpos ulong 1
30global bdtp ulong 1
31global bdte ulong 1
32global bdtx uchar 1048576
33global bdln uchar 520
34global bdll ulong 1
35global bdlno ulong 1
36global bdrs uchar 256
37global bdrsn ulong 1
38global bdn ulong 1
39global bdarc ulong 1
40global bdcmp ulong 16
41global bdds ulong 16
42global bdcs ulong 16
43global bdhh ulong 16
44global bdhd ulong 16
45global bdhp ulong 16
46global bdnb ulong 16
47global bdbo ulong 16
48global bdpo ulong 16
49global bdpn ulong 16
50global bdfn uchar 1536
51global bdfnl ulong 16
52global bdlb uchar 1024
53global bdlbl ulong 16
54global bdfm uchar 2560
55global bdfml ulong 16
56global bdid uchar 3072
57global bdidl ulong 16
58global bdraw uchar 16777216
59global bdrtop ulong 1
60global bdpl uchar 16777216
61global bdptop ulong 1
62global bdimg uchar 64
63global bdimgn ulong 1
64global bdfsh uchar 64
65note segment k of BDTXT is the text between the k-th and (k+1)-th '|': 1..76 refusal pieces in C
66note wording, 77..114 the C's line prefixes and tokens, 115.. report pieces
67text BDTXT "|line |: |line longer than 511 characters|array size out of range for |expected '{' after the array declaration of |out of memory for |file ends inside the array of |non-hex token in the array of |byte token without a trailing comma in |more bytes than the declared size in |fewer bytes than the declared size in |expected '{' in the storage record of |storage record: bad uncompressed size for |storage record: uncompressed size differs from DATA SIZE for |storage record: bad compressed size for |storage record: compressed size differs from the header for |storage record: missing pointer line for |storage record: data pointer is not the array of |storage record: missing compressed flag for |storage record: bad compressed flag for |storage record: compressed flag disagrees with COMPRESSION for |second archive table|archive table name is not the file's function|expected '{' after the archive declaration|archive: bad entryNum|archive: entryNum differs from the number of arrays|archive: expected entries block|archive: expected the entries comment|archive: missing entry |archive: malformed entry for |archive: entry order or label differs from the arrays at |archive: entry without a storage index for |archive: storage index is not this array for |archive: expected end of entries|archive: expected end of table|array | has no data or no storage record|more than 16 arrays|FUNCTION line without a BINDATA_LABEL|FUNCTION line not terminated|bad function name|bad label|array belongs to a different function: |duplicate label |function is not a kgspGetBinArchive function: |identifier too long for |data before the first FUNCTION header|bad file name for |FILE TYPE is not BINARY for |COMPRESSION is neither YES nor NO for |COMPLEX_STRUCT is not NO for |bad DATA SIZE for |bad COMPRESSED SIZE for |second array for |array header incomplete for |COMPRESSED SIZE missing with COMPRESSION YES for |COMPRESSED SIZE given with COMPRESSION NO for |array identifier is not the expected one for |storage record before the array of |second storage record for |no arrays found|no archive table|descriptor is not 84 bytes: |descriptor version is not 5: |descriptor monitor not enabled: |descriptor without a matching image: |descriptor manifest region outside the image or unaligned: |descriptor monitor-data region outside the image or unaligned: |descriptor monitor-code region outside the image or unaligned: |hash array is not 48 bytes|hash array without UCODE_IMAGE|UCODE_HASH is not SHA-384 of UCODE_IMAGE| is not 384 bytes|unknown label | (|)|// FUNCTION: |(\"BINDATA_LABEL_|\")|kgspGetBinArchive|kgspBinArchive|_BINDATA_LABEL_|_data|// FILE NAME: |// FILE TYPE: |BINARY|// COMPRESSION: |YES|NO|// COMPLEX_STRUCT: |// DATA SIZE (bytes): |// COMPRESSED SIZE (bytes): |N/A|static BINDATA_CONST NvU8 |[] = |#if defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)|static const BINDATA_ARCHIVE __|static BINDATA_CONST|static const BINDATA_ARCHIVE|{|};|NV_TRUE,|NV_FALSE,| =|// entries[]|{ BINDATA_LABEL_|&g_bindata_pvt[__idx_|}|UCODE_DESC|UCODE_IMAGE|UCODE_HASH|UCODE_SIG|UCODE_PKEY|//|function | arrays=|desc | version=| manifest=0x|+0x| monitor_data=0x| monitor_code=0x| monitor_enabled=| fb_reserved=0x| signed_as_code=| app_version=| image=|/|hash384 UCODE_HASH equals sha384(UCODE_IMAGE)| file=| stored=| compressed=|yes|no| size=| sha256=|RESULT OK arrays=|REFUSED: |REFUSED: cannot read | |"
68note build the segment table and reset the pools (call once per program)
69fn bdsetup to void
70if eq get bdok 0 1
71ret
72end
73put bdok 0 1
74fwsetup
75infsetup
76seg = ulong 0
77for i ulong len BDTXT
78c = get BDTXT i
79if eq c 124
80seg = add seg 1
81put bdsgb seg add i 1
82put bdsgl seg 0
83else
84put bdsgl seg add get bdsgl seg 1
85end
86end
87put bdrtop 0 0
88put bdptop 0 0
89end
90note ---- printing through the fw.om writer ----
91fn bdos k ulong to void
92b = get bdsgb k
93for i ulong get bdsgl k
94fwoc get BDTXT add b i
95end
96end
97fn bdolab i ulong to void
98b = mul i 64
99for j ulong get bdlbl i
100fwoc get bdlb add b j
101end
102end
103fn bdofn i ulong to void
104b = mul i 96
105for j ulong get bdfnl i
106fwoc get bdfn add b j
107end
108end
109fn bdofm i ulong to void
110b = mul i 160
111for j ulong get bdfml i
112fwoc get bdfm add b j
113end
114end
115fn bdopath to void
116for i ulong get bdpathn 0
117fwoc get bdpath i
118end
119end
120note ---- the refusal reason (C f->reason, 256 bytes: at most 255 characters kept) ----
121fn bdrc c uchar to void
122n = get bdrsn 0
123if lt n 255
124put bdrs n c
125put bdrsn 0 add n 1
126end
127end
128fn bdrseg k ulong to void
129b = get bdsgb k
130for i ulong get bdsgl k
131bdrc get BDTXT add b i
132end
133end
134fn bdrlab i ulong to void
135b = mul i 64
136for j ulong get bdlbl i
137bdrc get bdlb add b j
138end
139end
140fn bdrfn i ulong to void
141b = mul i 96
142for j ulong get bdfnl i
143bdrc get bdfn add b j
144end
145end
146note "line N: " (C refuse: N is the cursor's line count, 0 when the cursor is NULL)
147fn bdrhd ln ulong to void
148put bdrsn 0 0
149bdrseg 1
150d = new uchar 24 0
151n = ulong 0
152x = ln
153while lt n 24 bound 24
154put d n add uchar rem x 10 48
155n = add n 1
156x = div x 10
157if eq x 0
158break
159end
160end
161while gt n 0 bound 24
162n = sub n 1
163bdrc get d n
164end
165bdrseg 2
166end
167note refuse at the current line with reason k, then the label of block b unless b is BDNONE
168fn bdrl k ulong b ulong to ulong
169bdrhd get bdlno 0
170bdrseg k
171if ne b BDNONE
172bdrlab b
173end
174ret 2
175end
176note refuse at the current line with reason k and the function name of block b
177fn bdrf k ulong b ulong to ulong
178bdrhd get bdlno 0
179bdrseg k
180bdrfn b
181ret 2
182end
183note refuse with line 0 (semantic checks) with reason k, then the label of b unless BDNONE
184fn bdr0 k ulong b ulong to ulong
185bdrhd 0
186bdrseg k
187if ne b BDNONE
188bdrlab b
189end
190ret 2
191end
192note "array LABEL has no data or no storage record" at the current line
193fn bdrnd b ulong to ulong
194bdrhd get bdlno 0
195bdrseg 36
196bdrlab b
197bdrseg 37
198ret 2
199end
200note ---- the text window and C next_line ----
201note keep at least 512 unread bytes in bdtx unless the file is used up: 0, or 3 on a read error
202fn bdfill to ulong
203tp = get bdtp 0
204av = sub get bdte 0 tp
205if ge av 512
206ret 0
207end
208fp = get bdfpos 0
209if ge fp get bdfsz 0
210ret 0
211end
212for i ulong av
213put bdtx i get bdtx add tp i
214end
215put bdtp 0 0
216put bdte 0 av
217w = sub BDWIN av
218lf = sub get bdfsz 0 fp
219if lt lf w
220w = lf
221end
222q = ulong 0
223while lt q w bound 999999
224r = pread get bdfd 0 bdtx add av q sub w q long add fp q
225if lt r 1
226ret 3
227end
228q = add q ulong r
229end
230put bdte 0 add av w
231put bdfpos 0 add fp w
232ret 0
233end
234note C next_line into bdln[0..bdll): 1 = a line, 0 = end of text, 2 = longer than 511 characters
235note (reason set, line count advanced), 3 = read error. A trailing CR is dropped and the line ends
236note at its first NUL (the C's string view of its buffer).
237fn bdnext to ulong
238e = bdfill
239if ne e 0
240ret 3
241end
242tp = get bdtp 0
243te = get bdte 0
244if ge tp te
245ret 0
246end
247lim = add tp 512
248if gt lim te
249lim = te
250end
251j = tp
252nl = ulong 0
253while lt j lim bound 600
254if eq get bdtx j 10
255nl = 1
256break
257end
258j = add j 1
259end
260put bdlno 0 add get bdlno 0 1
261n = sub j tp
262if ge n 512
263bdrhd get bdlno 0
264bdrseg 3
265ret 2
266end
267for i ulong n
268put bdln i get bdtx add tp i
269end
270if gt n 0
271if eq get bdln sub n 1 13
272n = sub n 1
273end
274end
275k = ulong 0
276while lt k n bound 600
277if eq get bdln k 0
278break
279end
280k = add k 1
281end
282put bdln k 0
283put bdll 0 k
284if eq nl 1
285put bdtp 0 add j 1
286else
287put bdtp 0 te
288end
289ret 1
290end
291note ---- string helpers over the line ----
292fn bdlc i ulong to uchar
293if ge i get bdll 0
294ret 0
295end
296ret get bdln i
297end
298note does the line at p start with segment k (C after / strncmp)?
299fn bdpre p ulong k ulong to ulong
300b = get bdsgb k
301l = get bdsgl k
302if gt add p l get bdll 0
303ret 0
304end
305for i ulong l
306if ne get bdln add p i get BDTXT add b i
307ret 0
308end
309end
310ret 1
311end
312note is the line from p to its end exactly segment k (C strcmp)?
313fn bdeqs p ulong k ulong to ulong
314L = get bdll 0
315if gt p L
316ret 0
317end
318if ne sub L p get bdsgl k
319ret 0
320end
321ret bdpre p k
322end
323note C strspn(line + p, " ")
324fn bdsp p ulong to ulong
325q = p
326L = get bdll 0
327while lt q L bound 600
328if ne get bdln q 32
329break
330end
331q = add q 1
332end
333ret q
334end
335note first position at or after p where segment k occurs (C strstr), BDNONE if none
336fn bdfind p ulong k ulong to ulong
337q = p
338L = get bdll 0
339while le q L bound 600
340if eq bdpre q k 1
341ret q
342end
343q = add q 1
344end
345ret BDNONE
346end
347note first '(' at or after p (C strchr), BDNONE if none
348fn bdfpar p ulong to ulong
349q = p
350L = get bdll 0
351while lt q L bound 600
352if eq get bdln q 40
353ret q
354end
355q = add q 1
356end
357ret BDNONE
358end
359note C parse_u32 at p: decimal at most 4294967295, then spaces, commas or tabs, then the end or "//"
360fn bdu32 p ulong to long
361c = bdlc p
362if lt c 48
363ret -1
364end
365if gt c 57
366ret -1
367end
368acc = ulong 0
369q = p
370while lt q get bdll 0 bound 600
371c = get bdln q
372if lt c 48
373break
374end
375if gt c 57
376break
377end
378acc = add mul acc 10 sub ulong c 48
379if gt acc BDUNS
380ret -1
381end
382q = add q 1
383end
384while lt q get bdll 0 bound 600
385c = get bdln q
386if and and ne c 32 ne c 44 ne c 9
387break
388end
389q = add q 1
390end
391if lt q get bdll 0
392if eq bdpre q 114 0
393ret -1
394end
395end
396ret long acc
397end
398note does the line at p hold the first n characters of block i's identifier?
399fn bdlnid p ulong i ulong n ulong to ulong
400if gt add p n get bdll 0
401ret 0
402end
403b = mul i 192
404for j ulong n
405if ne get bdln add p j get bdid add b j
406ret 0
407end
408end
409ret 1
410end
411note does the line at p start with block i's label?
412fn bdlnlb p ulong i ulong to ulong
413n = get bdlbl i
414if gt add p n get bdll 0
415ret 0
416end
417b = mul i 64
418for j ulong n
419if ne get bdln add p j get bdlb add b j
420ret 0
421end
422end
423ret 1
424end
425note does the line at p start with block i's function name?
426fn bdlnfn p ulong i ulong to ulong
427n = get bdfnl i
428if gt add p n get bdll 0
429ret 0
430end
431b = mul i 96
432for j ulong n
433if ne get bdln add p j get bdfn add b j
434ret 0
435end
436end
437ret 1
438end
439note labels of blocks i and j equal?
440fn bdlbeq i ulong j ulong to ulong
441n = get bdlbl i
442if ne n get bdlbl j
443ret 0
444end
445a = mul i 64
446b = mul j 64
447for k ulong n
448if ne get bdlb add a k get bdlb add b k
449ret 0
450end
451end
452ret 1
453end
454note function names of blocks i and j equal?
455fn bdfneq i ulong j ulong to ulong
456n = get bdfnl i
457if ne n get bdfnl j
458ret 0
459end
460a = mul i 96
461b = mul j 96
462for k ulong n
463if ne get bdfn add a k get bdfn add b k
464ret 0
465end
466end
467ret 1
468end
469note label of block i starts with segment k (sw 0) or equals it (sw 1)?
470fn bdlbsg i ulong k ulong sw ulong to ulong
471n = get bdlbl i
472l = get bdsgl k
473if lt n l
474ret 0
475end
476if eq sw 1
477if ne n l
478ret 0
479end
480end
481a = mul i 64
482b = get bdsgb k
483for j ulong l
484if ne get bdlb add a j get BDTXT add b j
485ret 0
486end
487end
488ret 1
489end
490note function name of block i starts with segment k?
491fn bdfnsg i ulong k ulong to ulong
492n = get bdfnl i
493l = get bdsgl k
494if lt n l
495ret 0
496end
497a = mul i 96
498b = get bdsgb k
499for j ulong l
500if ne get bdfn add a j get BDTXT add b j
501ret 0
502end
503end
504ret 1
505end
506note C find_label for the name in bdimg[0..bdimgn): block index or BDNONE
507fn bdfimg to ulong
508for i ulong get bdn 0
509n = get bdlbl i
510if eq n get bdimgn 0
511a = mul i 64
512m = ulong 1
513for j ulong n
514if ne get bdlb add a j get bdimg j
515m = 0
516end
517end
518if eq m 1
519ret i
520end
521end
522end
523ret BDNONE
524end
525note C find_label for segment k: block index or BDNONE
526fn bdfseg k ulong to ulong
527for i ulong get bdn 0
528if eq bdlbsg i k 1 1
529ret i
530end
531end
532ret BDNONE
533end
534note ---- C copy_field into the block's fields (len checked by the caller) ----
535fn bdsetfn i ulong p ulong n ulong to void
536b = mul i 96
537for j ulong n
538put bdfn add b j get bdln add p j
539end
540put bdfnl i n
541end
542fn bdsetlb i ulong p ulong n ulong to void
543b = mul i 64
544for j ulong n
545put bdlb add b j get bdln add p j
546end
547put bdlbl i n
548end
549fn bdsetfm i ulong p ulong n ulong to void
550b = mul i 160
551for j ulong n
552put bdfm add b j get bdln add p j
553end
554put bdfml i n
555end
556note append segment k to block i's identifier at q; returns the new end
557fn bdidsg i ulong q ulong k ulong to ulong
558b = mul i 192
559s = get bdsgb k
560for j ulong get bdsgl k
561put bdid add b add q j get BDTXT add s j
562end
563ret add q get bdsgl k
564end
565note identifier kgspBinArchive<stem>_BINDATA_LABEL_<label>_data (caller checked its length)
566fn bdmkid i ulong to void
567b = mul i 192
568q = bdidsg i 0 81
569fb = mul i 96
570note stem = function name after "kgspGetBinArchive" (17 characters)
571for j ulong sub get bdfnl i 17
572put bdid add b q get bdfn add add fb 17 j
573q = add q 1
574end
575q = bdidsg i q 82
576lb = mul i 64
577for j ulong get bdlbl i
578put bdid add b q get bdlb add lb j
579q = add q 1
580end
581q = bdidsg i q 83
582put bdidl i q
583end
584note ---- C parse_file: one "// FUNCTION: " line ----
585fn bdfunc to ulong
586n = get bdn 0
587if gt n 0
588b = sub n 1
589if eq and eq get bdhd b 1 eq get bdhp b 1 false
590ret bdrnd b
591end
592end
593if ge n BDMAXB
594ret bdrl 38 BDNONE
595end
596v = get bdsgl 77
597lp = bdfpar v
598if eq lp BDNONE
599ret bdrl 39 BDNONE
600end
601if eq bdpre lp 78 0
602ret bdrl 39 BDNONE
603end
604pe = bdfind add lp 16 79
605if eq pe BDNONE
606ret bdrl 40 BDNONE
607end
608if ne add pe 2 get bdll 0
609ret bdrl 40 BDNONE
610end
611put bdn 0 add n 1
612fl = sub lp v
613if or eq fl 0 ge fl 96
614ret bdrl 41 BDNONE
615end
616bdsetfn n v fl
617ll = sub sub pe lp 16
618if or eq ll 0 ge ll 64
619ret bdrl 42 BDNONE
620end
621bdsetlb n add lp 16 ll
622ret bdfunc2 n
623end
624note the rest of the FUNCTION header: same function, unique label, stem, identifier
625fn bdfunc2 n ulong to ulong
626if gt n 0
627if eq bdfneq 0 n 0
628ret bdrf 43 n
629end
630end
631for j ulong n
632if eq bdlbeq j n 1
633ret bdrl 44 n
634end
635end
636put bdcs n BDUNS
637put bdcmp n 2
638put bdds n 0
639put bdhh n 0
640put bdhd n 0
641put bdhp n 0
642put bdnb n 0
643put bdfml n 0
644if eq bdfnsg n 80 0
645ret bdrf 45 n
646end
647note 14 + (funclen - 17) + 15 + labellen + 5 characters must stay below 192
648t = add sub get bdfnl n 17 34
649t = add t get bdlbl n
650if ge t 192
651ret bdrl 46 n
652end
653bdmkid n
654ret 0
655end
656note "static BINDATA_CONST NvU8 " line of block b
657fn bdarrhd b ulong to ulong
658if eq get bdhd b 1
659ret bdrl 54 b
660end
661c = get bdcmp b
662m = ulong 0
663if eq c 2
664m = 1
665end
666if eq get bdds b 0
667m = 1
668end
669if eq get bdcs b BDUNS
670m = 1
671end
672if eq get bdfml b 0
673m = 1
674end
675if eq m 1
676ret bdrl 55 b
677end
678if and eq c 1 eq get bdcs b 0
679ret bdrl 56 b
680end
681if and eq c 0 ne get bdcs b 0
682ret bdrl 57 b
683end
684v = get bdsgl 94
685il = get bdidl b
686if eq bdlnid v b il 0
687ret bdrl 58 b
688end
689if eq bdeqs add v il 95 0
690ret bdrl 58 b
691end
692put bdhh b 1
693ret bdhex b
694end
695fn bdhexd c uchar to ulong
696if and ge c 48 le c 57
697ret sub ulong c 48
698end
699if and ge c 97 le c 102
700ret sub ulong c 87
701end
702if and ge c 65 le c 70
703ret sub ulong c 55
704end
705ret 16
706end
707note C parse_hex_array: the "{" line, the byte lines, "};"
708fn bdhex b ulong to ulong
709want = get bdds b
710if eq get bdcmp b 1
711want = get bdcs b
712end
713if or eq want 0 gt want BDMAXA
714ret bdrl 4 b
715end
716r = bdnext
717if eq r 3
718ret 3
719end
720if ne r 1
721ret bdrl 5 b
722end
723if eq bdeqs 0 100 0
724ret bdrl 5 b
725end
726if eq get bdcmp b 1
727t = get bdrtop 0
728if gt add t want BDPOOL
729ret bdrl 6 b
730end
731put bdbo b t
732put bdrtop 0 add t want
733else
734t = get bdptop 0
735if gt add t want BDPOOL
736ret bdrl 6 b
737end
738put bdbo b t
739put bdpo b t
740put bdpn b want
741put bdptop 0 add t want
742end
743put bdnb b 0
744go = ulong 1
745while eq go 1 bound 100
746while eq go 1 bound 999999
747r = bdnext
748if eq r 3
749ret 3
750end
751if eq r 2
752ret 2
753end
754if eq r 0
755ret bdrl 7 b
756end
757if eq bdeqs 0 101 1
758go = 0
759else
760e = bdhexln b want
761if ne e 0
762ret e
763end
764end
765end
766end
767if ne get bdnb b want
768ret bdrl 11 b
769end
770put bdhd b 1
771ret 0
772end
773note one line of "0xHH," tokens of block b
774fn bdhexln b ulong want ulong to ulong
775p = ulong 0
776L = get bdll 0
777cm = get bdcmp b
778base = get bdbo b
779while lt p L bound 600
780while lt p L bound 600
781c = get bdln p
782if and ne c 32 ne c 9
783break
784end
785p = add p 1
786end
787if ge p L
788break
789end
790if ne bdlc p 48
791ret bdrl 8 b
792end
793if ne bdlc add p 1 120
794ret bdrl 8 b
795end
796h = bdhexd bdlc add p 2
797if eq h 16
798ret bdrl 8 b
799end
800l = bdhexd bdlc add p 3
801if eq l 16
802ret bdrl 8 b
803end
804if ne bdlc add p 4 44
805ret bdrl 9 b
806end
807nb = get bdnb b
808if ge nb want
809ret bdrl 10 b
810end
811v = uchar add mul h 16 l
812if eq cm 1
813put bdraw add base nb v
814else
815put bdpl add base nb v
816end
817put bdnb b add nb 1
818p = add p 5
819end
820ret 0
821end
822note C parse_pvt: the storage record after "#if defined(BINDATA_INCLUDE_STORAGE_PVT_DEFN)"
823fn bdpvt b ulong to ulong
824r = bdnext
825if eq r 3
826ret 3
827end
828if ne r 1
829ret bdrl 12 b
830end
831if eq bdeqs 0 100 0
832ret bdrl 12 b
833end
834r = bdnext
835if eq r 3
836ret 3
837end
838if ne r 1
839ret bdrl 13 b
840end
841v = bdu32 bdsp 0
842if lt v 0
843ret bdrl 13 b
844end
845if ne ulong v get bdds b
846ret bdrl 14 b
847end
848r = bdnext
849if eq r 3
850ret 3
851end
852if ne r 1
853ret bdrl 15 b
854end
855v = bdu32 bdsp 0
856if lt v 0
857ret bdrl 15 b
858end
859w = get bdds b
860if eq get bdcmp b 1
861w = get bdcs b
862end
863if ne ulong v w
864ret bdrl 16 b
865end
866ret bdpvt2 b
867end
868note the pointer and flag lines of the storage record
869fn bdpvt2 b ulong to ulong
870r = bdnext
871if eq r 3
872ret 3
873end
874if ne r 1
875ret bdrl 17 b
876end
877p = bdsp 0
878il = get bdidl b
879if eq bdlnid p b il 0
880ret bdrl 18 b
881end
882if ne bdlc add p il 44
883ret bdrl 18 b
884end
885r = bdnext
886if eq r 3
887ret 3
888end
889if ne r 1
890ret bdrl 19 b
891end
892p = bdsp 0
893fl = ulong 2
894if eq bdpre p 102 1
895fl = 1
896else
897if eq bdpre p 103 1
898fl = 0
899end
900end
901if eq fl 2
902ret bdrl 20 b
903end
904if ne fl get bdcmp b
905ret bdrl 21 b
906end
907put bdhp b 1
908ret 0
909end
910note C parse_archive (the declaration is the current line)
911fn bdarch to ulong
912if eq get bdarc 0 1
913ret bdrl 22 BDNONE
914end
915p = get bdsgl 97
916if eq bdlnfn p 0 0
917ret bdrl 23 BDNONE
918end
919if eq bdeqs add p get bdfnl 0 104 0
920ret bdrl 23 BDNONE
921end
922r = bdnext
923if eq r 3
924ret 3
925end
926if ne r 1
927ret bdrl 24 BDNONE
928end
929if eq bdeqs 0 100 0
930ret bdrl 24 BDNONE
931end
932r = bdnext
933if eq r 3
934ret 3
935end
936if ne r 1
937ret bdrl 25 BDNONE
938end
939v = bdu32 bdsp 0
940if lt v 0
941ret bdrl 25 BDNONE
942end
943if ne ulong v get bdn 0
944ret bdrl 26 BDNONE
945end
946ret bdarch2
947end
948note the entries block of the archive table
949fn bdarch2 to ulong
950r = bdnext
951if eq r 3
952ret 3
953end
954if ne r 1
955ret bdrl 27 BDNONE
956end
957if eq bdeqs bdsp 0 100 0
958ret bdrl 27 BDNONE
959end
960r = bdnext
961if eq r 3
962ret 3
963end
964if ne r 1
965ret bdrl 28 BDNONE
966end
967if eq bdpre bdsp 0 105 0
968ret bdrl 28 BDNONE
969end
970for i ulong get bdn 0
971e = bdarent i
972if ne e 0
973ret e
974end
975end
976r = bdnext
977if eq r 3
978ret 3
979end
980if ne r 1
981ret bdrl 34 BDNONE
982end
983if eq bdeqs bdsp 0 108 0
984ret bdrl 34 BDNONE
985end
986r = bdnext
987if eq r 3
988ret 3
989end
990if ne r 1
991ret bdrl 35 BDNONE
992end
993if eq bdeqs 0 101 0
994ret bdrl 35 BDNONE
995end
996put bdarc 0 1
997ret 0
998end
999note one archive entry, for block i
1000fn bdarent i ulong to ulong
1001r = bdnext
1002if eq r 3
1003ret 3
1004end
1005if ne r 1
1006ret bdrl 29 i
1007end
1008p = bdsp 0
1009if eq bdpre p 106 0
1010ret bdrl 30 i
1011end
1012lab = add p get bdsgl 106
1013if eq bdlnlb lab i 0
1014ret bdrl 31 i
1015end
1016c = bdlc add lab get bdlbl i
1017if and ne c 32 ne c 44
1018ret bdrl 31 i
1019end
1020x = bdfind lab 107
1021if eq x BDNONE
1022ret bdrl 32 i
1023end
1024x = add x get bdsgl 107
1025il = sub get bdidl i 5
1026if eq bdlnid x i il 0
1027ret bdrl 33 i
1028end
1029if ne bdlc add x il 93
1030ret bdrl 33 i
1031end
1032ret 0
1033end
1034note C parse_file: one line after the FUNCTION check (b = current block)
1035fn bdline to ulong
1036if eq bdpre 0 77 1
1037ret bdfunc
1038end
1039n = get bdn 0
1040if eq n 0
1041if or eq bdpre 0 98 1 eq bdpre 0 99 1
1042ret bdrl 47 BDNONE
1043end
1044ret 0
1045end
1046b = sub n 1
1047if eq bdpre 0 84 1
1048v = get bdsgl 84
1049l = sub get bdll 0 v
1050if or eq l 0 ge l 160
1051ret bdrl 48 b
1052end
1053bdsetfm b v l
1054ret 0
1055end
1056if eq bdpre 0 85 1
1057if eq bdeqs get bdsgl 85 86 0
1058ret bdrl 49 b
1059end
1060ret 0
1061end
1062if eq bdpre 0 87 1
1063v = get bdsgl 87
1064if eq bdeqs v 88 1
1065put bdcmp b 1
1066else
1067if eq bdeqs v 89 1
1068put bdcmp b 0
1069else
1070ret bdrl 50 b
1071end
1072end
1073ret 0
1074end
1075ret bdline2 b
1076end
1077note the remaining header comments and the three record kinds
1078fn bdline2 b ulong to ulong
1079if eq bdpre 0 90 1
1080if eq bdeqs get bdsgl 90 89 0
1081ret bdrl 51 b
1082end
1083ret 0
1084end
1085if eq bdpre 0 91 1
1086x = bdu32 get bdsgl 91
1087if lt x 0
1088ret bdrl 52 b
1089end
1090put bdds b ulong x
1091ret 0
1092end
1093if eq bdpre 0 92 1
1094v = get bdsgl 92
1095if eq bdeqs v 93 1
1096put bdcs b 0
1097else
1098x = bdu32 v
1099if lt x 0
1100ret bdrl 53 b
1101end
1102put bdcs b ulong x
1103end
1104ret 0
1105end
1106if eq bdpre 0 94 1
1107ret bdarrhd b
1108end
1109if eq bdeqs 0 96 1
1110if eq get bdhd b 0
1111ret bdrl 59 b
1112end
1113if eq get bdhp b 1
1114ret bdrl 60 b
1115end
1116ret bdpvt b
1117end
1118if eq bdpre 0 97 1
1119if eq and eq get bdhd b 1 eq get bdhp b 1 false
1120ret bdrnd b
1121end
1122ret bdarch
1123end
1124ret 0
1125end
1126note copy bdraw[o..o+n) to infin[0..n), n <= 8 MiB, in chunks below the 1,000,000-pass limit
1127fn bdcpin o ulong n ulong to void
1128p = ulong 0
1129while lt p n bound 64
1130k = sub n p
1131if gt k 524288
1132k = 524288
1133end
1134for j ulong k
1135put infin add p j get bdraw add add o p j
1136end
1137p = add p k
1138end
1139end
1140note copy infout[0..n) to bdpl[o..o+n)
1141fn bdcpout o ulong n ulong to void
1142p = ulong 0
1143while lt p n bound 64
1144k = sub n p
1145if gt k 524288
1146k = 524288
1147end
1148for j ulong k
1149put bdpl add add o p j get infout add p j
1150end
1151p = add p k
1152end
1153end
1154note inflate block i (C unpack_gz_body) into the plain pool
1155fn bdunpk i ulong to ulong
1156ds = get bdds i
1157t = get bdptop 0
1158if gt add t ds BDPOOL
1159ret bdrl 6 i
1160end
1161nb = get bdnb i
1162bdcpin get bdbo i nb
1163e = infgz nb ds
1164if ne e 0
1165infmsg e
1166bdrhd 0
1167for j ulong get infmn 0
1168bdrc get infm j
1169end
1170bdrseg 75
1171bdrlab i
1172bdrseg 76
1173ret 2
1174end
1175bdcpout t ds
1176put bdpo i t
1177put bdpn i ds
1178put bdptop 0 add t ds
1179ret 0
1180end
1181note C parse_file: the whole text, then the end checks and the inflation; 0, 2 refused, 3 read error
1182fn bdparse to ulong
1183put bdn 0 0
1184put bdarc 0 0
1185put bdlno 0 0
1186put bdtp 0 0
1187put bdte 0 0
1188put bdfpos 0 0
1189go = ulong 1
1190while eq go 1 bound 100
1191while eq go 1 bound 999999
1192r = bdnext
1193if eq r 3
1194ret 3
1195end
1196if eq r 2
1197ret 2
1198end
1199if eq r 0
1200go = 0
1201else
1202e = bdline
1203if ne e 0
1204ret e
1205end
1206end
1207end
1208end
1209if eq get bdn 0 0
1210ret bdrl 61 BDNONE
1211end
1212if eq get bdarc 0 0
1213ret bdrl 62 BDNONE
1214end
1215for i ulong get bdn 0
1216if eq and eq get bdhd i 1 eq get bdhp i 1 false
1217ret bdrnd i
1218end
1219if eq get bdcmp i 1
1220e = bdunpk i
1221if ne e 0
1222ret e
1223end
1224end
1225end
1226ret 0
1227end
1228note ---- C check_semantics ----
1229note little-endian u32 word w of block i's plain bytes
1230fn bdw32 i ulong w ulong to ulong
1231p = add get bdpo i mul w 4
1232v = ulong 0
1233for k ulong 4
1234v = or v shl ulong get bdpl add p k mul k 8
1235end
1236ret v
1237end
1238note C region_ok: size != 0, off % 256 == 0, off + size <= image
1239fn bdreg off ulong size ulong img ulong to ulong
1240if eq size 0
1241ret 0
1242end
1243if ne rem off 256 0
1244ret 0
1245end
1246if gt add off size img
1247ret 0
1248end
1249ret 1
1250end
1251note a UCODE_DESC* block
1252fn bddesc i ulong to ulong
1253if ne get bdpn i 84
1254ret bdr0 63 i
1255end
1256if ne bdw32 i 0 5
1257ret bdr0 64 i
1258end
1259if ne bdw32 i 14 1
1260ret bdr0 65 i
1261end
1262note img = "UCODE_IMAGE" + label suffix after "UCODE_DESC", cut to 63 characters (C char img[64])
1263n = ulong 0
1264s = get bdsgb 110
1265for j ulong get bdsgl 110
1266put bdimg n get BDTXT add s j
1267n = add n 1
1268end
1269a = mul i 64
1270j = ulong 10
1271while lt j get bdlbl i bound 64
1272if lt n 63
1273put bdimg n get bdlb add a j
1274n = add n 1
1275end
1276j = add j 1
1277end
1278put bdimgn 0 n
1279ib = bdfimg
1280if eq ib BDNONE
1281ret bdr0 66 i
1282end
1283ret bddesc2 i ib
1284end
1285note the three regions, then the desc line
1286fn bddesc2 i ulong ib ulong to ulong
1287il = get bdpn ib
1288if eq bdreg bdw32 i 8 bdw32 i 9 il 0
1289ret bdr0 67 i
1290end
1291if eq bdreg bdw32 i 10 bdw32 i 11 il 0
1292ret bdr0 68 i
1293end
1294if eq bdreg bdw32 i 12 bdw32 i 13 il 0
1295ret bdr0 69 i
1296end
1297bdos 117
1298bdolab i
1299bdos 118
1300fwodec bdw32 i 0
1301bdos 119
1302fwohex bdw32 i 8
1303bdos 120
1304fwohex bdw32 i 9
1305bdos 121
1306fwohex bdw32 i 10
1307bdos 120
1308fwohex bdw32 i 11
1309bdos 122
1310fwohex bdw32 i 12
1311bdos 120
1312fwohex bdw32 i 13
1313bdos 123
1314fwodec bdw32 i 14
1315bdos 124
1316fwohex bdw32 i 19
1317bdos 125
1318fwodec bdw32 i 20
1319bdos 126
1320fwodec bdw32 i 7
1321bdos 127
1322for j ulong get bdimgn 0
1323fwoc get bdimg j
1324end
1325bdos 128
1326fwodec il
1327fwoc 10
1328ret 0
1329end
1330note SHA-384 of bdpl[o..o+n) into shout[0..48)
1331fn bdsh384 o ulong n ulong to void
1332sh5init
1333p = ulong 0
1334while lt p n bound 64
1335k = sub n p
1336if gt k SHWIN
1337k = SHWIN
1338end
1339for j ulong k
1340put shin j get bdpl add add o p j
1341end
1342sh5upd 0 k
1343p = add p k
1344end
1345sh5fin
1346end
1347note SHA-256 (context 4) of bdpl[o..o+n) into shout[0..32)
1348fn bdsh256 o ulong n ulong to void
1349sh2init 4
1350p = ulong 0
1351while lt p n bound 64
1352k = sub n p
1353if gt k SHWIN
1354k = SHWIN
1355end
1356for j ulong k
1357put shin j get bdpl add add o p j
1358end
1359sh2upd 4 0 k
1360p = add p k
1361end
1362sh2fin 4
1363end
1364note the UCODE_HASH block
1365fn bdhash i ulong to ulong
1366if ne get bdpn i 48
1367ret bdr0 70 BDNONE
1368end
1369ib = bdfseg 110
1370if eq ib BDNONE
1371ret bdr0 71 BDNONE
1372end
1373bdsh384 get bdpo ib get bdpn ib
1374o = get bdpo i
1375for j ulong 48
1376if ne get shout j get bdpl add o j
1377ret bdr0 72 BDNONE
1378end
1379end
1380bdos 129
1381fwoc 10
1382ret 0
1383end
1384fn bdsem to ulong
1385for i ulong get bdn 0
1386e = ulong 0
1387if eq bdlbsg i 109 0 1
1388e = bddesc i
1389else
1390if eq bdlbsg i 111 1 1
1391e = bdhash i
1392else
1393if or eq bdlbsg i 112 1 1 eq bdlbsg i 113 1 1
1394if ne get bdpn i 384
1395bdrhd 0
1396bdrlab i
1397bdrseg 73
1398e = 2
1399end
1400else
1401if eq bdlbsg i 110 0 0
1402e = bdr0 74 i
1403end
1404end
1405end
1406end
1407if ne e 0
1408ret e
1409end
1410end
1411ret 0
1412end
1413note C report without --out: one array line per block
1414fn bdreport to void
1415for i ulong get bdn 0
1416bdos 36
1417bdofn i
1418bdos 140
1419bdolab i
1420bdos 130
1421bdofm i
1422bdos 131
1423fwodec get bdnb i
1424bdos 132
1425if eq get bdcmp i 1
1426bdos 133
1427else
1428bdos 134
1429end
1430bdos 135
1431fwodec get bdpn i
1432bdos 136
1433bdsh256 get bdpo i get bdpn i
1434shhex 32
1435fwohx 32
1436fwoc 10
1437end
1438end
1439note "REFUSED: cannot read PATH: <strerror e>"
1440fn bdcantrd e ulong to ulong
1441bdos 139
1442bdopath
1443fwos 64
1444fwoerrno e
1445fwoc 10
1446ret 2
1447end
1448note C cmd_extract (no --out) for the path in bdpath[0..bdpathn). Prints the C's stdout bytes
1449note through the fw.om writer. Returns 0 = RESULT OK, 2 = REFUSED. bdfsh gets the file's SHA-256
1450note hex, bdfsz its size; the blocks stay in bd* for the caller (bdpl keeps the plain bytes across
1451note calls: the pools are only reset by bdsetup).
1452fn bdfile to ulong
1453bdsetup
1454fd = fopen bdpath 0 get bdpathn 0 524288 0
1455if lt fd 0
1456ret bdcantrd ulong neg fd
1457end
1458put bdfd 0 int fd
1459s = fstat int fd bdst 0 128
1460if lt s 0
1461c = fclose int fd
1462ret bdcantrd ulong neg s
1463end
1464sz = ulong 0
1465for i ulong 8
1466sz = or sz shl ulong get bdst add 48 i mul i 8
1467end
1468md = ulong 0
1469for i ulong 4
1470md = or md shl ulong get bdst add 16 i mul i 8
1471end
1472note S_IFMT 0170000 = 61440, S_IFDIR 0040000 = 16384; 64 MiB = 67108864 (the C's read_file limit)
1473if or eq and md 61440 16384 gt sz 67108864
1474c = fclose int fd
1475ret bdcantrd 27
1476end
1477put bdfsz 0 sz
1478sh2init 3
1479r = sh2file 3 int fd 0 sz
1480if ne r 0
1481c = fclose int fd
1482ret bdcantrd 5
1483end
1484sh2fin 3
1485shhex 32
1486for i ulong 64
1487put bdfsh i get shhx i
1488end
1489ret bdfile2 int fd
1490end
1491fn bdfile2 fd int to ulong
1492e = bdparse
1493c = fclose fd
1494if eq e 3
1495ret bdcantrd 5
1496end
1497if eq e 2
1498bdos 138
1499for i ulong get bdrsn 0
1500fwoc get bdrs i
1501end
1502fwoc 10
1503ret 2
1504end
1505bdos 115
1506for i ulong get bdfnl 0
1507fwoc get bdfn i
1508end
1509bdos 116
1510fwodec get bdn 0
1511fwoc 10
1512e = bdsem
1513if eq e 2
1514bdos 138
1515for i ulong get bdrsn 0
1516fwoc get bdrs i
1517end
1518fwoc 10
1519ret 2
1520end
1521bdreport
1522bdos 137
1523fwodec get bdn 0
1524fwoc 10
1525ret 0
1526end