Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,79 @@ tag names, define the order. See the table at the bottom.
`Android/obb` folders through adb into shared storage. Android blocks both
folders for apps, including Termux, so direct access is not possible without
shell access.
- `lfa usb` exposes real USB devices through Termux:API. `status`, `list`, and
`refresh` read `termux-usb -l`; `scan` requests Android permission and reads
real descriptors; `enable` installs the guest helpers.
- `lfa usb attach` opens a guest session with a real `/dev/bus/usb/BBB/DDD`
and a `/sys/bus/usb/devices` tree built from the device's descriptors.
`lib/lfa-usb-node.c`, preloaded for the session, duplicates the usbfs
descriptor that Termux:API opened, reports sysfs magic for libudev, and
stubs the hotplug monitor, so unmodified `lsusb`, PyUSB, dfu-util and other
libusb tools work with real ioctls and transfers. The preload is built for
the guest architecture with clang/cc and falls back to
`lib/prebuilt/lfa-usb-node-<arch>.so` (aarch64 bundled).
- `lfa usb tty` bridges the device's bulk endpoints to a pty and exposes
`/dev/ttyUSB0` for serial tools. CDC-ACM, CP210x and CH340 are supported;
baud changes and DTR/RTS are forwarded to the chip, so esptool's reset
sequence works. `lfa usb flash` installs and runs `esptool` inside the
distribution instead of delegating to nrflash.
- `lfa usb exec DEV -- CMD` still forwards the raw descriptor as
`TERMUX_USB_FD` for guests where the preload cannot be built. proot still
cannot create device nodes; the node is a preload over the real descriptor.
See `docs/USB.md`.
- `lfa usb status` no longer asks you to install Termux:API when `pm list
packages` cannot see other apps (Android 11 and newer); a working
`termux-usb -l` counts as proof, and host mode is reported as
"not advertised" rather than "no".
- USB commands fall back to the instance remembered by `lfa usb enable`, so
`lfa usb scan` works with several distros installed, and an unusable or
missing instance is reported instead of exiting silently.
- `lfa usb attach`, `lfa usb tty` and `lfa usb exec` keep the terminal:
a zero-second timeout no longer wraps the session in GNU `timeout`, which
put the guest shell in a background process group so reads stopped with
SIGTTIN (or failed with EIO).
- USB sessions show output live. `termux-usb` runs its callback inside
`OUTPUT="$(...)"`, so the guest's stdout was captured and only printed when
the session ended; the callback now points stdout at the terminal when
stderr is still a tty. Redirecting both streams (`> log 2>&1`) still
captures the session.
- USB enumeration works in real usbutils/libusb. libudev scans `/sys/bus`
and `/sys/class` before `/sys/bus/usb/devices`, and Android's `/sys` is not
readable for an app, so `udev_enumerate_scan_devices()` failed with `EACCES`
and `lsusb` listed nothing even though the device tree was bound. The
session now binds a small readable `/sys/bus` (containing our
`usb/devices` tree) and an empty `/sys/class`, and the synthetic
`subsystem` links are absolute so proot cannot resolve them out of `/sys`.
- USB tools can open the device from inside the session's login shell. bash
marks the descriptors it inherited close-on-exec when it starts as an
interactive login shell, so the shell kept the device fd while every child
(`lsusb`, `python3`, `esptool`) got `EBADF`: `lsusb -v` printed
`Couldn't open device` and kept going on sysfs descriptors alone. The
preload now clears `FD_CLOEXEC` on `execve`, so the first child inherits a
plain descriptor and the flag is never set again for the rest of the
session.
- The inherited USB descriptor survives into child processes. Termux:API can
hand it over with `FD_CLOEXEC`, so `lsusb`, `python3` and `esptool` started
without it (`os.dup(7)` returned `EBADF`, control transfers `ENOTTY`) while
descriptors still came from sysfs. The generated session script now
duplicates it onto a free fd with a plain redirection (`dup2()` clears the
flag; bash's `{var}<&N` form allocates with `F_DUPFD_CLOEXEC` on some
builds), rewrites `LFA_USB_MAP` to the new number and prints whether
children really inherited it, and the login profile keeps `TERMUX_USB_FD`
in a USB session.
- The guest login profile keeps the USB session's `LD_PRELOAD`. The
"make env look normal" cleanup unset every `LD_PRELOAD`, including the
usbfs shim a `bash -l` session needs, so libusb still failed with `-99`.
It now removes only a Termux/bionic preload, recognised by the absence of
`LFA_USB_MAP`.
- The usbfs preload is set inside the guest script. proot-distro v5 removes
`LD_PRELOAD` from the environment it passes to proot, so libusb started
without the udev stubs and failed with `unable to initialize libusb: -99`.
The generated session scripts export it after proot has started.
- `/dev/bus/usb/BBB/DDD` is visible inside the guest. proot-distro binds the
host `/dev` over the rootfs, which hid the placeholder node; the node is now
also kept in a Termux-side shadow tree and bound over `/dev/bus/usb`, so
`ls`, `stat()` and tools that check the path before opening it work.
- Guest sessions now export `APPIMAGE_EXTRACT_AND_RUN=1`, because proot does
not expose `/dev/fuse` and an AppImage cannot self-mount. A direct
`./Foo.AppImage`, a file-manager double-click, and a menu entry all work.
Expand Down
50 changes: 47 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ hardware, not simulated. Full details, root causes, and evidence are in
| Alpine 3.18 (Android 5) | Yes | Yes | legacy VNC desktop, panel works; X11 and Both hidden | v1.7.1 |
| Ubuntu GNOME 46 (experimental) | Yes | Yes | shell and apps run; session services missing | v1.7.0 |

