summaryrefslogtreecommitdiff
path: root/Platform/ARM/Features/Fwu/GenFwuMetadata.py
blob: 1772559654388a14e5c59cee7ff05e176a403e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
## @file
# Generating Firmware Medata Data version 1 based on sepcification:
#    Platform Security Firmware Update for A-profile 1.0:
#        https://developer.arm.com/documentation/den0118/latest
#
# Copyright (c) 2024, Arm Limited. All rights reserved.<BR>
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
#  @par Reference(s):
#  - Platform Security Firmware Update for the A-profile Specification 1.0
#    (https://developer.arm.com/documentation/den0118/latest)
#
#  @par Glossary:
#    - FW          - Firmware
#    - FWU         - Firmware Update
#    - PSA         - Platform Security update for the A-profile specification
#    - IMG         - Image
#    - MM          - Management Mode
#    - PROP        - Property
#    - Bkup, Bkp   - Backup
#

import os
import sys
import argparse
import glob
import shutil
import struct
import uuid
import binascii
import zlib

#
# Global for help information
#
__prog__    = 'GenFwuMetadata.py'
__version__ = '0.1'
__description__ = 'Generate Firwmare update Metadata.\n'

class FwuImageBankInfoV2:
    #
    # struct fwu_image_bank_info_v2 {
    #    uuid_t img_guid;
    #    uint32_t accepted;
    #    uint8_t reserved_4[4];
    # }
    #
    _StructFormat = "<16sII"
    _StructSize = struct.calcsize(_StructFormat)

    def __init__(self, ImageGuid=None):
        self.ImageGuid = ImageGuid
        self.Accepted = 1
        self.Reserved4 = 0x00000000

    def Encode(self):
        FwuImageBankInfo = struct.pack(
                FwuImageBankInfoV2._StructFormat,
                self.ImageGuid.bytes_le,
                self.Accepted,
                self.Reserved4)

        return FwuImageBankInfo

    def Decode(self, Buffer):
        if len(Buffer) != FwuImageBankInfoV2._StructSize:
            raise ValueError

        (ImageGuid, Accepted, Reserved4) = \
                struct.unpack(
                        FwuImageBankInfoV2._StructFormat,
                        Buffer[0:FwuImageBankInfoV2._StructSize])

        if Reserved4 != 0x00000000:
            raise ValueError

        self.ImageGuid = uuid.UUID(bytes_le=ImageGuid)
        self.Accepted = Accepted

    def Dump(self):
        print("\t\t\timg_guid: {Guid}".format(Guid=str(self.ImageGuid)))
        print("\t\t\taccept: {Accpeted:d}".format(Accpeted=self.Accepted))
        print("\t\t\treserved_4: 0x{Reserved:08x}".format(Reserved=self.Reserved4))

class FwuImageEntryV2:
    #
    # struct fwu_image_entry_v2 {
    #    uuid_t img_type_guid;
    #    uuid_t location_guid;
    #    struct fwu_image_bank_info_v2 img_bank_info[];
    # }
    #
    _StructFormat = "<16s16s"
    _StructSize = struct.calcsize(_StructFormat)

    def __init__(self, ImageTypeGuid=None, LocationGuid=None, ImageBankInfoBuffer='b'):
        self.ImageTypeGuid = ImageTypeGuid
        self.LocationGuid = LocationGuid
        self.FwuImageBankInfo = ImageBankInfoBuffer

    def Encode(self):
        FwuImageEntry = struct.pack(
                FwuImageEntryV2._StructFormat,
                self.ImageTypeGuid.bytes_le,
                self.LocationGuid.bytes_le)

        return FwuImageEntry + self.FwuImageBankInfo

    def Decode(self, Buffer, NumBanks):
        EntrySize = FwuImageEntryV2._StructSize + (FwuImageBankInfoV2._StructSize * NumBanks)

        if len(Buffer) != EntrySize:
            raise ValueError

        (ImageTypeGuid, LocationGuid) = \
                struct.unpack(
                        FwuImageEntryV2._StructFormat,
                        Buffer[0:FwuImageEntryV2._StructSize])

        self.ImageTypeGuid = uuid.UUID(bytes_le=ImageTypeGuid)
        self.LocationGuid = uuid.UUID(bytes_le=LocationGuid)
        self.FwuImageBankInfo = Buffer[FwuImageEntryV2._StructSize:EntrySize]

    def Dump(self, NumBanks):
        print("\t\timg_type_guid: {Guid}".format(Guid=str(self.ImageTypeGuid)))
        print("\t\tlocation_guid: {Guid}".format(Guid=str(self.LocationGuid)))

        for Index in range(NumBanks):
            print("\t\tBank[{bank:d}]:".format(bank=Index))
            FwuImageBankInfo = FwuImageBankInfoV2()
            FwuImageBankInfo.Decode(
                    self.FwuImageBankInfo[
                        Index * FwuImageBankInfoV2._StructSize:
                        (Index + 1) * FwuImageBankInfoV2._StructSize
                        ])
            FwuImageBankInfo.Dump()

