User:Raphi/Import ROM map from Ghidra

From Boktai Hacking Wiki
Revision as of 12:51, 21 September 2024 by Raphi (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Limited support for importing a ROM map from Ghidra. TODO: We need the ability to merge the wikitext output into an already existing page.

Running this script will export all named functions in the current program to a wikitext file. It will skip functions that already exist in this Wiki (by matching the address):

/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Modified by Raphi for boktaihacking.net:
// * Exports entire signature, not just the name
// * Ignores thunks and FUN_ functions

// List function names and entry point addresses to a file in wikitext format
//@category Functions

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;

import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.*;

public class Boktai_ExportFunctions extends GhidraScript {

	private static final String NAME = "name";
	private static final String ENTRY = "entry";
	private static final String SIGNATURE = "signature";

	private ArrayList<String> tablePrefix;
	private ArrayList<BoktaiFunction> knownAddresses;
	private ArrayList<String> tableSuffix;

	@Override
	public void run() throws Exception {
		byte[] gameCodeBytes = new byte[4];
		currentProgram.getMemory().getBytes(currentProgram.getAddressFactory().getAddress("0x080000ac"), gameCodeBytes);
		String gameCode = new String(gameCodeBytes, "US-ASCII");
		println("Game code: " + gameCode);

		tablePrefix = new ArrayList();
		knownAddresses = new ArrayList();
		tableSuffix = new ArrayList();
		if (gameCode.equals("U3IE")) {
			loadKnownAddresses("https://boktaihacking.net/wiki/ROM_map_(Boktai_1)?action=raw");
		} else if (gameCode.equals("U32E")) {
			loadKnownAddresses("https://boktaihacking.net/wiki/ROM_map_(Boktai_2)?action=raw");
		} else if (gameCode.equals("U33J")) {
			loadKnownAddresses("https://boktaihacking.net/wiki/ROM_map_(Boktai_3)?action=raw");
		}

		Listing listing = currentProgram.getListing();
		FunctionIterator iter = listing.getFunctions(true);
		while (iter.hasNext() && !monitor.isCancelled()) {
			Function f = iter.next();
			long addr = f.getEntryPoint().getOffset();
			if (f.isThunk() || f.getName().startsWith("FUN_") || addr < 0x08000000) {
				// skip thunks, undocumented functions, and IWRAM code
				continue;
			}

			String name = f.getName();
			FunctionSignature sig = f.getSignature();

			BoktaiFunction tableFn = new BoktaiFunction();
			tableFn.address = addr;
			int tableIndex = Collections.binarySearch(knownAddresses, tableFn);
			if (tableIndex >= 0) {
				tableFn = knownAddresses.get(tableIndex);
			} else {
				knownAddresses.add(-(tableIndex+1), tableFn);
			}
			tableFn.description = sig.getPrototypeString().replace("'ROM", "");
			String comment = f.getComment();
			if (comment != null && comment.length() > 0) {
				tableFn.description += "\n" + comment;
			}
		}


		File outputFile = askFile("Please Select Output File", "Choose");
		FileWriter fileWriter = new FileWriter(outputFile);
		for (String s : tablePrefix) {
			fileWriter.write(s);
			fileWriter.write("\n");
		}
		boolean firstFn = true;
		for (BoktaiFunction fn : knownAddresses) {
			if (!firstFn) {
				fileWriter.write("|-\n");
			}
			firstFn = false;
			fileWriter.write("| 0x");
			fileWriter.write(Long.toHexString(fn.address));
			fileWriter.write(" || ");
			fileWriter.write(fn.description);
			fileWriter.write("\n");
		}
		for (String s : tableSuffix) {
			fileWriter.write(s);
			fileWriter.write("\n");
		}
		fileWriter.close();
		println("Wrote functions to " + outputFile);
	}

	private void loadKnownAddresses(String url) throws Exception {
		println("Downloading known addresses from " + url);
		HttpURLConnection http = (HttpURLConnection) new URL(url).openConnection();
		http.setRequestMethod("GET");
		BufferedReader httpStream = new BufferedReader(new InputStreamReader(http.getInputStream()));
		String line;
		final int STATE_PREFIX = 0;
		final int STATE_TABLE_HEADER1 = 1;
		final int STATE_TABLE_HEADER2 = 2;
		final int STATE_IN_ROW = 3;
		final int STATE_SUFFIX = 4;
		int state = STATE_PREFIX;
		BoktaiFunction fn = null;
		while ((line = httpStream.readLine()) != null) {
			if (state == STATE_PREFIX) {
				tablePrefix.add(line);
				if (line.startsWith("{|")) {
					state = STATE_TABLE_HEADER1;
				}
			} else if (state == STATE_TABLE_HEADER1) {
				tablePrefix.add(line);
				if (line.startsWith("! ")) {
					state = STATE_TABLE_HEADER2;
				}
			} else if (state == STATE_TABLE_HEADER2) {
				tablePrefix.add(line);
				if (line.startsWith("|-")) {
					state = STATE_IN_ROW;
				}
			} else if (state == STATE_IN_ROW) {
				if (line.startsWith("|}")) {
					finishParseFunction(fn);
					fn = null;
					state = STATE_SUFFIX;
					tableSuffix.add(line);
				} else {
					if (line.startsWith("|-")) {
						finishParseFunction(fn);
						fn = null;
					} else if (fn == null && line.startsWith("| ")) {
						fn = new BoktaiFunction();
						fn.description = "";
						int idx = line.indexOf("||");
						if (idx > 0) {
							fn.description = line.substring(idx + 2);
						}
						if (idx < 0) idx = line.length();
						fn.address = Integer.parseInt(line.substring(4, idx).trim(), 16);
					} else if (fn != null && line.startsWith("| ")) {
						fn.description = line.substring(2);
					} else {
						fn.description = fn.description + "\n" + line;
					}
				}
			} else if (state == STATE_SUFFIX) {
				tableSuffix.add(line);
			}
		}
		Collections.sort(knownAddresses);
		println("Found " + knownAddresses.size() + " addresses in the Wiki");
	}

	private void finishParseFunction(BoktaiFunction fn) {
		if (fn != null) {
			fn.description = fn.description.trim();
			knownAddresses.add(fn);
		}
	}

	static class BoktaiFunction implements Comparable<BoktaiFunction> {
		public long address;
		public String description;

		public int compareTo(BoktaiFunction other) {
			if (address < other.address) return -1;
			if (address > other.address) return 1;
			return 0;
		}
	}
}