> The test suite runs `627` checks with 0 failures. The same suite runs on the
> The test suite runs `811` checks with 0 failures. The same suite runs on the
> devices (`tests/device-matrix.sh`). The table is as of v1.7.1. Ubuntu was
> re-verified on v1.7.1, and Alpine 3.18 was re-verified on Android 5 on
> 2026-11-04; the other rows were last verified on v1.7.0. A row is
Expand Down Expand Up @@ -398,6 +398,12 @@ as experimental only.
- `lfa storage` repairs the shared-storage links and the false Thunar X, runs
scripts from shared storage without `chmod`, and copies `Android/data` and
`Android/obb` through adb.
- `lfa usb` exposes real Android USB devices through Termux:API. `list` and
`refresh` show the live device list, `scan` reads descriptors, `attach`
opens a guest session with a real `/dev/bus/usb/BBB/DDD` and a sysfs view,
`tty` provides `/dev/ttyUSB0` for serial tools, `flash` runs esptool in the
guest, `watch` reports hotplug, and `exec DEV -- CMD` passes the raw
descriptor in `TERMUX_USB_FD`.

### Portability and convenience

Expand Down Expand Up @@ -469,6 +475,7 @@ is accepted.
| `lfa remove` | delete a distribution (`-y` skips the question) |
| `lfa compat` | list, explain, enable, or disable compatibility shims |
| `lfa android` | check, install, test, and enable the Termux:API bridge |
| `lfa usb` | list, scan, attach, tty, flash, and exec real USB devices through Termux:API |
| `lfa appimage` / `lfa app` | extract an AppImage once and add a menu entry |
| `lfa run` | run an AppImage once without installing it |
| `lfa storage` | Android storage status, repair, run, and protected data helpers |
Expand All @@ -487,6 +494,10 @@ lfa autostart vnc -i ubuntu
lfa install --distro debian --desktop xfce4 --software python,git,htop -i dev
lfa compat list
lfa android test
lfa usb status
lfa usb scan
lfa usb enable -i ubuntu
lfa usb exec 001/002 -- lfa-usb desc
lfa appimage add ~/Foo.AppImage
lfa appimage run Foo
lfa storage status
Expand Down Expand Up @@ -525,6 +536,39 @@ TUN/TAP, iptables, CUDA, ROCm, KVM, and kernel modules remain unavailable.
The full list, with reasons, is in
[docs/INCOMPATIBLE.md](docs/INCOMPATIBLE.md).

### USB devices

`lfa usb` shows the USB devices Android actually exposes and gives a guest a
real userspace USB port. Enumeration, descriptors and transfers go through
Termux:API; nothing is simulated at the transfer level.

