From 2d26003df79839d7f6b5e30eaa49e191dc9e6c87 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 8 Jul 2019 14:25:45 -0600 Subject: [PATCH] binman: Support locating an image header Add support for locating an image header in an image. Signed-off-by: Simon Glass --- tools/binman/etype/image_header.py | 23 +++++++++++++++++++++++ tools/binman/ftest.py | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/tools/binman/etype/image_header.py b/tools/binman/etype/image_header.py index b1c4f8a07e..8f9c5aa5d9 100644 --- a/tools/binman/etype/image_header.py +++ b/tools/binman/etype/image_header.py @@ -15,6 +15,29 @@ from entry import Entry import fdt_util IMAGE_HEADER_MAGIC = b'BinM' +IMAGE_HEADER_LEN = 8 + +def LocateHeaderOffset(data): + """Search an image for an image header + + Args: + data: Data to search + + Returns: + Offset of image header in the image, or None if not found + """ + hdr_pos = data.find(IMAGE_HEADER_MAGIC) + if hdr_pos != -1: + size = len(data) + hdr = data[hdr_pos:hdr_pos + IMAGE_HEADER_LEN] + if len(hdr) == IMAGE_HEADER_LEN: + offset = struct.unpack('