<s> from nordicsemi . dfu import intelhex <EOL> from struct import unpack <EOL> class nRFHex ( intelhex . IntelHex ) : <EOL> """<STR_LIT>""" <EOL> info_struct_address_base = <NUM_LIT> <EOL> info_struct_address_offset = <NUM_LIT> <EOL> info_struct_magic_number = <NUM_LIT> <EOL> info_struct_magic_number_offset = <NUM_LIT> <EOL> s1x0_mbr_end_address = <NUM_LIT> <EOL> s132_mbr_end_address = <NUM_LIT> <EOL> def __init__ ( self , source , bootloader = None ) : <EOL> """<STR_LIT>""" <EOL> super ( nRFHex , self ) . __init__ ( ) <EOL> self . file_format = '<STR_LIT>' <EOL> if source . endswith ( '<STR_LIT>' ) : <EOL> self . file_format = '<STR_LIT>' <EOL> self . loadfile ( source , self . file_format ) <EOL> self . _removeuicr ( ) <EOL> self . bootloaderhex = None <EOL> if bootloader is not None : <EOL> self . bootloaderhex = nRFHex ( bootloader ) <EOL> def _removeuicr ( self ) : <EOL> uicr_start_address = <NUM_LIT> <EOL> maxaddress = self . maxaddr ( ) <EOL> if maxaddress >= uicr_start_address : <EOL> for i in range ( uicr_start_address , maxaddress + <NUM_LIT:1> ) : <EOL> self . _buf . pop ( i , <NUM_LIT:0> ) <EOL> def address_has_magic_number ( self , address ) : <EOL> try : <EOL> potential_magic_number = self . gets ( address , <NUM_LIT:4> ) <EOL> potential_magic_number = unpack ( '<STR_LIT:I>' , potential_magic_number ) [ <NUM_LIT:0> ] <EOL> return nRFHex . info_struct_magic_number == potential_magic_number <EOL> except Exception : <EOL> return False <EOL> def get_softdevice_variant ( self ) : <EOL> potential_magic_number_address = nRFHex . info_struct_address_base + nRFHex . info_struct_magic_number_offset <EOL> if self . address_has_magic_number ( potential_magic_number_address ) : <EOL> return "<STR_LIT>" <EOL> for i in xrange ( <NUM_LIT:4> ) : <EOL> potential_magic_number_address += nRFHex . info_struct_address_offset <EOL> if self . address_has_magic_number ( potential_magic_number_address ) : <EOL> return "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> def get_mbr_end_address ( self ) : <EOL> softdevice_variant = self . get_softdevice_variant ( ) <EOL> if softdevice_variant == "<STR_LIT>" : <EOL> return nRFHex . s132_mbr_end_address <EOL> else : <EOL> return nRFHex . s1x0_mbr_end_address <EOL> def minaddr ( self ) : <EOL> min_address = super ( nRFHex , self ) . minaddr ( ) <EOL> if self . file_format != '<STR_LIT>' : <EOL> min_address = max ( self . get_mbr_end_address ( ) , min_address ) <EOL> return min_address <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> min_address = self . minaddr ( ) <EOL> max_address = self . maxaddr ( ) <EOL> size = max_address - min_address + <NUM_LIT:1> <EOL> word_size = <NUM_LIT:4> <EOL> number_of_words = ( size + ( word_size - <NUM_LIT:1> ) ) / word_size <EOL> size = number_of_words * word_size <EOL> return size <EOL> def bootloadersize ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . bootloaderhex is None : <EOL> return <NUM_LIT:0> <EOL> return self . bootloaderhex . size ( ) <EOL> def tobinfile ( self , fobj , start = None , end = None , pad = None , size = None ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( fobj , "<STR_LIT>" , None ) is None : <EOL> fobj = open ( fobj , "<STR_LIT:wb>" ) <EOL> close_fd = True <EOL> else : <EOL> close_fd = False <EOL> start_address = self . minaddr ( ) <EOL> size = self . size ( ) <EOL> super ( nRFHex , self ) . tobinfile ( fobj , start = start_address , size = size ) <EOL> if self . bootloaderhex is not None : <EOL> self . bootloaderhex . tobinfile ( fobj ) <EOL> if close_fd : <EOL> fobj . close ( ) </s>
