Posts Tagged ‘copiar R5F212B7SNFP programa heximal de microcontrolador criptografado’

PostHeaderIcon R5F212B7SNFP: The Self-Programming Routine Is in the Image

Most R5F212B7SNFP MCU flash memory work is treated as a read problem: get the bytes out, hand them over, done. But the bytes, once out, answer a second question that is often more valuable than the copy itself. If the product can update its own firmware, the code that performs the update is inside the image, and reading it tells you exactly what happens to a field unit when the update is interrupted.

That question is usually asked in the wrong order. A customer sends in a dead unit and asks for the firmware. The honest sequence is to ask why the unit is dead, and the answer is frequently sitting in the image in the form of an erase loop that runs without a recovery path.

What self-programming means on this core

The device can erase and program its own flash under software control, but not while executing from flash, because the array is unavailable during the operation. The core solves this the only way it can: the routine that performs the erase is copied into RAM and executed there.

step where the code runs why
decide to update flash normal application code
copy the erase routine into RAM flash the routine must be somewhere else before the array goes away
enter the programming mode RAM requires disabling what would interrupt
erase the target block RAM the array is not readable
program the new content RAM same reason
leave the mode and reset RAM, then flash normal execution resumes at reset

That sequence leaves a very specific fingerprint in the image: a short block of instructions whose destination addresses are in RAM, followed by a jump to one of those addresses. It is the most reliable single indicator that a product has field-update capability, and it is visible without any understanding of the rest of the code.

Why a product would do this

Three reasons, and they look different in the code.

The first is a genuine firmware update path, usually triggered by a service interface or a bootloader handshake. The code for this is large, careful, and typically includes a checksum over the new image before committing it.

The second is a parameter store: calibration constants that the production line writes through the application rather than through a programmer. The code is small, it touches one or two blocks, and it has no checksum because the values are written once.

The third is a data logger that rotates through a region, erasing the oldest block when it needs space. This has no user-facing trigger at all and is often unknown to the customer until the flash wear shows up.

what the routine touches likely purpose what the customer will say
the whole array, with a checksum firmware update “it has a bootloader”
one or two blocks, no checksum production calibration “it was programmed at the factory”
a rotating region logging “it shouldn’t be writing anything”

The third row produces the most interesting conversations, because a customer who believes the device never writes to flash is looking at a device that has been writing to it every hour for a decade.

The signature in the disassembly

The copy-to-RAM block has a shape that is easy to recognise once you know to look for it. A source address inside the code region, a destination address in the RAM range, a length, and a tight loop that moves bytes or words between them. Then a computed jump, or a call through a register loaded with the RAM address.

The tell is the destination. Almost nothing else in a normal application copies code into RAM, so any loop that writes to the RAM range from a flash source is either the self-programming routine or the data for it.

The state machine around it

The routine itself is small. What surrounds it is the interesting part, because a correct implementation is a state machine and an incorrect one is a sequence.

A careful implementation marks the target block as being updated before erasing it, writes a marker that survives the erase elsewhere, and checks for that marker on the next boot. If the marker is present and the new content is incomplete, the device knows it was interrupted and can refuse to run rather than run half-updated code.

A careless implementation erases, then programs, and has no record that it was in the middle of anything. Its failure mode is the one described at the start: a unit that stops booting after a power cut, indistinguishable from a hardware failure.

element in the image careful implementation careless implementation
a marker written before the erase present, in a different block absent
a check for the marker at startup present, early in the startup path absent
validation of the new content checksum before commit none
behaviour on interruption refuses to run, stays in recovery runs whatever is there

The field failure it produces

The failure arrives as a unit that stopped working after a power cut, a brownout, or a service technician unplugging something at the wrong moment. The customer’s account is always the same: it worked, the power went away, it never came back.

From the outside this is indistinguishable from a hardware fault, which is why so many of these units are scrapped or sent for component-level repair before anyone considers that the firmware might be half-written. The diagnostic that separates the two costs nothing: read the block that the update path targets and check whether its content is uniform. A block that is partly erased and partly programmed is not a failed component.

The pattern is worth recognising because it changes the remedy. A half-written block is repaired by reflashing, and the reflash can be done through the same serial session that any normal unit accepts, provided the erase routine itself survived. When the erase routine lives in the block it was erasing, it does not survive, and the device has erased its own recovery path. That design exists, it is not rare, and it is the reason some of these units genuinely cannot be recovered in the field.

What this means for the recovery

Two practical consequences. The first is that the copy has to include the marker block, because without it the image cannot be interpreted correctly on a device that was mid-update. A file that omits the marker looks like a normal image with a corrupted code region.

The second is that a customer asking for the firmware of a dead unit should be shown this analysis before the file is delivered. If the routine is careless, the diagnosis is “interrupted update” and the remedy is a reflash, not a hardware repair. If it is careful, the device should have refused to run, and the fact that it does not run is consistent with the design rather than evidence of a fault.

What to report

The delivery states whether a self-programming routine was found, which blocks it can touch, and whether the surrounding logic protects against interruption. That last item is a design observation rather than a recovery result, and it is the one the customer will act on.

The related articles cover the surrounding mechanics: R5F212DASNFP flash data explains why the wear model differs between the regions this routine touches, and R5F21292SDSP locked MCU flash describes what a half-written block looks like when it is read back. For the analysis method that locates the routine in the first place, R5F21276KFP reverse engineering covers the vector-first approach to function boundaries.