52 lines
2.1 KiB
Java
52 lines
2.1 KiB
Java
// Exports binary read only and data segments to a binary + header file
|
|
// @category _Reman3
|
|
// @menupath Tools.Reman3.Export Data
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.PrintWriter;
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.program.model.address.Address;
|
|
import re3lib.RecompileConfig;
|
|
|
|
public class ExportData extends GhidraScript {
|
|
|
|
@Override
|
|
protected void run() throws Exception {
|
|
if (currentProgram == null) {
|
|
return;
|
|
}
|
|
RecompileConfig.INSTANCE = new RecompileConfig(this);
|
|
|
|
String dataFile = new File(RecompileConfig.INSTANCE.outputDir, "gh_datasegment.bin").toString();
|
|
String headerFile = new File(RecompileConfig.INSTANCE.outputDir, "gh_datasegment.h").toString();
|
|
|
|
FileOutputStream dataOutputStream = new FileOutputStream(dataFile);
|
|
PrintWriter headerWriter = new PrintWriter(headerFile, "UTF-8");
|
|
|
|
Address startAddr = RecompileConfig.INSTANCE.staticMemoryBlockStart;
|
|
Address endAddr = RecompileConfig.INSTANCE.staticMemoryBlockEnd;
|
|
|
|
// Dump all the memory to the bin file
|
|
int numBytes = (int) endAddr.subtract(startAddr);
|
|
byte[] buffer = new byte[numBytes];
|
|
currentProgram.getMemory().getBytes(startAddr, buffer);
|
|
dataOutputStream.write(buffer);
|
|
|
|
headerWriter.println("// AUTO-GENERATED FILE, DO NOT MODIFY!!!!!");
|
|
headerWriter.println("// Use 'Export Data' script to export the data segment");
|
|
headerWriter.println("#pragma once");
|
|
headerWriter.println("#include <cstddef>");
|
|
headerWriter.println("");
|
|
headerWriter.println("#define GH_DATA_START 0x" + startAddr.toString());
|
|
headerWriter.println("#define GH_DATA_END 0x" + endAddr.toString());
|
|
headerWriter.println("#define GH_DATA_SIZE (GH_DATA_END - GH_DATA_START)");
|
|
headerWriter.println("constexpr size_t gh_data_start = 0x" + startAddr.toString() + ";");
|
|
headerWriter.println("constexpr size_t gh_data_end = 0x" + endAddr.toString() + ";");
|
|
headerWriter.println("constexpr size_t gh_data_size = gh_data_end - gh_data_start;");
|
|
|
|
headerWriter.close();
|
|
dataOutputStream.close();
|
|
}
|
|
}
|