diff --git a/ci/afw_boards.py b/ci/afw_boards.py index 9d1c696..a31900c 100644 --- a/ci/afw_boards.py +++ b/ci/afw_boards.py @@ -44,6 +44,25 @@ AM335X_IMAGES = [ "format": "uimage", "partition": "mtd8", "install": True, "exact_bytes": None}, ] +# Tier-3 from-stock variant: same 12-partition NAND map, but mtd4 is written +# with our non-RSA U-Boot (a legacy uImage the preserved SPL chainloads), so its +# secure flag is False and it carries the u-boot image at write_order 3; the +# kernel moves to write_order 4 (U-Boot lands before the kernel). The SPL copies +# (mtd0-3) and bootenv (mtd5) stay preserved and are never write targets. +AM335X_STOCK_PARTITIONS = [] +for _p in AM335X_PARTITIONS: + _q = dict(_p) + if _q["name"] == "mtd4": + _q.update(secure=False, write_order=3, image="u-boot") + elif _q["name"] == "mtd7": + _q.update(write_order=4) + AM335X_STOCK_PARTITIONS.append(_q) + +AM335X_STOCK_IMAGES = [ + {"name": "u-boot", "source": "alkane-am335x-uboot-mtd4.uImage", "role": "bootloader", + "format": "uimage", "partition": "mtd4", "install": True, "exact_bytes": None}, +] + [dict(_img) for _img in AM335X_IMAGES] + BOARDS = { "am335x": { "arch": "arm", @@ -53,6 +72,14 @@ BOARDS = { "partitions": AM335X_PARTITIONS, "images": AM335X_IMAGES, }, + "am335x-stock": { + "arch": "arm", + "bootloader_policy": "include", + "buildable": True, + "format_checks": ["uimage", "uimage", "fdt", "uimage"], + "partitions": AM335X_STOCK_PARTITIONS, + "images": AM335X_STOCK_IMAGES, + }, "cv1835": { "arch": "arm64", "bootloader_policy": "preserve", diff --git a/ci/afw_manifest.py b/ci/afw_manifest.py index 244c841..690e41e 100755 --- a/ci/afw_manifest.py +++ b/ci/afw_manifest.py @@ -49,6 +49,15 @@ 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} +# Tier-3 from-stock: same map except mtd4 is a write target (our U-Boot). +AM335X_STOCK_MAP = dict(AM335X_MAP) +AM335X_STOCK_MAP["mtd4"] = (0x0080000, 0x1C0000, False, True, 3, "u-boot") +AM335X_STOCK_MAP["mtd7"] = (0x0280000, 0x500000, False, True, 4, "kernel") +AM335X_STOCK_TARGETS = {"u-boot": "mtd4", "kernel": "mtd7", "dtb": "mtd6", "rootfs": "mtd8"} +AM335X_STOCK_FORMATS = {"u-boot": "uimage", "kernel": "uimage", "dtb": "fdt", "rootfs": "uimage"} +AM335X_STOCK_CEILINGS = {"u-boot": 0x1C0000, "kernel": 0x500000, "dtb": 0x20000, "rootfs": 0x1400000} +# SPL copies (mtd0-3) and bootenv (mtd5) must never be written on either tier. +AM335X_NEVER_WRITE = {"mtd0", "mtd1", "mtd2", "mtd3", "mtd5"} def die(message): @@ -231,6 +240,76 @@ def validate_am335x(partitions, images): die("am335x partition sizes must sum to 0x7F80000") +def validate_am335x_stock(partitions, images): + """Reject the AM335x tier-3 (from-stock) brick paths before packing. + + This bundle DOES write mtd4 (our non-RSA U-Boot), so unlike the tier-2 + validator it permits a u-boot member - but it still guards every brick + path: the four install images are exactly u-boot/kernel/dtb/rootfs, mtd4 + carries the u-boot and no SPL copy is ever a write target, the 0x780000 + hole and the 12-partition geometry are exact, and no mtd0-3/mtd5 may hold + an image. + """ + installs = [image for image in images if image["install"]] + if len(installs) != 4: + die("am335x-stock must install exactly u-boot, kernel, dtb and rootfs") + if {image["name"] for image in installs} != set(AM335X_STOCK_TARGETS): + die("am335x-stock install images must be named u-boot, kernel, dtb and rootfs") + # The U-Boot member is expected here; a loose 'spl' member never is. + for image in images: + lowered = f"{image['name']} {image['file']}".lower() + if "spl" in lowered: + die(f"am335x-stock bundle carries an spl member: {image['file']!r}") + for image in installs: + name = image["name"] + if image["format"] != AM335X_STOCK_FORMATS[name]: + die(f"am335x-stock {name} format must be {AM335X_STOCK_FORMATS[name]}") + if image["partition"] != AM335X_STOCK_TARGETS[name]: + die(f"am335x-stock {name} must target {AM335X_STOCK_TARGETS[name]}") + if image["size"] > AM335X_STOCK_CEILINGS[name]: + die( + f"am335x-stock {name} is {image['size']} bytes, over the " + f"0x{AM335X_STOCK_CEILINGS[name]:x}-byte ceiling" + ) + if installs[[img["name"] for img in installs].index("u-boot")]["role"] != "bootloader": + die("am335x-stock u-boot image must have role bootloader") + seen = {} + for partition in partitions: + name = partition["name"] + if name not in AM335X_STOCK_MAP: + die(f"unknown am335x-stock partition {name!r}") + seen[name] = partition + offset, size, secure, boot_critical, write_order, image_name = AM335X_STOCK_MAP[name] + if int(partition["offset"]) != offset: + die(f"am335x-stock {name} offset must be 0x{offset:x}") + if int(partition["size"]) != size: + die(f"am335x-stock {name} size must be 0x{size:x}") + if bool(partition["secure"]) is not secure: + die(f"am335x-stock {name} secure flag is wrong") + if bool(partition["boot_critical"]) is not boot_critical: + die(f"am335x-stock {name} boot_critical flag is wrong") + if int(partition.get("write_order", 0)) != write_order: + die(f"am335x-stock {name} write_order must be {write_order}") + if partition.get("image") != image_name: + die(f"am335x-stock {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-stock {name} overlaps the 0x780000-0x800000 hole") + if name in AM335X_NEVER_WRITE and (write_order != 0 or partition.get("image") is not None): + die(f"am335x-stock {name} is preserved and must not be written") + if set(seen) != set(AM335X_STOCK_MAP): + die("am335x-stock manifest partition map is incomplete") + if int(seen["mtd7"]["offset"]) + int(seen["mtd7"]["size"]) != AM335X_HOLE[0]: + die("am335x-stock mtd7 must end at the 0x780000 hole") + if int(seen["mtd8"]["offset"]) != AM335X_HOLE[0] + AM335X_HOLE[1]: + die("am335x-stock 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-stock partition sizes must sum to 0x7F80000") + + def build(args): board = args.board if not afw_boards.known_board(board): @@ -341,6 +420,8 @@ def build(args): die("cv1835 p1 must be the 16 MiB padded FIT") elif board == "am335x": validate_am335x(partitions, images) + elif board == "am335x-stock": + validate_am335x_stock(partitions, images) policy = profile["bootloader_policy"] if policy == "preserve" and any(image["role"] == "bootloader" for image in images): diff --git a/ci/check-afw.py b/ci/check-afw.py index 90e392a..9acb054 100755 --- a/ci/check-afw.py +++ b/ci/check-afw.py @@ -42,6 +42,14 @@ 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} +# Tier-3 from-stock: same map, mtd4 written with our U-Boot; kernel at order 4. +AM335X_STOCK_MAP = dict(AM335X_MAP) +AM335X_STOCK_MAP["mtd4"] = (0x0080000, 0x1C0000, False, True, 3, "u-boot") +AM335X_STOCK_MAP["mtd7"] = (0x0280000, 0x500000, False, True, 4, "kernel") +AM335X_STOCK_TARGETS = {"u-boot": "mtd4", "kernel": "mtd7", "dtb": "mtd6", "rootfs": "mtd8"} +AM335X_STOCK_FORMATS = {"u-boot": "uimage", "kernel": "uimage", "dtb": "fdt", "rootfs": "uimage"} +AM335X_STOCK_CEILINGS = {"u-boot": 0x1C0000, "kernel": 0x500000, "dtb": 0x20000, "rootfs": 0x1400000} +AM335X_NEVER_WRITE = {"mtd0", "mtd1", "mtd2", "mtd3", "mtd5"} class AfwError(Exception): @@ -165,12 +173,15 @@ def check_format(manifest, image, 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": + if manifest["board"] in ("am335x", "am335x-stock"): require(len(payload) >= 64, f"{name} has no complete uImage header") - # ARM (2); kernel type 2, ramdisk type 3; gzip ramdisk. + # ih_arch (byte 29) ARM = 2; ih_type (byte 30): kernel 2, ramdisk 3, + # firmware (our U-Boot in mtd4) 5. 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") + elif image["name"] == "u-boot" or image["role"] == "bootloader": + require(payload[30] == 5, f"{name} must be a type-5 firmware (U-Boot) uImage") else: require(payload[30] == 2, f"{name} must be a type-2 kernel uImage") @@ -285,6 +296,77 @@ def validate_am335x(manifest, images, partitions, install_images): require(total == AM335X_CHIP - AM335X_HOLE[1], "am335x partition sizes must sum to 0x7F80000") +def validate_am335x_stock(manifest, images, partitions, install_images): + """Tier-3 from-stock gate: the bundle writes mtd4 (our U-Boot) but must + still never touch an SPL copy, and the geometry/ceilings/hole stay exact.""" + require(manifest["arch"] == "arm", "am335x-stock must be arm") + require( + manifest["bootloader_policy"] == "include", + "am335x-stock must declare bootloader_policy include", + ) + # A u-boot member is expected; a loose spl member never is. + for image in images: + lowered = f"{image['name']} {image['file']}".lower() + require("spl" not in lowered, f"am335x-stock bundle carries an spl member: {image['file']!r}") + require(len(install_images) == 4, "am335x-stock installs exactly u-boot, kernel, dtb and rootfs") + require( + {image["name"] for image in install_images} == set(AM335X_STOCK_TARGETS), + "am335x-stock install images must be named u-boot, kernel, dtb and rootfs", + ) + for image in install_images: + name = image["name"] + require( + image["format"] == AM335X_STOCK_FORMATS[name], + f"am335x-stock {name} format must be {AM335X_STOCK_FORMATS[name]}", + ) + require( + image["partition"] == AM335X_STOCK_TARGETS[name], + f"am335x-stock {name} must target {AM335X_STOCK_TARGETS[name]}", + ) + require( + image["size"] <= AM335X_STOCK_CEILINGS[name], + f"am335x-stock {name} ({image['size']} B) exceeds the " + f"0x{AM335X_STOCK_CEILINGS[name]:x}-byte ceiling", + ) + uboot = [image for image in install_images if image["name"] == "u-boot"][0] + require(uboot["role"] == "bootloader", "am335x-stock u-boot image must have role bootloader") + seen = {} + for partition in partitions: + name = partition["name"] + require(name in AM335X_STOCK_MAP, f"unknown am335x-stock partition {name!r}") + seen[name] = partition + offset, size, secure, boot_critical, write_order, image_name = AM335X_STOCK_MAP[name] + require(partition["offset"] == offset, f"am335x-stock {name} offset must be 0x{offset:x}") + require(partition["size"] == size, f"am335x-stock {name} size must be 0x{size:x}") + require(partition["secure"] is secure, f"am335x-stock {name} secure flag is wrong") + require( + partition["boot_critical"] is boot_critical, + f"am335x-stock {name} boot_critical flag is wrong", + ) + require( + partition["write_order"] == write_order, + f"am335x-stock {name} write_order must be {write_order}", + ) + require(partition.get("image") == image_name, f"am335x-stock {name} image must be {image_name!r}") + start = partition["offset"] + end = start + partition["size"] + if start < AM335X_HOLE[0] + AM335X_HOLE[1] and end > AM335X_HOLE[0]: + fail(f"am335x-stock {name} overlaps the 0x780000-0x800000 hole") + if name in AM335X_NEVER_WRITE and (write_order != 0 or partition.get("image") is not None): + fail(f"am335x-stock {name} is preserved and must not be written") + require(set(seen) == set(AM335X_STOCK_MAP), "am335x-stock manifest partition map is incomplete") + require( + seen["mtd7"]["offset"] + seen["mtd7"]["size"] == AM335X_HOLE[0], + "am335x-stock mtd7 must end at the 0x780000 hole", + ) + require( + seen["mtd8"]["offset"] == AM335X_HOLE[0] + AM335X_HOLE[1], + "am335x-stock mtd8 must start at 0x800000 after the hole", + ) + total = sum(part["size"] for part in partitions) + require(total == AM335X_CHIP - AM335X_HOLE[1], "am335x-stock 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": @@ -416,6 +498,9 @@ def validate(path, allow_unsigned=False, public_key=None, expect_board=None, pla if board == "am335x": validate_am335x(manifest, images, partitions, install_images) + if board == "am335x-stock": + validate_am335x_stock(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] diff --git a/ci/test-afw.py b/ci/test-afw.py index 4a94299..3a3194c 100755 --- a/ci/test-afw.py +++ b/ci/test-afw.py @@ -595,3 +595,87 @@ class Am335xAfwTests(unittest.TestCase): bundle, "--allow-unsigned", "--plan", str(plan_json) ) self.assertEqual(result.returncode, 0, result.stderr) + + +def am_stock_fixture_input(directory): + """The tier-3 from-stock input: adds our U-Boot (firmware uImage, type 5) + alongside the kernel/dtb/ramdisk.""" + am_fixture_input(directory) + (directory / "alkane-am335x-uboot-mtd4.uImage").write_bytes( + uimage_header(5, b"u-boot") + ) + return directory + + +def build_am_stock_afw(work, input_dir, extra=()): + output = work / "dist-am-stock" + result = run( + [ + "bash", str(MAKE), "--board", "am335x-stock", "--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 Am335xStockAfwTests(unittest.TestCase): + """Tier-3 from-stock bundle: writes U-Boot to mtd4 but must still refuse + every SPL write and a wrongly-typed U-Boot member.""" + + def setUp(self): + self.temp = Path(tempfile.mkdtemp(prefix="am335x-stock-afw-test.")) + self.input = am_stock_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_stock_bundle_passes_with_four_installs(self): + bundle = build_am_stock_afw(self.temp, self.input) + result = self.check(bundle, "--allow-unsigned", "--expect-board", "am335x-stock") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("afw: OK", result.stdout) + self.assertIn("bootloader include", result.stdout) + result = self.check(bundle, "--allow-unsigned", "--json") + self.assertEqual(result.returncode, 0, result.stderr) + installs = {item["image"]: item["partition"] for item in json.loads(result.stdout)["install"]} + self.assertEqual( + installs, {"u-boot": "mtd4", "kernel": "mtd7", "dtb": "mtd6", "rootfs": "mtd8"} + ) + + def test_u_boot_member_must_be_firmware_type(self): + # A U-Boot payload wrongly built as a type-2 kernel uImage must be caught. + (self.input / "alkane-am335x-uboot-mtd4.uImage").write_bytes(uimage_header(2, b"u-boot")) + result = run( + [ + "bash", str(MAKE), "--board", "am335x-stock", "--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("firmware", result.stderr + result.stdout) + + def test_making_an_spl_copy_a_write_target_is_refused(self): + bundle = build_am_stock_afw(self.temp, self.input) + + def edit(manifest, _work): + for part in manifest["partitions"]: + if part["name"] == "mtd0": + part["secure"] = False + part["write_order"] = 5 + part["image"] = "u-boot" + + bad = rewrite_bundle(bundle, self.temp / "spl-write.afw", edit_manifest=edit) + result = self.check(bad, "--allow-unsigned") + self.assertNotEqual(result.returncode, 0) + self.assertIn("mtd0", result.stderr)