coreboot/util/mediatek/check-pi-img.py
Yu-Ping Wu f762708822 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 <yupingso@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/87226
Reviewed-by: Yidi Lin <yidilin@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2025-04-14 07:35:00 +00:00

35 lines
821 B
Python
Executable file

#!/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()