MapFile.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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
  40. def get_data(self, filename):
  41. for r in self.rom:
  42. offset, name, length, data = r[0], r[1], r[2], r[3]
  43. if name == filename:
  44. return data
  45. return None