From 01681e300b3c5d487883f8e3f8b3943038b5c3f3 Mon Sep 17 00:00:00 2001 From: Wei-ju Wu Date: Tue, 12 May 2015 15:58:48 -0700 Subject: [PATCH] added rom decoding --- utils/decode_cloanto_rom.py | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 utils/decode_cloanto_rom.py diff --git a/utils/decode_cloanto_rom.py b/utils/decode_cloanto_rom.py new file mode 100755 index 0000000..1fbe76a --- /dev/null +++ b/utils/decode_cloanto_rom.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +""" +Convert Cloanto ROM to normal Kickstart ROM +""" +import argparse + +CLOANTO_ID = b'AMIROMTYPE1' +SIZE_CLASSIC = 0x40000 +SIZE_MODERN = 0x80000 +MAX_ROM_SIZE = SIZE_MODERN + +def decode_cloanto(rom, key, real_size): + rom = bytearray(rom) + key = bytearray(key) + result = bytearray(len(rom)) + key_size = len(key) + key_index = 0 + + for i in range(len(rom)): + result[i] = rom[i] ^ key[key_index] + key_index = (key_index + 1) % key_size + + return result + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description="Cloanto Decoder 1.0") + parser.add_argument('romfile') + parser.add_argument('keyfile') + parser.add_argument('target') + args = parser.parse_args() + + with open(args.keyfile, 'rb') as infile: + key_data = infile.read() + + with open(args.romfile, 'rb') as infile: + file_id = infile.read(len(CLOANTO_ID)) + if file_id == CLOANTO_ID: + data = infile.read() + size = len(data) + if size != SIZE_CLASSIC and size != SIZE_MODERN: + raise Exception("invalid rom size") + decoded = decode_cloanto(data, key_data, len(data)) + with open(args.target, 'wb') as outfile: + outfile.write(bytes(decoded)) + else: + raise Exception('not a cloanto rom: ', file_id)