NEC/Renesas V850 - Read protected flash memory

Disassembly, Reassembly, Tools and devleopment. Going deep with Hardware and Software.
User avatar
pman92
Posts: 672
Joined: Thu May 03, 2012 12:50 pm
Location: Castlemaine, Vic

NEC/Renesas V850 - Read protected flash memory

Post by pman92 »

This is a bit of a run down of how I've been able to dump the read-protected flash memory of a V850ES/FG3 μPD70F3375. There's more than enough information here for anyone who is a similar situation to be able to adapt it to work, and assumes you already have some V850 knowledge (hardware manuals, Renesas flash programming software, etc. are easy to find online).
Getting to the final result took me days of work, many hours in the hardware manual, and plenty of failed attempts. It was initially triggered by a Reddit post, where someone mentioned being able to dump flash by uploading a UART dumper routine and new reset vector - as long as flash was only read (and not write) protected.
RFP_FlashOptions.png
Initially, before I even started on this project, I had to remove a lot of components from the PCB and provide my own 5v power supply to the MCU. The original voltage regulator was integrated with an external watchdog which kept resetting the MCU. In the end I lifted off every IC and transistor around the chip and hooked up my own power supply. The module I'm working on (FG Falcon BEM) is cheap and I bought a few of them assuming I would have to sacrifice a couple.
You'll also need to find the hardware manual for your specific version of the V850, find the pinout, and identify the FLMD0 / Reset / 5v / GND and UART connections.
Setup.png
The overall idea is we compile some code to loop over and read bytes from flash, then spit them out a UART. We then need to write that to an unused area of flash memory, and then overwrite with reset vector with NOP instructions until we find space to write a new vector pointing to our code. Luckily in V850 assembly, NOP is a 2 byte all zero instruction "0x00 0x00", and since flash memory can be programmed by flipping bits from 1 to 0 (but not the other way around), its possible to overwrite any existing data with NOPs.

