interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

GitHub aien-dev/aienux#455 (code only)

closed derived 04b11cbb8985ae0f #455

Imported from GitHub aien-dev/aienux#455 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base 9f9b346c4d0d5f45768dbf9eb9d0d3f266c141fb · Head e41497b884dd6af04285a08758e008ccbc615054

Fetch it

A git bundle of the pull request's head, 18590 bytes.

curl -fsSL https://interplane.io/drakestapleton/aienux/patches/04b11cbb8985ae0f.bundle -o 04b11cbb8985ae0f.bundle
git fetch 04b11cbb8985ae0f.bundle refs/pull/455/head

Raw bundle · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/aienux
455
e41497b884dd6af04285a08758e008ccbc615054
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":455,"url":"https://github.com/aien-dev/aienux/pull/455","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:27Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/.crumb b/.crumb
index 75fca874..cddabd08 100644
--- a/.crumb
+++ b/.crumb
@@ -22,44 +22,44 @@
       "children": [
         {
           "digest": "791de86a40c7086efdbe9ae14a52ecb72fc03f2b2e65bcd864134c8bd2e56906",
-          "last_commit": "c45ea2c4",
+          "last_commit": "d7012755",
           "name": "crates",
           "purpose": "Directory crates. Purpose not yet described by a human."
         },
         {
           "digest": "99d177c93f58a07c5491ac84a08c71d3aa0b0512ce445dd839aa802a7490de89",
-          "last_commit": "d7012755",
+          "last_commit": "832bcbd9",
           "name": "docs",
           "purpose": "Directory docs. Purpose not yet described by a human."
         },
         {
           "digest": "8b81bbe64a067c5aeb6eb22561c27cd13a57774d7876ec2df337d9d2a00419fa",
-          "last_commit": "c45ea2c4",
+          "last_commit": "c01180eb",
           "name": "native",
           "purpose": "Directory native. Purpose not yet described by a human."
         },
         {
           "digest": "3ff6ec04ad2ae8ef9bc36a536cdf659cdfe5ea0960c3a21eb61ce6e9e16adfa4",
-          "last_commit": "d7012755",
+          "last_commit": "89e296db",
           "name": "scripts",
           "purpose": "Directory scripts. Purpose not yet described by a human."
         },
         {
-          "digest": "34003cd929e78814457231251c912b16e3207fb9753374db004bcca6775d56c4",
-          "last_commit": "",
+          "digest": "e891c078bec120c3fdb14e13b7dd25d69054f608c2ea632adf2fd2b6e45d8fa1",
+          "last_commit": "9f2e4cef",
           "name": "tools",
           "purpose": "First-party host artifact preparation tools"
         }
       ],
-      "children_root": "sha256:22879efaa30ec845fc93bf14cd35e0c57a62fedf49788a46df328af3d2fbb94f",
-      "compiled_at": "2026-10-30T02:44:39Z",
+      "children_root": "sha256:7c5afc1c612aef3f506be56d70fa538d5283bfd8f8c61fd2b8c4ef2566e99c0d",
+      "compiled_at": "2026-10-30T02:53:37Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:9eae4dc2e66defe28c5986c16e209a1dd62fe221eb118704f60343671e5486c2",
+      "digest": "sha256:25e3c07ce357c21d7b4ec67c13da5075a90f06bd393253ea6318a51db4916ee9",
       "evidence_files": 236,
       "evidence_root": "sha256:55ef5621f09577ce1ada90d990955aec996d15e16a9b0f6953ecb90f3ca499df",
