From f76270882211714db1d1a010c5ad294ad3e556de Mon Sep 17 00:00:00 2001 From: Yu-Ping Wu Date: Wed, 9 Apr 2025 07:50:09 +0800 Subject: [PATCH] util/mediatek: Add check-pi-img.py According to MediaTek's proprietary PI_IMG parser, two cookies (one header and one footer) are expected. Therefore, add a script to perform validity check of the PI_IMG firmware, so that format errors could be caught in build time. Change-Id: I27011492c7fab747aa3ee12d514d20a6a52d0a4d Signed-off-by: Yu-Ping Wu Reviewed-on: https://review.coreboot.org/c/coreboot/+/87226 Reviewed-by: Yidi Lin Tested-by: build bot (Jenkins) --- Documentation/util.md | 1 + util/README.md | 1 + util/mediatek/check-pi-img.py | 35 +++++++++++++++++++++++++++++++++++ util/mediatek/description.md | 1 + 4 files changed, 38 insertions(+) create mode 100755 util/mediatek/check-pi-img.py diff --git a/Documentation/util.md b/Documentation/util.md index ba4618fa69..2ba5fc1878 100644 --- a/Documentation/util.md +++ b/Documentation/util.md @@ -88,6 +88,7 @@ image for testing purposes and for working on firmware. `Bash` * __[me_cleaner](https://github.com/corna/me_cleaner)__ - Tool for partial deblobbing of Intel ME/TXE firmware images `Python` * __mediatek__ + * check-pi-img.py - Check `PI_IMG` firmware. `Python3` * gen-bl-img.py - Generate MediaTek bootloader header. `Python3` * __mma__ - Memory Margin Analysis automation tests `Bash` diff --git a/util/README.md b/util/README.md index 7c9394794a..dbb31110d9 100644 --- a/util/README.md +++ b/util/README.md @@ -79,6 +79,7 @@ image for testing purposes and for working on firmware. `Bash` * __[me_cleaner](https://github.com/corna/me_cleaner)__ - Tool for partial deblobbing of Intel ME/TXE firmware images `Python` * __mediatek__ + * check-pi-img.py - Check `PI_IMG` firmware. `Python3` * gen-bl-img.py - Generate MediaTek bootloader header. `Python3` * __mma__ - Memory Margin Analysis automation tests `Bash` diff --git a/util/mediatek/check-pi-img.py b/util/mediatek/check-pi-img.py new file mode 100755 index 0000000000..af15ee3a1f --- /dev/null +++ b/util/mediatek/check-pi-img.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# +# SPDX-License-Identifier: GPL-2.0-only + +import argparse + + +HEADER_OFFSET = 0x200 +COOKIE = 0x17C3A6B4 + + +def check_pi_img(pi_img): + cookie_count = 0 + with open(pi_img, "rb") as f: + f.seek(HEADER_OFFSET) + while True: + data = f.read(4) + if not data: + break + value = int.from_bytes(data, byteorder="little", signed=False) + if value == COOKIE: + cookie_count += 1 + if cookie_count != 2: + raise ValueError("Invalid PI_IMG {} (expected 2 cookies; found {})" + .format(pi_img, cookie_count)) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("pi_img") + args = parser.parse_args() + check_pi_img(args.pi_img) + +if __name__ == '__main__': + main() diff --git a/util/mediatek/description.md b/util/mediatek/description.md index 67ff39022b..568c8bbb1e 100644 --- a/util/mediatek/description.md +++ b/util/mediatek/description.md @@ -1,2 +1,3 @@ __mediatek__ + * check-pi-img.py - Check `PI_IMG` firmware. `Python3` * gen-bl-img.py - Generate MediaTek bootloader header. `Python3`