code wiki / _hdl_build / nx_uefi_ovmf_oracle.nx
nx_uefi_ovmf_oracle.nx source
↩ module page · 428 lines · 18008 B
1// nx_uefi_ovmf_oracle.nx -- BENCH-ONLY ORACLE (operator law 2026-07-20).
2//
3// THE THIRD REFERENCE FOR THE UEFI LANE. nx_boot_uefi AUTHORS the EFI image and nx_emu_uefi
4// EXECUTES it -- but both are OURS, so a GREEN from that pair proves the pair is self-consistent,
5// never that real firmware agrees. ("A TWO-WAY COMPARISON ATTRIBUTES THE INCUMBENT'S ERROR TO
6// YOU -- BUILD THE THIRD REFERENCE ONCE AND REUSE IT AT EVERY SCALE.") The third reference is
7// EDK2/OVMF: an independent UEFI implementation nobody here wrote.
8//
9// This organ does NOT run qemu (qemu is a foreign binary and lives only on the laptop; the NAS
10// has neither qemu nor OVMF -- measured 2026-08-08). It is the RULER: it takes the framebuffer
11// real firmware actually produced (a qemu `screendump` P6 PPM) and the framebuffer the sovereign
12// emulator modelled (`nx_emu_uefi --shot` 24-bit BMP) and compares them PIXEL FOR PIXEL, plus
13// re-derives nx_emu_uefi's own fb_measure statistics over the FOREIGN buffer so both engines are
14// judged by ONE ruler on ONE scale.
15//
16// Orientation/channel order are NOT assumed: PPM is top-down RGB, BMP is bottom-up BGR, and the
17// transform is applied explicitly. Geometry comes FROM each file's own header, never from a
18// constant -- a comparator that trusts the writer's constants is one implementation, not two.
19//
20// Usage: nx_uefi_ovmf_oracle <ovmf.ppm> <sov.bmp>
21// nx_uefi_ovmf_oracle selftest (teeth, incl. a planted-difference negative control)
22// Exit: 0 GREEN (dims agree AND zero differing pixels) | 1 RED | 3 UNPROVEN (could not read)
23// -- the third state exists so "I could not look" never reports as "it is broken".
24// Log -> knowledge/status/nishi_os.log, canonical verdict= on the LAST field (positional anchor).
25// Sovereign: syscalls only, no gcc/.sh. license_tier: ORIGINAL
26import "nx_syscalls.nx"
27const OVO_MAGIC_4096: i64 = 4096
28
29const OVO_MAXW: i64 = 8192
30const OVO_MAXH: i64 = 8192
31
32// out slots
33const O_W: i64 = 0
34const O_H: i64 = 1
35const O_TOTAL: i64 = 2
36const O_EQUAL: i64 = 3
37const O_DIFF: i64 = 4
38const O_FDX: i64 = 5
39const O_FDY: i64 = 6
40const O_COLOR: i64 = 7
41const O_MATCH: i64 = 8
42const O_ZEROS: i64 = 9
43const O_TRANS: i64 = 10
44
45func o_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
46func o_fp(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
47func o_fn(fd: i64, v: i64) -> i64 {
48 let bb: *u8 = sys_mmap(28); var m: i64 = v
49 if m < 0 { m = 0 - m; bb[0] = 45 as u8; sys_write(fd, bb, 1) }
50 let t: *u8 = sys_mmap(28); var k: i64 = 0
51 if m == 0 { t[0] = 48 as u8; k = 1 }
52 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
53 var i: i64 = 0
54 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
55 sys_write(fd, bb, k); return 0
56}
57func o_fx(fd: i64, v: i64) -> i64 {
58 let hx: *u8 = "0123456789ABCDEF" as *u8 // hoisted: indexing an inline-cast literal never matches
59 let bb: *u8 = sys_mmap(20)
60 var i: i64 = 0
61 while i < 8 { bb[i] = hx[(v >> ((7 - i) * 4)) & 0xf]; i = i + 1 }
62 sys_write(fd, bb, 8); return 0
63}
64func o_eq(a: *u8, b: *u8) -> i64 {
65 var i: i64 = 0
66 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
67 if b[i] != (0 as u8) { return 0 }
68 return 1
69}
70func rd32(b: *u8, o: i64) -> i64 {
71 return (b[o] as i64) | ((b[o+1] as i64) << 8) | ((b[o+2] as i64) << 16) | ((b[o+3] as i64) << 24)
72}
73
74// ---- P6 PPM header: "P6" ws [#comment] w ws h ws maxval <one ws> data ----------------------
75// ip[0] carries the cursor in and out. A SEPARATE `done` flag ends each scan: a loop that exits
76// by clobbering its own cursor destroys the position it was searching for.
77func ppm_ws(b: *u8, len: i64, ip: *i64) -> i64 {
78 var i: i64 = ip[0]
79 var done: i64 = 0
80 while done == 0 {
81 if i >= len { done = 1 }
82 else {
83 let c: i64 = b[i] as i64
84 if c == 35 { // '#' comment to end of line
85 var j: i64 = i
86 var eol: i64 = 0
87 while eol == 0 {
88 if j >= len { eol = 1 } else { if (b[j] as i64) == 10 { eol = 1 } else { j = j + 1 } }
89 }
90 i = j
91 } else {
92 if c == 32 { i = i + 1 } else {
93 if c == 9 { i = i + 1 } else {
94 if c == 10 { i = i + 1 } else {
95 if c == 13 { i = i + 1 } else { done = 1 } } } }
96 }
97 }
98 }
99 ip[0] = i
100 return 0
101}
102func ppm_int(b: *u8, len: i64, ip: *i64) -> i64 {
103 var i: i64 = ip[0]
104 var v: i64 = 0
105 var n: i64 = 0
106 var done: i64 = 0
107 while done == 0 {
108 if i >= len { done = 1 }
109 else {
110 let d: i64 = (b[i] as i64) - 48
111 if d < 0 { done = 1 } else { if d > 9 { done = 1 } else {
112 v = v * 10 + d; n = n + 1; i = i + 1
113 } }
114 }
115 }
116 ip[0] = i
117 if n == 0 { return 0 - 1 }
118 return v
119}
120
121// ---- the comparison. Returns 0 ok, negative = a NAMED refusal (never a silent zero) --------
122// -1 ppm magic -2 ppm header -3 ppm truncated -4 bmp magic -5 bmp bpp
123// -6 bmp truncated -7 dimension mismatch -8 insane geometry
124func fb_compare(pp: *u8, plen: i64, bm: *u8, blen: i64, out: *i64) -> i64 {
125 if plen < 16 { return 0 - 1 }
126 if (pp[0] as i64) != 80 { return 0 - 1 } // 'P'
127 if (pp[1] as i64) != 54 { return 0 - 1 } // '6'
128 let ip: *i64 = sys_mmap(16) as *i64
129 ip[0] = 2
130 ppm_ws(pp, plen, ip)
131 let pw: i64 = ppm_int(pp, plen, ip)
132 ppm_ws(pp, plen, ip)
133 let ph: i64 = ppm_int(pp, plen, ip)
134 ppm_ws(pp, plen, ip)
135 let pmax: i64 = ppm_int(pp, plen, ip)
136 if pw < 0 { return 0 - 2 }
137 if ph < 0 { return 0 - 2 }
138 if pmax != 255 { return 0 - 2 }
139 let pdata: i64 = ip[0] + 1 // exactly one whitespace byte precedes data
140 if pw <= 0 { return 0 - 8 }
141 if ph <= 0 { return 0 - 8 }
142 if pw > OVO_MAXW { return 0 - 8 }
143 if ph > OVO_MAXH { return 0 - 8 }
144 if (pdata + pw * ph * 3) > plen { return 0 - 3 }
145
146 if blen < 54 { return 0 - 4 }
147 if (bm[0] as i64) != 66 { return 0 - 4 } // 'B'
148 if (bm[1] as i64) != 77 { return 0 - 4 } // 'M'
149 let bdata: i64 = rd32(bm, 10)
150 let bw: i64 = rd32(bm, 18)
151 let bh: i64 = rd32(bm, 22)
152 let bpp: i64 = (bm[28] as i64) | ((bm[29] as i64) << 8)
153 if bpp != 24 { return 0 - 5 }
154 if bw != pw { return 0 - 7 }
155 if bh != ph { return 0 - 7 }
156 let rowb: i64 = bw * 3
157 var pad: i64 = 0
158 while ((rowb + pad) % 4) != 0 { pad = pad + 1 }
159 let stride: i64 = rowb + pad
160 if (bdata + stride * bh) > blen { return 0 - 6 }
161
162 var equal: i64 = 0
163 var diff: i64 = 0
164 var fdx: i64 = 0 - 1
165 var fdy: i64 = 0 - 1
166 // fb_measure over the FOREIGN buffer, in the SAME BGRX dword form and the SAME memory order
167 // (top-down) nx_emu_uefi uses, so the two stat vectors are comparable rather than merely alike.
168 var color: i64 = 0
169 var matches: i64 = 0
170 var zeros: i64 = 0
171 var trans: i64 = 0
172 var prev: i64 = 0
173 var first: i64 = 1
174
175 var y: i64 = 0
176 while y < ph {
177 let prow: i64 = pdata + y * pw * 3
178 let brow: i64 = bdata + (bh - 1 - y) * stride // BMP rows are bottom-up
179 var x: i64 = 0
180 while x < pw {
181 let pr: i64 = pp[prow + x * 3] as i64
182 let pg: i64 = pp[prow + x * 3 + 1] as i64
183 let pb: i64 = pp[prow + x * 3 + 2] as i64
184 let bb2: i64 = bm[brow + x * 3] as i64
185 let bg: i64 = bm[brow + x * 3 + 1] as i64
186 let br: i64 = bm[brow + x * 3 + 2] as i64
187 var same: i64 = 1
188 if pr != br { same = 0 }
189 if pg != bg { same = 0 }
190 if pb != bb2 { same = 0 }
191 if same == 1 { equal = equal + 1 } else {
192 diff = diff + 1
193 if fdx < 0 { fdx = x; fdy = y }
194 }
195 let dw: i64 = pb | (pg << 8) | (pr << 16) // B | G<<8 | R<<16 == the GOP BGRX dword
196 if first == 1 { color = dw; prev = dw; first = 0 }
197 if dw == color { matches = matches + 1 }
198 if dw == 0 { zeros = zeros + 1 }
199 if dw != prev { trans = trans + 1 }
200 prev = dw
201 x = x + 1
202 }
203 y = y + 1
204 }
205
206 out[O_W] = pw; out[O_H] = ph; out[O_TOTAL] = pw * ph
207 out[O_EQUAL] = equal; out[O_DIFF] = diff
208 out[O_FDX] = fdx; out[O_FDY] = fdy
209 out[O_COLOR] = color; out[O_MATCH] = matches; out[O_ZEROS] = zeros; out[O_TRANS] = trans
210 return 0
211}
212
213// ---- fixtures: build a matched PPM/BMP pair in memory, then plant a single-pixel difference --
214// The negative control is the whole point: a comparator that has only ever seen identical inputs
215// has not been shown to fire.
216func mk_ppm(b: *u8, w: i64, h: i64) -> i64 {
217 let hd: *u8 = "P6\n4 3\n255\n" as *u8 // fixture geometry is fixed at 4x3
218 var i: i64 = 0
219 while hd[i] != (0 as u8) { b[i] = hd[i]; i = i + 1 }
220 let d: i64 = i
221 var k: i64 = 0
222 while k < (w * h) {
223 b[d + k * 3] = (10 + k) as u8 // R
224 b[d + k * 3 + 1] = (20 + k * 2) as u8 // G
225 b[d + k * 3 + 2] = (30 + k * 3) as u8 // B
226 k = k + 1
227 }
228 return d + w * h * 3
229}
230func mk_bmp(b: *u8, w: i64, h: i64) -> i64 {
231 let rowb: i64 = w * 3
232 var pad: i64 = 0
233 while ((rowb + pad) % 4) != 0 { pad = pad + 1 }
234 let stride: i64 = rowb + pad
235 let total: i64 = 54 + stride * h
236 var z: i64 = 0
237 while z < total { b[z] = 0 as u8; z = z + 1 }
238 b[0] = 66 as u8; b[1] = 77 as u8
239 b[10] = 54 as u8
240 b[14] = 40 as u8
241 b[18] = (w & 0xff) as u8; b[19] = ((w >> 8) & 0xff) as u8
242 b[22] = (h & 0xff) as u8; b[23] = ((h >> 8) & 0xff) as u8
243 b[26] = 1 as u8; b[28] = 24 as u8
244 var y: i64 = 0
245 while y < h {
246 let row: i64 = 54 + (h - 1 - y) * stride
247 var x: i64 = 0
248 while x < w {
249 let k: i64 = y * w + x
250 b[row + x * 3] = (30 + k * 3) as u8 // B
251 b[row + x * 3 + 1] = (20 + k * 2) as u8 // G
252 b[row + x * 3 + 2] = (10 + k) as u8 // R
253 x = x + 1
254 }
255 y = y + 1
256 }
257 return total
258}
259
260func ovo_selftest() -> i64 {
261 let out: *i64 = sys_mmap(8 * 16) as *i64
262 var pass: i64 = 0
263 var teeth: i64 = 0
264 let W: i64 = 4
265 let H: i64 = 3
266
267 let pp: *u8 = sys_mmap(OVO_MAGIC_4096)
268 let bm: *u8 = sys_mmap(OVO_MAGIC_4096)
269 let plen: i64 = mk_ppm(pp, W, H)
270 let blen: i64 = mk_bmp(bm, W, H)
271
272 // T1 POSITIVE CONTROL: a matched pair must report zero differing pixels over the FULL count.
273 // Bound the assertion to its denominator -- a tooth that passes on the empty set is not a tooth.
274 teeth = teeth + 1
275 var r: i64 = fb_compare(pp, plen, bm, blen, out)
276 var ok: i64 = 0
277 if r == 0 { if out[O_TOTAL] == (W * H) { if out[O_DIFF] == 0 { if out[O_EQUAL] == (W * H) { ok = 1 } } } }
278 if ok == 1 { pass = pass + 1; o_p("OVO-T1 matched-pair-zero-diff GREEN\n" as *u8) }
279 else { o_p("OVO-T1 RED\n" as *u8) }
280
281 // T2 NEGATIVE CONTROL: plant ONE pixel difference at a KNOWN coordinate and demand the
282 // comparator name that exact coordinate. Asserting only "diff>0" would pass for a
283 // comparator that reports every pixel as different.
284 teeth = teeth + 1
285 let tx: i64 = 2
286 let ty: i64 = 1
287 let rowb2: i64 = W * 3
288 var pad2: i64 = 0
289 while ((rowb2 + pad2) % 4) != 0 { pad2 = pad2 + 1 }
290 let stride2: i64 = rowb2 + pad2
291 let poff: i64 = 54 + (H - 1 - ty) * stride2 + tx * 3
292 let before: i64 = bm[poff] as i64
293 bm[poff] = (before ^ 0xff) as u8 // flip the BLUE channel of one pixel
294 var fixture_moved: i64 = 0
295 if (bm[poff] as i64) != before { fixture_moved = 1 } // assert the fixture REACHED the condition
296 r = fb_compare(pp, plen, bm, blen, out)
297 ok = 0
298 if fixture_moved == 1 { if r == 0 {
299 if out[O_DIFF] == 1 { if out[O_FDX] == tx { if out[O_FDY] == ty { ok = 1 } } }
300 } }
301 if ok == 1 { pass = pass + 1; o_p("OVO-T2 neg-control-planted-pixel-located GREEN\n" as *u8) }
302 else {
303 o_p("OVO-T2 RED fixture_moved=" as *u8); o_fn(1, fixture_moved)
304 o_p(" rc=" as *u8); o_fn(1, r)
305 o_p(" diff=" as *u8); o_fn(1, out[O_DIFF])
306 o_p(" fdx=" as *u8); o_fn(1, out[O_FDX])
307 o_p(" fdy=" as *u8); o_fn(1, out[O_FDY]); o_p("\n" as *u8)
308 }
309 bm[poff] = before as u8 // restore: the restore is part of the experiment
310
311 // T3 a dimension mismatch must be REFUSED by name, never compared to a truncated overlap.
312 teeth = teeth + 1
313 bm[18] = 8 as u8
314 r = fb_compare(pp, plen, bm, blen, out)
315 if r == (0 - 7) { pass = pass + 1; o_p("OVO-T3 dim-mismatch-refused GREEN\n" as *u8) }
316 else { o_p("OVO-T3 RED rc=" as *u8); o_fn(1, r); o_p("\n" as *u8) }
317 bm[18] = (W & 0xff) as u8
318
319 // T4 a non-PPM input must be refused by name rather than parsed into confident nonsense.
320 teeth = teeth + 1
321 let sv: i64 = pp[1] as i64
322 pp[1] = 51 as u8 // "P3" -- ASCII PPM, not P6
323 r = fb_compare(pp, plen, bm, blen, out)
324 if r == (0 - 1) { pass = pass + 1; o_p("OVO-T4 non-p6-refused GREEN\n" as *u8) }
325 else { o_p("OVO-T4 RED rc=" as *u8); o_fn(1, r); o_p("\n" as *u8) }
326 pp[1] = sv as u8
327
328 // T5 the foreign-side stat vector must be REAL, not a constant wearing the shape of a
329 // measurement: the fixture is a gradient, so transitions must exceed zero.
330 teeth = teeth + 1
331 r = fb_compare(pp, plen, bm, blen, out)
332 ok = 0
333 if r == 0 { if out[O_TRANS] > 0 { if out[O_ZEROS] == 0 { ok = 1 } } }
334 if ok == 1 { pass = pass + 1; o_p("OVO-T5 stat-vector-non-constant GREEN\n" as *u8) }
335 else { o_p("OVO-T5 RED trans=" as *u8); o_fn(1, out[O_TRANS]); o_p("\n" as *u8) }
336
337 o_p("OVO-SELFTEST " as *u8); o_fn(1, pass); o_p("/" as *u8); o_fn(1, teeth); o_p("\n" as *u8)
338 let lf: i64 = sys_openat_append("knowledge/status/nishi_os.log" as *u8, 0x1a4)
339 if lf >= 0 {
340 o_fp(lf, "NOSOVMF selftest teeth=" as *u8); o_fn(lf, pass)
341 o_fp(lf, "of" as *u8); o_fn(lf, teeth)
342 o_fp(lf, " verdict=" as *u8)
343 if pass == teeth { o_fp(lf, "GREEN\n" as *u8) } else { o_fp(lf, "RED\n" as *u8) }
344 sys_close(lf)
345 }
346 if pass == teeth { sys_exit(0); return 0 }
347 sys_exit(1); return 1
348}
349
350func main(argc: i64, argv: *i64) -> i64 {
351 if argc >= 2 { if o_eq(argv[1] as *u8, "selftest" as *u8) == 1 { return ovo_selftest() } }
352 if argc < 3 {
353 o_p("usage: nx_uefi_ovmf_oracle <ovmf.ppm> <sov.bmp> | nx_uefi_ovmf_oracle selftest\n" as *u8)
354 sys_exit(3); return 3
355 }
356 let ppath: *u8 = argv[1] as *u8
357 let bpath: *u8 = argv[2] as *u8
358
359 let pl: *i64 = sys_mmap(16) as *i64
360 let pp: *u8 = sys_read_file(ppath, pl)
361 if pl[0] <= 0 {
362 o_p("NOS-OVMF UNPROVEN: cannot read " as *u8); o_p(ppath); o_p("\n" as *u8)
363 sys_exit(3); return 3
364 }
365 let bl: *i64 = sys_mmap(16) as *i64
366 let bm: *u8 = sys_read_file(bpath, bl)
367 if bl[0] <= 0 {
368 o_p("NOS-OVMF UNPROVEN: cannot read " as *u8); o_p(bpath); o_p("\n" as *u8)
369 sys_exit(3); return 3
370 }
371
372 let out: *i64 = sys_mmap(8 * 16) as *i64
373 let r: i64 = fb_compare(pp, pl[0], bm, bl[0], out)
374 if r < 0 {
375 o_p("NOS-OVMF RED: refused, code " as *u8); o_fn(1, r); o_p("\n" as *u8)
376 let lf2: i64 = sys_openat_append("knowledge/status/nishi_os.log" as *u8, 0x1a4)
377 if lf2 >= 0 {
378 o_fp(lf2, "NOSOVMF ovmf=" as *u8); o_fp(lf2, ppath)
379 o_fp(lf2, " sov=" as *u8); o_fp(lf2, bpath)
380 o_fp(lf2, " refused_code=" as *u8); o_fn(lf2, r)
381 o_fp(lf2, " verdict=RED\n" as *u8)
382 sys_close(lf2)
383 }
384 sys_exit(1); return 1
385 }
386
387 var permil: i64 = 0
388 if out[O_TOTAL] > 0 { permil = (out[O_EQUAL] * 1000) / out[O_TOTAL] }
389 var green: i64 = 0
390 if out[O_DIFF] == 0 { green = 1 }
391
392 o_p("NOS-OVMF " as *u8); o_fn(1, out[O_W]); o_p("x" as *u8); o_fn(1, out[O_H])
393 o_p(" px_total=" as *u8); o_fn(1, out[O_TOTAL])
394 o_p(" px_equal=" as *u8); o_fn(1, out[O_EQUAL])
395 o_p(" px_diff=" as *u8); o_fn(1, out[O_DIFF])
396 o_p(" agree_permil=" as *u8); o_fn(1, permil)
397 o_p(" first_diff_x=" as *u8); o_fn(1, out[O_FDX])
398 o_p(" first_diff_y=" as *u8); o_fn(1, out[O_FDY])
399 o_p(" ovmf_color=0x" as *u8); o_fx(1, out[O_COLOR])
400 o_p(" ovmf_match=" as *u8); o_fn(1, out[O_MATCH])
401 o_p(" ovmf_zeros=" as *u8); o_fn(1, out[O_ZEROS])
402 o_p(" ovmf_trans=" as *u8); o_fn(1, out[O_TRANS])
403 o_p("\n" as *u8)
404
405 let lf: i64 = sys_openat_append("knowledge/status/nishi_os.log" as *u8, 0x1a4)
406 if lf >= 0 {
407 o_fp(lf, "NOSOVMF ovmf=" as *u8); o_fp(lf, ppath)
408 o_fp(lf, " sov=" as *u8); o_fp(lf, bpath)
409 o_fp(lf, " w=" as *u8); o_fn(lf, out[O_W])
410 o_fp(lf, " h=" as *u8); o_fn(lf, out[O_H])
411 o_fp(lf, " px_total=" as *u8); o_fn(lf, out[O_TOTAL])
412 o_fp(lf, " px_equal=" as *u8); o_fn(lf, out[O_EQUAL])
413 o_fp(lf, " px_diff=" as *u8); o_fn(lf, out[O_DIFF])
414 o_fp(lf, " agree_permil=" as *u8); o_fn(lf, permil)
415 o_fp(lf, " ovmf_color=0x" as *u8); o_fx(lf, out[O_COLOR])
416 o_fp(lf, " ovmf_zeros=" as *u8); o_fn(lf, out[O_ZEROS])
417 o_fp(lf, " ovmf_trans=" as *u8); o_fn(lf, out[O_TRANS])
418 o_fp(lf, " engines=edk2-ovmf-vs-nx_emu_uefi verdict=" as *u8)
419 if green == 1 { o_fp(lf, "GREEN\n" as *u8) } else { o_fp(lf, "RED\n" as *u8) }
420 sys_close(lf)
421 }
422 if green == 1 {
423 o_p("NOS-OVMF GREEN: the sovereign emulator reproduces real EDK2 firmware PIXEL FOR PIXEL\n" as *u8)
424 sys_exit(0); return 0
425 }
426 o_p("NOS-OVMF RED: sovereign and real firmware DISAGREE on the framebuffer\n" as *u8)
427 sys_exit(1); return 1
428}