1616import logging
1717import struct
1818import sys
19-
20- from imgtool .image import (IMAGE_HEADER_SIZE , IMAGE_MAGIC ,
21- TLV_INFO_MAGIC , TLV_PROT_INFO_MAGIC , TLV_VALUES )
2219from shutil import copyfile
2320
21+ from imgtool .image import (
22+ IMAGE_HEADER_SIZE ,
23+ IMAGE_MAGIC ,
24+ TLV_INFO_MAGIC ,
25+ TLV_PROT_INFO_MAGIC ,
26+ TLV_VALUES ,
27+ )
28+
2429
2530def get_tlv_type_string (tlv_type ):
2631 tlvs = {v : f"IMAGE_TLV_{ k } " for k , v in TLV_VALUES .items ()}
27- return tlvs .get (tlv_type , "UNKNOWN({:d})" . format ( tlv_type ) )
32+ return tlvs .get (tlv_type , f "UNKNOWN({ tlv_type :d} )" )
2833
2934
3035class ImageHeader :
@@ -53,7 +58,7 @@ def read_from_binary(in_file):
5358
5459 def __repr__ (self ):
5560 return "\n " .join ([
56- " ih_magic = 0x{:X}" . format ( self . ih_magic ) ,
61+ f " ih_magic = 0x{ self . ih_magic :X} " ,
5762 " ih_load_addr = " + str (self .ih_load_addr ),
5863 " ih_hdr_size = " + str (self .ih_hdr_size ),
5964 " ih_protect_tlv_size = " + str (self .ih_protect_tlv_size ),
@@ -82,7 +87,7 @@ def read_from_binary(in_file):
8287
8388 def __repr__ (self ):
8489 return "\n " .join ([
85- " it_magic = 0x{:X}" . format ( self . it_magic ) ,
90+ f " it_magic = 0x{ self . it_magic :X} " ,
8691 " it_tlv_tot = " + str (self .it_tlv_tot )])
8792
8893 def __len__ (self ):
@@ -99,7 +104,7 @@ def __init__(self):
99104 def read_from_binary (in_file ):
100105 tlv = ImageTLV ()
101106 (tlv .it_type , _ , tlv .it_len ) = struct .unpack ('<BBH' , in_file .read (4 ))
102- (tlv .it_value ) = struct .unpack ('<{:d}s' . format ( tlv . it_len ) , in_file .read (tlv .it_len ))
107+ (tlv .it_value ) = struct .unpack (f '<{ tlv . it_len :d} s' , in_file .read (tlv .it_len ))
103108 return tlv
104109
105110 def __len__ (self ):
@@ -126,7 +131,7 @@ def get_arguments():
126131
127132def damage_tlv (image_offset , tlv_off , tlv , out_file_content ):
128133 damage_offset = image_offset + tlv_off + 4
129- logging .info (" Damaging TLV at offset 0x{:X}..." . format ( damage_offset ) )
134+ logging .info (f " Damaging TLV at offset 0x{ damage_offset :X} ..." )
130135 value = bytearray (tlv .it_value [0 ])
131136 value [0 ] = (value [0 ] + 1 ) % 256
132137 out_file_content [damage_offset ] = value [0 ]
@@ -142,17 +147,17 @@ def damage_image(args, in_file, out_file_content, image_offset):
142147 # Find the Image header
143148 image_header = ImageHeader .read_from_binary (in_file )
144149 if image_header .ih_magic != IMAGE_MAGIC :
145- raise Exception ("Invalid magic in image_header: 0x{:X} instead of 0x{:X}" . format ( image_header . ih_magic , IMAGE_MAGIC ) )
150+ raise Exception (f "Invalid magic in image_header: 0x{ image_header . ih_magic :X} instead of 0x{ IMAGE_MAGIC :X} " )
146151
147152 # Find the TLV header
148153 tlv_info_offset = image_header .ih_hdr_size + image_header .ih_img_size
149154 in_file .seek (image_offset + tlv_info_offset , 0 )
150155
151156 tlv_info = ImageTLVInfo .read_from_binary (in_file )
152157 if tlv_info .it_magic == TLV_PROT_INFO_MAGIC :
153- logging .debug ("Protected TLV found at offset 0x{:X}" . format ( tlv_info_offset ) )
158+ logging .debug (f "Protected TLV found at offset 0x{ tlv_info_offset :X} " )
154159 if image_header .ih_protect_tlv_size != tlv_info .it_tlv_tot :
155- raise Exception ("Invalid prot TLV len ({:d} vs. {:d})" . format ( image_header . ih_protect_tlv_size , tlv_info . it_tlv_tot ) )
160+ raise Exception (f "Invalid prot TLV len ({ image_header . ih_protect_tlv_size :d} vs. { tlv_info . it_tlv_tot :d} )" )
156161
157162 # seek to unprotected TLV
158163 tlv_info_offset += tlv_info .it_tlv_tot
@@ -163,9 +168,9 @@ def damage_image(args, in_file, out_file_content, image_offset):
163168 if image_header .ih_protect_tlv_size != 0 :
164169 raise Exception ("No prot TLV was found." )
165170
166- logging .debug ("Unprotected TLV found at offset 0x{:X}" . format ( tlv_info_offset ) )
171+ logging .debug (f "Unprotected TLV found at offset 0x{ tlv_info_offset :X} " )
167172 if tlv_info .it_magic != TLV_INFO_MAGIC :
168- raise Exception ("Invalid magic in tlv info: 0x{:X} instead of 0x{:X}" . format ( tlv_info . it_magic , TLV_INFO_MAGIC ) )
173+ raise Exception (f "Invalid magic in tlv info: 0x{ tlv_info . it_magic :X} instead of 0x{ TLV_INFO_MAGIC :X} " )
169174
170175 tlv_off = tlv_info_offset + len (ImageTLVInfo ())
171176 tlv_end = tlv_info_offset + tlv_info .it_tlv_tot
@@ -175,11 +180,9 @@ def damage_image(args, in_file, out_file_content, image_offset):
175180 in_file .seek (image_offset + tlv_off , 0 )
176181 tlv = ImageTLV .read_from_binary (in_file )
177182
178- logging .debug (" tlv {:24s} len = {:4d}, len = {:4d}" . format ( get_tlv_type_string ( tlv . it_type ), tlv . it_len , len (tlv )) )
183+ logging .debug (f " tlv { get_tlv_type_string ( tlv . it_type ) :24s} len = { tlv . it_len :4d} , len = { len (tlv ):4d } " )
179184
180- if is_valid_signature (tlv ) and args .signature :
181- damage_tlv (image_offset , tlv_off , tlv , out_file_content )
182- elif tlv .it_type == TLV_VALUES ['SHA256' ] and args .image_hash :
185+ if is_valid_signature (tlv ) and args .signature or tlv .it_type == TLV_VALUES ['SHA256' ] and args .image_hash :
183186 damage_tlv (image_offset , tlv_off , tlv , out_file_content )
184187
185188 tlv_off += len (tlv )
0 commit comments