PIC18F25K20: What a Hex File of a Locked Part Can Honestly Contain
A hex file has a shape that implies completeness: records from the first address to the last, an end-of-file marker, checksums on every line. It is a format that reports success. Nothing in it says “this region was not readable”, because the format has no way to say that.
On a device read under protection, the file therefore contains less than it appears to. The gap between appearance and content is where recovered firmware goes wrong — not at the moment of recovery, but years later, when someone programs the file and cannot understand why the device behaves oddly.
What the format can express
Intel HEX can express: data at an address, an end marker, an extended address change. That is all. It cannot express a hole, an uncertainty, or a comment.
| Fact about the recovery | Expressible in hex |
|---|---|
| Bytes at an address | Yes |
| A region that was not readable | No |
| Whether configuration was captured | Only as records, with no marker |
| The silicon revision | No |
| Which blocks were protected | No |
The first row is the format’s whole vocabulary. Everything else has to live outside it.
Filling a hole: the three options
When a region could not be read, a file has to do something about it. Three choices, each defensible, each with a different failure mode:
Omit the region. The file has no records there. A tool loading it leaves those addresses at whatever the destination contains — usually erased, sometimes not.
Fill with the erased value. The file has records covering the region, filled with the value an erased cell reads as. The file is continuous and the content is a lie that looks like a blank device.
Fill with zeros. The same, with a value that is also what a protected read returns, which makes the file indistinguishable from one captured from an unlocked device with a zeroed region.
The second is the most common and the least harmful in practice, because a device with an erased region behaves like an unprogrammed one and the symptom is obvious. The third is the most dangerous, because a run of zeros looks like data and produces a device that runs into a halt or a reset somewhere unexpected.
Configuration, and the assumption that it came along
Whether the configuration is in the file depends on the export. Many tools write it only if asked. A file without it produces a device configured however the destination part happened to be configured, which on a virgin part means factory defaults.
Factory defaults are frequently wrong for the application: internal oscillator instead of crystal, watchdog off, brown-out at the lowest setting. The device runs and behaves differently from the original in ways that look like code differences.
The remedy is not in the file. It is in the record kept alongside: the configuration bytes, read from the source device, stored with the image as text.
The header that makes a file durable
A recovered image should be accompanied by, at minimum:
- source device part number, revision and unique identifier if present;
- configuration bytes, verbatim;
- block map, stating which regions were read and which were not;
- the date and the method;
- what the data array contained, if it was read.
Five lines of text. Their absence is why files recovered in one year are misinterpreted in another, and the cost of writing them is minutes.
Why “reverse” is the right verb here
Working with a locked device of this size inverts the usual direction. You do not start from the code and ask what it does; you start from the device and ask what the code must be. Everything measurable — timing, pin behaviour, protocol, response — is evidence about an image that is only partly available.
The hex file is then a record of what was obtained, not a description of the device. Treating it as the latter is the mistake this article exists to prevent.
Practical sequence
- Read what is readable; record the block map as you go.
- Read the configuration.
- Record part number and revision.
- Assemble the file, choosing deliberately among omit, fill and zero.
- Write the header.
- Verify by loading the file into a tool that displays its address map, and confirm the map matches the record.
Step six is the only defence against the silent errors, because it is the only step that compares what the file says against what the device gave.
Related reading
- PIC18F24K22 and finding the readable boundary — how the block map is established.
- PIC18F23K20 and reading configuration first — the step that supplies the header’s most important line.
- PIC18F26K20 and what recovery does not produce — why the result is an image and not source.
FAQ
Can a hex file represent an unreadable region? No. The format carries data and addresses, and nothing about provenance.
Which fill value is safest? The erased value, because a device with an erased region fails obviously rather than subtly.
Is configuration always included in an export? No. It must be checked and, if absent, recorded separately.
Why does a recovered file produce a device with the wrong clock? Because the configuration was not captured and the destination used factory defaults.
What is the minimum documentation? Part, revision, configuration bytes, block map, date and method.
Conclusion
A hex file is a record of bytes, and a recovered file is a record of the bytes that were obtainable. The format cannot say so, which makes the accompanying header part of the deliverable rather than an optional courtesy — and makes the honest description of a partial recovery more valuable than a file that pretends to be complete.