PostHeaderIcon Attack Microchip MCU Firmware: When Row Erase Vetoes Code Protect

Microchip’s 8-bit families have been in production for three decades, which means two things for anyone attacking them. First, the protection mechanisms are simple and extremely well documented, because there has been a long time to document them. Second, the simple mechanisms have been re-implemented across dozens of device generations with small variations, so a technique that works on one part may be refused by another for a reason that is not obvious from the datasheet.

The interaction that governs most of this work is between the code protect bit and the row erase primitive. They are not independent features. They are two halves of one access-control decision, and understanding how they veto each other is the difference between a project that returns firmware and a project that returns a blank part.

What the code protect bit actually controls

On a PIC device, the code protect bit lives in the configuration word alongside oscillator selection, watchdog settings and brown-out thresholds. It does not encrypt anything. It gates the read path: with the bit set, a read of program memory through the programming interface returns a defined pattern rather than the stored contents.

The pattern is part of the specification, and it is worth knowing because it is frequently mistaken for data. Depending on the device, a protected read returns zeros, ones, or a scrambled pattern derived from the address. A pipeline that writes whatever bytes come back to a file will produce a file, and the file will look plausible to anyone who does not check entropy or disassemble it.

Read result Interpretation
All zeros Read refused; the bus returns a fixed pattern, not the array
All ones Same, with the opposite fill
Address-dependent scramble Deliberate obfuscation of the returned pattern
Real code Protection is not actually set, whatever the marking suggests

The fourth row is more common than expected on refurbished or remarked parts. A device sold as protected may have been erased and reprogrammed, or may be a different die entirely.

Why the erase comes first

The programming interface on these parts is stateful. To enter a mode where the array is addressable for reading, the device must first be in a known state, and on many families the only way to reach that state from an unknown state is a bulk or row erase. So the sequence that ends in a read begins with an erase, and the erase is not selective about what it removes.

Row erase is the finer-grained primitive: it clears a row of program memory rather than the whole array. On parts that offer it, row erase exists to support self-programming and bootloader designs, where a running program needs to rewrite a page. It is a legitimate feature with a legitimate use, and its side effect on the protection state is a consequence of how the configuration word is stored.

Sequence Effect on protection Effect on code
Bulk erase Cleared Destroyed
Row erase of the row holding the config word Cleared That row destroyed, others may survive
Row erase of a code row Unchanged That row destroyed
Read without erase Unchanged Returned only if unprotected

The second row is the interesting one, because it describes a real technique and a real limit at the same time. If the configuration word shares a row with code, clearing the protection destroys the code in that row along with it. On devices where the configuration word occupies its own row, the trade is cheaper. Which layout applies is a per-device fact, and it is the first thing to establish.

The asymmetry that makes physical routes necessary

Because the logical route is vetoed by its own precondition, the routes that actually return code are the ones that do not ask the device for permission. That means decapsulation and direct access to the array, or a side channel that leaks information about the contents without a read.

This is where the family’s physical characteristics help. Older PIC dies are fabricated on modest geometry with few metal layers, and the program array is a regular, recognisable structure under a microscope. The array is also relatively small, which keeps the imaging time bounded. None of this is unique to Microchip, but the combination of small arrays and simple layouts makes these parts unusually tractable for direct readout, and that is why they remain a large share of this kind of work.

Distinguishing protection from damage

A device that refuses to read is not necessarily protected. It may be damaged, it may be a counterfeit, or it may be in a state the programmer cannot exit. The distinction matters because the three cases have different costs and different prognoses.

Observation Likely cause Next step
Device ID reads correctly, memory reads blank Protection set Physical route or decapsulation
Device ID reads incorrectly Wrong part, remarked part, or damaged die Confirm identity before proceeding
No response at all Electrical damage or wrong programming entry Check supply, clock, and entry sequence

The first row is the normal case and the one this page is about. The second row is a category error that wastes weeks: work begins on the assumption of one device and the die turns out to be another. Restoring embedded information discusses what can be recovered from a device that has already been through an erase, which is a different problem with a different answer. The general taxonomy of methods, from the non-invasive to the destructive, is set out under MCU attack.

What the firmware looks like when it comes back

A successful read of a PIC program array produces a byte image that corresponds directly to instructions, because the architecture is word-oriented but the file is usually byte-oriented. The conversion is not always one-to-one, and on parts with word-addressed flash the difference produces the classic two-times offset error. Beyond that, the image contains more than code: the configuration word, the device ID location, and on some parts a user ID area that frequently carries a serial number or a calibration constant.

Losing the configuration word is a common and expensive mistake. A device reprogrammed with the correct code but the wrong oscillator configuration will not run, and the failure looks like a code problem. Any delivered image should carry the configuration word explicitly, not as part of a blob.

Turning the image into something readable

Once the array is out, the work changes character completely. The instruction set is small enough that disassembly is quick, but the architecture provides no stack frame, so function boundaries must be inferred from the call graph rather than recognised from a prologue. That problem is treated on its own in attacking Microchip source code, and it is the reason a PIC reconstruction takes longer than the instruction count suggests.

The decompilation itself is covered more generally in cracking microcontroller source code, which sets out the four losses that no tool can undo. On this family the losses are milder than on a 32-bit part — inlining is rare, scheduling is minimal — but the boundary problem is worse, and the two effects roughly cancel.

What a protected part costs on the bench

It is worth being concrete about what the physical route involves, because customers often assume it is a software procedure with an unusual name. It is not. The package is opened, the passivation is removed, and the array is either imaged or probed. The device does not survive as a working part in most cases, so the deliverable is the data and not a repaired chip.

That has two consequences for planning. First, the sample must be the right one, because there is no second attempt on the same device. Second, any behavioural measurement that requires a working device — timing, protocol capture, power sequencing — must be done before the package is opened, not after.

Services at this boundary

Work on this family covers protected readout through physical routes, configuration word recovery, device identity confirmation before work starts, and image validation including disassembly sanity checks. It does not cover clearing the protection bit while keeping the array contents, because on these parts the two share the same primitive, and it does not cover devices that have already been bulk-erased, because the contents are gone.

The practical rule is that a PIC part with code protect set is a physical project, not a logical one. Quoting it as a logical project is the single most reliable way to lose money on it.

Comments are closed.