import assert from 'node:assert/strict'; import {checkStorageComponent} from './k2150-native-check.mjs'; export const acceptanceScope='native-el0-storage'; export function checkProfile(){throw new Error('Full EL0, DMA, disk and embedded-program captures are required; use the K2150 storage matrix validator.');} const u=(b,i)=>b.readBigUInt64LE(i*8),R=1n<<63n; export function expectedCalls(caseId){ return [[9,16,0,R|1n],[9,17,0,1],[9,18,0,R|1n],[9,17,0,2],[9,1,0x2159,0x2159], [1,18,0,R|7n],[1,16,0,caseId===0?0:R|BigInt(caseId===1?5:110)],[1,17,0,1],[1,18,1,R|6n], [1,18,0,caseId===0?7:R|7n],[1,16,0,R|114n],[1,17,0,2],[1,1,0x2150,0x2150]]; } export function checkShutdownProfile(input){ const {caseId,stages,final,el0Final,programs}=input; const count=caseId===0?9:caseId===1?3:6,flushCount=caseId===1?3:6; const order=[1,200,...Array.from({length:flushCount},(_,i)=>101+i),201, ...Array.from({length:count-flushCount},(_,i)=>101+flushCount+i),2,3]; assert.deepEqual(stages.map(s=>s.stage),order); const native=checkStorageComponent({...input,stages:stages.filter(s=>![200,201].includes(s.stage))}); assert.equal(el0Final.length,2048);assert.deepEqual(el0Final,stages.at(-1).el0); const leased=u(stages[0].bytes,4),calls=expectedCalls(caseId),finished=stages.find(s=>s.stage===201).el0; for(const s of stages){ const b=s.el0;assert.equal(b.length,2048); const initial=s.stage===1,worker=s.stage===200,pending=s.stage>=101&&s.stage<=100+flushCount; const total=initial?0:worker?5:pending?7:13; assert.equal(u(b,0),0x4b32313530454c30n);assert.equal(u(b,1),BigInt(s.stage));assert.equal(u(b,2),BigInt(caseId)); assert.equal(u(b,3),leased);assert.equal(u(b,4),initial?0n:leased); assert.equal(u(b,5),total===13?leased:0n); for(const [index,value] of [[6,initial?0:0x2159],[7,initial?0:5],[8,0],[9,total===13?0x2150:0], [10,total===13?8:0],[11,0],[12,total===13?1:0],[13,total===13&&caseId===0?1:0], [14,total===13?(caseId===0?0:caseId===1?5:110):0],[15,total===13?flushCount:0],[16,total], [21,0],[22,initial?0:2],[23,total===13?2:0],[24,initial?0:2],[25,total>=7?1:0], [26,total===13?1:0],[27,total===13?1:0],[28,total===13&&caseId!==0?1:0],[29,total===13&&caseId===0?1:0], [30,0],[31,total===13?flushCount:0]])assert.equal(u(b,index),BigInt(value),'EL0 header '+index); for(const index of [17,18,19,20]){ const exists=index===17||index===19?total>=5:total>=7; if(exists){assert.equal(u(b,index),u(finished,index));assert.ok(u(b,index)>=0x40000000n&&u(b,index)%4096n===0n);} else assert.equal(u(b,index),0n); } for(let n=0;nassert.equal(u(b,base+i),BigInt(value),`SVC ${n+1} field ${i}`)); const elr=u(b,base+8),sp=u(b,base+9),spsr=u(b,base+10),esr=u(b,base+11); const program=programs[pid];assert.ok(Buffer.isBuffer(program)); assert.ok(elr>=0x80000004n&&elr<=0x80000000n+BigInt(Math.min(program.length,4096))&&elr%4n===0n); assert.equal(program.readUInt32LE(Number(elr-0x80000004n)),0xd4000001,'ELR follows actual embedded SVC #0'); assert.ok(sp>=0x80003000n&&sp<=0x80004000n&&sp%16n===0n); assert.equal(spsr&0xfn,0n);assert.equal(spsr&0x3c0n,0x3c0n); assert.equal(esr>>26n,0x15n);assert.equal(esr&0xffffn,0n); assert.equal(u(b,base+12),8n);assert.equal(u(b,base+13),BigInt(n+1)); assert.equal(u(b,base+14),0n);assert.equal(u(b,base+15),0n); // Every completed SVC is immutable across later captures. The flush // currently on the stack has no result until its real I/O returns. if(!(pending&&n===6))assert.deepEqual(b.subarray(base*8,(base+16)*8),finished.subarray(base*8,(base+16)*8)); else for(const i of [0,1,2,3,4,5,7,8,9,10,11,12,13,14,15])assert.equal(u(b,base+i),u(finished,base+i)); } for(let i=32+total*16;i<256;i++)assert.equal(u(b,i),0n,'EL0 unused word '+i); if(s.stage===200){assert.equal(u(s.bytes,12),0n,'unauthorized worker submitted no disk requests'); for(const page of s.dmaPages)assert.deepEqual(page.bytes,Buffer.alloc(4096));} if(s.stage===201)assert.equal(u(s.bytes,12),BigInt(flushCount),'PID1 completed exact storage sequence'); } const summary=input.serial.match(/K2150-EL0-STORAGE: WORKER_EXIT=0x([0-9a-f]{16}) PID1_EXIT=0x([0-9a-f]{16}) CALLS=0x([0-9a-f]{16}) CLEAN=0x([0-9a-f]{16})/); assert.ok(summary);assert.deepEqual(summary.slice(1).map(s=>BigInt('0x'+s)),[0x2159n,0x2150n,13n,caseId===0?1n:0n]); return {...native,scope:acceptanceScope,el0Calls:13,workerExit:0x2159,pid1Exit:0x2150,el0StorageVerified:true}; }