Posts Tagged ‘read arm mcu ic source code’

PostHeaderIcon Read ARM Cortex-M Firmware: Two Words Validate the Whole Image

A Cortex-M image has a property that most other architectures lack: its first two words are self-describing and mutually constraining. The first word is the initial value of the stack pointer, and the second is the reset vector. Both are addresses, and both must fall in plausible regions for the image to be valid.

This gives a free integrity check on any extraction. It costs two reads of memory and it catches offset errors, base address errors, byte-swapped reads, and images that are not code at all.

What the two words mean

Word Content Plausibility test
0 Initial stack pointer Points near the top of a RAM region, aligned
1 Reset vector Points into the code region, with the Thumb bit set
2 onward Exception vectors Each points into the code region

The second row carries a detail that is easy to miss: on Cortex-M, function addresses in the vector table have the least significant bit set to indicate Thumb state. A reset vector with an even value is therefore either an error or a device that only supports a non-Thumb encoding, which is unusual.

Why these two words are a good test

The values are not arbitrary. They are generated by the linker from the memory map, so they encode the map.

Property What it implies
Stack pointer in the top quarter of a RAM region The linker placed the stack at the top of RAM
Stack pointer aligned to eight bytes The ABI requires it
Reset vector within the first part of the image The reset handler is early in the code
Reset vector with bit zero set Thumb encoding
All vectors pointing into the same region A single code region
Vectors clustered at one address An unhandled-exception stub

The fourth row is the fastest possible check for the byte-swapping error that plagues extraction: a byte-swapped image produces a reset vector with implausible alignment, and the check fails immediately.

The base address question

The vector table does not have to be at address zero. It can be relocated, and on Cortex-M the relocation is controlled by a register rather than by a pin.

Configuration Vector table location Consequence
Default Address zero, aliased to flash or RAM The image starts at zero
Relocated by register Any aligned address in the code region The image may start elsewhere
Bootloader plus application Two tables, one switched in Two images in one device
Remapped by a memory controller The alias changes after boot Address zero is not flash

The third row is extremely common: an application built to be started by a bootloader is linked at an offset, and its vector table sits at that offset. An extraction that assumes zero produces an image whose first word is not a stack pointer, and the check that makes this page useful is exactly the check that failing image fails.

Two images in one device

The bootloader arrangement produces a device with two valid images and one active vector table.

Region Content Vector table
Bootloader Update logic, no application At zero, active after reset
Application The product firmware At an offset, or copied to RAM
Configuration Settings, sometimes a copy of the table None

Reading such a device produces a file that contains both images, and the analysis has to separate them. The separation is usually obvious from the two vector tables, each of which looks like a valid table.

Other checks worth doing

The two-word test is the cheapest and it is not the only one.

Check Catches
Image length is a multiple of the page size Truncated reads
All vectors point inside the image Base address errors
The reset handler disassembles plausibly Wrong encoding assumption
A pool of strings exists and decodes An image that is not code
The region beyond the image is erased A read that ran past the end

The fifth row is a useful confirmation that the image boundary is real. If the bytes after the last vector-referenced function continue as valid code for megabytes, either the device is much larger than assumed or the read is returning something other than flash contents.

Where the validation fits in a workflow

The validation belongs immediately after the read and before any analysis, because everything downstream depends on it.

Step Output
Read the image Bytes
Check the two words Whether the base is right
Check the vectors Whether the image is coherent
Disassemble the reset handler Whether the encoding assumption holds
Proceed to function mapping Analysis

The third step is where a byte-swap or an offset error is caught. The fourth is where a wrong encoding assumption is caught, because a Thumb reset handler decoded as ARM produces immediate nonsense.

Protection and what it does to the image

The two-word check also distinguishes a refused read from a real one.

Read result First word Interpretation
Real image Plausible stack pointer Valid
All zeros Zero Refused, or blank
All ones All ones Refused, or blank
Scrambled Implausible Refused with a pattern, or wrong base

The fourth row is the one that requires care, because a scrambled image sometimes contains a plausible-looking word by chance. Checking all the vectors, not just the first two, is what makes the test reliable.

Where the next steps are

The protection levels that decide whether a read is possible at all are in extracting ARM MCU flash. The disassembly work that follows validation is in reading ARM software. Where the image has to be turned back into a loadable artefact with its segments, extracting an ARM chip covers the reconstruction.

Services at this boundary

Work covers readout, two-word and full vector validation, base address determination, bootloader and application separation, and encoding confirmation. It does not cover recovering an image from a device whose protection blocks the read, and it does not cover guessing a base address that cannot be derived from the image.

The habit worth adopting is to check the first two words before doing anything else with an ARM image, because the check takes a second and the errors it finds take days.