• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4'''
5* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10*     http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Description: allinone.bin packet too scripts.
19'''
20
21import struct
22import sys
23import os
24
25class crc16:
26    POLYNOMIAL = 0x1021
27    PRESET = 0x0000
28    _tab = []
29    def __init__(self):
30        self._tab = [self._initial(i) for i in range(256)]
31
32    def _initial(self, c):
33        crc = 0
34        c = c << 8
35        for j in range(8):
36            if (crc ^ c) & 0x8000:
37                crc = (crc << 1) ^ self.POLYNOMIAL
38            else:
39                crc = crc << 1
40            c = c << 1
41        return crc
42
43    def _update_crc(self, crc, c):
44        cc = 0xff & int(c)
45
46        tmp = (crc >> 8) ^ cc
47        crc = (crc << 8) ^ self._tab[tmp & 0xff]
48        crc = crc & 0xffff
49
50        return crc
51
52    def crc(self, str):
53        crc = self.PRESET
54        for c in str:
55            crc = self._update_crc(crc, ord(c))
56        return crc
57
58    def crcb(self, i):
59        crc = self.PRESET
60        for c in i:
61            crc = self._update_crc(crc, c)
62        return crc
63
64t = crc16()
65def packet_bin(outputPath, inputList):
66    pathList = []
67    burnAddrList = []
68    burnSizeList = []
69    imageSizeList = []
70    typeList = []
71    for item in inputList:
72        path, burnAddr, burnSize, type = item.split("|")
73        imageSize = os.path.getsize(path)
74        pathList.append(path)
75        burnAddrList.append(int(burnAddr))
76        burnSizeList.append(int(burnSize))
77        imageSizeList.append(imageSize)
78        typeList.append(int(type))
79
80    print(pathList)
81    print(burnAddrList)
82    print(burnSizeList)
83    print(imageSizeList)
84    print(typeList)
85
86    flag = 0xefbeaddf
87    print(flag)
88    crc = 0
89    imageNum = len(pathList)
90    #sizeof(IMAGE_INFO) is 52, sizeof(FWPKG_HEAD) is 12
91    headLen = imageNum * 52 + 12
92    #each image has 16byte 0 in the end
93    padLen = 16 * imageNum
94    totalFileSize = sum(imageSizeList) + headLen + padLen
95    with open(outputPath, 'wb+') as file:
96        file.write(struct.pack('IHHI', flag, crc, imageNum, totalFileSize))
97        startIndex = headLen
98        times = 0
99        for path in pathList:
100            pathName = os.path.basename(path)
101            file.write(
102                struct.pack('32sIIIII', bytes(pathName, 'ascii'), startIndex, imageSizeList[times], burnAddrList[times],
103                            burnSizeList[times], typeList[times]))
104            #separate each image with 16 bytes 0
105            startIndex = startIndex + imageSizeList[times] + 16
106            times += 1
107
108        for path in pathList:
109            subfile = open(path, 'rb+')
110            data = subfile.read()
111            file.write(data)
112            file.write(struct.pack('IIII', 0, 0, 0, 0))
113
114        file.flush()
115        #crc range is FWPKG_HEAD.imageNum to end, begin index is 6bytes
116        file.seek(6)
117        newdata = file.read(headLen - 6)
118        crc16 = t.crcb(newdata)
119        file.seek(4)
120        file.write(struct.pack('H', crc16))
121        file.close()
122def update_bin(packet_path, inputList):
123    pathNameList = []
124    burnAddrList = []
125    burnSizeList = []
126    imageSizeList = []
127    typeList = []
128    startIndexList = []
129    fileInfoList = []
130
131    pathNameListNew = []
132    burnAddrListNew = []
133    burnSizeListNew = []
134    imageSizeListNew = []
135    typeListNew = []
136    for item in inputList:
137        path, burnAddr, burnSize, type = item.split("|")
138        imageSize = os.path.getsize(path)
139        pathNameListNew.append(path)
140        burnAddrListNew.append(int(burnAddr))
141        burnSizeListNew.append(int(burnSize))
142        imageSizeListNew.append(imageSize)
143        typeListNew.append(int(type))
144
145    with open(packet_path, 'rb+') as file:
146        info = file.read(struct.calcsize('IHHI'))
147        flag, crc, imageNum, totalFileSize = struct.unpack('IHHI', info)
148        times = 0
149        for index in range(0, imageNum):
150            info = file.read(struct.calcsize('32sIIIII'))
151            pathName, startIndex, imageSize, burnAddr, burnSize, type2 = struct.unpack('32sIIIII', info)
152            #separate each image with 16 bytes 0
153            pathName = str(pathName, encoding="utf-8")
154            pathNameNew = ''
155            for char in pathName:
156                if char != '\x00':
157                    pathNameNew += char
158            print(pathNameNew)
159            pathNameList.append(pathNameNew)
160            startIndexList.append(startIndex)
161            burnAddrList.append(burnAddr)
162            burnSizeList.append(burnSize)
163            imageSizeList.append(imageSize)
164            typeList.append(type2)
165
166        for index in range(0, imageNum):
167            file.seek(startIndexList[index])
168            fileInfoList.append(file.read(imageSizeList[index]))
169
170        for index in range(0, len(pathNameListNew)):
171            pathNew = pathNameListNew[index]
172            print(os.path.basename(pathNew))
173            pathIn32Byte = os.path.basename(pathNew)
174            if len(pathIn32Byte) > 32:
175                pathIn32Byte = pathIn32Byte[0:32]
176            if pathIn32Byte in pathNameList:
177                for index2 in range(0, imageNum):
178                    path = pathNameList[index2]
179                    if path == pathIn32Byte:
180                        subfile = open(pathNew, 'rb+')
181                        fileInfoList[index2] = subfile.read()
182                        subfile.close()
183                        pathNameList[index2] = os.path.basename(pathNew)
184                        burnAddrList[index2] = burnAddrListNew[index]
185                        burnSizeList[index2] = burnSizeListNew[index]
186                        imageSizeList[index2] = imageSizeListNew[index]
187                        typeList[index2] = typeListNew[index]
188                        break
189            else:
190                pathNameList.append(pathNameListNew[index])
191                burnAddrList.append(burnAddrListNew[index])
192                burnSizeList.append(burnSizeListNew[index])
193                imageSizeList.append(imageSizeListNew[index])
194                typeList.append(typeListNew[index])
195                subfile = open(pathNew, 'rb+')
196                fileInfoList.append(subfile.read())
197                subfile.close()
198        file.close()
199    print(pathNameList)
200    print(burnAddrList)
201    print(burnSizeList)
202    print(imageSizeList)
203    print(typeList)
204
205    flag = 0xefbeaddf
206    print(flag)
207    crc = 0
208    imageNum = len(pathNameList)
209    #sizeof(IMAGE_INFO) is 52, sizeof(FWPKG_HEAD) is 12
210    headLen = imageNum * 52 + 12
211    #each image has 16byte 0 in the end
212    padLen = 16 * imageNum
213    totalFileSize = sum(imageSizeList) + headLen + padLen
214    with open(outputPath, 'wb+') as file:
215        file.write(struct.pack('IHHI', flag, crc, imageNum, totalFileSize))
216        startIndex = headLen
217        times = 0
218        for path in pathNameList:
219            pathName = os.path.basename(path)
220            file.write(
221                struct.pack('32sIIIII', bytes(pathName, 'ascii'), startIndex, imageSizeList[times], burnAddrList[times],
222                            burnSizeList[times], typeList[times]))
223            #separate each image with 16 bytes 0
224            startIndex = startIndex + imageSizeList[times] + 16
225            times += 1
226
227        for info in fileInfoList:
228            file.write(info)
229            file.write(struct.pack('IIII', 0, 0, 0, 0))
230
231        file.flush()
232        #crc range is FWPKG_HEAD.imageNum to end, begin index is 6bytes
233        file.seek(6)
234        newdata = file.read(headLen - 6)
235        crc16 = t.crcb(newdata)
236        file.seek(4)
237        file.write(struct.pack('H', crc16))
238        file.close()
239def delete_bin(packet_path, name_list):
240    pathNameList = []
241    burnAddrList = []
242    burnSizeList = []
243    imageSizeList = []
244    typeList = []
245    startIndexList = []
246    fileInfoList = []
247
248    pathNameListNew = name_list
249
250    with open(packet_path, 'rb+') as file:
251        info = file.read(struct.calcsize('IHHI'))
252        flag, crc, imageNum, totalFileSize = struct.unpack('IHHI', info)
253        times = 0
254        for index in range(0, imageNum):
255            info = file.read(struct.calcsize('32sIIIII'))
256            pathName, startIndex, imageSize, burnAddr, burnSize, type2 = struct.unpack('32sIIIII', info)
257            #separate each image with 16 bytes 0
258            pathName = str(pathName, encoding="utf-8")
259            pathNameNew = ''
260            for char in pathName:
261                if char != '\x00':
262                    pathNameNew += char
263            print(pathNameNew)
264            pathNameList.append(pathNameNew)
265            startIndexList.append(startIndex)
266            burnAddrList.append(burnAddr)
267            burnSizeList.append(burnSize)
268            imageSizeList.append(imageSize)
269            typeList.append(type2)
270
271        for index in range(0, imageNum):
272            file.seek(startIndexList[index])
273            fileInfoList.append(file.read(imageSizeList[index]))
274
275        for pathNew in pathNameListNew:
276            print(os.path.basename(pathNew))
277            pathIn32Byte = os.path.basename(pathNew)
278            if len(pathIn32Byte) > 32:
279                pathIn32Byte = pathIn32Byte[0:32]
280            if pathIn32Byte in pathNameList:
281                for index2 in range(0, imageNum):
282                    path = pathNameList[index2]
283                    if path == pathIn32Byte:
284                        del fileInfoList[index2]
285                        del pathNameList[index2]
286                        del burnAddrList[index2]
287                        del burnSizeList[index2]
288                        del imageSizeList[index2]
289                        del typeList[index2]
290                        break
291        file.close()
292    print(pathNameList)
293    print(burnAddrList)
294    print(burnSizeList)
295    print(imageSizeList)
296    print(typeList)
297
298    flag = 0xefbeaddf
299    print(flag)
300    crc = 0
301    imageNum = len(pathNameList)
302    #sizeof(IMAGE_INFO) is 52, sizeof(FWPKG_HEAD) is 12
303    headLen = imageNum * 52 + 12
304    #each image has 16byte 0 in the end
305    padLen = 16 * imageNum
306    totalFileSize = sum(imageSizeList) + headLen + padLen
307    with open(outputPath, 'wb+') as file:
308        file.write(struct.pack('IHHI', flag, crc, imageNum, totalFileSize))
309        startIndex = headLen
310        times = 0
311        for path in pathNameList:
312            pathName = os.path.basename(path)
313            file.write(
314                struct.pack('32sIIIII', bytes(pathName, 'ascii'), startIndex, imageSizeList[times], burnAddrList[times],
315                            burnSizeList[times], typeList[times]))
316            #separate each image with 16 bytes 0
317            startIndex = startIndex + imageSizeList[times] + 16
318            times += 1
319
320        for info in fileInfoList:
321            file.write(info)
322            file.write(struct.pack('IIII', 0, 0, 0, 0))
323
324        file.flush()
325        #crc range is FWPKG_HEAD.imageNum to end, begin index is 6bytes
326        file.seek(6)
327        newdata = file.read(headLen - 6)
328        crc16 = t.crcb(newdata)
329        file.seek(4)
330        file.write(struct.pack('H', crc16))
331        file.close()
332def split_bin(packet_path, output_path):
333    pathNameList = []
334    burnAddrList = []
335    burnSizeList = []
336    imageSizeList = []
337    typeList = []
338    startIndexList = []
339    fileInfoList = []
340
341    pathNameListNew = []
342    burnAddrListNew = []
343    burnSizeListNew = []
344    imageSizeListNew = []
345    typeListNew = []
346    with open(packet_path, 'rb+') as file:
347        info = file.read(struct.calcsize('IHHI'))
348        flag, crc, imageNum, totalFileSize = struct.unpack('IHHI', info)
349        times = 0
350        for index in range(0, imageNum):
351            info = file.read(struct.calcsize('32sIIIII'))
352            pathName, startIndex, imageSize, burnAddr, burnSize, type2 = struct.unpack('32sIIIII', info)
353            #separate each image with 16 bytes 0
354            pathName = str(pathName, encoding="utf-8")
355            pathNameNew = ''
356            for char in pathName:
357                if char != '\x00':
358                    pathNameNew += char
359            print(pathNameNew)
360            pathNameList.append(pathNameNew)
361            startIndexList.append(startIndex)
362            burnAddrList.append(burnAddr)
363            burnSizeList.append(burnSize)
364            imageSizeList.append(imageSize)
365            typeList.append(type2)
366
367        for index in range(0, imageNum):
368            file.seek(startIndexList[index])
369            fileInfoList.append(file.read(imageSizeList[index]))
370
371        file.close()
372    print(pathNameList)
373    print(burnAddrList)
374    print(burnSizeList)
375    print(imageSizeList)
376    print(typeList)
377
378    imageNum = len(pathNameList)
379    for index in range(0, imageNum):
380        pathName = os.path.join(output_path, pathNameList[index])
381        print(pathName)
382        with open(pathName, 'wb+') as file:
383            file.write(fileInfoList[index])
384            file.close()
385def show_bin(packet_path):
386    pathNameList = []
387    burnAddrList = []
388    burnSizeList = []
389    imageSizeList = []
390    typeList = []
391    startIndexList = []
392    fileInfoList = []
393
394    with open(packet_path, 'rb+') as file:
395        info = file.read(struct.calcsize('IHHI'))
396        flag, crc, imageNum, totalFileSize = struct.unpack('IHHI', info)
397        times = 0
398        for index in range(0, imageNum):
399            info = file.read(struct.calcsize('32sIIIII'))
400            pathName, startIndex, imageSize, burnAddr, burnSize, type2 = struct.unpack('32sIIIII', info)
401            #separate each image with 16 bytes 0
402            pathName = str(pathName, encoding="utf-8")
403            pathNameNew = ''
404            for char in pathName:
405                if char != '\x00':
406                    pathNameNew += char
407            print(pathNameNew)
408            pathNameList.append(pathNameNew)
409            startIndexList.append(startIndex)
410            burnAddrList.append(burnAddr)
411            burnSizeList.append(burnSize)
412            imageSizeList.append(imageSize)
413            typeList.append(type2)
414
415        for index in range(0, imageNum):
416            file.seek(startIndexList[index])
417            fileInfoList.append(file.read(imageSizeList[index]))
418
419        file.close()
420    print(pathNameList)
421    print(burnAddrList)
422    print(burnSizeList)
423    print(imageSizeList)
424    print(typeList)
425
426if __name__=="__main__":
427    args = len(sys.argv)
428    argv = sys.argv
429    if(args <= 1):
430        print("parma error, please use -help for usage")
431        sys.exit()
432    if (argv[1] == '-packet'):
433        outputPath = argv[2]
434        print(outputPath)
435        del argv[2]
436        del argv[1]
437        del argv[0]
438        packet_bin(outputPath, argv)
439    elif (argv[1] == '-update'):
440        outputPath = argv[2]
441        print(outputPath)
442        del argv[2]
443        del argv[1]
444        del argv[0]
445        update_bin(outputPath, argv)
446    elif (argv[1] == '-split'):
447        input_file = argv[2]
448        outputPath = argv[3]
449        split_bin(input_file, outputPath)
450    elif (argv[1] == '-show'):
451        input_file = argv[2]
452        show_bin(input_file)
453    elif (argv[1] == '-delete'):
454        outputPath = argv[2]
455        print(outputPath)
456        del argv[2]
457        del argv[1]
458        del argv[0]
459        delete_bin(outputPath, argv)
460    elif (argv[1] == '-help'):
461        print("\
462        -packet,param should be : python packet_create.py -packet \"outputPath\" \"name|burnAddr|burnSize|type\"...\r\n\
463        -split,param should be : python packet_create.py -split \"inputefile\" \"outputPath\"\r\n\
464        -update,param should be : python packet_create.py -update \"outputPath\" \"name|burnAddr|burnSize|type\"...\r\n\
465        -delete,param should be : python packet_create.py -delete \"inputefile\" \"name1\"...\r\n\
466        -show\r\n\
467        -help\r\n")
468    else:
469        print("parma error, please use -help for usage")
470