Ghidra Configuration - M16C Processor
This page covers setting up Ghidra to disassemble and decompile firmware for the Renesas M16C/60 series, in particular the M16C/62P (M30626FHPGP) in the Generation 4 Landis+Gyr meter. For getting the firmware off the chip see Extract Firmware - Landis+Gyr (M16C).
The Ghidra 11.0.x releases we use have no M16C support, so we use a separate processor module, m16c-62p (BitBangingBytes/m16c-62p). It is a fork of silverchris's m16c module, with fixes to the bit instructions by esaulenka, the M16C/62P special function registers (SFRs), and 3-byte pointers so the reset vector resolves. Newer Ghidra releases ship their own M16C/60 processor; it uses a different language ID, so programs made with one module do not open with the other. We have not compared the two.
Install
cd <ghidra_home>/Ghidra/Extensions/ git clone https://github.com/BitBangingBytes/m16c-62p.git
- Clone into
<ghidra_home>/Ghidra/Extensions/, not<ghidra_home>/Extensions/. - Start Ghidra. The language appears as Renesas M16C (
m16c:LE:16:default). Ghidra compilesm16c.slafromm16c.slaspecthe first time the language is used. - After pulling an update, rebuild and restart Ghidra; a running Ghidra does not reload a changed language:
cd <ghidra_home>/Ghidra/Extensions/m16c-62p/data/languages <ghidra_home>/support/sleigh m16c.slaspec m16c.sla
- Every Ghidra that opens a program made with this module needs the module installed, including the machines of anyone sharing a Ghidra server project with you.
- We use it with Ghidra 11.0.3.
Import
- Format: Raw Binary. Language: Renesas M16C,
m16c:LE:16:default. - Base address: the address the dump starts at. A 384 KB program-flash read of the M30626FHP starts at
0xA0000; a 512 KB read starts at0x80000(on this chip0x80000-0x9FFFFis not flash and reads as zeros). - Do not run auto-analysis yet. Set up the memory map and registers first.
Memory map
Add the blocks the dump does not contain (Window → Memory Map), as uninitialized and writable:
| Block | Start | End | Notes |
|---|---|---|---|
| SFR | 0x00000 |
0x003FF |
Special function registers. The module names them automatically (about 190 SFRs from the M16C/62P datasheet). |
| RAM | 0x00400 |
0x07FFF |
31 KB internal RAM of the M30626FHP |
| Data flash | 0x0F000 |
0x0FFFF |
Blocks A and B. Load a dump of them here if you have one. |
| Program flash | 0xA0000 |
0xFFFFF |
From the import |
Two address spaces over one memory. The module has a 16-bit near space, used by ordinary data accesses (RAM and SFR globals in most code), and a 20-bit ram space, used by far accesses and for flash. Put labels for RAM variables in near; flash addresses exist only in ram.
Vectors and entry points
- The fixed vector table is at
0xFFFDC-0xFFFFF: nine 4-byte entries (undefined instruction, overflow, BRK, address match, single step, watchdog, DBC, NMI, reset), each holding a 20-bit address in its low three bytes. Disassemble from the reset vector at0xFFFFCfirst, then the others. - The variable vector table (peripheral interrupts) is wherever the reset code points INTB with
LDINTB. Follow that instruction and disassemble each entry. - On the Landis+Gyr firmware the reset vector leads to the Landis+Gyr boot loader at
0xFC000-0xFFFFF, which checks the application image and calls into it at0xA0000+.
Registers
- SB (static base): many instructions address RAM as
dsp[SB]. Find theLDC #imm,SBin the reset code and set SB to that value over the code (select the range, then Set Register Values). In the Landis+Gyr application SB is0x580. Leave out any function that changes SB itself. - FB (frame base) is the per-function frame pointer. Do not give it a fixed value; doing so turns stack variables into bogus global references.
Known issues and tips
- Truncated functions: Ghidra sometimes marks a helper such as
memsetas non-returning, which cuts off the decompile of every caller. Clear the No Return flag on the helper and re-analyse the callers. - Decompiler crashes or timeouts: look for
CALL_RETURNflow overrides on jumps that stay inside the same function and clear them. - Spotting dump errors: a branch that lands in the middle of another instruction is a strong sign of a bad byte in the dump. That is how we found a two-byte read error in one of our images.
References
- m16c-62p processor module, based on silverchris/m16c.
- Renesas M16C/60, M16C/20, M16C/Tiny Series Software Manual (instruction set; included in the module's
data/manuals). - Renesas M16C/62P Group hardware manual (memory map, SFRs).