"""Refuse a PsionLX release that could damage a netBook Pro. python3 release-safety-check.py Exits non-zero, naming the file, if the release contains anything that can write or repartition a netBook Pro's internal NAND without the user meaning it to. This project erased one machine's Windows CE with a file that was sitting on a boot partition, so this check is mechanical and whitelisted, not a list of things remembered to leave out: * BooSt scripts (header "BStSnBk2", the file BooSt runs automatically from a card when it is named nbkpro.img). Each one in the release must be one of the five below, byte for byte in its commands, at that exact path. The LX prototype's script is allowed only under a name BooSt will never run. * Boot partitions -- every FAT16 image, and partition 1 of every disk image. Each must be byte-identical to one of the approved boot partitions, or hold only the expected files: no NBLXLoad.sh (Psion's NAND installer, which both Psion's and fuchs's initrds run if present), an nBkPro.img that only loads Linux, and no NAND command anywhere in its sectors -- deleted files and free space included. * Names that must never ship: psionlx-cf.img, step1-repartition. """ import hashlib, os, re, struct, sys ALLOWED_SCRIPTS = { '2-WINDOWS-CE-RESTORE/boost-scripts/stepA-dump/nbkpro.img': b'nand\r\n', '2-WINDOWS-CE-RESTORE/boost-scripts/stepB-repartition-540/nbkpro.img': b'nand partition 540\r\nnand\r\n', '2-WINDOWS-CE-RESTORE/boost-scripts/step2-upgrade/nbkpro.img': b'load nBkProBt.img run\r\nload nBkProCE.img\r\nnand -w ecc\r\nload nBkProPC.img\r\n' b'load /nand0-0/nBkProOS.img run\r\n', '2-WINDOWS-CE-RESTORE/boost-scripts/psionlx-autoboot/nbkpro.img': b'load nBkProOS.img run\r\n', '3-PSION-SOURCES/original-images/nBkPro.img.LX-PROTOTYPE-NAND-SCRIPT-DO-NOT-BOOT': b'nand -ignore erase 18 ee8\nnand partition e8\n', } AUTOBOOT = b'load nBkProOS.img run\r\n' FORBIDDEN_NAMES = re.compile(r'(^|/)(psionlx-cf\.img|step1-repartition)(/|$)', re.I) # A BooSt NAND command as it appears in a script: lowercase, one per line, hex # arguments -- so prose such as "mounts NAND partition 1" in a README is not one. NAND_CMD = re.compile(rb'(?:^|[\x00\r\n])[ \t]*nand[ \t]+(?:-\S+[ \t]+)*' rb'(?:partition[ \t]+[0-9a-fA-F]+|erase[ \t]+[0-9a-fA-F]+(?:[ \t]+[0-9a-fA-F]+)?|-w[ \t]+\S+)' rb'[ \t]*(?:\r?\n|\x00|$)') problems = [] def booster_payload(data): return data[256:].rstrip(b'\0') if data[8:16] == b'BStSnBk2' else None def check_fat(label, fat, approved): """fat: bytes of a FAT16 volume.""" if hashlib.sha256(fat).hexdigest() in approved: return 'approved' if fat[510:512] != b'\x55\xaa' or fat[54:59] != b'FAT16': return None bps, spc = struct.unpack_from('= 1 << 20: # a disk image: check partition 1 if it is FAT ptype = head[446 + 4] lba, count = struct.unpack_from('