BlogPart 2 · the power cut

Turning a 2003 Palm into a universal remote, after a power cut

Part two: the new battery goes in, the T3 forgets everything, and IR Knife comes back from the SD card broken. Four wrong theories, a bisect over HotSync, and a bug that had been waiting for a power cut since the day it was written.

Part one ended with a safety net. My Tungsten T3 had no battery and ran from its cradle, so any power cut would wipe its RAM, and with it the app and its code databases. Copies of both sat on the SD card, and the app knew how to import its databases from there.

The replacement battery arrived. Swapping it meant unplugging the T3, the RAM was wiped as expected, and I tapped IR Knife in the launcher’s card view. The screen said Importing codes from card…, and then everything was wrong:

  • The category list was empty.
  • Search still found remotes, but opening one drew the screen twice, a smaller copy over a full-size one.
  • Pressing a button did nothing. No error, and the IR LED stayed dark.

The T3 showing an Airwell remote drawn twice: a shrunken copy of the buttons and tabs over a full-size one

The Airwell remote after the power cut. The Main and All tabs appear twice.

It had all worked before the battery swap. Nothing in the code had changed. So the obvious conclusion was that the power cut had changed something on the T3, and that conclusion was wrong.

Everything a reset takes with it

A Palm keeps everything in RAM: apps, databases, system preferences, and any add-ons installed on top of the OS. One thing was clear early: search went through the same category table as the empty list, so the codes database had imported correctly. That left the T3 itself, and there was no shortage of suspects:

  • A lost system add-on. The T3 has no PINS, the standard API for resizing apps around the writing area. palmOne released a compatibility update that adds it, and it lives in RAM. The app’s Display info screen said PINS: no, and reported an input-area state of 4, which the code had never seen. It turned out to mean “slider closed”. Slider open, it read 2, and the screen sizes were exactly what they should be.
  • Beam Receive. A reset restores every system preference, and if Beam Receive came back on, the OS could hold the IR port. The app opens that port to power the transceiver and, as I found while reading the code, carries on silently if the open fails. Turning Beam Receive off changed nothing. The silent failure is still worth fixing.
  • Running from the card. An app launched from the card view is copied into RAM fresh on every launch, and freshly copied ARM code had caused cache crashes before. I installed the app into RAM over HotSync instead. Still broken.
  • A clean slate. I deleted the app and its database from the T3 and installed both again, straight from the build. Still broken. The only thing left over was a system entry named after the app that the T3 refused to delete.

Every one of these assumed the same thing: that the app on the T3 was the app that had worked.

The assumption

It was not. The shell history and the file dates told the story in one line each:

  • v0.17 was built at 22:52 on the last evening of part one.
  • Two minutes later it was copied to the SD card, and only to the SD card.

The T3’s RAM still held the build before it, and that is what I had been using since. “It worked before the power cut” was true, but it described a different binary. The power cut was simply the first time v0.17 ever ran.

Bisecting over HotSync

The repository had the answer. The last eight commits before v0.17 had gone in within about thirty minutes: double-buffered drawing, a scroll bar, input-area fixes, and v0.17 itself with favorites and settings on the SD card. Nothing recorded which of them had ever run on the T3.

So I bisected on the device. Each step was: check out an old commit, build it, install it over HotSync, launch it, open the Airwell remote, press a button.

Build What it added On the T3
a38b416 the Haier AC module, the last commit before the burst works
5f9ef9b label fitting, redrawing only the touched key works
542cdcb double-buffered drawing works
156d678 favorites, settings on the card, two code segments (v0.17) broken

I had been sure it was the double buffering, because a screen drawn twice looks exactly like an offscreen buffer copied to the wrong place. It was fine. Every symptom came from the last commit, and from one line in its message: split code into two segments (over the 32 KB limit).

Two segments, one address space

A 68k Palm app’s code lives in resources, and the compiler reaches functions with relative branches that reach 32 KB. v0.17 had grown past that, so the build split the code in two: the main segment, and a second one called seg2 holding the AC screen, the AC modules and the IR renderer.

prc-tools has a proper way to do this: each function is tagged in the source with the section it belongs in, so the compiler knows which calls cross between segments. My build skipped that. It compiled everything as one segment and then renamed the chosen files’ code to seg2 afterwards. The compiler never knew anything had moved.

The disassembly makes it plain. This is the browse screen building the category list:

000016ec  6100 f8da   bsr.w   0x0fc8 <ac_module_count>

ac_module_count is at 0x0fc8, in seg2. A bsr.w is relative to where it is, so it jumps to 0x0fc8 in the main segment, which is the end of the database code. Both segments start at address zero, so the name the disassembler prints is the right function in the wrong place.

A call meant for the second segment lands in the first Both code segments start at address zero. The category list code at 0x16ec in segment 1 branches to 0x0fc8, meaning ac_module_count in seg2, but a relative branch stays in its own segment and lands at 0x0fc8 in segment 1, inside unrelated database code. Segment 1 app code, starts at 0x0000 0x0fc8 end of the database code 0x16ec · category list bsr.w 0x0fc8 where it lands seg2 AC and IR code, also starts at 0x0000 0x0fc8 · ac_module_count where it was meant to go

Both segments start at zero, so every call between them is off by a whole segment.

Every call between the two segments did this, in both directions. The category list asked a piece of database code how many AC modules there were. The remote screen’s call to the IR renderer went somewhere else too, and nothing reached the LED. The AC screen’s calls into the drawing code went astray the same way. None of it crashed outright, which is why it looked like a configuration problem and not a broken build.

The fix

The better split was in the code all along. The AC protocol modules (Gree, Coolix and Haier, plus Gree’s checksum helpers) are plain C89 and call nothing outside themselves. The rest of the app only ever reaches them through function pointers in each module’s descriptor, and a function pointer is an absolute address, which works across segments. So seg2 now holds exactly those four files, and the main segment drops to 27,972 bytes, comfortably under the limit.

The build also checks it now. Before linking, it lists every function each segment defines and every function each segment calls, and refuses to link if they cross. Pointed at the old split, it fails like this:

Direct calls across code segments:
AcHandleEvent
ac_module
ac_module_count
AcShow
ir_decode_raw
ir_render_parsed
BufferBegin
...
UiSetInputArea

Twenty-four calls in all. With the new split the list is empty, and on the T3 the categories came back, the remote screens drew once, and the air conditioner turned on.

The Airwell remote drawn correctly

The same remote with the fixed build, drawn once.

What I took from it

  • “It worked before” names a device, not a build. The first thing to check after a mystery regression is which binary was running when it last worked. Here it was not the one I was debugging.
  • The card copy is the real install. On a device that forgets its RAM, whatever sits on the card is what runs after the next power cut. Updating the card is now part of every install.
  • Four builds beat four theories. Each theory above was reasonable, and each one cost more time than the whole bisect.

The T3 has its new battery, the card holds the fixed build, and the safety net from part one is finally doing its job.

← All posts