• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 bestechnic (Shanghai) Technologies CO., LIMITED.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import re
18import subprocess
19import shutil
20import sys
21import json
22
23
24def usage():
25    print('\r\n{0} Usage:\r\n'.format(sys.argv[0]))
26    print(' -h: for help\r\n')
27    print(' {0} {1} {2} {3} {4}\r\n'.format(sys.argv[0],'<product path>','<origin bin path>','<gen hash and sec path>','<sig tool path>'))
28    print('parameters guide:')
29    print('     <product path> {}'.format('example: ./vendor/bestechnic/iotlink_demo'))
30    print('     <origin bin path> {}'.format('example: ./out/bes2600w/iotlink_demo/release_bin'))
31    print('     <gen hash and sec path> {}'.format('example: ./device/bestechnic/bes2600w/burn_tools/tools'))
32    print('     <sig tool path> {}'.format('producter own sig function,need custom complement,which is a python or shell file path'))
33    print('\r\n')
34    sys.exit(1)
35
36for var in sys.argv[1:]:
37    if var == '-h':
38        usage()
39
40if len(sys.argv) < 5:
41    usage()
42
43sig_args = ''
44for var in sys.argv[1:]:
45    sig_args = sig_args + ' ' + var
46
47product_path = sys.argv[1]
48src_bin_path = sys.argv[2]
49tools_path = sys.argv[3]
50gen_sig_tool_path = sys.argv[4]
51
52if not os.path.isfile(gen_sig_tool_path):
53    raise OSError('{} not found'.format(gen_sig_tool_path))
54
55# Read json file data
56def read_json_file(input_file):
57    if not os.path.isfile(input_file):
58        raise OSError('{} not found'.format(input_file))
59    print("readjson {}".format(input_file))
60    with open(input_file, 'rb') as input_f:
61        try:
62            data = json.load(input_f)
63            return data
64        except json.JSONDecodeError:
65            raise Exception('{} parsing error!'.format(input_file))
66
67
68# transfer path
69def transfer_path(dirpath,file):
70    if not dirpath.endswith('/'):
71        file_path = dirpath + '/' + file
72    else:
73        file_path = dirpath + file
74    return file_path
75
76# gen hash of bin
77def gen_bin_hash(tool,src_bin):
78
79    if not os.path.isfile(tool):
80        raise OSError('{} not found'.format(tool))
81
82    if not os.path.isfile(src_bin):
83        raise OSError('{} not found'.format(src_bin))
84
85    os.system('chmod a+x '+tool)
86    os.system('./'+tool+' '+ src_bin)
87
88# gen sec bin
89def gen_sec_bin(tool,src_bin,bin_sig):
90
91    if not os.path.isfile(tool):
92        raise OSError('{} not found'.format(tool))
93
94    if not os.path.isfile(src_bin):
95        raise OSError('{} not found'.format(src_bin))
96
97    if not os.path.isfile(bin_sig):
98        raise OSError('{} not found'.format(bin_sig))
99
100    os.system('chmod a+x '+ tool)
101    print("./{0} {1} {2}".format(tool,src_bin,bin_sig))
102    os.system('./'+ tool +' '+ src_bin + ' ' + bin_sig)
103
104# gen bin sig
105def gen_bin_sig(tool,bin_hash,bin_sig,args_list):
106    if not os.path.isfile(tool):
107        raise OSError('{} not found'.format(tool))
108
109    if not os.path.isfile(bin_hash):
110        raise OSError('{} not found'.format(bin_hash))
111
112    os.system('chmod a+x '+ tool)
113    os.system('./'+ tool + ' ' + bin_hash +  ' ' + bin_sig + ' ' + args_list)
114
115product_json = os.path.join(product_path,'config.json')
116product_obj = read_json_file(product_json)
117
118# harmonyos image
119bin_list = product_obj.get('bin_list', [])
120for bin_obj in bin_list:
121    bin_name = bin_obj.get('burn_name')
122    if bin_obj.get('enable') != 'true':
123        continue
124
125    src_bin = transfer_path(src_bin_path,bin_name + '.bin')
126    if not os.path.isfile(src_bin):
127        raise OSError('{} not found'.format(src_bin))
128
129    tool_hash = transfer_path(tools_path,'gen_hash_of_bin')
130    if not os.path.isfile(tool_hash):
131        raise OSError('{} not found'.format(tool_hash))
132
133    gen_bin_hash(tool_hash,src_bin)
134
135    bin_hash = transfer_path(src_bin_path,bin_name + '_hash.bin')
136    if not os.path.isfile(bin_hash):
137        raise OSError('{} not found'.format(bin_hash))
138
139    bin_sig = transfer_path(src_bin_path,bin_name + '_sig.bin')
140    gen_bin_sig(gen_sig_tool_path,bin_hash,bin_sig,sig_args)
141
142    tool_sec = transfer_path(tools_path,'gen_sec_bin')
143    if not os.path.isfile(tool_sec):
144        raise OSError('{} not found'.format(tool_sec))
145
146    gen_sec_bin(tool_sec,src_bin,bin_sig)
147
148
149# file system image
150fs_list = product_obj.get('fs_list', [])
151for fs_obj in fs_list:
152    fs_name = fs_obj.get('burn_name')
153    if fs_obj.get('enable') != 'true':
154        continue
155
156    fs_bin = transfer_path(src_bin_path,fs_name + '.bin')
157    if not os.path.isfile(fs_bin):
158        raise OSError('{} not found'.format(fs_bin))
159
160    tool_hash = transfer_path(tools_path,'gen_hash_of_littlefs')
161    if not os.path.isfile(tool_hash):
162        raise OSError('{} not found'.format(tool_hash))
163
164    gen_bin_hash(tool_hash,fs_bin)
165
166    fs_hash = transfer_path(src_bin_path,fs_name + '_hash.bin')
167    if not os.path.isfile(fs_hash):
168        raise OSError('{} not found'.format(fs_hash))
169
170    fs_sig = transfer_path(src_bin_path,fs_name + '_sig.bin')
171    gen_bin_sig(gen_sig_tool_path,fs_hash,fs_sig,sig_args)
172
173    tool_sec = transfer_path(tools_path,'gen_sec_littlefs')
174    if not os.path.isfile(tool_sec):
175        raise OSError('{} not found'.format(tool_sec))
176
177    gen_sec_bin(tool_sec,fs_bin,fs_sig)
178
179# bsp own bin
180bsp_bin_list = product_obj.get('bsp_bin_list', [])
181for bsp_bin_obj in bsp_bin_list:
182    bsp_bin_name = bsp_bin_obj.get('burn_name')
183    if bin_obj.get('enable') != 'true':
184        continue
185
186    src_bsp_bin = transfer_path(src_bin_path,bsp_bin_name + '.bin')
187    if not os.path.isfile(src_bsp_bin):
188        raise OSError('{} not found'.format(src_bsp_bin))
189
190    tool_hash = transfer_path(tools_path,'gen_hash_of_bin')
191    if not os.path.isfile(tool_hash):
192        raise OSError('{} not found'.format(tool_hash))
193
194    gen_bin_hash(tool_hash,src_bsp_bin)
195
196    bsp_bin_hash = transfer_path(src_bin_path,bsp_bin_name + '_hash.bin')
197    if not os.path.isfile(bsp_bin_hash):
198        raise OSError('{} not found'.format(bsp_bin_hash))
199
200    bsp_bin_sig = transfer_path(src_bin_path,bsp_bin_name + '_sig.bin')
201    gen_bin_sig(gen_sig_tool_path,bsp_bin_hash,bsp_bin_sig,sig_args)
202
203    tool_sec = transfer_path(tools_path,'gen_sec_bin')
204    if not os.path.isfile(tool_sec):
205        raise OSError('{} not found'.format(tool_sec))
206
207    gen_sec_bin(tool_sec,src_bsp_bin,bsp_bin_sig)
208