PostHeaderIcon PIC18F13K50: Two Gates, Not One, on a USB Bootloader Design

Most discussion of protection treats it as one thing: a bit that stops a programmer reading the array. On a part with a USB bootloader that framing breaks down, because there are two audiences for the same memory — a programmer outside the device, and code inside it.

The two are gated separately. Code protection stops reads from outside. Table-read protection stops reads from code, whichever block that code happens to live in. A device can be protected against one and open to the other, and on parts designed for field updates that combination is not an accident.

Why a bootloader needs the application readable

A USB bootloader’s job is to receive a new application and write it into the array. Before writing, it typically verifies what is already there: a checksum, a version, a signature. That requires reading the application block.

If the application were protected against table reads from the boot block, verification would return zeros and the bootloader could not do its job. So the application is usually left readable from code, and protected — or not — against external reads.

The reverse applies to the bootloader. It is the vendor’s intellectual property and the customer’s application has no reason to read it. Table-read protection on the boot block makes the bootloader invisible to the application while leaving it executable.

Setting External read Read from code Typical use
No protection Allowed Allowed Development
Code protect only Zeros Allowed Application, verifiable by bootloader
Table-read protect on boot block Allowed or gated Zeros in boot block Vendor bootloader
Both Zeros Zeros where protected Shipped, fully closed

What this means for an outside reader

The first practical consequence: a device that is readable from code is not therefore readable from a programmer, and the reverse. Establishing which gate is closed requires two different tests.

The second: table-read protection is enforced while code runs, so a debugger that halts the core and reads memory through the programmer interface is subject to the external gate, not the internal one. A device that returns zeros through the programmer and behaves normally in operation is consistent with several configurations, and the config word is the only reliable discriminator.

The USB interface as an observation point

A USB device announces itself. Enumeration transfers a device descriptor, a configuration descriptor, and string descriptors, and these are generated by firmware that lives in the array.

That makes enumeration a read of sorts — not of the code, but of strings and constants that the code emits. On a locked device, this is often the first concrete information obtained:

  • Vendor and product identifiers identify the product family, and often the vendor’s design house.
  • String descriptors frequently carry a product name and a firmware version.
  • The device class narrows what the firmware does: HID, CDC, vendor-specific.
  • Endpoint configuration describes the communication shape, which constrains the protocol.

None of that is the program. All of it is metadata the firmware must produce to work, and none of it is protected by a code-protection bit.

Recovering the protocol from the host side

Given a working device, the host-side driver or application is often available, and it documents the protocol by construction. Capturing the bus during normal operation gives the exchange formats, and the timing gives the frame rate.

For a device whose firmware cannot be read, the protocol plus the descriptor set plus the behaviour is a specification, and a specification is what a rebuild needs. This is the standard route for USB peripherals, and it is more reliable than it sounds because USB leaves so little room for variation in the lower layers.

What the protection actually costs

On an 8K part, protection has an unusually high price because there is so little array. The upper block, or the whole array depending on configuration, is a meaningful fraction of the device’s code.

That has an implication for what the protection was for. A vendor protecting an 8K USB part is usually protecting a small, valuable piece of logic — an authentication routine, a proprietary protocol handler, a calibration algorithm. Those are exactly the pieces where behavioural reconstruction is most difficult, because the behaviour is designed not to be observable.

The honest assessment: on this part, a fully protected device with a well-designed protocol is a hard target, and the realistic output is a device that speaks the protocol rather than a device that contains the same code.

Checks before drawing conclusions

  • Confirm the part number from the descriptor string and the device marking; they do not always agree.
  • Read the config word where possible; it settles which gate is closed.
  • Test whether the bootloader still functions. A device whose bootloader responds can still be updated, which may be all that is needed.
  • Distinguish “cannot read” from “cannot run”. A device that enumerates on USB is running code, whatever a programmer reports.

The fourth item eliminates the most common false conclusion in this family: a device reported as dead because a programmer could not read it, when in fact it boots and waits for a USB connection that was not present.

Related reading

FAQ

Can a bootloader read a protected application? Normally yes, because code protection gates external reads while table-read protection gates reads from code, and the two are configured separately.

Does code protection stop USB enumeration? No. Enumeration is normal operation and does not require reading protected memory.

Is table-read protection defeated by a debugger? A debugger reading through the programmer interface is subject to the external gate. The internal gate applies to code that runs.

What can be learned from a locked USB device? Descriptors, strings, endpoint layout, protocol behaviour and timing — all without reading the array.

Does the bootloader remain usable on a locked part? Usually. Field update is the reason it exists, and blocking it would defeat the design.

Conclusion

The K50 family shows why “protected” needs a subject. Two gates, two audiences, four configurations, and a USB interface that leaks metadata regardless. Recognising the split turns a single opaque question into a short list of specific ones, most of which can be answered without touching the array.

Comments are closed.