Jump to content

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

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.


1. Export your functions from Ghidra using the following script:
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="c" lines>
<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 JSON format
// 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 com.google.gson.*;
import java.net.HttpURLConnection;
import com.google.gson.stream.JsonWriter;
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);


Gson gson = new GsonBuilder().setPrettyPrinting().create();
tablePrefix = new ArrayList();
 
knownAddresses = new ArrayList();
File outputFile = askFile("Please Select Output File", "Choose");
tableSuffix = new ArrayList();
JsonWriter jsonWriter = new JsonWriter(new FileWriter(outputFile));
if (gameCode.equals("U3IE")) {
jsonWriter.beginArray();
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();
Address entry = f.getEntryPoint();
FunctionSignature sig = f.getSignature();
FunctionSignature sig = f.getSignature();


JsonObject json = new JsonObject();
BoktaiFunction tableFn = new BoktaiFunction();
json.addProperty(NAME, name);
tableFn.address = addr;
json.addProperty(ENTRY, entry.toString());
int tableIndex = Collections.binarySearch(knownAddresses, tableFn);
json.addProperty(SIGNATURE, sig.getPrototypeString());
if (tableIndex >= 0) {
 
tableFn = knownAddresses.get(tableIndex);
gson.toJson(json, jsonWriter);
} 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;
}
}
}


jsonWriter.endArray();
jsonWriter.close();


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);
}
}
}
</syntaxhighlight>


2. Convert the JSON file you got to WikiText using the following Python script:
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");
}


<syntaxhighlight lang="python" lines>
private void finishParseFunction(BoktaiFunction fn) {
# Point this at the .json file generated by Boktai_ExportFunctionInfoScript.java
if (fn != null) {
import json
fn.description = fn.description.trim();
import sys
knownAddresses.add(fn);
}
}


with open(sys.argv[1]) as f:
static class BoktaiFunction implements Comparable<BoktaiFunction> {
    fns = json.load(f)
public long address;
public String description;


for f in fns:
public int compareTo(BoktaiFunction other) {
    name = f["name"]
if (address < other.address) return -1;
    sig = f["signature"]
if (address > other.address) return 1;
    addr = int(f["entry"], 16)
return 0;
 
}
    print( "|-")
}
    print(f"| 0x{addr:08x} || {sig}")
}
</syntaxhighlight>
</syntaxhighlight>