Skip to content

19. The USB command layer, and a switch that lies about its case values

The protocol itself lives in docs/usb-protocol.md, which is the spec other tools consume. This section is the reasoning: how it was found, and the two places it could have been read wrongly.

Route in

The USB registers on this family are at 0xF4C to 0xF65, which nothing knew until section 18. Tracing them split the driver into two clusters immediately: 0x16E00 to 0x17300 writes UCFG, UEP1, UEP2 and sets USBEN, so initialisation and attach, and 0x1AD80 to 0x1AF00 reads UIR eight times with a bit test after each, so the interrupt service.

Neither leads to the protocol, because the report buffers live in the buffer descriptor table and are only ever reached through FSR, which pic18_trace.py cannot see. What worked was going at the accessors: the buffer descriptor registers are ordinary banked addresses, so tracing 0x410 to 0x413 found the four instructions that hand endpoint 2 OUT to the hardware, and reading the literals off them gave the buffer address, 0x0428. Then pic18_xref.py, written for this, walked callers from the accessor that returns that address up to the command entry point at 0x0BD0A.

That tool did not exist before this section. pic18_trace.py answers "what touches this variable"; there was no way to ask "what calls this routine", which is the question that connects a generated helper to the logic that uses it.

The switch that lies

The command table is a chain of XORLW comparisons, which is how this compiler emits a switch on a processor with no compare-against-literal instruction. The literals are not the case values. Each one is the XOR of the previous case with the next, so the case value is the running XOR of every literal so far:

0bdcc: MOVF  0xd00,W
0bdce: XORLW 0x05    ; case 0x05
0bdd0: BNZ   0x0bdd4
0bdd2: BRA   0x0c500
0bdd4: XORLW 0xb5    ; 0x05 ^ 0xb5 = 0xb0, so case 0xb0, not case 0xb5
0bdd6: BNZ   0x0bdda
0bdd8: BRA   0x0c4ce

Read as case values, the literals give 0x05, 0xB5, 0x10, 0x70, 0xA0, 0x20, 0x60, 0x20. Read correctly, they give 0x05, 0xB0, 0xA0, 0xD0, 0x70, 0x50, 0x30, 0x10, which are seven known command bytes plus one internal case. The wrong reading contains a duplicate, 0x20 twice, which is the tell: a switch cannot have two identical cases. That is the only warning the wrong reading gives, and it would be easy to miss.

So the chain is decoded programmatically in harmony/pic18/chains.py rather than by hand, for the same reason there is one opcode table. The remaining MISC sub-command chains will go through it too.

What closes the reading

Three things, none of which the derivation was aimed at.

The length nibble ends exactly at the report size. Nibbles 0 to 7 mean 0 to 7 payload bytes, and 8, 9 and 0xA mean 15, 31 and 63. Those are 2^4-1, 2^5-1 and 2^6-1, and 63 payload bytes plus the one command byte is 64, which is the report size the USB descriptors declare in section 1 of the protocol document. Two independent parts of the firmware agreeing on 64.

The state numbers are identical across both architectures while the state variable is not. Every command handler opens by assigning a state and then just parses its arguments, leaving the work to the main loop. WRITE_FLASH is 2, READ_FLASH 4, START_IRCAP 5, ERASE_FLASH 8, WRITE_MISC 9, READ_MISC 10, GET_VERSION 1, in the 700, the 600 and the One alike. The variable holding it is at 0xEC9, 0x1C1 and 0x284 respectively. Same protocol implementation compiled for three different memory maps, rather than three implementations that happen to agree.

Endpoint 1 OUT is absent three times over. The endpoint descriptors declare IN on endpoint 1 and OUT on endpoint 2. UEP1 = 0x1A has EPOUTEN clear. And the buffer descriptor for endpoint 1 OUT, at 0x408, has no direct access anywhere in the image, while its three neighbours at 0x40C, 0x410 and 0x400 have between them dozens. Indirect access through FSR would be invisible, so that third point is weaker than the other two on its own; taken with them it is confirmation. Three independent statements of the same asymmetry, which matters because a host that assumes endpoint 1 in both directions will not talk to the remote at all.

The state gate, and why it is a safety property

The dispatch is itself gated on the state, so the table above is the idle table. In state 2 a different two entry chain runs, handling 0x40 and 0xF0, and 0x40 is WRITE_FLASH_DATA. WRITE_FLASH sets state 2 as its first instruction.

So the firmware accepts flash data only after it has agreed to a write. That is worth stating plainly because the write rails in CLAUDE.md are host side, and it is useful to know which of them the device also enforces. This one it does. It does not follow that any other rail is enforced, and nothing here has been tested against hardware.

