rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | import sys |
| 2 | import os |
| 3 | import struct |
| 4 | script_folder, script_name = os.path.split(os.path.realpath(__file__)) |
| 5 | sys.path.append(os.path.join(script_folder, "lib")) |
| 6 | import gfh |
| 7 | import cert |
| 8 | |
| 9 | def get_file_sizeb(file_path): |
| 10 | if not os.path.isfile(file_path): |
| 11 | return 0 |
| 12 | file_handle = open(file_path, "rb") |
| 13 | file_handle.seek(0, 2) |
| 14 | file_size = file_handle.tell() |
| 15 | file_handle.close() |
| 16 | return file_size |
| 17 | |
| 18 | def concatb(file1_path, file2_path): |
| 19 | file1_size = get_file_sizeb(file1_path) |
| 20 | file2_size = get_file_sizeb(file2_path) |
| 21 | file1 = open(file1_path, "ab+") |
| 22 | file2 = open(file2_path, "rb") |
| 23 | file1.write(file2.read(file2_size)) |
| 24 | file2.close() |
| 25 | file1.close() |
| 26 | |
| 27 | class tool_auth: |
| 28 | def __init__(self, out_path, tool_auth_path): |
| 29 | self.m_out_path = out_path |
| 30 | if not os.path.exists(self.m_out_path): |
| 31 | os.makedirs(self.m_out_path) |
| 32 | self.m_tool_auth_path = tool_auth_path |
| 33 | self.m_gfh = gfh.image_gfh() |
| 34 | self.m_sig_handler = None |
| 35 | def create_gfh(self, gfh_config): |
| 36 | self.m_gfh.load_ini(gfh_config) |
| 37 | return |
| 38 | def sign(self, key_ini_path): |
| 39 | #tool auth contains only gfh and signature, no extra content |
| 40 | self.m_gfh.finalize(0, key_ini_path) |
| 41 | #write tbs_tool_auth |
| 42 | tbs_toolauth_file_path = os.path.join(self.m_out_path, "tbs_toolauth.bin") |
| 43 | tbs_tool_auth_file = open(tbs_toolauth_file_path, "wb") |
| 44 | tbs_tool_auth_file.write(self.m_gfh.pack()) |
| 45 | tbs_tool_auth_file.close() |
| 46 | print "===tool_auth signing===" |
| 47 | if self.m_gfh.get_sig_type() == "SINGLE": |
| 48 | self.m_sig_handler = cert.sig_single(self.m_gfh.get_pad_type()) |
| 49 | self.m_sig_handler.set_out_path(self.m_out_path) |
| 50 | self.m_sig_handler.create(key_ini_path, tbs_toolauth_file_path) |
| 51 | self.m_sig_handler.sign() |
| 52 | sig_name = "toolauth.sig" |
| 53 | sig_file_path = os.path.join(self.m_out_path, sig_name) |
| 54 | self.m_sig_handler.output(self.m_out_path, sig_name) |
| 55 | #create final toolauth file |
| 56 | if os.path.isfile(self.m_tool_auth_path): |
| 57 | os.remove(self.m_tool_auth_path) |
| 58 | concatb(self.m_tool_auth_path, tbs_toolauth_file_path) |
| 59 | concatb(self.m_tool_auth_path, sig_file_path) |
| 60 | else: |
| 61 | print "unknown signature type" |
| 62 | #clean up |
| 63 | os.remove(tbs_toolauth_file_path) |
| 64 | os.remove(sig_file_path) |
| 65 | return |
| 66 | |
| 67 | def main(): |
| 68 | #parameter parsing |
| 69 | idx = 1 |
| 70 | key_ini_path = "" |
| 71 | gfh_config_ini_path = "" |
| 72 | while idx < len(sys.argv): |
| 73 | if sys.argv[idx][0] == '-': |
| 74 | if sys.argv[idx][1] == 'i': |
| 75 | print "key: " + sys.argv[idx + 1] |
| 76 | key_ini_path = sys.argv[idx + 1] |
| 77 | idx += 2 |
| 78 | elif sys.argv[idx][1] == 'g': |
| 79 | print "gfh: " + sys.argv[idx + 1] |
| 80 | gfh_config_ini_path = sys.argv[idx + 1] |
| 81 | idx += 2 |
| 82 | else: |
| 83 | print "unknown input" |
| 84 | idx += 2 |
| 85 | else: |
| 86 | tool_auth_path = sys.argv[idx] |
| 87 | print "tool_auth_path: " + tool_auth_path |
| 88 | idx += 1 |
| 89 | |
| 90 | if not key_ini_path: |
| 91 | print "key path is not given!" |
| 92 | return -1 |
| 93 | if not gfh_config_ini_path: |
| 94 | print "gfh config path is not given!" |
| 95 | return -1 |
| 96 | if not tool_auth_path: |
| 97 | print "tool_auth path is not given!" |
| 98 | return -1 |
| 99 | |
| 100 | out_path = os.path.dirname(os.path.abspath(tool_auth_path)) |
| 101 | |
| 102 | tool_auth_obj = tool_auth(out_path, tool_auth_path) |
| 103 | tool_auth_obj.create_gfh(gfh_config_ini_path) |
| 104 | tool_auth_obj.sign(key_ini_path) |
| 105 | |
| 106 | return |
| 107 | |
| 108 | if __name__ == '__main__': |
| 109 | main() |
| 110 | |