```bash
lfa usb status # Termux:API, host mode, bridge state
lfa usb list # real devices, lsusb-style
lfa usb scan # Android permission + real descriptors
lfa usb enable -i ubuntu # helpers, libusb preload, tty bridge
lfa usb attach -i ubuntu # shell with /dev/bus/usb/001/002 + sysfs
lfa usb attach 001/002 -- lsusb -v
lfa usb tty -i ubuntu # shell with /dev/ttyUSB0
lfa usb flash --install # one-time: esptool in the guest
lfa usb flash probe
lfa usb flash write 0x0 /sdcard/firmware.bin
lfa usb watch --interval 5 # poll for hotplug
lfa usb exec 001/002 -- lfa-usb desc
```

`attach` maps the real usbfs descriptor onto `/dev/bus/usb/BBB/DDD` with a
small preload and binds a sysfs tree built from the real descriptors, so
`lsusb`, PyUSB, dfu-util and other libusb tools work unmodified. `tty` bridges
the device's bulk endpoints to a pty, so `picocom` and `esptool` see a normal
`/dev/ttyUSB0`; DTR/RTS and baud changes are forwarded to the chip. `exec`
stays available for the raw descriptor in `TERMUX_USB_FD`.

proot still cannot create device nodes; the node comes from the preload, not
from `mknod`. Kernel drivers, USB audio, and printer queues therefore still do
not attach. The full model, requirements, security notes, and troubleshooting
are in [docs/USB.md](docs/USB.md).

### AppImages

An AppImage normally mounts itself through FUSE. proot does not expose
Expand Down Expand Up @@ -1184,7 +1228,7 @@ short list is:
- CUDA, ROCm, vendor GPU compute, hardware video encode/decode
- kernel modules, real `mount`/`chroot`/`unshare`, real root/SELinux control
- Bluetooth/BlueZ, NFC, camera as `/dev/video*`, sensors, USB host as device
nodes
nodes (`lfa usb` provides userspace access instead)
- Wayland compositors, simultaneous X11 + VNC, foreign-architecture binaries

Full reasons, the “works with caveats” list, Android 5/6/7 differences, and
Expand Down Expand Up @@ -1510,7 +1554,7 @@ plus `script` from util-linux for the pty test, which is skipped if absent:

```bash
chmod +x tests/test-all.sh
./tests/test-all.sh # expect: PASS=627 FAIL=0
./tests/test-all.sh # expect: PASS=811 FAIL=0
```

The suite is deliberately stubbed. It cannot prove a desktop actually appears.
Expand Down
52 changes: 52 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,58 @@ Neither layer may fake a kernel facility. Docker, Podman, Flatpak, TUN/TAP,
iptables, CUDA, ROCm, KVM, and modules are listed as unsupported by
`lfa compat list` and explained by `lfa compat explain`.

### USB bridge

```
Termux:API app ── Binder ──> UsbManager ──> /dev/bus/usb/BBB/DDD
│ (Android-owned)
▼
termux-usb -l ────────────────> $PREFIX/tmp/lfa-usb/devices.tsv
│ │
│ ▼ guest /tmp/lfa-usb
│ lsusb / lfa-usb (shims)
▼
termux-usb -E -e lfa-usb-callback DEV
│ TERMUX_USB_FD = real descriptor
▼
descriptor pass: lib/usb-desc.py --sysfs -> $PREFIX/tmp/lfa-usb/session-sys
│
▼
proot-distro login --bind sys:/sys/bus/usb/devices -- CMD
│ LFA_USB_MAP=BBB/DDD=<fd>, LD_PRELOAD=liblfa-usb-node.so
▼
guest CMD: open("/dev/bus/usb/BBB/DDD") -> dup(real fd)
ioctl/read/write -> the real device
```

`lfa usb refresh` is the only writer of the shared view. It enumerates with
`termux-usb -l`, enriches from host sysfs when Android allows it, and publishes
`devices.tsv` plus a small sysfs-shaped tree under `sys/`. `lfa usb scan`
requests permission for each device and runs `lib/usb-desc.py` in the guest
with the inherited descriptor; that script reads the device, string, and
configuration descriptors with raw ioctls, so it needs no libusb.

The descriptor is owned by Android and handed to one process at a time by
`termux-usb`. proot does not virtualise file descriptors, so the number is
valid inside the guest. `lfa usb attach` uses that to build a real port:

