Jump to content

User:Raphi/Import ROM map from Ghidra: Difference between revisions

no edit summary
No edit summary
No edit summary
 
Line 33: Line 33:
import java.net.HttpURLConnection;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URL;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Collections;


import ghidra.app.script.GhidraScript;
import ghidra.app.script.GhidraScript;
Line 45: Line 46:
private static final String SIGNATURE = "signature";
private static final String SIGNATURE = "signature";


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


@Override
@Override
Line 54: Line 57:
println("Game code: " + gameCode);
println("Game code: " + gameCode);


knownAddresses = new HashSet();
tablePrefix = new ArrayList();
knownAddresses = new ArrayList();
tableSuffix = new ArrayList();
if (gameCode.equals("U3IE")) {
if (gameCode.equals("U3IE")) {
loadKnownAddresses("https://boktaihacking.net/wiki/ROM_map_(Boktai_1)?action=raw");
loadKnownAddresses("https://boktaihacking.net/wiki/ROM_map_(Boktai_1)?action=raw");
Line 62: Line 67:
loadKnownAddresses("https://boktaihacking.net/wiki/ROM_map_(Boktai_3)?action=raw");
loadKnownAddresses("https://boktaihacking.net/wiki/ROM_map_(Boktai_3)?action=raw");
}
}
File outputFile = askFile("Please Select Output File", "Choose");
FileWriter fileWriter = new FileWriter(outputFile);


Listing listing = currentProgram.getListing();
Listing listing = currentProgram.getListing();
Line 70: 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();
Address entry = f.getEntryPoint();
FunctionSignature sig = f.getSignature();
FunctionSignature sig = f.getSignature();


if (knownAddresses.contains(entry.getOffset())) continue;
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;
}
}


fileWriter.write("|-\n");
 
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("| 0x");
fileWriter.write(entry.toString());
fileWriter.write(Long.toHexString(fn.address));
fileWriter.write(" || ");
fileWriter.write(" || ");
fileWriter.write(sig.getPrototypeString());
fileWriter.write(fn.description);
fileWriter.write("\n");
}
for (String s : tableSuffix) {
fileWriter.write(s);
fileWriter.write("\n");
fileWriter.write("\n");
}
}
fileWriter.close();
fileWriter.close();
println("Wrote functions to " + outputFile);
println("Wrote functions to " + outputFile);
Line 98: Line 129:
BufferedReader httpStream = new BufferedReader(new InputStreamReader(http.getInputStream()));
BufferedReader httpStream = new BufferedReader(new InputStreamReader(http.getInputStream()));
String line;
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) {
while ((line = httpStream.readLine()) != null) {
if (line.startsWith("| 0x")) {
if (state == STATE_PREFIX) {
long address = Integer.parseInt(line.substring(4, 12), 16);
tablePrefix.add(line);
knownAddresses.add(address);
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");
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>