image07 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 time
  8. import getopt
  9. import os.path
  10. import sys
  11. import MapFile
  12. import Dump
  13. def usage(message):
  14. if None != message:
  15. print 'error:', message
  16. print 'usage: %s [--verbose] --image=<rom.image> --map-file=<map>' % os.path.basename(__file__)
  17. exit(1)
  18. def main():
  19. try:
  20. opts, args = getopt.getopt(sys.argv[1:], 'hvi:m:', ['help', 'verbose', 'image-file=', 'map-file='])
  21. except getopt.GetoptError, err:
  22. usage(err)
  23. verbose = False
  24. imagefile = 'flash.image'
  25. mapfile = ''
  26. for opt, arg in opts:
  27. if opt in ('-v', '--verbose'):
  28. verbose = True
  29. elif opt in ('-h', '--help'):
  30. usage(None)
  31. elif opt in ('-i', '--image-file'):
  32. imagefile = arg
  33. elif opt in ('-m', '--map-file'):
  34. mapfile = arg
  35. else:
  36. usage('unhandled option: ' + opt)
  37. if not os.path.isfile(mapfile):
  38. usage('mapfile: "%s" does not exist' % mapfile)
  39. map = MapFile.MapFile(mapfile)
  40. if not map.ok():
  41. usage('Invalid mapfile')
  42. rom_image = 65536 * '\xff'
  43. for r in map.items():
  44. offset, filename, bytecount, data = r
  45. if verbose:
  46. print '0x%04x %s (%d bytes)' % (offset, filename, bytecount)
  47. rom_image = rom_image[0:offset] + data + rom_image[offset + bytecount:65536]
  48. if verbose:
  49. Dump.hex(rom_image)
  50. fh = open(imagefile, 'w')
  51. fh.write(rom_image)
  52. fh.close()
  53. if __name__ == '__main__':
  54. main()