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): |
Manish V Badarkhe | 69b951e | 2024-02-20 13:44:13 +0000 | [diff] [blame] | 14 | def add_field_to_metadata(value, size): |
Manish V Badarkhe | 75a4f84 | 2024-01-25 13:38:34 +0000 | [diff] [blame] | 15 | # Write the integer values to file in little endian representation |
| 16 | with open(metadata_file, "ab") as fp: |
Manish V Badarkhe | 69b951e | 2024-02-20 13:44:13 +0000 | [diff] [blame] | 17 | fp.write(value.to_bytes(size, byteorder='little')) |
Manish V Badarkhe | 75a4f84 | 2024-01-25 13:38:34 +0000 | [diff] [blame] | 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 |
Manish V Badarkhe | 69b951e | 2024-02-20 13:44:13 +0000 | [diff] [blame] | 30 | add_field_to_metadata(2, 4) #fwu metadata version=2 |
| 31 | add_field_to_metadata(0, 4) #active_index=0 |
| 32 | add_field_to_metadata(0, 4) #previous_active_index=0 |
| 33 | add_field_to_metadata(116, 4) #metadata_size |
| 34 | add_field_to_metadata(32, 2) #desc_offset = 0x20 |
| 35 | add_field_to_metadata(0, 2) #reserved1 |
| 36 | add_field_to_metadata(252, 1) #Bank 0 - Accepted=0xFC |
| 37 | add_field_to_metadata(252, 1) #Bank 1 - Accepted=0xFC |
| 38 | add_field_to_metadata(255, 1) #Bank 2 - Invalid=0xFF |
| 39 | add_field_to_metadata(255, 1) #Bank 3 - Invalid=0xFF |
| 40 | add_field_to_metadata(0, 4) #reserved2 |
| 41 | |
| 42 | # fwu_fw_store_descriptor |
| 43 | add_field_to_metadata(2, 1) #num_banks |
| 44 | add_field_to_metadata(0, 1) #reserved |
| 45 | add_field_to_metadata(1, 2) #num_images |
| 46 | add_field_to_metadata(80, 2) #img_entry_size |
| 47 | add_field_to_metadata(24, 2) #bank_info_entry_size |
Manish V Badarkhe | 75a4f84 | 2024-01-25 13:38:34 +0000 | [diff] [blame] | 48 | |
| 49 | for img_type_uuid, location_uuid, img_uuids in image_data: |
| 50 | # Fill metadata image entry |
| 51 | add_uuid_to_metadata(img_type_uuid) # img_type_uuid |
| 52 | add_uuid_to_metadata(location_uuid) # location_uuid |
| 53 | |
Manish V Badarkhe | 69b951e | 2024-02-20 13:44:13 +0000 | [diff] [blame] | 54 | if len(img_uuids) <= 2: |
| 55 | for img_uuid in img_uuids: |
| 56 | # Fill metadata bank image info |
| 57 | add_uuid_to_metadata(img_uuid) # image unique bank_uuid |
| 58 | add_field_to_metadata(1, 4) # accepted=1 |
| 59 | add_field_to_metadata(0, 4) # reserved (MBZ) |
| 60 | else: |
| 61 | raise ValueError(f"img_uuids should have at most 2 elements, \ |
| 62 | but it has {len(img_uuids)} elements.") |
Manish V Badarkhe | 75a4f84 | 2024-01-25 13:38:34 +0000 | [diff] [blame] | 63 | |
| 64 | # Prepend CRC32 |
| 65 | with open(metadata_file, 'rb+') as fp: |
| 66 | content = fp.read() |
| 67 | crc = zlib.crc32(content) |
| 68 | fp.seek(0) |
| 69 | fp.write(crc.to_bytes(4, byteorder='little') + content) |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | parser = argparse.ArgumentParser() |
| 73 | |
| 74 | parser.add_argument('--metadata_file', required=True, |
| 75 | help='Output binary file to store the metadata') |
| 76 | parser.add_argument('--image_data', required=True, |
| 77 | help='image data in a format <img_type_uuid, \ |
| 78 | location_uuid, <img_id1, img_id2, ...>') |
| 79 | |
| 80 | args = parser.parse_args() |
| 81 | |
| 82 | # evaluated the string containing the python literals |
| 83 | image_data = ast.literal_eval(args.image_data) |
| 84 | if not isinstance(image_data, list): |
| 85 | raise argparse.ArgumentError("Invalid input format. \ |
| 86 | Please provide a valid list.") |
| 87 | |
| 88 | gen_fwu_metadata(args.metadata_file, image_data) |