class FwuFwStoreDescV2:
    #
    # struct fwu_fw_store_desc_v2 {
    #     uint8_t num_banks;
    #     uint8_t reserved;
    #     uint16_t num_images;
    #     uint16_t img_entry_size;
    #     uint16_t bank_info_entry_size;
    #     struct fwu_image_entry_v2 img_entry[];
    # }
    #
    _StructFormat = "<BBHHH"
    _StructSize = struct.calcsize(_StructFormat)

    def __init__(self, NumBanks=2, ImagePerBank=1, ImageEntryBuffer='b'):
        self.NumBanks = NumBanks
        self.Reserved = 0x00
        self.NumImages = ImagePerBank
        self.ImageEntrySize = FwuImageEntryV2._StructSize +(FwuImageBankInfoV2._StructSize * NumBanks)
        self.BankInfoEntrySize = FwuImageBankInfoV2._StructSize
        self.ImageEntry = ImageEntryBuffer

    def Encode(self):
        FwuFwStoreDesc = struct.pack(
                FwuFwStoreDescV2._StructFormat,
                self.NumBanks,
                self.Reserved,
                self.NumImages,
                self.ImageEntrySize,
                self.BankInfoEntrySize)

        return FwuFwStoreDesc + self.ImageEntry

    def Decode(self, Buffer):
        if len(Buffer) < FwuFwStoreDescV2._StructSize:
            return ValueError

        (NumBanks, Reserved, NumImages, ImageEntrySize, BankInfoEntrySize) = \
                struct.unpack(
                        FwuFwStoreDescV2._StructFormat,
                        Buffer[0:FwuFwStoreDescV2._StructSize])

        DescSize = FwuFwStoreDescV2._StructSize + \
                (NumImages * (FwuImageEntryV2._StructSize + \
                (FwuImageBankInfoV2._StructSize * NumBanks)))

        if len(Buffer) != DescSize:
            raise ValueError

        self.NumBanks = NumBanks
        self.Reserved = Reserved
        self.NumImages = NumImages
        self.ImageEntrySize = ImageEntrySize
        self.BankInfoEntrySize = BankInfoEntrySize
        self.ImageEntry = Buffer[FwuFwStoreDescV2._StructSize:DescSize]

    def Dump(self):
        print("\tnum_banks: {NumBanks:d}".format(NumBanks=self.NumBanks))
        print("\treserved: 0x{Reserved:02x}".format(Reserved=self.Reserved))
        print("\tnum_images: {NumImages:d}".format(NumImages=self.NumImages))
        print("\timg_entry_size: {ImageEntrySize}".format(ImageEntrySize=str(self.ImageEntrySize)))
        print("\tbank_info_entry_size: {BankInfoEntrySize}".format(BankInfoEntrySize=str(self.BankInfoEntrySize)))

        print("\tIMAGE_ENTRIES:")
        for Index in range(self.NumImages):
            print("\tIMAGE[{image:d}]:".format(image=Index))
            FwuImageEntry = FwuImageEntryV2()
            FwuImageEntry.Decode(
                    self.ImageEntry[
                        Index * self.ImageEntrySize:
                        (Index + 1) * self.ImageEntrySize
                        ], self.NumBanks)
            FwuImageEntry.Dump(self.NumBanks)

