189. The bootloader is a USB flash programmer, and it does test a key¶
Danny asked whether the firmware could settle what a recovery from a failed first write would entail. It can, and answering it refuted section 118 on the load bearing point. The recovery route exists, it is resident in internal flash on both bench architectures, it is entered from a key held at power on, and it does not depend on the application, the config or a host having anything to say. It does depend on external flash in one narrow way, per the gate below, and that dependency makes the guarantee stronger rather than weaker. What it cannot be shown to do is restore a config, and that limit is the useful half.
Section 118 was wrong, and two tests agreed with each other instead of with the image¶
Section 118 states, of the Harmony One's internal page: "The internal bootloader does not test a
key. Every port access in 0x000000 to 0x001000 is a write ... Zero reads." Section 87 states
the opposite of the same 4 KiB of the same file, that it "scans the keypad before anything else", which is section 87's own wording and is corrected there too. Both
carried a passing test, which is why this sat here unnoticed: docs/findings.md is the document that
has never drifted because every section in it carries a regression test, and here two sections carried
tests that could not see each other's claim.
Section 87's test asserts that two literals are compared and that one branch reaches 0x0100A. That is
true and it does not reach the word "keypad". Section 118's test scans for port reads and missed the one
that exists, twice over:
- its
READStuple is('BTFSS', 'BTFSC', 'MOVF'), and the instruction that reads the keypad is aMOVFF - it guards with
if ins.fields.get('a') != 0: continue, and aMOVFFhas noafield at all, soNone != 0is true and the instruction is discarded before the port check runs
The second is the transferable one. A guard written to select one addressing mode silently discards
every instruction that has no addressing mode, and the project's own pic18_trace.py does not have
this defect: its docstring says it sees banked accesses and MOVFF. The test hand rolled a scanner
instead of using it. There is exactly one port read in the Harmony One's bootloader,
MOVFF PORTB,gprF3B at 0x0095A, and it sits inside the routine the boot decision calls.
Section 118's other two measurements stand. The safe mode image above 0x1000 does read the matrix,
and no literal config base reaches TBLPTRU in the application image.
The boot decision, read through¶
Two things happen before the keypad is touched, which is why "scans the keypad before anything
else" is the wrong phrasing and section 87's too. 0x00062 to 0x0006C sets up the external memory
bus, and 0x0006E calls 0x000D8, which points the table pointer at external 0x020024, reads one
byte, and reports failure if that byte is 0x00 or 0xFF. Those are the two values blank or
zeroed flash returns, so the routine is asking whether external flash is programmed at all. Its caller
keeps the answer at 0xD06 and skips the keypad scan entirely when it failed, branching straight
to the validator.
So the key test is conditional on external flash being programmed. That is not a hole in the
recovery route, it is the opposite, and both arms are worth stating. With external flash intact,
which is every case after a config write since 0x020024 is in the application region and no rail
here permits writing there, the key test runs and a held key keeps the bootloader resident. With
external flash blank or zeroed, the key test is skipped and the path falls through the validator to
either the internal image or status 9, and status 9 lands in the USB loop. So a host can reach the
remote either way, and in the damaged case it does not need a key at all.
On the Harmony One, 0x00078 then calls 0x008D8, stores the byte it returns at 0xF2A, and compares
it against two literals:
the byte at 0xF2A |
what happens |
|---|---|
0x0E |
status 6, then fall through to the USB service loop and stay there |
0x1E |
GOTO 0x0100A immediately, without validating the image |
| anything else | call the validator at 0x001FE; if it passes, GOTO 0x0100A; if not, status 9 |
The validator is section 87's 48 47 header magic check at 0x001008, so the default path runs the
image above 0x1000 when that image is present. 0x1E therefore is not a synonym for the default: it
is a force the image to run even though its header does not validate, which is an affordance for a
partially damaged image and nothing else.
Every path that does not hand off converges on 0x000C4, from status 6, from status 9 and from two
further branches, and 0x000C4 clears TBLPTR and calls 0x00282 and 0x002E8. 0x002E8 writes
0x14 to UCFG and enters a three call loop at 0x002F2 that branches back to itself and never
returns. So the remote configures USB and services it forever. A Harmony One whose image above
0x1000 is destroyed still reaches that loop, by the status 9 route, which is the deepest recovery
guarantee in the part.
The keypad scan, and why a port scan found little¶
0x008D8 is a binary search over row groups. It drives eleven active low masks, 0x0F, 0x3F, 0x7F,
0xBF, 0xDF, 0xEF, 0xF3, 0xF7, 0xFB, 0xFD and 0xFE, through the helper at 0x0097E, and
composes a number 1 to 8 into 0xD02. The helper does not touch a port: it stores the mask, calls
0x007C8, which sets TBLPTR to 0x020020 and does a TBLWT*, then calls 0x007DE, which sets
TBLPTR to 0x020021 and does a TBLRD*. So the row drive and the column sense are reached over the
external memory bus rather than through the MCU's own ports, which is why counting PORTx accesses
understated the scan so badly. The one PORTB read at 0x0095A is additional and follows a CLRF of
0xF3C and a BCF INTCON,0.
What sits at 0x020020 and 0x020021 during the bootloader is not established here and must not be
guessed: those addresses are also where the application image lives once the external bus is configured
for the parallel NOR, and reconciling the two is a separate reading.
Twelve commands, on both architectures, from one source¶
The service loop's 0x00436 waits for a report on endpoint 1 and switches on its first byte with an
XORLW chain at 0x0047C, decoded through chains.py because the literals in such a chain are not its
case values. Twelve cases, 0x00 to 0x0A contiguous plus 0xFF, no duplicate:
| command | routine | what it does, mechanically |
|---|---|---|
0x00 |
0x00338 |
reply four bytes, the third and fourth being 0x00 and 0x02 |
0x01, 0x06 |
0x00342, flag 0xA5 clear |
read program memory through TBLRD into the reply |
0x09 |
0x00342, flag 0xA5 set |
read data memory through FSR into the reply |
0x02 |
0x00372, flag 0xA6 clear |
copy the request into the holding latches and commit |
0x08 |
0x00372, flag 0xA6 set |
the same, without the commit |
0x0A |
0x003AA |
write 64 bytes of 0xFF through TBLWT and commit |
0x03 |
0x003CA |
erase, refusing any address below 0x001000 |
0x04 |
0x003F8 |
disarm EECON1 and fill the reply with 0xBB |
0x05 |
0x0040E |
count the request's bytes and reply with the length |
0x07 |
none at all | reply one byte |
0xFF |
0x00524 |
disable USB, delay, then GOTO 0x0100A |
That last column says "mechanically" on purpose, and the row that forced it is 0x00.
One buffer carries both directions, and reading that backwards cost a table row. Endpoint 1 OUT's
buffer descriptor points at 0x0420 and so does endpoint 1 IN's, 0x40E and 0x40F holding 0x20 and
0x04, with the reply length copied into 0x40D from 0x060. So bytes 2 to 4 of that buffer are the
request's 24 bit address on the commands that take one and the reply's payload on the commands
that produce one. This table first read command 0x00's two stores into 0x422 and 0x423 as "set the
working address to 0x000200" when they are a reply being filled in, four bytes long,
which makes it a version or identity query rather than a setter. The direction was stated by the buffer
descriptors and was inferred from the arithmetic instead, which is the same family as inferring a
structure's form from its contents when a header states it.
The commit is the textbook PIC18 unlock, 0x55 and 0xAA into EECON2 then BSF EECON1,1, at
0x0032C, with a second copy at 0x00EE4 outside the command paths.
The bootloader is not Logitech's, on the evidence of its own descriptors¶
Both bootloaders carry USB descriptors of their own, inside the 4 KiB, at 0x0F6E on the Harmony One and
0x0EA6 on the Harmony 600, and the two are identical where it counts: 04D8:000B, bcdDevice
0x0000, device class zero, an 8 byte control endpoint, one configuration of 32 bytes at 100 mA, and
every string index zero, so no manufacturer, product or serial string at all. 0x04D8 is
Microchip's vendor id, not Logitech's 0x046D. The image above 0x1000 carries a separate block at
0x0BB66 which is 046D:C121 with the string Harmony Remote 4-3.4.0.
Two consequences, one immediate and one a hypothesis.
The immediate one is that the recovery state has a machine readable signature, which is what a bench
confirmation needs: a Harmony One in its bootloader enumerates as 04D8:000B with no product string,
where a normally booted one is 046D:C121 and names itself. That is decidable by listHarmony, which
enumerates and opens nothing, so it needs no command sent and no rail moved. It is also a better
observable than the screen, since the bootloader writes 13 ports in total and nothing here shows it
driving the display.
The hypothesis is that this is a stock Microchip HID bootloader rather than something Logitech wrote.
It would mean the command numbering is documented outside this project, which is an external cross-check
on the table above of exactly the kind this repository prizes and rarely gets. It also accounts for the
table's shape better than anything read here does: four of the twelve commands do nothing useful, and
neither bench part has data EEPROM or a separate configuration access path, so a stock bootloader's
commands for those would have nothing to do. That is a better account of 0x04 filling a reply with a
constant, 0x05 counting without touching anything, 0x07 having no routine at all, and 0x01 sharing
a routine with 0x06 than "four commands are stubs" is. It takes the ordinary route, decision 7:
nothing here has compared the numbering against Microchip's documentation, and until that happens the
table is read from the image and the names are not.
The Harmony 600's bootloader carries the same twelve commands, switch at 0x001A8, the same case
set with the same two commands sharing one routine, and its erase routine at 0x000F6 is the same
instruction sequence as the Harmony One's at 0x003CA with only the branch targets differing: the same
three byte compare against 0x001000, the same BNC past everything, the same MOVLW 0x14 into
EECON1. Two architectures agreeing instruction for instruction is one programmer compiled from one
source rather than two readings of one image.
The programmer protects itself, and that bound is the finding's best closure¶
Command 0x03 compares its 24 bit address against 0x001000 and returns without touching EECON1 when
the address is below it. 0x001000 is the end of the bootloader. So the erase command refuses to
erase the bootloader and there is no upper bound at all, which is the shape of a rail written by
somebody protecting the recovery path rather than the payload. It is also the reason the recovery route
can be trusted to survive its own use.
The register's bits are per part too, which this section got wrong within the hour¶
Each bootloader writes EECON1 seven times, and the values are the same in the same order on both:
0x04, 0x04, 0x14, a CLRF, 0xC4, 0x14, 0x04. On the PIC18F87J50 and PIC18F67J50, per
gputils' own headers, EECON1 is WR at bit 1, WREN at 2, WRERR at 3, FREE at 4 and WPROG at
5, with bits 6 and 7 unimplemented. So 0x04 is write enable, 0x14 adds the row erase bit, and
0x00 disarms.
0xC4 was written up here as reaching the configuration words and that is wrong. It sets bits 6
and 7, which do not exist on either bench part, so the value is WREN and nothing more. The reading
came from the generic PIC18 layout, where bit 6 is CFGS and bit 7 is EEPGD, and it would have been
right on the Harmony 525's PIC18F4550, whose header has exactly those two bits and no WPROG. So
this is the pitfall this project has recorded twice at the register level, one layer down at the bit
level: choosing the wrong part's bit names is as silent as choosing the wrong part's register map,
and it produced a claim about configuration memory out of a routine that writes ordinary program flash.
What actually distinguishes the unreferenced routine at 0x0041C from command 0x02 is not
established, and the honest statement is that it arms a write, commits through the same unlock, and
no command reaches it.
WPROG never appears, in either bootloader, which is worth stating because it is the bit that would
switch programming to one word at a time. Its absence means writes go through the holding latches a
block at a time, and that is coherent with command 0x08 existing at all: loading latches without
committing only means something if a commit programs more than the last word written.
What it means for a first write, and the limit that matters more¶
The favourable half. Recovery on a Harmony One is entered before the application runs, from a key held
at power on, needs no host software and no config, and lands in a USB service loop that also catches the
case where the image it would otherwise run is gone, and the case where external flash is blank. Contrast arch 9, section 118, where entering safe
mode copies an image over the application and is therefore a one way door: on arch 12 the bootloader
GOTOs a second image resident beside it and copies nothing, so entering recovery destroys nothing. The
Harmony 525's hazard does not transfer to the Harmony One, and this is the mechanism rather than an
analogy.
The limit, and it is a limit of the bootloader rather than of recovery, which is how this paragraph
should have read. Every write and erase path in the bootloader goes through EECON1 and EECON2,
the PIC18's internal self programming interface, and the config a first write would target sits in
external parallel NOR at 0x040000. Nothing in the bootloader issues a NOR command sequence, its only
two 0xAA literals being the second halves of the two EECON2 unlocks above, and an EECON1 commit
with TBLPTR outside internal flash is not a NOR write. So the bootloader is not shown to restore a
config and what it is shown to do is reinstall the internal image.
This said "this programmer is not shown to restore a config" as a claim about recovery, and section 191 refutes it at the level above. The safe mode image carries its own copy of a full external NOR programmer, 601 bytes byte identical to the resident library the application calls, with separate erase and program entry points and the AMD command set. So a Harmony One can write the flash a config lives in, and the level that cannot is this one.
Which leaves the actual reassurance in a different place than the question expected. A damaged config
does not prevent booting, because the bootloader and the image above 0x1000 are in different memory
from the config and a write into the config region cannot reach them. So the recovery from a bad config
write is that the remote still comes up and our own read and write path still works, and the bootloader's
programmer is the backstop one layer below that.
Which physical key carries 0x0E is still unanswered and still cannot be answered from firmware,
per section 48: a scan code does not yield a key on arch 12 because sixteen buttons share one sense line.
The published procedure in section 118 remains a third party claim about which key, and the cheap
confirmation remains read only hardware on the spare Harmony One.
Section 188 | the archive | section 190. Section 189 of the findings archive, which is frozen; the reference holds the current answer. The archive on GitHub.