Files
reverse-engineering-anything/bridge/ghidra/ReaGhidraPrepareCom.java
T
86cf8942de feat(ghidra)!: import recognized DOS COM files with ComLoader (#12086)
* feat(ghidra)!: import recognized DOS COM files with ComLoader

Select Ghidra 12.2 ComLoader for an admitted .com image and do not pass a
base address, so the file stays at 0000:0100. Images ComLoader rejects use
the explicit BinaryLoader import at 1000:0100 and record the typed reason.

BREAKING CHANGE: A DOS COM file that ComLoader accepts now loads at linear
0x100 instead of 0x10100. Read load_address. The BinaryLoader fallback
remains at 1000:0100.

* fix(ghidra): score COM dollar-strings in one pass

ComLoader's printable-run walk restarts at every offset, so a 64 KiB image
of printable bytes blocked profile resolution. The same capped count now
skips a run once an earlier start has seen every later dollar sign.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(ghidra): align COM import facts with the image Ghidra receives

Mismatch load-image coordinates follow the measured loader and address.
A dotfile named .com keeps that suffix on the snapshot, and selection
uses that snapshot name. The COM fallback verifier owns its MCP
connection before connect.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* style(ghidra): format COM snapshot and verifier files

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(ghidra): keep measured COM facts and required MZ coordinates

DOS MZ and a verified COM image keep load_segment and entry required.
A COM mismatch emits a known loader or load address on its own, and
omits the segment and entry only when that pair is not a known layout.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(ghidra): require the verified COM layout and document both entries

A verified COM image is either ComLoader at 0000:0100 or BinaryLoader at
1000:0100 with a fallback reason. The DOS guide queries 0x100 only for
the ComLoader entry and 0x10100 for the fallback.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(ghidra): attest COM imports against the committed loader

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-18 16:40:18 -05:00

78 lines
3.7 KiB
Java

import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.MemoryBlock;
import java.math.BigInteger;
/**
* Seed the declared DOS COM analysis context before default auto-analysis.
*
* ComLoader ("Old-style DOS Executable (COM)") maps a 256-byte placeholder at
* 0000:0000 and the file at 0000:0100. The explicit BinaryLoader fallback maps
* the file alone at 1000:0100. Both stay on x86:LE:16:Real Mode. Segment
* registers match the load segment so later instructions use that coordinate.
*/
public final class ReaGhidraPrepareCom extends GhidraScript {
private static final String COM_FORMAT = "Old-style DOS Executable (COM)";
private static final String RAW_FORMAT = "Raw Binary";
@Override
public void run() throws Exception {
if (!currentProgram.getLanguageID().getIdAsString().equals("x86:LE:16:Real Mode")) {
throw new IllegalArgumentException("DOS COM preparation requires x86 real-mode import");
}
String format = currentProgram.getExecutableFormat();
AddressSpace space = currentProgram.getAddressFactory().getDefaultAddressSpace();
if (format.equals(COM_FORMAT)) prepareComLoader(space);
else if (format.equals(RAW_FORMAT)) prepareBinaryLoader(space);
else {
throw new IllegalArgumentException(
"DOS COM preparation requires the ComLoader or raw BinaryLoader import"
);
}
currentProgram.getOptions(Program.PROGRAM_INFO).setBoolean("REA DOS COM prepared", true);
}
/** ComLoader's PSP placeholder and file image, with segment registers at 0. */
private void prepareComLoader(AddressSpace space) throws Exception {
Address psp = space.getAddress("0000:0000");
Address entry = space.getAddress("0000:0100");
MemoryBlock placeholder = currentProgram.getMemory().getBlock("PSP");
MemoryBlock code = currentProgram.getMemory().getBlock("CODE");
if (placeholder == null || code == null ||
!placeholder.getStart().equals(psp) || placeholder.getSize() != 0x100 ||
!code.getStart().equals(entry) || code.getSize() < 1 || code.getSize() > 0xff00 ||
!currentProgram.getMaxAddress().equals(code.getEnd())) {
throw new IllegalArgumentException(
"DOS COM ComLoader import must map a 256-byte PSP at 0000:0000 and 1..65280 code bytes at 0000:0100"
);
}
seed(entry, psp, code.getEnd(), BigInteger.ZERO);
}
/** Explicit raw fallback: file bytes only, at the fixed real-mode segment. */
private void prepareBinaryLoader(AddressSpace space) throws Exception {
Address entry = space.getAddress("1000:0100");
Address start = currentProgram.getMinAddress();
Address end = currentProgram.getMaxAddress();
if (!entry.equals(start) || end.subtract(start) + 1 > 0xff00) {
throw new IllegalArgumentException("DOS COM import must map 1..65280 bytes at 1000:0100");
}
seed(entry, start, end, BigInteger.valueOf(0x1000));
}
private void seed(Address entry, Address start, Address end, BigInteger segment) throws Exception {
for (String name : new String[] { "CS", "DS", "ES", "SS" }) {
monitor.checkCancelled();
currentProgram.getProgramContext().setValue(
currentProgram.getRegister(name), start, end, segment
);
}
addEntryPoint(entry);
if (!disassemble(entry) || createFunction(entry, "entry") == null) {
throw new IllegalStateException("DOS COM entry cannot be decoded as a function");
}
}
}