Posts Tagged ‘fpga mcu source code read’

PostHeaderIcon Three Read Channels and the Address Space Each Can Reach

There is more than one way to read a microcontroller’s memory, and they are not equivalent. The debug interface reads memory as a side effect of being able to see everything. The in-system programming interface reads it through a dedicated command. The parallel programmer reads it by driving the bus directly.

Each reaches a different address range, at a different speed, and is gated by a different protection bit. That last difference is the one that produces findings, because a design that closes one channel and forgets another is a common shape.

Channel one: debug memory access

A debug interface exposes memory through the same mechanism it uses for everything else: a transaction that reads a location on behalf of the debugger.

On ARM’s SWD this is a register write to a transfer register followed by a read of a data register. Each transaction moves one word, and the overhead per word is substantial — a few microseconds — which caps the throughput in the low hundreds of kilobytes per second.

Property Value
Address coverage Entire map: flash, RAM, peripherals, system
Granularity Word, sometimes byte
Throughput 100–500 KB/s
Gated by Debug lock
Halts the core No, if the interface supports non-intrusive access

The breadth of coverage is what makes this channel valuable and what makes it dangerous. A debug session can read the flash, inspect the RAM to see decrypted data, and read peripheral registers to see keys loaded into a crypto engine. The last of those is frequently overlooked: a key that never leaves the crypto block still passes through a register, and the register is in the address map.

Channel two: the ISP read command

The in-system programming interface is a command protocol rather than a memory access protocol. It understands a small set of operations: read memory, write memory, erase, read status, read identifier.

Its coverage is narrower. Most implementations read the program and data memories and nothing else. RAM is not addressable, peripherals are not addressable, and the identifier is a separate command.

Property Value
Address coverage Program and data memory only
Granularity Block, typically 64 to 256 bytes
Throughput 10–50 KB/s at typical baud rates
Gated by Read protection, and often the bootloader’s own enable
Halts the core Yes, usually

The block granularity is why the throughput is respectable despite a slow link. A read command returns a block, and the host pipelines the next command while the previous block arrives, so the effective rate approaches the link rate rather than the transaction rate.

Channel three: the parallel programmer

A parallel programmer does not use the device’s protocols at all. It takes control of the address and data buses and drives a memory cycle directly, using the device’s external bus interface or a dedicated programming mode.

Property Value
Address coverage Whatever the bus can address
Granularity Bus width, one word per cycle
Throughput Hundreds of KB/s to several MB/s
Gated by The programming-mode enable, and the lock bits
Halts the core Yes, completely

This is the fastest channel and the most invasive. On parts that support it, it also bypasses the serial interface’s own state machine, which is why it is the recovery route when the serial interface has been disabled, as described in AVR lock bits and HVPP.

Where the channels disagree

The three channels are documented separately and, on a well-designed part, gated by a single consistent protection state. On many real parts they are not, and the disagreements are the findings.

Inconsistency Consequence
Debug lock gates reads but not the ISP command ISP is the way in
Read protection gates the ISP command but not debug Debug is the way in
Parallel mode ignores the read lock Parallel is the way in
A factory test mode ignores all three Test mode is the way in

The second row is the most common and the most surprising. A design where the read-protection bit is checked by the bootloader’s read command but not by the debug unit’s memory access is a design that protects the wrong interface. The protection is implemented in software in one place and in hardware in another, and only one of them was tested.

Channel Which bit should gate it Which bit is often the only one present
Debug Debug lock Debug lock
ISP Read protection Read protection
Parallel Both Programming-mode enable

Reading speed as a constraint

The three channels differ in throughput by two orders of magnitude, and that difference changes what is practical.

A full 512 KB image over a debug interface at 200 KB/s takes about two and a half seconds. The same image over an ISP link at 20 KB/s takes twenty-six seconds. Neither is a problem.

The problem appears when many reads are needed. A reliability study that reads an image three times for comparison, on twenty devices, is 120 reads: five minutes over debug and an hour over ISP. A parameter sweep that reads one register per attempt is a different matter entirely, and there the debug channel’s per-transaction overhead dominates.

Task Channel Duration
Single image, debug Debug Seconds
Single image, ISP ISP Tens of seconds
Image plus RAM plus registers Debug Seconds
Repeated single-register reads Debug Milliseconds each
Twenty devices, three passes Either Minutes to an hour

What the read returns versus what the core sees

One more distinction matters and it is easy to miss: none of the three channels necessarily returns what the core sees.

On a part with fetch-path decryption, the core reads plaintext and the array holds ciphertext, so a read through any channel returns ciphertext. On a part with a memory protection unit that remaps regions, a read at an address may return the contents of a different physical location. On a part with a cache, a debug read may return the cached value rather than the memory value, which is correct for the core and confusing for the analyst.

Channel Returns May differ from core view
Debug Memory as the bus sees it Cache contents, remapped regions
ISP The array as stored Decrypted view, if any
Parallel The array as stored Nothing; it is the raw array

The third row is the useful one when the question is what is actually in the memory. A parallel read is a raw read, and a raw read is the ground truth against which the other two are interpreted.

How the channel map is produced

The map is produced by testing each channel against each permission state, on a sacrificial unit.

Start with an unprotected device and confirm that all three channels read the same content. That establishes the baseline and confirms that the addresses correspond.

Step Action Output
One All channels on an unprotected part Baseline and address correspondence
Two Set read protection, retest each channel Which channels the bit gates
Three Set debug lock, retest Which channels the lock gates
Four Attempt the parallel mode with protection set Whether it is gated at all

The fourth step is the one that produces findings most often, because parallel modes are frequently implemented for factory use and gated by a different mechanism from the serial interfaces.

Where this fits

The channel map is the input to the surface selection in MCU attack surfaces, and the permission model it tests is the one set out in programming port permissions.

Where the dump from a channel turns out to be high entropy, the explanation is in instruction-fetch decryption, and the difference between a raw read and a decrypted view is what decides whether the image is usable.

What we deliver for this layer is the channel map plus a raw read through the channel with the widest coverage that the protection permits. Where all three channels are gated consistently, the map says so and the work moves to a route that does not go through the array at all.

The prior question, of which region of the code is worth reading in the first place, is the one in discovering IC code, and it frequently narrows a full extraction to a single block.