-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeboot.py
More file actions
83 lines (73 loc) · 1.87 KB
/
makeboot.py
File metadata and controls
83 lines (73 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/python3
# makeboot
import sys
def usage():
print(f"Makeboot - usage: {sys.argv[0]} [-ibm|-atari|-amiga|-msx] <image file name>")
exit()
def ibm_makeboot(filename):
with open(filename, 'r+b') as fp:
fp.seek(0x1FE)
fp.write(b'\x55\xAA')
def atari_makeboot(filename):
with open(filename, "r+b") as fp:
checksum = 0x1234
for i in range(510):
if (i & 1) == 0:
checksum -= (fp.read(1)[0] & 0xFF) << 8
else:
checksum -= fp.read(1)[0] & 0xFF
checksum &= 0xFFFF
fp.write(bytes([(checksum >> 8) & 0xFF, checksum & 0xFF]))
def msx_makeboot(filename):
with open(filename, "r+b") as fp:
fp.write(b'\xEB\xFE\x90')
# entry at offset 0x1E
def amiga_makeboot(filename, block_size = 1024):
with open(filename, "r+b") as fp:
fp.write(b"DOS\0\0\0\0\0\0\0\x03\x70")
fp.seek(0)
checksum = 0
for i in range(block_size):
if (i & 3) == 0:
checksum += (fp.read(1)[0] & 0xFF) << 24
elif (i & 3) == 1:
checksum += (fp.read(1)[0] & 0xFF) << 16
elif (i & 3) == 2:
checksum += (fp.read(1)[0] & 0xFF) << 8
elif (i & 3) == 3:
checksum += fp.read(1)[0] & 0xFF
if checksum > 0xFFFFFFFF:
checksum += 1
checksum &= 0xFFFFFFFF
fp.seek(4)
checksum = ~checksum
fp.write(bytes([(checksum >> 24) & 0xFF, (checksum >> 16) & 0xFF, (checksum >> 8) & 0xFF, checksum & 0xFF]))
# entry at offset 0x0C
def main():
if len(sys.argv) <= 1:
usage()
exit()
if sys.argv[1] == '-ibm':
if len(sys.argv) <= 2:
usage()
exit()
ibm_makeboot(sys.argv[2])
elif sys.argv[1] == '-atari':
if len(sys.argv) <= 2:
usage()
exit()
atari_makeboot(sys.argv[2])
elif sys.argv[1] == '-msx':
if len(sys.argv) <= 2:
usage()
exit()
msx_makeboot(sys.argv[2])
elif sys.argv[1] == '-amiga':
if len(sys.argv) <= 2:
usage()
exit()
amiga_makeboot(sys.argv[2])
else:
ibm_makeboot(sys.argv[1])
if __name__ == '__main__':
main()