PostHeaderIcon ST72F32AJ1 Flash Memory Binary: The Map Grows Downward, Not Upward

Every engineer who has worked on a modern microcontroller carries an assumption that is never written down: the program starts at the bottom of the map and grows upward, and the space above it is either empty or holds something exotic. On this family the assumption is exactly backwards, and a ST72F32AJ1 flash memory binary produced under it will be misaligned in a way that is invisible until someone tries to load it.

The flash on an ST7 ends at the top of the address space. The interrupt vector table occupies the last thirty-two bytes, the reset vector occupies the last two, and the application fills downward from just below them. Capacity is not a range that grows from zero. It is a distance measured down from 0xFFFF.

The map ends at 0xFFFF and grows down

The reason for the arrangement is the vector table. Interrupt entries have to be at fixed addresses, because the hardware has no other way to find them. Fixing them at the top means the table is at the same address on every member of the family, from the smallest part to the largest, and the only thing that changes between them is how far down the application region reaches.

capacity first flash address last flash address distance from 0xFFFF
8 KB 0xE000 0xFFFF 0x2000
16 KB 0xC000 0xFFFF 0x4000
32 KB 0x8000 0xFFFF 0x8000
60 KB 0x1000 0xFFFF 0xF000

Three consequences follow immediately, and each one contradicts a habit.

The first is that a read which starts at address zero and proceeds upward produces, at its start, a long run of bytes that are not part of the device at all. Whatever the interface returns for unmapped addresses — zeros, FF, or repeated patterns — is not firmware.

The second is that the vector table is always at the end of the file, not at the beginning. A disassembler that assumes a vector table in the first bytes will interpret application code as vectors and produce a call graph pointing at nonsense.

The third is that capacity can be measured. The first address that contains real code is the start of flash, and the distance from there to 0xFFFF is the capacity. No datasheet lookup is required, and no part number decoding.

Reading the start address as a capacity measurement

The measurement is worth doing on every device, even when the part number is unambiguous, because it is also a check on whether the read covered the whole array.

The technique is to walk down from the top in block steps and look for the first region that is not uniformly erased. Under a top-aligned map, that region is either the beginning of the code or the beginning of a constant table placed at the low end, and both are informative: a constant table at the very bottom of a device that should have code there means the linker used a different memory model than expected.

observation interpretation
first non-erased address equals the expected start for the nominal capacity the read is complete and the part matches its marking
first non-erased address is lower than expected either the part is larger than marked, or erase left residue below the code
first non-erased address is higher than expected the read stopped early, or the linker placed code in the upper region only
a uniform erased run interrupts the code region a block was erased after programming; look for an interrupted update

The second row deserves a warning. Residue below the nominal start address is common on parts that have been reprogrammed, because erasing a larger device and using only part of it leaves the earlier contents’ shadow in the lower blocks. Treating residue as code produces an image that starts with an unrelated program.

What a binary file loses, and why that matters

A raw binary is the most convenient format to hand over and the least self-describing. It contains bytes and nothing else: no address is recorded, no checksum is attached, and no indication of where in the map the first byte belongs.

That is fine when the recipient knows the base. It is catastrophic when they do not, because loading a top-aligned image at address zero shifts every absolute address by the size of the map. Every jump target, every table pointer and every vector entry will be wrong by a constant, and the resulting disassembly will look almost plausible — the offsets between things are preserved — which is the worst kind of wrong.

format records addresses self-verifying risk when the base is assumed
raw binary no no total misalignment, undetectable
Intel HEX yes per-record checksum low, the records carry the truth
Motorola S19 yes per-record checksum low, plus a documented start address
binary with a sidecar map no, but documented depends on the sidecar low if the sidecar travels with the file

The fourth row is the pragmatic compromise, and it is what a delivery should contain even when the primary file is raw: a short text file stating the base address, the length, the checksum and the vector table contents.

Loading the image back for analysis

With the base known, the load is straightforward, and the first validation is the vector table. Reading the sixteen words at the top of the file gives sixteen entry points. Every one of them should land inside the application region, and the reset vector should land on code that begins with a startup sequence — stack pointer setup, RAM initialisation, peripheral configuration — rather than in the middle of an instruction.

A vector entry that lands outside the application region is a real finding. It means the image is incomplete, or the base is wrong, or the part is not the capacity it was assumed to be. All three are worth knowing before the disassembly is attempted.

This is where the top-aligned map becomes an analytical asset rather than an obstacle. The vector table is at a known location on every part in the family, so the first thirty-two bytes you read are always the same kind of thing, and they always tell you whether the rest is consistent.

The residue below the start

A device that has been reprogrammed more than once often shows non-erased bytes below the nominal first address of the current program. This is not a mapping error and it is not a failed read; it is the shadow of an earlier program on a device that was erased as a whole but written only in part.

The tell is that the residue decodes as a coherent program in its own right, with its own vector table pointing into the same region. Two coherent programs in one image means two generations of firmware, and the boundary between them is the boundary between the old build and the new one.

That boundary is worth locating before the analysis begins, because it tells you which part of the image is current. A customer who receives a disassembly of the residue rather than the live code will not recognise their own product, and the report that follows will be about the wrong program.

pattern at the boundary likely history
clean cut, old code below, new code above the device was programmed twice with different link widths
interleaved old and new blocks a partial update was applied, then abandoned
uniform erased region between generations a full erase separated the two
no residue at all the device has been programmed once

Three offsets that must be recorded

A ST72F32AJ1 flash memory binary delivery that is going to be used by someone else needs three numbers written down: the base address where the first byte belongs, the length of the image, and the address of the vector table. With those three, any tool can reconstruct the mapping without knowing anything about the part.

Without them, the recipient has to guess, and guessing produces the failure described above. The cost of recording three numbers is a few lines in a text file; the cost of not recording them is a day of confusion at the other end, usually followed by an email asking whether the read was good.

What the delivery includes

Alongside the binary, the useful package contains the base address and length, the raw vector table with the reset vector called out, a block-level read status, and a note on whether the region below the nominal start contained residue.

The comparison with a bottom-aligned family is instructive here. On an R8C the same measurement works in the opposite direction, because there the top holds a vector block that moves with capacity; the technique is described in R5F212A7SDFA flash program. Two families, two directions, one principle: the map is a measurement, not an assumption. The code-versus-data question that follows a successful load is covered in ST72F325J6 program code extraction, and the length-mismatch argument that customers raise most often is worked through in ST72F325S6 embedded flash binary file.

Comments are closed.