code wiki / _hdl_build / nx_nishi_bootimg.nx

nx_nishi_bootimg.nx source

↩ module page · 67 lines · 4673 B

1// nx_nishi_bootimg.nx -- x86 full-system ladder R9: a REAL BOOTABLE USB IMAGE (MBR boot sector). 2// Authors a genuine 512-byte master boot record: 16-bit real-mode boot code that prints a banner via 3// the BIOS teletype (INT 10h, AH=0x0E) then halts, padded to 510 bytes, ending in the 0x55AA boot 4// signature. Written to knowledge/status/nishi_boot.img -- a real image you could 5// dd if=nishi_boot.img of=/dev/sdX and boot on real x86 hardware (= R10). 6// KAT verifies the MBR structure (size 512, 0x55AA signature, boot code + banner present). 7// HONEST SCOPE: a minimal boot sector (print + halt via BIOS). A full loader (read more sectors, 8// load the R8 kernel, switch to long mode, run it) is the next R9 rung; running it = real BIOS/hw 9// (R10), since our sovereign emu does not model BIOS INT services. No persistent-hardware writes -- 10// this writes a FILE, not a device (Rule 26). expect_exit: 0 license_tier: ORIGINAL 11import "nx_syscalls.nx" 12 13func bi_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func bi_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 } 15func bi_b(c: *u8, o: i64, b: i64) -> i64 { c[o]=(b & 0xff) as u8; return o+1 } 16 17func main() -> i64 { 18 bi_puts("x86 full-system ladder R9: authoring a REAL bootable MBR (512B, 0x55AA)\n" as *u8) 19 let img: *u8 = sys_mmap(512) 20 // boot code loads at 0x7C00; offsets below are within-sector (0-based). 21 var o: i64 = 0 22 // mov si, 0x7C00+msg (BE imm16) -- patch imm after we know msg offset 23 o=bi_b(img,o,0xBE); let si_imm: i64=o; o=bi_b(img,o,0); o=bi_b(img,o,0) 24 let loop_off: i64 = o 25 o=bi_b(img,o,0xAC) // lodsb (AL=[SI], SI++) 26 o=bi_b(img,o,0x08); o=bi_b(img,o,0xC0) // or al, al 27 o=bi_b(img,o,0x74); let jz_imm: i64=o; o=bi_b(img,o,0) // jz hang (rel8, patch) 28 o=bi_b(img,o,0xB4); o=bi_b(img,o,0x0E) // mov ah, 0x0E (teletype) 29 o=bi_b(img,o,0xCD); o=bi_b(img,o,0x10) // int 0x10 30 o=bi_b(img,o,0xEB); o=bi_b(img,o, loop_off - (o+1)) // jmp loop 31 let hang_off: i64 = o 32 o=bi_b(img,o,0xF4) // hlt 33 o=bi_b(img,o,0xEB); o=bi_b(img,o, hang_off - (o+1)) // jmp hang 34 let msg_off: i64 = o 35 // banner "NISHIOS x86 boots\r\n" + NUL 36 let msg: *u8 = "NISHIOS x86 boots\x0D\x0A\x00" 37 var mi: i64=0 38 while msg[mi] != (0 as u8) { o=bi_b(img,o, msg[mi] as i64); mi=mi+1 } 39 o=bi_b(img,o,0) // NUL terminator for lodsb loop 40 // patch jz rel8 = hang_off - (jz_imm+1) 41 img[jz_imm] = ((hang_off - (jz_imm+1)) & 0xff) as u8 42 // patch mov si imm16 = 0x7C00 + msg_off 43 let abs: i64 = 0x7C00 + msg_off 44 img[si_imm] = (abs & 0xff) as u8 45 img[si_imm+1] = ((abs >> 8) & 0xff) as u8 46 // pad to 510, then the 0x55AA boot signature 47 while o < 510 { img[o]=0 as u8; o=o+1 } 48 o=bi_b(img,o,0x55) 49 o=bi_b(img,o,0xAA) 50 51 let fd: i64 = sys_openat_wr("knowledge/status/nishi_boot.img\x00" as *u8, 0x1a4) 52 if fd<=0 { bi_puts("R9 RED: cannot write image\n" as *u8); sys_exit(1); return 1 } 53 sys_write(fd, img, 512) 54 sys_close(fd) 55 bi_puts(" wrote knowledge/status/nishi_boot.img (512 bytes) -- dd to a USB + boot on x86 = R10\n" as *u8) 56 57 var pass: i64=0 58 var ttl: i64=0 59 ttl=ttl+1; bi_puts(" T1 image is exactly 512 bytes (one sector): " as *u8); if o==512 { pass=pass+1; bi_puts("PASS\n" as *u8) } else { bi_puts("FAIL\n" as *u8) } 60 ttl=ttl+1; bi_puts(" T2 boot signature 0x55 0xAA at 510/511: " as *u8); if img[510]==(0x55 as u8) { if img[511]==(0xAA as u8) { pass=pass+1; bi_puts("PASS\n" as *u8) } else { bi_puts("FAIL\n" as *u8) } } else { bi_puts("FAIL\n" as *u8) } 61 ttl=ttl+1; bi_puts(" T3 boot code present (starts with mov si, 0xBE): " as *u8); if img[0]==(0xBE as u8) { pass=pass+1; bi_puts("PASS\n" as *u8) } else { bi_puts("FAIL\n" as *u8) } 62 ttl=ttl+1; bi_puts(" T4 banner 'NISHIOS' embedded at msg offset " as *u8); bi_num(msg_off); bi_puts(": " as *u8); if img[msg_off]==(78 as u8) { if img[msg_off+1]==(73 as u8) { pass=pass+1; bi_puts("PASS\n" as *u8) } else { bi_puts("FAIL\n" as *u8) } } else { bi_puts("FAIL\n" as *u8) } 63 64 bi_puts("X86-BOOTIMG-GATE passed " as *u8); bi_num(pass); bi_puts("/" as *u8); bi_num(ttl) 65 if pass==ttl { bi_puts(" verdict=GREEN (a REAL bootable MBR exists; full loader + run on real hw = R9-2/R10)\n" as *u8); sys_exit(0); return 0 } 66 bi_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1 67}