mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
* feat(firmware): propose CPU, endianness, and load-base candidates for firmware blobs * fix(firmware): preserve MCP caller inputs, x32 decoder width, and bounded ELF tallies * fix(firmware): leave M-profile variant and PPC width unresolved and report observed file size * chore(firmware): remove unused exported types flagged by knip * fix(firmware): recognize x86_64 leave;ret functions, keep uImage ppc width unknown, and preserve MIPS ELF flags * fix(firmware): preserve MIPS/ARM decoder uncertainty and partial scan stages * fix(firmware): preserve MIPS16 and microMIPS ELF ASE mode declarations * fix(firmware): admit Cortex-M vector tables with filled reserved slots The Raspberry Pi Pico SDK fills reserved vector slots with its default handler. The detector required them to be zero, so it missed the vector table and load base of the MicroPython 1.24.1 RP2040 image and offered only an instruction-census guess. Reserved slots may now hold a Thumb handler in the Reset region; any other value still rejects the table. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> * feat(firmware): identify ESP-IDF images from their header chip_id The detectors found no candidate for the MicroPython 1.24.1 ESP32 image. They have no Xtensa signature, and the chip is named in the image header. A new esp-image-header detector recognizes the ESP-IDF header at offset 0 or a ROM bootloader offset, and takes the ISA from the existing ESP-IDF chip table. The chip_id field now has one reader shared with the ESP image parser. ESP32 images report Xtensa and ESP32-C3 images RISC-V. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --------- Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com> Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
355 lines
20 KiB
Markdown
355 lines
20 KiB
Markdown
# Headerless raw-binary analysis with Ghidra
|
|
|
|
Headerless images are covered here. GameCube/Wii DOL images have a header and
|
|
use their own format selection; see [GameCube DOL analysis](../ghidra-dol.md).
|
|
|
|
REA admits a headerless executable when the caller supplies the CPU family and
|
|
the address where file offset 0 should be mapped. That interpretation is not
|
|
inferred from a `.bin` suffix or from unrecognized bytes. ARM M-profile images
|
|
can also name their profile and take their entry from the vector table; see
|
|
[ARM M-profile images](#arm-m-profile-images).
|
|
|
|
## Open and inspect
|
|
|
|
```bash
|
|
rea inspect /absolute/path/to/kernel.img --target-format raw-binary --architecture arm --entry-point 0x8000 --provider ghidra --json
|
|
rea function /absolute/path/to/kernel.img 0x8000 --target-format raw-binary --architecture arm --entry-point 0x8000 --provider ghidra --json
|
|
rea inspect-native-load-image /absolute/path/to/kernel.img --target-format raw-binary --architecture arm --entry-point 0x8000 --provider ghidra --json
|
|
```
|
|
|
|
```json
|
|
{
|
|
"name": "open_binary",
|
|
"arguments": {
|
|
"path": "/absolute/path/to/kernel.img",
|
|
"format": "raw-binary",
|
|
"architecture": "arm",
|
|
"entry_point": "0x8000",
|
|
"provider_id": "ghidra"
|
|
}
|
|
}
|
|
```
|
|
|
|
The example maps a 32-bit ARM-state image at `0x8000`. For `arm`, decoding
|
|
starts at the entry in ARM (A32) state, so Thumb-only images, including Cortex-M
|
|
firmware, are not supported. See [Limitations](#limitations).
|
|
|
|
`architecture` is one of `x86`, `x86_64`, `arm`, `arm64`, `6502`, `m68k`, or `8051`. `entry_point` is a
|
|
hexadecimal address of the form `0x…`. The complete file is mapped starting at
|
|
that address, and Ghidra seeds an `entry` function there before auto-analysis.
|
|
ARM and AArch64 entries must be 4-byte aligned; m68k entries must be 2-byte
|
|
aligned; x86, 6502 and 8051 entries may be byte-aligned.
|
|
For 6502 and 8051, the entry and complete mapped file must fit within `0x0000..0xffff`;
|
|
images that cross the 16-bit boundary are rejected before provider startup.
|
|
For m68k, the mapping must fit within `0x0..0xffffffff`.
|
|
`open_binary` and an open `binary_session` return the canonical `entry_point`
|
|
for raw targets so callers can verify the selected mapping directly.
|
|
Omitted `format` keeps header-based detection; architecture and entry without
|
|
`format=raw-binary` are rejected.
|
|
|
|
Ghidra imports a private snapshot with `BinaryLoader` and these generic
|
|
languages and compiler specifications. Each family has one fixed byte order:
|
|
m68k and 8051 are big-endian and the others are little-endian.
|
|
|
|
| architecture | language_id | compiler_spec_id |
|
|
| ------------ | --------------------- | ---------------- |
|
|
| x86 | `x86:LE:32:default` | `gcc` |
|
|
| x86_64 | `x86:LE:64:default` | `gcc` |
|
|
| arm | `ARM:LE:32:v7` | `default` |
|
|
| arm64 | `AARCH64:LE:64:v8A` | `default` |
|
|
| 6502 | `6502:LE:16:default` | `default` |
|
|
| m68k | `68000:BE:32:default` | `default` |
|
|
| 8051 | `8051:BE:16:default` | `default` |
|
|
|
|
Compiler specifications select analysis conventions; they do not identify the
|
|
original compiler or ABI of a headerless target. Ghidra's x86 languages do not
|
|
provide a compiler specification named `default`.
|
|
|
|
Those settings, the caller-supplied entry, and `language_selection:
|
|
caller-supplied` are committed in the analysis profile. A different entry
|
|
or architecture is a different profile; snapshots from header-detected targets
|
|
do not replay.
|
|
|
|
The mapped start is independent of Ghidra's program image base, which remains
|
|
`0x0` for BinaryLoader. Ghidra may report additional language-defined entry
|
|
points, including unmapped ARM vectors. Load-image verification requires the
|
|
caller-supplied entry and preserves those other entries as observations.
|
|
For 6502, BinaryLoader maps the file before adding its language-defined zero-page
|
|
and stack blocks. Conflicting default blocks are skipped; low-address files stay
|
|
in the default address space rather than an overlay. Non-conflicting uninitialized
|
|
blocks remain observations, not file-backed bytes.
|
|
Independent raw verification streams the complete private snapshot digest and
|
|
extent with bounded memory and supports cancellation during the read.
|
|
It also requires the source-backed block to be initialized, loaded, and
|
|
non-overlay in the bound language's default address space. Contradictory mapping
|
|
observations are retained with `mismatch`, even when the source hashes agree.
|
|
|
|
Hopper and IDA refuse raw-binary targets. Windows Ghidra P0 admits native PE
|
|
applications/DLLs and explicitly selected raw 6502 images. Other raw CPU families
|
|
remain outside the Windows boundary. Native path admission, private DACLs, Job
|
|
Object ownership, immutable snapshots, and the no-mutation policy still apply.
|
|
|
|
## ARM M-profile images
|
|
|
|
Cortex-M flash images start with a vector table and run only Thumb code. A dump
|
|
read from a device, for example with `st-flash read dump.bin 0x08000000 <size>`,
|
|
records neither the address it was read from nor the M profile of the core.
|
|
Name both:
|
|
|
|
```bash
|
|
rea inspect /absolute/path/to/dump.bin --target-format raw-binary --architecture arm --architecture-variant armv7-m --base-address 0x08000000 --provider ghidra --json
|
|
```
|
|
|
|
```json
|
|
{
|
|
"name": "open_binary",
|
|
"arguments": {
|
|
"path": "/absolute/path/to/dump.bin",
|
|
"format": "raw-binary",
|
|
"architecture": "arm",
|
|
"architecture_variant": "armv7-m",
|
|
"base_address": "0x08000000",
|
|
"provider_id": "ghidra"
|
|
}
|
|
}
|
|
```
|
|
|
|
`architecture_variant` names the M profile and selects the Ghidra decoder:
|
|
|
|
| architecture_variant | Profile | language_id | compiler_spec_id |
|
|
| -------------------- | ------------------ | ------------------ | ---------------- |
|
|
| `armv6-m` | ARMv6-M | `ARM:LE:32:Cortex` | `default` |
|
|
| `armv7-m` | ARMv7-M | `ARM:LE:32:Cortex` | `default` |
|
|
| `armv7e-m` | ARMv7E-M | `ARM:LE:32:Cortex` | `default` |
|
|
| `armv8-m.base` | ARMv8-M Baseline | `ARM:LE:32:v8-m` | `default` |
|
|
| `armv8-m.main` | ARMv8-M Mainline | `ARM:LE:32:v8-m` | `default` |
|
|
| `armv8.1-m.main` | ARMv8.1-M Mainline | `ARM:LE:32:v8-m` | `default` |
|
|
|
|
`ARM:LE:32:Cortex` is Ghidra's ARMv7 decoder with Thumb as the default state. It
|
|
has no ARMv8-M encodings, such as the `MSPLIM` and `PSPLIM` system registers that
|
|
v8-M startup code sets, so the v8-M and v8.1-M variants use `ARM:LE:32:v8-m`.
|
|
Ghidra 12.2's `ARM.ldefs` lists `armv8.1-m.main` for that language, which also
|
|
decodes ARMv8.1-M encodings such as MVE and the low-overhead loops (`wls`, `le`);
|
|
the loop branches' flow is wrong, as Limitations describes.
|
|
Neither decoder refuses instructions that a smaller profile lacks: `armv6-m`
|
|
code is decoded with the ARMv7-M instruction set, and `armv8-m.main` code with
|
|
the ARMv8.1-M one.
|
|
|
|
- `base_address` maps file offset 0 at its own address, and `entry_point` must
|
|
then lie inside the mapped image. It is required with `architecture_variant`
|
|
and refused without it, the one lane whose verifier covers a separate base. A
|
|
dump records neither its load address nor where Reset is, and mapping it at
|
|
the entry would put a vector table at Reset; a code fragment that starts at
|
|
its entry passes that address as `base_address`.
|
|
- Without `entry_point`, REA reads the vector table at `vector_table_address`, or
|
|
at `base_address` when that is omitted. Word 1 is the Reset handler with bit 0
|
|
set for Thumb state. The vector value is observed, and the entry is derived by
|
|
clearing bit 0. Word 0, the initial stack pointer, is recorded as observed and
|
|
is not validated.
|
|
- Use `vector_table_address` when a header precedes the vector table, such as the
|
|
256-byte boot2 stage of an RP2040 image or an MCUboot header. It applies only
|
|
when `entry_point` is omitted. The table, whether named or at the base, must be
|
|
128-byte aligned, as VTOR requires (`TBLOFF` holds address bits 31:7), and it
|
|
must lie inside the mapped image.
|
|
- REA refuses a Reset word that is even, that points outside the mapped image or
|
|
into the 8 table bytes it read, or that lies past the end of the file. These
|
|
are `load_selection_rejected` refusals with the observed words in
|
|
`details.diagnostics`: a raw dump has no format they can violate, and the usual
|
|
cause is a wrong `base_address`, `vector_table_address` or variant.
|
|
- An explicit `entry_point` must be even. An odd value is a Thumb interworking
|
|
address, such as a word copied from the vector table. REA refuses it instead
|
|
of clearing bit 0; `details.diagnostics` in the error give the even
|
|
`code_address` and suggest omitting `entry_point`.
|
|
- Each refusal of a load selection is an `invalid_request` with
|
|
`details.constraint: "load_selection_rejected"` and, when one input is to
|
|
blame, that input in `details.field`, such as `entry_point` for an odd Thumb
|
|
entry. The Reset-word refusals name none.
|
|
|
|
Before auto-analysis, REA's preparation script requires Ghidra's Thumb context
|
|
(`TMode=1`) at the entry and seeds the entry there; `inspect_native_load_image`
|
|
attests that context independently. Ghidra imports the file without its
|
|
processor-defined labels. `ARMCortex.pspec` names the vector table at
|
|
`0x0`-`0x40` and marks every handler slot an entry, reserved slots included, so
|
|
at base `0x0` its names would cover the table and the code after it. The load
|
|
image attests that the seeded entry is the only one.
|
|
|
|
`npm run verify:ghidra:cortex-m` checks this lane against real Ghidra 12.2
|
|
with source-owned fixtures and LLVM as the decoding oracle; see
|
|
[testing lanes](../testing-lanes.md).
|
|
|
|
### Identity and Evidence
|
|
|
|
A load whose base is its caller-selected entry and that names no variant is the
|
|
same load as before these inputs existed, decided on these resolved values. Its
|
|
analysis profile, snapshot target, session reuse and `raw-binary` load image are
|
|
unchanged. Any other raw load:
|
|
|
|
- reports `base_address`, `architecture_variant` and, when Reset was read from
|
|
the vector table, `vector_table_address` in `open_binary` and
|
|
`binary_session`;
|
|
- commits `raw_load` (`version: 1`) to its analysis profile and snapshot target:
|
|
the base, the variant, the vector table that Reset was read from, and the
|
|
entry's address, instruction set, `role` and `basis`;
|
|
- returns the `segments` load image (`layout: "segments"`, `version: 1`): one
|
|
segment backed by the whole file at the base, one entry with its `isa_mode`,
|
|
`role` and `basis`, and, when the basis is the vector table, `vector_table`
|
|
with the table address and the two words read from it, as full-width 32-bit
|
|
words such as `0x080001c5`; the entry holds the canonical code address.
|
|
|
|
An entry's `role` names its function: `entry` for an entry the caller named,
|
|
`reset` for the Reset handler. Its `basis` records how REA chose it: `caller`
|
|
or `vector-table`. Ghidra names the function it seeds there `entry` whatever
|
|
the role, as its loaders name a program entry; the role, not that name, says
|
|
which handler it is.
|
|
|
|
Identity has two levels:
|
|
|
|
- Coordinate identity (`function_coordinate_context`) covers only resolved
|
|
values that move coordinates: the decoder, the base, and the entry's decode
|
|
context. Every entry of one image at one base gets the same coordinate
|
|
context, so functions found from a caller-named entry and from Reset compare.
|
|
A legacy load maps file offset 0 at its entry, so there the entry address is
|
|
the base.
|
|
- Session and snapshot identity also cover the provenance that results report:
|
|
`raw_load` adds the variant, the vector-table address, and the entry's role
|
|
and basis. The vector words need no place of their own: they are the file
|
|
bytes at the table address, which the file digest and the base fix. A reused
|
|
session never reports a basis or vector words that the current request did
|
|
not produce.
|
|
|
|
Neither level covers spellings: `0x08000000` and `0x8000000` are the same base,
|
|
an omitted `vector_table_address` equals an explicit one at the base, and REA
|
|
reports canonical addresses only. No result retains the spelling a caller used
|
|
yet. A caller that needs its spelling keeps its own request: an MCP client
|
|
already holds its arguments, and a CLI caller can save its command beside the
|
|
Evidence it writes, together with its `evidence_id`.
|
|
|
|
These inputs leave a load without them exactly as REA loaded it before: its
|
|
analysis profile inputs, snapshot target and `raw-binary` load image are
|
|
unchanged, so its saved load images stay readable. Snapshots still follow the
|
|
[producer rule](ghidra-provider.md#admitted-function-analysis-semantics): every
|
|
analysis profile commits to the REA release and its packaged bridge scripts, so
|
|
a snapshot that an earlier REA release saved is refused after an upgrade, with
|
|
or without these inputs. Open the target again and save a fresh snapshot. A
|
|
snapshot of a load that uses these inputs also has a different analysis profile,
|
|
so REA versions without these inputs cannot replay it.
|
|
|
|
## NMOS 6502
|
|
|
|
```bash
|
|
rea function /absolute/path/to/image.bin 0x8001 --target-format raw-binary --architecture 6502 --entry-point 0x8001 --provider ghidra --json
|
|
rea inspect-native-load-image /absolute/path/to/image.bin --target-format raw-binary --architecture 6502 --entry-point 0x8001 --provider ghidra --json
|
|
```
|
|
|
|
Select only headerless NMOS 6502 code with a known entry at file offset 0.
|
|
The built-in Ghidra processor supplies static instructions, functions,
|
|
pseudocode, and references. Evidence preserves `architecture: "6502"`, the
|
|
selected language, entry, file digest, and verified source mapping. Register and
|
|
calling conventions are analysis conventions, not a recovered source ABI.
|
|
|
|
This lane does not interpret iNES headers, cartridge mappers, bank switching,
|
|
or other console-specific memory layouts. 65C02 and 65816 programs need different
|
|
processor semantics and are not admitted as those variants. REA does not infer
|
|
the CPU from the file suffix, guess the reset vector, or emulate the target.
|
|
|
|
`npm run verify:ghidra:6502` assembles and links the source-owned fixture with
|
|
`ca65` and `ld65` (tested from cc65 tag `V2.19`, whose tools report `V2.18`).
|
|
Set `REA_CA65` and `REA_LD65` to explicit executable paths if needed. The lane
|
|
checks real CLI/MCP load-image parity, mapped bytes, decoded calls, decompilation,
|
|
invalid mappings, unchanged source bytes and runtime cleanup.
|
|
Low-memory checks include a file spanning the zero-page/stack boundary at `0xff`
|
|
and a complete 64-KiB image at `0x0`, with verified default-space mappings through
|
|
both CLI and MCP. On Windows, the lane also checks that a caller-selected
|
|
junction path is rejected at snapshot admission through CLI and MCP, with
|
|
unchanged source bytes and complete runtime cleanup. Windows requires the
|
|
matching native addon; the POSIX provider path has not been exercised by the
|
|
Windows run.
|
|
|
|
## Motorola 68000
|
|
|
|
```bash
|
|
rea function /absolute/path/to/image.bin 0x1000 --target-format raw-binary --architecture m68k --entry-point 0x1000 --provider ghidra --json
|
|
rea inspect-native-load-image /absolute/path/to/image.bin --target-format raw-binary --architecture m68k --entry-point 0x1000 --provider ghidra --json
|
|
```
|
|
|
|
Select only headerless big-endian 68000-family code with a known, even entry at
|
|
file offset 0. Ghidra's `68000:BE:32:default` language decodes the 68040
|
|
superset in the 32-bit `ram` address space. Instructions that a real MC68000
|
|
would not execute are therefore not rejected, and 68020, 68030, CPU32, and
|
|
ColdFire programs are not admitted as those variants. Addresses are not masked
|
|
to the MC68000 24-bit bus. Evidence preserves `architecture: "m68k"`, the
|
|
selected language, entry, file digest, and verified source mapping. Register and
|
|
calling conventions are analysis conventions, not a recovered source ABI.
|
|
|
|
This lane does not interpret Amiga hunks, Atari ST, or other container headers,
|
|
cartridge or console memory maps, or bank switching. REA does not read the
|
|
initial stack pointer and reset vector from the first eight bytes; select the
|
|
entry explicitly. Windows Ghidra P0 does not admit m68k.
|
|
|
|
`npm run verify:ghidra:m68k` uses a source-owned 22-byte fixture written as
|
|
annotated hex in the verifier, so it needs no assembler. The lane checks real
|
|
CLI/MCP load-image parity, mapped bytes, a decoded call, decompilation by address
|
|
and file offset, misaligned and out-of-range mappings, unchanged source bytes and
|
|
runtime cleanup. It passed on Ghidra 12.2 with JDK 25 on macOS arm64; Linux has
|
|
not been exercised.
|
|
|
|
## Intel 8051 (MCS-51)
|
|
|
|
```bash
|
|
rea inspect /absolute/path/to/bank03.bin --target-format raw-binary --architecture 8051 --entry-point 0x0 --provider ghidra --json
|
|
rea decompile /absolute/path/to/bank03.bin 0x1000 --target-format raw-binary --architecture 8051 --entry-point 0x0 --provider ghidra --json
|
|
```
|
|
|
|
Select headerless classic 8051 code mapped into the 16-bit `CODE` space.
|
|
Ghidra's `8051:BE:16:default` language places the file in `CODE` and adds its
|
|
processor-defined `REG_BANK_1`…`REG_BANK_4`, `INTMEM` and `SFR` blocks, plus
|
|
bit-mapped `BITS` and `SFR-BITS` blocks, as uninitialized observations. No
|
|
external RAM block is created: references to external data appear as `EXTMEM`
|
|
addresses, but their bytes are not readable. The 80251, 80390, MX51 and CIP-51
|
|
variants are not selectable.
|
|
Evidence preserves `architecture: "8051"`, the selected language, entry, file
|
|
digest and verified source mapping.
|
|
|
|
The lane loads one image of at most 64 KiB, mapped from the entry address.
|
|
REA does not model a bank register, overlay spaces or cross-bank calls, so a
|
|
banked image is opened one bank at a time after splitting it into bank-sized
|
|
files. When each bank is a complete 64 KiB code image, open it at `0x0`. When
|
|
banks share a common area and switch only a window (for example 32 KiB pages
|
|
at `0x8000`), open the bank file at the window address; the common area is a
|
|
separate target. The entry byte must decode as an instruction, since the lane
|
|
seeds an `entry` function there. Auto-analysis starts at that entry only, so
|
|
code reached through bank-switch thunks or Keil C51 `?C?CCASE` switch tables
|
|
is not discovered. Supply known entries through `REA_GHIDRA_SEED_FILE`.
|
|
Windows Ghidra P0 does not admit 8051.
|
|
|
|
There is no source-owned `verify:ghidra:8051` lane yet.
|
|
|
|
## Limitations
|
|
|
|
- Headers are not discovered from file bytes. REA does not guess load
|
|
addresses, profiles, vector-table locations, or memory maps during
|
|
`open_binary`; use
|
|
[`inspect_firmware_architecture_candidates`](../guides/firmware/firmware-architecture-candidates.md)
|
|
to inspect ranked CPU, endianness, and Cortex-M base-address candidates
|
|
offline first.
|
|
- Big-endian ARM is unsupported; m68k and 8051 are the big-endian families.
|
|
`architecture_variant` names only the ARM M profiles; no other processor of a
|
|
family, such as the 65C02 or ColdFire, can be selected.
|
|
- An M-profile load maps one segment: the file at its base. SRAM, peripheral and
|
|
system blocks are not created, and only the Reset vector is read. Ghidra's
|
|
processor-defined vector labels are not applied, so other vectors are neither
|
|
entries nor labels; use
|
|
[`inspect_firmware_vector_table`](../guides/firmware/firmware-vector-tables.md)
|
|
to decode the full vector table and optional CMSIS-SVD IRQ names offline.
|
|
Floating-point state, TrustZone and device memory maps are not modelled.
|
|
- Ghidra 12.2, like 12.1.4, decodes the ARMv8.1-M low-overhead-loop branches
|
|
`wls` and `le` with the right mnemonics, but not their flow. In the verifier's Cortex-M55
|
|
image, `wls` branched 2 bytes short of its target, `le` branched 4 bytes past
|
|
the loop start, and `le` had no fall-through. Code after such a loop may be
|
|
left undisassembled; `armv8.1-m.main` results say so.
|
|
- Reset vectors, memory-mapped I/O, and firmware boot sequences are not
|
|
emulated.
|
|
- Independent load-image verification covers source identity, mapped file
|
|
bytes, the bound language, and the caller-supplied entry. Original bytes
|
|
stay unchanged.
|