A prediction, then a measurement

The transport section of docs/usb-protocol.md said, before anything was checked, that the Harmony 600 should report bcdDevice 0x1071. The reasoning was two images, 0x1054 on a remote known to be skin 54 and 0x1066 on one known to be skin 66, so the low byte read as BCD is the skin, so the 600 at skin 71 should say 0x1071. Written down as a prediction because the 600's own dump is truncated before its descriptor block and could not settle it.

The remote was then plugged in and enumerated. bcdDevice 4209, which is 0x1071.

Everything else agreed too: product 0xC122, shared with the 700 as claimed, one HID interface with two endpoints, 64 byte input and output reports, a 1 ms interval, full speed, bMaxPacketSize0 8, and the string Harmony Remote 0-0.2.0 in the same shape as the other two with this remote's firmware version in it.

The most useful part was not the prediction. The 33 byte HID report descriptor came back byte for byte identical to the 700 image's, 81 06 included, where the arch 12 image has 81 02. That one flag difference was recorded earlier as a curiosity and a fingerprint; it is now known to track the architecture rather than the model or the firmware version, because a third arch 14 device with a wildly different firmware version, 0.2 against 2.8, sides with the other arch 14 image. It also means the 700, which is not on the bench, is a sound reading proxy for the 600 at least this far.

The endpoint descriptors needed a second tool, because ioreg does not report them. pyusb does, still without opening the device: libusb caches a device's descriptors when it enumerates. IN on endpoint 1, OUT on endpoint 2, 64 bytes, 1 ms, measured, plus bmAttributes 0xC0 and 100 mA off the configuration descriptor. Field for field the image's 07 05 81 03 40 00 01 and 07 05 02 03 40 00 01. The asymmetry was the one transport detail a host implementation is most likely to get wrong, and it is no longer taken on the word of a remote nobody here owns.

Recorded as hardware measurement in an otherwise offline document, and pinned in tests/test_usbdesc.py so the images and the measurements cannot drift apart silently.

Two lessons came out of the method rather than the result, and both are now in the probe-remote skill so they are not rediscovered. system_profiler SPUSBDataType returns nothing at all on this machine while exiting 0, which produced a confident false negative: a watcher polling it reported no remote for six minutes while the remote was plugged in. And the useful measurements are all available without opening the device, which is what makes them safe to take at all while the project is read only.

READ_FLASH, and a lead that survived

The previous version of this section recorded the READ_FLASH argument layout as an unconfirmed lead: bytes 1 to 3 a 24-bit address and bytes 4 and 5 a 16-bit count, with the top address byte doubling as a region selector. Following the switch confirmed it, and the confirmation is the kind worth preferring, because the firmware demonstrates the meaning rather than being consistent with it.

The three address bytes are copied into TBLPTRL, TBLPTRH and TBLPTRU. That fixes two things: the bytes are an address, and the wire order is most significant first.

Correction, made one commit later. The paragraph above originally said a third thing, that the target is the external config flash, because the TBLPTR load sits between BCF LATF,7 and BSF LATF,7 and LATF bit 7 is the flash chip select from section 13. The chip select bracket is real. Attributing it to READ_FLASH was not checked, and it does not survive checking: the routine it lives in, 0x13E90, has one caller, behind a flag test, and its other branch calls 0x1B50A, which sets EECON1 to FREE | WREN. Those are the internal flash erase enable and write enable bits. A routine whose two branches are a read and an erase is not a read path, so 0x13E90 is more likely the erase or write machinery, which on arch 14 has to prepare internal flash because the config is copied there to run.

The same correction takes the response chunking with it. A 16-bit remaining count is compared against 0x3F and data moves in 63 byte pieces, on the same variable pair READ_FLASH parses its last two bytes into, so those bytes being a count is a strong inference. But which command reaches that code was never established, and calling it READ_FLASH's response was the same mistake twice in one commit.

What the mistake was, precisely: following variables instead of following control flow. The argument variables are shared between commands, so finding code that uses them proves what the variables are and nothing about which command is running. The request side is safe because the parser is reached from the dispatch table, so its ownership is known.

Doing it the right way, immediately afterwards

Starting from the state machine instead found it. State 4 is not compared anywhere with the SUBWF form most of the machine uses, so it took a second look: it is a case in an XORLW chain at 0x0D388. That chain's seven cases are 2, 4, 5, 6, 0x0B, 0x20 and 0x35, which are plausible small state values, and that plausibility check is the whole reason to trust this decode when the 32 case result elsewhere in the same firmware is not trusted.