-      "files": 3546,
-      "generated_at_commit": "ab7768c0dca2ffe6a6b03c722b280ebadde2b971",
-      "source_tree": "sha256:38e0ee565ff675e8dfa06384e60640fe884ff02b4c9173b055d158959269cf4c"
+      "files": 3549,
+      "generated_at_commit": "9f2e4cef4b53bc147516eee438b78658641e4cf5",
+      "source_tree": "sha256:ad964a4a15e2ac6b2666bda2452a64c69258ef225482cfdf59186357d1404eeb"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/tools/.crumb b/tools/.crumb
index 60797c2a..63033a13 100644
--- a/tools/.crumb
+++ b/tools/.crumb
@@ -2,6 +2,12 @@
   "extensions": {
     "generated": {
       "children": [
+        {
+          "digest": "47807b4d88aefedaafd57fc41c556e99170a544223d70cbf9d6c291c35392449",
+          "last_commit": "4d082319",
+          "name": "imac-image",
+          "purpose": "Omega-written builder for the Allen iMac boot medium: GPT with one FAT32 ESP"
+        },
         {
           "digest": "87907ee5c6857824b705d78468e0b6b8a0c4adf94dca0929ca39c076c3a62d02",
           "last_commit": "",
@@ -9,15 +15,15 @@
           "purpose": "Deterministic GPT FAT32 packaging for read-only aarch64 core boot"
         }
       ],
-      "children_root": "sha256:f66e8a9da302bf130e227169544e7011b7ec052b59d85617ea45de135ad23120",
-      "compiled_at": "2026-10-26T16:52:40Z",
+      "children_root": "sha256:79e83672c9b2f9133cc6f0fdd7bfa1329b7b46d82c5cdcb9b37f5928ab055772",
+      "compiled_at": "2026-10-30T02:44:51Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:34003cd929e78814457231251c912b16e3207fb9753374db004bcca6775d56c4",
+      "digest": "sha256:e891c078bec120c3fdb14e13b7dd25d69054f608c2ea632adf2fd2b6e45d8fa1",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 2,
-      "generated_at_commit": "659ddd2d7e3e140cb9bf942c3171cbd9cf3d092e",
-      "source_tree": "sha256:9121a590add04faf36f06485ef046ea047e85429b4f9d097a956766186b08ba9"
+      "files": 5,
+      "generated_at_commit": "1ac32eb3971a5a78e8ea2909a53dca852e4a1b8d",
+      "source_tree": "sha256:e0a2efeb587da8620ec52813b183521d17793d65063e462884192acdc6a71a6b"
     }
   },
   "layer": "tooling",
