MapFile.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # COPYRIGHT: Openmoko Inc. 2009
  4. # LICENSE: GPL Version 3 or later
  5. # DESCRIPTION: Simple FLASH programmer
  6. # AUTHOR: Christopher Hall <hsw@openmoko.com>
  7. import os.path
  8. import sys
  9. import re
  10. class MapFile:
  11. MAP_LINE_RE = re.compile(r'^\s*(0x[0-9a-f]+)\s+(\S+)\s*(#.*)?$', re.IGNORECASE)
  12. MAP_COMMENT_RE = re.compile(r'^\s*(#.*)?$', re.IGNORECASE)
  13. def __init__(self, filename, romSize = 65536):
  14. self.rom = []
  15. self.status = True
  16. for line in open(filename, 'rt'):
  17. m = MapFile.MAP_LINE_RE.match(line)
  18. c = MapFile.MAP_COMMENT_RE.match(line)
  19. if m:
  20. offset = eval(m.group(1))
  21. file = m.group(2)
  22. if '*ERASE' == file:
  23. data = '\xff'
  24. else:
  25. if not os.path.isfile(file):
  26. file = os.path.join(os.path.dirname(filename), file)
  27. if not os.path.isfile(file):
  28. print 'Missing program: "%s"' % m.group(2)
  29. self.status = False
  30. data = open(file, 'rb').read()
  31. data_len = len(data)
  32. self.rom.append((offset, file, data_len, data))
  33. elif not c:
  34. print 'invalid map line:', line
  35. self.status = False
  36. def items(self):
  37. return self.rom
  38. def ok(self):
  39. return self.status