PIC18F24K22: Finding the Gate by Testing Addresses
On a 16K device with four protection blocks, a device can present a readable region that stops at one of four addresses. Which one it stops at is a fact about the configuration, and it is recoverable by experiment even when the configuration cannot be read.
The method is a binary search over addresses, and it takes fewer reads than most people expect: with four possible boundaries, two well-chosen probes distinguish most cases.
Why the boundary is informative
The position of the transition tells you which blocks are open, which tells you something about the product’s design:
| Transition at | Blocks open | Likely intent |
|---|---|---|
| Immediately | None | Fully locked |
| First boundary | Boot block only | Vendor loader visible |
| Second boundary | Lower half | Development build shipped locked above |
| Third boundary | Three quarters | Update path visible, application protected |
| Never | All | Unprotected, or read attempt failed |
The fourth row is the interesting one, and it is more common than the others on products that were updated in the field: the region containing the loader and the vector table is open, and the application above it is not.
The search
Each probe is a read of one word at a candidate address, compared against what a populated address should contain. The comparison is not against an expected value — you do not know the contents — but against the pattern of a protected read, which is uniform.
The procedure:
- Read a word at the lowest application address. If it is uniform (all zeros or all ones), the first block is closed and the search is over.
- Otherwise, read at each block boundary. The first address that returns uniform values is the transition.
- Refine within the block before the transition if more precision is needed — though on this family, blocks are the granularity, so it is not.
The number of reads is small, and each is cheap. The cost is in interpreting the results, which requires knowing that a uniform read can mean three different things.
Three explanations for a uniform read
The block is protected. The gate returns a fixed value.
The block is erased. There was never any content.
The read is failing. The programmer is configured for the wrong part, or the device is not entering programming mode reliably.
The first two are indistinguishable at a single address and distinguishable in aggregate: an erased block is uniform everywhere, and so is a protected one. Only the configuration word separates them — which is why the binary search is a supporting technique rather than a primary one.
The third is distinguishable by reading an address that must be populated on any working device. The reset vector is the standard choice, and on a device whose first block is open, it is the first thing the search reads.
What “code recovery” means at this point
Once the boundary is known, the readable portion is available and the unreadable portion is not. Recovery from that position means:
- reading the open blocks completely, twice, at different speeds;
- recording the block map alongside the image;
- treating the image as partial in every subsequent use.
The last item is where discipline is needed. A partial image written to a file and handed to someone else will, eventually, be programmed into a device, and the device will boot from a vector that points into a region the file does not cover. Recording the map prevents that; omitting it guarantees it.
The vectors question
If the first block is open, the vector table is available, and the readable region can be decoded from its entry points. If the first block is the closed one, the remainder of the image has no anchors and must be decoded by scanning.
That asymmetry means the boundary’s position determines not just how much is readable but how legible it is. Three quarters of an image including the vectors is more useful than three quarters excluding them.
Limits
- Protection is per block; there is nothing to find within a block.
- A uniform read has three explanations, and only configuration distinguishes them.
- A partial image is partial regardless of how carefully it is recorded.
Related reading
- PIC18F14K22 and the two-block version — the same search with fewer candidates.
- PIC18F25K22 and restoring what was read — what a partial image still cannot reproduce.
- PIC18F23K22 and the protection bits themselves — the configuration that the search is inferring.
FAQ
How many probes does the search need? A handful. With four blocks there are four candidate boundaries.
Can a uniform read be trusted as evidence of protection? No. Erasure and read failures produce the same result; only configuration separates them.
What if the first block is the closed one? The image has no vector table, and decoding has to proceed by scanning.
Is there any granularity finer than a block? No. The block is the unit of protection.
Why read twice at different speeds? To distinguish content from sampling error, which produces non-uniform values that look like data.
Conclusion
On a device with four blocks, the position of the readable region’s edge is a measurement rather than a guess, and it is obtained by probing a few addresses. What the boundary means depends on which blocks it exposes — and the most valuable configuration is the one where the vectors are inside the readable part.