diff --git a/tools/imac-image/.crumb b/tools/imac-image/.crumb
new file mode 100644
index 00000000..7d1bc42f
--- /dev/null
+++ b/tools/imac-image/.crumb
@@ -0,0 +1,19 @@
+{
+  "extensions": {
+    "generated": {
+      "children": [],
+      "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+      "compiled_at": "2026-10-25T23:20:57Z",
+      "compiler": "crumb-compile/1",
+      "digest": "sha256:47807b4d88aefedaafd57fc41c556e99170a544223d70cbf9d6c291c35392449",
+      "evidence_files": 0,
+      "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+      "files": 3,
+      "generated_at_commit": "d715310158ef316961391b1c9eddeb28efafdd58",
+      "source_tree": "sha256:32ea83a81a68c9f64a034a822e0ea65215ac71968cfdbdf63ba023a48fdcfc03"
+    }
+  },
+  "name": "imac-image",
+  "purpose": "Omega-written builder for the Allen iMac boot medium: GPT with one FAT32 ESP",
+  "schema_version": "1.0.0"
+}
diff --git a/tools/imac-image/README.md b/tools/imac-image/README.md
new file mode 100644
index 00000000..c65e13b2
--- /dev/null
+++ b/tools/imac-image/README.md
@@ -0,0 +1,92 @@
+# Omega boot-image builder
+
+`imac-image.om` writes the whole Allen boot medium with the seeded compiler: protective MBR, a GPT
+with one FAT32 EFI System Partition, and `\EFI\BOOT\BOOTX64.EFI` plus `STARTUP.NSH` inside it. No
+host partitioning tool, FAT tool or filesystem library is used — every byte, including both GPT
+headers with their CRCs, the FAT32 BPB, FSInfo, both FAT copies and the directory entries, is
+written by this program.
+
+An unpartitioned FAT "superfloppy" boots on OVMF but is not what Apple firmware enumerates:
+it looks for an `\EFI\BOOT\BOOTX64.EFI` on a partition. This builder exists so the attended iMac
+trial stages a partitioned disk.
+
+## Build
+
+Two steps, both from this directory:
+
+```sh
+OMC=<seed>/seed/out/omc                                   # the pinned seed compiler
+export OMEGA_COMPILER="$OMC" ALLEN_SRC=<checkout>/native/omega/allen
+
+# 1. the EFI application, composed in the order that compiles (exit 65 on digest drift)
+sh build-efi.sh BOOTX64.EFI ff17a936719049af35c793860d246003170ade526bc8da4a421bdaf9cf0dc33b
+
+# 2. the boot medium that carries it
+"$OMC" < imac-image.om > imac-image && chmod +x imac-image
+./imac-image                      # writes imac-boot.img next to BOOTX64.EFI
+```
+
+`build-efi.sh` writes `target x86efi`, concatenates the twenty sources in the verified order, pipes
+them to the pinned compiler, prints the compiler and output digests, and exits 65 when the digest
+does not match the one you passed. The twenty sources are **not on main**: they are on aienux
+`feat/allen-imac-competition` (`d4ffde3c`), so point `ALLEN_SRC` at a checkout of that branch. On the
+`d4ffde3c` sources with compiler `c99eda24…` it reproduces `ff17a936…` exactly.
+
+`build-efi.sh` exit status: 0 written, 64 the compiler refused (nothing published), 65 digest
+mismatch, 66 missing sources, 69 no `OMEGA_COMPILER`, 73 `$out` already exists.
+
+`imac-image` exit status: 0 written, 2 input open, 3 input empty, 4 output open, 5 truncate,
+**10 the EFI is larger than the ESP data area** (191497 clusters, about 98 MB), 6 sync, 7 close
+output, 8 a short write, 9 input seek/close.
+
+The payload is streamed one 512-byte cluster at a time, copied from the input and written straight
+into the image, so there is no in-memory size ceiling: a 1.5 MiB EFI and a 3 MiB EFI both build, and
+the only limit is the fixed ESP geometry, which is checked before anything is written. The geometry is fixed (disk 196608 sectors, ESP
+2048..196574, FAT32 with 512-byte sectors, 1 sector per cluster, 32 reserved, 2 FATs of 1497 sectors,
+191501 clusters, constant volume serial and constant directory timestamps), so the same EFI bytes
+always give the same image digest.
+
+## Receipt (first run, 2026-10-25)
+
+| Item | Value |
+|---|---|
+| builder source | `imac-image.om` sha256 `d76d3852daf026238d22b0d88faedba15338e372e5d423a8e3c5b677fdde5f14` |
+| compiler | omega `feat/allen-imac-competition` `c3fb79697`, `seed/out/omc` sha256 `c99eda2465036c0637a444689a65dd7fc736e4fa8a8b4cf3456b0ce1b57f8996` |
+| input EFI | aienux `feat/allen-imac-competition` `d4ffde3c`, `target x86efi`, sha256 `ff17a936719049af35c793860d246003170ade526bc8da4a421bdaf9cf0dc33b` (1528832 bytes) |
+| output image | `imac-boot.img` sha256 `f0811738c2a30bc93076f18884fb9cdc2c081ae83775d147565352b40a4c85f0`, 100663296 bytes |
+
+Reproducibility is the useful property here, and it is checkable from the repositories rather than
+asserted: `build-efi.sh` re-derives `ff17a936…` from the `d4ffde3c` sources with that compiler, and
+two runs of `imac-image` on the same EFI produce the same image digest. This deliberately does not
+chase byte-identity with the `sgdisk`/`mkfs.vfat`/`mtools` image that preceded it: `mkfs.vfat` takes
+the volume serial from the clock and mtools stamps directory times, so that reference is not
+reproducible by construction. The acceptance is same inputs, same digest, firmware boots it, and the
+payload round-trips.
+
+## Verification performed
+
+Reading tools are used only as independent checkers, never as part of the build:
+
+- `sgdisk -p imac-boot.img` — partition 1, sectors 2048..196574, code `EF00`, name `ALLEN`.
+- `sgdisk -v imac-boot.img` — `No problems found` (both CRCs, primary and backup headers agree).
+- `fsck.vfat -n <partition>` — clean: 5 files, 2990/191501 clusters, no FSInfo or `..` complaints.
+- `mdir -i imac-boot.img@@1M ::/EFI/BOOT` — `BOOTX64.EFI`, 1528832 bytes.
+- Payload sizes: 1528832 bytes → `f0811738…` (identical to the buffered builder's output, so the
+  streaming refactor changed no byte), 3145728 bytes → rc 0 with `fsck.vfat` clean at 6148 clusters,
+  125829120 bytes → rc 10 before any write. The earlier whole-file read would have trapped at the
+  first byte past 2 MiB.
+- The printed build block, executed verbatim in a clean directory: `build-efi.sh` → `ff17a936…`
+  (rc 0), `imac-image` → `f0811738…` (rc 0). Exit paths exercised rather than declared: 65 wrong
+  expected digest, 66 missing sources, 69 no `OMEGA_COMPILER`, 64 compiler refusal, 73 existing
+  output. A refusal publishes nothing (compile goes to a scratch file and is linked into place only
+  on success), so a retry at the same path succeeds and `imac-image` can never pick up a zero-byte
+  EFI.
+- `mcopy` round trip on the final image `f0811738…` — the extracted EFI hashes to `ff17a936…` and
+  `cmp` is silent; `STARTUP.NSH` reads `fs0:\EFI\BOOT\BOOTX64.EFI\r\n`.
+- OVMF with a fresh NVRAM (no boot entry) — `BdsDxe` creates its own entry from the ESP, starts the
+  image, and Allen's window renders with `Network is up. Type a request.` The capture holds only the
+  firmware's own ICMPv6 (3 packets, no DHCP/DNS/TCP), so this proves the layout, a cold boot and the
+  SNP probe; it is not evidence about the Omega transport path, which is still blocked on aienux#454.
+
+Emulator evidence, not hardware qualification: no physical iMac was touched, and Apple firmware has
+not been exercised.
diff --git a/tools/imac-image/build-efi.sh b/tools/imac-image/build-efi.sh
new file mode 100755
index 00000000..92949e6f
--- /dev/null
+++ b/tools/imac-image/build-efi.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+# Compose the firmware-hosted Allen sources in the order that compiles, add the `target x86efi`
+# selector, and build the EFI application the boot image carries.
+#
+# The twenty sources are not on main: they live on aienux `feat/allen-imac-competition`
+# (`d4ffde3c`). Build them from a checkout of that branch, or point ALLEN_SRC at one.
+#
+#   OMEGA_COMPILER=<seed>/seed/out/omc sh build-efi.sh BOOTX64.EFI
+#   OMEGA_COMPILER=<seed>/seed/out/omc sh build-efi.sh BOOTX64.EFI ff17a936...
+#
+# Second argument, if given, is the expected sha256; a difference exits 65 instead of passing.
+set -eu
+root=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
+allen=${ALLEN_SRC:-"$root/native/omega/allen"}
+compiler=${OMEGA_COMPILER:-}
+files="platform.om base.om vendor/sha256-core.om font-data.om font.om surface.om github.om
+http.om net.om tls-crypto.om vendor/cert-sha384.om vendor/cert-p256.om tls-p384.om
+vendor/cert-root.om tls-cert.om tls.om session.om repository-screen.om efi-main.om demo.om"
+
+[ -n "$compiler" ] && [ -x "$compiler" ] || {
+ echo 'OMEGA_COMPILER must name the seed compiler (seed/out/omc)' >&2
+ exit 69
+}
+
+missing=0
+for f in $files; do
+ if [ ! -f "$allen/$f" ]; then
+  echo "missing $allen/$f" >&2
+  missing=1
+ fi
+done
+[ "$missing" = 0 ] || {
+ echo 'the Allen sources are on aienux feat/allen-imac-competition (d4ffde3c), not on main' >&2
+ exit 66
+}
+
+out=${1:-BOOTX64.EFI}
+[ ! -e "$out" ] && [ ! -L "$out" ] || { echo 'fresh output required' >&2; exit 73; }
+unit="${out}.om"
+trap 'rm -f -- "$unit"' EXIT HUP INT TERM
+
+{
+ echo 'target x86efi'
+ for f in $files; do
+  cat "$allen/$f"
+ done
+} > "$unit"
+
+# Compile to a scratch file and publish by link: a refused compile leaves nothing at $out, so a
+# retry is not blocked by the fresh-output guard and no zero-byte EFI can be picked up by imac-image.
+tmp="${out}.build.$$"
+trap 'rm -f -- "$unit" "$tmp"' EXIT HUP INT TERM
+if ! "$compiler" < "$unit" > "$tmp" || [ ! -s "$tmp" ]; then
+ echo 'compile refused; nothing published' >&2
+ exit 64
+fi
+got=$(sha256sum "$tmp" | cut -d' ' -f1)
+
+want=${2:-}
+if [ -n "$want" ] && [ "$got" != "$want" ]; then
+ echo "expected $want; the sources or compiler differ" >&2
+ exit 65
+fi
+
+ln -T -- "$tmp" "$out"
+printf '%s  %s\n' "$got" "$out"
+printf 'compiler %s\n' "$(sha256sum "$compiler" | cut -d' ' -f1)"
diff --git a/tools/imac-image/imac-image.om b/tools/imac-image/imac-image.om
new file mode 100644
index 00000000..3408e004
--- /dev/null
+++ b/tools/imac-image/imac-image.om
@@ -0,0 +1,605 @@
+note SPDX-License-Identifier: MIT
+note Builds the Allen iMac boot image with the seeded compiler alone: protective MBR,
+note GPT with one FAT32 EFI System Partition, \EFI\BOOT\BOOTX64.EFI and STARTUP.NSH.
+note No host partitioning or FAT tool is used; every byte is written by this program.
+note Geometry is fixed so the same EFI bytes always give the same image digest:
+note   disk 196608 sectors, ESP 2048..196574 (194527 sectors), FAT32 512-byte sectors,
+note   1 sector per cluster, 32 reserved sectors, 2 FATs of 1497 sectors, 191501 clusters.
+
+const TOTS ulong 196608
+const ESPST ulong 2048
+const ESPOF ulong 1048576
+const ESPS ulong 194527
+const RSV ulong 32
+const FATSZ ulong 1497
+const FATS ulong 1497
+const CLUS ulong 191501
+const BKLBA ulong 196607
+const BKENT ulong 196575
+const LASTU ulong 196574
+const RDIR ulong 2
+const EDIR ulong 3
+const BDIR ulong 4
+const FCLS ulong 5
+const VOLID ulong 2065321822
+const DDATE ulong 23897
+const ECOUNT ulong 128
+const ESIZE ulong 128
+const IMGLN ulong 100663296
+const EOC ulong 268435455
+
+global Nes ulong 1
+global Err ulong 1
+global Tv ulong 4
+
+fn s16 S edit uchar 512 off ulong v ulong to void
+put S off uchar and v 255
+put S add off 1 uchar and shr v 8 255
+end
+
+fn s32 S edit uchar 512 off ulong v ulong to void
+put S off uchar and v 255
+put S add off 1 uchar and shr v 8 255
+put S add off 2 uchar and shr v 16 255
+put S add off 3 uchar and shr v 24 255
+end
+
+fn s64 S edit uchar 512 off ulong v ulong to void
+s32 edit S off v
+s32 edit S add off 4 shr v 32
+end
+
+fn g64 S edit uchar 16384 off ulong v ulong to void
+put S off uchar and v 255
+put S add off 1 uchar and shr v 8 255
+put S add off 2 uchar and shr v 16 255
+put S add off 3 uchar and shr v 24 255
+put S add off 4 uchar and shr v 32 255
+put S add off 5 uchar and shr v 40 255
+put S add off 6 uchar and shr v 48 255
+put S add off 7 uchar and shr v 56 255
+end
+
+fn scrc S view uchar 512 n ulong to ulong
+note CRC32 of the first n bytes of the working sector.
+c = ulong 4294967295
+i = ulong 0
+while lt i n bound 512
+c = xor c ulong get S i
+k = ulong 0
+while lt k 8 bound 8
+if eq and c 1 1
+c = xor shr c 1 3988292384
+else
+c = shr c 1
+end
+k = add k 1
+end
+i = add i 1
+end
+ret xor c 4294967295
+end
+
+fn gcrc G view uchar 16384 to ulong
+note CRC32 of the whole 16384-byte GPT entry array.
+c = ulong 4294967295
+i = ulong 0
+while lt i 16384 bound 20000
+c = xor c ulong get G i
+k = ulong 0
+while lt k 8 bound 8
+if eq and c 1 1
+c = xor shr c 1 3988292384
+else
+c = shr c 1
+end
+k = add k 1
+end
+i = add i 1
+end
+ret xor c 4294967295
+end
+
+fn zsec S edit uchar 512 to void
+i = ulong 0
+while lt i 512 bound 512
+put S i uchar 0
+i = add i 1
+end
+end
+
+fn zsecs fd long S edit uchar 512 n ulong to void
+i = ulong 0
+while lt i n bound 2100
+zsec edit S
+if lt write int fd S 0 512 512
+put Err 0 add get Err 0 1
+end
+i = add i 1
+end
+end
+
+fn swr fd long S view uchar 512 to void
+if lt write int fd S 0 512 512
+put Err 0 add get Err 0 1
+end
+end
+
+fn gwr fd long G view uchar 16384 to void
+if lt write int fd G 0 16384 16384
+put Err 0 add get Err 0 1
+end
+end
+
+fn datap fi int fo long to void
+note Copy the EFI payload one cluster at a time, zero padding a short final read. No size ceiling.
+C = new uchar 512 0
+i = ulong 0
+while lt i get Nes 0 bound 200000
+zsec edit C
+r = read fi C 0 512
+if lt r 0
+put Err 0 add get Err 0 1
+end
+if lt write int fo C 0 512 512
+put Err 0 add get Err 0 1
+end
+i = add i 1
+end
+end
+
+fn seek fd long off ulong to void
+r = fseek int fd long off 0
+if lt r 0
+put Err 0 add get Err 0 1
+end
+end
+
+fn spcs S edit uchar 512 off ulong n ulong to void
+i = ulong 0
+while lt i n bound 12
+put S add off i uchar 32
+i = add i 1
+end
+end
+
+fn dent S edit uchar 512 off ulong at ulong clus ulong sz ulong to void
+note One 32-byte directory entry; the name bytes were written before this call.
+put S add off 11 uchar at
+put S add off 12 uchar 0
+put S add off 13 uchar 0
+s16 edit S add off 14 0
+s16 edit S add off 16 DDATE
+s16 edit S add off 18 DDATE
+s16 edit S add off 20 shr clus 16
+s16 edit S add off 22 0
+s16 edit S add off 24 DDATE
+s16 edit S add off 26 and clus 65535
+s32 edit S add off 28 sz
+end
+
+fn fatvl c ulong to ulong
+note FAT32 chain value for one cluster index; EOC marks the end of a chain.
+last = add FCLS sub get Nes 0 1
+nsh = add FCLS get Nes 0
+if lt c RDIR
+ret 0
+end
+if lt c FCLS
+ret EOC
+end
+if lt c last
+ret add c 1
+end
+if eq c last
+ret EOC
+end
+if eq c nsh
+ret EOC
+end
+ret 0
+end
+
+fn fspar to void
+note FAT32 free-cluster count and next free cluster for the current EFI size.
+n = get Nes 0
+put Tv 0 sub sub CLUS 4 n
+put Tv 1 add add RDIR 4 n
+end
+
+fn fats S edit uchar 512 idx ulong to void
+note Build FAT sector idx from the chain function so both copies are identical.
+zsec edit S
+i = ulong 0
+while lt i 128 bound 128
+c = add mul idx 128 i
+if lt c add CLUS 2
+v = fatvl c
+if eq c 0
+v = 4294967288
+end
+if eq c 1
+v = EOC
+end
+s32 edit S mul i 4 v
+end
+i = add i 1
+end
+end
+
+fn mbrmk S edit uchar 512 to void
+zsec edit S
+put S 446 uchar 0
+put S 450 uchar 238
+put S 454 uchar 1
+put S 458 uchar and sub TOTS 1 255
+put S 459 uchar and shr sub TOTS 1 8 255
+put S 460 uchar and shr sub TOTS 1 16 255
+put S 461 uchar and shr sub TOTS 1 24 255
+put S 510 uchar 85
+put S 511 uchar 170
+end
+
+fn gpthd S edit uchar 512 cur ulong ent ulong back ulong ecr ulong to void
+note Build one GPT header sector and return the CRC32 of its 92 bytes.
+zsec edit S
+put S 0 uchar 69
+put S 1 uchar 70
+put S 2 uchar 73
+put S 3 uchar 32
+put S 4 uchar 80
+put S 5 uchar 65
+put S 6 uchar 82
+put S 7 uchar 84
+s32 edit S 8 65536
+s32 edit S 12 92
+s32 edit S 20 0
+s64 edit S 24 cur
+s64 edit S 32 back
+s64 edit S 40 34
+s64 edit S 48 LASTU
+s32 edit S 56 305419896
+s32 edit S 60 2271560481
+s32 edit S 64 1432778632
+s32 edit S 68 3723482536
+s64 edit S 72 ent
+s32 edit S 80 ECOUNT
+s32 edit S 84 ESIZE
+s32 edit S 88 ecr
+s32 edit S 16 scrc view S 92
+end
+
+fn entar G edit uchar 16384 to void
+note Build the 128-entry GPT array; entry 0 is the EFI System Partition.
+i = ulong 0
+while lt i 16384 bound 20000
+put G i uchar 0
+i = add i 1
+end
+put G 0 uchar 40
+put G 1 uchar 115
+put G 2 uchar 42
+put G 3 uchar 193
+put G 4 uchar 31
+put G 5 uchar 248
+put G 6 uchar 210
+put G 7 uchar 17
+put G 8 uchar 186
+put G 9 uchar 75
+put G 10 uchar 0
+put G 11 uchar 160
+put G 12 uchar 201
+put G 13 uchar 62
+put G 14 uchar 201
+put G 15 uchar 59
+put G 16 uchar 17
+put G 17 uchar 34
+put G 18 uchar 51
+put G 19 uchar 68
+put G 20 uchar 85
+put G 21 uchar 102
+put G 22 uchar 119
+put G 23 uchar 136
+put G 24 uchar 153
+put G 25 uchar 170
+put G 26 uchar 187
+put G 27 uchar 204
+put G 28 uchar 221
+put G 29 uchar 238
+put G 30 uchar 255
+put G 31 uchar 0
+g64 edit G 32 ESPST
+g64 edit G 40 LASTU
+g64 edit G 48 0
+put G 56 uchar 65
+put G 58 uchar 76
+put G 60 uchar 76
+put G 62 uchar 69
+put G 64 uchar 78
+end
+
+fn bootsc S edit uchar 512 to void
+note FAT32 BPB for the ESP.
+zsec edit S
+put S 0 uchar 235
+put S 1 uchar 88
+put S 2 uchar 144
+put S 3 uchar 79
+put S 4 uchar 77
+put S 5 uchar 69
+put S 6 uchar 71
+put S 7 uchar 65
+put S 8 uchar 32
+put S 9 uchar 32
+put S 10 uchar 32
+s16 edit S 11 512
+put S 13 uchar 1
+s16 edit S 14 RSV
+put S 16 uchar 2
+s16 edit S 17 0
+s16 edit S 19 0
+put S 21 uchar 248
+s16 edit S 22 0
+s16 edit S 24 63
+s16 edit S 26 255
+s32 edit S 28 ESPST
+s32 edit S 32 ESPS
+s32 edit S 36 FATSZ
+s16 edit S 40 0
+s16 edit S 42 0
+s32 edit S 44 RDIR
+s16 edit S 48 1
+s16 edit S 50 6
+s32 edit S 52 0
+s32 edit S 56 0
+put S 64 uchar 128
+put S 66 uchar 41
+s32 edit S 67 VOLID
+put S 71 uchar 65
+put S 72 uchar 76
+put S 73 uchar 76
+put S 74 uchar 69
+put S 75 uchar 78
+spcs edit S 76 6
+put S 82 uchar 70
+put S 83 uchar 65
+put S 84 uchar 84
+put S 85 uchar 51
+put S 86 uchar 50
+spcs edit S 87 3
+put S 510 uchar 85
+put S 511 uchar 170
+end
+
+fn fsinf S edit uchar 512 frc ulong nxt ulong to void
+zsec edit S
+s32 edit S 0 1096897106
+s32 edit S 484 1631679090
+s32 edit S 488 frc
+s32 edit S 492 nxt
+s32 edit S 508 2857697280
+end
+
+fn rootd S edit uchar 512 to void
+note Volume label, EFI directory and the STARTUP.NSH file in the root cluster.
+zsec edit S
+put S 0 uchar 65
+put S 1 uchar 76
+put S 2 uchar 76
+put S 3 uchar 69
+put S 4 uchar 78
+spcs edit S 5 6
+dent edit S 0 8 0 0
+put S 32 uchar 69
+put S 33 uchar 70
+put S 34 uchar 73
+spcs edit S 35 8
+dent edit S 32 16 EDIR 0
+put S 64 uchar 83
+put S 65 uchar 84
+put S 66 uchar 65
+put S 67 uchar 82
+put S 68 uchar 84
+put S 69 uchar 85
+put S 70 uchar 80
+put S 71 uchar 32
+put S 72 uchar 78
+put S 73 uchar 83
+put S 74 uchar 72
+c = add FCLS get Nes 0
+dent edit S 64 32 c 27
+end
+
+fn efidr S edit uchar 512 to void
+zsec edit S
+put S 0 uchar 46
+spcs edit S 1 10
+dent edit S 0 16 EDIR 0
+put S 32 uchar 46
+put S 33 uchar 46
+spcs edit S 34 9
+dent edit S 32 16 0 0
+put S 64 uchar 66
+put S 65 uchar 79
+put S 66 uchar 79
+put S 67 uchar 84
+spcs edit S 68 7
+dent edit S 64 16 BDIR 0
+end
+
+fn bootd S edit uchar 512 to void
+zsec edit S
+put S 0 uchar 46
+spcs edit S 1 10
+dent edit S 0 16 BDIR 0
+put S 32 uchar 46
+put S 33 uchar 46
+spcs edit S 34 9
+dent edit S 32 16 EDIR 0
+put S 64 uchar 66
+put S 65 uchar 79
+put S 66 uchar 79
+put S 67 uchar 84
+put S 68 uchar 88
+put S 69 uchar 54
+put S 70 uchar 52
+put S 71 uchar 32
+put S 72 uchar 69
+put S 73 uchar 70
+put S 74 uchar 73
+dent edit S 64 32 FCLS get Tv 2
+end
+
+fn startn S edit uchar 512 to void
+zsec edit S
+put S 0 uchar 102
+put S 1 uchar 115
+put S 2 uchar 48
+put S 3 uchar 58
+put S 4 uchar 92
+put S 5 uchar 69
+put S 6 uchar 70
+put S 7 uchar 73
+put S 8 uchar 92
+put S 9 uchar 66
+put S 10 uchar 79
+put S 11 uchar 79
+put S 12 uchar 84
+put S 13 uchar 92
+put S 14 uchar 66
+put S 15 uchar 79
+put S 16 uchar 79
+put S 17 uchar 84
+put S 18 uchar 88
+put S 19 uchar 54
+put S 20 uchar 52
+put S 21 uchar 46
+put S 22 uchar 69
+put S 23 uchar 70
+put S 24 uchar 73
+put S 25 uchar 13
+put S 26 uchar 10
+end
+
+fn main to void
+Sct = new uchar 512 0
+Gpt = new uchar 16384 0
+Pth = new uchar 32 0
+
+put Pth 0 uchar 66
+put Pth 1 uchar 79
+put Pth 2 uchar 79
+put Pth 3 uchar 84
+put Pth 4 uchar 88
+put Pth 5 uchar 54
+put Pth 6 uchar 52
+put Pth 7 uchar 46
+put Pth 8 uchar 69
+put Pth 9 uchar 70
+put Pth 10 uchar 73
+
+fi = fopen Pth 0 11 0 0
+if lt fi 0
+exit 2
+end
+n = fseek int fi 0 2
+if lt n 1
+exit 3
+end
+q = fseek int fi 0 0
+if lt q 0
+exit 9
+end
+put Nes 0 ulong div add n 511 512
+if gt get Nes 0 sub CLUS 4
+exit 10
+end
+put Tv 2 ulong n
+fspar
+
+put Pth 0 uchar 105
+put Pth 1 uchar 109
+put Pth 2 uchar 97
+put Pth 3 uchar 99
+put Pth 4 uchar 45
+put Pth 5 uchar 98
+put Pth 6 uchar 111
+put Pth 7 uchar 111
+put Pth 8 uchar 116
+put Pth 9 uchar 46
+put Pth 10 uchar 105
+put Pth 11 uchar 109
+put Pth 12 uchar 103
+
+fo = fopen Pth 0 13 577 420
+if lt fo 0
+exit 4
+end
+if lt ftrunc int fo long IMGLN 0
+exit 5
+end
+
+mbrmk edit Sct
+swr fo view Sct
+entar edit Gpt
+cc = gcrc view Gpt
+gpthd edit Sct 1 2 BKLBA cc
+swr fo view Sct
+gwr fo view Gpt
+
+seek fo ESPOF
+bootsc edit Sct
+swr fo view Sct
+fsinf edit Sct get Tv 0 get Tv 1
+swr fo view Sct
+zsecs fo edit Sct 4
+bootsc edit Sct
+swr fo view Sct
+fsinf edit Sct get Tv 0 get Tv 1
+swr fo view Sct
+zsecs fo edit Sct 24
+
+i = ulong 0
+while lt i FATS bound 1500
+fats edit Sct i
+swr fo view Sct
+i = add i 1
+end
+i = ulong 0
+while lt i FATS bound 1500
+fats edit Sct i
+swr fo view Sct
+i = add i 1
+end
+
+rootd edit Sct
+swr fo view Sct
+efidr edit Sct
+swr fo view Sct
+bootd edit Sct
+swr fo view Sct
+
+datap int fi fo
+startn edit Sct
+swr fo view Sct
+
+seek fo mul BKENT 512
+gwr fo view Gpt
+seek fo mul BKLBA 512
+gpthd edit Sct BKLBA BKENT 1 cc
+swr fo view Sct
+
+if lt fsync int fo 0
+exit 6
+end
+if lt fclose int fo 0
+exit 7
+end
+if lt fclose int fi 0
+exit 9
+end
+if ne get Err 0 0
+exit 8
+end
+exit 0
+end