State 4 goes to 0x0D3A8, and the body is READ_FLASH's per chunk step. It ORs the two count bytes together to test for zero, clears the state variable and returns when they reach it, and otherwise subtracts the chunk size from them as a 16-bit quantity. So bytes 4 and 5 of the request are a 16-bit count, established from the dispatch this time rather than from a shared variable, and the command completes by returning the state machine to idle.

The chunk size comes out of 0xED3, which is the same variable READ_MISC parses its item selector into. So the sharing that caused the retraction is not hypothetical, it is confirmed in the very next routine.

And then the state machine turned out to be one table

Deriving the state dispatch properly settled the rest of it, and revealed that a claim two paragraphs up was made too early. The main loop's dispatch is one XORLW chain of 70 cases from 0x0C720 to 0x0C8FE, values 0x01 to 0xD6, all distinct, reaching 31 bodies. States 2 and 0x0B are special cased with ordinary comparisons just before it.

Asked for that chain earlier, the decoder returned 32 cases and I wrote that up as the tool over-running into unrelated code and declared the result untrustworthy. It was the default limit of 32. The chain is real and continuous, the values are coherent, and the honest version is that I mistook my own truncation for a defect in the analysis. The docstring in chains.py now says so, along with the second trap the same episode exposed: a case value depends on where the walk starts, so beginning one comparison too late shifts every value and both readings can look plausible.

With the table in hand:

  • every command state has an executor, and 0x0C982 is READ_FLASH's
  • the 63 byte chunking is READ_FLASH's after all. 0x0C9B2 is reached from 0x0C988, two instructions after the state 4 body. So that attribution is restored, this time by control flow from the dispatch rather than by finding code touching the same variables
  • state 4 also has a second dispatch site, the seven case chain at 0x0D388, which sends it to 0x0D3A8. Both are real. That 0x0C982 starts a chunk and 0x0D3A8 finishes one is what the two bodies suggest, and it is inference

Live RAM over USB, and a number worth not adopting

READ_MISC's executor is state 10 at 0x0CB92. It replies 0xC2, echoes the selector, then the data, and dispatches on the selector to exactly four bodies: 0x01, 0x06, 0x07 and 0x0C.

0xC2 is itself a small finding: responses reuse the request encoding, a code in the high nibble and a payload length in the low one, so 0xC2 means two payload bytes, the selector and one data byte. WRITE_MISC replies 0xF0 then 0xA0, a bare acknowledgement naming what it acknowledges.

Selector 0x07:

0cbf4: ce ce e9 ff MOVFF 0xece,FSR0L
0cbf8: cf ce ea ff MOVFF 0xecf,FSR0H
0cbfc: ef cf 64 fd MOVFF INDF0,0xd64

The 16-bit parameter becomes FSR0 and the byte at that data address comes back. Live RAM of a running remote is readable over USB. That is the capability the roadmap wants in place of the deferred emulator, and it makes the button mapping experiment reachable: poll the keypad scanner's index variable while pressing every key.

And it is 0x07, not the 0x06 libconcord's header calls MISC_RAM. On arch 14, 0x06 is a different accessor, going through 0x1AB8A. Whether the upstream number is right for the architecture it was written against is not established here. This is the clearest return the project's "derive rather than adopt" rule has paid: 0x06 taken on faith would have read the wrong thing and still returned a plausible byte, which is the failure mode that does not announce itself.

The rest of the layouts, and no event injection

WRITE_MISC's selector chain turned out to be at parse time, 0x0C3AA, which is why the executor looked empty: it only acknowledges. Nine selectors, 0x01, 0x02, 0x05 to 0x0B, and three of them close open questions.

0x07 writes an arbitrary data address, exactly mirroring the read: MOVFF 0xd5e,FSR0L, MOVFF 0xd5f,FSR0H, MOVFF 0xd61,INDF0. Volatile, so it cannot brick a remote, but it is a write to a live device and it is now in the safety rails in docs/plans/002-the-roadmap.md and CLAUDE.md rather than in the toolkit.

0x09 sets the packet-handled flag and branches out, doing nothing. 0x03 is not in the chain at all. libconcord names those MISC_QUEUE_EVENT and MISC_QUEUE_ACTION, so on that naming there is no event injection on arch 14: driving the remote from the host is not available and the button mapping experiment stays a human at the keypad, which is what the roadmap assumed. The caveat is the same one this whole section keeps earning: the names are upstream's, and upstream's MISC_RAM 0x06 was wrong here. What the image establishes is that 0x09 is a no-op and 0x03 is unhandled.

