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`