PostHeaderIcon PIC18F14K50: What a USB Device Tells You Before You Read a Byte

A code-protection bit stops a programmer reading the array. It does not stop the device from doing its job, and for a USB peripheral the job begins with describing itself. Before any application-level exchange, the host asks for a device descriptor and the firmware answers with a structure that it must have been built to produce.

That answer is a statement about the firmware, made by the firmware, in a format defined by a standard. It is not protected, it cannot be protected, and on a locked device it is the first real evidence available.

The descriptor set, and what each part says

Enumeration proceeds in stages, and each stage yields something different:

Descriptor Contains What it narrows
Device Vendor and product ID, class, version Product family, vendor, firmware generation
Configuration Power, interfaces, endpoints Communication shape
String Manufacturer, product, serial Naming conventions, sometimes a build identifier
Endpoint Transfer type, size, interval Timing budget of the protocol

The vendor and product identifiers are usually the most specific. They are allocated, they appear in the vendor’s driver, and on a product that reused a development ID they can identify the design house rather than the product.

String descriptors are the ones people forget to look at. A product name string frequently contains a version, and a version identifies a build — which is the single most useful fact when the goal is to find the corresponding update package.

Why the protocol is recoverable

USB’s lower layers are fixed by the specification, so the only unknowns are the application protocol and the vendor-specific requests. Both are observable from the host side:

  1. Capture the bus during normal operation with the vendor’s own software running.
  2. Separate control transfers from bulk or interrupt traffic.
  3. Identify the vendor-specific requests by their request codes.
  4. Map each request to the response length and content.

The result is a protocol description, and a protocol description plus the descriptor set is most of a specification. What remains is the internal logic that produces the responses, which is precisely what the protection was for.

The 16K question

The array on this part is 16K, which is enough for a USB stack and a small application. That has a practical effect on reconstruction: a USB stack is largely boilerplate, and boilerplate is predictable. What is not boilerplate is the vendor’s logic between the endpoints.

An experienced reader can therefore estimate the proportion of the image that is stack and the proportion that is application, and the second proportion is small. On a part this size, the non-standard code is often a few kilobytes, and a few kilobytes of behaviour is characterisable.

What the protection configuration can still tell you

The configuration words are usually readable through the programmer even when the array is not, and on this family they answer specific questions:

  • whether code protection is on for the whole array or one block;
  • whether the boot block is separately table-read protected;
  • whether low-voltage programming is enabled, which decides how the device can be entered;
  • the oscillator selection, which tells you whether the board provides a crystal.

The third item is operationally important. A device with low-voltage programming disabled requires a high-voltage entry, which changes the hardware needed to talk to it at all. Discovering that after building a fixture is a waste of a week.

The bootloader, if there is one

Microchip’s USB stack includes a bootloader, and many products ship with it. A device with a working bootloader accepts an update over USB, which means:

  • a known-good image can be loaded without any programmer;
  • the bootloader’s existence is confirmed by its response to the entry sequence;
  • the update format reveals the memory layout the vendor used.

That last point is subtle and valuable. A bootloader that accepts records with addresses tells you the address map of the application it expects, and the map constrains where the application’s code and constants sit.

When the answer is “not this device”

Not every locked USB device is worth pursuing. Three conditions make it a poor use of effort:

  • The vendor’s driver is available and the protocol is documented, in which case a rebuild is straightforward and an extraction is not needed.
  • The product is one of many and a working unit exists, in which case duplication is the answer.
  • The proprietary logic is small and the value is in the hardware, in which case the firmware is not the asset.

Conversely, a device is worth pursuing when it is the only unit, its protocol is undocumented, and its function cannot be reproduced from behaviour. Those three together are the case where reading the array actually matters.

A short procedure

  1. Enumerate the device and record every descriptor verbatim.
  2. Capture traffic during normal operation and during a firmware update, if one is possible.
  3. Read the configuration words.
  4. Determine whether a bootloader is present and what format it accepts.
  5. Decide, on the evidence, whether the remaining unknown is worth the cost of extraction.

Step five is the one that gets skipped, usually because steps one to four are interesting enough to continue past the point where the decision should have been made.

Related reading

FAQ

Can descriptors be suppressed on a locked device? No. Enumeration is required for the device to function as a USB peripheral.

Are string descriptors always present? No, but when they are they frequently carry a product name and a version.

Is the USB protocol enough to rebuild the firmware? It is enough to rebuild a device that behaves identically at the interface. The internal logic remains unknown.

Does code protection affect the bootloader? It can, depending on configuration. The boot block is often left writable so that updates remain possible.

How do I know low-voltage programming is enabled? From the configuration word. It determines which entry method the device accepts.

Conclusion

A USB peripheral describes itself as a condition of working, and on a protected device that description is the cheapest information available. Descriptors, strings, endpoints and captured traffic together constrain the firmware to a small space — often small enough that the remaining question is whether extraction is worth doing at all.

Comments are closed.