import fs from 'fs'; import crypto from 'crypto'; import { ESP32 } from '../web/js/soc.js'; const romDir = new URL('../web/rom/', import.meta.url).pathname; const soc = new ESP32({}); const flash = fs.readFileSync(process.argv[2]); soc.loadRoms(fs.readFileSync(romDir + 'esp32-v3-rom.bin'), fs.readFileSync(romDir + 'esp32-v3-rom-app.bin')); soc.setFlash(flash); soc.enablePsram(false); soc.onUart = (n, b) => process.stdout.write(String.fromCharCode(b)); soc.reset('power'); const sha = soc.sha; const ow = sha.write.bind(sha); let blocks = 0, calls = []; let bi=0; sha.write = (off, v) => { if ((off===0x90||off===0x94)) { const b=Buffer.alloc(64); for(let i=0;i<16;i++) b.writeInt32BE(sha.text[i],i*4); const exp=flash.subarray(0x10000+64*bi,0x10000+64*bi+64); if (!b.equals(exp)) { console.log('MISMATCH blk',bi, 'off',(0x10000+64*bi).toString(16)); console.log(b.toString('hex')); console.log(exp.toString('hex')); bi=1e9; } else bi++; } if (off >= 0x80) { calls.push(off.toString(16)); if (off === 0x98) { const dig = Buffer.alloc(32); for (let i = 0; i < 8; i++) dig.writeInt32BE(sha.text[i] === undefined ? 0 : sha.s256[i], i * 4); console.log('SHA256 LOAD after', calls.length, 'ops:', dig.toString('hex')); calls = []; } } return ow(off, v); }; let resets = 0; soc.requestReset = () => { console.log('RESET'); process.exit(0); }; for (let k = 0; k < 4000; k++) soc.run(100000); // expected const len = flash.readUInt32LE(0x10000 + 0 ) ; const segs = flash[0x10001]; console.log('app segs', segs); let off = 0x10000 + 24, end = off; for (let i = 0; i < segs; i++) { const l = flash.readUInt32LE(end + 4); end += 8 + l; } const pad = ((end + 1 + 15) & ~15) - 1; console.log('end', end.toString(16), 'checksum at', pad.toString(16)); const after = pad + 1; console.log('sha(image[0:after]) =', crypto.createHash('sha256').update(flash.subarray(0x10000, after)).digest('hex')); console.log('appended hash =', flash.subarray(after, after + 32).toString('hex'));