def create(prmbl_size, num_pages): # Create the preamble header prmbl_hdr = MbnHdr80B(PAD_BYTE_1 * PrmblHdr.get_size()) prmbl_hdr.codeword = FLASH_CODE_WORD prmbl_hdr.magic = MAGIC_NUM prmbl_hdr.image_id = PRMBL_MAGIC_NUMS[num_pages] prmbl_hdr = PrmblHdr(prmbl_hdr.pack()) return _add_padding(prmbl_hdr, prmbl_size)
def generate(self, args): infile = open(args.input_file, "rb") infile_data = infile.read() infile.close() if self.is_mbn_file(infile_data, args.input_file) is not None: logger.warning( "WARNING! {0} appears to already have an MBN header!".format( args.input_file)) # sectools will update these values when it inputs the cert chain and sig # but in this feature just set them to zero code_size = ALIGNED_IMAGE_SIZE(os.path.getsize(args.input_file)) signature_size = 0 signature_ptr = 0 cert_chain_ptr = 0 cert_chain_size = 0 image_size = code_size + signature_size + cert_chain_size header = None if args.header_length == 40: header = MbnHdr40B() header._unpack_data_list([ args.image_id, args.header_version, args.image_src, args.image_dest_ptr, image_size, code_size, signature_ptr, signature_size, cert_chain_ptr, cert_chain_size ]) elif args.header_length == 80: header = MbnHdr80B() # todo: verify magic and codeword are correct header._unpack_data_list([ MBN_80_CODEWORD, # codeword I pulled from given file MBN_80_MAGIC, # magic I pulled from given file args.image_id, 0xffffffff, 0xffffffff, args.image_src, args.image_dest_ptr, image_size, code_size, signature_ptr, signature_size, cert_chain_ptr, cert_chain_size, 0, # this has a value of 1 in a file I was given. what does it mean? 0, 0, 0, 0, 0, 0 ]) else: raise RuntimeError( "received invalid MBN header length in generate. This should never happen" ) outfile_name = os.path.basename(args.input_file) + ".mbn" if not c_path.validate_dir_write(args.output_dir): os.makedirs(args.output_dir) outfile = open(c_path.join(args.output_dir, outfile_name), "wb") outfile.write(header.pack()) outfile.write(infile_data) outfile.close() infile.close() logger.info("output \"{0}\" has been successfully generated".format( c_path.join(args.output_dir, outfile_name)))
def validate(self): MbnHdr80B.validate(self) if ((self.codeword != FLASH_CODE_WORD) or (self.magic != MAGIC_NUM) or (self.image_id not in PRMBL_MAGIC_NUMS.values())): raise RuntimeError('Preamble is invalid')