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.

преди 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/python
  2. #
  3. # Compute and insert the vector table checksum required for booting the
  4. # LPC43xx and some other NXP ARM microcontrollers.
  5. #
  6. # usage: lpcvtcksum firmware.bin
  7. #
  8. # This file is part of the libopencm3 project.
  9. #
  10. # Copyright (C) 2012 Michael Ossmann <mike@ossmann.com>
  11. #
  12. # This library is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU Lesser General Public License as published by
  14. # the Free Software Foundation, either version 3 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This library is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU Lesser General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU Lesser General Public License
  23. # along with this library. If not, see <http://www.gnu.org/licenses/>.
  24. import sys, struct
  25. binfile = open(sys.argv[1], 'r+b')
  26. rawvectors = binfile.read(32)
  27. vectors = list(struct.unpack('<IIIIIIII', rawvectors))
  28. # compute vector table checksum
  29. sum = 0
  30. for i in range(7):
  31. sum += vectors[i]
  32. vectors[7] = 1 + (0xffffffff ^ (0xffffffff & sum))
  33. print "computed vector table checksum: 0x%08x" % vectors[7]
  34. rawremainder = binfile.read()
  35. remainder = list(struct.unpack('B' * len(rawremainder), rawremainder))
  36. numbytes = len(remainder) + 32
  37. # pad to multiple of 4096 bytes to make GoodFET happy
  38. if (numbytes % 4096):
  39. remainder.extend([0] * (4096 - numbytes % 4096))
  40. # rewrite file with checksum and padding
  41. data = vectors
  42. data.extend(remainder)
  43. binfile.seek(0)
  44. binfile.write(struct.pack('<IIIIIIII' + 'B' * len(remainder), *data))