efuse_bingen.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. import binascii
  2. import os.path
  3. import sys
  4. from xml.dom import minidom
  5. import argparse
  6. import re
  7. import base64
  8. import subprocess
  9. import os
  10. import hashlib
  11. import logging
  12. log_file_path = ""
  13. def convert_to_int_value(val, tag_name, attr_name=None) :
  14. if (val == "") or (val == 0) or (val == "0") :
  15. return 0
  16. if (val == 1) or (val == "1") :
  17. return 1
  18. if isinstance(val, basestring) : #check if "val" is string type (only string has lower() method)
  19. if val.lower() == "true" :
  20. return 1
  21. if val.lower() == "false" :
  22. return 0
  23. if attr_name : #has Atribute
  24. PrintError_RaiseException_StopBuild("Attribute Name: " + attr_name + " => I got value: \"" + str(val) + "\". The value should be: \"1\" or \"0\" or \"true\" or \"false\" or empty(=false)")
  25. else :
  26. PrintError_RaiseException_StopBuild("Tag Name: " + tag_name + " => I got value: \"" + str(val) + "\". The value should be: \"1\" or \"0\" or \"true\" or \"false\" or empty(=false)")
  27. def writeBitValueAndOneHexStringToFile(fstream, lstBitValue, hexString) :
  28. """
  29. Notice: The priority of bit input(lstBitValue) is higher than hex input(hexString) if hexString overlaps lstBitValue
  30. """
  31. result = 0;
  32. priority_mask = 0xFFFFFFFF
  33. for bit_number, value in lstBitValue :
  34. if value == 1 :
  35. result = result + (2 ** bit_number) #transform bit to decimal
  36. priority_mask = priority_mask & ~(2 ** bit_number)
  37. hexString2Decimal = int(hexString, 16) #transform hex to decimal
  38. result = (hexString2Decimal & priority_mask) | result #merge
  39. plain_hex_string = str(hex(result))[2:] #result is decimal, transform to hex
  40. plain_hex_string = plain_hex_string.zfill(8) #padding 0 at right to 8 digits
  41. lstHexArr = re.findall('..', plain_hex_string)
  42. fstream.write(chr(int(lstHexArr[3], 16)))
  43. fstream.write(chr(int(lstHexArr[2], 16)))
  44. fstream.write(chr(int(lstHexArr[1], 16)))
  45. fstream.write(chr(int(lstHexArr[0], 16)))
  46. def getBitValueAndOneHexString(lstBitValue, hexString) :
  47. """
  48. Notice: The priority of bit input(lstBitValue) is higher than hex input(hexString) if hexString overlaps lstBitValue
  49. This function is the same as function "writeBitValueAndOneHexStringToFile" except it return the merged value rather than writing to file
  50. """
  51. result = 0;
  52. priority_mask = 0xFFFFFFFF
  53. for bit_number, value in lstBitValue :
  54. if value == 1 :
  55. result = result + (2 ** bit_number) #transform bit to decimal
  56. priority_mask = priority_mask & ~(2 ** bit_number)
  57. hexString2Decimal = int(hexString, 16) #transform hex to decimal
  58. result = (hexString2Decimal & priority_mask) | result #merge
  59. plain_hex_string = str(hex(result))[2:] #result is decimal, transform to hex
  60. plain_hex_string = plain_hex_string.zfill(len(hexString)) #padding 0 at right to 8 digits
  61. return plain_hex_string.upper()
  62. def writeBitValueToFile(fstream, lstBitValue) :
  63. result = 0;
  64. for bit_number, value in lstBitValue :
  65. if value == 1 :
  66. result = result + (2 ** bit_number)
  67. plain_hex_string = str(hex(result))[2:] #result is decimal
  68. plain_hex_string = plain_hex_string.zfill(8)
  69. lstHexArr = re.findall('..', plain_hex_string)
  70. fstream.write(chr(int(lstHexArr[3], 16)))
  71. fstream.write(chr(int(lstHexArr[2], 16)))
  72. fstream.write(chr(int(lstHexArr[1], 16)))
  73. fstream.write(chr(int(lstHexArr[0], 16)))
  74. def get_file_sha256hexdigest(file_name):
  75. hash_result = ""
  76. with open(file_name) as f:
  77. m = hashlib.sha256()
  78. m.update(f.read())
  79. hash_result = m.hexdigest()
  80. return hash_result.upper()
  81. def writeHexStringToFile(fstream, hexString) :
  82. plain_hex_string = hexString.ljust(8, '0') #padding 0 at right to 8 digits
  83. lstHexArr = re.findall('..', plain_hex_string)
  84. fstream.write(chr(int(lstHexArr[0], 16)))
  85. fstream.write(chr(int(lstHexArr[1], 16)))
  86. fstream.write(chr(int(lstHexArr[2], 16)))
  87. fstream.write(chr(int(lstHexArr[3], 16)))
  88. def checkLessThan32BitsHexStringLength(tag_name, hexString, min_index, max_index) :
  89. if (max_index <= min_index) :
  90. PrintError_RaiseException_StopBuild("[Coding Error] Maximum index should be bigger than minimum index!")
  91. plain_hex_string = hexString.ljust(8, '0') #padding 0 at right to 8 digits
  92. lstHexArr = re.findall('..', plain_hex_string)
  93. result = 0;
  94. mask = 0xFFFFFFFF
  95. for bit_number in range(0, 32) :
  96. if (min_index <= bit_number <= max_index) :
  97. mask = mask & ~(2 ** bit_number)
  98. input_length_mask = int(lstHexArr[3] + lstHexArr[2] + lstHexArr[1] + lstHexArr[0], 16)
  99. forbidden_field_mask = mask
  100. # print(hex(input_length_mask))
  101. # print(hex(forbidden_field_mask))
  102. if (input_length_mask & forbidden_field_mask) != 0 :
  103. PrintError_RaiseException_StopBuild("Tag Name: " + tag_name + " (Wrong value length. The length of value should be \"" + tag_name + "[" + str(max_index) + ":" + str(min_index) + "]\")")
  104. def parseXmlTagAndAttribute(xml_file, tag_name, attr_name, inputValueLengthLimit=1, inputIsStringType=False) :
  105. if inputIsStringType :
  106. retVal = '0' * inputValueLengthLimit
  107. else :
  108. retVal = 0
  109. try:
  110. if not xml_file.getElementsByTagName(tag_name) :
  111. raise KeyError
  112. tag_number = len(xml_file.getElementsByTagName(tag_name))
  113. if tag_number > 1 :
  114. raise ValueError("Duplicated tag name. It appears " + str(tag_number) + " times!!")
  115. tmp_parse_value = xml_file.getElementsByTagName(tag_name)[0].attributes[attr_name].value
  116. if inputIsStringType :
  117. if (tmp_parse_value == "") :
  118. return retVal
  119. if (inputValueLengthLimit <> len(tmp_parse_value)) :
  120. raise ValueError("Wrong value length. The length of value should be: " + str(inputValueLengthLimit))
  121. if isinstance(tmp_parse_value, basestring) : #check if "tmp_parse_value" is string type (only string has upper() method)
  122. if not isValidHexString(tmp_parse_value) : #only string can be processed by regex
  123. raise ValueError("Wrong hex value type! The value should be within [0-9|A-F]")
  124. return tmp_parse_value.upper()
  125. return tmp_parse_value
  126. else : #not string
  127. return convert_to_int_value(tmp_parse_value, tag_name, attr_name)
  128. except IndexError:
  129. #Tag or Attribute not exist
  130. #printAndLog("[Warning][Not Exist] Tag Name: " + tag_name + ", Attribute Name: " + attr_name + " => Set to default value: " + str(retVal))
  131. return retVal
  132. except KeyError:
  133. #Tag or Attribute not exist
  134. #printAndLog("[Warning][Not Exist] Tag Name: " + tag_name + ", Attribute Name: " + attr_name + " => Set to default value: " + str(retVal))
  135. return retVal
  136. except ValueError as err:
  137. PrintError_RaiseException_StopBuild("Tag Name: " + tag_name + ", Attribute Name: " + attr_name + " (" + str(err) + ")")
  138. def parseXmlTagInnerValue(xml_file, tag_name, inputValueLengthLimit=1, inputIsStringType=False) :
  139. if inputIsStringType :
  140. retVal = '0' * inputValueLengthLimit
  141. else :
  142. retVal = 0
  143. try:
  144. if not xml_file.getElementsByTagName(tag_name) :
  145. raise KeyError
  146. tag_number = len(xml_file.getElementsByTagName(tag_name))
  147. if tag_number > 1 :
  148. raise ValueError("Duplicated tag name. It appears " + str(tag_number) + " times!!")
  149. tmp_parse_value = xml_file.getElementsByTagName(tag_name)[0].childNodes[0].data
  150. if inputIsStringType :
  151. if (tmp_parse_value == "") :
  152. return retVal
  153. if (inputValueLengthLimit <> len(tmp_parse_value)) :
  154. raise ValueError("Wrong value length. The length of value should be: " + str(inputValueLengthLimit))
  155. if isinstance(tmp_parse_value, basestring) : #check if "tmp_parse_value" is string type (only string has upper() method)
  156. if not isValidHexString(tmp_parse_value) : #only string can be processed by regex
  157. raise ValueError("Wrong hex value type! The value should be within [0-9|A-F]")
  158. return tmp_parse_value.upper()
  159. return tmp_parse_value
  160. else : #not string
  161. return convert_to_int_value(tmp_parse_value, tag_name)
  162. except IndexError:
  163. #Tag or Attribute not exist
  164. #printAndLog("[Warning][Not Exist] Tag Name: " + tag_name + " => Set to default value: " + str(retVal))
  165. return retVal
  166. except KeyError:
  167. #Tag or Attribute not exist
  168. #printAndLog("[Warning][Not Exist] Tag Name: " + tag_name + " => Set to default value: " + str(retVal))
  169. return retVal
  170. except ValueError as err:
  171. PrintError_RaiseException_StopBuild("Tag Name: " + tag_name + " (" + str(err) + ")")
  172. def isValidHexString(hex_input) :
  173. if re.match(r"^[0-9A-F]*$", hex_input, re.IGNORECASE) :
  174. return True
  175. return False
  176. def printAndLog(msg, criticalLevel=False):
  177. print(msg)
  178. global log_file_path
  179. if (log_file_path) :
  180. logging.basicConfig(format='[%(asctime)s] %(message)s', filename=log_file_path, level=logging.DEBUG)
  181. if criticalLevel :
  182. logging.critical(msg)
  183. else :
  184. logging.info(msg)
  185. def PrintError_RaiseException_StopBuild(err) :
  186. printAndLog("[Error] " + err, criticalLevel=True)
  187. raise Exception("[Error] " + err)
  188. def main():
  189. parser = argparse.ArgumentParser(description='MediaTek EFUSE XML Parser')
  190. parser.add_argument('--file', '-f',
  191. required=True,
  192. help='Provide the xml file')
  193. parser.add_argument('--output_bin_name', '-o',
  194. required=False,
  195. default='xml_output.bin',
  196. help='Provide output file name')
  197. parser.add_argument('--key_hash', '-k',
  198. required=False,
  199. help='Provide the file name path of key hash')
  200. parser.add_argument('--log_output_file', '-l',
  201. required=False,
  202. help='Provide the log output file name')
  203. args = parser.parse_args()
  204. if (args.log_output_file) :
  205. if os.path.isfile(args.log_output_file) :
  206. try :
  207. os.remove(args.log_output_file)
  208. except :
  209. pass
  210. global log_file_path
  211. log_file_path = args.log_output_file
  212. printAndLog("***************************************************************************")
  213. printAndLog("************** MediaTek 6755 EFUSE XML Parser ([MTK_XML2BIN]) **************")
  214. printAndLog("****************************** version 1.4.2 ******************************")
  215. printAndLog("***************************************************************************")
  216. printAndLog("Loading XML file: " + os.path.abspath(args.file))
  217. if os.path.isfile(args.output_bin_name) :
  218. os.remove(args.output_bin_name)
  219. printAndLog("Remove old image file: " + os.path.abspath(args.output_bin_name))
  220. printAndLog("-----------------------------------------------")
  221. if not os.path.isfile(args.file) :
  222. PrintError_RaiseException_StopBuild("XML file not exist!!")
  223. try :
  224. xml_file = minidom.parse(args.file)
  225. except Exception:
  226. printAndLog("[Error] ***** XML format is NOT CORRECT. Please check your XML input file. *****")
  227. printAndLog("[Error] ***** XML format is NOT CORRECT. Please check your XML input file. *****")
  228. PrintError_RaiseException_StopBuild("***** XML format is NOT CORRECT. Please check your XML input file. *****")
  229. #Parsing XML to variable
  230. printAndLog("Parsing XML file ...")
  231. #Parse String value
  232. EFUSE_KEY1 = parseXmlTagAndAttribute(xml_file, "magic-key", "key1", 8, True)
  233. EFUSE_KEY2 = parseXmlTagAndAttribute(xml_file, "magic-key", "key2", 8, True)
  234. EFUSE_ac_key = parseXmlTagInnerValue(xml_file, "ac-key", 32, True)
  235. EFUSE_usb_vid = parseXmlTagAndAttribute(xml_file, "usb-id", "vid", 4, True)
  236. EFUSE_usb_pid = parseXmlTagAndAttribute(xml_file, "usb-id", "pid", 4, True)
  237. EFUSE_c_data_0 = parseXmlTagInnerValue(xml_file, "c_data_0", 16, True)
  238. EFUSE_c_data_1 = parseXmlTagInnerValue(xml_file, "c_data_1", 16, True)
  239. EFUSE_c_ctrl1 = parseXmlTagInnerValue(xml_file, "c_ctrl1", 4, True)
  240. #Parse Integer value
  241. EFUSE_Disable_NAND_boot_speedup = parseXmlTagAndAttribute(xml_file, "common-ctrl", "Disable_NAND_boot_speedup")
  242. EFUSE_USB_download_type = parseXmlTagAndAttribute(xml_file, "common-ctrl", "USB_download_type")
  243. EFUSE_Disable_NAND_boot = parseXmlTagAndAttribute(xml_file, "common-ctrl", "Disable_NAND_boot")
  244. EFUSE_Disable_EMMC_boot = parseXmlTagAndAttribute(xml_file, "common-ctrl", "Disable_EMMC_boot")
  245. EFUSE_Enable_SW_JTAG_CON = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Enable_SW_JTAG_CON")
  246. EFUSE_Enable_Root_Cert = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Enable_Root_Cert")
  247. EFUSE_Enable_ACC = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Enable_ACC")
  248. EFUSE_Enable_ACK = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Enable_ACK")
  249. EFUSE_Enable_SLA = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Enable_SLA")
  250. EFUSE_Enable_DAA = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Enable_DAA")
  251. EFUSE_Enable_SBC = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Enable_SBC")
  252. EFUSE_Disable_JTAG = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Disable_JTAG")
  253. EFUSE_Disable_DBGPORT_LOCK = parseXmlTagAndAttribute(xml_file, "secure-ctrl", "Disable_DBGPORT_LOCK")
  254. EFUSE_c2k_sbc_en = parseXmlTagAndAttribute(xml_file, "c_ctrl_0", "c2k_sbc_en")
  255. EFUSE_md1_sbc_en = parseXmlTagAndAttribute(xml_file, "c_ctrl_0", "md1_sbc_en")
  256. EFUSE_disable_self_blow = parseXmlTagAndAttribute(xml_file, "c_ctrl_0", "disable_self_blow")
  257. EFUSE_c_ctrl1_lock = parseXmlTagAndAttribute(xml_file, "c_lock", "c_ctrl1_lock")
  258. EFUSE_c_ctrl0_lock = parseXmlTagAndAttribute(xml_file, "c_lock", "c_ctrl0_lock")
  259. EFUSE_c_data1_lock = parseXmlTagAndAttribute(xml_file, "c_lock", "c_data1_lock")
  260. EFUSE_c_data0_lock = parseXmlTagAndAttribute(xml_file, "c_lock", "c_data0_lock")
  261. """
  262. EFUSE_C_CTRL = parseXmlTagAndAttribute(xml_file, "cust-common-ctrl", "c_ctrl", 2, True)
  263. EFUSE_DISABLE_EFUSE = parseXmlTagAndAttribute(xml_file, "cust-common-ctrl", "DISABLE_EFUSE_BLOW")
  264. """
  265. EFUSE_com_ctrl_lock = parseXmlTagAndAttribute(xml_file, "common-lock", "com_ctrl_lock")
  266. EFUSE_usb_id_lock = parseXmlTagAndAttribute(xml_file, "common-lock", "usb_id_lock")
  267. EFUSE_sec_attr_lock = parseXmlTagAndAttribute(xml_file, "secure-lock", "sec_attr_lock")
  268. EFUSE_ackey_lock = parseXmlTagAndAttribute(xml_file, "secure-lock", "ackey_lock")
  269. EFUSE_sbc_pubk_hash_lock = parseXmlTagAndAttribute(xml_file, "secure-lock", "sbc_pubk_hash_lock")
  270. """
  271. if (int(EFUSE_C_CTRL, 16) & 0x80) != 0 : #bit[7] is not masked
  272. PrintError_RaiseException_StopBuild("C_CTRL should only have bit[6:0]. The bit[7] should not be set.")
  273. """
  274. checkLessThan32BitsHexStringLength("c_ctrl1", EFUSE_c_ctrl1, 0, 13);
  275. printAndLog("Parsing XML file ... Done")
  276. #Pre-process value
  277. printAndLog("-----------------------------------------------")
  278. #[Important] Please make sure the value of "print" is the same as xml value
  279. printAndLog("EFUSE_ac_key = " + str(EFUSE_ac_key))
  280. printAndLog("EFUSE_usb_vid = " + str(EFUSE_usb_vid))
  281. printAndLog("EFUSE_usb_pid = " + str(EFUSE_usb_pid))
  282. printAndLog("EFUSE_Disable_NAND_boot_speedup = " + str(EFUSE_Disable_NAND_boot_speedup))
  283. printAndLog("EFUSE_USB_download_type = " + str(EFUSE_USB_download_type))
  284. printAndLog("EFUSE_Disable_NAND_boot = " + str(EFUSE_Disable_NAND_boot))
  285. printAndLog("EFUSE_Disable_EMMC_boot = " + str(EFUSE_Disable_EMMC_boot))
  286. printAndLog("EFUSE_Enable_SW_JTAG_CON = " + str(EFUSE_Enable_SW_JTAG_CON))
  287. printAndLog("EFUSE_Enable_Root_Cert = " + str(EFUSE_Enable_Root_Cert))
  288. printAndLog("EFUSE_Enable_ACC = " + str(EFUSE_Enable_ACC))
  289. printAndLog("EFUSE_Enable_ACK = " + str(EFUSE_Enable_ACK))
  290. printAndLog("EFUSE_Enable_SLA = " + str(EFUSE_Enable_SLA))
  291. printAndLog("EFUSE_Enable_DAA = " + str(EFUSE_Enable_DAA))
  292. printAndLog("EFUSE_Enable_SBC = " + str(EFUSE_Enable_SBC))
  293. printAndLog("EFUSE_Disable_JTAG = " + str(EFUSE_Disable_JTAG))
  294. printAndLog("EFUSE_Disable_DBGPORT_LOCK = " + str(EFUSE_Disable_DBGPORT_LOCK))
  295. # printAndLog("EFUSE_DISABLE_EFUSE = " + str(EFUSE_DISABLE_EFUSE))
  296. # printAndLog("EFUSE_C_CTRL = " + str(hex(int(EFUSE_C_CTRL, 16) & 0x7F))[2:].zfill(2).upper()) #Mask 0x7F
  297. printAndLog("EFUSE_com_ctrl_lock = " + str(EFUSE_com_ctrl_lock))
  298. printAndLog("EFUSE_usb_id_lock = " + str(EFUSE_usb_id_lock))
  299. printAndLog("EFUSE_sec_attr_lock = " + str(EFUSE_sec_attr_lock))
  300. printAndLog("EFUSE_ackey_lock = " + str(EFUSE_ackey_lock))
  301. printAndLog("EFUSE_sbc_pubk_hash_lock = " + str(EFUSE_sbc_pubk_hash_lock))
  302. printAndLog("EFUSE_c_data_0 = " + str(EFUSE_c_data_0))
  303. printAndLog("EFUSE_c_data_1 = " + str(EFUSE_c_data_1))
  304. printAndLog("EFUSE_c_ctrl1 = " + str(EFUSE_c_ctrl1))
  305. printAndLog("EFUSE_c2k_sbc_en = " + str(EFUSE_c2k_sbc_en))
  306. printAndLog("EFUSE_md1_sbc_en = " + str(EFUSE_md1_sbc_en))
  307. printAndLog("EFUSE_disable_self_blow = " + str(EFUSE_disable_self_blow))
  308. printAndLog("EFUSE_c_ctrl1_lock = " + str(EFUSE_c_ctrl1_lock))
  309. printAndLog("EFUSE_c_ctrl0_lock = " + str(EFUSE_c_ctrl0_lock))
  310. printAndLog("EFUSE_c_data1_lock = " + str(EFUSE_c_data1_lock))
  311. printAndLog("EFUSE_c_data0_lock = " + str(EFUSE_c_data0_lock))
  312. printAndLog("-----------------------------------------------")
  313. EFUSE_SBC_PUBK_HASH = '0' * 64
  314. if args.key_hash :
  315. printAndLog("[Info] Loading SBC_PUBK_HASH from key hash file: " + os.path.abspath(args.key_hash))
  316. if os.path.isfile(args.key_hash) :
  317. try:
  318. with open(args.key_hash, 'r') as f:
  319. EFUSE_SBC_PUBK_HASH = f.read()
  320. except Exception:
  321. PrintError_RaiseException_StopBuild("***** Error while reading key hash file *****")
  322. EFUSE_SBC_PUBK_HASH = EFUSE_SBC_PUBK_HASH.strip()
  323. if EFUSE_SBC_PUBK_HASH == "" :
  324. PrintError_RaiseException_StopBuild("SBC_PUBK_HASH is empty and not generated")
  325. if len(EFUSE_SBC_PUBK_HASH) <> 64 :
  326. PrintError_RaiseException_StopBuild("SBC_PUBK_HASH is not in length 64. Current length of SBC_PUBK_HASH is: " + str(len(EFUSE_SBC_PUBK_HASH)))
  327. EFUSE_SBC_PUBK_HASH = EFUSE_SBC_PUBK_HASH.upper()
  328. if not isValidHexString(EFUSE_SBC_PUBK_HASH) :
  329. PrintError_RaiseException_StopBuild("SBC_PUBK_HASH contains invalid hex string(s)! The value should be within [0-9|A-F]")
  330. else :
  331. PrintError_RaiseException_StopBuild(args.key_hash + " is not generated from getKeyHash.sh for SBC_Key_Hash!!")
  332. else :
  333. printAndLog("[Info] SBC_PUBK_HASH is not loaded from key hash file.")
  334. EFUSE_SBC_PUBK_HASH = '0' * 64
  335. printAndLog("EFUSE_SBC_PUBK_HASH = " + EFUSE_SBC_PUBK_HASH)
  336. printAndLog("-----------------------------------------------")
  337. #usb-vid and usb-pid are special cases (The priority is important! PUT IT AFTER PRINT)
  338. EFUSE_usb_vid = EFUSE_usb_vid[2:4] + EFUSE_usb_vid[0:2]
  339. EFUSE_usb_pid = EFUSE_usb_pid[2:4] + EFUSE_usb_pid[0:2]
  340. with open(args.output_bin_name, "wb") as f :
  341. writeHexStringToFile(f, EFUSE_KEY1) #0x0
  342. writeHexStringToFile(f, EFUSE_KEY2) #0x4
  343. writeBitValueToFile(f, [(0, 0)]) #0x8
  344. writeHexStringToFile(f, EFUSE_ac_key[0:8]) #0xC
  345. writeHexStringToFile(f, EFUSE_ac_key[8:16]) #0x10
  346. writeHexStringToFile(f, EFUSE_ac_key[16:24]) #0x14
  347. writeHexStringToFile(f, EFUSE_ac_key[24:32]) #0x18
  348. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[0:8]) #0x1C
  349. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[8:16]) #0x20
  350. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[16:24]) #0x24
  351. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[24:32]) #0x28
  352. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[32:40]) #0x2C
  353. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[40:48]) #0x30
  354. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[48:56]) #0x34
  355. writeHexStringToFile(f, EFUSE_SBC_PUBK_HASH[56:64]) #0x38
  356. writeHexStringToFile(f, EFUSE_usb_pid) #0x3C (only 4 digits, auto padding to 8 digits in writeHexStringToFile)
  357. writeHexStringToFile(f, EFUSE_usb_vid) #0x40 (only 4 digits, auto padding to 8 digits in writeHexStringToFile)
  358. writeHexStringToFile(f, EFUSE_c_data_0[0:8]) #0x44
  359. writeHexStringToFile(f, EFUSE_c_data_0[8:16]) #0x48
  360. writeHexStringToFile(f, EFUSE_c_data_1[0:8]) #0x4C
  361. writeHexStringToFile(f, EFUSE_c_data_1[8:16]) #0x50
  362. writeBitValueToFile(f, [(0, EFUSE_Disable_EMMC_boot), (1, EFUSE_Disable_NAND_boot), (2, EFUSE_USB_download_type), (4, EFUSE_Disable_NAND_boot_speedup)]) #0x54
  363. writeBitValueToFile(f, [(0, EFUSE_Disable_JTAG), (1, EFUSE_Enable_SBC), (2, EFUSE_Enable_DAA), (3, EFUSE_Enable_SLA),(4, EFUSE_Enable_ACK), (5, EFUSE_Enable_ACC), (6, EFUSE_Enable_SW_JTAG_CON), (7, EFUSE_Enable_Root_Cert), (9, EFUSE_Disable_DBGPORT_LOCK)]) #0x58
  364. writeHexStringToFile(f, EFUSE_c_ctrl1) #0x5C
  365. writeBitValueToFile(f, [(0, EFUSE_c2k_sbc_en), (1, EFUSE_md1_sbc_en), (2, EFUSE_disable_self_blow)]) #0x60
  366. writeBitValueToFile(f, [(0, EFUSE_c_data0_lock), (1, EFUSE_c_data1_lock), (4, EFUSE_c_ctrl0_lock), (5, EFUSE_c_ctrl1_lock)]) #0x64
  367. """
  368. if EFUSE_DISABLE_EFUSE == 1 :
  369. writeBitValueAndOneHexStringToFile(f, [(7, 1)], EFUSE_C_CTRL) #0x50
  370. else :
  371. writeBitValueAndOneHexStringToFile(f, [(7, 0)], EFUSE_C_CTRL) #0x50 #[IMPORTANT] You MUST still set bit 7 to value 0 as a mask because the priority of bit input(lstBitValue) is higher than hex input(hexString).
  372. """
  373. writeBitValueToFile(f, [(0, EFUSE_sbc_pubk_hash_lock), (1, EFUSE_ackey_lock), (2, EFUSE_sec_attr_lock)]) #0x68
  374. writeBitValueToFile(f, [(1, EFUSE_usb_id_lock), (2, EFUSE_com_ctrl_lock)]) #0x6C
  375. #extends to 512 bytes file siz
  376. for i in range(92) : #0x5C
  377. writeBitValueToFile(f, [(0, 0)])
  378. bin_file_size_before_hash = os.path.getsize(args.output_bin_name)
  379. printAndLog("")
  380. sha256_hash = get_file_sha256hexdigest(args.output_bin_name)
  381. printAndLog("Image file(" + str(bin_file_size_before_hash) + " bytes) sha256 hash: " + sha256_hash)
  382. with open(args.output_bin_name, "ab") as f :
  383. writeHexStringToFile(f, sha256_hash[0:8]) #0x1E0
  384. writeHexStringToFile(f, sha256_hash[8:16]) #0x1E4
  385. writeHexStringToFile(f, sha256_hash[16:24]) #0x1E8
  386. writeHexStringToFile(f, sha256_hash[24:32]) #0x1EC
  387. writeHexStringToFile(f, sha256_hash[32:40]) #0x1F0
  388. writeHexStringToFile(f, sha256_hash[40:48]) #0x1F4
  389. writeHexStringToFile(f, sha256_hash[48:56]) #0x1F8
  390. writeHexStringToFile(f, sha256_hash[56:64]) #0x1FC
  391. printAndLog("Append sha256 hash to bin: Done!")
  392. bin_file_size = os.path.getsize(args.output_bin_name)
  393. printAndLog("")
  394. printAndLog("[Success] Write to bin: " + os.path.abspath(args.output_bin_name) + " (size: " + str(bin_file_size) + " bytes)")
  395. printAndLog("")
  396. if __name__ == '__main__':
  397. main()