saimaa: sblmain stack-pc override prop; sbl analysis tools

This commit is contained in:
SashegDev
2026-09-07 09:28:02 +00:00
parent 61cb6b9db3
commit 5f14a0eac5
4 changed files with 150 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""sbl_adr.py — поиск ADR (ADD/SUB pc) ссылок на интересные строки SBL1.
Использование: ./tools/sbl_adr.py fw/sbl/img0.elf
"""
import struct
import sys
d = open(sys.argv[1], "rb").read()
STRINGS = [b"SBL1, Start", b"SBL1, End", b"SBL1, Delta",
b"Sahara: Hello pkt sent", b"bl_power_on", b"sbl1_main"]
svas = {}
for s in STRINGS:
i = d.find(s)
if i < 0:
continue
# find which segment contains it
phoff = struct.unpack("<I", d[28:32])[0]
phnum = struct.unpack("<H", d[44:46])[0]
for k in range(phnum):
p = d[phoff + k * 32:phoff + (k + 1) * 32]
t, off, v, pa, fsz, msz, fl, al = struct.unpack("<IIIIIIII", p)
if t == 1 and off <= i < off + fsz:
svas[s] = v + (i - off)
for s, va in svas.items():
print(s.decode(), "VA:", hex(va))
phoff = struct.unpack("<I", d[28:32])[0]
phnum = struct.unpack("<H", d[44:46])[0]
segs = []
for k in range(phnum):
p = d[phoff + k * 32:phoff + (k + 1) * 32]
t, off, v, pa, fsz, msz, fl, al = struct.unpack("<IIIIIIII", p)
if t == 1 and fsz and (fl & 1 or True):
segs.append((off, v, fsz))
for s, va in svas.items():
print("--- refs to", s.decode(), hex(va))
n = 0
for off, v, sz in segs:
code = d[off:off + sz]
for j in range(0, len(code) - 4, 4):
x = struct.unpack("<I", code[j:j + 4])[0]
# ADD Rd,pc,#imm12 rotated; SUB Rd,pc,#imm
if (x & 0x0FFF0000) in (0x028F0000, 0x024F0000):
imm = x & 0xFFF
rot = (x & 0xF00) >> 7
imm = ((imm >> rot) | (imm << (32 - rot))) & 0xFFFFFFFF if rot else imm
tgt = (v + j + 8 + (imm if (x & 0x400000) else -imm)) & 0xFFFFFFFF
if abs(tgt - va) < 0x100:
print(" ", hex(v + j), "->", hex(tgt))
n += 1
if n > 6:
break