Manish V Badarkhe | 75a4f84 | 2024-01-25 13:38:34 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2024, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # Generate FWU metadata Version 1. |
| 6 | |
| 7 | import argparse |
| 8 | import uuid |
| 9 | import zlib |
| 10 | import os |
| 11 | import ast |
| 12 | |
| 13 | def gen_fwu_metadata(metadata_file, image_data): |
| 14 | def add_field_to_metadata(value): |
| 15 | # Write the integer values to file in little endian representation |
| 16 | with open(metadata_file, "ab") as fp: |
| 17 | fp.write(value.to_bytes(4, byteorder='little')) |
| 18 | |
| 19 | def add_uuid_to_metadata(uuid_str): |
| 20 | # Validate UUID string and write to file in little endian representation |
| 21 | uuid_val = uuid.UUID(uuid_str) |
| 22 | with open(metadata_file, "ab") as fp: |
| 23 | fp.write(uuid_val.bytes_le) |
| 24 | |
| 25 | # Delete the metadata_file if it exists |
| 26 | if os.path.exists(metadata_file): |
| 27 | os.remove(metadata_file) |
| 28 | |
| 29 | # Fill metadata preamble |
| 30 | add_field_to_metadata(1) #fwu metadata version=1 |
| 31 | add_field_to_metadata(0) #active_index=0 |
| 32 | add_field_to_metadata(0) #previous_active_index=0 |
| 33 | |
| 34 | for img_type_uuid, location_uuid, img_uuids in image_data: |
| 35 | # Fill metadata image entry |
| 36 | add_uuid_to_metadata(img_type_uuid) # img_type_uuid |
| 37 | add_uuid_to_metadata(location_uuid) # location_uuid |
| 38 | |
| 39 | for img_uuid in img_uuids: |
| 40 | # Fill metadata bank image info |
| 41 | add_uuid_to_metadata(img_uuid) # image unique bank_uuid |
| 42 | add_field_to_metadata(1) # accepted=1 |
| 43 | add_field_to_metadata(0) # reserved (MBZ) |
| 44 | |
| 45 | # Prepend CRC32 |
| 46 | with open(metadata_file, 'rb+') as fp: |
| 47 | content = fp.read() |
| 48 | crc = zlib.crc32(content) |
| 49 | fp.seek(0) |
| 50 | fp.write(crc.to_bytes(4, byteorder='little') + content) |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | parser = argparse.ArgumentParser() |
| 54 | |
| 55 | parser.add_argument('--metadata_file', required=True, |
| 56 | help='Output binary file to store the metadata') |
| 57 | parser.add_argument('--image_data', required=True, |
| 58 | help='image data in a format <img_type_uuid, \ |
| 59 | location_uuid, <img_id1, img_id2, ...>') |
| 60 | |
| 61 | args = parser.parse_args() |
| 62 | |
| 63 | # evaluated the string containing the python literals |
| 64 | image_data = ast.literal_eval(args.image_data) |
| 65 | if not isinstance(image_data, list): |
| 66 | raise argparse.ArgumentError("Invalid input format. \ |
| 67 | Please provide a valid list.") |
| 68 | |
| 69 | gen_fwu_metadata(args.metadata_file, image_data) |