From 174a6aab6f49019bf4669db22049fe8a2284cd7a Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Wed, 22 Apr 2026 21:30:46 +1200 Subject: [PATCH] feat(mklittlefs): add command to format littlefs filesystems Add an NSH command to format a device with littlefs, analogous to mkfatfs for FAT filesystems. The command unmounts the mount point, then remounts with forceformat to format and mount in one step. Enable the command on boards that use littlefs as primary storage (airbrainh743 and kakuteh7v2), and document it in the airbrainh743 flight controller page as a recovery procedure for a corrupted flash filesystem. Usage: mklittlefs /dev/mtd0 /fs/flash --- boards/gearup/airbrainh743/default.px4board | 1 + boards/holybro/kakuteh7v2/default.px4board | 1 + .../flight_controller/gearup_airbrainh743.md | 12 +++ docs/en/releases/main.md | 1 + src/systemcmds/mklittlefs/CMakeLists.txt | 41 ++++++++++ src/systemcmds/mklittlefs/Kconfig | 12 +++ src/systemcmds/mklittlefs/mklittlefs.cpp | 78 +++++++++++++++++++ 7 files changed, 146 insertions(+) create mode 100644 src/systemcmds/mklittlefs/CMakeLists.txt create mode 100644 src/systemcmds/mklittlefs/Kconfig create mode 100644 src/systemcmds/mklittlefs/mklittlefs.cpp diff --git a/boards/gearup/airbrainh743/default.px4board b/boards/gearup/airbrainh743/default.px4board index 1af794b76c..f5ed5b8434 100644 --- a/boards/gearup/airbrainh743/default.px4board +++ b/boards/gearup/airbrainh743/default.px4board @@ -71,6 +71,7 @@ CONFIG_SYSTEMCMDS_HARDFAULT_LOG=y CONFIG_SYSTEMCMDS_I2CDETECT=y CONFIG_SYSTEMCMDS_LED_CONTROL=y CONFIG_SYSTEMCMDS_MFT=y +CONFIG_SYSTEMCMDS_MKLITTLEFS=y CONFIG_SYSTEMCMDS_MTD=y CONFIG_SYSTEMCMDS_NSHTERM=y CONFIG_SYSTEMCMDS_PARAM=y diff --git a/boards/holybro/kakuteh7v2/default.px4board b/boards/holybro/kakuteh7v2/default.px4board index 1b4dc72136..da707c268d 100644 --- a/boards/holybro/kakuteh7v2/default.px4board +++ b/boards/holybro/kakuteh7v2/default.px4board @@ -78,6 +78,7 @@ CONFIG_SYSTEMCMDS_HARDFAULT_LOG=y CONFIG_SYSTEMCMDS_I2CDETECT=y CONFIG_SYSTEMCMDS_LED_CONTROL=y CONFIG_SYSTEMCMDS_MFT=y +CONFIG_SYSTEMCMDS_MKLITTLEFS=y CONFIG_SYSTEMCMDS_NSHTERM=y CONFIG_SYSTEMCMDS_PARAM=y CONFIG_SYSTEMCMDS_PERF=y diff --git a/docs/en/flight_controller/gearup_airbrainh743.md b/docs/en/flight_controller/gearup_airbrainh743.md index 0fe295acae..bf612e332d 100644 --- a/docs/en/flight_controller/gearup_airbrainh743.md +++ b/docs/en/flight_controller/gearup_airbrainh743.md @@ -91,6 +91,18 @@ Firmware can be installed in any of the normal ways: - [Load the firmware](../config/firmware.md) using _QGroundControl_. You can use either pre-built firmware or your own custom firmware. +### Flash Storage Troubleshooting + +The AirBrainH743 uses a 128MB NAND flash (W25N) with a littlefs filesystem for [logging](../dev_log/logging.md). +If the flash filesystem becomes corrupted, you can reformat it using the [System Console](../debug/system_console.md): + +```sh +mklittlefs /dev/mtd0 /fs/flash +``` + +This will erase all data on the flash and create a fresh littlefs filesystem. +The filesystem is immediately available after the command completes. + ### System Console UART1 (ttyS0) is configured for use as the [System Console](../debug/system_console.md). diff --git a/docs/en/releases/main.md b/docs/en/releases/main.md index de76276d70..372f6890a8 100644 --- a/docs/en/releases/main.md +++ b/docs/en/releases/main.md @@ -80,6 +80,7 @@ Please continue reading for [upgrade instructions](#upgrade-guide). - New [SDLOG_MAX_SIZE](../advanced_config/parameter_reference.md#SDLOG_MAX_SIZE) (default `1024` MB) caps the size of a single log file; once reached, the logger closes the current file and starts a new one. - New [SDLOG_ROTATE](../advanced_config/parameter_reference.md#SDLOG_ROTATE) (default `90`) sets the maximum disk usage percentage. Cleanup guarantees `(100 - SDLOG_ROTATE)%` of the disk stays free at all times, even while writing a new log file. Set `0` to disable space-based cleanup, `100` to allow filling the disk completely. - `SDLOG_DIRS_MAX` is now an orthogonal cap on the number of log directories (default `0` = disabled), on top of the space-based cleanup driven by `SDLOG_ROTATE` and `SDLOG_MAX_SIZE`. SITL defaults to `7`. +- New `mklittlefs` systemcmd for reformatting a littlefs volume from the NSH console, analogous to `mkfatfs` for FAT filesystems. ### Ethernet diff --git a/src/systemcmds/mklittlefs/CMakeLists.txt b/src/systemcmds/mklittlefs/CMakeLists.txt new file mode 100644 index 0000000000..c58cc37937 --- /dev/null +++ b/src/systemcmds/mklittlefs/CMakeLists.txt @@ -0,0 +1,41 @@ +############################################################################ +# +# Copyright (c) 2026 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ +px4_add_module( + MODULE systemcmds__mklittlefs + MAIN mklittlefs + STACK_MAIN 2048 + COMPILE_FLAGS + SRCS + mklittlefs.cpp + DEPENDS + ) diff --git a/src/systemcmds/mklittlefs/Kconfig b/src/systemcmds/mklittlefs/Kconfig new file mode 100644 index 0000000000..e17ede2e78 --- /dev/null +++ b/src/systemcmds/mklittlefs/Kconfig @@ -0,0 +1,12 @@ +menuconfig SYSTEMCMDS_MKLITTLEFS + bool "mklittlefs" + default n + ---help--- + Enable support for mklittlefs + +menuconfig USER_MKLITTLEFS + bool "mklittlefs running as userspace module" + default y + depends on BOARD_PROTECTED && SYSTEMCMDS_MKLITTLEFS + ---help--- + Put mklittlefs in userspace memory diff --git a/src/systemcmds/mklittlefs/mklittlefs.cpp b/src/systemcmds/mklittlefs/mklittlefs.cpp new file mode 100644 index 0000000000..2beecf3c56 --- /dev/null +++ b/src/systemcmds/mklittlefs/mklittlefs.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file mklittlefs.cpp + * + * Format a device with littlefs filesystem. + */ + +#include +#include +#include + +#include +#include +#include + +static void print_usage() +{ + PRINT_MODULE_DESCRIPTION("Format a device with the littlefs filesystem."); + + PRINT_MODULE_USAGE_NAME_SIMPLE("mklittlefs", "command"); + PRINT_MODULE_USAGE_ARG(" ", "Device and mount point (e.g. /dev/mtd0 /fs/flash)", false); +} + +extern "C" __EXPORT int mklittlefs_main(int argc, char *argv[]) +{ + if (argc < 3) { + print_usage(); + return 1; + } + + const char *device = argv[1]; + const char *mountpoint = argv[2]; + + // Try to unmount first (ignore error if not mounted) + umount(mountpoint); + + int ret = mount(device, mountpoint, "littlefs", 0, "forceformat"); + + if (ret < 0) { + PX4_ERR("format failed: %s", strerror(errno)); + return 1; + } + + PX4_INFO("formatted %s as littlefs, mounted at %s", device, mountpoint); + return 0; +}