PostHeaderIcon PIC18F23K22: Write Protection Is Not Read Protection

Two bits sit next to each other in the configuration word, both named for protection, both applied per block. One stops you reading the array; the other stops you writing it. They are independent, and the independence is the source of an expensive mistake that happens on benches regularly: a device that could have been read is erased, on the assumption that removing write protection would expose it.

Getting these straight is a five-minute exercise that saves devices.

What each one does

Code protection gates reads. With it set, the array cannot be read from outside the device — through the programmer, or through table reads from unprotected code. The device still runs.

Write protection gates modification. With it set, the block cannot be erased or programmed. The array remains fully readable if code protection is clear.

Read-protected Write-protected Read Modify What it implies
No No Yes Yes Development, or unlocked production
No Yes Yes No Fixed content, readable — the useful case
Yes No No Yes Locked but recoverable by erase
Yes Yes No No Fully closed

The second row is the one people fail to look for. A device with write protection and no read protection is a device whose content is available right now, and it is also the configuration a vendor chooses for a boot block that must never change.

Why a vendor protects writes without protecting reads

Because it is the configuration that makes a fixed region safe without making it secret. A bootloader in the boot block should never be modified by field activity; it does not necessarily need to be hidden.

There is a second motive, less flattering: some production processes set write protection because the tooling offers it in the same dialog as code protection, and nobody separated the two. Those devices are readable, and the fact that they are readable is discovered only by trying.

The mistake, in full

The sequence that destroys images:

  1. A device will not respond to a read. The reader concludes “protected”.
  2. The reader decides to remove protection by erasing.
  3. The erase clears the array, and with it the program.
  4. The device is now readable and empty.

Step one is where the error occurs, and it usually comes from treating a failed read as evidence of code protection. A failed read has several causes: wrong part selected in the tool, wrong programming entry voltage, low-voltage programming disabled so the entry method is wrong, or a device whose configuration is genuinely protective.

Only the last of those justifies an erase, and it is distinguishable from the others by reading the configuration word — which is exactly the read that remains available when everything else is closed.

Order of tests that avoids the mistake

Before any modifying operation:

  1. Read the configuration word. It states both protection settings.
  2. Attempt to read a known-populated address. The vector table at the bottom of the array is the standard target, since every program has one.
  3. If that read returns zeros while the configuration says code protection is clear, the problem is the interface, not the device. Fix the interface.
  4. Only if the configuration states code protection is set, consider whether an erase is acceptable — which requires knowing that the content is expendable.

Step four is the point where a decision has to be made honestly. Erasing to unprotect is a valid technique for a device whose program exists elsewhere. It is a total loss for a device whose program does not.

Write protection’s own complications

A write-protected block is protected against the ordinary programming sequence, and removing the protection requires an erase — of the whole device on this family, since configuration is erased with the array.

There is a further wrinkle: some write-protect bits are applied by a bootloader at runtime, so a device can be writable when read immediately after reset and write-protected once the application is running. Reading the configuration twice catches this, and it is a useful signal that the device has a bootloader at all.

What survives a partial attempt

If an erase was performed and the program was expendable, the device is restored and the story ends well. If it was not expendable, what remains is:

  • the behaviour recorded before the erase, if anyone recorded it;
  • sibling units with the same build, if any exist;
  • the vendor’s update package, if the product had one.

Nothing in the array. This is why the checklist above is worth more than the technique it protects: the technique recovers a device, and the checklist decides whether the device should be touched at all.

Related reading

FAQ

Does write protection stop a read? No. Write protection gates modification; read protection gates reads.

Can write protection be removed without an erase? Not on this family. Configuration is cleared by the erase that removes the protection.

Why would a device be writable but unreadable? That is a locked part. It can be erased and rewritten, which recovers the device and loses the program.

Is a failed read proof of code protection? No. Wrong part selection, wrong entry voltage and disabled low-voltage programming all produce a failed read.

Can a bootloader change these settings? Yes, which is why the configuration should be read both after reset and during operation.

Conclusion

Two adjacent bits, two different questions, and a habit of treating them as one. Code protection hides the array; write protection freezes it. Reading the configuration word before touching anything tells you which applies, and that single read is what stands between a recoverable device and an erased one.

Comments are closed.