Corrected by section 326: a host can queue an action instruction on arch 14, by another command. Command state 0x34, selected by a version request with a payload, section 304, reads three bytes and pushes them onto the action queue through the routine the configuration's own instructions take, on the Harmony 600 and 650 0.2, the 650 0.4 and the 700 2.5 and 2.8. So "driving the remote from the host is not available" was wrong for actions: what is unhandled is WRITE_MISC selector 3, not action queueing. How the error came about: the conclusion was drawn from one command, under upstream's name for the selector, and the sentence generalised a missing selector into a missing capability without looking for another command that reaches the queue; the version request's payload states were not read until section 304, and 0x34 not until section 326. Command state 0xB1, which writes any data byte, is a second host route to the same effect. Queueing a key event is still unfound. Read, never sent.

The remaining request layouts came out of one derivation, each parser bounded by the next parser's entry address:

Command Bytes Layout
0x30 WRITE_FLASH 5 24-bit address, 16-bit count, identical to READ_FLASH
0x70 START_IRCAP 0 no arguments
0xA0 WRITE_MISC 5 selector, 16-bit address, 16-bit value
0xD0 ERASE_FLASH 3 24-bit address, no count

The first version of that scan had no bound and ran each handler into the next, reporting eight argument bytes for READ_FLASH where five had already been derived by hand. The five were right. Catching it took comparing a scan against a hand reading of the same code, which is the argument for doing one of each rather than trusting either alone.

Two of those rows have consequences beyond the protocol. WRITE_FLASH and READ_FLASH share their encoding and their validator, so region rules apply to writes as well as reads. And ERASE_FLASH takes no count, so an erase cannot be scoped by the caller and the only available bound is refusing the address.

63 matching the largest payload the length nibble can describe survives as an agreement between two parts of the firmware. What does not survive is calling it the fourth confirmation of the 64 byte report, since the third and fourth were the same observation counted twice.

Byte 1 is also validated, and that is where the regions turned out to live. Below 0x20 is an ordinary flash address. 0xFE and 0xFF select something else, with the low bit kept as a sub-selector and the remaining 16 bits bounded to 0xFFC0, exactly 64 short of the end of a 64 KiB window. Anything else is refused.

What that does not settle, and the distinction matters because docs/plans/002-the-roadmap.md asks the question in terms of four named regions: one sub-selector bit gives two regions, not four, and the routine those reads go to has not been read. So the shape of the region mechanism is established and the mapping onto MCU_FLASH, MCU_EEPROM, MCU_ID and EXT_FLASH is not. Either not all four are reachable on arch 14, or a region is selected some other way as well.

The config validator, and three searches that failed first

Roadmap step 3 asks for the boot-time config validator to be located, not derived, because the trailer checksum lives there and is wanted in step 6. It is found, in both images:

cookie check end marker check
Harmony 700 2.8 0x16492 0x1652C
Harmony One 3.4 0x28DAC 0x28E18
16492: 47 0e       MOVLW 0x47        ; 'G'
16496: 00 19       XORWF 0xd00,W
16498: 08 e1       BNZ 0x164aa       ; bail out on the first mismatch
1649a: 53 0e       MOVLW 0x53        ; 'S'
164a0: 50 0e       MOVLW 0x50        ; 'P'
164a6: 4d 0e       MOVLW 0x4d        ; 'M'
164ac: 07 d1       BRA 0x166bc       ; rejected

Worth recording how it was found, because three earlier searches came back empty and each empty result was itself a fact:

  1. The markers are not in either image as text. GSPM, PTYY and WLWL are all absent as ASCII. So the obvious search finds nothing, and the compiler has instead emitted them as four consecutive MOVLW instructions, which is what the successful search looked for.
  2. There is no 16-bit accumulate anywhere near a config byte read. The whole 700 image contains exactly one ADDWF followed by ADDWFC, and it is nowhere near the config accessors. So whatever the trailer checksum is, it is not a plain 16-bit sum accumulated that way.
  3. Broadening to XORWF and to ADDWF with a separate carry propagate gave 27 and 14 candidates near a config read, none distinctive, and most of the XORWF hits are comparisons rather than accumulation. Not resolved, and not resolvable by pattern alone.

Neither validator address has a direct caller, so both are reached by a computed jump, which is why walking the call graph would not have found them either.

The checksum algorithm is not derived, and the second search above is a useful constraint on what it can be rather than a step towards it. Step 6 starts from the code around these two addresses.

What the first hardware run changed