- `lib/usb-desc.py --sysfs` writes a sysfs-shaped directory from the real
descriptors (device, configuration, interfaces, endpoints).
- `proot-distro login --bind` mounts it at `/sys/bus/usb/devices`.
- `lib/lfa-usb-node.c`, built for the guest architecture and preloaded as
`liblfa-usb-node.so`, duplicates the inherited descriptor on `open()` of
the node path, reports `SYSFS_MAGIC` for `statfs()` on `/sys` (libudev
rejects syspaths elsewhere), and replaces the udev hotplug monitor with an
idle pipe (Android has no udev daemon).

The node name is therefore virtual, but the descriptor and every `ioctl`,
`read`, and `write` are the real ones. A plain regular-file placeholder would
open successfully and then fail every `USBDEVFS_*` request, which is why LFA
does not use one. `lfa usb tty` builds on the same descriptor: it claims the
bulk interface and bridges it to a pty, with baud and DTR/RTS forwarded, so
serial tools see `/dev/ttyUSB0`. `lfa usb exec` remains the raw-descriptor
door for guests where the preload cannot be built.

## Storage model

Android shared storage is at `/storage/emulated/0`. proot binds it at the
Expand Down
8 changes: 7 additions & 1 deletion docs/INCOMPATIBLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ It is not a kernel or root bridge.
| Clipboard, notifications, vibrate, torch, share | yes | Android-side actions |
| Wi-Fi information | yes | connection/scan data, not full control |
| Telephony / SMS / contacts | yes, with permissions | Android APIs |
| USB | partial | `termux-usb` file descriptor + libusb, not `/dev/bus/usb` |
| USB | partial, implemented | `lfa usb` lists real devices, maps the Termux:API descriptor to `/dev/bus/usb` and `/dev/ttyUSB0` with a preload, and runs esptool in the guest; kernel drivers still do not attach (see [USB.md](USB.md)) |
| Bluetooth | partial / version dependent | some Android APIs, not BlueZ HCI |
| Kernel modules, `mount`, namespaces, cgroups | no | unavailable |
| Raw sockets, TUN/TAP, iptables | no | unavailable |
Expand All @@ -136,6 +136,12 @@ what this device actually provides. The bridge writes files into
`$PREFIX/tmp`, which the guest sees as `/tmp` because LFA launches proot with
`--shared-tmp`.

`lfa usb` uses the same bridge for USB. It shows the real device list from
`termux-usb -l`, reads descriptors after Android grants permission, and maps
the real descriptor into guest sessions (`lfa usb attach`, `lfa usb tty`).
libusb and serial tools work against the real device; kernel USB drivers
(`usb-storage`, `usbhid`, USB audio) still do not attach. See [USB.md](USB.md).

## Emulator-verified vs device-verified

The CI matrix runs on GitHub-hosted Android emulators. That covers the
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Start here to understand the project rather than install it.
| [TEST-REPORT.md](TEST-REPORT.md) | What was verified, on which device, with which version |
| [CI-ANDROID.md](CI-ANDROID.md) | Android emulator workflows, matrix tiers, pinned APKs, and gaps |
| [INCOMPATIBLE.md](INCOMPATIBLE.md) | Software that cannot run, and why shims cannot fix kernel limits |
| [USB.md](USB.md) | Real USB devices in the guest: /dev/bus/usb, /dev/ttyUSB0, esptool, and their limits |
| [issues.md](../issues.md) | Every known defect and limitation, with severity and status |
| [ghissue.md](../ghissue.md) | Repository-level improvements: metadata, CI, releases |
| [tracker.md](../tracker.md) | Live progress for the audit above |
Expand Down
46 changes: 43 additions & 3 deletions docs/TEST-REPORT.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
# Test report for v1.7.1

> Current development suite: `PASS=627 FAIL=0`.
> Current development suite: `PASS=811 FAIL=0`.

## USB bridge, 2026-11-18

Host verification of the `lfa usb` bridge. `termux-usb` and `proot-distro` are
faked, so the complete host path is exercised without hardware. The Android
permission dialog, a real descriptor, real transfers and flashing still need a
phone with an OTG adapter; do not claim them before that run.

