"""Build the hardened boot image, nBkProOs.img.safe, from fuchs's CF boot image. python3 mkbootsafe.py [--install] [fuchs-image] [bootimg-dir] The safe and full PsionLX images boot fuchs@gibson's March 2005 CF image -- the one proven on this hardware -- with only its initial ramdisk changed. Every nBkProOs.img payload is kernel + stub + an 8 MB ext2 initrd, and the initrd's /linuxrc decides where root comes from. fuchs's linuxrc is Psion's with the search order swapped (card first, NAND second), and both still run NBLXLoad.sh, Psion's NAND installer, from the card's first partition if one is there. The hardened initrd: /linuxrc replaced by linuxrc.safe: mounts root from /dev/hda2 only, read-only, and never runs NBLXLoad.sh /linuxrc~ removed (Psion's NAND-first original, kept by fuchs) /bin/flash_eraseall, /bin/nandwrite removed jffs2.ko removed, and its line taken out of the 2.6.9 modules.dep (modules.dep.safe), so nothing can mount the NAND as JFFS2 The kernel and stub are copied byte for byte; scripts/nbkproimg.py rewrites the payload length and both checksums. The result goes to /nBkProOs.img.new and initrd-safe.ext2.new. If a shipped nBkProOs.img.safe is present the two are compared by content: the kernel bytes, and every file, mode and owner in the initrd. They cannot match byte for byte, because ext2 records the time each change was made. --install replaces the shipped pair with the new one; then put it on the boot partitions with python3 fatpatch.py ../bootimg/boot-safe.fat NBKPROOS.IMG ../bootimg/nBkProOs.img.safe """ import os, shutil, subprocess, sys, tempfile, filecmp HERE = os.path.dirname(os.path.abspath(__file__)) INSTALL = '--install' in sys.argv ARGS = [a for a in sys.argv[1:] if a != '--install'] sys.path.insert(0, HERE) from nbkproimg import verify, inflate, rebuild W = os.path.join(HERE, '..') # the project keeps it in PsionLX-CF-kit/; the release in 3-PSION-SOURCES/original-images/ FUCHS = ARGS[0] if len(ARGS) > 0 else next( (c for c in (os.path.join(W, '..', 'PsionLX-CF-kit', 'nBkProOs.img_cf'), os.path.join(W, '..', '3-PSION-SOURCES', 'original-images', 'nBkProOs.img_cf.fuchs-gibson-20050325')) if os.path.exists(c)), os.path.join(W, '..', 'PsionLX-CF-kit', 'nBkProOs.img_cf')) OUT = ARGS[1] if len(ARGS) > 1 else os.path.join(W, 'bootimg') E = os.environ.get('E2FS') or next((d for d in ('/opt/homebrew/opt/e2fsprogs/sbin', '/usr/local/opt/e2fsprogs/sbin', '/sbin', '/usr/sbin') if os.path.exists(os.path.join(d, 'debugfs'))), '/sbin') DEBUGFS, E2FSCK = os.path.join(E, 'debugfs'), os.path.join(E, 'e2fsck') INITRD_AT = 2533776 # fuchs's initrd begins here in the inflated payload INITRD_LEN = 8 * 1024 * 1024 KMOD = '/lib/modules/2.6.9-rc1-bk18-nb1' def sh(*cmd, inp=None): r = subprocess.run(cmd, input=inp, capture_output=True, text=True) return r.returncode, r.stdout + r.stderr def rdump(img, dest): os.makedirs(dest) sh(DEBUGFS, '-R', f'rdump / {dest}', img) def listing(img): """path -> (mode, uid, gid, size) for every inode, read with debugfs.""" out = {} def walk(d): _, txt = sh(DEBUGFS, '-R', f'ls -p {d}', img) for line in txt.splitlines(): f = line.split('/') # '', inode, mode, uid, gid, name, size, '' if len(f) < 7 or f[0] != '' or f[5] in ('.', '..'): continue path = d.rstrip('/') + '/' + f[5] out[path] = (int(f[2], 8), f[3], f[4], f[6]) if int(f[2], 8) & 0o170000 == 0o040000: walk(path) walk('/') return out def main(): img = open(FUCHS, 'rb').read() if not all(verify(img)): sys.exit(f'{FUCHS}: checksums do not verify -- not the image this was written for') blob = inflate(img) if len(blob) != INITRD_AT + INITRD_LEN or blob[INITRD_AT + 1080:INITRD_AT + 1082] != b'\x53\xef': sys.exit('the payload is not kernel + 8 MB ext2 initrd at the expected offset') kernel, initrd = blob[:INITRD_AT], blob[INITRD_AT:] tmp = tempfile.mkdtemp() cf = os.path.join(tmp, 'initrd-cf.ext2'); safe = os.path.join(tmp, 'initrd-safe.ext2') open(cf, 'wb').write(initrd); shutil.copy(cf, safe) for need in ('linuxrc.safe', 'modules.dep.safe'): if not os.path.exists(os.path.join(OUT, need)): sys.exit(f'missing {OUT}/{need}') script = f"""rm /linuxrc~ rm /bin/flash_eraseall rm /bin/nandwrite rm {KMOD}/kernel/fs/jffs2/jffs2.ko rm /linuxrc write {OUT}/linuxrc.safe /linuxrc sif /linuxrc mode 0100777 sif /linuxrc uid 0 sif /linuxrc gid 0 rm {KMOD}/modules.dep write {OUT}/modules.dep.safe {KMOD}/modules.dep sif {KMOD}/modules.dep mode 0100666 sif {KMOD}/modules.dep uid 0 sif {KMOD}/modules.dep gid 0 """ sh(DEBUGFS, '-w', '-f', '-', safe, inp=script) rc, txt = sh(E2FSCK, '-fn', safe) if rc != 0: sys.exit('hardened initrd does not fsck clean:\n' + txt) new = rebuild(img, kernel + open(safe, 'rb').read()) if not all(verify(new)) or inflate(new)[:INITRD_AT] != kernel: sys.exit('rebuilt image failed its own checks') dest = os.path.join(OUT, 'nBkProOs.img.new') open(dest, 'wb').write(new) shutil.copy(safe, os.path.join(OUT, 'initrd-safe.ext2.new')) shutil.copy(cf, os.path.join(OUT, 'initrd-cf.ext2')) print(f' {dest}: {len(new):,} bytes, checksums verify, kernel byte-identical') shipped = os.path.join(OUT, 'nBkProOs.img.safe') if os.path.exists(shipped): old = inflate(open(shipped, 'rb').read()) same_kernel = old[:INITRD_AT] == kernel olds = os.path.join(tmp, 'shipped.ext2'); open(olds, 'wb').write(old[INITRD_AT:]) a, b = os.path.join(tmp, 'a'), os.path.join(tmp, 'b') rdump(olds, a); rdump(safe, b) cmp = filecmp.dircmp(a, b) def differs(c): return bool(c.left_only or c.right_only or c.diff_files or c.funny_files) or \ any(differs(s) for s in c.subdirs.values()) same_files = not differs(cmp) same_meta = listing(olds) == listing(safe) print(f' against the shipped nBkProOs.img.safe: kernel {"identical" if same_kernel else "DIFFERS"}, ' f'initrd files {"identical" if same_files else "DIFFER"}, ' f'modes and owners {"identical" if same_meta else "DIFFER"}') if not (same_kernel and same_files and same_meta) and not INSTALL: sys.exit(1) shutil.rmtree(tmp) if INSTALL: os.replace(dest, shipped) os.replace(os.path.join(OUT, 'initrd-safe.ext2.new'), os.path.join(OUT, 'initrd-safe.ext2')) print(f' installed as {shipped}') if __name__ == '__main__': main()