feat(mtd): allow bulk erase

This commit is contained in:
alexcekay
2026-06-24 15:32:44 +02:00
committed by Alexander Lerach
parent 6f1f7035a5
commit 246484e91b
4 changed files with 36 additions and 15 deletions
@@ -31,6 +31,7 @@
*
****************************************************************************/
#pragma once
#include <stdbool.h>
#include <stdint.h>
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;
@@ -31,6 +31,7 @@
*
****************************************************************************/
#pragma once
#include <stdbool.h>
#include <stdint.h>
__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;
/*
@@ -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;
+31 -15
View File
@@ -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;