PostHeaderIcon Where the Code Lives: On-Chip Flash, External SPI, OTP

The phrase “extract the MCU code” contains an assumption that is wrong often enough to be worth challenging: that the code is in the MCU. In products built around a small microcontroller, a substantial minority keep the application in an external serial flash and use the MCU only as a bootstrap. Attempting the MCU in those products yields a hundred bytes of loader and a customer who believes the extraction failed.

The first job is therefore not extraction. It is location.

The three carriers

Code can live in three places, and they differ in almost every property that matters.

On-chip flash is the common case for anything with a modern microcontroller. It is programmable, it is protected by a bit, and it is read either through the debug interface or through a flash command, or not at all.

External serial flash is the common case for anything with a large application, a display, an audio stream, or a part chosen for cost. The microcontroller boots from ROM, fetches a small loader from the flash, and the loader brings in the rest. The flash is a separate package on the same board, usually eight pins, and it usually has no protection at all.

OTP and mask ROM are the cases where the code is genuinely unchangeable. One-time-programmable memory is written once at the factory; mask ROM is written by the mask. Both exist in high-volume cost-sensitive products and in parts old enough that flash was expensive.

Carrier Typical product Protection Read route
On-chip flash Modern MCU designs Bit or fuse Debug or command
External serial flash Large application, cost-driven Usually none Direct clip or desolder
OTP High volume, fixed function Sometimes none Interface or optical
Mask ROM Very high volume None, but not rewritable Optical

The second row is the one that changes projects. An external flash with no protection is a ten-second read with a clip, and it converts a job that was quoted as a protected-MCU extraction into a job that is finished before lunch.

How to tell which one applies

Four observations, in increasing order of cost.

Read the board. An eight-pin SOIC or a small BGA sitting near the microcontroller with four signal lines to it — clock, data in, data out, chip select — is a serial flash. That layout is unmistakable once seen, and it is visible without powering anything.

Check the part numbers. A microcontroller whose datasheet lists an external bus interface and a flash whose datasheet lists a standard command set are a strong indication that the pair is a boot-from-flash system.

Capture the bus at power-up. This is the decisive observation and it takes ten minutes with a logic analyser. Clip onto the four lines, power the board, and look at the first millisecond. A system that boots from external flash shows a read command and a burst of addresses immediately after reset, before anything else happens. A system that boots from internal flash shows no activity on those lines at all, or shows activity only later when the application decides to read its own data.

Measure the current profile. A device that fetches instructions from an external bus has a current profile with bursty structure at the bus rate; a device running from internal flash has a smoother profile. This is a weaker signal than the bus capture but requires no connection.

Observation Cost Confidence
Board inspection for an eight-pin part Minutes High
Datasheet interface check Minutes Medium
Power-up bus capture Ten minutes Decisive
Current profile Minutes Low

Extracting from each carrier

Once the carrier is known, the route follows from it.

External flash is the easiest and the work is a read. In-circuit reading with a clip works when the microcontroller is held in reset, because otherwise two masters drive the same lines and the result is a wired-AND of both. Where the package is not clip-compatible, the part is removed and read in a socket, which is destructive to the board and not to the device. The command set is small and standard, and the full array comes out in seconds to a minute depending on size; the mechanics are covered in brute force on EEPROM, which shares the same bus and the same lack of protection.

On-chip flash requires the protection to be dealt with first, and that is the subject of the whole attack-surface analysis in MCU attack surfaces. Once the array is readable, the extraction is a dump.

OTP is the awkward case. If it is implemented as locked flash, the route is the flash route. If it is an antifuse array, the stored value is a physical link rather than a charge, and the only route is optical — which means the device does not survive and the cost is set by the process node.

Mask ROM is optical by definition. The pattern is visible under a microscope after the passivation is removed, and the reading is a matter of imaging and decoding rather than of protocol.

The part number is not the answer

The instinct when a customer names the microcontroller is to look up its memory size and conclude where the code is. That inference fails often enough to be untrustworthy.

