1*bec4ebc2SBrad Bishop#!/usr/bin/env python3
2*bec4ebc2SBrad Bishop# Copyright (c) 2021, Arm Limited. All rights reserved.
3*bec4ebc2SBrad Bishop#
4*bec4ebc2SBrad Bishop# SPDX-License-Identifier: BSD-3-Clause
5*bec4ebc2SBrad Bishop
6*bec4ebc2SBrad Bishopimport argparse
7*bec4ebc2SBrad Bishopimport uuid
8*bec4ebc2SBrad Bishopimport zlib
9*bec4ebc2SBrad Bishop
10*bec4ebc2SBrad Bishopdef main(metadata_file, img_type_uuids, location_uuids, img_uuids):
11*bec4ebc2SBrad Bishop    def add_field_to_metadata(value):
12*bec4ebc2SBrad Bishop        # Write the integer values to file in little endian representation
13*bec4ebc2SBrad Bishop        with open(metadata_file, "ab") as fp:
14*bec4ebc2SBrad Bishop            fp.write(value.to_bytes(4, byteorder='little'))
15*bec4ebc2SBrad Bishop
16*bec4ebc2SBrad Bishop    def add_uuid_to_metadata(uuid_str):
17*bec4ebc2SBrad Bishop        # Validate UUID string and write to file in little endian representation
18*bec4ebc2SBrad Bishop        uuid_val = uuid.UUID(uuid_str)
19*bec4ebc2SBrad Bishop        with open(metadata_file, "ab") as fp:
20*bec4ebc2SBrad Bishop            fp.write(uuid_val.bytes_le)
21*bec4ebc2SBrad Bishop
22*bec4ebc2SBrad Bishop    # Fill metadata preamble
23*bec4ebc2SBrad Bishop    add_field_to_metadata(1) #version=1
24*bec4ebc2SBrad Bishop    add_field_to_metadata(0) #active_index=0
25*bec4ebc2SBrad Bishop    add_field_to_metadata(0) #previous_active_index=0
26*bec4ebc2SBrad Bishop
27*bec4ebc2SBrad Bishop    for img_type_uuid, location_uuid in zip(img_type_uuids, location_uuids):
28*bec4ebc2SBrad Bishop        # Fill metadata image entry
29*bec4ebc2SBrad Bishop        add_uuid_to_metadata(img_type_uuid) # img_type_uuid
30*bec4ebc2SBrad Bishop        add_uuid_to_metadata(location_uuid) # location_uuid
31*bec4ebc2SBrad Bishop
32*bec4ebc2SBrad Bishop        for img_uuid in img_uuids:
33*bec4ebc2SBrad Bishop            # Fill metadata bank image info
34*bec4ebc2SBrad Bishop            add_uuid_to_metadata(img_uuid) # image unique bank_uuid
35*bec4ebc2SBrad Bishop            add_field_to_metadata(1)       # accepted=1
36*bec4ebc2SBrad Bishop            add_field_to_metadata(0)       # reserved (MBZ)
37*bec4ebc2SBrad Bishop
38*bec4ebc2SBrad Bishop    # Prepend CRC32
39*bec4ebc2SBrad Bishop    with open(metadata_file, 'rb+') as fp:
40*bec4ebc2SBrad Bishop        content = fp.read()
41*bec4ebc2SBrad Bishop        crc = zlib.crc32(content)
42*bec4ebc2SBrad Bishop        fp.seek(0)
43*bec4ebc2SBrad Bishop        fp.write(crc.to_bytes(4, byteorder='little') + content)
44*bec4ebc2SBrad Bishop
45*bec4ebc2SBrad Bishopif __name__ == "__main__":
46*bec4ebc2SBrad Bishop    parser = argparse.ArgumentParser()
47*bec4ebc2SBrad Bishop    parser.add_argument('--metadata_file', required=True,
48*bec4ebc2SBrad Bishop                        help='Output binary file to store the metadata')
49*bec4ebc2SBrad Bishop    parser.add_argument('--img_type_uuids', type=str, nargs='+', required=True,
50*bec4ebc2SBrad Bishop                        help='A list of UUIDs identifying the image types')
51*bec4ebc2SBrad Bishop    parser.add_argument('--location_uuids', type=str, nargs='+', required=True,
52*bec4ebc2SBrad Bishop                        help='A list of UUIDs of the storage volumes where the images are located. '
53*bec4ebc2SBrad Bishop                             'Must have the same length as img_type_uuids.')
54*bec4ebc2SBrad Bishop    parser.add_argument('--img_uuids', type=str, nargs='+', required=True,
55*bec4ebc2SBrad Bishop                        help='A list UUIDs of the images in a firmware bank')
56*bec4ebc2SBrad Bishop
57*bec4ebc2SBrad Bishop    args = parser.parse_args()
58*bec4ebc2SBrad Bishop
59*bec4ebc2SBrad Bishop    if len(args.img_type_uuids) != len(args.location_uuids):
60*bec4ebc2SBrad Bishop        parser.print_help()
61*bec4ebc2SBrad Bishop        raise argparse.ArgumentError(None, 'Arguments img_type_uuids and location_uuids must have the same length.')
62*bec4ebc2SBrad Bishop
63*bec4ebc2SBrad Bishop    main(args.metadata_file, args.img_type_uuids, args.location_uuids, args.img_uuids)
64