30
30
import hashlib
31
31
import array
32
32
import os .path
33
+ import re
33
34
import struct
35
+ import uuid
34
36
from enum import Enum
35
37
36
38
import click
96
98
'DECOMP_SHA' : 0x71 ,
97
99
'DECOMP_SIGNATURE' : 0x72 ,
98
100
'COMP_DEC_SIZE' : 0x73 ,
101
+ 'UUID_VID' : 0x80 ,
102
+ 'UUID_CID' : 0x81 ,
99
103
}
100
104
101
105
TLV_SIZE = 4
@@ -254,6 +258,23 @@ def tlv_matches_key_type(tlv_type, key):
254
258
255
259
return False
256
260
261
+ def parse_uuid (namespace , value ):
262
+ # Check if UUID is in the RAW format (12345678-1234-5678-1234-567812345678)
263
+ uuid_re = r'[0-9A-f]{8}-[0-9A-f]{4}-[0-9A-f]{4}-[0-9A-f]{4}-[0-9A-f]{12}'
264
+ if re .match (uuid_re , value ):
265
+ uuid_bytes = bytes .fromhex (value .replace ('-' , '' ))
266
+
267
+ # Check if UUID is in the string format
268
+ elif value .isprintable ():
269
+ if namespace is not None :
270
+ uuid_bytes = uuid .uuid5 (namespace , value ).bytes
271
+ else :
272
+ raise ValueError (f"Unknown namespace for UUID: { value } " )
273
+ else :
274
+ raise ValueError (f"Unknown UUID format: { value } " )
275
+
276
+ return uuid_bytes
277
+
257
278
258
279
class Image :
259
280
@@ -263,7 +284,7 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
263
284
overwrite_only = False , endian = "little" , load_addr = 0 ,
264
285
rom_fixed = None , erased_val = None , save_enctlv = False ,
265
286
security_counter = None , max_align = None ,
266
- non_bootable = False ):
287
+ non_bootable = False , vid = None , cid = None ):
267
288
268
289
if load_addr and rom_fixed :
269
290
raise click .UsageError ("Can not set rom_fixed and load_addr at the same time" )
@@ -292,6 +313,8 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
292
313
self .enctlv_len = 0
293
314
self .max_align = max (DEFAULT_MAX_ALIGN , align ) if max_align is None else int (max_align )
294
315
self .non_bootable = non_bootable
316
+ self .vid = vid
317
+ self .cid = cid
295
318
296
319
if self .max_align == DEFAULT_MAX_ALIGN :
297
320
self .boot_magic = bytes ([
@@ -321,7 +344,7 @@ def __repr__(self):
321
344
return "<Image version={}, header_size={}, security_counter={}, \
322
345
base_addr={}, load_addr={}, align={}, slot_size={}, \
323
346
max_sectors={}, overwrite_only={}, endian={} format={}, \
324
- payloadlen=0x{:x}>" .format (
347
+ payloadlen=0x{:x}, vid={}, cid={} >" .format (
325
348
self .version ,
326
349
self .header_size ,
327
350
self .security_counter ,
@@ -333,7 +356,9 @@ def __repr__(self):
333
356
self .overwrite_only ,
334
357
self .endian ,
335
358
self .__class__ .__name__ ,
336
- len (self .payload ))
359
+ len (self .payload ),
360
+ self .vid ,
361
+ self .cid )
337
362
338
363
def load (self , path ):
339
364
"""Load an image from a given file"""
@@ -500,6 +525,16 @@ def create(self, key, public_key_format, enckey, dependencies=None,
500
525
# = 4 + 4 = 8 Bytes
501
526
protected_tlv_size += TLV_SIZE + 4
502
527
528
+ if self .vid is not None :
529
+ # Size of the VID TLV: header ('HH') + payload ('16s')
530
+ # = 4 + 16 = 20 Bytes
531
+ protected_tlv_size += TLV_SIZE + 16
532
+
533
+ if self .cid is not None :
534
+ # Size of the CID TLV: header ('HH') + payload ('16s')
535
+ # = 4 + 16 = 20 Bytes
536
+ protected_tlv_size += TLV_SIZE + 16
537
+
503
538
if sw_type is not None :
504
539
if len (sw_type ) > MAX_SW_TYPE_LENGTH :
505
540
msg = "'{}' is too long ({} characters) for sw_type. Its " \
@@ -603,6 +638,21 @@ def create(self, key, public_key_format, enckey, dependencies=None,
603
638
if compression_tlvs is not None :
604
639
for tag , value in compression_tlvs .items ():
605
640
prot_tlv .add (tag , value )
641
+
642
+ if self .vid is not None :
643
+ vid = parse_uuid (uuid .NAMESPACE_DNS , self .vid )
644
+ payload = struct .pack (e + '16s' , vid )
645
+ prot_tlv .add ('UUID_VID' , payload )
646
+
647
+ if self .cid is not None :
648
+ if self .vid is not None :
649
+ namespace = uuid .UUID (bytes = vid )
650
+ else :
651
+ namespace = None
652
+ cid = parse_uuid (namespace , self .cid )
653
+ payload = struct .pack (e + '16s' , cid )
654
+ prot_tlv .add ('UUID_CID' , payload )
655
+
606
656
if custom_tlvs is not None :
607
657
for tag , value in custom_tlvs .items ():
608
658
prot_tlv .add (tag , value )
0 commit comments