Yu-Ping Wu has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/87226?usp=email )
Change subject: util/mediatek: Add check-pi-img.py ......................................................................
util/mediatek: Add check-pi-img.py
Add a script to perform validity check of the PI_IMG firmware.
Change-Id: I27011492c7fab747aa3ee12d514d20a6a52d0a4d Signed-off-by: Yu-Ping Wu yupingso@chromium.org --- M Documentation/util.md M util/README.md A util/mediatek/check-pi-img.py M util/mediatek/description.md 4 files changed, 38 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/26/87226/1
diff --git a/Documentation/util.md b/Documentation/util.md index 8a52557..6ac904a 100644 --- a/Documentation/util.md +++ b/Documentation/util.md @@ -88,6 +88,7 @@ * __[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 0253a71..099845d 100644 --- a/util/README.md +++ b/util/README.md @@ -79,6 +79,7 @@ * __[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 0000000..1587dbe --- /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 {}, {} 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 67ff390..568c8bb 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`