PostHeaderIcon R5F211A2SP: On a Small R8C, the Vector Table Eats the Image

A R5F211A2SP flash memory data recovery is often treated as the easy end of the work. Small device, small image, fewer blocks to lose, shorter session. That reputation is mostly deserved, but for a reason that is rarely stated: the smaller the device, the larger the share of its image is structural rather than functional, and structure is the part that can be reconstructed with certainty.

On a part with a few kilobytes of flash, the fixed vector table, the special-page vectors and the option bytes together occupy a fraction of the map that would be invisible on a device ten times the size. That overhead is documented, it is predictable, and it constrains everything that follows it. For a recovery, constraints are useful.

What “small” actually changes

Three things change as capacity shrinks, and only one of them is about size.

The first is granularity of loss. With a small array there are few blocks, so a single damaged block is a large proportion of the image. This is the change everybody notices and it is the least interesting, because a small image is also a small thing to reconstruct from a sibling unit.

The second is code density. Compilers targeting a small device are pushed toward size optimisation, which produces heavy inlining, shared exit paths and a lot of short functions. The disassembly looks dense and repetitive, and function boundaries are less obvious from the instruction stream alone.

The third is the ratio of structure to code. The vectors, the option bytes and the startup code are fixed costs that do not shrink with the array. On a small part they may be a tenth of the image rather than a hundredth, and that tenth is fully determined before a single byte of application code is written.

component roughly fixed or scales why it matters in a small image
low fixed vector table fixed its entries give you function addresses immediately
special-page vector block fixed it anchors the top of the map
option bytes fixed they fix the top of the map to a block boundary
startup and initialisation mostly fixed recognisable, and it locates the initial stack pointer
application code scales with capacity the only part that has to be interpreted

Why the vectors are worth more than their size

A vector table is the closest thing a firmware image has to a table of contents. Each entry is an address, and each address is a function entry point that the hardware calls at a defined moment. On a device where the application is dense and inlined, the vector list is the only reliable way to find where functions begin.

Reading the low vectors gives you a set of start addresses for the interrupt handlers, and the reset vector gives you the entry point. Reading the top block gives you the special-page handlers. Together they partition the code region into known-starting segments, which is exactly what a disassembler needs and exactly what the instruction stream does not provide.

Complete reconstruction is more reliable here

The counterintuitive conclusion is that a small image is easier to restore to a trustworthy state than a large one, because the parts that must be inferred are a smaller fraction of the whole.

On a large device, a damaged block in the middle of the application leaves a hole whose contents can only be guessed. On a small device, the same block may contain the entire application, and if it is lost the recovery has a different and clearer answer: the image cannot be reconstructed from the device, and a sibling unit or a production archive is required.

That is a better outcome than a partially guessed reconstruction, because it is honest. A file with a confidently wrong middle section is more dangerous than a file that says which block is missing, and the block-level reporting convention described in R5F21292SDSP locked MCU flash exists for exactly that reason.

The startup path is a fingerprint

Immediately after the reset vector there is a sequence that every image on this family shares in outline and differs in detail. It sets the stack pointer, clears or initialises the low RAM, configures the clock, and then calls whatever the application calls first.

That sequence is worth photographing from a known-good unit, because it is stable across builds from the same toolchain. Two devices from the same product line will have the same initialisation order, the same register writes in the same sequence, and the same constant in the stack pointer load. A device whose startup differs from the family is either a different product or a different compiler, and both are findings worth having before the recovery is planned.

detail in the startup path what it reveals
the immediate value loaded into the stack pointer the RAM size, and therefore the group
the order of peripheral register writes the toolchain’s startup library
the value written to the clock control register the intended operating frequency
whether a checksum loop runs over the code region whether the product validates itself

Where the space actually goes

A useful exercise before any session is to account for the map in advance.

region position recoverable by
low fixed vectors start of map reading the first block
startup code immediately after the vectors reading the first block
application between startup and the top block reading the remaining blocks
option bytes immediately below the top block reading the last block
top special-page vectors last block, upper portion reading the last block

With that layout in hand, the first and last blocks of the map carry a disproportionate amount of information. On a device where only a partial read is possible, those two blocks are the ones worth prioritising, because they establish the map for everything between them.

What a small device is usually doing

The application profile is predictable enough to be useful. A part with this little memory is doing one job: reading a sensor, driving a display segment, running a motor, acting as a protocol bridge. The firmware is a single loop with interrupt handlers, no operating system, no file system, and almost no dynamic allocation.

That profile tells you what to look for in the disassembly. There will be a main loop that polls, handlers that set flags, and a small number of peripheral initialisation sequences that appear once each. There will be no scheduler to confuse the call graph and no heap to make pointers untraceable.

It also tells you what the customer wants. Nobody recovers a device like this to study its architecture. They want the source-equivalent behaviour so they can repair units, or they want the calibration data so they can restore a batch. Both are served by a clean image plus a documented map, and neither is served by a binary alone.

The limits of what can be concluded

A complete image from a small device does not mean a complete understanding. Inlined code eliminates the call structure that the source had, shared exit paths merge functions that were separate, and size optimisation can make two source functions compile to one block of instructions with two entry points.

What the image does support is behavioural recovery: what the device does, in what order, with what constants. That is what repair and replacement require. It is not the same as the original source, and a delivery that says so is more useful than one that implies otherwise.

The related work on this family covers the other end of the range: R5F212A7SDFA flash program shows how the top of the map is established on a larger device, and R5F21336TNFP MCU flash program explains the one byte that sits just below the top block on every part in the group, large or small.

Comments are closed.