mturney mturney has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/38558 )
Change subject: trogdor: update python scripts for python3 ......................................................................
trogdor: update python scripts for python3
Change-Id: I46525243729c1dbcd30b346d4603452eea14ad9d Signed-off-by: T Michael Turney mturney@codeaurora.org --- M util/qualcomm/createxbl.py M util/qualcomm/ipqheader.py M util/qualcomm/mbn_tools.py M util/qualcomm/mbncat.py M util/qualcomm/qgpt.py 5 files changed, 171 insertions(+), 158 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/58/38558/1
diff --git a/util/qualcomm/createxbl.py b/util/qualcomm/createxbl.py index 9c6a9bc..4cac0c4 100755 --- a/util/qualcomm/createxbl.py +++ b/util/qualcomm/createxbl.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 #============================================================================ # #/** @file createxbl.py @@ -237,9 +237,9 @@ 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 + #print("Input file 1:", elf_inp_file1) + #print("Input file 2:", elf_inp_file2) + #print("Output file:", binary_out)
merge_elfs([], elf_inp_file1, @@ -268,7 +268,7 @@ elf_out_file_name = target_phdr_elf, secure_type = image_header_secflag, header_version = header_version ) if rv: - raise RuntimeError, "Failed to run pboot_gen_elf" + raise RuntimeError("Failed to run pboot_gen_elf")
# Create hash table header rv = mbn_tools.image_header([], @@ -279,7 +279,7 @@ elf_file_name = source_elf, header_version = header_version) if rv: - raise RuntimeError, "Failed to create image header for hash segment" + 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) @@ -367,7 +367,7 @@
# Create a new ELF header for the output file if is_out_elf_64_bit: - out_elf_header = mbn_tools.Elf64_Ehdr('\0' * ELF64_HDR_SIZE) + out_elf_header = mbn_tools.Elf64_Ehdr(b'\0' * ELF64_HDR_SIZE) out_elf_header.e_phoff = ELF64_HDR_SIZE out_elf_header.e_ehsize = ELF64_HDR_SIZE out_elf_header.e_phentsize = ELF64_PHDR_SIZE @@ -382,7 +382,7 @@
out_elf_header.e_entry = elf_header1.e_entry else: - out_elf_header = mbn_tools.Elf32_Ehdr('\0' * ELF32_HDR_SIZE) + out_elf_header = mbn_tools.Elf32_Ehdr(b'\0' * ELF32_HDR_SIZE) out_elf_header.e_phoff = ELF32_HDR_SIZE out_elf_header.e_ehsize = ELF32_HDR_SIZE out_elf_header.e_phentsize = ELF32_PHDR_SIZE @@ -399,7 +399,7 @@ # Address needs to be verified that it is not greater than 32 bits # as it is possible to go from a 64 bit elf to 32. if (elf_header1.e_entry > 0xFFFFFFFF): - print "ERROR: File 1's entry point is too large to convert." + print("ERROR: File 1's entry point is too large to convert.") exit() out_elf_header.e_entry = elf_header1.e_entry
@@ -455,7 +455,7 @@ # Copy program header piece by piece to ensure possible conversion success if is_out_elf_64_bit == True: # Converting from 32 to 64 elf requires no data size validation - new_phdr = mbn_tools.Elf64_Phdr('\0' * ELF64_PHDR_SIZE) + new_phdr = mbn_tools.Elf64_Phdr(b'\0' * ELF64_PHDR_SIZE) new_phdr.p_type = curr_phdr.p_type new_phdr.p_offset = segment_offset new_phdr.p_vaddr = curr_phdr.p_vaddr @@ -468,7 +468,7 @@ # Converting from 64 to 32 elf requires data size validation # Note that there is an option to discard a segment if it is only ZI # and its address is greater than 32 bits - new_phdr = mbn_tools.Elf32_Phdr('\0' * ELF32_PHDR_SIZE) + new_phdr = mbn_tools.Elf32_Phdr(b'\0' * ELF32_PHDR_SIZE) new_phdr.p_type = curr_phdr.p_type new_phdr.p_offset = segment_offset
@@ -476,7 +476,7 @@ if (zi_oob_enabled == True) and (curr_phdr.p_filesz == 0): continue else: - print "ERROR: File 1 VAddr is too large for conversion." + print("ERROR: File 1 VAddr is too large for conversion.") exit() new_phdr.p_vaddr = curr_phdr.p_vaddr
@@ -484,33 +484,33 @@ if (zi_oob_enabled == True) and (curr_phdr.p_filesz == 0): continue else: - print "ERROR: File 1 PAddr is too large for conversion." + print("ERROR: File 1 PAddr is too large for conversion.") exit() new_phdr.p_paddr = curr_phdr.p_paddr
if curr_phdr.p_filesz > 0xFFFFFFFF: - print "ERROR: File 1 Filesz is too large for conversion." + print("ERROR: File 1 Filesz is too large for conversion.") exit() new_phdr.p_filesz = curr_phdr.p_filesz
if curr_phdr.p_memsz > 0xFFFFFFFF: - print "ERROR: File 1 Memsz is too large for conversion." + print("ERROR: File 1 Memsz is too large for conversion.") exit() new_phdr.p_memsz = curr_phdr.p_memsz
if curr_phdr.p_flags > 0xFFFFFFFF: - print "ERROR: File 1 Flags is too large for conversion." + print("ERROR: File 1 Flags is too large for conversion.") exit() new_phdr.p_flags = curr_phdr.p_flags
if curr_phdr.p_align > 0xFFFFFFFF: - print "ERROR: File 1 Align is too large for conversion." + print("ERROR: File 1 Align is too large for conversion.") exit() new_phdr.p_align = curr_phdr.p_align
- #print "i=",i - #print "phdr_offset=", phdr_offset + #print("i=",i) + #print("phdr_offset=", phdr_offset)
# update output file location to next phdr location elf_out_fp.seek(phdr_offset) @@ -519,14 +519,14 @@
inp_data_offset = curr_phdr.p_offset # used to read data from input file
-# print "inp_data_offset=" -# print inp_data_offset +# print("inp_data_offset=") +# print(inp_data_offset) # -# print "curr_phdr.p_offset=" -# print curr_phdr.p_offset +# print("curr_phdr.p_offset=") +# print(curr_phdr.p_offset) # -# print "curr_phdr.p_filesz=" -# print curr_phdr.p_filesz +# print("curr_phdr.p_filesz=") +# print(curr_phdr.p_filesz)
# output current phdr if is_out_elf_64_bit == False: @@ -553,7 +553,7 @@ # Copy program header piece by piece to ensure possible conversion success if is_out_elf_64_bit == True: # Converting from 32 to 64 elf requires no data size validation - new_phdr = mbn_tools.Elf64_Phdr('\0' * ELF64_PHDR_SIZE) + new_phdr = mbn_tools.Elf64_Phdr(b'\0' * ELF64_PHDR_SIZE) new_phdr.p_type = curr_phdr.p_type new_phdr.p_offset = segment_offset new_phdr.p_vaddr = curr_phdr.p_vaddr @@ -566,7 +566,7 @@ # Converting from 64 to 32 elf requires data size validation # Note that there is an option to discard a segment if it is only ZI # and its address is greater than 32 bits - new_phdr = mbn_tools.Elf32_Phdr('\0' * ELF32_PHDR_SIZE) + new_phdr = mbn_tools.Elf32_Phdr(b'\0' * ELF32_PHDR_SIZE) new_phdr.p_type = curr_phdr.p_type new_phdr.p_offset = segment_offset
@@ -574,7 +574,7 @@ if (zi_oob_enabled == True) and (curr_phdr.p_filesz == 0): continue else: - print "ERROR: File 2 VAddr is too large for conversion." + print("ERROR: File 2 VAddr is too large for conversion.") exit() new_phdr.p_vaddr = curr_phdr.p_vaddr
@@ -582,33 +582,33 @@ if (zi_oob_enabled == True) and (curr_phdr.p_filesz == 0): continue else: - print "ERROR: File 2 PAddr is too large for conversion." + print("ERROR: File 2 PAddr is too large for conversion.") exit() new_phdr.p_paddr = curr_phdr.p_paddr
if curr_phdr.p_filesz > 0xFFFFFFFF: - print "ERROR: File 2 Filesz is too large for conversion." + print("ERROR: File 2 Filesz is too large for conversion.") exit() new_phdr.p_filesz = curr_phdr.p_filesz
if curr_phdr.p_memsz > 0xFFFFFFFF: - print "ERROR: File 2 Memsz is too large for conversion." + print("ERROR: File 2 Memsz is too large for conversion.") exit() new_phdr.p_memsz = curr_phdr.p_memsz
if curr_phdr.p_flags > 0xFFFFFFFF: - print "ERROR: File 2 Flags is too large for conversion." + print("ERROR: File 2 Flags is too large for conversion.") exit() new_phdr.p_flags = curr_phdr.p_flags
if curr_phdr.p_align > 0xFFFFFFFF: - print "ERROR: File 2 Align is too large for conversion." + print("ERROR: File 2 Align is too large for conversion.") exit() new_phdr.p_align = curr_phdr.p_align
-# print "i=",i -# print "phdr_offset=", phdr_offset +# print("i=",i) +# print("phdr_offset=", phdr_offset)
# update output file location to next phdr location elf_out_fp.seek(phdr_offset) @@ -617,14 +617,14 @@
inp_data_offset = curr_phdr.p_offset # used to read data from input file
-# print "inp_data_offset=" -# print inp_data_offset +# print("inp_data_offset=") +# print(inp_data_offset) # -# print "curr_phdr.p_offset=" -# print curr_phdr.p_offset +# print("curr_phdr.p_offset=") +# print(curr_phdr.p_offset) # -# print "curr_phdr.p_filesz=" -# print curr_phdr.p_filesz +# print("curr_phdr.p_filesz=") +# print(curr_phdr.p_filesz)
# output current phdr if is_out_elf_64_bit == False: @@ -656,14 +656,14 @@ entry_seg_offset = phdr.p_offset break if entry_seg_offset == -1: - print "Error: Failed to find entry point in any segment!" + print("Error: Failed to find entry point in any segment!") exit() # magical equation for program header's phys and virt addr phys_virt_addr = entry_addr - entry_seg_offset
if is_out_elf_64_bit: # Converting from 32 to 64 elf requires no data size validation - new_phdr = mbn_tools.Elf64_Phdr('\0' * ELF64_PHDR_SIZE) + new_phdr = mbn_tools.Elf64_Phdr(b'\0' * ELF64_PHDR_SIZE) new_phdr.p_type = 0x1 new_phdr.p_offset = segment_offset new_phdr.p_vaddr = phys_virt_addr @@ -671,36 +671,36 @@ new_phdr.p_filesz = os.path.getsize(elf_in_file_xbl_sec) new_phdr.p_memsz = new_phdr.p_filesz if header_version == 5 or header_version == 6: - new_phdr.p_flags = (0x5 | - (mbn_tools.MI_PBT_XBL_SEC_SEGMENT << - mbn_tools.MI_PBT_FLAG_SEGMENT_TYPE_SHIFT)); + new_phdr.p_flags = (0x5 | + (mbn_tools.MI_PBT_XBL_SEC_SEGMENT << + mbn_tools.MI_PBT_FLAG_SEGMENT_TYPE_SHIFT)); else: - new_phdr.p_flags = 0x5 + new_phdr.p_flags = 0x5 new_phdr.p_align = 0x1000 else: # Converting from 64 to 32 elf requires data size validation # Don't discard the segment containing xbl_sec, simply error out # if the address is greater than 32 bits - new_phdr = mbn_tools.Elf32_Phdr('\0' * ELF32_PHDR_SIZE) + new_phdr = mbn_tools.Elf32_Phdr(b'\0' * ELF32_PHDR_SIZE) new_phdr.p_type = 0x1 # new_phdr.p_offset = segment_offset if header_version == 5 or header_version == 6: - new_phdr.p_flags = (0x5 | - (mbn_tools.MI_PBT_XBL_SEC_SEGMENT << - mbn_tools.MI_PBT_FLAG_SEGMENT_TYPE_SHIFT)); + new_phdr.p_flags = (0x5 | + (mbn_tools.MI_PBT_XBL_SEC_SEGMENT << + mbn_tools.MI_PBT_FLAG_SEGMENT_TYPE_SHIFT)); else: - new_phdr.p_flags = 0x5 + new_phdr.p_flags = 0x5 new_phdr.p_align = 0x1000
if phys_virt_addr > 0xFFFFFFFF: if zi_oob_enabled == False or curr_phdr.p_filesz != 0: - print "ERROR: File xbl_sec VAddr or PAddr is too big for conversion." + print("ERROR: File xbl_sec VAddr or PAddr is too big for conversion.") exit() new_phdr.p_vaddr = phys_virt_addr new_phdr.p_paddr = phys_virt_addr
if os.path.getsize(elf_in_file_xbl_sec) > 0xFFFFFFFF: - print "ERROR: File xbl_sec Filesz is too big for conversion." + print("ERROR: File xbl_sec Filesz is too big for conversion.") exit() new_phdr.p_filesz = os.path.getsize(elf_in_file_xbl_sec) new_phdr.p_memsz = new_phdr.p_filesz diff --git a/util/qualcomm/ipqheader.py b/util/qualcomm/ipqheader.py index 7615146..fe14e1f 100755 --- a/util/qualcomm/ipqheader.py +++ b/util/qualcomm/ipqheader.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # # Copyright (c) 2013 The Linux Foundation. All rights reserved. # @@ -97,7 +97,7 @@ if msg != None: sys.stderr.write("%s: %s\n" % (PROG_NAME, msg))
- print "Usage: %s <base-addr> <input-file> <output-file>" % PROG_NAME + print("Usage: %s <base-addr> <input-file> <output-file>" % PROG_NAME)
if msg != None: exit(1) diff --git a/util/qualcomm/mbn_tools.py b/util/qualcomm/mbn_tools.py index 9c403a7..a4a2f62 100755 --- a/util/qualcomm/mbn_tools.py +++ b/util/qualcomm/mbn_tools.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 #=============================================================================== # # MBN TOOLS @@ -103,24 +103,24 @@ ELFINFO_MAG1_INDEX = 1 ELFINFO_MAG2_INDEX = 2 ELFINFO_MAG3_INDEX = 3 -ELFINFO_MAG0 = '\x7f' -ELFINFO_MAG1 = 'E' -ELFINFO_MAG2 = 'L' -ELFINFO_MAG3 = 'F' +ELFINFO_MAG0 = 127 # 0x7F +ELFINFO_MAG1 = 69 # E +ELFINFO_MAG2 = 76 # L +ELFINFO_MAG3 = 70 # F ELFINFO_CLASS_INDEX = 4 -ELFINFO_CLASS_32 = '\x01' -ELFINFO_CLASS_64 = '\x02' +ELFINFO_CLASS_32 = 1 +ELFINFO_CLASS_64 = 2 ELFINFO_VERSION_INDEX = 6 -ELFINFO_VERSION_CURRENT = '\x01' +ELFINFO_VERSION_CURRENT = 1 ELF_BLOCK_ALIGN = 0x1000 ALIGNVALUE_1MB = 0x100000 ALIGNVALUE_4MB = 0x400000 -ELFINFO_DATA2LSB = '\x01' -ELFINFO_EXEC_ETYPE = '\x02\x00' -ELFINFO_ARM_MACHINETYPE = '\x28\x00' -ELFINFO_VERSION_EV_CURRENT = '\x01\x00\x00\x00' +ELFINFO_DATA2LSB = b'\x01' +ELFINFO_EXEC_ETYPE = b'\x02\x00' +ELFINFO_ARM_MACHINETYPE = b'\x28\x00' +ELFINFO_VERSION_EV_CURRENT = b'\x01\x00\x00\x00' ELFINFO_SHOFF = 0x00 -ELFINFO_PHNUM = '\x01\x00' +ELFINFO_PHNUM = b'\x01\x00' ELFINFO_RESERVED = 0x00
# ELF Program Header Types @@ -334,9 +334,9 @@ self.e_version = unpacked_data[3]
def printValues(self): - print "ATTRIBUTE / VALUE" - for attr, value in self.__dict__.iteritems(): - print attr, value + print("ATTRIBUTE / VALUE") + for attr, value in self.__dict__.items(): + print(attr, value)
@@ -366,9 +366,9 @@ self.e_shstrndx = unpacked_data[13]
def printValues(self): - print "ATTRIBUTE / VALUE" - for attr, value in self.__dict__.iteritems(): - print attr, value + print("ATTRIBUTE / VALUE") + for attr, value in self.__dict__.items(): + print(attr, value)
def getPackedData(self): values = [self.e_ident, @@ -410,9 +410,9 @@ self.p_align = unpacked_data[7]
def printValues(self): - print "ATTRIBUTE / VALUE" - for attr, value in self.__dict__.iteritems(): - print attr, value + print("ATTRIBUTE / VALUE") + for attr, value in self.__dict__.items(): + print(attr, value)
def getPackedData(self): values = [self.p_type, @@ -453,12 +453,16 @@ self.e_shstrndx = unpacked_data[13]
def printValues(self): - print "ATTRIBUTE / VALUE" - for attr, value in self.__dict__.iteritems(): - print attr, value + print("ATTRIBUTE / VALUE") + for attr, value in self.__dict__.items(): + print(attr, value)
def getPackedData(self): - values = [self.e_ident, + if type(self.e_ident) == str: + packvalue = bytes(self.e_ident, 'utf-8') + else: + packvalue = self.e_ident + values = [packvalue, self.e_type, self.e_machine, self.e_version, @@ -497,9 +501,9 @@ self.p_align = unpacked_data[7]
def printValues(self): - print "ATTRIBUTE / VALUE" - for attr, value in self.__dict__.iteritems(): - print attr, value + print("ATTRIBUTE / VALUE") + for attr, value in self.__dict__.items(): + print(attr, value)
def getPackedData(self): values = [self.p_type, @@ -522,7 +526,7 @@ def __init__(self): self.flag = 0 def printValues(self): - print 'Flag: ' + str(self.flag) + print('Flag: ' + str(self.flag))
#---------------------------------------------------------------------------- # Regular Boot Header Class @@ -744,7 +748,7 @@ xml_target_file.close() else: xml_target_file.close() - raise RuntimeError, "XML Size too large: " + str(xml_header_size) + raise RuntimeError("XML Size too large: " + str(xml_header_size))
#---------------------------------------------------------------------------- # encrypt_mbn @@ -835,7 +839,7 @@ is_elf_64_bit = False):
if (output_file_name is None): - raise RuntimeError, "Requires a ELF header file" + raise RuntimeError("Requires a ELF header file")
# Create a elf header and program header # Write the headers to the output file @@ -926,13 +930,13 @@
# Preliminary checks if (requires_preamble is True) and (preamble_file_name is None): - raise RuntimeError, "Image Header requires a preamble file" + raise RuntimeError("Image Header requires a preamble file")
if (gen_dict['IMAGE_KEY_MBN_TYPE'] == 'elf') and (elf_file_name is None): - raise RuntimeError, "ELF Image Headers require an elf file" + raise RuntimeError("ELF Image Headers require an elf file")
if (in_code_size is None) and (os.path.exists(code_file_name) is False): - raise RuntimeError, "Code size unavailable, and input file does not exist" + raise RuntimeError("Code size unavailable, and input file does not exist")
# Initialize if in_code_size is not None: @@ -1024,7 +1028,7 @@ boot_header.writePackedData(target = output_file_name, write_full_hdr = write_full_hdr)
else: - raise RuntimeError, "Header format not supported: " + str(header_format) + raise RuntimeError("Header format not supported: " + str(header_format)) return 0
@@ -1073,15 +1077,15 @@ hashtable_shift = 0
if elf_header.e_ident[ELFINFO_CLASS_INDEX] == ELFINFO_CLASS_64: - new_phdr = Elf64_Phdr('\0' * ELF64_PHDR_SIZE) + new_phdr = Elf64_Phdr(b'\0' * ELF64_PHDR_SIZE) elf_header_size = ELF64_HDR_SIZE is_elf64 = True else: - new_phdr = Elf32_Phdr('\0' * ELF32_PHDR_SIZE) + new_phdr = Elf32_Phdr(b'\0' * ELF32_PHDR_SIZE) elf_header_size = ELF32_HDR_SIZE is_elf64 = False
- hash = '\0' * MI_PROG_BOOT_DIGEST_SIZE + hash = b'\0' * MI_PROG_BOOT_DIGEST_SIZE phdr_start = 0 bytes_to_pad = 0 hash_seg_end = 0 @@ -1090,7 +1094,7 @@ if elf_out_file_name is not None: # Assert limit on number of program headers in input ELF if num_phdrs > MAX_PHDR_COUNT: - raise RuntimeError, "Input ELF has exceeded maximum number of program headers" + raise RuntimeError("Input ELF has exceeded maximum number of program headers")
# Create new program header for the ELF Header + Program Headers new_phdr.p_type = NULL_TYPE @@ -1100,11 +1104,11 @@ elf_header.e_phnum += 2
# Create an empty hash entry for PHDR_TYPE - hash_out_fp.write('\0' * MI_PROG_BOOT_DIGEST_SIZE) + hash_out_fp.write(b'\0' * MI_PROG_BOOT_DIGEST_SIZE) hashtable_size += MI_PROG_BOOT_DIGEST_SIZE
# Create an empty hash entry for the hash segment itself - hash_out_fp.write('\0' * MI_PROG_BOOT_DIGEST_SIZE) + hash_out_fp.write(b'\0' * MI_PROG_BOOT_DIGEST_SIZE) hashtable_size += MI_PROG_BOOT_DIGEST_SIZE
# Begin hash table generation @@ -1124,7 +1128,7 @@
# Seg_size should be page aligned if (seg_size & (ELF_BLOCK_ALIGN - 1)) > 0: - raise RuntimeError, "seg_size: " + hex(seg_size) + " is not ELF page aligned!" + raise RuntimeError("seg_size: " + hex(seg_size) + " is not ELF page aligned!")
off = seg_offset + seg_size
@@ -1141,7 +1145,7 @@ if MI_PBT_CHECK_FLAG_TYPE(curr_phdr.p_flags) is True: hash = generate_hash(fbuf, sha_algo) else: - hash = '\0' * MI_PROG_BOOT_DIGEST_SIZE + hash = b'\0' * MI_PROG_BOOT_DIGEST_SIZE
# Write hash to file hash_out_fp.write(hash) @@ -1160,7 +1164,7 @@ if (MI_PBT_CHECK_FLAG_TYPE(curr_phdr.p_flags) is True) and (data_len > 0): hash = generate_hash(file_buff, sha_algo) else: - hash = '\0' * MI_PROG_BOOT_DIGEST_SIZE + hash = b'\0' * MI_PROG_BOOT_DIGEST_SIZE
# Write hash to file hash_out_fp.write(hash) @@ -1186,9 +1190,9 @@ if (hash_seg_max_size is not None): # Error checking for hash segment size validity if hashtable_size > hash_seg_max_size: - raise RuntimeError, "Hash table exceeds maximum hash segment size: " + hex(hash_seg_max_size) + raise RuntimeError("Hash table exceeds maximum hash segment size: " + hex(hash_seg_max_size)) if (hash_seg_max_size & (ELF_BLOCK_ALIGN-1)) is not 0: - raise RuntimeError, "Hash segment size passed is not ELF Block Aligned: " + hex(hash_seg_max_size) + raise RuntimeError("Hash segment size passed is not ELF Block Aligned: " + hex(hash_seg_max_size))
# Check if hash physical address parameter was passed if last_phys_addr is not None: @@ -1331,7 +1335,7 @@ file_copy_offset(hash_tbl_fp, 0, elf_out_fp, hash_hdr_offset, hash_size)
else: - raise RuntimeError, "Hash segment program header not found in file " + elf_in_file_name + raise RuntimeError("Hash segment program header not found in file " + elf_in_file_name)
# Close files elf_in_fp.close() @@ -1346,7 +1350,7 @@ def image_auth(env, *args):
if len(args) < 7 or len(args) > 8: - raise RuntimeError, "Usage Invalid" + raise RuntimeError("Usage Invalid")
# Initialize File Names binary_in = args[0] @@ -1376,7 +1380,7 @@ num_certs = num_certs + 1
if (num_certs == 0): - raise RuntimeError, "Missing file(s) required for signing.\n" + raise RuntimeError("Missing file(s) required for signing.\n")
# Create the Certificate Chain concat_files (cert_chain_out, cert_list) @@ -1390,7 +1394,7 @@ pad_file(cert_fp, bytes_to_pad, PAD_BYTE_1) cert_fp.close() else: - raise RuntimeError, "Certificate Size too large: " + str(cert_size) + raise RuntimeError("Certificate Size too large: " + str(cert_size))
# Create the Final Signed Image File concat_files (signed_image_out, [binary_in, signature, cert_chain_out]) @@ -1495,7 +1499,7 @@
# Check for corresponding number of segments if len(segment_list) is not elf_header.e_phnum: - raise RuntimeError, 'SCL file and ELF file have different number of segments!' + raise RuntimeError('SCL file and ELF file have different number of segments!')
# Go to the start of the p_flag entry in the first program header file_offset = elf_header.e_phoff + phdr_flag_off @@ -1602,11 +1606,11 @@ (curr_phdr.p_flags & PH_PERM_MASK) == PH_PERM_RX and curr_pages == code_seg_pages): if (code_seg_idx != -1): - raise RuntimeError, 'Multiple code segments match for: ' + code_seg_pages + ' pages' + raise RuntimeError('Multiple code segments match for: ' + code_seg_pages + ' pages') code_seg_idx = i
if (code_seg_idx == -1): - raise RuntimeError, 'No matching code segment found' + raise RuntimeError('No matching code segment found')
code_phdr = phdr_table[code_seg_idx]
@@ -1680,7 +1684,7 @@ # Token 1: Segment Name # Token 2: Start Address -- not used in MBN tools if len(tokens) < 2: - raise RuntimeError, 'SCL Segment Syntax malformed: ' + previous_line + raise RuntimeError('SCL Segment Syntax malformed: ' + previous_line)
# Get the segment flags corresponding to the segment name description new_scl_entry.flag = getSegmentFlag(tokens[0].strip(strip_chars)) @@ -1727,7 +1731,7 @@ UNSECURE = "UNSECURE"
if seg_info is None or len(seg_info) is 0: - raise RuntimeError, 'Invalid segment information passed: ' + seg_info + raise RuntimeError('Invalid segment information passed: ' + seg_info)
# Conditional checks and assignments of the corresponding segment flag values if NOTPAGEABLE in seg_info: @@ -1789,7 +1793,7 @@ ret_val = MI_PBT_ELF_UNSECURE_SEGMENT
else: - raise RuntimeError, 'The segment name is wrongly defined in the SCL file: ' + seg_info + raise RuntimeError('The segment name is wrongly defined in the SCL file: ' + seg_info)
return ret_val
@@ -1800,7 +1804,7 @@ def pad_file(fp, num_bytes, value):
if num_bytes < 0: - raise RuntimeError, "Number of bytes to pad must be greater than zero" + raise RuntimeError("Number of bytes to pad must be greater than zero")
while num_bytes > 0: fp.write('%c' % value) @@ -1869,7 +1873,7 @@ def populate_dictionary(*args):
if len(args) < 1: - raise RuntimeError, "At least 1 file must be specified as an input" + raise RuntimeError("At least 1 file must be specified as an input")
global_dict = {} Fields = ["Define", "Key", "Value"] @@ -1922,11 +1926,11 @@ # Check for Image Type # If IMAGE_TYPE parameter is not provided, raise error if not kwargs.has_key('IMAGE_TYPE'): - raise RuntimeError, "IMAGE_TYPE must be defined to use FilterDictionary." + raise RuntimeError("IMAGE_TYPE must be defined to use FilterDictionary.") else: image_type = kwargs.get('IMAGE_TYPE') if type(image_type) is not str: - raise RuntimeError, "IMAGE_TYPE must be of string type." + raise RuntimeError("IMAGE_TYPE must be of string type.")
# Check for Flash Type # If FLASH_TYPE parameter is not provided, default to 'nand' @@ -1935,7 +1939,7 @@ else: flash_type = kwargs.get('FLASH_TYPE') if type(flash_type) is not str: - raise RuntimeError, "FLASH_TYPE must be of string type. " + raise RuntimeError("FLASH_TYPE must be of string type. ")
# Check for MBN Type # If MBN_TYPE parameter is not provided, default to 'elf' @@ -1944,7 +1948,7 @@ else: mbn_type = kwargs.get('MBN_TYPE') if mbn_type != 'elf' and mbn_type != 'bin': - raise RuntimeError, "MBN_TYPE currently not supported: " + mbn_type + raise RuntimeError("MBN_TYPE currently not supported: " + mbn_type)
# Check for Image ID # If IMAGE_ID parameter is not provided, default to ID 0 @@ -1953,7 +1957,7 @@ else: image_id = kwargs.get('IMAGE_ID') if type(image_id) is not int: - raise RuntimeError, "IMAGE_ID must be of integer type." + raise RuntimeError("IMAGE_ID must be of integer type.")
# Initialize gen_dict = {} @@ -1978,9 +1982,9 @@ if template_key_match in global_dict: image_dest = global_dict[template_key_match] else: - raise RuntimeError, "Builds file does not have IMAGE_KEY pair for: " + image_type + raise RuntimeError("Builds file does not have IMAGE_KEY pair for: " + image_type) else: - raise RuntimeError, "MBN_TYPE currently not supported: " + mbn_type + raise RuntimeError("MBN_TYPE currently not supported: " + mbn_type)
# Assign generic dictionary key/value pairs gen_dict['IMAGE_KEY_IMAGE_ID'] = id @@ -2011,7 +2015,7 @@ gen_dict['IMAGE_KEY_OEM_NUM_ROOT_CERTS'] = oem_num_root_certs
else: - raise RuntimeError, "Invalid OEM root certificate configuration values" + raise RuntimeError("Invalid OEM root certificate configuration values")
# Assign additional dictionary key/values pair as needed by tools.
@@ -2041,7 +2045,7 @@ elf_header = Elf_Ehdr_common(elf_fp.read(ELF_HDR_COMMON_SIZE))
if verify_elf_header(elf_header) is False: - raise RuntimeError, "ELF file failed verification: " + elf_file_name + raise RuntimeError("ELF file failed verification: " + elf_file_name)
elf_fp.seek(0)
@@ -2054,7 +2058,7 @@
# Verify ELF header information if verify_elf_header(elf_header) is False: - raise RuntimeError, "ELF file failed verification: " + elf_file_name + raise RuntimeError("ELF file failed verification: " + elf_file_name)
# Get program header size phdr_size = elf_header.e_phentsize @@ -2104,17 +2108,26 @@ # Verify ELF header contents from an input ELF file #---------------------------------------------------------------------------- def verify_elf_header(elf_header): - if (elf_header.e_ident[ELFINFO_MAG0_INDEX] != ELFINFO_MAG0) or \ - (elf_header.e_ident[ELFINFO_MAG1_INDEX] != ELFINFO_MAG1) or \ - (elf_header.e_ident[ELFINFO_MAG2_INDEX] != ELFINFO_MAG2) or \ - (elf_header.e_ident[ELFINFO_MAG3_INDEX] != ELFINFO_MAG3) or \ - ((elf_header.e_ident[ELFINFO_CLASS_INDEX] != ELFINFO_CLASS_64) and \ - (elf_header.e_ident[ELFINFO_CLASS_INDEX] != ELFINFO_CLASS_32)) or \ - (elf_header.e_ident[ELFINFO_VERSION_INDEX] != ELFINFO_VERSION_CURRENT): - + if (elf_header.e_ident[ELFINFO_MAG0_INDEX] != ELFINFO_MAG0): + print("MAG0[{:d}]\n".format((elf_header.e_ident[ELFINFO_MAG0_INDEX]))) return False - else: - return True + if (elf_header.e_ident[ELFINFO_MAG1_INDEX] != ELFINFO_MAG1): + print("MAG1[{:d}]\n".format((elf_header.e_ident[ELFINFO_MAG1_INDEX]))) + return False + if (elf_header.e_ident[ELFINFO_MAG2_INDEX] != ELFINFO_MAG2): + print("MAG2[{:d}]\n".format((elf_header.e_ident[ELFINFO_MAG2_INDEX]))) + return False + if (elf_header.e_ident[ELFINFO_MAG3_INDEX] != ELFINFO_MAG3): + print("MAG3[{:d}]\n".format((elf_header.e_ident[ELFINFO_MAG3_INDEX]))) + return False + if ((elf_header.e_ident[ELFINFO_CLASS_INDEX] != ELFINFO_CLASS_64) and \ + (elf_header.e_ident[ELFINFO_CLASS_INDEX] != ELFINFO_CLASS_32)): + print("ELFINFO_CLASS_INDEX[{:d}]\n".format((elf_header.e_ident[ELFINFO_CLASS_INDEX]))) + return False + if (elf_header.e_ident[ELFINFO_VERSION_INDEX] != ELFINFO_VERSION_CURRENT): + print("ELFINFO_VERSION_INDEX[{:d}]\n".format((elf_header.e_ident[ELFINFO_VERSION_INDEX]))) + return False + return True
#---------------------------------------------------------------------------- # Perform file copy given offsets and the number of bytes to copy @@ -2166,9 +2179,9 @@
# Update the hash table program header if is_elf64 is True: - hash_Phdr = Elf64_Phdr('\0'*ELF64_PHDR_SIZE) + hash_Phdr = Elf64_Phdr(b'\0'*ELF64_PHDR_SIZE) else: - hash_Phdr = Elf32_Phdr('\0'*ELF32_PHDR_SIZE) + hash_Phdr = Elf32_Phdr(b'\0'*ELF32_PHDR_SIZE) hash_Phdr.p_flags = MI_PBT_ELF_HASH_SEGMENT hash_Phdr.p_align = ELF_BLOCK_ALIGN hash_Phdr.p_offset = hash_hdr_offset @@ -2250,7 +2263,7 @@ try: fp = open(file_name, mode) except IOError: - raise RuntimeError, "The file could not be opened: " + file_name + raise RuntimeError("The file could not be opened: " + file_name)
# File open has succeeded with the given mode, return the file object return fp diff --git a/util/qualcomm/mbncat.py b/util/qualcomm/mbncat.py index c4da265..4113bc2 100755 --- a/util/qualcomm/mbncat.py +++ b/util/qualcomm/mbncat.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # Copyright (c) 2014, The Linux Foundation. All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -75,27 +75,27 @@ self.verbose = verbose self.mbn_file_names = [] if self.verbose: - print 'Reading ' + sbl1 + print('Reading ' + sbl1)
try: self.sbl1 = open(sbl1, 'rb').read() except IOError as e: - print 'I/O error({0}): {1}'.format(e.errno, e.strerror) + print('I/O error({0}): {1}'.format(e.errno, e.strerror)) raise
(codeword, magic, _) = struct.unpack_from( self.NOR_SBL1_HEADER, self.sbl1)
if codeword != self.NOR_CODE_WORD: - print '\n\nError: Unexpected Codeword!' - print 'Codeword : ' + ('0x%x' % self.NOR_CODE_WORD) + \ - ' != ' + ('0x%x' % codeword) + print('\n\nError: Unexpected Codeword!') + print('Codeword : ' + ('0x%x' % self.NOR_CODE_WORD) + \ + ' != ' + ('0x%x' % codeword)) sys.exit(-1)
if magic != self.MAGIC_NUM: - print '\n\nError: Unexpected Magic!' - print 'Magic : ' + ('0x%x' % self.MAGIC_NUM) + \ - ' != ' + ('0x%x' % magic) + print('\n\nError: Unexpected Magic!') + print('Magic : ' + ('0x%x' % self.MAGIC_NUM) + \ + ' != ' + ('0x%x' % magic)) sys.exit(-1)
def Append(self, src): @@ -122,7 +122,7 @@ pad = '\377' * pad_size outfile.write(pad) if self.verbose: - print 'Added %d byte padding' % pad_size + print('Added %d byte padding' % pad_size) return pad_size return 0
@@ -145,8 +145,8 @@ mbn_file_data = open(mbn_file_name, 'r').read() outfile.write(mbn_file_data) if self.verbose: - print 'Added %s (%d bytes)' % (mbn_file_name, - len(mbn_file_data)) + print('Added %s (%d bytes)' % (mbn_file_name, + len(mbn_file_data))) total_size += len(mbn_file_data)
outfile.seek(28) @@ -155,13 +155,13 @@
def Usage(v): - print '%s: [-v] [-h] [-o Output MBN] sbl1 sbl2 [bootblock]' % ( - os.path.basename(sys.argv[0])) - print - print 'Concatenates up to three mbn files: two SBLs and a coreboot bootblock' - print ' -h This message' - print ' -v verbose' - print ' -o Output file name, (default: %s)\n' % DEFAULT_OUTPUT_FILE_NAME + print('%s: [-v] [-h] [-o Output MBN] sbl1 sbl2 [bootblock]' % ( + os.path.basename(sys.argv[0]))) + print() + print('Concatenates up to three mbn files: two SBLs and a coreboot bootblock') + print(' -h This message') + print(' -v verbose') + print(' -o Output file name, (default: %s)\n' % DEFAULT_OUTPUT_FILE_NAME) sys.exit(v)
def main(): diff --git a/util/qualcomm/qgpt.py b/util/qualcomm/qgpt.py index 0b096b9..fbf6d10 100755 --- a/util/qualcomm/qgpt.py +++ b/util/qualcomm/qgpt.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 #============================================================================ # #/** @file qgpt.py