Three commands have now been sent to the programmed Harmony 600 from our own host code, read only. The layouts are in docs/usb-protocol.md section 4; what belongs here is what the run says about the method.

The strongest result is the one with an external answer. 256 bytes read from flash 0x030000 are byte-identical to the lab dump of that same unit, which concordance made months earlier. A read that returns plausible bytes proves nothing at all, and almost every mistake available here returns plausible bytes: a wrong length nibble, a byte-swapped address, an off-by-one chunk boundary. Only a comparison against an independently obtained answer separates those from a correct read.

Two things this project had inferred were wrong, and both were wrong in the same way. An acknowledgement's length nibble is 0 while its command byte follows it anyway, and a data chunk's first payload byte is a sequence number rather than data. In both cases a test existed and passed, because the test encoded the same assumption as the code. That is the failure mode of testing against a document rather than a device, and it is worth stating plainly: the tests were not weak, they were circular. The tell was available in advance, though, and was written down: the first version of the chunk test used code 0xFA, which decodes as an acknowledgement, and choosing an arbitrary value for an unestablished field is itself the warning.

One result could not have been faked. READ_MISC selector 0x07 reading data address 0x1C1 returns 10, and 10 is the state that READ_MISC itself sets. So the read observes the command performing the read, at an address predicted from the disassembly, holding the value the disassembly says it holds at that instant. Compare the alternative evidence for "this is a RAM read", which would have been that the byte looked reasonable.

And the same address through selector 0x06 returns 0. libconcord's header calls 0x06 MISC_RAM. Both selectors are serviced and they are not the same accessor, so adopting the upstream number would have produced a read of the wrong thing that still returned a plausible byte, which is precisely the case the project's derive-rather-than-adopt rule exists for. This is the first time that rule has been vindicated by measurement rather than by argument.

GET_VERSION's twelve bytes are six identified, by prediction rather than by fitting. Comparing the 600's block against concordance -i gave a reading of six fields. That reading was written down as a prediction for the Harmony One before the One was read, and the One's first six bytes came back exactly as predicted, on a remote differing in skin, firmware, hardware version, flash part and architecture. The flash id pair and the packed nibble shape were already characterised from the image, so two of the six are agreements between a disassembly and a device.

Six fields remain, and what is said about them is deliberately weaker than what is said about the first six. Field 6 is 0x0C on both remotes, so it is a constant. Fields 7, 10 and 11 repeat field 0 on both, which is an observation and not a reading: three copies of the firmware version is a strange thing to carry, and other components' version numbers happening to match on both units is the more likely explanation. Fields 8 and 9 are unexplained.

Two negative results, which are worth as much as the positives here. A read of internal program memory reaches the first 64 KiB only, because the top address byte is spent on the region selector and the firmware bounds the rest to 0xFFC0, while a PIC18 keeps its device id at 0x3FFFFE. So the route to MCU_ID that docs/plans/002-the-roadmap.md wanted does not exist, and the arch 12 part number stays inferred until another route is found. And a 63 byte read of that same region restarted a remote: it left the USB bus, re-enumerated by itself, and came back healthy with its config still byte-identical to its dump. The owner watched it restart, so it is the device resetting rather than a host artefact. Every command in that session was a read.

That one was then reproduced on purpose, on the spare unprogrammed unit, because "sometimes it restarts" is not a finding and a rail built on it would be superstition. Five restarts, all self-recovering, config verified against the dump across three windows afterwards. The table is in docs/usb-protocol.md section 4; what it rules out is more useful than what it shows. Not the ordering, since the failing case fails last as readily as first. Not the chunk count, since 124 bytes is two chunks and is fine. Not the size 63 alone, since 63 at offset zero is fine. What is left is a final chunk of exactly one byte, with offset zero somehow exempt, and that is where it was left.

~~packages/usb caps an internal read at one chunk~~, which was a bound around the hazard rather than the hazard itself. It refuses the measured condition instead since 9 August 2026, count % 62 == 1, which is section 93. Still a workaround and not an explanation.

An honest gap

The four sub-commands of the 0xE0 escape are 0x01, 0x02, 0x03 and 0x05. What each does is not established beyond the first few instructions of each path, and the 0x05 one is the odd one, since it feeds the internal case of the main table rather than doing anything itself. The 0x05 case is present in both arch 14 images and absent from the arch 12 one, which tracks the architecture rather than the firmware version, the 600 being 0.2 against the 700's 2.8. No explanation is offered for that yet.


Section 18 | the archive | section 20. Section 19 of the findings archive, which is frozen; the reference holds the current answer. The archive on GitHub.