summaryrefslogtreecommitdiff
path: root/df10ch_setup_pkg/firmware.py
blob: 24dc5779ae4e81ee7897be4f5746db18b983fc04 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#
# Copyright (C) 2010 Andreas Auras
#
# This file is part of the DF10CH Atmolight controller project.
#
# DF10CH Atmolight controller is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# DF10CH Atmolight controller is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
#
# This file is part of the DF10CH setup program
#

import array
import fileinput
import string

class FirmwareFlashError(Exception):
    def __init__(self, msg = None):
        self.message = msg

    def __str__(self):
        return self.message


class FlashPage:
    
    def __init__(self, addr, pageSize):
        self.pageSize = pageSize
        self.baseAddr = addr - addr % pageSize
        self.data = array.array('B', [ 0xFF ] * pageSize)

    def insert(self, addr, value):
        self.data[addr % self.pageSize] = value

    def verify(self, data):
        for i in range(self.pageSize):
            if data[i] != self.data[i]:
                raise FirmwareFlashError("verify of flash data against firmware fails at {0:04X}: {1:02X} <> {2:02X}".format(self.baseAddr + i, data[i], self.data[i]))

    def __str__(self):
        s = "{0:04X}: ".format(self.baseAddr)
        for v in self.data:
            s = s + "{0:02X} ".format(v)
        return s


class FlashMem:
    
    def __init__(self, fileName, pageSize, targetInfoMustExist = False):
        self.pageSize = pageSize
        self.pageList = list()
        self.lastLookupPage = None
        self.target = None
        self.version = None
        self.loadFromHexFile(fileName)
        if targetInfoMustExist and (not self.target or not self.version):
            raise FirmwareFlashError("no target and/or version information found!")
        
    def getPageForAddr(self, addr):
        baseAddr = addr - addr % self.pageSize
        if self.lastLookupPage and self.lastLookupPage.baseAddr == baseAddr:
            return self.lastLookupPage;
        for p in self.pageList:
            if p.baseAddr == baseAddr:
                self.lastLookupPage = p
                return p
        return None
    
    def insert(self, addr, value):
        p = self.getPageForAddr(addr)
        if not p:
            p = FlashPage(addr, self.pageSize)
            self.pageList.append(p)
        p.insert(addr, value)

    def loadFromHexFile(self, fileName):
        file = None
        try:
            file = fileinput.FileInput(fileName)
            for line in file:
                line = string.rstrip(line)
                lineLen = len(line)
                if not lineLen:
                    continue
                lineType = line[0:1]
                if lineType == "#":
                    continue
                if lineType == "@":
                    try:
                        self.target, self.version = string.split(line[1:])
                    except:
                        raise FirmwareFlashError()
                    continue
                if lineLen < 9 or lineType != ":":
                    raise FirmwareFlashError()
                try:
                    n = int(line[1:3], 16)
                    addr = int(line[3:7], 16)
                    type = int(line[7:9], 16)
                except:
                    raise FirmwareFlashError()
                if type != 0:
                    break
                if n > 0:
                    if lineLen < (9 + 2 * n):
                        raise FirmwareFlashError()
                    for i in range(n):
                        try:
                            data = int(line[i * 2 + 9: i * 2 + 11], 16)
                        except:
                            raise FirmwareFlashError()
                        self.insert(addr + i, data)
        except IOError as err:
            raise FirmwareFlashError("could not read firmware file '{0}': {1}".format(fileName, err.__str__()))
        except FirmwareFlashError:
            raise FirmwareFlashError("could not read firmware file '{0}': syntax error at line {1}".format(fileName, file.lineno()))
        finally:
            if file:
                file.close()

if __name__ == "__main__":
    m = FlashMem("10ch_usb_ctrl.hex", 16)
    print "target:", m.target, "version:", m.version
    for p in m.pageList:
        print p