The same part appears in products with completely different architectures. In one, the application fits in the internal flash and the external bus is unused. In another, the same part runs a two-hundred-byte loader from internal flash and pulls a sixteen-megabyte image from an external part, because the application is a user interface with fonts and audio. The silicon is identical and the carrier is different.

Same MCU, different products Internal flash holds External flash holds
Simple controller The whole application Nothing
Appliance with a display A loader Application, fonts, sounds
Connected device Bootloader plus application Firmware images, certificates
Sensor node The whole application Calibration, logged data

The fourth row is a trap in the other direction: the external flash exists and holds data rather than code, so a read succeeds and produces something that is clearly not a program. Recognising that is the difference between concluding “the extraction failed” and concluding “the code is in the MCU”.

The rule that follows is simple. The board decides, and the part number only tells you what is possible.

A note on reading the external part

Reading a serial flash is straightforward, and three details account for most of the failed attempts.

The first is the status register. A flash that is still completing an internal write ignores commands, and a read issued during that window returns garbage or nothing. Polling the write-in-progress bit until it clears, before the first read, costs a second and prevents a dump that looks plausible and is partly wrong.

The second is the address width. Parts above sixteen megabytes require four-byte addressing, and a command issued in three-byte mode on such a part reads the wrong region entirely — usually the first sixteen megabytes, with the rest inaccessible. The mode is selected by a command or by a dedicated instruction, and it is not always the default.

The third is the fast-read dummy cycle. The high-speed read instruction inserts one or more dummy bytes between the address and the data, and a reader that omits them shifts the entire dump by a byte or more. The shift is invisible in a hex viewer, and it produces an image that is wrong in a way that is very hard to see.

Detail Symptom if wrong Check
Write in progress Garbage at the start of the dump Poll the status bit
Address width Only the first part of the array readable Compare size to part number
Dummy cycles Every byte shifted Look for a vector table at offset 0
Chip select timing First byte lost or duplicated Compare two reads

The third row is the one to check first when the dump does not look like code, because a shifted dump has no valid vector table at offset zero and everything after it looks like noise.

The bootstrap case

There is a fourth arrangement worth describing because it is frequently misdiagnosed: a system where the MCU has internal code and the external flash holds something else.

A common example is a microcontroller with a fixed firmware that implements a function — a display controller, a USB bridge, a radio — paired with an external flash that holds configuration, fonts, or a protocol table. Reading the flash yields data that is clearly not code: no vector table, no plausible instruction density, a repeating structure.

The symptom is a customer who says the extraction produced nothing useful. The correct interpretation is that the extraction produced exactly what was there, and the code was in the MCU all along.

What the flash contains Signature Implication
Application code Vector table at the start, high code density The MCU is a bootstrap
Configuration and fonts Repeating structures, no vector table The MCU holds the code
A compressed image High entropy, a header with a length field Decompress after read
A second-stage loader Short, jumps to an address outside itself Follow the jump

The fourth row is worth checking because it is the case where reading the flash appears to succeed and produces almost nothing. A loader of a few hundred bytes that ends by jumping to an address it has just written tells you that the real image arrives over a different path — often a network or a USB interface — and that the flash is only a staging area.

What the service delivers

The first deliverable is a location statement: which carrier holds the code, what else is on the board, and what the extraction route and cost are for that carrier. It is produced in a day and it prevents the most common wasted engagement in this field, which is a protected-MCU attack on a product whose code was in a flash chip all along.

Where the carrier is an unprotected flash, the extraction is hours and the deliverable is a complete image plus a note on whether it is compressed or staged. Where it is a protected MCU, the cost is set by the surface map. Where it is OTP or mask ROM, the deliverable is a statement about survivability and a quotation for the optical route, which is described in extracting IC flash.

What we do not do is accept a request to “read the MCU” without first confirming that the code is in the MCU. That confirmation is cheap, and the alternative — a decapsulation performed on a part that held only a bootstrap — is the most expensive way to learn the same fact.

Comments are closed.