You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.6KB

  1. #!/usr/bin/python2.5
  2. #
  3. # Copyright 2009 Olivier Gillet.
  4. #
  5. # Author: Olivier Gillet (ol.gillet@gmail.com)
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # -----------------------------------------------------------------------------
  19. #
  20. # Python module for loading/writing Hex files.
  21. """Intel .hex file loader/writer"""
  22. import logging
  23. import sys
  24. def LoadHexFile(lines):
  25. """Loads a Hex file."""
  26. data = []
  27. for line_number, line in enumerate(lines):
  28. line = line.strip()
  29. if len(line) < 9:
  30. logging.info('Line %(line_number)d: line too short' % locals())
  31. return None
  32. if not all(x in '0123456789abcdefABCDEF' for x in line[1:]):
  33. logging.info('Line %(line_number)d: unknown character' % locals())
  34. return None
  35. bytes = [int(line[i:i+2], 16) for i in xrange(1, len(line), 2)]
  36. if bytes[0] != len(bytes) - 5:
  37. logging.info('Line %(line_number)d: invalid byte count' % locals())
  38. return None
  39. if sum(bytes) % 256 != 0:
  40. logging.info('Line %(line_number)d: corrupted line' % locals())
  41. return None
  42. if bytes[3] == 1:
  43. if bytes[0] != 0 or bytes[1] != 0 or bytes[2] != 0:
  44. logging.info('Line %(line_number)d: invalid end of file' % locals())
  45. return None
  46. else:
  47. break
  48. elif bytes[3] == 0:
  49. address = bytes[1] << 8 | bytes[2]
  50. padding_size = address + bytes[0] - len(data)
  51. if padding_size > 0:
  52. data += [0] * padding_size
  53. data[address:address + bytes[0]] = bytes[4:-1]
  54. return data
  55. def WriteHexFile(data, file_object, chunk_size=32):
  56. """Writes a Hex file."""
  57. for address in xrange(0, len(data), chunk_size):
  58. chunk = data[address:address+chunk_size]
  59. chunk_len = len(chunk)
  60. address_l = address & 255
  61. address_h = address >> 8
  62. file_object.write(':%(chunk_len)02x%(address_h)02x%(address_l)02x00' % vars())
  63. file_object.write(''.join('%02x' % value for value in chunk))
  64. checksum = (-(chunk_len + address_l + address_h + sum(chunk))) & 255
  65. file_object.write('%02x\n' % checksum)
  66. file_object.write(':00000001FF\n')