code wiki / _hdl_build / nx_nishi_diskimg_test.nx

nx_nishi_diskimg_test.nx source

↩ module page · 53 lines · 3897 B

1// nx_nishi_diskimg_test.nx -- x86 ladder R9 rung-2: the LOADER PIPELINE (disk image -> load -> run). 2// A real loader's job: read the kernel off disk into memory and jump to it. This builds a multi- 3// sector image (sector 0 = MBR with the 0x55AA signature; sector 1 = the kernel payload), MODELS the 4// load (copy the kernel sector from the image into a load buffer = what BIOS INT 13h / a real loader 5// does), and RUNS the loaded kernel on the sovereign emu (emu_x86_run_k) -- proving the whole 6// image->load->execute pipeline sovereignly (no BIOS, no qemu). KAT: MBR signature, byte-faithful 7// load, and the loaded kernel exits 42. HONEST SCOPE: the 16-bit BIOS disk-read code itself + real 8// boot = R10 (hardware); this proves the load+run model. No persistent-hardware writes (Rule 26). 9// expect_exit: 0 license_tier: ORIGINAL 10import "nx_emu_x86_k.nx" 11 12func di_b(c: *u8, o: i64, b: i64) -> i64 { c[o]=(b & 0xff) as u8; return o+1 } 13func di_i32(c: *u8, o: i64, v: i64) -> i64 { c[o]=(v&0xff) as u8; c[o+1]=((v>>8)&0xff) as u8; c[o+2]=((v>>16)&0xff) as u8; c[o+3]=((v>>24)&0xff) as u8; return o+4 } 14func di_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func di_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 } 16func di_beq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } 17 18func main() -> i64 { 19 di_puts("x86 ladder R9 rung-2: LOADER PIPELINE (disk image -> load kernel -> run on sovereign emu)\n" as *u8) 20 let img: *u8 = sys_mmap(2048) 21 var i: i64=0 22 while i<2048 { img[i]=0 as u8; i=i+1 } 23 // sector 0 = MBR: a marker byte + the 0x55AA boot signature 24 img[0]=0xEB as u8 // (real MBRs start with a short jmp) 25 img[510]=0x55 as u8 26 img[511]=0xAA as u8 27 // sector 1 (offset 512) = the kernel payload: mov rdi,42 ; mov rax,60 ; syscall (exit 42) 28 var o: i64 = 512 29 o=di_b(img,o,0x48); o=di_b(img,o,0xC7); o=di_b(img,o,0xC7); o=di_i32(img,o,42) 30 o=di_b(img,o,0x48); o=di_b(img,o,0xC7); o=di_b(img,o,0xC0); o=di_i32(img,o,60) 31 o=di_b(img,o,0x0F); o=di_b(img,o,0x05) 32 let kstub_len: i64 = o - 512 33 34 // MODEL THE LOAD: copy the kernel sector from the image into a load buffer (what a loader does). 35 let load: *u8 = sys_mmap(4096) 36 var j: i64=0 37 while j<kstub_len { load[j]=img[512+j]; j=j+1 } 38 39 // RUN the loaded kernel on the sovereign emu. 40 let mem: *u8 = sys_mmap(65536) 41 let rc: i64 = emu_x86_run_k(load, kstub_len, mem) 42 di_puts(" kernel loaded from sector 1 (" as *u8); di_num(kstub_len); di_puts(" bytes), ran on the sovereign emu -> exit " as *u8); di_num(rc); di_puts("\n" as *u8) 43 44 var pass: i64=0 45 var ttl: i64=0 46 ttl=ttl+1; di_puts(" T1 MBR boot signature 0x55AA: " as *u8); if img[510]==(0x55 as u8) { if img[511]==(0xAA as u8) { pass=pass+1; di_puts("PASS\n" as *u8) } else { di_puts("FAIL\n" as *u8) } } else { di_puts("FAIL\n" as *u8) } 47 ttl=ttl+1; di_puts(" T2 load byte-faithful (load == image sector 1): " as *u8); if di_beq(load, ((img as i64)+512) as *u8, kstub_len)==1 { pass=pass+1; di_puts("PASS\n" as *u8) } else { di_puts("FAIL\n" as *u8) } 48 ttl=ttl+1; di_puts(" T3 loaded kernel executes (exit 42): " as *u8); if rc==42 { pass=pass+1; di_puts("PASS\n" as *u8) } else { di_puts("FAIL\n" as *u8) } 49 50 di_puts("X86-DISKIMG-GATE passed " as *u8); di_num(pass); di_puts("/" as *u8); di_num(ttl) 51 if pass==ttl { di_puts(" verdict=GREEN (image->load->run kernel proven sovereignly; 16-bit BIOS disk-read + real boot = R10)\n" as *u8); sys_exit(0); return 0 } 52 di_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1 53}