nuttx/drivers/power/battery
Marco Casaroli 209f73ff0e drivers/power: Do not call gauge methods a driver did not implement.
Every BATIOC_* case in the gauge upper half calls dev->ops->X() after
checking only that the *argument* pointer is non-NULL.  The operations
themselves are optional -- max1704x.c implements four of the eight, and
the in-tree fake gauge leaves chipid and operate NULL -- so asking a gauge
for something it does not provide calls through a NULL pointer.

On ARM that is not a null dereference but a branch to address 0, which has
the Thumb bit clear: the core takes a usage fault with CFSR bit 17,
INVSTATE, escalated to a hard fault.  From userspace it looks like the
program stopped mid-run for no reason, and on a board whose assert path
delays before resetting it looks like a hang.

Guard all eight.  A missing method is now ENOTTY, which is what the default
case already returns for an unrecognised command and what a caller can
sensibly probe for.

Tested on sim:nsh with the fake gauge and a local test app.  Before the
change, ioctl(BATIOC_CHIPID) kills the simulator with SIGSEGV.  After the
change, BATIOC_CHIPID and BATIOC_OPERATE return ENOTTY and
BATIOC_VOLTAGE still returns the voltage.  nucleo-f412zg:nsh with
BATTERY_GAUGE, MAX1704X, BQ27426 and BATTERY_FAKE_GAUGE builds.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-10-08 15:52:44 -03:00
..
axp202.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
battery_charger.c drivers/: Multiple Drivers Are Registered With World Writable - Part 2 2026-07-15 15:27:28 +08:00
battery_fakegauge.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
battery_gauge.c drivers/power: Do not call gauge methods a driver did not implement. 2026-10-08 15:52:44 -03:00
battery_monitor.c drivers/: Multiple Drivers Are Registered With World Writable - Part 2 2026-07-15 15:27:28 +08:00
bq769x0.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
bq2425x.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
bq2425x.h drivers: migrate to SPDX identifier 2024-11-06 18:02:25 +08:00
bq2429x.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
bq27426.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
CMakeLists.txt power/battery: fake gauge support for rpmsg communication 2025-12-22 09:15:56 -03:00
goldfish_battery.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
Kconfig power/battery: fake gauge support for rpmsg communication 2025-12-22 09:15:56 -03:00
Make.defs power/battery: fake gauge support for rpmsg communication 2025-12-22 09:15:56 -03:00
max1704x.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
mcp73871.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00