At 3:05 on a Monday morning my parents' TV box restarted and never came back. The screen showed the boot animation I had made for them the day before, a namaste with their names on it, and three dots that kept pulsing. It was still pulsing at noon.
The box was fine at midnight. The only thing that had changed was me. The day before,
my coding agent and I had spent hours turning an ISP-issued IPTV box into something
two elderly people could use with one remote: a big Nepali home screen, ad blocking,
a phone remote, remote access over Tailscale. The last step was a tidy-up. Over
adb, three built-in apps that looked useless were switched off with
pm disable-user. One of them was com.android.statementservice.
This post is what that one command does to Android 7, every way out that turned out to be closed, the one that wasn't, and what is on the box now so it cannot happen again. Total hardware spend for the fix: zero. Total spend on things I bought before I had done the research: one USB A-to-A cable that was never going to work.
This is part two of a series. Part one is the home screen we built that day; part three is the camera and talk button that came after; and the rules I now give a coding agent are what all three taught me.
The box
| SoC | Amlogic S905X (p212 reference board) |
| OS | Android 7.1.2, a userdebug build |
| Inputs | One HDMI, Ethernet, two USB-A sockets, microSD, a latching power switch |
| Not present | A reset button, a recovery key combination that the firmware honours, any labelled debug header |
userdebug matters later. Remember it.
What one switched-off app does to Android 7
During boot, system_server starts PackageManagerService, which reads its
settings and then looks up a handful of components the platform cannot run without.
One of them is the intent-filter verifier, the app that checks App Links. In
AOSP 7.1.2 the lookup ends like this:
private ComponentName getIntentFilterVerifierComponentNameLPr() {
...
throw new RuntimeException("There must be at least one intent filter verifier");
}
(source)
On this box the only verifier is com.android.statementservice. Disabled, it no longer
counts. The exception is thrown inside system_server's constructor, system_server
dies, init restarts zygote, zygote forks a new system_server, and it reads the same
settings file again.
- 01
init starts zygote
- 02
zygote forks system_server
- 03
PackageManager reads its settings
- 04
It looks for the intent-filter verifier
- 05
Boot completes
- system_server restarts
- 0
- on the TV
- boot animation
init starts zygote. The kernel is up, /data is mounted, init launches the Java world.
Two details turn this from a crash into a trap:
- The state lives in
/data.pm disable-userwritesenabled="3"into/data/system/users/0/package-restrictions.xml. A firmware update replaces/system; it does not touch that file. Reflashing fixes it only by wiping/data, which is everything we had built. - Networking is a Java service. On Android 7, DHCP for Ethernet runs inside
system_server. Asystem_serverthat dies after two seconds never asks for an IP address, soadbover the network, the way we had controlled the box all day, is gone too.
Every door, and why it was closed
I am including the failures because they are most of the work, and because the first of them cost money.
| Route | What I tried | Why it is closed on this box |
|---|---|---|
| adb over Ethernet | Router ARP sweep, Tailscale, IPv6 link-local, then a cable straight into the laptop with tcpdump | Zero packets from the box in 45 s. No DHCP, no router solicitation. The network stack never starts |
| adb over USB | A real A-to-A cable, both sockets, every power-cycle order | The device tree gives both sockets to the dwc3 host controller. The device-mode controller exists, but its driver refuses to bind: host only, not probe usb_otg (driver source) |
| Recovery menu | Every remote, keyboard and power-switch combination I could find | No reset button, no pads for one, and the bootloader that decides is encrypted, so I could not read what it waits for |
| Boot from SD or USB | Amlogic BootROM order | The BootROM only falls through to SD or USB if eMMC is unbootable or a hardware strap says so. Neither is reachable from outside |
| Factory reset / reflash | Would fix it, by deleting everything we had set up. Kept as the last resort |
The USB row is the one I got wrong. I recommended buying the cable before I had read the device tree. Ten minutes in the firmware image would have shown that USB device mode is not wired to any socket. Research before spending; I wrote that down as a rule for the agent too.
The open door: userdebug
Inside the boot image's ramdisk, init.rc has this:
on property:ro.debuggable=1
start console
service console /system/bin/sh
class core
console
On a userdebug build, init runs a shell on the serial console, in class core, which
starts long before system_server and does not care whether it keeps dying.
/system/xbin/su is present. If I could reach the serial pins, I would have a root
shell on a box that looked dead.
There was no labelled header, but on the underside of the board, between the USB sockets and the SoC, was a row of four pads with the first one square. That is the usual Amlogic layout: ground, TX, RX, 3.3 V.
I did not own a USB-to-serial adapter. I did own an Arduino Uno clone, and an Uno is a USB-to-serial adapter with a microcontroller attached. Hold the microcontroller in reset (I taped the button down) and its pins go high-impedance, so the board's USB bridge listens straight to pin 0 and 1. Ground to the square pad, the Arduino's TX pin to pad 2, 115200 baud, and the laptop filled with kernel log:
init: couldn't write 14563 to /dev/cpuset/camera-daemon/tasks
binder: transaction failed 29189
init: couldn't write 14833 to /dev/cpuset/camera-daemon/tasks
cameraserver and audioserver restarting every few seconds. That is what a
system_server crash loop looks like from the kernel's side. Pad 2 was the box's TX,
and the box was alive in there.
Typing into a 3.3 V pin with a 5 V board and no resistors
Reading is safe. Writing is not. The Uno's pins switch between 0 V and 5 V; the SoC's pins are rated for 3.3 V. The usual fix is a two-resistor divider. I had two jumper wires and no resistors.
So the Arduino never drives the line high at all. To send a 0 it makes the pin an output at 0 V; to send a 1 it turns the pin back into an input and lets the box's own pull-up hold the line at the box's own 3.3 V. That is open-drain signalling, and it is safe by construction.
Open-drain: to send 0 the pin becomes an output at 0 V; to send 1 it becomes an input and the line floats up to the box’s own 3.3 V.
The AVR's hardware UART cannot do open-drain, so the bits are clocked by hand off Timer1 at 16 MHz, 139 ticks per bit:
static inline void lineHigh() { DDRD &= ~_BV(7); PORTD |= _BV(7); } // input: box pulls up
static inline void lineLow() { PORTD &= ~_BV(7); DDRD |= _BV(7); } // output: 0 V
uint16_t frame = ((uint16_t)b << 1) | 0x200; // start bit, 8 data bits, stop bit
for (uint8_t i = 0; i < 10; i++) {
if (frame & 1) lineHigh(); else lineLow();
frame >>= 1;
while ((uint16_t)(TCNT1 - t0) < T[i]) {} // T[i] = 139 * (i + 1)
}
The first attempts failed blind
I sent the fix three times and the box kept looping. Each time I had no way to see the other end. With one wire on the RX pad, I could not also read TX. The lesson from that hour was simple: an action you cannot observe has not happened yet.
The second version of the sketch uses one pin for both directions. It bit-bangs out on pin 7 when the laptop sends something, and otherwise samples pin 7 as a software UART receiver and forwards what it hears to the laptop. No tape on the reset button, no second wire. The Arduino end never moves; only the box end goes between pad 3 and pad 2.
That still leaves the problem of reading an answer through a wire you are using to ask the question. The fix is to make the box answer later:
su
(sleep 60; echo ===DIAG; id; grep -E 'statementservice|...' package-restrictions*.xml;
for f in package-restrictions*.xml; do sed -E -i '/statementservice.../s/ enabled="3"//' $f; done;
echo ===AFTER; ...; sync; sleep 20; reboot) &
- youWire on pad 3 (box RX)
- boxThe job is sleeping
- youMove the wire to pad 2 (box TX)
- boxThe job wakes and reports
- boxIt applies the fix and reboots
Send su, then one line that starts a background job with sleep 60 at the front. The shell answers, but nobody is listening yet.
With the wire back on pad 2, the kernel log showed a sync, an emergency remount and
reboot: Restarting system. Then a full boot log, and then silence. Before the fix
that console had never stopped printing crash messages. A query through the same
trick confirmed sys.boot_completed=1, and the TV showed a home screen.
Then it lost everything
The home screen on the TV was the wrong one. Android was asking which launcher to use, and every app we had switched off was back on. The box had booted with factory settings.
The log said why, in one line:
PackageManager: Error reading settings: java.io.FileNotFoundException:
/data/system/users/0/package-restrictions.xml (Permission denied)
sed -i does not edit a file. It writes a new file and renames it over the old one,
and the new file belongs to whoever ran sed, which on that console was root, mode
0600. system_server runs as the user system. It could not open its own settings,
treated them as missing, and started fresh.
# sed -i '…' package-restrictions.xml
resulting file
0600 root:root
system_server reads it
FileNotFoundException (Permission denied)
what the TV shows
“Which home app?” chooser, all apps back on
sed -i writes a new file and renames it over the old one. The new file belongs to whoever ran sed: root.
The fix command had saved an untouched copy of the original file first, so recovery
was mechanical. Stop zygote so nothing is holding the settings in memory, copy the
original back with only the three enabled="3" attributes removed, then
chown system:system, chmod 660, restorecon, reboot. All 8 apps were off again, our
home screen was the default again, and Android logged no read errors.
Then I made the same mistake again an hour later, in the code meant to prevent all of this, which is the next section.
The guard
Two requirements: what happened must never reach the TV again, and anything else that stops boot should have a way back that does not need a screwdriver.
- 0 skernel, /data mounted
- ~6 sguard · fix
Sees statementservice (or another of the five) switched off and switches it back on, in place.
- ~12 szygote, system_server
- ~30 sboot completed
Boot completes, because the verifier exists again.
- +60 s / 10 minguard · watch
Saves the repaired settings as last-known-good.
result · Home screen. Exactly the failure from this post, never reaching the TV.
It is one .rc file in /system/etc/init and one shell script:
on post-fs-data
exec u:r:su:s0 root root -- /system/bin/sh /data/local/tailscale/boot-rescue.sh fix
start bootrescue
exec blocks init until the script finishes, at post-fs-data. /data is mounted,
zygote is not running, and nothing will overwrite the file. The first version of the
script used sed -i. On the very next restart it re-enabled nothing, because nothing
needed it. It still rewrote the file as root, and the box came up with factory settings
again. The same bug, in the guard against it.
The version on the box only writes when a required app is actually off, and writes
back with cat tmp > file, which keeps the inode and so the owner, mode and SELinux
label:
R='\.(statementservice|packageinstaller|providers\.settings|managedprovisioning|webview_shell)"'
if grep -E "$R" "$f" | grep -qE ' enabled="[234]"'; then
sed -E "/$R/s/ enabled=\"[234]\"//" "$f" > "$tmp" && cat "$tmp" > "$f"
fi
Both branches have a check: on a copy of the broken file the output is byte-identical to the file that boots, and on a healthy file the modification time does not move.
What I have not done is the honest final test: switch statementservice off on the
real box on purpose and restart it. The agent's safety layer refused to switch off a
system app deliberately, even a harmless one, and given that switching off a system
app is how this whole story started, I think it was right to ask. That test will happen
with me approving each step by hand.
I kept going, and froze it
Once the box was stable, I used it as a platform. In one evening:
- A camera. A USB webcam on the box's side socket, read with raw V4L2
ioctls from a small Go daemon (32-bit ARM, no cgo) and served as JPEG snapshots. The first viewer used an MJPEG stream and ran seconds behind on a slow link, because frames pile up in TCP buffers. Switching the viewer to fetch the newest snapshot, one at a time, cut it to about 0.1–0.2 s. - Talk. Speak on a phone, hear it through the TV. A 12 KB Android app with one
service that plays a file with
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, aPOST /talkon the daemon, and a 10 dBLoudnessEnhancer, because iPhone recordings came out 12 dB quieter than the laptop's and nobody in the room noticed the first message. - Reach without an app. Tailscale Funnel with
path-scoped serving: only
/talkand one secret-path web page are public. The camera, the remote control andadbstay private. A wrong key gets the same 404 as a wrong path. The phone side became a web page usingMediaRecorder, after an iOS Shortcut kept hanging. The phone was not on the tailnet, and the Shortcut had no way to say so.
The camera exposed a bug in the box's xhci driver. Restart the capture process
mid-stream and the USB controller jams (Not enough bandwidth for altsetting 3) until
the camera is physically replugged. I taught the daemon to unbind and rebind the whole
controller when that happened. It worked, twice. An hour later the box froze solid,
no network, with that looping "dududu" from the speaker. A hard power cycle brought it
back. The saved kernel log was too short to prove the cause, and the controller reset
is my prime suspect, so it is gone.
That evening is the part I would do differently. Each feature was small and tested on its own. Five of them in two hours, on a box that had been bricked that morning, was not.
What I would tell another engineer, or another agent
- Never switch off a system package on Android 7 or older without reading what
PackageManagerService requires.
statementservice,packageinstallerand the settings provider are all looked up by name at boot. On newer versions the list changes; read it for your version. - Ownership is part of the data. Any root edit of a file a system service reads needs the owner, mode and SELinux label put back, or a write method that keeps them.
- An action you cannot observe has not happened. Three blind sends taught me nothing. One send with a delayed answer told me everything.
- Check, then act, in that order. One restart that day happened while a video was playing: the "is anyone watching" check and the reboot ran in parallel, and the check answered too late. Checks that gate an action have to finish first.
- "Verified" needs a scope. "Boots and runs for five minutes" and "runs all night" are different claims. I made the first and said the second. Say which one you mean.
- Research before buying. The device tree would have shown that the USB cable could never work, before I bought it.
If you are building something that lets an agent act on real machines, the Agent Reliability Scorecard is the checklist I now run before giving one shell access to anything I cannot walk over and unplug.
The box is on the TV now with a remote in my father's hand. The webcam is back in,
and tomorrow's job is to read uptime and find out whether it ran all night.