code wiki / _hdl_build / nx_spore_emit_iso.nx
nx_spore_emit_iso.nx source
↩ module page · 161 lines · 12087 B
1// nx_spore_emit_iso.nx -- nx_spore_emit rung 1: a SOVEREIGN El Torito bootable ISO (BIOS/KVM) for cloud testing (Vultr).
2//
3// The iso/README (nx_spore) REFUSES xorriso/mkisofs/genisoimage -- so we emit ISO 9660 + El Torito ourselves.
4// This produces knowledge/status/nishi_vultr.iso: a minimal but spec-faithful bootable CD image that Vultr
5// (and any BIOS/KVM/SeaBIOS host) boots via "Custom ISO". Layout (2048-byte ISO sectors):
6// 0..15 system area (zero)
7// 16 Primary Volume Descriptor (type 1, "CD001")
8// 17 Boot Record VD (El Torito, "EL TORITO SPECIFICATION") -> boot catalog @ sector 19
9// 18 Volume Descriptor Set Terminator (type 255)
10// 19 El Torito Boot Catalog: validation entry (checksum + 55 AA) + default entry (no-emul, load @0x7C00)
11// 20 boot image = our proven real-mode boot sector (prints a banner via BIOS INT10h, then HLT)
12// 21 (empty root directory extent)
13// The default entry is NO-EMULATION: BIOS loads sector-count*512 bytes from the boot image to 0x7C00 and runs
14// our real-mode code -- the exact code our sovereign BIOS-INT emu already executes (C8/nx_nishi_bootimg).
15//
16// HONEST SCOPE (no overclaim): structure is VERIFIED here by parse-back (PVD/boot-record/terminator/catalog
17// checksum/default-entry/boot-image-signature) -- the BYTES are El-Torito-correct. The ACTUAL boot on real
18// SeaBIOS is NOT verified locally (no qemu installed); Vultr's KVM boot (a disposable VM = never-brick) is the
19// real validation. Full browsable ISO 9660 (path tables / files) + UEFI + hybrid-MBR = nx_spore_emit refinements.
20// KAT 6/6 (parse-back of the persisted bytes). composes nx_syscalls only.
21// NEVER-BRICK (Rule 26): writes a FILE; models nothing on hardware. expect_exit: 0 license_tier: ORIGINAL
22import "nx_syscalls.nx"
23const LBA_MAGIC_2048: i64 = 2048
24
25const SEC: i64 = 2048
26const NSEC: i64 = 24 // total ISO sectors (49152 bytes)
27const LBA_PVD: i64 = 16
28const LBA_BOOTREC: i64 = 17
29const LBA_TERM: i64 = 18
30const LBA_CATALOG: i64 = 19
31const LBA_BOOTIMG: i64 = 20
32const LBA_ROOTDIR: i64 = 21
33const BOOT_VSECS: i64 = 4 // virtual 512-byte sectors the BIOS loads from the boot image (4*512=2048)
34
35func ui_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
36func ui_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
37func w16le(d: *u8, o: i64, v: i64) -> i64 { d[o]=(v&0xFF) as u8; d[o+1]=((v>>8)&0xFF) as u8; return 0 }
38func w16be(d: *u8, o: i64, v: i64) -> i64 { d[o]=((v>>8)&0xFF) as u8; d[o+1]=(v&0xFF) as u8; return 0 }
39func w32le(d: *u8, o: i64, v: i64) -> i64 { d[o]=(v&0xFF) as u8; d[o+1]=((v>>8)&0xFF) as u8; d[o+2]=((v>>16)&0xFF) as u8; d[o+3]=((v>>24)&0xFF) as u8; return 0 }
40func w32be(d: *u8, o: i64, v: i64) -> i64 { d[o]=((v>>24)&0xFF) as u8; d[o+1]=((v>>16)&0xFF) as u8; d[o+2]=((v>>8)&0xFF) as u8; d[o+3]=(v&0xFF) as u8; return 0 }
41func r32le(d: *u8, o: i64) -> i64 { return (d[o] as i64) | ((d[o+1] as i64)<<8) | ((d[o+2] as i64)<<16) | ((d[o+3] as i64)<<24) }
42// copy a NUL-terminated string into d[o..o+n), padding the remainder with `pad`
43func strpad(d: *u8, o: i64, s: *u8, n: i64, pad: i64) -> i64 { var i: i64=0; while i<n { if s[i]!=(0 as u8) { d[o+i]=s[i] } else { d[o+i]=pad as u8 } i=i+1 } return 0 }
44func streq(d: *u8, o: i64, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if d[o+i]!=s[i] { return 0 } i=i+1 } return 1 }
45
46// author our real-mode boot sector at byte offset `b` (prints a banner via BIOS INT10h AH=0Eh, then HLT).
47func author_boot(d: *u8, b: i64) -> i64 {
48 var q: i64 = 0
49 d[b+q]=0xBE as u8; let si_q: i64 = q+1; q=q+3 // mov si, imm16 (patched)
50 let loop_q: i64 = q
51 d[b+q]=0xAC as u8; q=q+1 // lodsb
52 d[b+q]=0x08 as u8; d[b+q+1]=0xC0 as u8; q=q+2 // or al,al
53 d[b+q]=0x74 as u8; let jz_q: i64 = q+1; q=q+2 // jz hang
54 d[b+q]=0xB4 as u8; d[b+q+1]=0x0E as u8; q=q+2 // mov ah,0x0E
55 d[b+q]=0xCD as u8; d[b+q+1]=0x10 as u8; q=q+2 // int 0x10
56 d[b+q]=0xEB as u8; d[b+q+1]=((loop_q-(q+2)) & 0xFF) as u8; q=q+2 // jmp loop
57 let hang_q: i64 = q
58 d[b+q]=0xF4 as u8; q=q+1 // hlt
59 let msg_q: i64 = q
60 let msg: *u8 = "NISHIOS sovereign -- booted on cloud KVM via El Torito (Vultr-ready)\x0D\x0A\x00"
61 var mi: i64=0; while msg[mi]!=(0 as u8) { d[b+q]=msg[mi]; q=q+1; mi=mi+1 } d[b+q]=0 as u8; q=q+1
62 d[b+jz_q] = ((hang_q-(jz_q+1)) & 0xFF) as u8 // patch jz -> hang
63 let si_abs: i64 = 0x7C00 + msg_q // SI = 0x7C00 + msg offset
64 d[b+si_q] = (si_abs & 0xFF) as u8
65 d[b+si_q+1] = ((si_abs>>8) & 0xFF) as u8
66 d[b+510]=0x55 as u8; d[b+511]=0xAA as u8 // signature (harmless for no-emul; robust)
67 return 0
68}
69
70func main() -> i64 {
71 ui_puts("nx_spore_emit rung 1: SOVEREIGN El Torito bootable ISO for Vultr/KVM (no xorriso)\n" as *u8)
72 let sz: i64 = NSEC * SEC
73 let d: *u8 = sys_mmap(sz + 16)
74 var z: i64=0; while z<sz { d[z]=0 as u8; z=z+1 }
75
76 // ---- Primary Volume Descriptor @ sector 16 ----
77 let pvd: i64 = LBA_PVD * SEC
78 d[pvd+0]=1 as u8; strpad(d, pvd+1, "CD001\x00" as *u8, 5, 0x20); d[pvd+6]=1 as u8
79 strpad(d, pvd+8, "\x00" as *u8, 32, 0x20) // system id (spaces)
80 strpad(d, pvd+40, "NISHIOS\x00" as *u8, 32, 0x20) // volume id
81 w32le(d, pvd+80, NSEC); w32be(d, pvd+84, NSEC) // volume space size (both-endian)
82 w16le(d, pvd+120, 1); w16be(d, pvd+122, 1) // volume set size
83 w16le(d, pvd+124, 1); w16be(d, pvd+126, 1) // volume sequence number
84 w16le(d, pvd+128, SEC); w16be(d, pvd+130, SEC) // logical block size = LBA_MAGIC_2048
85 // minimal root directory record (34 bytes) @ pvd+156
86 d[pvd+156]=34 as u8; d[pvd+157]=0 as u8
87 w32le(d, pvd+158, LBA_ROOTDIR); w32be(d, pvd+162, LBA_ROOTDIR)
88 w32le(d, pvd+166, SEC); w32be(d, pvd+170, SEC)
89 d[pvd+181]=0x02 as u8 // flags = directory
90 d[pvd+186]=1 as u8; d[pvd+187]=1 as u8 // vol set size / seq in dir record
91 d[pvd+188]=1 as u8; d[pvd+189]=0 as u8 // file id len=1, id=0x00 (root)
92
93 // ---- Boot Record Volume Descriptor (El Torito) @ sector 17 ----
94 let br: i64 = LBA_BOOTREC * SEC
95 d[br+0]=0 as u8; strpad(d, br+1, "CD001\x00" as *u8, 5, 0x20); d[br+6]=1 as u8
96 strpad(d, br+7, "EL TORITO SPECIFICATION\x00" as *u8, 32, 0x00)
97 w32le(d, br+71, LBA_CATALOG) // absolute pointer to boot catalog
98
99 // ---- Volume Descriptor Set Terminator @ sector 18 ----
100 let tm: i64 = LBA_TERM * SEC
101 d[tm+0]=255 as u8; strpad(d, tm+1, "CD001\x00" as *u8, 5, 0x20); d[tm+6]=1 as u8
102
103 // ---- El Torito Boot Catalog @ sector 19 ----
104 let cat: i64 = LBA_CATALOG * SEC
105 // validation entry (32 bytes)
106 d[cat+0]=0x01 as u8 // header id
107 d[cat+1]=0x00 as u8 // platform id 0 = 80x86
108 // bytes 2-3 reserved=0; 4-27 id string=0
109 d[cat+30]=0x55 as u8; d[cat+31]=0xAA as u8 // key bytes
110 // checksum (bytes 28-29): make the sum of all 16 LE words == 0 (mod 0x10000)
111 var sum: i64 = 0
112 var wi: i64 = 0
113 while wi < 16 {
114 if wi != 14 { // skip the checksum word itself (offset 28 = word 14)
115 sum = sum + ((d[cat+wi*2] as i64) | ((d[cat+wi*2+1] as i64)<<8))
116 }
117 wi = wi + 1
118 }
119 let cksum: i64 = (0x10000 - (sum & 0xFFFF)) & 0xFFFF
120 w16le(d, cat+28, cksum)
121 // default/initial entry (32 bytes) @ cat+32
122 d[cat+32]=0x88 as u8 // bootable
123 d[cat+33]=0x00 as u8 // boot media type 0 = no emulation
124 w16le(d, cat+34, 0) // load segment 0 -> 0x7C00
125 d[cat+36]=0x00 as u8 // system type
126 w16le(d, cat+38, BOOT_VSECS) // sector count (512-byte virtual sectors)
127 w32le(d, cat+40, LBA_BOOTIMG) // load RBA = boot image sector
128
129 // ---- boot image @ sector 20 (our real-mode boot sector) ----
130 author_boot(d, LBA_BOOTIMG * SEC)
131
132 // PERSIST
133 let fd: i64 = sys_openat_wr("knowledge/status/nishi_vultr.iso\x00" as *u8, 0x1a4)
134 if fd<=0 { ui_puts("RED: cannot write iso\n" as *u8); sys_exit(1); return 1 }
135 sys_write(fd, d, sz)
136 sys_close(fd)
137 ui_puts(" wrote knowledge/status/nishi_vultr.iso ("); ui_num(sz); ui_puts(" bytes)\n" as *u8)
138
139 // ---- parse-back KAT (verify the persisted bytes are El-Torito-correct) ----
140 // recompute the catalog checksum over the written bytes (must total 0)
141 var vsum: i64 = 0; var vi: i64=0
142 while vi < 16 { vsum = vsum + ((d[cat+vi*2] as i64) | ((d[cat+vi*2+1] as i64)<<8)); vi = vi + 1 }
143 let cksum_ok: i64 = (vsum & 0xFFFF)
144
145 var pass: i64=0
146 var ttl: i64=0
147 ttl=ttl+1; ui_puts(" T1 PVD: type 1 + 'CD001' @ sector 16: " as *u8); if (d[pvd] as i64)==1 { if streq(d, pvd+1, "CD001" as *u8, 5)==1 { pass=pass+1; ui_puts("PASS\n" as *u8) } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) }
148 ttl=ttl+1; ui_puts(" T2 Boot Record VD: type 0 + 'EL TORITO SPECIFICATION' + catalog ptr==19: " as *u8); if (d[br] as i64)==0 { if streq(d, br+7, "EL TORITO SPECIFICATION" as *u8, 23)==1 { if r32le(d, br+71)==LBA_CATALOG { pass=pass+1; ui_puts("PASS\n" as *u8) } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) }
149 ttl=ttl+1; ui_puts(" T3 Terminator: type 255 + 'CD001' @ sector 18: " as *u8); if (d[tm] as i64)==255 { if streq(d, tm+1, "CD001" as *u8, 5)==1 { pass=pass+1; ui_puts("PASS\n" as *u8) } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) }
150 ttl=ttl+1; ui_puts(" T4 Boot Catalog validation: header 0x01 + key 55AA + checksum totals 0: " as *u8); if (d[cat] as i64)==0x01 { if (d[cat+30] as i64)==0x55 { if (d[cat+31] as i64)==0xAA { if cksum_ok==0 { pass=pass+1; ui_puts("PASS\n" as *u8) } else { ui_puts("FAIL(cksum)\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) }
151 ttl=ttl+1; ui_puts(" T5 Default entry: bootable 0x88 + no-emul 0x00 + load RBA==20 + boot-sig 55AA: " as *u8); if (d[cat+32] as i64)==0x88 { if (d[cat+33] as i64)==0x00 { if r32le(d, cat+40)==LBA_BOOTIMG { if (d[LBA_BOOTIMG*SEC+510] as i64)==0x55 { pass=pass+1; ui_puts("PASS\n" as *u8) } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) } } else { ui_puts("FAIL\n" as *u8) }
152 // liar-kill: corrupt one validation byte -> checksum no longer totals 0
153 let dd: i64 = (d[cat+4] as i64); d[cat+4] = ((dd+1)&0xFF) as u8
154 var bsum: i64=0; var bi: i64=0; while bi<16 { bsum = bsum + ((d[cat+bi*2] as i64) | ((d[cat+bi*2+1] as i64)<<8)); bi=bi+1 }
155 d[cat+4] = dd as u8 // restore (so the file on disk stays valid)
156 ttl=ttl+1; ui_puts(" T6 liar-kill: corrupting the validation entry breaks the checksum total: " as *u8); if (bsum & 0xFFFF)!=0 { pass=pass+1; ui_puts("PASS\n" as *u8) } else { ui_puts("FAIL\n" as *u8) }
157
158 ui_puts("SPORE-EMIT-ISO-GATE passed " as *u8); ui_num(pass); ui_puts("/" as *u8); ui_num(ttl)
159 if pass==ttl { ui_puts(" verdict=GREEN (sovereign El Torito ISO, structure spec-correct; boots our real-mode banner on BIOS/KVM; real-SeaBIOS boot = Vultr/qemu validation)\n" as *u8); sys_exit(0); return 0 }
160 ui_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1
161}