From 0b44f4a11fa13f9eab84aeb5918a190240fb511d Mon Sep 17 00:00:00 2001 From: Cameron Kaiser Date: Fri, 21 Jul 2023 22:14:10 -0700 Subject: [PATCH] initial commit --- README.md | 36 +++++++++++++++ checksum.pl | 21 +++++++++ resscan.pl | 119 +++++++++++++++++++++++++++++++++++++++++++++++++ splicedisk0.pl | 102 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 278 insertions(+) create mode 100644 README.md create mode 100755 checksum.pl create mode 100755 resscan.pl create mode 100755 splicedisk0.pl diff --git a/README.md b/README.md new file mode 100644 index 0000000..d22b385 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Apple Set Top Box/Interactive Television Box Toolsuite + +[Another Old VCR Super Hit!](http://oldvcr.blogspot.com/) + +Copyright 2023 Cameron Kaiser. +All rights reserved. +BSD license. + +## What it is + +[Read the blog post first!](https://oldvcr.blogspot.com/2023/07/apples-interactive-television-box.html) + +This is a set of three Perl tools for working with a "red" ROM dump for the Apple Interative Television Box/Set Top Box (aka AITB, ITV or STB), specifically the STB3. + +* The tool `checksum.pl` reads the embedded checksum in a classic Mac ROM and compares with a computed one. Pass the ROM dump on standard input or as a filename argument. + +* The tool `resscan.pl` walks a ROM dump and emits the resources it finds. **Currently this only works for red STB ROMs, not the green ROM (which is actually a regular Quadra 605 ROM) and not any other ROMs, though this is intended in the future.** Pass the ROM dump on standard input or as a filename argument. If you pass (a) pair(s) of resource codes and numbers after the ROM filename, then the scanner will emit a dump of that code and resource number, if it exists (such as `DRVR-0.dump`). + +* The tool `splicedisk0.pl` walks a ROM dump and inserts the provided disk image into resource `disk#0`. The disk image must be bootable HFS with boot blocks and a blessed System Folder, which the tool will check, and must fit within the provided space. This is intended only for the STB ROM disk image. If you did not change the folder structure but the System Folder got unblessed, try adding the `-fix16` argument to attempt to automatically repair the disk image for insertion. + +There are many "gotchas" about this process which are best explained by [the original blog post](https://oldvcr.blogspot.com/2023/07/apples-interactive-television-box.html). + +## License + +Copyright (C) 2023 Cameron Kaiser. All rights reserved. It is released under the three-clause BSD license, i.e.: + +"Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF/SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." + diff --git a/checksum.pl b/checksum.pl new file mode 100755 index 0000000..c5e6b8d --- /dev/null +++ b/checksum.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +# +# Compute Mac checksum for a ROM and compares it with the existing one. +# Provide the ROM as an argument or on standard input. +# +# Copyright (C) 2023 Cameron Kaiser. All rights reserved. +# BSD license. +# oldvcr.blogspot.com + +eval "use bytes"; +$/ = \4; $actual = unpack("N", <>); printf("expected = 0x%08x\n", $actual); + +read(ARGV, $buf, 8388608); +die("length of buffer is not even: @{[ length($buf) ]} bytes\n") + if (length($buf) & 1); +print "bytes read = @{[ length($buf) + 4 ]}\n"; +$checksum = 0; +map { $checksum += $_ } unpack("n*", $buf); # map is faster than grep +$checksum &= 4294967295; +printf("computed = 0x%08x (%s)\n", $checksum, + ($checksum == $actual) ? "matches" : "DOES NOT MATCH"); diff --git a/resscan.pl b/resscan.pl new file mode 100755 index 0000000..2485c2d --- /dev/null +++ b/resscan.pl @@ -0,0 +1,119 @@ +#!/usr/bin/perl +# +# Scan for resources in a ROM dump. Right now, this only works with the +# backchains in the red Set Top Box ROM, but I'd like to make it be able +# to parse most classic Mac Toolbox ROM dumps generally. +# +# Pass the ROM as an argument or on standard input. +# If you provide pairs of resource codes and numbers after, then this scanner +# will put them in files for you. +# +# Example: ./resscan.pl RED.rom disk 0 +# +# Copyright (C) 2023 Cameron Kaiser. All rights reserved. +# BSD license. +# oldvcr.blogspot.com + +use bytes; + +# the resource code must fall on a 32-bit boundary +undef $/; $rrom = <>; @rom = unpack("N*", $rrom); +$rlen = scalar(@rom); +print (($rlen * 4). " bytes loaded\n"); + +$marker0 = hex("78000000"); +$marker1 = hex("08000000"); +$marker2 = hex("18000000"); +$marker3 = hex("28000000"); +$marker4 = hex("38000000"); +$marker5 = hex("70000000"); +$backchain = 0; +# resource entries always on 16-byte boundary +for($i=0;$i<$rlen;$i+=4) { + $type = $rom[$i]; + next if (($type & 134217727) || ($type == 0)); # 0x07ffffff + next unless $rom[$i+1] == 0; + # rooted type + next unless ($rom[$i+2] == $backchain || $type == $marker2); + + $start = $rom[$i+3]; + next if ($start & 3); # unpossible + + # candidate found, see if it's rational + if ($type == $marker0) { + $backchain = ($i * 4); + $end = $backchain; + $i += 4; + } elsif ($type == $marker1 || $type == $marker2 || + $type == $marker3 || $type == $marker4 || + $type == $marker5) { + # XXX: not working yet + $backchain = ($i * 4); + $end = 0; + } else { + next; + } + + # resource type should be printable + $roff = ($i * 4); + $resc = substr($rrom, $roff, 4); + next if grep { $_ < 32 || $_ > 127 } unpack("C4", $resc); + + $rnum = ($rom[$i+1] >> 16); + $flags = ($rom[$i+1] & 65535); + + # get name + $i += 2; + $hexn = sprintf("%08x%08x%08x%08x%08x%08x", $rom[$i], + $rom[$i+1],$rom[$i+2],$rom[$i+3],$rom[$i+4],$rom[$i+5]); + ($hname, $crap) = split("00", $hexn, 2); + $name = pack("H*", $hname); + $i += 2; + + printf "[%08x] found %s #%d at 0x%08x 0x%08x \"%s\" \n", $type, + $resc, $rnum, $start, $end, $name; + + if ($resc eq 'STR ') { + $length = unpack("C", substr($rrom, $start, 1)); + print " [$resc] \"", substr($rrom, $start+1, $length), + "\" (length $length)\n"; + } + if ($resc eq 'STR#') { + $num = unpack("n", substr($rrom, $start, 2)); + $anustart = $start + 2; # KEEP TOBIAS BLUE + print " [STR#] $num strings follow\n"; + print " -------------------\n"; + for(1..$num) { + $length = unpack("C", substr($rrom, $anustart++, 1)); + if ($length) { + print " [$_] \"", + substr($rrom, $anustart, $length), + "\" (length $length)\n"; + } else { + print " [$_] (length 0)\n"; + } + $anustart += $length; + } + } + + if ($resc eq $ARGV[0] && $rnum == $ARGV[1]) { + print STDOUT "... writing to disk\n"; + open(S, ">$resc-$rnum.dump") || die("can't write resource: $!\n"); + print S substr($rrom, $start, ($end - $start)); + close(S); + shift @ARGV; + shift @ARGV; + } +} + +__DATA__ + + 001c7a70: 78 00 00 00 00 00 00 00 00 1c 78 80 00 1c 78 b0 x.........x...x. + 001c7a80: 50 49 43 54 b5 12 58 00 6b 63 6b 63 6b 63 6b 63 PICT..X.kckckckc + 001c7a90: 4b 75 72 74 c0 a0 00 00 00 00 00 c6 00 00 05 d4 Kurt............ + + 000572a0: 78 00 00 00 00 00 00 00 00 05 71 a0 00 05 72 e0 x.........q...r. + 000572b0: 50 49 43 54 00 63 58 10 44 69 73 6b 4d 6f 64 65 PICT.cX.DiskMode + 000572c0: 20 42 61 74 74 65 72 79 00 00 00 00 00 00 00 00 Battery........ + 000572d0: 00 00 00 00 c0 a0 00 00 00 00 00 d1 00 00 02 b4 ................ + diff --git a/splicedisk0.pl b/splicedisk0.pl new file mode 100755 index 0000000..4d76849 --- /dev/null +++ b/splicedisk0.pl @@ -0,0 +1,102 @@ +#!/usr/bin/perl -s +# +# Inserts an HFS image into an existing STB ROM. Must fit, must pass +# sanity checks. +# +# Copyright (C) 2023 Cameron Kaiser. All rights reserved. +# BSD license. +# oldvcr.blogspot.com +# +# usage: ./splicedisk0.pl input-rom disk-img output-rom + +use bytes; +die("usage: $0 input-rom disk-img output-rom\n") if ($#ARGV != 2); + +open(W, "$ARGV[0]") || die("can't open input ROM: $!\n"); +undef $/; $rrom = ; @rom = unpack("N*", $rrom); +$rlen = scalar(@rom); +print (($rlen * 4). " ROM bytes loaded\n"); +close(W); + +open(X, "$ARGV[1]") || die("can't open input image: $!\n"); +undef $/; $rdisk = ; +print length($rdisk), " disk image bytes loaded\n"; +die("boot blocks are missing\n") if (substr($rdisk, 0, 2) ne 'LK'); +if (unpack("H*", substr($rdisk, 1119, 1)) eq '00') { + unless ($fix16) { + die("not a bootable image: check byte 1119/0x045f\n") + } else { + print "fixing bootable folder byte at 0x045f to 0x10\n"; + $rdisk = substr($rdisk, 0, 1119) . pack("H*", "10") . + substr($rdisk, 1120); + } +} +close(X); + +$marker0 = hex("78000000"); +$backchain = 0; +# resource entries always on 16-byte boundary +for($i=0;$i<$rlen;$i+=4) { + $type = $rom[$i]; + next if (($type & 134217727) || ($type == 0)); # 0x07ffffff + next unless $rom[$i+1] == 0; + # rooted type + next unless ($rom[$i+2] == $backchain); + + $start = $rom[$i+3]; + next if ($start & 3); # unpossible + + # candidate found, see if it's rational + if ($type == $marker0) { + $backchain = ($i * 4); + $end = $backchain; + $i += 4; + } else { + next; + } + + # resource type should be printable + $roff = ($i * 4); + $resc = substr($rrom, $roff, 4); + next if grep { $_ < 32 || $_ > 127 } unpack("C4", $resc); + + $rnum = ($rom[$i+1] >> 16); + $flags = ($rom[$i+1] & 65535); + + # get name + $i += 2; + $hexn = sprintf("%08x%08x%08x%08x%08x%08x", $rom[$i], + $rom[$i+1],$rom[$i+2],$rom[$i+3],$rom[$i+4],$rom[$i+5]); + ($hname, $crap) = split("00", $hexn, 2); + $name = pack("H*", $hname); + $i += 2; + + printf "[%08x] found %s #%d at 0x%08x 0x%08x \"%s\" \n", $type, + $resc, $rnum, $start, $end, $name; + + if ($resc eq "disk" && $rnum == 0) { + die("can't splice, new disk image length does not match\n") + if (($end - $start) != length($rdisk)); + + print STDOUT "... splicing in new HFS disk image\n"; + $nrom = substr($rrom, 0, $start); + $nrom .= $rdisk; + $nrom .= substr($rrom, $end); + die("oops\n") if (length($nrom) != length($rrom)); + + $nrom = substr($nrom, 4); $checksum = 0; + map { $checksum += $_ } unpack("n*", $nrom); + $checksum &= 4294967295; + printf STDOUT "... new checksum=0x%08x\n", $checksum; + + open(S, ">$ARGV[2]") || die("can't write output: $!\n"); + print S pack("N", $checksum); + print S $nrom; + close(S); + exit 0; + } +} + +print "unable to find resource disk#0\n"; +exit 1; +