class FwuMetadataV2:
    #
    # struct fwu_metadata_v2 {
    #    uint32_t crc_32;
    #    uint32_t version;
    #    uint32_t active_index;
    #    uint32_t previous_active_index;
    #    uint32_t metadata_size;
    #    uint16_t descriptor_offset;
    #    uint16_t reserved_2;
    #    uint8_t  bank_state[4];
    #    uint32_t reserved_4;
    #    struct fwu_fw_store_desc_v2 fw_store_desc;
    # }
    #
    _StructFormat = "<IIIIIHH4sI"
    _StructSize = struct.calcsize(_StructFormat)

    def __init__(self, FwuFwStoreDescBuffer=b''):
        self.Crc32 = 0x00
        self.Version = 0x00000002
        self.MetadataSize = 0x00;
        self.ActiveIndex = 0x00
        self.PreviousActiveIndex = 0x01
        self.DescriptorOffset = 0x20;
        self.Reserved2 = 0x0000;
        self.BankState = [ 0xFF, 0xFF, 0xFF, 0xFF ]
        self.Reserved4 = 0x00000000
        self.FwuFwStoreDesc = FwuFwStoreDescBuffer

    def Encode(self, NumBanks=2):
        for Index in range(NumBanks):
            self.BankState[Index] = 0xFC;

        self.MetadataSize = FwuMetadataV2._StructSize + len(self.FwuFwStoreDesc)
        BankState = struct.pack(
                "<BBBB",
                self.BankState[0],
                self.BankState[1],
                self.BankState[2],
                self.BankState[3])

        FwuMetadata = struct.pack(
                FwuMetadataV2._StructFormat,
                self.Crc32,
                self.Version,
                self.ActiveIndex,
                self.PreviousActiveIndex,
                self.MetadataSize,
                self.DescriptorOffset,
                self.Reserved2,
                BankState,
                self.Reserved4)

        TmpBuf = FwuMetadata + self.FwuFwStoreDesc
        TmpBuf = TmpBuf[4:]

        self.Crc32 = (zlib.crc32(TmpBuf))

        FwuMetadata = struct.pack(
                FwuMetadataV2._StructFormat,
                self.Crc32,
                self.Version,
                self.ActiveIndex,
                self.PreviousActiveIndex,
                self.MetadataSize,
                self.DescriptorOffset,
                self.Reserved2,
                BankState,
                self.Reserved4)

        return FwuMetadata + self.FwuFwStoreDesc

    def Decode(self, Buffer, ImagePerBanks, NumBanks):
        TotalSize = FwuMetadataV2._StructSize + \
                FwuFwStoreDescV2._StructSize + \
                (ImagePerBanks * \
                (FwuImageEntryV2._StructSize + \
                (FwuImageBankInfoV2._StructSize * NumBanks)))

        if len(Buffer) != TotalSize:
            raise ValueError

        (Crc32, Version, ActiveIndex, PreviousActiveIndex, \
                MetadataSize, DescriptorOffset, Reserved2, \
                BankState, Reserved4) = struct.unpack(
                        FwuMetadataV2._StructFormat,
                        Buffer[0:FwuMetadataV2._StructSize])

        self.Crc32 = Crc32
        self.Version = Version
        self.ActiveIndex = ActiveIndex
        self.PreviousActiveIndex = PreviousActiveIndex
        self.MetadataSize = MetadataSize
        self.DescriptorOffset = DescriptorOffset
        self.Reserved2 = Reserved2
        self.BankState = BankState
        self.Reserved4 = Reserved4
        self.FwuFwStoreDesc = Buffer[FwuMetadataV2._StructSize:]

    def Dump(self):
        print("FWU METADATA HEADER:")
        print("\tcrc32: 0x{Crc32:04x}".format(Crc32=self.Crc32))
        print("\tversion: 0x{Version:04x}".format(Version=self.Version))
        print("\tactive_index: {ActiveIndex:d}".format(ActiveIndex=self.ActiveIndex))
        print("\tprevious_active_index: {PIndex:d}".format(PIndex=self.PreviousActiveIndex))
        print("\tmetadata_size: {MetadataSize:d}".format(MetadataSize=self.MetadataSize))
        print("\tdescriptor_offset: {DescOff:d}".format(DescOff=self.DescriptorOffset))
        print("\treserved: 0x{Reserved2:04x}".format(Reserved2=self.Reserved2))
        for Index in range (4):
            print("\tbank_state[{Idx:d}]: 0x{State:02x}".format(
                Idx=Index, State=self.BankState[Index]))
        print("\treserved: 0x{Reserved4:08x}".format(Reserved4=self.Reserved4))

        print("FIRMWARE STORE DESCRIPTION:")
        FwuFwStoreDesc = FwuFwStoreDescV2()
        FwuFwStoreDesc.Decode(self.FwuFwStoreDesc)
        FwuFwStoreDesc.Dump()

