• 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: packet allinone bin file
19'''
20import struct
21import sys
22import os
23
24class crc16:
25    POLYNOMIAL = 0x1021
26    PRESET = 0x0000
27    _tab = []
28    def __init__(self):
29        self._tab = [self._initial(i) for i in range(256)]
30
31    def _initial(self, c):
32        crc = 0
33        c = c << 8
34        for j in range(8):
35            if (crc ^ c) & 0x8000:
36                crc = (crc << 1) ^ self.POLYNOMIAL
37            else:
38                crc = crc << 1
39            c = c << 1
40        return crc
41
42    def _update_crc(self, crc, c):
43        cc = 0xff & int(c)
44
45        tmp = (crc >> 8) ^ cc
46        crc = (crc << 8) ^ self._tab[tmp & 0xff]
47        crc = crc & 0xffff
48
49        return crc
50
51    def crc(self, str):
52        crc = self.PRESET
53        for c in str:
54            crc = self._update_crc(crc, ord(c))
55        return crc
56
57    def crcb(self, i):
58        crc = self.PRESET
59        for c in i:
60            crc = self._update_crc(crc, c)
61        return crc
62
63t = crc16()
64def packet_bin(outputPath, inputList):
65    pathList = []
66    burnAddrList = []
67    burnSizeList = []
68    imageSizeList = []
69    typeList = []
70    for item in inputList:
71        path, burnAddr, burnSize, type = item.split("|")
72        imageSize = os.path.getsize(path)
73        pathList.append(path)
74        burnAddrList.append(int(burnAddr))
75        burnSizeList.append(int(burnSize))
76        imageSizeList.append(imageSize)
77        typeList.append(int(type))
78
79    print(pathList)
80    print(burnAddrList)
81    print(burnSizeList)
82    print(imageSizeList)
83    print(typeList)
84
85    flag = 0xefbeaddf
86    print(flag)
87    crc = 0
88    imageNum = len(pathList)
89    headLen = imageNum*52 + 12
90    totalFileSize = sum(imageSizeList) + headLen
91
92    with open(outputPath, 'wb+') as file:
93        file.write(struct.pack('IHHI', flag, crc, imageNum, totalFileSize))
94        startIndex = headLen
95        times = 0
96        for path in pathList:
97            pathName = os.path.basename(path)
98            file.write(
99                struct.pack('32sIIIII', bytes(pathName, 'ascii'), startIndex, imageSizeList[times], burnAddrList[times],
100                            burnSizeList[times], typeList[times]))
101            startIndex = startIndex + imageSizeList[times] + 16
102            times += 1
103
104        for path in pathList:
105            with  open(path, 'rb+') as subfile:
106                data = subfile.read()
107                file.write(data)
108                file.write(struct.pack('IIII', 0, 0, 0, 0))
109
110        file.flush()
111        file.seek(6)
112        newdata = file.read(headLen - 6)
113        crc16 = t.crcb(newdata)
114        file.seek(4)
115        file.write(struct.pack('H', crc16))
116
117if __name__=="__main__":
118    print("main")
119    args = len(sys.argv)
120    argv = sys.argv
121    print(args)
122    print(argv)
123    if(args <= 2):
124        print("param should be : python main.py \"outputPath\" \"name|burnAddr|burnSize|type\"...")
125        '''
126        enum IMAGE_TYPE {
127        IMAGE_TYPE_LOADER,
128        IMAGE_TYPE_NORMAL,
129        IMAGE_TYPE_PARAM,
130        IMAGE_TYPE_EFUSE,
131        IMAGE_TYPE_OTP,
132        IMAGE_TYPE_FORMALBIN,
133        };
134        '''
135        sys.exit()
136
137    outputPath = argv[1]
138    print(outputPath)
139    del argv[1]
140    del argv[0]
141    packet_bin(outputPath, argv)
142
143''''
144pathList = []
145burnAddrList = []
146burnSizeList = []
147imageSizeList = []
148typeList = []
149
150for arg in argv:
151    path, burnAddr, burnSize, type = arg.split("|")
152    imageSize = os.path.getsize(path)
153    pathList.append(path)
154    burnAddrList.append(int(burnAddr))
155    burnSizeList.append(int(burnSize))
156    imageSizeList.append(imageSize)
157    typeList.append(int(type))
158
159print(pathList)
160print(burnAddrList)
161print(burnSizeList)
162print(imageSizeList)
163print(typeList)
164
165flag = 0xefbeaddf
166print(flag)
167crc = 0
168imageNum = len(pathList)
169headLen = imageNum*52 + 12
170totalFileSize = sum(imageSizeList) + headLen
171
172file = open(outputPath, 'wb+')
173
174file.write(struct.pack('IHHI', flag, crc, imageNum, totalFileSize))
175startIndex = headLen
176times = 0
177for path in pathList:
178    pathName = path.split("\\")[-1]
179    file.write(struct.pack('32sIIIII', bytes(pathName, 'ascii'), startIndex, imageSizeList[times], burnAddrList[times],
180                           burnSizeList[times], typeList[times]))
181    startIndex = startIndex + imageSizeList[times] + 16
182    times += 1
183
184for path in pathList:
185    subfile = open(path, 'rb+')
186    data = subfile.read()
187    file.write(data)
188    file.write(struct.pack('IIII', 0, 0, 0, 0))
189
190file.flush()
191file.seek(6)
192newdata = file.read(headLen - 6)
193t = crc16()
194crc16 = t.crcb(newdata)
195file.seek(4)
196file.write(struct.pack('H', crc16))
197file.close()
198'''''
199
200