Posts Tagged ‘rompere il flash protetto da microcontrollore pic18f13k22’
PIC18F13K22: One Protection Bit Over Eight Kilobytes
Protection on larger parts is a partition: several blocks, each with its own bit, arranged so that a bootloader can be readable while the application is not. On the 13K22 there is no partition. A single configuration bit covers the array, and the array is small enough that the vendor had no reason to offer more.
The simplicity is useful. It removes a whole category of questions — which block is which, which boundary matters, whether a sibling block is exposed — and replaces them with one: is the bit set or not. What it does not do is make the part easier to read. A single bit over eight kilobytes is still a closed door.
The array, and its shape
Eight kilobytes of program memory in a 20-pin package. The core addresses it in words, with instructions two bytes wide, which means four thousand instruction slots. Code plus constants have to fit, and on a part this size they usually consume most of it.
That density matters for recovery. On a device using 6K of its 8K, an erased flash looks quite different from a programmed one in aggregate statistics — not in any single word, since protected reads return zeros uniformly, but in the distribution of what a full read produces when protection is off.
| Configuration | Reads return | Interpretation |
|---|---|---|
| Unprotected, programmed | Actual contents | Direct read |
| Unprotected, blank | FF words |
Genuinely empty |
| Protected, programmed | Zeros | Gate active |
| Protected, blank | Zeros | Indistinguishable from above |
The last two rows are the same result, and no amount of reading separates them.
What the single bit actually gates
The protection bit affects data reads from the array. It does not stop the core from executing, which is why a protected device is a perfectly functional product.
On this family the gate is enforced at the point where the array is read as data: table reads through the program memory access mechanism, and reads from an external programmer. Both return zero. The instruction fetch path is unaffected, because a device that could not fetch its own instructions would not run.
This produces the standard consequence: code can execute, so the device’s behaviour is available; code cannot be read, so the instructions are not.
Why there is no partial image
On parts with block protection, a common outcome is a partial read: some blocks visible, others not, and a resulting image with holes whose edges correspond to block boundaries. On the 13K22 that outcome cannot occur from protection — the whole array is one unit.
Where a partial read does occur, it means something else:
- A read interrupted at the interface. The transport stopped, so the image is short.
- A device that has been partly erased. Sector-level erase exists even where read protection does not.
- A read taken at marginal conditions. Some words returned, others did not, and the pattern is noise rather than structure.
Distinguishing these from each other is easier than distinguishing them from protection, because each has a signature: a short read is contiguous, a partly erased device has FF regions, and a marginal read disagrees with itself on repetition.
Configuration recovery
Even when the array is closed, the configuration word can sometimes be obtained. On many parts the config word is readable through the programming interface even when code protection is enabled, because the programmer needs it to know how to proceed.
Where that holds, the config tells you:
- whether the code-protect bit is set (confirming the diagnosis rather than inferring it);
- whether the watchdog is enabled, which affects timing measurements;
- the oscillator selection, which tells you what the board must provide;
- the brown-out level, which constrains supply behaviour.
On a device where only eight kilobytes exist, knowing the oscillator and watchdog settings is a substantial part of reconstructing the environment the firmware ran in — often more useful than a few hundred bytes of recovered code would be.
Reading a part this small by other means
The productive approach on a locked 13K22 is behavioural, and the size helps:
The job is small. An application that fits in 8K does a bounded set of things. Enumerate the pins, and the set of possible functions is short.
The interfaces are few. Twenty pins, most of them shared with analog functions. Measuring which are digital, which are analog, and at what rates defines the application’s shape.
Timing is measurable. A control loop in an 8K part runs at a period you can observe on a pin. That period constrains the instruction count per iteration, which constrains the algorithm.
Assembling those three gives a specification that a rebuild can satisfy, and a rebuild is a legitimate outcome when the original cannot be read.
What to check before concluding a device is locked
Three things produce a false “locked” diagnosis:
- Reading through an interface that does not support reading that region.
- A programmer configured for a different part in the same family, which reads at the wrong addresses.
- An erased device, which returns the same value as a protected one.
The first two are configuration errors and cost nothing to exclude. The third is excluded by the config word, which is why reading it first is worth the effort.
Related reading
- PIC18F14K22 and protection with two blocks — what changes when the array can be partitioned.
- PIC18F14K50 and USB descriptors — information that leaks without reading the array.
- PIC18F23K22 and the difference between read and write protection — the second gate that is often confused with the first.
FAQ
Can a single block be read on this part? No. There is one protection bit for the whole array.
Does protection stop the device running? No. It affects data reads, not instruction fetches.
Is a zero read proof of protection? No. An erased array read through the same path can return similar values; the config word distinguishes them.
Is the config word readable when code is protected? On this family, usually yes — the programmer needs it. That is enough to confirm the diagnosis.
Can an 8K device be reconstructed from behaviour? More readily than a large one: fewer pins, fewer interfaces, a shorter list of possible functions.
Conclusion
The 13K22 reduces code protection to a single bit over a single array. That removes the block-boundary questions entirely and leaves one binary fact, which the configuration word usually settles. From there the work is behavioural, and eight kilobytes of application is a small enough space for that to be realistic.