if __name__ == "__main__":
    def convert_arg_line_to_args(arg_line):
        for arg in arg_line.split():
            if not arg.split():
                continue
            yield arg

    def ValidateUint(args):
        try:
            Value = int (args, 0)
        except:
            Msg = "{Arg} isn't integer format".format(Arg=args)
            raise argparse.ArgumentTypeError(Msg)

        if Value < 0:
            Msg = "{Arg} is negative ".format(Arg=args)
            raise argparse.ArgumentTypeError(Msg)

        return Value


    def ValidateGuidList(args):
        GuidList = []

        for arg in args.split(','):
            try:
                Value = uuid.UUID(arg)
            except:
                Msg = "{Arg} isn't valid GUID format".format(Arg=arg)
                raise argparse.ArgumentTypeError(Msg)

            GuidList.append(Value)

        return GuidList


    # create command line argument parser object.
    parser = argparse.ArgumentParser (
                        prog = __prog__,
                        description = __description__,
                        conflict_handler = 'resolve',
                        fromfile_prefix_chars = '@'
                        )
    parser.convert_arg_line_to_args = convert_arg_line_to_args

    #
    # add input and output file arguments
    #
    parser.add_argument("InputFile", type = argparse.FileType('rb'), nargs='?',
                        metavar="filename",
                        help="input filename. used only when dump fwu_metadata.")
    parser.add_argument("-o", "--output", dest="OutputFile", type = argparse.FileType('wb'),
                        metavar="filename",
                        help="output filename.")

    #
    # add group for -c and -d options taht are mutually exclusive and required.
    #
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("-d", "--dump", dest="DumpMode", action="store_true",
                        help="Dump firmware metadata given by [InputFile]")
    group.add_argument("-g", "--generate", dest="GenMode", action="store_true",
                        help="Generate firmware metadata")

    #
    # add optional arguments.
    #
    parser.add_argument("-v", "--version", dest="Version", type=ValidateUint,
                        default=2, metavar="number",
                        help="Metadata Version (default: 2).")
    parser.add_argument("-b", "--banks", dest="NumBanks", type=ValidateUint,
                        default=2, metavar="number",
                        help="Number of banks (default: 2).")
    parser.add_argument("-p", "--img_per_bank", dest="ImagePerBank", type=ValidateUint,
                        default=1, metavar="number",
                        help="Number of image per bank (default: 1).")
    parser.add_argument("-i", "--img_type_guid", dest="ImgTypeGuidList", type=ValidateGuidList,
                        default=[], metavar="guid_list",
                        help="Image Type Guid list separated by ','. if not, Generate based on image per bank.")
    parser.add_argument("-l", "--location_guid", dest="LocationGuidList", type=ValidateGuidList,
                        default=[], metavar="guid_list",
                        help="Location Guid list separated by ','. if not, Generate based on image per bank.")

    args = parser.parse_args()

    if args.DumpMode:
        if args.InputFile is None:
            print("ERROR: No Input file.");
            sys.exit(2)

        Buffer = args.InputFile.read()
        args.InputFile.close()

        if (args.Version == 1):
            print("ERROR: Metadata v1 isn't supported.");
            sys.exit(2)
        else:
            FwuMetadata = FwuMetadataV2()
            FwuMetadata.Decode(Buffer, args.ImagePerBank, args.NumBanks)
            FwuMetadata.Dump()

        sys.exit(0)

    #
    # generate firmware update metadata.
    #
    if len(args.ImgTypeGuidList) == 0:
        for Index in range(args.ImagePerBank):
            args.ImgTypeGuidList.append(uuid.uuid4())

    if len(args.LocationGuidList) == 0:
        for Index in range(args.ImagePerBank):
            args.LocationGuidList.append(uuid.uuid4())

    if (len(args.ImgTypeGuidList) < args.ImagePerBank or
            len(args.LocationGuidList) < args.ImagePerBank):
        print("ERROR: Please input proper list: img_type_guid or location_guid")
        sys.exit(22)


    if (args.Version == 1):
        print("ERROR: Metadata v1 isn't supported.");
        sys.exit(2)
    else:
        ImgEntryBuffer = b''

        for EntryIndex in range(args.ImagePerBank):
            ImageTypeGuid = args.ImgTypeGuidList[EntryIndex]
            LocationGuid = args.LocationGuidList[EntryIndex]
            ImageBankInfoBuffer = b''

            for InfoIndex in range(args.NumBanks):
                FwuImageBankInfo = FwuImageBankInfoV2(ImageTypeGuid)
                ImageBankInfoBuffer += FwuImageBankInfo.Encode()

            FwuImageEntry = FwuImageEntryV2(ImageTypeGuid, LocationGuid, ImageBankInfoBuffer)
            ImgEntryBuffer += FwuImageEntry.Encode()

        FwuFwStoreDesc = FwuFwStoreDescV2(args.NumBanks, args.ImagePerBank, ImgEntryBuffer)
        FwStoreDescBuffer = FwuFwStoreDesc.Encode()

        FwuMetadata = FwuMetadataV2(FwStoreDescBuffer)
        MetadataBuffer = FwuMetadata.Encode(args.NumBanks)

        print("Generated Firmware Metadata:")
        FwuMetadata.Dump()

    if args.OutputFile is not None:
        args.OutputFile.write(MetadataBuffer)
        args.OutputFile.close()

    sys.exit(0)