raw2bin.py 909 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. # Written by Pedro Gimeno
  3. # args: infile outfile startaddr [execaddr]
  4. # execaddr defaults to startaddr
  5. # For lack of knowledge, currently only executable binaries are supported.
  6. import sys, struct
  7. def main():
  8. if len(sys.argv) < 4:
  9. sys.stderr.write("Usage: raw2bin.py infile outfile startaddr [execaddr]\n")
  10. return
  11. start = int(sys.argv[3], 0) & 0xFFFF
  12. if len(sys.argv) > 4:
  13. exe = int(sys.argv[4], 0)
  14. else:
  15. exe = start
  16. if sys.argv[1] != '-':
  17. f = open(sys.argv[1], 'rb')
  18. try:
  19. data = f.read()
  20. finally:
  21. f.close()
  22. else:
  23. data = sys.stdin.read()
  24. end = start + len(data) - 1
  25. if sys.argv[2] != '-':
  26. g = open(sys.argv[2], 'wb')
  27. else:
  28. g = sys.stdout.buffer
  29. try:
  30. g.write(b'\xFE')
  31. g.write(struct.pack('<HHH', start, end, exe))
  32. g.write(data)
  33. finally:
  34. if sys.argv[2] != '-':
  35. g.close()
  36. main()