nuttx/drivers/power
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
..
battery drivers/power: Do not call gauge methods a driver did not implement. 2026-10-08 15:52:44 -03:00
pm !include/fcntl.h: align open flags with Linux values 2026-06-30 13:43:44 +08:00
relay drivers/: Multiple Drivers Are Registered With World Writable - Part 2 2026-07-15 15:27:28 +08:00
supply drivers/power: Describe the regulators through procfs. 2026-08-18 17:28:02 +08:00
CMakeLists.txt
Kconfig
Make.defs