def get_romimage_characteristics(self): # Extract the ROMimage size and characteritics from the first FFFF # header in the buffer. # Unpack the fixed part of the header ffff_hdr = unpack_from("<16s16s48sLLLLL", self.ffff_buf) sentinel = ffff_hdr[0] self.timestamp = ffff_hdr[1] self.flash_image_name = ffff_hdr[2] self.flash_capacity = ffff_hdr[3] self.erase_block_size = ffff_hdr[4] self.header_size = ffff_hdr[5] self.flash_image_length = ffff_hdr[6] self.header_generation_number = ffff_hdr[7] # Unpack the 2nd sentinel at the tail ffff_hdr = unpack_from("<16s", self.ffff_buf, FFFF_HDR_OFF_TAIL_SENTINEL) tail_sentinel = ffff_hdr[0] # Verify the sentinels if sentinel != FFFF_SENTINEL or \ tail_sentinel != FFFF_SENTINEL: raise ValueError("invalid sentinel") # Validate the block size and image length if not is_power_of_2(self.erase_block_size): raise ValueError("Erase block size must be 2**n") elif (self.flash_image_length % self.erase_block_size) != 0: raise ValueError("Image length must be a multiple of erase bock size") # Determine the ROM range that can hold the elements self.element_location_min = 2 * self.header_block_size() self.element_location_max = self.flash_capacity
def init(self, flash_image_name, flash_capacity, erase_block_size, image_length, header_generation_number, header_size): """"FFFF post-constructor initializer for a new FFFF FFFF post-constructor initializer for creating an FFFF (as opposed to reading an existing one from a file), and returns a success flag. The FFFF ROMimage buffer is sized explicitly from the image_length parameter. """ # Validate the parameters if (header_size < FFFF_HEADER_SIZE_MIN) or \ (header_size > FFFF_HEADER_SIZE_MAX): raise ValueError("header_size is out of range") elif not is_power_of_2(erase_block_size): raise ValueError("Erase block size must be 2**n") elif (image_length % erase_block_size) != 0: raise ValueError("Image length must be a multiple " "of erase bock size") self.header_size = header_size self.flash_image_name = flash_image_name self.flash_capacity = flash_capacity self.erase_block_size = erase_block_size self.flash_image_length = image_length self.header_generation_number = header_generation_number # Determine the ROM range that can hold the elements self.element_location_min = 2 * self.get_header_block_size() self.element_location_max = image_length # Resize the ROMimage buffer to the correct size self.ffff_buf = bytearray(image_length) #self.mv = memoryview(self.ffff_buf) # Create the 2 FFFF headers self.ffff0 = Ffff(self.ffff_buf, 0, flash_image_name, flash_capacity, erase_block_size, image_length, header_generation_number, header_size) self.ffff1 = Ffff(self.ffff_buf, self.get_header_block_size(), flash_image_name, flash_capacity, erase_block_size, image_length, header_generation_number, header_size) return True
def validate_ffff_header(self): # Perform a quick validity check of the header. Generally done when # importing an existing FFFF file. self.header_validity = FFFF_HDR_VALID # Check for erased header span = self.ffff_buf[self.header_offset:self.header_offset + FFFF_HDR_LENGTH] if is_constant_fill(span, 0) or \ is_constant_fill(span, 0xff): error("FFFF header validates as erased.") self.header_validity = FFFF_HDR_ERASED return self.header_validity # Valid sentinels? if self.sentinel != FFFF_SENTINEL or \ self.tail_sentinel != FFFF_SENTINEL: error("Invalid sentinel") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify sizes if not is_power_of_2(self.erase_block_size): error("Erase block size must be 2**n") self.header_validity = FFFF_HDR_INVALID return self.header_validity elif (self.flash_image_length % self.erase_block_size) != 0: error("Image length is not a multiple of erase bock size") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify that the unused portions of the header are zeroed, per spec. span_start = self.header_offset + FFFF_HDR_OFF_ELEMENT_TBL + \ len(self.elements) * FFFF_ELT_LENGTH span_end = self.header_offset + FFFF_HDR_OFF_TAIL_SENTINEL - \ span_start if not is_constant_fill(self.ffff_buf[span_start:span_end], 0): error("Unused portions of header are not zeroed: " + str(span_start) + \ " to " + str(span_end)) self.header_validity = FFFF_HDR_INVALID return self.header_validity # check for elemental problems if not self.validate_element_table(): error("Invalid element table.") self.header_validity = FFFF_HDR_INVALID return self.header_validity
def validate_ffff_header(self): # Perform a quick validity check of the header. Generally done when # importing an existing FFFF file. self.header_validity = FFFF_HDR_VALID # Check for erased header span = self.ffff_buf[self.header_offset: self.header_offset+FFFF_HDR_LENGTH] if is_constant_fill(span, 0) or \ is_constant_fill(span, 0xff): error("FFFF header validates as erased.") self.header_validity = FFFF_HDR_ERASED return self.header_validity # Valid sentinels? if self.sentinel != FFFF_SENTINEL or \ self.tail_sentinel != FFFF_SENTINEL: error("Invalid sentinel") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify sizes if not is_power_of_2(self.erase_block_size): error("Erase block size must be 2**n") self.header_validity = FFFF_HDR_INVALID return self.header_validity elif (self.flash_image_length % self.erase_block_size) != 0: error("Image length is not a multiple of erase bock size") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify that the unused portions of the header are zeroed, per spec. span_start = self.header_offset + FFFF_HDR_OFF_ELEMENT_TBL + \ len(self.elements) * FFFF_ELT_LENGTH span_end = self.header_offset + FFFF_HDR_OFF_TAIL_SENTINEL - \ span_start if not is_constant_fill(self.ffff_buf[span_start:span_end], 0): error("Unused portions of header are not zeroed: " + str(span_start) + \ " to " + str(span_end)) self.header_validity = FFFF_HDR_INVALID return self.header_validity # check for elemental problems if not self.validate_element_table(): error("Invalid element table.") self.header_validity = FFFF_HDR_INVALID return self.header_validity
def get_romimage_characteristics(self): # Extract the ROMimage size and characteritics from the first FFFF # header in the buffer. # Unpack the fixed part of the header ffff_hdr = unpack_from("<16s16s48sLLLLL", self.ffff_buf) sentinel = ffff_hdr[0] self.timestamp = ffff_hdr[1] self.flash_image_name = ffff_hdr[2] self.flash_capacity = ffff_hdr[3] self.erase_block_size = ffff_hdr[4] self.header_size = ffff_hdr[5] self.flash_image_length = ffff_hdr[6] self.header_generation_number = ffff_hdr[7] FFFF_HDR_OFF_TAIL_SENTINEL = (self.header_size - FFFF_HDR_LEN_TAIL_SENTINEL) # Because we have variable-size FFFF headers, we need to recalculate # the number of entries in the section table, and the offsets to all # fields which follow. self.recalculate_header_offsets() # Unpack the 2nd sentinel at the tail ffff_hdr = unpack_from("<16s", self.ffff_buf, FFFF_HDR_OFF_TAIL_SENTINEL) tail_sentinel = ffff_hdr[0] # Verify the sentinels if sentinel != FFFF_SENTINEL: raise ValueError("invalid sentinel") if sentinel != tail_sentinel != FFFF_SENTINEL: raise ValueError("invalid tail sentinel '{0:16s}'".format(tail_sentinel)) # Validate the block size and image length if not is_power_of_2(self.erase_block_size): raise ValueError("Erase block size must be 2**n") elif (self.flash_image_length % self.erase_block_size) != 0: raise ValueError("Image length must be a multiple " "of erase bock size") # Determine the ROM range that can hold the elements self.element_location_min = 2 * self.get_header_block_size() self.element_location_max = self.flash_capacity
def get_romimage_characteristics(self): # Extract the ROMimage size and characteritics from the first FFFF # header in the buffer. # Unpack the fixed part of the header ffff_hdr = unpack_from("<16s16s48sLLLLL", self.ffff_buf) sentinel = ffff_hdr[0] self.timestamp = ffff_hdr[1] self.flash_image_name = ffff_hdr[2] self.flash_capacity = ffff_hdr[3] self.erase_block_size = ffff_hdr[4] self.header_size = ffff_hdr[5] self.flash_image_length = ffff_hdr[6] self.header_generation_number = ffff_hdr[7] FFFF_HDR_OFF_TAIL_SENTINEL = (self.header_size - FFFF_HDR_LEN_TAIL_SENTINEL) # Because we have variable-size FFFF headers, we need to recalculate # the number of entries in the section table, and the offsets to all # fields which follow. self.recalculate_header_offsets() # Unpack the 2nd sentinel at the tail ffff_hdr = unpack_from("<16s", self.ffff_buf, FFFF_HDR_OFF_TAIL_SENTINEL) tail_sentinel = ffff_hdr[0] # Verify the sentinels if sentinel != FFFF_SENTINEL: raise ValueError("invalid sentinel") if sentinel != tail_sentinel != FFFF_SENTINEL: raise ValueError( "invalid tail sentinel '{0:16s}'".format(tail_sentinel)) # Validate the block size and image length if not is_power_of_2(self.erase_block_size): raise ValueError("Erase block size must be 2**n") elif (self.flash_image_length % self.erase_block_size) != 0: raise ValueError("Image length must be a multiple " "of erase bock size") # Determine the ROM range that can hold the elements self.element_location_min = 2 * self.get_header_block_size() self.element_location_max = self.flash_capacity
def get_romimage_characteristics(self): # Extract the ROMimage size and characteritics from the first FFFF # header in the buffer. # Unpack the fixed part of the header ffff_hdr = unpack_from("<16s16s48sLLLLL", self.ffff_buf) sentinel = ffff_hdr[0] self.timestamp = ffff_hdr[1] self.flash_image_name = ffff_hdr[2] self.flash_capacity = ffff_hdr[3] self.erase_block_size = ffff_hdr[4] self.header_size = ffff_hdr[5] self.flash_image_length = ffff_hdr[6] self.header_generation_number = ffff_hdr[7] # Unpack the 2nd sentinel at the tail ffff_hdr = unpack_from("<16s", self.ffff_buf, FFFF_HDR_OFF_TAIL_SENTINEL) tail_sentinel = ffff_hdr[0] # Verify the sentinels if sentinel != FFFF_SENTINEL or \ tail_sentinel != FFFF_SENTINEL: error("invalid sentinel") return False # Validate the block size and image length if not is_power_of_2(self.erase_block_size): error("Erase block size must be 2**n") return False elif (self.flash_image_length % self.erase_block_size) != 0: error("Image length must be a multiple of erase bock size") return False # Determine the ROM range that can hold the elements self.element_location_min = 2 * self.header_block_size() self.element_location_max = self.flash_capacity return True
def validate_ffff_header(self): # Perform a quick validity check of the header. Generally done when # importing an existing FFFF file. self.header_validity = FFFF_HDR_VALID # Check for erased header span = self.ffff_buf[self.header_offset:self.header_offset + self.header_size] if is_constant_fill(span, 0) or \ is_constant_fill(span, 0xff): error("FFFF header validates as erased.") self.header_validity = FFFF_HDR_ERASED return self.header_validity # Valid sentinels? if self.sentinel != FFFF_SENTINEL or \ self.tail_sentinel != FFFF_SENTINEL: error("Invalid sentinel") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify sizes if (self.header_size < FFFF_HEADER_SIZE_MIN) or \ (self.header_size > FFFF_HEADER_SIZE_MAX): error("header_size is out of range") return self.header_validity elif not is_power_of_2(self.erase_block_size): error("Erase block size must be 2**n") self.header_validity = FFFF_HDR_INVALID return self.header_validity elif (self.flash_image_length % self.erase_block_size) != 0: error("Image length is not a multiple of erase bock size") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify that the reserved portion of the header is zeroed. for rsvd in self.reserved: if rsvd != 0: error("Reserved fields are non-zero") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify that the unused portions of the header are zeroed, per spec. span_start = self.header_offset + FFFF_HDR_OFF_ELEMENT_TBL + \ len(self.elements) * FFFF_ELT_LENGTH span_end = self.header_offset + FFFF_HDR_OFF_TAIL_SENTINEL - \ span_start if not is_constant_fill(self.ffff_buf[span_start:span_end], 0): error("Unused portions of FFFF header are non-zero: " "(0x{0:x}-0x{1:x})".format(span_start, span_end)) self.header_validity = FFFF_HDR_INVALID return self.header_validity # check for elemental problems if not self.validate_element_table(): error("Invalid element table.") self.header_validity = FFFF_HDR_INVALID return self.header_validity
def validate_ffff_header(self): # Perform a quick validity check of the header. Generally done when # importing an existing FFFF file. self.header_validity = FFFF_HDR_VALID # Check for erased header span = self.ffff_buf[self.header_offset: self.header_offset+self.header_size] if is_constant_fill(span, 0) or \ is_constant_fill(span, 0xff): error("FFFF header validates as erased.") self.header_validity = FFFF_HDR_ERASED return self.header_validity # Valid sentinels? if self.sentinel != FFFF_SENTINEL or \ self.tail_sentinel != FFFF_SENTINEL: error("Invalid sentinel") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify sizes if (self.header_size < FFFF_HEADER_SIZE_MIN) or \ (self.header_size > FFFF_HEADER_SIZE_MAX): error("header_size is out of range") return self.header_validity elif not is_power_of_2(self.erase_block_size): error("Erase block size must be 2**n") self.header_validity = FFFF_HDR_INVALID return self.header_validity elif (self.flash_image_length % self.erase_block_size) != 0: error("Image length is not a multiple of erase bock size") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify that the reserved portion of the header is zeroed. for rsvd in self.reserved: if rsvd != 0: error("Reserved fields are non-zero") self.header_validity = FFFF_HDR_INVALID return self.header_validity # Verify that the unused portions of the header are zeroed, per spec. span_start = self.header_offset + FFFF_HDR_OFF_ELEMENT_TBL + \ len(self.elements) * FFFF_ELT_LENGTH span_end = self.header_offset + FFFF_HDR_OFF_TAIL_SENTINEL - \ span_start if not is_constant_fill(self.ffff_buf[span_start:span_end], 0): error("Unused portions of FFFF header are non-zero: " "(0x{0:x}-0x{1:x})".format(span_start, span_end)) self.header_validity = FFFF_HDR_INVALID return self.header_validity # check for elemental problems if not self.validate_element_table(): error("Invalid element table.") self.header_validity = FFFF_HDR_INVALID return self.header_validity