From 95568d733966a546dcd8bf4fefc0668fcaaa9f5b Mon Sep 17 00:00:00 2001 From: Noah <2+noah@noreply.gitea.bluefla.re> Date: Wed, 4 Nov 2026 17:03:13 -0600 Subject: [PATCH] Emit and gate legacy-uImage AM335x .afw bundles --- ci/afw_boards.py | 52 +++++++++- ci/afw_manifest.py | 150 ++++++++++++++++++++++++----- ci/check-afw.py | 116 +++++++++++++++++++++- ci/test-afw.py | 234 +++++++++++++++++++++++++++++++++++++++++++++ docs/afw-format.md | 33 +++++++ 5 files changed, 556 insertions(+), 29 deletions(-) diff --git a/ci/afw_boards.py b/ci/afw_boards.py index b4e1e08..9d1c696 100644 --- a/ci/afw_boards.py +++ b/ci/afw_boards.py @@ -3,12 +3,56 @@ An .afw is valid for exactly one board family. The arch and bootloader policy come from this table, never from the caller alone, so a bundle cannot claim a -board it was not built for. Only cv1835 has a complete, buildable profile in -this release; the other Antminer families are listed so a wrong-board bundle -is rejected by name instead of being accepted as "unknown". +board it was not built for. cv1835 and am335x have complete, buildable profiles in this release; the +other Antminer families are listed so a wrong-board bundle is rejected by name +instead of being accepted as "unknown". """ +AM335X_PARTITIONS = [ + {"name": "mtd0", "device": "/dev/mtd0", "offset": 0x0000000, "size": 0x20000, + "secure": True, "boot_critical": True, "write_order": 0, "image": None}, + {"name": "mtd1", "device": "/dev/mtd1", "offset": 0x0020000, "size": 0x20000, + "secure": True, "boot_critical": True, "write_order": 0, "image": None}, + {"name": "mtd2", "device": "/dev/mtd2", "offset": 0x0040000, "size": 0x20000, + "secure": True, "boot_critical": True, "write_order": 0, "image": None}, + {"name": "mtd3", "device": "/dev/mtd3", "offset": 0x0060000, "size": 0x20000, + "secure": True, "boot_critical": True, "write_order": 0, "image": None}, + {"name": "mtd4", "device": "/dev/mtd4", "offset": 0x0080000, "size": 0x1C0000, + "secure": True, "boot_critical": True, "write_order": 0, "image": None}, + {"name": "mtd5", "device": "/dev/mtd5", "offset": 0x0240000, "size": 0x20000, + "secure": True, "boot_critical": True, "write_order": 0, "image": None}, + {"name": "mtd6", "device": "/dev/mtd6", "offset": 0x0260000, "size": 0x20000, + "secure": False, "boot_critical": True, "write_order": 2, "image": "dtb"}, + {"name": "mtd7", "device": "/dev/mtd7", "offset": 0x0280000, "size": 0x500000, + "secure": False, "boot_critical": True, "write_order": 3, "image": "kernel"}, + {"name": "mtd8", "device": "/dev/mtd8", "offset": 0x0800000, "size": 0x1400000, + "secure": False, "boot_critical": True, "write_order": 1, "image": "rootfs"}, + {"name": "mtd9", "device": "/dev/mtd9", "offset": 0x1C00000, "size": 0x200000, + "secure": True, "boot_critical": False, "write_order": 0, "image": None}, + {"name": "mtd10", "device": "/dev/mtd10", "offset": 0x1E00000, "size": 0x200000, + "secure": True, "boot_critical": False, "write_order": 0, "image": None}, + {"name": "mtd11", "device": "/dev/mtd11", "offset": 0x2000000, "size": 0x6000000, + "secure": True, "boot_critical": False, "write_order": 0, "image": None}, +] + +AM335X_IMAGES = [ + {"name": "kernel", "source": "alkane-am335x.uImage", "role": "kernel", + "format": "uimage", "partition": "mtd7", "install": True, "exact_bytes": None}, + {"name": "dtb", "source": "am335x-alkane.dtb", "role": "dtb", + "format": "fdt", "partition": "mtd6", "install": True, "exact_bytes": None}, + {"name": "rootfs", "source": "alkane-am335x-ramdisk.uImage", "role": "rootfs", + "format": "uimage", "partition": "mtd8", "install": True, "exact_bytes": None}, +] + BOARDS = { + "am335x": { + "arch": "arm", + "bootloader_policy": "preserve", + "buildable": True, + "format_checks": ["uimage", "fdt", "uimage"], + "partitions": AM335X_PARTITIONS, + "images": AM335X_IMAGES, + }, "cv1835": { "arch": "arm64", "bootloader_policy": "preserve", @@ -102,7 +146,7 @@ BOARDS = { "zynq-s17": {"arch": "armv7", "bootloader_policy": "include", "buildable": False}, "a113d": {"arch": "armv7", "bootloader_policy": "preserve", "buildable": False}, "a113d-s21pro": {"arch": "armv7", "bootloader_policy": "preserve", "buildable": False}, - "am335x-bb": {"arch": "armv7", "bootloader_policy": "include", "buildable": False}, + "am335x-bb": {"arch": "armv7", "bootloader_policy": "preserve", "buildable": False}, } SAFE_IMAGE_FORMATS = { diff --git a/ci/afw_manifest.py b/ci/afw_manifest.py index 03466f0..244c841 100755 --- a/ci/afw_manifest.py +++ b/ci/afw_manifest.py @@ -30,6 +30,26 @@ P1_LOAD = 0x100200000 GOLDEN_LBA_BYTES = 0x300000 * 512 REVISION = 40 +AM335X_MAP = { + "mtd0": (0x0000000, 0x20000, True, True, 0, None), + "mtd1": (0x0020000, 0x20000, True, True, 0, None), + "mtd2": (0x0040000, 0x20000, True, True, 0, None), + "mtd3": (0x0060000, 0x20000, True, True, 0, None), + "mtd4": (0x0080000, 0x1C0000, True, True, 0, None), + "mtd5": (0x0240000, 0x20000, True, True, 0, None), + "mtd6": (0x0260000, 0x20000, False, True, 2, "dtb"), + "mtd7": (0x0280000, 0x500000, False, True, 3, "kernel"), + "mtd8": (0x0800000, 0x1400000, False, True, 1, "rootfs"), + "mtd9": (0x1C00000, 0x200000, True, False, 0, None), + "mtd10": (0x1E00000, 0x200000, True, False, 0, None), + "mtd11": (0x2000000, 0x6000000, True, False, 0, None), +} +AM335X_HOLE = (0x780000, 0x80000) +AM335X_CHIP = 0x8000000 +AM335X_TARGETS = {"kernel": "mtd7", "dtb": "mtd6", "rootfs": "mtd8"} +AM335X_FORMATS = {"kernel": "uimage", "dtb": "fdt", "rootfs": "uimage"} +AM335X_CEILINGS = {"kernel": 0x500000, "dtb": 0x20000, "rootfs": 0x1400000} + def die(message): print(f"afw_manifest: error: {message}", file=sys.stderr) @@ -152,6 +172,65 @@ def signing_key(path): return private, public.hex() +def validate_am335x(partitions, images): + """Reject the AM335x brick paths before a bundle is packed.""" + installs = [image for image in images if image["install"]] + if len(installs) != 3: + die("am335x must install exactly kernel, dtb and rootfs") + if {image["name"] for image in installs} != set(AM335X_TARGETS): + die("am335x install images must be named kernel, dtb and rootfs") + for image in images: + lowered = f"{image['name']} {image['file']}".lower() + if "spl" in lowered or "u-boot" in lowered or "uboot" in lowered: + die(f"am335x bundle carries an spl/u-boot member: {image['file']!r}") + for image in installs: + name = image["name"] + if image["format"] != AM335X_FORMATS[name]: + die(f"am335x {name} format must be {AM335X_FORMATS[name]}") + if image["partition"] != AM335X_TARGETS[name]: + die(f"am335x {name} must target {AM335X_TARGETS[name]}") + if image["size"] > AM335X_CEILINGS[name]: + die( + f"am335x {name} is {image['size']} bytes, over the " + f"0x{AM335X_CEILINGS[name]:x}-byte ceiling" + ) + seen = {} + for partition in partitions: + name = partition["name"] + if name not in AM335X_MAP: + die(f"unknown am335x partition {name!r}") + seen[name] = partition + offset, size, secure, boot_critical, write_order, image_name = AM335X_MAP[name] + if int(partition["offset"]) != offset: + die(f"am335x {name} offset must be 0x{offset:x}") + if int(partition["size"]) != size: + die(f"am335x {name} size must be 0x{size:x}") + if bool(partition["secure"]) is not secure: + die(f"am335x {name} secure flag is wrong") + if bool(partition["boot_critical"]) is not boot_critical: + die(f"am335x {name} boot_critical flag is wrong") + if int(partition.get("write_order", 0)) != write_order: + die(f"am335x {name} write_order must be {write_order}") + if partition.get("image") != image_name: + die(f"am335x {name} image must be {image_name!r}") + start = int(partition["offset"]) + end = start + int(partition["size"]) + hole_start, hole_size = AM335X_HOLE + if start < hole_start + hole_size and end > hole_start: + die(f"am335x {name} overlaps the 0x780000-0x800000 hole") + if name in {f"mtd{index}" for index in range(0, 6)} and write_order != 0: + die(f"am335x {name} is preserved and must not be written") + if set(seen) != set(AM335X_MAP): + die("am335x manifest partition map is incomplete") + if int(seen["mtd7"]["offset"]) + int(seen["mtd7"]["size"]) != AM335X_HOLE[0]: + die("am335x mtd7 must end at the 0x780000 hole") + if int(seen["mtd8"]["offset"]) != AM335X_HOLE[0] + AM335X_HOLE[1]: + die("am335x mtd8 must start at 0x800000 after the hole") + total = sum(int(part["size"]) for part in partitions) + if total != AM335X_CHIP - AM335X_HOLE[1]: + die("am335x partition sizes must sum to 0x7F80000") + + def build(args): board = args.board if not afw_boards.known_board(board): @@ -260,6 +339,8 @@ def build(args): die("cv1835 must install exactly one image, p1") if installs[0]["size"] != 16 * 1024 * 1024: die("cv1835 p1 must be the 16 MiB padded FIT") + elif board == "am335x": + validate_am335x(partitions, images) policy = profile["bootloader_policy"] if policy == "preserve" and any(image["role"] == "bootloader" for image in images): @@ -288,6 +369,44 @@ def build(args): "public_key_hex": public_hex, } + if board == "am335x": + boot_switch = { + "method": "preserved-u-boot-go", + "detail": "mtd0-5 (SPL and our non-RSA U-Boot) are preserved and never " + "written. The update writes mtd8 root, mtd6 dtb and mtd7 kernel as " + "legacy ARM uImages, and the existing boot command loads them.", + } + preflight = { + "required_commands": ["flash_erase", "nandwrite", "nanddump", "md5sum", "sync"], + "forbidden_devices": [f"/dev/mtd{index}" for index in range(0, 6)], + "min_free_bytes": 32 * 1024 * 1024, + "source_os": ["alkane", "ouros", "braiins"], + "held_partitions": ["mtd8", "mtd6", "mtd7"], + "preserve_partitions": [f"mtd{index}" for index in range(0, 6)], + } + recovery = { + "network": "afw restore --host --held-backup --confirm FLASH", + "held_partition": "mtd8", + "held_partitions": ["mtd8", "mtd6", "mtd7"], + } + else: + boot_switch = { + "method": "partition-overwrite", + "detail": "Writing /dev/mmcblk0p1 replaces the FIT. The vendor boot0, " + "BL2, BL31 and U-Boot chain is preserved and never written.", + } + preflight = { + "required_commands": ["dd", "md5sum", "sync"], + "forbidden_devices": ["/dev/mmcblk0", "/dev/mmcblk0boot0", "/dev/mmcblk0boot1"], + "min_free_bytes": 32 * 1024 * 1024, + "source_os": ["stock-bitmain", "vnish", "braiins", "ouros", "alkane"], + } + recovery = { + "network": "afw restore --host --held-backup ", + "held_partition": "p1", + "golden_lba_bytes": GOLDEN_LBA_BYTES, + } + manifest = { "schema": SCHEMA, "format": "afw", @@ -314,30 +433,15 @@ def build(args): for item in partitions ], "images": images, - "boot_switch": { - "method": "partition-overwrite" - if board == "cv1835" - else "board-specific", - "detail": "Writing /dev/mmcblk0p1 replaces the FIT. The vendor boot0, " - "BL2, BL31 and U-Boot chain is preserved and never written." - if board == "cv1835" - else "See the board plan.", - }, - "preflight": { - "required_commands": ["dd", "md5sum", "sync"], - "forbidden_devices": ["/dev/mmcblk0", "/dev/mmcblk0boot0", "/dev/mmcblk0boot1"], - "min_free_bytes": 32 * 1024 * 1024, - "source_os": ["stock-bitmain", "vnish", "braiins", "ouros", "alkane"], - }, - "recovery": { - "network": "afw restore --host --held-backup ", - "held_partition": "p1", - "golden_lba_bytes": GOLDEN_LBA_BYTES if board == "cv1835" else None, - }, - "marker": (plan_document or {}).get("marker") or args.marker, + "boot_switch": boot_switch, + "preflight": preflight, + "recovery": recovery, + "marker": (plan_document or {}).get("marker") + or args.marker + or ("Alkane" if board == "am335x" else "ALKANE"), "kernel_version": (plan_document or {}).get("kernel_version") or args.kernel_version - or ("6.18.48" if board == "cv1835" else "unknown"), + or "6.18.48", } if args.notes: manifest["notes"] = args.notes @@ -368,7 +472,7 @@ def main(): parser.add_argument("--os-dir", default=".") parser.add_argument("--signing-key", help="raw 32-byte or 64-hex Ed25519 seed file") parser.add_argument("--kernel-version") - parser.add_argument("--marker", default="ALKANE") + parser.add_argument("--marker") parser.add_argument("--notes") args = parser.parse_args() try: diff --git a/ci/check-afw.py b/ci/check-afw.py index 8141b1b..90e392a 100755 --- a/ci/check-afw.py +++ b/ci/check-afw.py @@ -23,6 +23,26 @@ BLOCK = 512 MAX_MANIFEST = 64 * 1024 MAX_ARCHIVE = 512 * 1024 * 1024 +AM335X_MAP = { + "mtd0": (0x0000000, 0x20000, True, True, 0, None), + "mtd1": (0x0020000, 0x20000, True, True, 0, None), + "mtd2": (0x0040000, 0x20000, True, True, 0, None), + "mtd3": (0x0060000, 0x20000, True, True, 0, None), + "mtd4": (0x0080000, 0x1C0000, True, True, 0, None), + "mtd5": (0x0240000, 0x20000, True, True, 0, None), + "mtd6": (0x0260000, 0x20000, False, True, 2, "dtb"), + "mtd7": (0x0280000, 0x500000, False, True, 3, "kernel"), + "mtd8": (0x0800000, 0x1400000, False, True, 1, "rootfs"), + "mtd9": (0x1C00000, 0x200000, True, False, 0, None), + "mtd10": (0x1E00000, 0x200000, True, False, 0, None), + "mtd11": (0x2000000, 0x6000000, True, False, 0, None), +} +AM335X_HOLE = (0x780000, 0x80000) +AM335X_CHIP = 0x8000000 +AM335X_TARGETS = {"kernel": "mtd7", "dtb": "mtd6", "rootfs": "mtd8"} +AM335X_FORMATS = {"kernel": "uimage", "dtb": "fdt", "rootfs": "uimage"} +AM335X_CEILINGS = {"kernel": 0x500000, "dtb": 0x20000, "rootfs": 0x1400000} + class AfwError(Exception): pass @@ -131,7 +151,8 @@ def is_hex(value, length=64): ) -def check_format(fmt, payload, name): +def check_format(manifest, image, payload, name): + fmt = image["format"] if fmt == "fit": require(payload.startswith(b"\xd0\x0d\xfe\xed"), f"{name} is not a FIT") require(b"config@cv1835_miner_emmc" in payload, f"{name} has no default CV config") @@ -144,6 +165,14 @@ def check_format(fmt, payload, name): require(payload.startswith(b"\x1f\x8b"), f"{name} is not gzip-compressed") elif fmt == "uimage": require(payload.startswith(b"\x27\x05\x19\x56"), f"{name} is not a legacy uImage") + if manifest["board"] == "am335x": + require(len(payload) >= 64, f"{name} has no complete uImage header") + # ARM (2); kernel type 2, ramdisk type 3; gzip ramdisk. + require(payload[29] == 2, f"{name} is not an ARM uImage") + if image["name"] == "rootfs" or image["role"] == "rootfs": + require(payload[30] == 3, f"{name} must be a type-3 ramdisk uImage") + else: + require(payload[30] == 2, f"{name} must be a type-2 kernel uImage") def verify_signature(manifest_bytes, manifest, signature_member, public_key): @@ -176,6 +205,86 @@ def verify_signature(manifest_bytes, manifest, signature_member, public_key): return "ed25519" +def validate_am335x(manifest, images, partitions, install_images): + require(manifest["arch"] == "arm", "am335x must be arm") + require( + manifest["bootloader_policy"] == "preserve", + "am335x must preserve mtd0-5", + ) + for image in images: + lowered = f"{image['name']} {image['file']}".lower() + require( + "spl" not in lowered and "u-boot" not in lowered and "uboot" not in lowered, + f"am335x bundle carries an spl/u-boot member: {image['file']!r}", + ) + require(len(install_images) == 3, "am335x installs exactly kernel, dtb and rootfs") + require( + {image["name"] for image in install_images} == set(AM335X_TARGETS), + "am335x install images must be named kernel, dtb and rootfs", + ) + for image in install_images: + name = image["name"] + require( + image["format"] == AM335X_FORMATS[name], + f"am335x {name} format must be {AM335X_FORMATS[name]}", + ) + require( + image["partition"] == AM335X_TARGETS[name], + f"am335x {name} must target {AM335X_TARGETS[name]}", + ) + require( + image["size"] <= AM335X_CEILINGS[name], + f"am335x {name} ({image['size']} B) exceeds the " + f"0x{AM335X_CEILINGS[name]:x}-byte mtd ceiling", + ) + seen = {} + for partition in partitions: + name = partition["name"] + require(name in AM335X_MAP, f"unknown am335x partition {name!r}") + if name not in AM335X_MAP: + continue + seen[name] = partition + offset, size, secure, boot_critical, write_order, image_name = AM335X_MAP[name] + require(partition["offset"] == offset, f"am335x {name} offset must be 0x{offset:x}") + require(partition["size"] == size, f"am335x {name} size must be 0x{size:x}") + require(partition["secure"] is secure, f"am335x {name} secure flag is wrong") + require( + partition["boot_critical"] is boot_critical, + f"am335x {name} boot_critical flag is wrong", + ) + require( + partition["write_order"] == write_order, + f"am335x {name} write_order must be {write_order}", + ) + require( + partition.get("image") == image_name, + f"am335x {name} image must be {image_name!r}", + ) + start = partition["offset"] + end = start + partition["size"] + hole_start, hole_size = AM335X_HOLE + require( + not (start < hole_start + hole_size and end > hole_start), + f"am335x {name} overlaps the 0x780000-0x800000 hole", + ) + if name in {f"mtd{index}" for index in range(0, 6)}: + require( + partition["write_order"] == 0 and partition.get("image") is None, + f"am335x {name} is preserved and must not be written", + ) + require(set(seen) == set(AM335X_MAP), "am335x manifest partition map is incomplete") + require( + seen["mtd7"]["offset"] + seen["mtd7"]["size"] == AM335X_HOLE[0], + "am335x mtd7 must end at the 0x780000 hole", + ) + require( + seen["mtd8"]["offset"] == AM335X_HOLE[0] + AM335X_HOLE[1], + "am335x mtd8 must start at 0x800000 after the hole", + ) + total = sum(part["size"] for part in partitions) + require(total == AM335X_CHIP - AM335X_HOLE[1], "am335x partition sizes must sum to 0x7F80000") + + def validate(path, allow_unsigned=False, public_key=None, expect_board=None, plan_path=None): members = read_tar(Path(path)) if members[0][0] != "manifest.json": @@ -304,6 +413,9 @@ def validate(path, allow_unsigned=False, public_key=None, expect_board=None, pla f"{partition['name']} ({partition['size']} B)", ) + if board == "am335x": + validate_am335x(manifest, images, partitions, install_images) + if board == "cv1835": require(len(install_images) == 1 and install_images[0]["name"] == "p1", "cv1835 installs only p1") p1 = install_images[0] @@ -351,7 +463,7 @@ def validate(path, allow_unsigned=False, public_key=None, expect_board=None, pla require(len(payload) == image["size"], f"size mismatch for {name}") digest = hashlib.sha256(payload).hexdigest() require(digest == image["sha256"], f"sha256 mismatch for {name}") - check_format(image["format"], payload, name) + check_format(manifest, image, payload, name) summary = { "file": str(path), diff --git a/ci/test-afw.py b/ci/test-afw.py index 3a88332..4a94299 100755 --- a/ci/test-afw.py +++ b/ci/test-afw.py @@ -361,3 +361,237 @@ class AfwTests(unittest.TestCase): if __name__ == "__main__": unittest.main(verbosity=2) + + +AM_KERNEL_MAX = 0x500000 +AM_ROOT_MAX = 0x1400000 + + +def uimage_header(kind, payload=b"payload"): + header = bytearray(64) + header[0:4] = b"\x27\x05\x19\x56" + header[12:16] = len(payload).to_bytes(4, "big") + header[29] = 2 # ARM + header[30] = kind # 2 kernel, 3 ramdisk + header[31] = 0 if kind == 2 else 1 + header[32:48] = b"alkane-am335x\0\0\0" + return bytes(header) + payload + + +def am_fixture_input(directory): + directory.mkdir(parents=True, exist_ok=True) + (directory / "alkane-am335x.uImage").write_bytes(uimage_header(2, b"kernel")) + dtb = bytearray(2048) + dtb[0:4] = b"\xd0\x0d\xfe\xed" + (directory / "am335x-alkane.dtb").write_bytes(dtb) + (directory / "alkane-am335x-ramdisk.uImage").write_bytes( + uimage_header(3, b"ramdisk") + ) + return directory + + +def build_am_afw(work, input_dir, extra=()): + output = work / "dist-am" + result = run( + [ + "bash", + str(MAKE), + "--board", + "am335x", + "--version", + "0.1.0", + "--input", + str(input_dir), + "--output", + str(output), + "--sdk-revision", + REVISION, + "--os-revision", + REVISION, + "--allow-unsigned", + *extra, + ] + ) + if result.returncode != 0: + raise AssertionError(f"producer failed:\n{result.stdout}\n{result.stderr}") + files = list(output.glob("*.afw")) + if len(files) != 1: + raise AssertionError(f"expected one bundle, found {files}") + return files[0] + + +class Am335xAfwTests(unittest.TestCase): + def setUp(self): + self.temp = Path(tempfile.mkdtemp(prefix="am335x-afw-test.")) + self.input = am_fixture_input(self.temp / "sdk-out") + + def tearDown(self): + shutil.rmtree(self.temp, ignore_errors=True) + + def check(self, path, *extra): + return run([sys.executable, str(CHECK), str(path), *extra]) + + def test_good_bundle_passes_and_reports_three_installs(self): + bundle = build_am_afw(self.temp, self.input) + result = self.check(bundle, "--allow-unsigned", "--expect-board", "am335x") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("afw: OK", result.stdout) + self.assertIn("board am335x arch arm", result.stdout) + self.assertIn("bootloader preserve", result.stdout) + result = self.check(bundle, "--allow-unsigned", "--json") + self.assertEqual(result.returncode, 0, result.stderr) + summary = json.loads(result.stdout) + installs = {item["image"]: item["partition"] for item in summary["install"]} + self.assertEqual( + installs, {"kernel": "mtd7", "dtb": "mtd6", "rootfs": "mtd8"} + ) + + def test_oversize_kernel_is_refused_by_producer(self): + path = self.input / "alkane-am335x.uImage" + path.write_bytes(path.read_bytes() + b"\0" * (AM_KERNEL_MAX + 1)) + result = run( + [ + "bash", str(MAKE), "--board", "am335x", "--version", "0.1.0", + "--input", str(self.input), "--output", str(self.temp / "dist"), + "--sdk-revision", REVISION, "--os-revision", REVISION, + "--allow-unsigned", + ] + ) + self.assertNotEqual(result.returncode, 0) + self.assertIn("larger than", result.stderr) + + def test_oversize_root_is_refused_by_producer(self): + path = self.input / "alkane-am335x-ramdisk.uImage" + path.write_bytes(path.read_bytes() + b"\0" * (AM_ROOT_MAX + 1)) + result = run( + [ + "bash", str(MAKE), "--board", "am335x", "--version", "0.1.0", + "--input", str(self.input), "--output", str(self.temp / "dist"), + "--sdk-revision", REVISION, "--os-revision", REVISION, + "--allow-unsigned", + ] + ) + self.assertNotEqual(result.returncode, 0) + self.assertIn("larger than", result.stderr) + + def test_u_boot_member_under_preserve_is_refused(self): + bundle = build_am_afw(self.temp, self.input) + + def edit(manifest, work): + manifest["images"].append( + { + "name": "u-boot", + "file": "payload/u-boot.img", + "role": "component", + "format": "uimage", + "size": 4, + "sha256": __import__("hashlib").sha256(b"boot").hexdigest(), + "partition": None, + "install": False, + } + ) + (work / "payload" / "u-boot.img").write_bytes(b"boot") + + bad = rewrite_bundle(bundle, self.temp / "uboot.afw", edit_manifest=edit) + result = self.check(bad, "--allow-unsigned") + self.assertNotEqual(result.returncode, 0) + self.assertIn("u-boot", result.stderr) + + def test_manifest_cannot_claim_arm64(self): + bundle = build_am_afw(self.temp, self.input) + + def edit(manifest, _work): + manifest["arch"] = "arm64" + + bad = rewrite_bundle(bundle, self.temp / "wrong-arch.afw", edit_manifest=edit) + result = self.check(bad, "--allow-unsigned") + self.assertNotEqual(result.returncode, 0) + self.assertIn("must use arm", result.stderr) + + def test_mtd4_write_target_is_refused(self): + bundle = build_am_afw(self.temp, self.input) + + def edit(manifest, _work): + for part in manifest["partitions"]: + if part["name"] == "mtd4": + part["write_order"] = 1 + part["image"] = "kernel" + part["secure"] = False + + bad = rewrite_bundle(bundle, self.temp / "mtd4.afw", edit_manifest=edit) + result = self.check(bad, "--allow-unsigned") + self.assertNotEqual(result.returncode, 0) + self.assertIn("mtd4", result.stderr) + + def test_offset_in_the_0x780000_hole_is_refused(self): + bundle = build_am_afw(self.temp, self.input) + + def edit(manifest, _work): + for part in manifest["partitions"]: + if part["name"] == "mtd8": + part["offset"] = 0x780000 + + bad = rewrite_bundle(bundle, self.temp / "hole.afw", edit_manifest=edit) + result = self.check(bad, "--allow-unsigned") + self.assertNotEqual(result.returncode, 0) + self.assertIn("hole", result.stderr) + + def test_ramdisk_must_be_type_3(self): + (self.input / "alkane-am335x-ramdisk.uImage").write_bytes( + uimage_header(2, b"not-a-ramdisk") + ) + result = run( + [ + "bash", str(MAKE), "--board", "am335x", "--version", "0.1.0", + "--input", str(self.input), "--output", str(self.temp / "dist"), + "--sdk-revision", REVISION, "--os-revision", REVISION, + "--allow-unsigned", + ] + ) + self.assertNotEqual(result.returncode, 0) + self.assertIn("type-3 ramdisk", result.stderr) + + def test_truncated_tar_fails(self): + bundle = build_am_afw(self.temp, self.input) + truncated = self.temp / "truncated.afw" + truncated.write_bytes(bundle.read_bytes()[:100]) + result = self.check(truncated, "--allow-unsigned") + self.assertNotEqual(result.returncode, 0) + self.assertIn("truncated", result.stderr) + + def test_traversal_member_fails(self): + import io + + bundle = self.temp / "traversal.afw" + with tarfile.open(bundle, "w", format=tarfile.USTAR_FORMAT) as archive: + for name, data in ( + ("manifest.json", b"{}\n"), + ("payload/../escape", b"x"), + ): + info = tarfile.TarInfo(name) + info.size = len(data) + archive.addfile(info, io.BytesIO(data)) + result = self.check(bundle, "--allow-unsigned") + self.assertNotEqual(result.returncode, 0) + self.assertIn("traversal", result.stderr) + + def test_plan_json_cross_check(self): + sdk = Path("/home/noah/projects/alkane-sdk") + plan_yaml = sdk / "install" / "plans" / "am335x.yaml" + if not plan_yaml.is_file(): + self.skipTest("alkane-sdk am335x plan not present") + plan_json = self.temp / "am335x-plan.json" + result = run( + [ + sys.executable, str(sdk / "ci" / "check-plans.py"), + "--plan", str(plan_yaml), "--emit-json", str(plan_json), + ] + ) + self.assertEqual(result.returncode, 0, result.stderr) + bundle = build_am_afw( + self.temp, self.input, extra=("--plan", str(plan_json)) + ) + result = self.check( + bundle, "--allow-unsigned", "--plan", str(plan_json) + ) + self.assertEqual(result.returncode, 0, result.stderr) diff --git a/docs/afw-format.md b/docs/afw-format.md index 1cce730..60a42cf 100644 --- a/docs/afw-format.md +++ b/docs/afw-format.md @@ -144,3 +144,36 @@ preflights the board, holds the current p1 over the network, writes only the declared install partition, reads the whole partition back, and compares the hash before it reboots. No install step needs a serial console, an SD card, a U-Boot prompt, JTAG or a button. + +## AM335x (legacy-uImage, non-FIT) + +`am335x` is the BeagleBone-class AM335x slice. The board already runs our +non-RSA U-Boot, so the bundle preserves mtd0-5 (SPL, U-Boot, bootenv) and +writes only: + +| Region | Device | Size | Image | Order | +|---|---|---|---|---| +| `mtd8` | `/dev/mtd8` | 20 MiB | type-3 ramdisk uImage | 1 | +| `mtd6` | `/dev/mtd6` | 128 KiB | dtb (FDT) | 2 | +| `mtd7` | `/dev/mtd7` | 5 MiB | type-2 kernel uImage | 3 | + +`mtd0-5` are declared secure, image-free and outside the write order. Every +partition offset is taken from the 12-entry map, including the unmapped +512 KiB hole at `0x780000-0x800000`; mtd7 ends at `0x780000` and mtd8 starts +at `0x800000`, never `0x780000`. The producer and both checkers refuse an +offset in the hole, a write to mtd0-5, an image over its mtd ceiling, a +bundle carrying an spl/u-boot member, a wrong arch, an oversize image, a +truncated tar, or a traversal member. The images are legacy ARM uImages +(kernel type 2, ramdisk type 3); a FIT is rejected. + +```sh +alkane-os/ci/make-afw.sh \ + --board am335x --version 0.1.0 \ + --input alkane-sdk/build/out/am335x \ + --sdk-dir alkane-sdk --plan am335x-plan.json \ + --output alkane-os/dist --allow-unsigned + +python3 alkane-os/ci/check-afw.py \ + alkane-os/dist/alkane-os-0.1.0-am335x-arm.afw \ + --allow-unsigned --expect-board am335x +```