User:Raphi/Import ROM map from Ghidra: Difference between revisions
No edit summary |
No edit summary |
||
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: | 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 27: | Line 27: | ||
//@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 java.net.HttpURLConnection; | |||
import java.net.URL; | |||
import java.util.HashSet; | |||
import ghidra.app.script.GhidraScript; | import ghidra.app.script.GhidraScript; | ||
Line 39: | Line 44: | ||
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 HashSet<Long> knownAddresses; | |||
@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); | |||
knownAddresses = new HashSet(); | |||
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"); | |||
} | |||
File outputFile = askFile("Please Select Output File", "Choose"); | File outputFile = askFile("Please Select Output File", "Choose"); | ||
FileWriter fileWriter = new FileWriter(outputFile); | FileWriter fileWriter = new FileWriter(outputFile); | ||
Line 56: | Line 77: | ||
Address entry = f.getEntryPoint(); | Address entry = f.getEntryPoint(); | ||
FunctionSignature sig = f.getSignature(); | FunctionSignature sig = f.getSignature(); | ||
if (knownAddresses.contains(entry.getOffset())) continue; | |||
fileWriter.write("|-\n"); | fileWriter.write("|-\n"); | ||
Line 67: | Line 90: | ||
fileWriter.close(); | fileWriter.close(); | ||
println("Wrote functions to " + outputFile); | 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; | |||
while ((line = httpStream.readLine()) != null) { | |||
if (line.startsWith("| 0x")) { | |||
long address = Integer.parseInt(line.substring(4, 12), 16); | |||
knownAddresses.add(address); | |||
} | |||
} | |||
println("Found " + knownAddresses.size() + " addresses in the Wiki"); | |||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 09:53, 31 August 2024
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.HashSet;
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 HashSet<Long> knownAddresses;
@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);
knownAddresses = new HashSet();
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");
}
File outputFile = askFile("Please Select Output File", "Choose");
FileWriter fileWriter = new FileWriter(outputFile);
Listing listing = currentProgram.getListing();
FunctionIterator iter = listing.getFunctions(true);
while (iter.hasNext() && !monitor.isCancelled()) {
Function f = iter.next();
if (f.isThunk() || f.getName().startsWith("FUN_")) {
continue;
}
String name = f.getName();
Address entry = f.getEntryPoint();
FunctionSignature sig = f.getSignature();
if (knownAddresses.contains(entry.getOffset())) continue;
fileWriter.write("|-\n");
fileWriter.write("| 0x");
fileWriter.write(entry.toString());
fileWriter.write(" || ");
fileWriter.write(sig.getPrototypeString());
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;
while ((line = httpStream.readLine()) != null) {
if (line.startsWith("| 0x")) {
long address = Integer.parseInt(line.substring(4, 12), 16);
knownAddresses.add(address);
}
}
println("Found " + knownAddresses.size() + " addresses in the Wiki");
}
}