def main(): parser = OptionParser(usage='usage: %prog [options] arguments') parser.add_option("-f", "--first_filepath", action="store", type="string", dest="elf_inp_file1", help="First ELF file to merge.") parser.add_option("-s", "--second_filepath", action="store", type="string", dest="elf_inp_file2", help="Second ELF file to merge.") parser.add_option("-x", "--xbl_sec_filepath", action="store", type="string", dest="elf_inp_xbl_sec", help="Second ELF file to merge.") parser.add_option("-o", "--output_filepath", action="store", type="string", dest="binary_out", help="Merged filename and path.") parser.add_option("-a", "--first_elf_arch", action="store", type="string", dest="elf_1_arch", help="First (and output) ELF file architecture. '32' or '64'") parser.add_option("-b", "--second_elf_arch", action="store", type="string", dest="elf_2_arch", help="Second ELF file architecture. '32' or '64'") parser.add_option("-d", "--xbl_sec_elf_arch", action="store", type="string", dest="elf_xbl_sec_arch", help="xbl_sec file architecture. '32' or '64'") parser.add_option("-c", "--output_elf_arch", action="store", type="string", dest="elf_out_arch", help="Output ELF file architecture. '32' or '64'" + \ " If not given defaults to first file arch.") parser.add_option("-n", "--no_hash", action="store_true", dest="hash_image", help="Disables hashing of image after merging.") parser.add_option("-z", "--zi_out_of_bounds", action="store_true", dest="zi_oob", help="Removes ZI segments that have addresses greater" + \ " than 32 bits when converting from a 64 to 32 bit ELF") (options, args) = parser.parse_args() if not options.elf_inp_file1: parser.error('First ELF filename not given') if not options.binary_out: parser.error('Output filename not given') if not options.elf_1_arch: parser.error('First ELF architecture not given') if (not options.elf_1_arch == '64') and (not options.elf_1_arch == '32'): parser.error('Invalid First ELF architecture given') # Only evaluate elf_2_arch if two files are given for merging if options.elf_inp_file2: if (not options.elf_2_arch == '64') and (not options.elf_2_arch == '32'): parser.error('Invalid Second ELF architecture given') # Only evaluate elf_xbl_sec_arch if file is given if options.elf_inp_xbl_sec: if (not options.elf_xbl_sec_arch == '64') and (not options.elf_xbl_sec_arch == '32'): parser.error('Invalid xbl_sec ELF architecture given') # If output file architecture is given ensure it is either '32' or '64' if options.elf_out_arch: if (not options.elf_out_arch == '64') and (not options.elf_out_arch == '32'): parser.error('Invalid Output ELF architecture given') gen_dict = {} elf_inp_file1 = options.elf_inp_file1 # It is valid for only one file to be "merged". This essentially just # strips off the section names. If second file name is not given then # set elf_inp_file2 to "" if options.elf_inp_file2: elf_inp_file2 = options.elf_inp_file2 else: elf_inp_file2 = "" # Do same for xbl_sec elf_inp_xbl_sec = options.elf_inp_xbl_sec if options.elf_inp_xbl_sec else "" binary_out = options.binary_out if options.elf_1_arch == '64': is_elf1_64_bit = True else: is_elf1_64_bit = False # If second filename is not given then set is_elf2_64_bit to false so it # can be passed even though it is not used. if options.elf_inp_file2: if options.elf_2_arch == '64': is_elf2_64_bit = True else: is_elf2_64_bit = False else: is_elf2_64_bit = False if options.elf_inp_xbl_sec: if options.elf_xbl_sec_arch == '64': is_elf_xbl_sec_64_bit = True else: is_elf_xbl_sec_64_bit = False else: is_elf_xbl_sec_64_bit = False # If output ELF arch is given then set is_out_elf_64_bit accordingly. # If not then default to be input1's setting if options.elf_out_arch: if options.elf_out_arch == '64': is_out_elf_64_bit = True else: is_out_elf_64_bit = False else: is_out_elf_64_bit = is_elf1_64_bit # Store ZI Out of Bounds value if not options.zi_oob: zi_oob_enabled = False else: zi_oob_enabled = True mbn_type = 'elf' header_format = 'reg' gen_dict['IMAGE_KEY_IMAGE_ID'] = mbn_tools.ImageType.APPSBL_IMG #gen_dict['IMAGE_KEY_IMAGE_SOURCE'] = 0 #gen_dict['IMAGE_KEY_IMAGE_DEST'] = 0 gen_dict['IMAGE_KEY_MBN_TYPE'] = mbn_type image_header_secflag = 'non_secure' source_base = os.path.splitext(str(binary_out))[0] target_base = os.path.splitext(str(binary_out))[0] merged_elf = source_base + "_merged.elf" source_elf = source_base + "_nohash.elf" target_hash = target_base + ".hash" target_hash_hd = target_base + "_hash.hd" target_phdr_elf = target_base + "_phdr.pbn" target_nonsec = target_base + "_combined_hash.mbn" #print "Input file 1:", elf_inp_file1 #print "Input file 2:", elf_inp_file2 #print "Output file:", binary_out merge_elfs([], elf_inp_file1, elf_inp_file2, elf_inp_xbl_sec, merged_elf, is_elf1_64_bit, is_elf2_64_bit, is_elf_xbl_sec_64_bit, is_out_elf_64_bit, zi_oob_enabled) # Hash the image if user did not explicitly say not to if options.hash_image: # Just copy the merged elf to the final output name shutil.move(merged_elf, binary_out) else: shutil.copy(merged_elf, source_elf) # Create hash table rv = mbn_tools.pboot_gen_elf([], source_elf, target_hash, elf_out_file_name = target_phdr_elf, secure_type = image_header_secflag) if rv: raise RuntimeError, "Failed to run pboot_gen_elf" # Create hash table header rv = mbn_tools.image_header([], gen_dict, target_hash, target_hash_hd, image_header_secflag, elf_file_name = source_elf) if rv: raise RuntimeError, "Failed to create image header for hash segment" files_to_cat_in_order = [target_hash_hd, target_hash] mbn_tools.concat_files (target_nonsec, files_to_cat_in_order) # Add the hash segment into the ELF mbn_tools.pboot_add_hash([], target_phdr_elf, target_nonsec, binary_out) return
target_pre_encrypt_elf = target_base + "_pre_encrypt.pbn" target_encrypt_xml = target_base + "_enc_md.xml" # Create elf header for RPM rv = mbn_tools.pboot_gen_elf([], source_full, target_hash, elf_out_file_name=target_phdr_elf, secure_type=image_header_secflag) if rv: raise RuntimeError, "Failed to run pboot_gen_elf" if not rv: rv = mbn_tools.image_header([], gen_dict, target_hash, target_hash_hd, image_header_secflag, elf_file_name=source_full) target_nonsec = target_base + "_combined_hash.mbn" target_nonsec_xml = target_base + "_combined_hash_xml.mbn" files_to_cat_in_order = [target_hash_hd, target_hash] mbn_tools.concat_files(target_nonsec, files_to_cat_in_order) #Add the hash segment into the ELF mbn_tools.pboot_add_hash([], target_add_hash_elf, target_nonsec, target_full) else: print "Enter mbn_type as elf"
#---------------------------------------------------------------------------- # Generate UEFI mbn #---------------------------------------------------------------------------- # MBN is generated in BIN format if mbn_type == 'bin': # Create mbn header for UEFI rv = mbn_tools.image_header(os.environ, gen_dict, source_full, target_base + ".hd", image_header_secflag, header_format) if rv: raise RuntimeError, "Failed to create mbn header" files_to_cat_in_order = [target_base + ".hd", source_full] mbn_tools.concat_files (target_full, files_to_cat_in_order) # MBN is generated in ELF format elif mbn_type == 'elf': source_elf = source_base + ".elf" target_hash = target_base + ".hash" target_hash_hd = target_base + "_hash.hd" target_phdr_elf = target_base + "_phdr.pbn" target_nonsec = target_base + "_combined_hash.mbn" # Create elf header for UEFI rv = mbn_tools.create_elf_header(target_base + ".hd", image_destination, image_size, is_elf_64_bit = is_elf_64_bit) if rv: raise RuntimeError, "Failed to create elf header"
def main(): parser = OptionParser(usage='usage: %prog [options] arguments') parser.add_option("-f", "--first_filepath", action="store", type="string", dest="elf_inp_file1", help="First ELF file to merge.") parser.add_option("-o", "--output_filepath", action="store", type="string", dest="binary_out", help="Merged filename and path.") parser.add_option("-v", "--mbn_version_number", action="store", type="string", dest="mbnv", help="Default MBN version is 3.") parser.add_option("-c", "--com compression option", action="store", type="string", dest="compress_method", help="Compression method") (options, args) = parser.parse_args() if not options.elf_inp_file1: parser.error('First ELF filename not given') if not options.binary_out: parser.error('Output filename not given') if options.mbnv != '6': mbnv = 3 else: mbnv = 6 if options.compress_method: compress_method = options.compress_method else: compress_method = "" gen_dict = {} elf_inp_file1 = options.elf_inp_file1 binary_out = options.binary_out mbn_type = 'elf' header_format = 'reg' gen_dict['IMAGE_KEY_IMAGE_ID'] = mbn_tools.ImageType.SBL1_IMG gen_dict['IMAGE_KEY_MBN_TYPE'] = mbn_type image_header_secflag = 'non_secure' source_base = os.path.splitext(str(binary_out))[0] target_base = os.path.splitext(str(binary_out))[0] source_elf = source_base + "_nohash.elf" target_hash = target_base + ".hash" target_hash_hd = target_base + "_hash.hd" target_phdr_elf = target_base + "_phdr.pbn" target_nonsec = target_base + "_combined_hash.mbn" # Create hash table rv = mbn_tools.pboot_gen_elf([], elf_inp_file1, target_hash, compress_method, elf_out_file_name=target_phdr_elf, secure_type=image_header_secflag, mbn_version=mbnv) if rv: raise RuntimeError, "Failed to run pboot_gen_elf" # Create hash table header rv = mbn_tools.image_header([], gen_dict, target_hash, target_hash_hd, image_header_secflag, elf_file_name=target_phdr_elf, mbn_version=mbnv) if rv: raise RuntimeError, "Failed to create image header for hash segment" files_to_cat_in_order = [target_hash_hd, target_hash] mbn_tools.concat_files(target_nonsec, files_to_cat_in_order) # Add the hash segment into the ELF mbn_tools.pboot_add_hash([], target_phdr_elf, target_nonsec, binary_out) return