My UART dumper for μPD70F3375 is attached:
UART Dumper.zip
It was compiled with V850 GCC (https://www.gnutoolchains.com/v850-elf/). I originally tried to use the Renesas CS+ IDE to generate me some setup code, but it didn't seem to support code generation for this older v850. It didn't even seem to have libraries that defined register addresses. So in the end I had to find all the registers in the hardware manual and configure them manually. It should be fairly trivial to port to other flavors of V850 chip - read up in the appropriate hardware manual for your chip. The functionality is very simple:

Code: Select all

void main(void) {
	volatile unsigned long i; // C89 requires all variables declared at start of function
	volatile unsigned char *flash_ptr;
	unsigned char sync_byte;
	
	/* HARDWARE SETUP */
	WDTM2 = 0x1F; 	// Disable watchdog
	
	PRCMD = 0x00; 	// Scratch the write protection register (to enable clock write)
	MCM = 0x00; 	// Use 8mhz internal oscillator (should default to this anyway) - much simpler to config 
	 
	PFCE9L |= 0x03;   // Select alternate function 3 for pins P90/P91 (TXDD1/RXDD1)
	PFC9L &= ~0x03;   
	PMC9L |= 0x03;    // Enable alternate mode 
	
	/* UART SETUP */
	UD1CTL1 = 0x03;   // UART Base clock: fXP1/8 = 1MHz (assuming 8MHz system clock and cleared PRSI)
	UD1CTL2 = 52;     // Baud rate divisor for 9600 baud (assuming PRSI in option byte 0x7B cleared, otherwise 4800 baud)
	UD1CTL0 = 0xC2;   // 11000010 - Power ON, TX enabled, 8-bit, 1 stop, no parity
	for(i=0; i<1000; i++) { }; // Small delay for UART to stabilize
	
	/* TRANSMIT 32 SYNC BYTES */
	sync_byte = 0xAA;  // Just incase I need to account for baud rate error
	
	for(i=0; i<32; i++) {
		while(UD1STR & 0x80); 	
		UD1TX = sync_byte;
	}
	
	/* DUMP FLASH */
	flash_ptr = (volatile unsigned char*)0x00000;
	
	for(i=0; i<=0x3FFFF; i++) {
		while(UD1STR & 0x80); // Wait while transmitting
		UD1TX = *flash_ptr; // Write byte to transmit
		flash_ptr++;
	}
	
	/* FOREVER LOOP */
	while(1);
    
};
The next step was uploading it. The Renesas flash programmer software is no use here, as it only accepts .hex files of a certain format (ie. starting at address 0x000000) and falls over completely on the first write error. We want to write our code to some arbitrary address, and we expect write errors when trying to overwrite the reset vector.

I spent quite some time sniffing the serial communication between the Renesas debugger and MCU. This is only UART and you can actually use any USB-UART adaptor with the flash programmer (you don't need the renesas debugger).
Packets of data seem to be transmitted in this format:
byte 0: 01 from debugger, 02 from MCU
byte 1: byte length of payload
byte 2 to n-3: <payload bytes>
byte n-1: 2's complement checksum
byte n: "03" (ascii end of text)

Here's the exchange from a "get flash options" using the Renesas flash programmer software:
FlashProgProtocol.png
To enter programming / boot mode, the debugger holds FLMD0 high and resets the MCU - this puts it in programing mode. It then send 2x "0x00" bytes to allow the MCU to sync baud rate with the debugger. And then sends commands as required.
The MCU generally responds to every command with an "06" (ascii acknowledge), and then responds again with data if required.

When attempting to write flash, we have this sequence of commands to start with:
FlashProgWriteFlash.png
With trial and error, I have confirmed the final "0x40" command is setting up the memory address range we are going to write (in this case 0x000000 to 0x03FFFF). I don't know what any of the other commands do.
The Renesas software sends 256 byte chunks of data to write after this, with 2 header bytes "02 00", and 2 footer bytes (checksum and 0x17 - ascii "end of transmission block"). The MCU responds "06 06" on success, or "06 xx" where xx is an error code (eg. 1C when writing data to flash that's already written and didn't verify correctly afterwards).

In the end I used an Arduino Mega to act as the programmer. My source is attached here, and rather than try and explain it its probably easier to look yourself:
Arduino.zip
It acts as a utility and does a few things automatically:
- Blank scans 256 byte blocks by writing all FF to them. If the MCU returns 06 06, the block must have already been blank. This is how I found blank areas of flash to write my dumper code.
- Writes the UART dumper code to the address you define (in 2 chunks, since its slightly larger than 256 bytes).
- Writes a "all zero" block to the address you define and returns result code (06 or 1C). I used this to confirm the reset vector writer was working.
- The big one: Writes the reset vector, starting at address 0x000000 and incrementing 1 NOP instruction at a time. Resetting the MCU and checking for UART output from the dumper after each write. Keeps going up to your maximum defined address, until it detects the reset vector worked and your code is running, at which point it stops.
You do not have the required permissions to view the files attached to this post.
User avatar
pman92
Posts: 672
Joined: Thu May 03, 2012 12:50 pm
Location: Castlemaine, Vic

Re: NEC/Renesas V850 - Read protected flash memory

Post by pman92 »

Using the arduino, I found blank flash at 0x7100 to 0x7FFF in my case:
BlankBlocks.png
After writing the dumper routine at 0x7200, they no longer tested blank. It wasn't until this point that I knew I was actually able to write flash:
NoLongerBlank.png
I had to wipe out the first 0x986 bytes of flash before I found a location my reset vector worked:
ResetVectorSuccess.png
And I've now got a constant stream of bytes spilling out my UART when I reset it. So it's just a matter of capturing that data and converting back to a .bin file.

Obviously this isn't a perfect flash image. The original reset vector and start of flash ends up overwritten and zeroed out. But hopefully it is enough for whatever you are aiming to do (maybe even finding out how to do a proper flash read). And by sharing this hopefully I give someone else a starting point and save them hours or days of work.
You do not have the required permissions to view the files attached to this post.
User avatar
pman92
Posts: 672
Joined: Thu May 03, 2012 12:50 pm
Location: Castlemaine, Vic

Re: NEC/Renesas V850 - Read protected flash memory

Post by pman92 »

I've got my dump off and into ghidra. It turns out the UART was transmitting MSB first (instead of LSB first) so I had to swap around every byte.

This is probably due to mis-configuration somewhere along the line caused by my reset vector not actually working how I originally intended. It got mangled by data already programmed, and wasn't exactly as I had designed (a straight jump to my code). However the end result was the MCU reached my code eventually, and still dumped the whole flash and then sat there in a infinite loop doing nothing. So it worked, I just had to swap MSB to LSB before loading into ghidra.
User avatar
antus
Site Admin
Posts: 10111
Joined: Sat Feb 28, 2009 10:34 am
cars: TX Gemini 2L Twincam 8psi
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Subaru WRX 2007

Re: NEC/Renesas V850 - Read protected flash memory

Post by antus »

Wow great write up, thanks for shareing!

I wonder if you could move your dumper slightly and get another dump from another module with a different part missing and use the two images to created a complete bin.
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
User avatar
pman92
Posts: 672
Joined: Thu May 03, 2012 12:50 pm
Location: Castlemaine, Vic

Re: NEC/Renesas V850 - Read protected flash memory

Post by pman92 »

antus wrote: Sun Jan 18, 2026 5:40 am Wow great write up, thanks for shareing!

I wonder if you could move your dumper slightly and get another dump from another module with a different part missing and use the two images to created a complete bin.
I could dump another one to get some of the data I initially overwrote or corrupted.
I can confirm now that I have a dump that 0x7100 to 0x7FFF was blank. Everything up to around 0x7100 looks like a bootloader, and the main program has an entry point of 0x8000.
But I would still have to over write the original reset vector and start of flash. So unless I find the parts I'm interested in are damaged I'll keep chipping along with what I've got for now.

Edit: actually, now I've got a copy of most of it. I probably could hook in somewhere else other than the reset vector. And create a complete bin from the 2 of them.
User avatar
Gatecrasher
Posts: 435
Joined: Fri Apr 24, 2020 8:09 pm

Re: NEC/Renesas V850 - Read protected flash memory

Post by Gatecrasher »

That is awesome. Nice work.