blob: 472f99f5b3f9d375e93b76edb63d32528802b5a9 [file] [log] [blame]
Manish V Badarkhe75a4f842024-01-25 13:38:34 +00001#!/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
7import argparse
8import uuid
9import zlib
10import os
11import ast
12
13def gen_fwu_metadata(metadata_file, image_data):
Manish V Badarkhe69b951e2024-02-20 13:44:13 +000014 def add_field_to_metadata(value, size):
Manish V Badarkhe75a4f842024-01-25 13:38:34 +000015 # Write the integer values to file in little endian representation
16 with open(metadata_file, "ab") as fp:
Manish V Badarkhe69b951e2024-02-20 13:44:13 +000017 fp.write(value.to_bytes(size, byteorder='little'))
Manish V Badarkhe75a4f842024-01-25 13:38:34 +000018
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 Badarkhe69b951e2024-02-20 13:44:13 +000030 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 Badarkhe75a4f842024-01-25 13:38:34 +000048
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 Badarkhe69b951e2024-02-20 13:44:13 +000054 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 Badarkhe75a4f842024-01-25 13:38:34 +000063
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
71if __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)