signrom.py 778 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #
  2. # Option ROM signing utility
  3. #
  4. # Authors:
  5. # Jan Kiszka <jan.kiszka@siemens.com>
  6. #
  7. # This work is licensed under the terms of the GNU GPL, version 2 or later.
  8. # See the COPYING file in the top-level directory.
  9. import sys
  10. import struct
  11. if len(sys.argv) < 3:
  12. print('usage: signrom.py input output')
  13. sys.exit(1)
  14. fin = open(sys.argv[1], 'rb')
  15. fout = open(sys.argv[2], 'wb')
  16. fin.seek(2)
  17. size = ord(fin.read(1)) * 512 - 1
  18. fin.seek(0)
  19. data = fin.read(size)
  20. fout.write(data)
  21. checksum = 0
  22. for b in data:
  23. # catch Python 2 vs. 3 differences
  24. if isinstance(b, int):
  25. checksum += b
  26. else:
  27. checksum += ord(b)
  28. checksum = (256 - checksum) % 256
  29. # Python 3 no longer allows chr(checksum)
  30. fout.write(struct.pack('B', checksum))
  31. fin.close()
  32. fout.close()