hc
2024-08-16 62c46c9150c4afde7e5b25436263fddf79d66f0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
#
# Copyright 2023, Rockchip Electronics Co., Ltd
# hisping lin, <hisping.lin@rock-chips.com>
#
# SPDX-License-Identifier:    GPL-2.0+
#
 
def get_args():
   import argparse
 
   parser = argparse.ArgumentParser()
 
   parser.add_argument('--out', required=True, \
       help='Name of image for the TA')
 
   parser.add_argument('--tadir', required=True, help='Path of TA director')
 
   return parser.parse_args()
 
def main():
   import os
   import array
   import glob
   import struct
   import uuid
   try:
       from Cryptodome.Hash import SHA256
   except ImportError:
       from Crypto.Hash import SHA256
 
   args = get_args();
 
   path = args.tadir + "/*.ta"
   files = glob.glob(path)
   for f in files:
       print(f)
 
   fimg = open(args.out, 'wb')
   magic = 0x524B5441 #RKTA
   img_ver = 0x00000001
   ta_num = len(files)
   reserve = 0
   header = struct.pack('<IIII', magic, img_ver, ta_num, reserve)
   fimg.write(header)
 
   g_offset = len(header) + (ta_num * 32)
   for f in files:
       fta = open(f, 'rb')
       tadata = fta.read()
       fta.close
       ta_name = os.path.basename(f)
       ta_name_without_extension = os.path.splitext(ta_name)[0]
       ta_uuid = uuid.UUID(ta_name_without_extension)
       ta_uuid_hex = bytes.fromhex(ta_uuid.hex)
       fimg.write(ta_uuid_hex)
       ta_offset = struct.pack('<I', g_offset)
       fimg.write(ta_offset)
       ta_len = struct.pack('<I', len(tadata))
       fimg.write(ta_len)
       ta_ver_num = len(ta_name_without_extension) - 34;
       ta_ver = struct.pack('<I', ta_ver_num)
       fimg.write(ta_ver)
       ta_reserve = struct.pack('<I', reserve)
       fimg.write(ta_reserve)
       g_offset += len(tadata)
 
   for f in files:
       fta = open(f, 'rb')
       tadata = fta.read()
       fta.close
       fimg.write(tadata)
   fimg.close
 
   fimg = open(args.out, 'rb+')
   userta = fimg.read()
   h = SHA256.new()
   h.update(userta)
   img_digest = h.digest()
   fimg.write(img_digest)
   fimg.close
 
   print("pack ta binary success!")
 
if __name__ == "__main__":
   main()