amiga_boottest

diff tools/mk_adf.py @ 0:51422ea54b9d

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 21 Feb 2018 12:00:13 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/mk_adf.py	Wed Feb 21 12:00:13 2018 +0200
     1.3 @@ -0,0 +1,56 @@
     1.4 +#!/usr/bin/python
     1.5 +# mk_adf.py <bootblock> <payload> <output_adf>
     1.6 +#
     1.7 +# Stuff a given bootblock and payload into an output ADF image.
     1.8 +# 
     1.9 +# Written & released by Keir Fraser <keir.xen@gmail.com>
    1.10 +# 
    1.11 +# This is free and unencumbered software released into the public domain.
    1.12 +# See the file COPYING for more details, or visit <http://unlicense.org>.
    1.13 +
    1.14 +import struct, sys
    1.15 +
    1.16 +# Amiga bootblock checksum
    1.17 +def checksum(bb, sum=0):
    1.18 +    while len(bb):
    1.19 +        x, bb = struct.unpack(">L",bb[:4]), bb[4:]
    1.20 +        sum += x[0]
    1.21 +        if sum >= (1<<32):
    1.22 +            sum -= (1<<32)-1
    1.23 +    return sum
    1.24 +
    1.25 +def main(argv):
    1.26 +    bb_f = open(argv[1], "rb")
    1.27 +    pl_f = open(argv[2], "rb")
    1.28 +    out_f = open(argv[3], "wb")
    1.29 +    bb_dat = bb_f.read()
    1.30 +    pl_dat = pl_f.read()
    1.31 +
    1.32 +    # Construct bootblock header. We will splice in the checksum later.
    1.33 +    header = struct.pack(">ccccLLLL",
    1.34 +                         'D', 'O', 'S', '\0',         # Bootblock signature
    1.35 +                         0,                           # Checksum (placeholder)
    1.36 +                         880,                         # Root block
    1.37 +                         0x60060000,                  # BRA.B +6
    1.38 +                         (len(pl_dat) + 511) & ~511)  # Payload length, padded
    1.39 +                         
    1.40 +    
    1.41 +    # Compute checksum over header, bootblock, and first 512 bytes of payload.
    1.42 +    sum = checksum(pl_dat[:512], checksum(bb_dat, checksum(header)))
    1.43 +    sum ^= 0xFFFFFFFF
    1.44 +    # Splice the computed checksum into the header
    1.45 +    header = header[:4] + struct.pack(">L", sum) + header[8:]
    1.46 +    # Write out the header and bootblock code
    1.47 +    out_f.write(header)
    1.48 +    out_f.write(bb_dat)
    1.49 +    # Pad bootblock to 512 bytes
    1.50 +    for x in xrange((512-len(bb_dat)-len(header))/4):
    1.51 +        out_f.write(struct.pack(">L", 0))
    1.52 +    # Write the payload from sector 1 onwards
    1.53 +    out_f.write(pl_dat)
    1.54 +    # Pad the ADF image to 880kB
    1.55 +    for x in xrange((901120-len(pl_dat)-512)/4):
    1.56 +        out_f.write(struct.pack(">L", 0))
    1.57 +        
    1.58 +if __name__ == "__main__":
    1.59 +    main(sys.argv)