florianscholz

Untitled

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