Fix detokenizer for OF 1.x images.

This commit is contained in:
Maxim Poliakovski 2022-07-28 01:30:13 +02:00
parent e3616ecc59
commit 4e75ac0f3c
2 changed files with 19 additions and 6 deletions

View File

@ -169,6 +169,7 @@ FORTH_WORDS = {
0xFD : ('version1', ['fcode_hdr', 'offset8']),
0x102 : ('my-address', []),
0x103 : ('my-space', []),
0x104 : ('memmap', []),
0x110 : ('property', []),
0x111 : ('encode-int', []),
0x112 : ('encode+', []),
@ -179,6 +180,7 @@ FORTH_WORDS = {
0x11A : ('device-type', []),
0x11C : ('is-install', []),
0x11D : ('is-remove', []),
0x11F : ('new-device', []),
0x125 : ('get-msecs', []),
0x126 : ('ms', []),
0x128 : ('decode-phys', []),
@ -192,10 +194,12 @@ FORTH_WORDS = {
0x18B : ('fb8-install', []),
0x201 : ('device-name', []),
0x203 : ('my-self', []),
0x204 : ('find-package', []),
0x207 : ('find-method', []),
0x209 : ('$call-parent', []),
0x20A : ('my-parent', []),
0x20B : ('ihandle>phandle', []),
0x20F : ('$open-package', []),
0x216 : ('abort', []),
0x21A : ('get-my-property', []),
0x21B : ('decode-int', []),
@ -331,5 +335,4 @@ class DeTokenizer():
elif tok_num in self.user_dict: # check user dictionary
print("0x%X ; %s" % (tok_num, self.user_dict[tok_num]))
else:
print("Token %X not found at offset 0x%X!" % (tok_num, self.pos))
crash()
print("0x%X ; %s" % (tok_num, 'undefined_' + format(tok_num, 'x')))

View File

@ -1,7 +1,7 @@
'''
DeTokenizer for Apple OpenFirmware.
Author: Max Poliakovski 2019-2021
Author: Max Poliakovski 2019-2022
'''
import struct
from argparse import ArgumentParser
@ -42,9 +42,6 @@ def populate_user_dict(src_dict, dst_dict):
dst_dict[tok_num] = word['name']
# add Apple specific FCodes for managing stack frames
for i in range(0,9):
dst_dict[0x407 + i] = '(pushlocals_%s)' % i
for i in range(0,8):
dst_dict[0x410 + i] = '(local@%s)' % i
dst_dict[0x418 + i] = '(local!%s)' % i
@ -60,6 +57,10 @@ def main():
dest='of_offset',
help='offset to OF container (autodetect attempt if omitted)',
metavar='OF_OFFSET', required=True)
parser.add_argument('--of_version', type=int,
dest='of_version',
help='Open Firmware version used to produce the input token stream',
metavar='OF_VERSION', default=2)
opts = parser.parse_args()
with open(opts.rom_path, 'rb') as infile:
@ -89,6 +90,15 @@ def main():
populate_user_dict(dict, detokenizer.user_dict)
# OF v1.x uses 0x401,XX sequences for (pushlocals_XX)
# where the 2nd byte XX specifies the number of locals to push
# OF v2.x uses FCodes in the range 0x407...0x40F for the same purpose
if opts.of_version == 1:
detokenizer.builtin_dict[0x401] = ('(pushlocals)', ['offset'])
else:
for i in range(0,9):
detokenizer.user_dict[0x407 + i] = '(pushlocals_%s)' % i
detokenizer.decode_stream()
print("\nDetokenizing device packages...")