summaryrefslogtreecommitdiffstats
path: root/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/hexfile.py
blob: c484fdf607f5da0d734227286fbe9f301eb472e1 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# The MIT License (MIT)
# =====================
#
# Copyright (c) 2014 Ryan Sturmer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# https://github.com/ryansturmer/hexfile/blob/master/hexfile/core.py

import itertools

def short(msb,lsb):
    return (msb<<8) | lsb

class HexFile(object):
    def __init__(self, segments):
        self.segments = segments

    def __getitem__(self, val):
        if isinstance(val, slice):
            address = val.start
        else:
            address = val

        for segment in self.segments:
            if address in segment:
                return segment[val]

        raise IndexError('No segment contains address 0x%x' % address)

    def __len__(self):
        return sum(map(len, self.segments))

    @property
    def size(self):
        return len(self)

    def __iter__(self):
        return itertools.chain(*self.segments)

    @staticmethod
    def load(filename):
        segments = [Segment(0)]

        with open(filename) as fp:
            lines = fp.readlines()

        extended_linear_address = 0
        current_address = 0
        end_of_file = False

        lineno = 0
        for line in lines:
            lineno += 1
            line = line.strip();
            if not line.startswith(':'):
                continue

            if end_of_file:
                raise Exception("Record found after end of file on line %d" % lineno)

            bytes = [int(line[i:i+2], 16) for i in range(1,len(line), 2)]
            byte_count = bytes[0]
            address = short(*bytes[1:3])
            record_type = bytes[3]
            checksum = bytes[-1]
            data = bytes[4:-1]
            computed_checksum = ((1 << 8)-(sum(bytes[:-1]) & 0xff)) & 0xff

            if(computed_checksum != checksum):
                raise Exception("Record checksum doesn't match on line %d" % lineno)

            if record_type == 0:
                if byte_count == len(data):
                    current_address = (address | extended_linear_address)
                    have_segment = False
                    for segment in segments:
                        if segment.end_address == current_address:
                            segment.data.extend(data)
                            have_segment = True
                            break
                    if not have_segment:
                        segments.append(Segment(current_address, data))
                else:
                    raise Exception("Data record reported size does not match actual size on line %d" % lineno)
            elif record_type == 1:
                end_of_file = True
            elif record_type == 4:
                if byte_count != 2 or len(data) != 2:
                    raise Exception("Byte count misreported in extended linear address record on line %d" % lineno)
                extended_linear_address = short(*data) << 16

            else:
                raise Exception("Unknown record type: %s" % record_type)
        return HexFile(segments)

    def pretty_string(self, stride=16):
        retval = []
        for segment in self.segments:
            retval.append('Segment @ 0x%08x (%d bytes)' % (segment.start_address, segment.size))
            retval.append(segment.pretty_string(stride=stride))
            retval.append('')
        return '\n'.join(retval)

def load(filename):
    return HexFile.load(filename)

class Segment(object):
    def __init__(self, start_address, data = None):
        self.start_address = start_address
        self.data = data or []

    def pretty_string(self, stride=16):
        retval = []
        addresses = self.addresses
        ranges = [addresses[i:i+stride] for i in range(0, self.size, stride)]
        for r in ranges:
            retval.append('%08x ' % r[0] + ' '.join(['%02x' % self[addr] for addr in r]))
        return '\n'.join(retval)

    def __str__(self):
        return '<%d byte segment @ 0x%08x>' % (self.size, self.start_address)
    def __repr__(self):
        return str(self)

    @property
    def end_address(self):
        return self.start_address + len(self.data)

    @property
    def size(self):
        return len(self.data)

    def __contains__(self, address):
        return address >= self.start_address and address < self.end_address

    def __getitem__(self, address):
        if isinstance(address, slice):
            if address.start not in self or address.stop-1 not in self:
                raise IndexError('Address out of range for this segment')
            else:
                d = self.data[address.start-self.start_address:address.stop-self.start_address:address.step]
                start_address = address.start + self.start_address
                return Segment(start_address, d)
        else:
            if not address in self:
                raise IndexError("Address 0x%x is not in this segment" % address)
            return self.data[address-self.start_address]

    @property
    def addresses(self):
        return range(self.start_address, self.end_address)

    def __len__(self):
        return len(self.data)

    def __iter__(self):
        return iter(self.data)
OpenPOWER on IntegriCloud