summaryrefslogtreecommitdiff
path: root/test-cli/test/tasks/flasheeprom.py
blob: 775c5565de6196f89ebf801ae039fa5b054609a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import binascii
from test.helpers.globalVariables import globalVar

class EEprom_Flasher:
    __Name = None
    __varlist = None
    __xmlObj = None
    __lastError = ''

    def __init__(self, varlist):
        self.__varlist = varlist
        self.__xmlObj = varlist['xml']
        self.__Name = varlist.get('name_eeprom', self.__xmlObj.getKeyVal('EEProm_Flasher', 'name', 'EEProm_Flasher'))
        # self.__igepflashPath = self.__xmlObj.getKeyVal('NAND_Flasher', 'igep_flash', '/usr/bin/igep-flash')
        # self.__ImagePath = varlist.get('flashimagepath', '')
        # self.__SkipNandtest = varlist.get('skipnandtest', self.__xmlObj.getKeyVal('NAND_Flasher', 'skipnandtest', '1'))

    def getTaskName(self):
        return self.__Name

    def getError(self):
        return self.__lastError

    def Execute(self):
        return True, None

    def __generate_data_bytes(self, boarduuid, mac0, mac1):
        data = bytearray()
        data += (2029785358).to_bytes(4, 'big')  # magicid --> 0x78FC110E
        data += bytearray([0, 0, 0, 0])  # crc32 = 0
        data += bytearray(boarduuid + "\0",
                      'ascii')  # uuid --> 'c0846c8a-5fa5-11ea-8576-f8b156ac62d7' and \0 at the end
        data += binascii.unhexlify(mac0.replace(':', ''))  # mac0 --> 'f8:b1:56:ac:62:d7'
        if mac1 is not None:
            data += binascii.unhexlify(mac1.replace(':', ''))  # mac1 --> 'f8:b1:56:ac:62:d7'
        else:
            data += bytearray([0, 0, 0, 0, 0, 0])  # mac1 --> 0:0:0:0:0:0
        # calculate crc
        crc = (binascii.crc32(data, 0)).to_bytes(4, 'big')
        data[4:8] = crc
        return data


    def __generate_output_data(self, data_rx):
        boarduuid = data_rx[8:44].decode('ascii')  # no 8:45 porque el \0 final hace que no funcione postgresql
        mac0 = binascii.hexlify(data_rx[45:51]).decode('ascii')
        mac1 = binascii.hexlify(data_rx[51:57]).decode('ascii')
        smac0 = ':'.join(mac0[i:i + 2] for i in range(0, len(mac0), 2))
        smac1 = ':'.join(mac1[i:i + 2] for i in range(0, len(mac1), 2))
        eepromdata = "UUID " + boarduuid + " , MAC0 " + smac0 + " , MAC1 " + smac1
        return eepromdata


    # returns exitcode and data saved into eeprom
    def flash_eeprom(self, eeprompath, boarduuid, mac0, mac1=None):
        print("Start programming Eeprom...")
        # check if eeprompath is correct
        if os.path.isfile(eeprompath):
            # create u-boot data struct
            data = self.__generate_data_bytes(boarduuid, mac0, mac1)
            # write into eeprom and read back
            f = open(eeprompath, "r+b")
            f.write(data)
            f.seek(0)
            data_rx = f.read(57)
            for i in range(57):
                if data_rx[i] != data[i]:
                    print("Error while programming eeprom memory.")
                    return 1, None
            print("Eeprom programmed succesfully.")
            # generated eeprom read data
            eepromdata = self.__generate_output_data(data_rx)
            return 0, eepromdata
        else:
            print("Eeprom memory not found.")
            return 1, None