302 lines
9.9 KiB
Lua
302 lines
9.9 KiB
Lua
-- xshow.lua -- run dwt/gui/build/xshow.coff through the COFF loader and report
|
|
-- whether the 1-bit XImage reached the screen.
|
|
--
|
|
-- Same debugger-assisted path as dwt/loader/loader.lua (no network, no rdebug,
|
|
-- no sudo): at frame XSHOW_AT it pokes the loader at bit 0x01880000, the COFF
|
|
-- at XSHOW_BUF (default 0x01900000), the descriptor at 0x01882C00, and sets
|
|
-- PC = 0x01880000. The loader copies the sections and calls create_process().
|
|
--
|
|
-- Env:
|
|
-- XSHOW_AT injection frame (default 2500)
|
|
-- XSHOW_END inspection/exit frame (default 2700)
|
|
-- XSHOW_SNAP frame to snapshot (default XSHOW_END)
|
|
-- XSHOW_COFF COFF (default dwt/gui/build/xshow.coff)
|
|
-- XSHOW_BIN loader payload (default dwt/loader/build/loader.bin)
|
|
-- XSHOW_BUF COFF buffer bit address (default 0x01900000)
|
|
-- XSHOW_OUT evidence dir (default dwt/gui/evidence)
|
|
-- XSHOW_NOSNAP 1 = do not write a PNG
|
|
--
|
|
-- Marker cells (the payload writes both blocks):
|
|
-- 01890100/120/140/160/180/1A0/1C0 xhello-compatible cells (heap; may be
|
|
-- overwritten after the app finishes)
|
|
-- 018D8000/20/40/60/80/A0/C0/E0 .bss mirror: stage, display, window,
|
|
-- gc, image, putret, syncret, strlen
|
|
|
|
local function hex(v, w) return string.format("%0" .. (w or 8) .. "X", v) end
|
|
|
|
local machine = manager.machine
|
|
local cpu = machine.devices[':maincpu']
|
|
local dbg = machine.debugger
|
|
local prog = cpu.spaces['program']
|
|
|
|
if not dbg then
|
|
print("XSHOW: debugger not enabled; pass -debug")
|
|
machine:exit()
|
|
return
|
|
end
|
|
|
|
local AT = tonumber(os.getenv("XSHOW_AT") or "2500")
|
|
local ENDF = tonumber(os.getenv("XSHOW_END") or "2700")
|
|
local SNAP = tonumber(os.getenv("XSHOW_SNAP") or tostring(ENDF))
|
|
local BIN = os.getenv("XSHOW_BIN") or "dwt/loader/build/loader.bin"
|
|
local COFF = os.getenv("XSHOW_COFF") or "dwt/gui/build/xshow.coff"
|
|
local BUF = tonumber(os.getenv("XSHOW_BUF") or "0x01900000")
|
|
local OUT = os.getenv("XSHOW_OUT") or "dwt/gui/evidence"
|
|
local NOSNAP = os.getenv("XSHOW_NOSNAP") == "1"
|
|
|
|
local LOADER_BASE = 0x01880000
|
|
local DESC = 0x01882C00
|
|
local RESULT = 0x01882D00
|
|
|
|
local function slurp(p)
|
|
local f, err = io.open(p, "rb")
|
|
if not f then error("cannot open " .. p .. ": " .. tostring(err)) end
|
|
local s = f:read("*a")
|
|
f:close()
|
|
return s
|
|
end
|
|
|
|
local function u16(s, o)
|
|
return s:byte(o + 1) + s:byte(o + 2) * 256
|
|
end
|
|
|
|
local function u32(s, o)
|
|
return s:byte(o + 1) + s:byte(o + 2) * 256
|
|
+ s:byte(o + 3) * 65536 + s:byte(o + 4) * 16777216
|
|
end
|
|
|
|
local function parse_coff(d)
|
|
local c = { data = d }
|
|
c.magic = u16(d, 0)
|
|
c.nscns = u16(d, 2)
|
|
c.opthdr = u16(d, 16)
|
|
c.sections = {}
|
|
if c.opthdr > 0 then
|
|
c.optmagic = u16(d, 20)
|
|
c.entry = u32(d, 20 + 16)
|
|
c.textstart = u32(d, 20 + 20)
|
|
c.datastart = u32(d, 20 + 24)
|
|
end
|
|
local base = 20 + c.opthdr
|
|
for i = 0, c.nscns - 1 do
|
|
local o = base + i * 40
|
|
local name = d:sub(o + 1, o + 8):gsub("%z", "")
|
|
c.sections[#c.sections + 1] = {
|
|
name = name,
|
|
paddr = u32(d, o + 8),
|
|
size = u32(d, o + 16), -- bits
|
|
scnptr = u32(d, o + 20),
|
|
flags = u32(d, o + 36),
|
|
}
|
|
end
|
|
return c
|
|
end
|
|
|
|
local function poke_bytes(bitaddr, data)
|
|
local n = math.floor((#data + 1) / 2)
|
|
for i = 0, n - 1 do
|
|
local lo = data:byte(i * 2 + 1) or 0
|
|
local hi = data:byte(i * 2 + 2) or 0
|
|
prog:write_u16(bitaddr + i * 16, lo + hi * 256)
|
|
end
|
|
return n
|
|
end
|
|
|
|
local function verify_bytes(bitaddr, data)
|
|
local n = math.floor((#data + 1) / 2)
|
|
local bad = 0
|
|
for i = 0, n - 1 do
|
|
local lo = data:byte(i * 2 + 1) or 0
|
|
local hi = data:byte(i * 2 + 2) or 0
|
|
if prog:read_u16(bitaddr + i * 16) ~= lo + hi * 256 then
|
|
bad = bad + 1
|
|
end
|
|
end
|
|
return n, bad
|
|
end
|
|
|
|
local function w32(a, v) prog:write_u32(a, v) end
|
|
local function r32(a)
|
|
local ok, v = pcall(function() return prog:read_u32(a) end)
|
|
if ok then return v end
|
|
return 0xDEADBEEF
|
|
end
|
|
|
|
local loader = slurp(BIN)
|
|
local coff = slurp(COFF)
|
|
local app = parse_coff(coff)
|
|
|
|
print(string.format("XSHOW: payload %s (%d B) -> bit %s", BIN, #loader, hex(LOADER_BASE)))
|
|
print(string.format("XSHOW: COFF %s (%d B) -> bit %s", COFF, #coff, hex(BUF)))
|
|
print(string.format("XSHOW: file magic=%04X nscns=%d opthdr=%d entry=%s textstart=%s",
|
|
app.magic, app.nscns, app.opthdr, hex(app.entry or 0), hex(app.textstart or 0)))
|
|
for _, s in ipairs(app.sections) do
|
|
print(string.format("XSHOW: sec %-8s paddr=%s size=%d bits (%d B) flags=%#x",
|
|
s.name, hex(s.paddr), s.size, math.floor(s.size / 8), s.flags))
|
|
end
|
|
|
|
-- The .data bytes the probe will hand to XCreateImage, read out of the COFF.
|
|
for _, s in ipairs(app.sections) do
|
|
if s.name == ".data" and s.scnptr ~= 0 and s.size > 0 then
|
|
local nb = math.floor(s.size / 8)
|
|
local bytes = coff:sub(s.scnptr + 1, s.scnptr + nb)
|
|
local first = {}
|
|
for i = 1, math.min(16, #bytes) do first[#first + 1] = string.format("%02X", bytes:byte(i)) end
|
|
print(string.format("XSHOW: .data first %d bytes: %s", #first, table.concat(first, " ")))
|
|
end
|
|
end
|
|
|
|
local frames = 0
|
|
local injected = false
|
|
local inspected = false
|
|
local snapped = false
|
|
|
|
local function markers()
|
|
return {
|
|
stage = r32(0x01890140),
|
|
display = r32(0x01890100),
|
|
window = r32(0x01890120),
|
|
image = r32(0x01890160),
|
|
gc = r32(0x01890180),
|
|
putret = r32(0x018901A0),
|
|
syncret = r32(0x018901C0),
|
|
}
|
|
end
|
|
|
|
local function mirror()
|
|
return {
|
|
stage = r32(0x018D8000),
|
|
display = r32(0x018D8020),
|
|
window = r32(0x018D8040),
|
|
gc = r32(0x018D8060),
|
|
image = r32(0x018D8080),
|
|
putret = r32(0x018D80A0),
|
|
syncret = r32(0x018D80C0),
|
|
strlen = r32(0x018D80E0),
|
|
}
|
|
end
|
|
|
|
local function report(tag)
|
|
local m = markers()
|
|
local r = mirror()
|
|
print(string.format(
|
|
"XSHOW: %s cells stage=%s display=%s window=%s gc=%s image=%s putret=%s syncret=%s",
|
|
tag, hex(m.stage), hex(m.display), hex(m.window), hex(m.gc), hex(m.image),
|
|
hex(m.putret), hex(m.syncret)))
|
|
print(string.format(
|
|
"XSHOW: %s mirror stage=%s display=%s window=%s gc=%s image=%s putret=%s syncret=%s strlen=%s",
|
|
tag, hex(r.stage), hex(r.display), hex(r.window), hex(r.gc), hex(r.image),
|
|
hex(r.putret), hex(r.syncret), hex(r.strlen)))
|
|
end
|
|
|
|
-- Dump the XImage fields the ROM's own code reads (byte offsets are the
|
|
-- standard 32-bit XImage layout; 0x120/0x140/0x160 = depth/bpl/bpp).
|
|
local IMG_FIELDS = {
|
|
{ "width", 0x000 }, { "height", 0x020 }, { "xoffset", 0x040 },
|
|
{ "format", 0x060 }, { "data", 0x080 }, { "byte_order", 0x0A0 },
|
|
{ "bitmap_unit", 0x0C0 }, { "bitmap_bit_order", 0x0E0 },
|
|
{ "bitmap_pad", 0x100 }, { "depth", 0x120 }, { "bytes_per_line", 0x140 },
|
|
{ "bits_per_pixel", 0x160 },
|
|
}
|
|
|
|
local function dump_image(ptr, tag)
|
|
if ptr == 0 then return end
|
|
local parts = {}
|
|
for _, f in ipairs(IMG_FIELDS) do
|
|
parts[#parts + 1] = string.format("%s=%s", f[1], hex(r32(ptr + f[2])))
|
|
end
|
|
print(string.format("XSHOW: %s image@%s %s", tag, hex(ptr), table.concat(parts, " ")))
|
|
end
|
|
|
|
local function dump_dpy(ptr, tag)
|
|
if ptr == 0 then return end
|
|
print(string.format(
|
|
"XSHOW: %s dpy@%s byte_order=%s bitmap_unit=%s scanline_pad=%s bitmap_bit_order=%s",
|
|
tag, hex(ptr), hex(r32(ptr + 0x1A0)), hex(r32(ptr + 0x1C0)),
|
|
hex(r32(ptr + 0x1E0)), hex(r32(ptr + 0x200))))
|
|
-- XPutImage's bits_per_pixel check walks dpy->pixmap_formats: count at
|
|
-- +0x220, pointer at +0x240, entry stride 0x80 bits, depth at +0x20,
|
|
-- bits_per_pixel at +0x40.
|
|
local n = r32(ptr + 0x220)
|
|
local list = r32(ptr + 0x240)
|
|
print(string.format("XSHOW: %s pixmap_formats n=%d list=%s", tag, n, hex(list)))
|
|
for i = 0, math.min(n, 8) - 1 do
|
|
local e = list + i * 0x80
|
|
print(string.format("XSHOW: %s fmt[%d] depth=%s bits_per_pixel=%s pad=%s",
|
|
tag, i, hex(r32(e + 0x20)), hex(r32(e + 0x40)), hex(r32(e + 0x60))))
|
|
end
|
|
end
|
|
|
|
local function inject()
|
|
poke_bytes(LOADER_BASE, loader)
|
|
local _, bad = verify_bytes(LOADER_BASE, loader)
|
|
print(string.format("XSHOW: loader poked %d words, readback %d bad at %s",
|
|
math.floor((#loader + 1) / 2), bad, hex(LOADER_BASE)))
|
|
|
|
poke_bytes(BUF, coff)
|
|
local _, cbad = verify_bytes(BUF, coff)
|
|
print(string.format("XSHOW: COFF poked %d words, readback %d bad at %s",
|
|
math.floor((#coff + 1) / 2), cbad, hex(BUF)))
|
|
|
|
w32(DESC + 0x00, 0)
|
|
prog:write_u16(DESC + 0x00, 0x4C44) -- "LD"
|
|
prog:write_u16(DESC + 0x10, 0x5231) -- "R1"
|
|
w32(DESC + 0x20, BUF)
|
|
w32(DESC + 0x40, #coff)
|
|
w32(DESC + 0x60, 0) -- mode 0: debug-resume
|
|
print(string.format("XSHOW: descriptor @%s magic=%04X/%04X buf=%s len=%d mode=%d",
|
|
hex(DESC), prog:read_u16(DESC + 0x00), prog:read_u16(DESC + 0x10),
|
|
hex(r32(DESC + 0x20)), r32(DESC + 0x40), r32(DESC + 0x60)))
|
|
|
|
local orig_pc = cpu.state['PC'].value
|
|
w32(0x01882A20, orig_pc) -- LD_ORIG_PC
|
|
print(string.format("XSHOW: will resume at %s after create_process", hex(orig_pc)))
|
|
cpu.state['A8'].value = 0
|
|
cpu.state['PC'].value = LOADER_BASE
|
|
injected = true
|
|
print(string.format("XSHOW: frame %d: entered loader at %s (caller SP=%s)",
|
|
frames, hex(LOADER_BASE), hex(cpu.state['SP'].value)))
|
|
end
|
|
|
|
local function snapshot(path)
|
|
local screen = nil
|
|
for _, s in pairs(machine.screens) do screen = s break end
|
|
if not screen then
|
|
print("XSHOW: no screen device")
|
|
return
|
|
end
|
|
local ok, err = pcall(function() return screen:snapshot(path) end)
|
|
print(string.format("XSHOW: snapshot %s ok=%s %s", path, tostring(ok), tostring(err or "")))
|
|
end
|
|
|
|
emu.register_frame_done(function()
|
|
frames = frames + 1
|
|
if frames == AT and not injected then inject() end
|
|
if injected and not inspected and frames >= AT + 40 then
|
|
inspected = true
|
|
report("f" .. frames)
|
|
end
|
|
if injected and frames == SNAP and not snapped then
|
|
snapped = true
|
|
report("f" .. frames)
|
|
if not NOSNAP then snapshot(OUT .. "/xshow_frame" .. frames .. ".png") end
|
|
end
|
|
if frames >= ENDF then
|
|
if injected then
|
|
report("final")
|
|
local st = r32(RESULT + 0x000)
|
|
print(string.format("XSHOW: loader status=%d entry=%s text=%s/%d bits",
|
|
st, hex(r32(RESULT + 0x020)),
|
|
hex(r32(RESULT + 0x040)), r32(RESULT + 0x060)))
|
|
print(string.format("XSHOW: PC at exit = %s", hex(cpu.state['PC'].value)))
|
|
local m = markers()
|
|
dump_image(m.image, "final")
|
|
dump_dpy(m.display, "final")
|
|
end
|
|
machine:exit()
|
|
end
|
|
end, "vt1200-xshow")
|
|
|
|
dbg.visible_cpu = cpu
|
|
dbg.execution_state = 'run'
|