| Check | Result |
|---|---|
| Host suite (`./tests/test-all.sh`) | `PASS=811 FAIL=0` |
| Descriptor parser (`python3 lib/usb-desc.py --selftest`) | device and configuration descriptors parsed; sysfs blob built |
| Serial bridge (`python3 lib/lfa-usb-tty.py --selftest`) | CH340 divisor encoding, CDC/CP210x detection, endpoint selection |
| Preload build | `lib/lfa-usb-node.c` built for the guest architecture, no `DT_NEEDED`; aarch64 prebuilt bundled |
| Preload map | `open()` on a mapped node returns a duplicate of the inherited fd; reads and writes reach it; an unmapped node is untouched; DTR/RTS ioctls succeed and are forwarded to the bridge |
| Enumeration | `termux-usb -l` JSON parsed into `devices.tsv`, `sys/`, and `state.env` |
| Guest view | `lsusb` default, `-d`, `-s`, `-t`, `-v`, `-V`, and missing-bridge paths |
| sysfs shape | device, configuration, interface and endpoint directories written from the descriptors, with the attributes usbutils reads |
| Guest helper | `lfa-usb status`, `info`, `path`, `fd`, `desc`, and `help` |
| Real access | `lfa usb exec` ran the Termux:API callback and entered the guest with `--shared-tmp`; the run script was removed |
| Attach | descriptor pass wrote the sysfs tree; session bound it at `/sys/bus/usb/devices`, mapped `001/002=<fd>`, preloaded the node helper, and created the placeholder node |
| TTY | `/dev/ttyUSB0` session forwarded baud and the client command to `lfa-usb-tty.py`; a fake-device harness relayed bytes through the pty in both directions |
| ESP32 flashing | `--install`, `probe` -> `flash_id`, and `write` wrapper checked for `--port /dev/ttyUSB0` and the requested baud; a real board is not run |
| Hotplug | `lfa usb watch --count 1` printed the initial add event |
| Status honesty | a working `termux-usb -l` counts as proof that Termux:API is installed when `pm` cannot list packages, and a feature list without `usb.host` reads "not advertised" |
| Instance selection | USB commands reuse the instance saved by `lfa usb enable`; a saved-but-missing or unset instance prints the exact `-i NAME` hint instead of exiting silently |
| Failure paths | missing client, non-JSON `termux-usb` output, and old `termux-api` without `-E` reported honestly |
| Real USB hardware | **not yet run** |

The session preload was also exercised outside the fake harness: with a
regular file standing in for the usbfs descriptor, `open()` on a mapped node
returned a duplicate of that descriptor; with a synthetic sysfs tree bound
over `/sys/bus/usb/devices`, the host `libusb`/`lsusb` enumerated the device,
including its strings, interface class, endpoint address, packet size, and
serial number. That test found three requirements that are now handled:
libudev rejects a syspath whose filesystem is not sysfs, libusb's init fails
when the udev hotplug monitor cannot be created in an Android guest, and
usbutils reads `bMaxPacketSize0`, `bcdDevice`, `bmAttributes`, `maxchild`
and the lane counts straight from sysfs. `lsusb -t` stays empty because the
tree has no host-controller root hub.

## Android storage, 2026-11-06

Expand All @@ -9,7 +49,7 @@ storage changes.

| Check | Result |
|---|---|
| Host suite | `PASS=627 FAIL=0` |
| Host suite | `PASS=742 FAIL=0` |
| Thunar false X | gid 1023; folder icons normal; screenshot checked |
| `lfa storage exec` | shell and Python scripts from `/storage/emulated/0` ran from a `/tmp` copy |
| `lfa storage data` | adb at `127.0.0.1:5555`; `list` and `pull` worked for `Android/data` and `Android/obb`; guest read the copy |
Expand All @@ -33,7 +73,7 @@ the network during this pass, so the legacy-device rows remain pending.

| Check | Result |
|---|---|
| Host suite (`./tests/test-all.sh`) | `PASS=627 FAIL=0` |
| Host suite (`./tests/test-all.sh`) | `PASS=742 FAIL=0` |
| On-device VNC scenario (`termux-e2e.sh`) | `PASS=14 FAIL=0` |
| On-device Both → VNC/X11 switch | `PASS=22 FAIL=0` |
| On-device X11 scenario | `PASS=14 FAIL=0` |
Expand Down
Loading
Loading