Skip to content

Commit

Permalink
Initial functional implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniovazquezblanco committed Dec 1, 2023
1 parent 4a95034 commit 6a0f2da
Show file tree
Hide file tree
Showing 3 changed files with 741 additions and 408 deletions.
69 changes: 61 additions & 8 deletions src/main/java/devicetreeblob/DeviceTreeBlobPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JComponent;

import devicetreeblob.DtbParser.Block;
import devicetreeblob.DtbParser.Block.Reg;
import docking.action.builder.ActionBuilder;
import docking.tool.ToolConstants;
import docking.widgets.filechooser.GhidraFileChooser;
Expand All @@ -33,7 +38,14 @@
import ghidra.framework.plugintool.PluginTool;
import ghidra.framework.plugintool.util.PluginStatus;
import ghidra.framework.preferences.Preferences;
import ghidra.framework.store.LockException;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressOverflowException;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.Memory;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.mem.MemoryConflictException;
import ghidra.util.Msg;
import ghidra.util.filechooser.ExtensionFileFilter;

Expand All @@ -54,7 +66,7 @@ public DeviceTreeBlobPlugin(PluginTool tool) {
super(tool);
createActions();
}

private void createActions() {
new ActionBuilder("Load DTB File", this.getName()).withContext(ProgramActionContext.class)
.validContextWhen(pac -> pac.getProgram() != null).menuPath(ToolConstants.MENU_FILE, "Load DTB File...")
Expand All @@ -68,23 +80,64 @@ private void loadDtb(ProgramActionContext pac) {
Msg.showWarn(getClass(), null, "Load PDB", "Unable to load PDB file while analysis is running.");
return;
}

File file = getDtbFileFromDialog(pac.getComponentProvider().getComponent());
if (file == null)
return;

Msg.info(getClass(), "Loading " + file.getPath());
Dtb dtb;
DtbParser dtb;
try {
dtb = Dtb.fromFile(file.getAbsolutePath());
} catch (IOException e) {
dtb = new DtbParser(file);
} catch (IOException | ParseException e) {
Msg.error(getClass(), "Could not parse DTB file!", e);
return;
}



Msg.info(getClass(), "Filtering unwanted DTB blocks...");
List<Block> memBlocks = dtb.mBlocks.stream().filter(x -> (!x.name().equalsIgnoreCase("cpu") && x.hasRegs()))
.collect(Collectors.toList());

Msg.info(getClass(), "Creating regions in memory...");
Memory memory = program.getMemory();
AddressSpace addrSpace = program.getAddressFactory().getDefaultAddressSpace();
for (Block block : memBlocks)
for (Reg region : block.getRegs()) {
String reg_name = block.name() + ((region.name != null) ? ("_" + region.name) : "");
Address addr = addrSpace.getAddress(region.addr);

int transactionId = program.startTransaction("Device Tree Blob memory block creation");
boolean ok = createMemoryRegion(memory, reg_name, addr, region.size);
program.endTransaction(transactionId, ok);
}
}

private boolean createMemoryRegion(Memory memory, String name, Address addr, Long size) {
try {
MemoryBlock memBlock = memory.createUninitializedBlock(name, addr, size, false);
boolean isRam = name.equals("memory");
memBlock.setRead(true);
memBlock.setWrite(true);
memBlock.setExecute(isRam);
memBlock.setVolatile(!isRam);
memBlock.setComment("Generated by Device Tree Blob");
return true;
} catch (MemoryConflictException e) {
Msg.error(getClass(),
"Could not create a region for " + name + "@" + String.format("0x%08x", addr.getOffset()) + "+"
+ String.format("0x%08x", size) + ". It conflicts with an existing region!",
e);
} catch (LockException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AddressOverflowException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

private File getDtbFileFromDialog(JComponent parent) {
Expand Down
Loading

0 comments on commit 6a0f2da

Please sign in to comment.