From 246484e91b0d3bb061abd69c575afdbac96e1928 Mon Sep 17 00:00:00 2001 From: alexcekay Date: Mon, 15 Jun 2026 17:48:15 +0200 Subject: [PATCH] feat(mtd): allow bulk erase --- .../px4_platform_common/mtd_manifest.h | 2 + .../include/px4_platform_common/px4_mtd.h | 2 + platforms/nuttx/src/px4/common/px4_mtd.cpp | 1 + src/systemcmds/mtd/mtd.cpp | 46 +++++++++++++------ 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/platforms/common/include/px4_platform_common/mtd_manifest.h b/platforms/common/include/px4_platform_common/mtd_manifest.h index 5f6780d5d8d..aec7db2f4ff 100644 --- a/platforms/common/include/px4_platform_common/mtd_manifest.h +++ b/platforms/common/include/px4_platform_common/mtd_manifest.h @@ -31,6 +31,7 @@ * ****************************************************************************/ #pragma once +#include #include typedef enum { @@ -55,6 +56,7 @@ typedef struct { typedef struct { const px4_mft_device_t *device; const uint32_t npart; + const bool bulk_erase; const px4_mtd_part_t partd[]; } px4_mtd_entry_t; diff --git a/platforms/common/include/px4_platform_common/px4_mtd.h b/platforms/common/include/px4_platform_common/px4_mtd.h index 4fa184f3c95..36daee0cd23 100644 --- a/platforms/common/include/px4_platform_common/px4_mtd.h +++ b/platforms/common/include/px4_platform_common/px4_mtd.h @@ -31,6 +31,7 @@ * ****************************************************************************/ #pragma once +#include #include __BEGIN_DECLS @@ -48,6 +49,7 @@ typedef struct { struct mtd_dev_s **part_dev; uint32_t devid; unsigned n_partitions_current; + bool bulk_erase; } mtd_instance_s; /* diff --git a/platforms/nuttx/src/px4/common/px4_mtd.cpp b/platforms/nuttx/src/px4/common/px4_mtd.cpp index a5b2948ce24..380bf493b0a 100644 --- a/platforms/nuttx/src/px4/common/px4_mtd.cpp +++ b/platforms/nuttx/src/px4/common/px4_mtd.cpp @@ -346,6 +346,7 @@ memoryout: uint32_t nparts = mtd_list->entries[num_entry]->npart; instances[i]->devid = mtd_list->entries[num_entry]->device->devid; + instances[i]->bulk_erase = mtd_list->entries[num_entry]->bulk_erase; instances[i]->mtd_dev = nullptr; instances[i]->n_partitions_current = 0; diff --git a/src/systemcmds/mtd/mtd.cpp b/src/systemcmds/mtd/mtd.cpp index dcf516b8fda..b2cac2014f2 100644 --- a/src/systemcmds/mtd/mtd.cpp +++ b/src/systemcmds/mtd/mtd.cpp @@ -158,26 +158,42 @@ static void print_usage() int mtd_erase(mtd_instance_s &instance) { - uint8_t v[32]; - memset(v, 0xFF, sizeof(v)); - - for (uint8_t i = 0; i < instance.n_partitions_current; i++) { - - uint32_t count = 0; - printf("Erasing %s\n", instance.partition_names[i]); - int fd = open(instance.partition_names[i], O_WRONLY); - - if (fd == -1) { - PX4_ERR("Failed to open partition"); + if (instance.bulk_erase) { + if (!instance.mtd_dev) { + PX4_ERR("MTD device not initialized"); return 1; } - while (write(fd, v, sizeof(v)) == sizeof(v)) { - count += sizeof(v); + int ret = MTD_IOCTL(instance.mtd_dev, MTDIOC_BULKERASE, 0); + + if (ret < 0) { + PX4_ERR("MTDIOC_BULKERASE failed: %d", ret); + return 1; } - printf("Erased %" PRIu32 " bytes\n", count); - close(fd); + printf("Erased device via chip erase\n"); + + } else { + uint8_t v[32]; + memset(v, 0xFF, sizeof(v)); + + for (uint8_t i = 0; i < instance.n_partitions_current; i++) { + uint32_t count = 0; + printf("Erasing %s\n", instance.partition_names[i]); + int fd = open(instance.partition_names[i], O_WRONLY); + + if (fd == -1) { + PX4_ERR("Failed to open partition"); + return 1; + } + + while (write(fd, v, sizeof(v)) == sizeof(v)) { + count += sizeof(v); + } + + printf("Erased %" PRIu32 " bytes\n", count); + close(fd); + } } return 0;