PIC18F46K22: Working With Three Quarters of an Image
Sixty-four kilobytes divided into four blocks, three of them readable. The arithmetic suggests seventy-five percent of the work is done, and the arithmetic is wrong in both directions: the readable portion is more useful than its share, and the missing portion matters more than its size.
The reason is that the missing block is usually the one containing the entry points, and entry points are what turn a byte sequence into a program.
What the missing quarter typically contains
On a device configured by a vendor who understood protection, the first block holds:
- the reset vector;
- the interrupt vectors;
- start-up code, including the clock configuration;
- whatever small routines the linker placed early.
Losing those removes the anchors and the initialisation, and keeps the application’s body — which is the bulk of the code and the part that is hardest to interpret without the rest.
| Missing | Present | Consequences |
|---|---|---|
| Vectors, start-up, small routines | Application body, tables | No anchors; clock unknown |
| Application body | Vectors, start-up | Anchors present; behaviour unclear |
| Application body, tables | Vectors, start-up, small routines | Structure known; logic missing |
| Everything | Nothing | Behavioural work only |
The first row is the common case and, awkwardly, the worst of the three partial outcomes.
Reconstructing the anchors
Four techniques, all usable on the readable three quarters:
Call target histogram. Every call carries its destination. Destinations that appear many times are library-like utilities; destinations that appear once, from a single site, are usually application functions. The histogram gives a provisional function list.
Handler recognition. A handler saves the working registers, does something short, restores and returns. The prologue and epilogue are distinctive, and their absence in the missing block tells you the vectors pointed into the readable region.
Table and dispatcher pairs. A jump table is a run of addresses followed by the dispatcher that indexes it. Finding one gives a set of entry points without needing vectors.
Entry-sequence matching. Start-up code initialises the clock in a fixed order. If any fragment of it survives, it identifies the reset path and gives the clock.
The clock is the first thing to establish
Without the start-up code, the clock configuration is unknown, and every timing constant in the readable code becomes uninterpretable. A delay loop of two hundred iterations is meaningless until you know the instruction rate.
Three ways to recover it, in order of preference:
- Measure it — an oscillator output pin, or a timed output.
- Infer it from a protocol — a serial rate is exact and derives from the clock.
- Constrain it from behaviour — a control loop period bounds the clock from below.
The second is usually available and usually ignored. A UART or CAN rate observed on a bus fixes the oscillator within a small tolerance, and that is enough.
Handling the image honestly
A partial image must be stored and passed on as partial. Practical measures:
- record the block map with the image;
- store the missing region as absent rather than filled, so that any use of it fails visibly;
- note the configuration and the revision;
- state which of the anchors were reconstructed rather than read.
Without those, a partial image is indistinguishable from a complete one, and the eventual failure will be attributed to the device rather than to the file.
When the missing quarter is fatal
Three situations where three quarters is not enough:
- The application’s behaviour depends on start-up ordering that cannot be observed.
- The clock cannot be measured or inferred.
- The missing block contains tables rather than code, and the tables are the domain knowledge.
The third is the one that surprises people. A block chosen for protection is sometimes chosen because it holds constants rather than because it holds vectors, and constants cannot be inferred from behaviour the way control flow can.
Related reading
- PIC18F45K22 and the boot block arrangement — the configuration that produces this partial outcome.
- PIC18F44K22 and the vectors as an anchor — what is lost when the first block closes.
- PIC18F24K22 and locating the boundary — how the readable extent is measured.
FAQ
Is three quarters of an image useful? Yes, and less than it appears: the missing block usually holds the anchors and the clock configuration.
Can entry points be found without vectors? Yes, from call targets, handler shapes and jump tables.
How is the clock recovered? By measurement, by inference from a serial rate, or by bounding it from a control loop period.
Should the missing region be filled? No. Leave it absent so that any use of it fails visibly.
When is a partial image not worth completing? When the missing block holds the domain constants rather than the control flow.
Conclusion
A three-quarter image is a workable position and a misleading one. The readable bulk is the application, the missing quarter is the frame that makes it readable, and the recovery of the frame — clock, entry points, handlers — is what determines whether the partial image becomes a program.