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 / aienux / disk / tests / run_slice_d.sh

353 lines · 13239 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Host verification suite for Slice D (Piece 4: RO NVMe + GPT + ext4 + SHA-256 + RTL8127/TCP + a64efi).
4# Strictly host-only (NOT_RUN on Spark silicon). Zero real firmware bytes.
5set -eu
6
7ROOT=$(CDPATH= cd -- "$(dirname "$0")/../../.." && pwd)
8OMC="${OMC:-${OMEGA_MACOS_ROOT:-$HOME/workspace/omega-mv-macos}/seed/out/omc-macos}"
9if [ ! -x "$OMC" ]; then
10 OMC="$ROOT/seed/out/omc"
11fi
12
13TMP=$(mktemp -d "${TMPDIR:-/tmp}/slice-d-test.XXXXXX")
14trap 'rm -rf "$TMP"' EXIT
15
16OS=$(uname -s)
17if [ "$OS" = "Darwin" ]; then
18 HOST_HDR="target a64macos"
19 sign_bin() { codesign -s - "$1" >/dev/null 2>&1; }
20else
21 HOST_HDR=""
22 sign_bin() { :; }
23fi
24
25compile_host() {
26 out="$1"; shift
27 if [ -n "$HOST_HDR" ]; then
28 { printf '%s\n' "$HOST_HDR"; cat "$@"; } | "$OMC" > "$out"
29 else
30 cat "$@" | "$OMC" > "$out"
31 fi
32 chmod +x "$out"
33 sign_bin "$out"
34}
35
36compile_linux() {
37 out="$1"; shift
38 cat "$@" | "$OMC" > "$out"
39}
40
41echo "=== [1/7] NVMe Read-Only Bare-Metal Simulator Test ==="
42compile_host "$TMP/test_nvme_ro_bare" \
43 "$ROOT/aienux/disk/nvme_ro_bare.om" \
44 "$ROOT/aienux/disk/tests/test_nvme_ro_bare.om"
45"$TMP/test_nvme_ro_bare"
46compile_linux "$TMP/test_nvme_ro_bare.elf" \
47 "$ROOT/aienux/disk/nvme_ro_bare.om" \
48 "$ROOT/aienux/disk/tests/test_nvme_ro_bare.om"
49echo "PASS: test_nvme_ro_bare (host run + Linux AArch64 ELF compile)"
50
51echo "=== [2/7] Offline OMFWBND1 Bundle Packer (Synthetic Blobs Only) ==="
52compile_host "$TMP/pack_bundle" \
53 "$ROOT/aienux/disk/sha256_bare.om" \
54 "$ROOT/aienux/disk/pack_bundle.om"
55compile_linux "$TMP/pack_bundle.elf" \
56 "$ROOT/aienux/disk/sha256_bare.om" \
57 "$ROOT/aienux/disk/pack_bundle.om"
58python3 - "$TMP" <<'PY'
59import os, sys
60tmp = sys.argv[1]
61blobs = [
62 ("gsp.bin", b"SYNTH_GSP_ELF64" * 300),
63 ("bl.img", b"SYNTH_BL_IMG" * 120),
64 ("fmc.img", b"SYNTH_FMC_IMG" * 120),
65 ("fmc.hash", bytes(range(48))),
66 ("fmc.pubkey", bytes([(i * 7) & 0xff for i in range(384)])),
67 ("fmc.sig", bytes([(i * 13) & 0xff for i in range(384)])),
68 ("bl_desc", bytes([(i * 3) & 0xff for i in range(84)])),
69]
70for name, data in blobs:
71 with open(os.path.join(tmp, name), "wb") as f:
72 f.write(data)
73PY
74"$TMP/pack_bundle" "$TMP/spark-bundle.img" 58017302 \
75 1 "$TMP/gsp.bin" \
76 2 "$TMP/bl.img" \
77 3 "$TMP/fmc.img" \
78 4 "$TMP/fmc.hash" \
79 5 "$TMP/fmc.pubkey" \
80 6 "$TMP/fmc.sig" \
81 7 "$TMP/bl_desc"
82# Regression: pack_bundle MUST reject u64 overflow (> 2^64-1) and >20-digit args with exit 2
83set +e
84"$TMP/pack_bundle" "$TMP/bad1.img" 18446744073709551616 1 "$TMP/gsp.bin"
85rc1=$?
86"$TMP/pack_bundle" "$TMP/bad2.img" 123456789012345678901 1 "$TMP/gsp.bin"
87rc2=$?
88set -e
89if [ "$rc1" -ne 2 ] || [ "$rc2" -ne 2 ]; then
90 echo "FAIL: pack_bundle bad-arg rejection (rc1=$rc1 rc2=$rc2)" >&2
91 exit 1
92fi
93echo "PASS: pack_bundle produced $(wc -c < "$TMP/spark-bundle.img") byte synthetic OMFWBND1 bundle + overflow/length rejection"
94
95echo "=== [3/7] Synthetic GPT + 64-bit ext4 Image & Read-Only Test ==="
96python3 - "$TMP/spark-bundle.img" "$TMP/disk.img" <<'PY'
97import struct, sys, zlib
98
99bundle = open(sys.argv[1], "rb").read()
100total_secs = 4096
101img = bytearray(total_secs * 512)
102
103# 1. Protective MBR (LBA 0)
104img[446 + 4] = 0xEE
105struct.pack_into("<II", img, 446 + 8, 1, total_secs - 1)
106img[510] = 0x55
107img[511] = 0xAA
108
109# 2. GPT Partition Entries (LBA 2..33, 128 entries x 128 B = 16384 B)
110ents = bytearray(128 * 128)
111# Partition 1: EFI System (LBA 34..63)
112efi_guid = bytes.fromhex("28732ac11ff8d211ba4b00a0c93ec93b")
113struct.pack_into("<16s16sQQQ", ents, 0, efi_guid, b"\x01"*16, 34, 63, 0)
114# Partition 2: Linux Filesystem (LBA 64..4062, 3999 sectors -> 499 x 4KiB blocks)
115linux_guid = bytes.fromhex("af3dc60f838472478e793d69d8477de4")
116part2_lba = 64
117part2_last = total_secs - 34
118struct.pack_into("<16s16sQQQ", ents, 128, linux_guid, b"\x02"*16, part2_lba, part2_last, 0)
119ent_crc = zlib.crc32(ents) & 0xffffffff
120
121def make_gpt_hdr(my_lba, bak_lba, ent_lba):
122 hdr = bytearray(92)
123 struct.pack_into("<8sIIIIQQQQ16sQIII", hdr, 0,
124 b"EFI PART", 0x00010000, 92, 0, 0,
125 my_lba, bak_lba, 34, total_secs - 34,
126 b"\x11"*16, ent_lba, 128, 128, ent_crc)
127 hcrc = zlib.crc32(hdr) & 0xffffffff
128 struct.pack_into("<I", hdr, 16, hcrc)
129 return hdr
130
131img[512:512+92] = make_gpt_hdr(1, total_secs - 1, 2)
132img[1024:1024+len(ents)] = ents
133img[(total_secs - 1)*512:(total_secs - 1)*512+92] = make_gpt_hdr(total_secs - 1, 1, total_secs - 33)
134
135# 3. Synthetic 64-bit ext4 inside Partition 2 (4 KiB blocks, desc_size=64, 256B inodes)
136p2_off = part2_lba * 512
137BS = 4096
138def blk_off(b): return p2_off + b * BS
139
140# Superblock at p2_off + 1024
141sb = p2_off + 1024
142n_blocks = (part2_last - part2_lba + 1) // 8
143struct.pack_into("<I", img, sb + 0, 128) # s_inodes_count
144struct.pack_into("<I", img, sb + 4, n_blocks) # s_blocks_count_lo
145struct.pack_into("<I", img, sb + 24, 2) # s_log_block_size = 2 (4096 B)
146struct.pack_into("<I", img, sb + 32, 32768) # s_blocks_per_group
147struct.pack_into("<I", img, sb + 40, 128) # s_inodes_per_group
148struct.pack_into("<H", img, sb + 56, 0xEF53) # s_magic
149struct.pack_into("<H", img, sb + 88, 256) # s_inode_size
150struct.pack_into("<I", img, sb + 96, 0x40|0x80) # INCOMPAT_EXTENTS | INCOMPAT_64BIT
151struct.pack_into("<H", img, sb + 254, 64) # s_desc_size = 64
152struct.pack_into("<I", img, sb + 336, 0) # s_blocks_count_hi
153
154# Block 1: 64-bit Group Descriptor Table (inode table at block 2)
155gdt = blk_off(1)
156struct.pack_into("<I", img, gdt + 8, 2) # bg_inode_table_lo = 2
157struct.pack_into("<I", img, gdt + 40, 0) # bg_inode_table_hi = 0
158
159# Block 2..9: Inode table (128 inodes * 256 B = 32 KiB = 8 blocks)
160def write_inode(ino, mode, size, flags, ext_bytes):
161 ioff = blk_off(2) + (ino - 1) * 256
162 struct.pack_into("<H", img, ioff + 0, mode)
163 struct.pack_into("<I", img, ioff + 4, size & 0xffffffff)
164 struct.pack_into("<I", img, ioff + 32, flags)
165 struct.pack_into("<I", img, ioff + 108, (size >> 32) & 0xffffffff)
166 img[ioff + 40 : ioff + 40 + len(ext_bytes)] = ext_bytes
167
168def ext_hdr(entries, max_e, depth, magic=0xF30A):
169 return struct.pack("<HHHHI", magic, entries, max_e, depth, 0)
170
171def ext_leaf(ee_blk, ee_len, pblk):
172 return struct.pack("<IHHI", ee_blk, ee_len, (pblk >> 32) & 0xffff, pblk & 0xffffffff)
173
174def ext_idx(ei_blk, leaf_blk):
175 return struct.pack("<IIHH", ei_blk, leaf_blk & 0xffffffff, (leaf_blk >> 32) & 0xffff, 0)
176
177def make_dir_block(entries):
178 buf = bytearray(BS)
179 pos = 0
180 for idx, (ino, name, ftype) in enumerate(entries):
181 nb = name.encode("ascii")
182 raw = 8 + len(nb)
183 rlen = (raw + 3) & ~3
184 if idx == len(entries) - 1:
185 rlen = BS - pos
186 struct.pack_into("<IHBB", buf, pos, ino, rlen, len(nb), ftype)
187 buf[pos + 8 : pos + 8 + len(nb)] = nb
188 pos += rlen
189 return buf
190
191# Directories: root (ino 2 -> blk 10), /aienos (ino 12 -> blk 11), /bad (ino 13 -> blk 12)
192img[blk_off(10):blk_off(11)] = make_dir_block([(2, ".", 2), (2, "..", 2), (12, "aienos", 2), (13, "bad", 2)])
193write_inode(2, 0x41ED, BS, 0x80000, ext_hdr(1, 4, 0) + ext_leaf(0, 1, 10))
194
195img[blk_off(11):blk_off(12)] = make_dir_block([(12, ".", 2), (2, "..", 2), (14, "sparse.bin", 1), (15, "spark-bundle.img", 1)])
196write_inode(12, 0x41ED, BS, 0x80000, ext_hdr(1, 4, 0) + ext_leaf(0, 1, 11))
197
198img[blk_off(12):blk_off(13)] = make_dir_block([
199 (13, ".", 2), (2, "..", 2),
200 (16, "inline", 1), (17, "encrypt", 1), (18, "noext", 1), (19, "badmagic", 1)
201])
202write_inode(13, 0x41ED, BS, 0x80000, ext_hdr(1, 4, 0) + ext_leaf(0, 1, 12))
203
204# /aienos/sparse.bin (ino 14, 16384 B, DEPTH-1 extent tree! index -> leaf block 13):
205# lblock 0 -> blk 14 (0xA5)
206# lblock 1 -> blk 15 (unwritten ee_len = 32768 + 1, disk has 0xEE garbage -> MUST read as 0x00!)
207# lblock 2 -> sparse hole (no extent -> MUST read as 0x00!)
208# lblock 3 -> blk 16 (0x5A)
209img[blk_off(14):blk_off(15)] = bytes([0xA5]) * BS
210img[blk_off(15):blk_off(16)] = bytes([0xEE]) * BS
211img[blk_off(16):blk_off(17)] = bytes([0x5A]) * BS
212leaf13 = ext_hdr(3, 340, 0) + ext_leaf(0, 1, 14) + ext_leaf(1, 32769, 15) + ext_leaf(3, 1, 16)
213img[blk_off(13):blk_off(13)+len(leaf13)] = leaf13
214write_inode(14, 0x81A4, 16384, 0x80000, ext_hdr(1, 4, 1) + ext_idx(0, 13))
215
216# /aienos/spark-bundle.img (ino 15, blocks 20..)
217bnd_blks = (len(bundle) + BS - 1) // BS
218img[blk_off(20):blk_off(20)+len(bundle)] = bundle
219write_inode(15, 0x81A4, len(bundle), 0x80000, ext_hdr(1, 4, 0) + ext_leaf(0, bnd_blks, 20))
220
221# Hostile inodes in /bad/
222write_inode(16, 0x81A4, 16, 0x80000 | 0x10000000, ext_hdr(0, 4, 0)) # inline
223write_inode(17, 0x81A4, 16, 0x80000 | 0x800, ext_hdr(0, 4, 0)) # encrypt
224write_inode(18, 0x81A4, 16, 0, b"\x00"*60) # no extents
225write_inode(19, 0x81A4, 4096, 0x80000, ext_hdr(1, 4, 0, magic=0xDEAD) + ext_leaf(0, 1, 14))
226
227open(sys.argv[2], "wb").write(img)
228PY
229
230# Also build a META_BG (0x10) superblock copy to verify E4_E_FEATURE fail-closed refusal
231python3 - "$TMP/disk.img" "$TMP/disk_metabg.img" <<'PY'
232import struct, sys
233img = bytearray(open(sys.argv[1], "rb").read())
234sb = 64 * 512 + 1024
235inc = struct.unpack_from("<I", img, sb + 96)[0]
236struct.pack_into("<I", img, sb + 96, inc | 0x10)
237open(sys.argv[2], "wb").write(img)
238PY
239
240compile_host "$TMP/test_gpt_ext4_ro" \
241 "$ROOT/aienux/disk/sha256_bare.om" \
242 "$ROOT/aienux/disk/gpt_ro.om" \
243 "$ROOT/aienux/disk/ext4_ro.om" \
244 "$ROOT/aienux/disk/tests/test_gpt_ext4_ro.om"
245"$TMP/test_gpt_ext4_ro" "$TMP/disk.img" "$TMP/disk_metabg.img"
246compile_linux "$TMP/test_gpt_ext4_ro.elf" \
247 "$ROOT/aienux/disk/sha256_bare.om" \
248 "$ROOT/aienux/disk/gpt_ro.om" \
249 "$ROOT/aienux/disk/ext4_ro.om" \
250 "$ROOT/aienux/disk/tests/test_gpt_ext4_ro.om"
251echo "PASS: test_gpt_ext4_ro (GPT + 64-bit ext4 + depth-1/unwritten/sparse/extent-run + OMFWBND1 overlap/SHA-256 + META_BG/S_IFREG/hostile refusals)"
252
253echo "=== [4/7] RTL8127 + TCP Glue End-to-End Model Test & Existing Driver Regression ==="
254compile_host "$TMP/test_tcp_glue" \
255 "$ROOT/aienux/net/rtl8127/om/rtl8127/rtl8127.om" \
256 "$ROOT/aienux/net/rtl8127/om/rtl8127/model.om" \
257 "$ROOT/aienux/net/tcp/tcp.om" \
258 "$ROOT/aienux/net/rtl8127/om/rtl8127/tcp_glue.om" \
259 "$ROOT/aienux/net/rtl8127/om/rtl8127/test/test_tcp_glue.om"
260"$TMP/test_tcp_glue"
261compile_linux "$TMP/test_tcp_glue.elf" \
262 "$ROOT/aienux/net/rtl8127/om/rtl8127/rtl8127.om" \
263 "$ROOT/aienux/net/rtl8127/om/rtl8127/model.om" \
264 "$ROOT/aienux/net/tcp/tcp.om" \
265 "$ROOT/aienux/net/rtl8127/om/rtl8127/tcp_glue.om" \
266 "$ROOT/aienux/net/rtl8127/om/rtl8127/test/test_tcp_glue.om"
267compile_host "$TMP/rtl8127_driver_test" \
268 "$ROOT/aienux/net/rtl8127/om/rtl8127/rtl8127.om" \
269 "$ROOT/aienux/net/rtl8127/om/rtl8127/model.om" \
270 "$ROOT/aienux/net/rtl8127/om/rtl8127/test/driver.om"
271"$TMP/rtl8127_driver_test"
272echo "PASS: test_tcp_glue + existing rtl8127 driver.om regression"
273
274echo "=== [5/7] RTL8127 Direct Normal-NC Physical DMA Regression Test ==="
275compile_host "$TMP/test_rtl8127_nc_dma" \
276 "$ROOT/aienux/net/rtl8127/om/rtl8127/rtl8127.om" \
277 "$ROOT/aienux/net/rtl8127/om/rtl8127/test/test_rtl8127_nc_dma.om"
278"$TMP/test_rtl8127_nc_dma"
279compile_linux "$TMP/test_rtl8127_nc_dma.elf" \
280 "$ROOT/aienux/net/rtl8127/om/rtl8127/rtl8127.om" \
281 "$ROOT/aienux/net/rtl8127/om/rtl8127/test/test_rtl8127_nc_dma.om"
282echo "PASS: test_rtl8127_nc_dma (direct Normal-NC RX/TX ring + payload before doorbell, .bss rdma untouched)"
283
284echo "=== [6/7] Bare-Metal target a64efi PE32+ Compile Check (NVMe + GPT + ext4 + SHA-256) ==="
285cat <<'OM' > "$TMP/efi_nvme_stub.om"
286fn a64_dsb_sy to void
287end
288fn a64_dc_civac_range pa ulong nbytes ulong to void
289end
290fn gpt_read_lba lba ulong to int
291ret 0
292end
293fn ext4_read_lba lba ulong to int
294ret 0
295end
296fn ext4_sink off ulong b uchar to void
297end
298fn main to int
299r = nvme_init 1645215744 268435456 1
300if ne r 0
301ret r
302end
303r2 = gpt_ro_parse 4096
304if ne r2 0
305ret r2
306end
307ret ext4_ro_mount 64 4000
308end
309OM
310{ printf 'target a64efi\n'; \
311 cat "$ROOT/aienux/disk/nvme_ro_bare.om" \
312 "$ROOT/aienux/disk/nvme_bare_a64efi.om" \
313 "$ROOT/aienux/disk/sha256_bare.om" \
314 "$ROOT/aienux/disk/gpt_ro.om" \
315 "$ROOT/aienux/disk/ext4_ro.om" \
316 "$TMP/efi_nvme_stub.om"; } | "$OMC" > "$TMP/nvme_storage.efi"
317echo "PASS: target a64efi NVMe+GPT+ext4+SHA256 PE32+ compiled ($(wc -c < "$TMP/nvme_storage.efi") bytes)"
318
319echo "=== [7/7] Bare-Metal target a64efi PE32+ Compile Check (RTL8127 + TCP) ==="
320cat <<'OM' > "$TMP/efi_net_stub.om"
321fn a64_dsb_sy to void
322end
323fn a64_dc_civac_range pa ulong nbytes ulong to void
324end
325fn main to int
326r = rtl_bare_setup RTL_SPARK_BAR2 268435456
327if ne r 0
328ret r
329end
330e = rtlinit 268435456
331if ne e 0
332ret int e
333end
334rtltcp_bind_mac
335tinit
336put Tcf 1 3232235884
337put Tcf 2 3232235777
338put Tcf 3 4294967040
339put Tcf 5 12345
340tlisten 9000
341z = rtltcp_poll 0
342ret 0
343end
344OM
345{ printf 'target a64efi\n'; \
346 cat "$ROOT/aienux/net/rtl8127/om/rtl8127/rtl8127.om" \
347 "$ROOT/aienux/net/rtl8127/om/rtl8127/bare_a64efi.om" \
348 "$ROOT/aienux/net/tcp/tcp.om" \
349 "$ROOT/aienux/net/rtl8127/om/rtl8127/tcp_glue.om" \
350 "$TMP/efi_net_stub.om"; } | "$OMC" > "$TMP/rtl8127_tcp.efi"
351echo "PASS: target a64efi RTL8127+TCP PE32+ compiled ($(wc -c < "$TMP/rtl8127_tcp.efi") bytes)"
352
353echo "SLICE D ALL 7 CHECKS PASSED"