User:Raphi/Import ROM map from Ghidra: Difference between revisions
Appearance
Created page with "Limited support for importing a ROM map from Ghidra. '''TODO:''' We need the ability to merge the wikitext output into an already existing page. 1. Export your functions from Ghidra using the following script: <syntaxhighlight lang="c" lines> /* ### * 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.apach..." |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Limited support for importing a ROM map from Ghidra. '''TODO:''' We need the ability to merge the wikitext output into an already existing page. | 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): | |||
<syntaxhighlight lang=" | <syntaxhighlight lang="java" lines> | ||
/* ### | /* ### | ||
* IP: GHIDRA | * IP: GHIDRA | ||
Line 24: | Line 24: | ||
// * Ignores thunks and FUN_ functions | // * Ignores thunks and FUN_ functions | ||
// List function names and entry point addresses to a file in | // List function names and entry point addresses to a file in wikitext format | ||
//@category Functions | //@category Functions | ||
import java.io.BufferedReader; | |||
import java.io.File; | import java.io.File; | ||
import java.io.FileWriter; | import java.io.FileWriter; | ||
import java.io.InputStreamReader; | |||
import | import java.net.HttpURLConnection; | ||
import | import java.net.URL; | ||
import java.util.ArrayList; | |||
import java.util.Collections; | |||
import ghidra.app.script.GhidraScript; | import ghidra.app.script.GhidraScript; | ||
Line 42: | Line 45: | ||
private static final String ENTRY = "entry"; | private static final String ENTRY = "entry"; | ||
private static final String SIGNATURE = "signature"; | private static final String SIGNATURE = "signature"; | ||
private ArrayList<String> tablePrefix; | |||
private ArrayList<BoktaiFunction> knownAddresses; | |||
private ArrayList<String> tableSuffix; | |||
@Override | @Override | ||
public void run() throws Exception { | 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(); | Listing listing = currentProgram.getListing(); | ||
Line 56: | Line 72: | ||
while (iter.hasNext() && !monitor.isCancelled()) { | while (iter.hasNext() && !monitor.isCancelled()) { | ||
Function f = iter.next(); | Function f = iter.next(); | ||
if (f.isThunk() || f.getName().startsWith("FUN_")) { | long addr = f.getEntryPoint().getOffset(); | ||
if (f.isThunk() || f.getName().startsWith("FUN_") || addr < 0x08000000) { | |||
// skip thunks, undocumented functions, and IWRAM code | |||
continue; | continue; | ||
} | } | ||
String name = f.getName(); | String name = f.getName(); | ||
FunctionSignature sig = f.getSignature(); | 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); | println("Wrote functions to " + outputFile); | ||
} | } | ||
2. | 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; | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |