annotate python/hexfile.py @ 104:ed230e947dc6

Added hexviewer
author windel
date Sun, 30 Dec 2012 22:31:55 +0100
parents
children d3dccf12ca88
rev   line source
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
1 import os
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
3 class HexFileException(Exception):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
4 pass
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
5
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
6 def parseHexLine(line):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
7 """ Parses a hexfile line into three parts """
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
8 line = line[1:] # Remove ':'
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
9 nums = bytes.fromhex(line)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
10 bytecount = nums[0]
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
11 if len(nums) != bytecount + 5:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
12 raise HexFileException('byte count field incorrect')
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
13 crc = sum(nums)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
14 if (crc & 0xFF) != 0:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
15 raise HexFileException('crc incorrect')
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
16 address = nums[1] * 256 + nums[2]
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
17 typ = nums[3]
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
18 data = nums[4:-1]
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
19 return (address, typ, data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
20
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
21 class HexFile:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
22 """ Represents an intel hexfile """
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
23 def __init__(self, filename=None):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
24 self.regions = []
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
25 self.startAddress = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
26 self.filename = None
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
27 if filename:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
28 self.load(filename)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
29
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
30 def load(self, filename):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
31 with open(filename, 'r') as f:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
32 endOfFile = False
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
33 offset = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
34 startAddress = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
35 curAddress = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
36 curData = bytearray()
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
37 for line in f:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
38 line = line.strip() # Strip spaces and newlines
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
39 if not line: continue # Skip empty lines
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
40 if line[0] != ':': continue # Skip lines that do not start with a ':'
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
41 if endOfFile: raise HexFileException('hexfile line after end of file record')
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
42 address, typ, data = parseHexLine(line)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
43 if typ == 0x0: # Data record
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
44 address += offset # Fix address with offset
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
45 # Append data
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
46 if address == curAddress:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
47 curData += data
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
48 curAddress += len(data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
49 else:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
50 if curData:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
51 self.regions.append(HexFileRegion(startAddress, bytes(curData)))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
52 startAddress = address
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
53 curAddress = address + len(data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
54 curData = bytearray(data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
55 elif typ == 0x4: # Extended linear address record
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
56 offset = ((data[0] << 8) + data[1]) << 16
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
57 elif typ == 0x1: # End of file record
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
58 if len(data) != 0:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
59 raise HexFileException('end of file record must contain no data')
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
60 endOfFile = True
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
61 elif typ == 0x5: # Start address record (where IP goes after loading)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
62 self.startAddress = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
63 else:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
64 raise HexFileException('record type {0} not implemented'.format(typ))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
65 print(hex(address), typ, data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
66 # After all lines:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
67 if curData:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
68 self.regions.append(HexFileRegion(startAddress, bytes(curData)))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
69 # Store the filename:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
70 self.filename = filename
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
71 def __repr__(self):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
72 i = []
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
73 i.append(super().__repr__())
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
74 i.append('Start address {0}'.format(hex(self.startAddress)))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
75 i.append('Filename: {0}'.format(self.filename))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
76 for r in self.regions:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
77 i.append(str(r))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
78 return os.linesep.join(i)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
79 def save(self, filename):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
80 with open(filename, 'w') as f:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
81 for line in f:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
82 pass
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
83
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
84 class HexFileRegion:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
85 def __init__(self, address, data = bytes()):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
86 self.address = address
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
87 self.data = data
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
88 def __repr__(self):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
89 return 'Region at 0x{0:X} of {1} bytes'.format(self.address, len(self.data))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
90
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
91 if __name__ == '__main__':
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
92 h = HexFile()
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
93 print(h)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
94 """ Test hexfile implementation with some hexfile """
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
95 h1 = HexFile('audio.hex')
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
96 print(h1)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
97