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
omega-mv / aienux / spark / mk_qemu_disk.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # aienux/spark/mk_qemu_disk.py OUT_IMG WORKDIR: |
| 4 | # Builds the synthetic NVMe disk for the QEMU spark_main e2e gate step (gate-only test data): |
| 5 | # protective MBR + GPT (UEFI 2.10 ยง5.3, header + entry-array CRC32, backup header at last LBA) |
| 6 | # with one Linux-filesystem partition (0FC63DAF-8483-4772-8E79-3D69D8477DE4) holding a 4K-block |
| 7 | # ext4 (mke2fs -d) that contains /aienos/spark-bundle.img: an OMFWBND1 v1 bundle with kinds 1..6 |
| 8 | # (pseudo-random payloads, real SHA-256 per entry). No NVIDIA firmware bytes are used. |
| 9 | import hashlib, os, struct, subprocess, sys, uuid, zlib, random, shutil |
| 10 | |
| 11 | out, work = sys.argv[1], sys.argv[2] |
| 12 | SEC = 512 |
| 13 | TOTAL = 131072 # 64 MiB disk |
| 14 | P_FIRST, P_SECS = 2048, 65536 # 32 MiB partition |
| 15 | P_LAST = P_FIRST + P_SECS - 1 |
| 16 | |
| 17 | def bundle(): |
| 18 | rng = random.Random(0x5BA4C) |
| 19 | # kind -> size: gsp.bin, bl.img, fmc.img, fmc.hash, fmc.pubkey, fmc.sig |
| 20 | ents = [(1, 65536), (2, 212992), (3, 212296), (4, 48), (5, 384), (6, 384)] |
| 21 | off = 4096 |
| 22 | toc = bytearray(4096) |
| 23 | toc[0:8] = b"OMFWBND1" |
| 24 | struct.pack_into("<III", toc, 8, 1, 0x00030000, len(ents)) |
| 25 | body = [] |
| 26 | for i, (kind, size) in enumerate(ents): |
| 27 | data = bytes(rng.getrandbits(8) for _ in range(size)) |
| 28 | e = 32 + i * 64 |
| 29 | struct.pack_into("<IIQQ", toc, e, kind, 0, off, size) |
| 30 | toc[e + 32:e + 64] = hashlib.sha256(data).digest() |
| 31 | body.append((off, data)) |
| 32 | off = (off + size + 4095) & ~4095 |
| 33 | img = bytearray(off) |
| 34 | img[0:4096] = toc |
| 35 | for o, d in body: |
| 36 | img[o:o + len(d)] = d |
| 37 | return bytes(img) |
| 38 | |
| 39 | root = os.path.join(work, "root") |
| 40 | shutil.rmtree(root, ignore_errors=True) |
| 41 | os.makedirs(os.path.join(root, "aienos")) |
| 42 | open(os.path.join(root, "aienos", "spark-bundle.img"), "wb").write(bundle()) |
| 43 | part = os.path.join(work, "part.img") |
| 44 | if os.path.exists(part): |
| 45 | os.remove(part) |
| 46 | mke2fs = shutil.which("mke2fs") |
| 47 | for c in ("/opt/homebrew/bin/mke2fs", "/opt/homebrew/opt/e2fsprogs/sbin/mke2fs", "/sbin/mke2fs", "/usr/sbin/mke2fs"): |
| 48 | if mke2fs is None and os.path.exists(c): |
| 49 | mke2fs = c |
| 50 | if mke2fs is None: |
| 51 | sys.exit("FAIL: mke2fs (e2fsprogs) not found") |
| 52 | env = dict(os.environ, E2FSPROGS_FAKE_TIME="1700000000") |
| 53 | subprocess.run([mke2fs, "-q", "-F", "-t", "ext4", "-b", "4096", "-O", "64bit,extent", "-L", "aienos", |
| 54 | "-U", "5ba4c000-0000-4000-8000-000000000001", "-E", "root_owner=0:0", |
| 55 | "-d", root, part, str(P_SECS * SEC // 1024) + "k"], check=True, env=env) |
| 56 | pdata = open(part, "rb").read() |
| 57 | assert len(pdata) == P_SECS * SEC |
| 58 | |
| 59 | LINUX_FS = uuid.UUID("0FC63DAF-8483-4772-8E79-3D69D8477DE4").bytes_le |
| 60 | ent = bytearray(128 * 128) |
| 61 | struct.pack_into("<16s16sQQQ", ent, 0, LINUX_FS, uuid.UUID("5ba4c000-0000-4000-8000-000000000002").bytes_le, |
| 62 | P_FIRST, P_LAST, 0) |
| 63 | name = "aienos-root".encode("utf-16-le") |
| 64 | ent[56:56 + len(name)] = name |
| 65 | ecrc = zlib.crc32(bytes(ent)) & 0xffffffff |
| 66 | disk_guid = uuid.UUID("5ba4c000-0000-4000-8000-000000000003").bytes_le |
| 67 | |
| 68 | def hdr(my, alt, ent_lba): |
| 69 | h = bytearray(92) |
| 70 | struct.pack_into("<8sIIIIQQQQ16sQIII", h, 0, b"EFI PART", 0x00010000, 92, 0, 0, |
| 71 | my, alt, 34, TOTAL - 34, disk_guid, ent_lba, 128, 128, ecrc) |
| 72 | struct.pack_into("<I", h, 16, zlib.crc32(bytes(h)) & 0xffffffff) |
| 73 | return bytes(h) |
| 74 | |
| 75 | img = bytearray(TOTAL * SEC) |
| 76 | mbr = bytearray(512) |
| 77 | struct.pack_into("<B3sB3sII", mbr, 446, 0, b"\x00\x02\x00", 0xEE, b"\xff\xff\xff", 1, TOTAL - 1) |
| 78 | mbr[510:512] = b"\x55\xaa" |
| 79 | img[0:512] = mbr |
| 80 | img[SEC:SEC + 92] = hdr(1, TOTAL - 1, 2) |
| 81 | img[2 * SEC:2 * SEC + len(ent)] = ent |
| 82 | img[(TOTAL - 33) * SEC:(TOTAL - 1) * SEC] = ent |
| 83 | img[(TOTAL - 1) * SEC:(TOTAL - 1) * SEC + 92] = hdr(TOTAL - 1, 1, TOTAL - 33) |
| 84 | img[P_FIRST * SEC:P_FIRST * SEC + len(pdata)] = pdata |
| 85 | open(out, "wb").write(img) |
| 86 | print("PASS mk_qemu_disk: %s (GPT linux-fs part LBA %d+%d, ext4 4K, /aienos/spark-bundle.img %d B)" |
| 87 | % (out, P_FIRST, P_SECS, os.path.getsize(os.path.join(root, "aienos", "spark-bundle.img")))) |