1#!/usr/bin/env python
2#-*- coding: utf-8 -*-
3
4import os
5import os.path
6
7import serial, time
8import array
9import sys, getopt
10
11class bootdownload(object):
12    '''
13    Hisilicon boot downloader
14
15    >>> downloader = bootdownload()
16    >>> downloader.download(filename)
17
18    '''
19
20    # crctab calculated by Mark G. Mendel, Network Systems Corporation
21    crctable = [
22        0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
23        0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
24        0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
25        0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
26        0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
27        0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
28        0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
29        0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
30        0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
31        0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
32        0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
33        0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
34        0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
35        0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
36        0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
37        0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
38        0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
39        0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
40        0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
41        0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
42        0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
43        0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
44        0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
45        0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
46        0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
47        0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
48        0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
49        0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
50        0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
51        0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
52        0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
53        0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
54    ]
55
56    startframe = {
57        'hi3716cv200':[0xFE,0x00,0xFF,0x01,0x00,0x00,0x00,0x04,0x00,0x00,0x02,0x01]
58    }
59
60    headframe = {
61        'hi3716cv200':[0xFE,0x00,0xFF,0x01,0x00,0x00,0x00,0x04,0x00,0x00,0x02,0x01]
62    }
63
64    bootheadaddress = {
65        'hi3716cv200':0xF9800800
66    }
67
68    bootdownloadaddress = {
69        'hi3716cv200':0x07000000
70    }
71
72    BOOT_HEAD_LEN = 0x4F00
73    MAX_DATA_LEN  = 0x400
74
75    def __init__(self,chiptype,serialport):
76        try:
77            self.s = serial.Serial(port=serialport, baudrate=115200, timeout=1)
78        except serial.serialutil.SerialException:
79            #no serial connection
80            self.s = None
81            print "\nFailed to open serial!", serialport
82            sys.exit(2)
83
84        self.chip = chiptype
85
86    def __del__(self):
87        if self.s != None:
88            self.s.close()
89
90    def calc_crc(self, data, crc=0):
91        for char in data:
92            crc = ((crc << 8) | ord(char)) ^ self.crctable[(crc >> 8) & 0xff]
93        for i in range(0,2):
94            crc = ((crc << 8) | 0) ^ self.crctable[(crc >> 8) & 0xff]
95        return crc & 0xffff
96
97    def getsize(self, filename):
98        st = os.stat(filename)
99        return st.st_size
100
101    def sendframe(self, data, loop):
102        for i in range(1, loop):
103            self.s.flushOutput()
104            self.s.write(data)
105            self.s.flushInput()
106            try:
107                ack = self.s.read()
108                if len(ack) == 1:
109                    if ack == chr(0xaa):
110                        return None
111            except:
112                return None
113
114        print 'failed'
115
116    def sendstartframe(self):
117	self.s.timeout = 0.01
118        data = array.array('B', self.startframe[self.chip]).tostring()
119        crc = self.calc_crc(data)
120        data += chr((crc >> 8)&0xff)
121        data += chr(crc&0xff)
122        self.sendframe(data,10000)
123
124    def sendheadframe(self,length,address):
125	self.s.timeout = 0.03
126        self.headframe[self.chip][4] = (length>>24)&0xff
127        self.headframe[self.chip][5] = (length>>16)&0xff
128        self.headframe[self.chip][6] = (length>>8)&0xff
129        self.headframe[self.chip][7] = (length)&0xff
130        self.headframe[self.chip][8] = (address>>24)&0xff
131        self.headframe[self.chip][9] = (address>>16)&0xff
132        self.headframe[self.chip][10] = (address>>8)&0xff
133        self.headframe[self.chip][11] = (address)&0xff
134
135        data = array.array('B', self.headframe[self.chip]).tostring()
136        crc = self.calc_crc(data)
137
138        data += chr((crc >> 8)&0xff)
139        data += chr(crc&0xff)
140
141        self.sendframe(data,16)
142
143
144    def senddataframe(self,seq,data):
145        self.s.timeout = 0.15
146        head = chr(0xDA)
147        head += chr(seq&0xFF)
148        head += chr((~seq)&0xFF)
149
150        data = head + data
151
152        crc = self.calc_crc(data)
153        data += chr((crc >> 8)&0xff)
154        data += chr(crc&0xff)
155
156        self.sendframe(data,32)
157
158    def sendtailframe(self,seq):
159        data = chr(0xED)
160        data += chr(seq&0xFF)
161        data += chr((~seq)&0xFF)
162        crc = self.calc_crc(data)
163        data += chr((crc >> 8)&0xff)
164        data += chr(crc&0xff)
165
166        self.sendframe(data,16)
167
168    def senddata(self, data, address):
169        length=len(data)
170        self.sendheadframe(length,address)
171        seq=1
172        while length > self.MAX_DATA_LEN:
173            self.senddataframe(seq,data[(seq-1)*self.MAX_DATA_LEN:seq*self.MAX_DATA_LEN])
174            seq = seq+1
175            length = length-self.MAX_DATA_LEN
176        self.senddataframe(seq,data[(seq-1)*self.MAX_DATA_LEN:])
177        self.sendtailframe(seq+1)
178
179
180    def download(self, filename1, filename2):
181
182        f=open(filename1,"rb")
183        data = f.read()
184        f.close()
185
186        print 'Sending', filename1, '...'
187        self.senddata(data,self.bootheadaddress[self.chip])
188        print 'Done\n'
189
190        if filename2:
191            f=open(filename2,"rb")
192            data = f.read()
193            f.close()
194
195            print 'Sending', filename2, '...'
196            self.senddata(data,self.bootdownloadaddress[self.chip])
197            print 'Done\n'
198
199
200def burnboot(chiptype, serialport, filename1, filename2=''):
201    downloader = bootdownload(chiptype, serialport)
202    downloader.download(filename1, filename2)
203
204def startterm(serialport=0):
205    try:
206        miniterm = Miniterm(
207            serialport,
208            115200,
209            'N',
210            rtscts=False,
211            xonxoff=False,
212            echo=False,
213            convert_outgoing=2,
214            repr_mode=0,
215        )
216    except serial.SerialException, e:
217        sys.stderr.write("could not open port %r: %s\n" % (port, e))
218        sys.exit(1)
219    miniterm.start()
220    miniterm.join(True)
221    miniterm.join()
222
223def main(argv):
224    '''
225    img2 = 'fastboot2.img'
226    '''
227    img1 = 'fastboot1.img'
228    img2 = ''
229    dev  = '/dev/serial/by-id/usb-䕇䕎䥎_㄰㌲㔴㜶㤸-if00-port0'
230    try:
231        opts, args = getopt.getopt(argv,"hd:",["img1=","img2="])
232    except getopt.GetoptError:
233        print 'hisi-idt.py -d device --img1 <fastboot1> --img2 <fastboot2>'
234        sys.exit(2)
235    for opt, arg in opts:
236        if opt == '-h':
237            print 'hisi-idt.py -d device --img1 <fastboot1> --img2 <fastboot2>'
238            sys.exit()
239        elif opt in ("-d"):
240            dev = arg
241        elif opt in ("--img1"):
242            img1 = arg
243        elif opt in ("--img2"):
244            img2 = arg
245    print '+----------------------+'
246    print ' Serial: ', dev
247    print ' Image1: ', img1
248    print ' Image2: ', img2
249    print '+----------------------+\n'
250
251    if not os.path.isfile(img1):
252        print "Image don't exists:", img1
253        sys.exit(1)
254
255    if (img2):
256        if not os.path.isfile(img2):
257            print "Image don't exists:", img2
258            sys.exit(1)
259
260    burnboot('hi3716cv200', dev, img1, img2)
261
262if __name__ == "__main__":
263    main(sys.argv[1:])
264