From 352f773ec466008db9c40665065636484d415297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Mi=C5=A1i=C4=87?= Date: Thu, 23 Feb 2023 09:57:18 +0100 Subject: [PATCH] systemcmds/mtd: fix rwtest - force data to/from the device Block Device driver uses a buffer so we need to ensure data is written or read to the device and not to the buffer so we can be sure if the device works properly --- src/systemcmds/mtd/mtd.cpp | 56 ++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/systemcmds/mtd/mtd.cpp b/src/systemcmds/mtd/mtd.cpp index 0ebe4c9573..1864073002 100644 --- a/src/systemcmds/mtd/mtd.cpp +++ b/src/systemcmds/mtd/mtd.cpp @@ -250,33 +250,71 @@ int mtd_rwtest(const mtd_instance_s &instance) } printf("rwtest %s testing %zd bytes\n", instance.partition_names[i], expected_size); - int fd = open(instance.partition_names[i], O_RDWR); - if (fd == -1) { - PX4_ERR("Failed to open partition"); - return 1; - } + bool run = true; + + while (run) { + + int fd = open(instance.partition_names[i], O_RDWR); + + if (fd == -1) { + PX4_ERR("Failed to open partition"); + return 1; + } + + if (read(fd, v, sizeof(v)) != sizeof(v)) { + PX4_ERR("read failed"); + close(fd); + return 1; + } - while (read(fd, v, sizeof(v)) == sizeof(v)) { count += sizeof(v); if (lseek(fd, offset, SEEK_SET) != offset) { PX4_ERR("seek failed"); + close(fd); return 1; } if (write(fd, v, sizeof(v)) != sizeof(v)) { PX4_ERR("write failed"); + close(fd); + return 1; + } + + //sync and close to discard data from the Block Device buffer + if (OK != fsync(fd)) { + PX4_ERR("Failed to fsync"); + close(fd); + return 1; + } + + if (OK != close(fd)) { + PX4_ERR("Failed to close partition"); + return 1; + } + + fd = open(instance.partition_names[i], O_RDONLY); + + if (fd == -1) { + PX4_ERR("Failed to open partition"); return 1; } if (lseek(fd, offset, SEEK_SET) != offset) { PX4_ERR("seek failed"); + close(fd); return 1; } if (read(fd, v2, sizeof(v2)) != sizeof(v2)) { PX4_ERR("read failed"); + close(fd); + return 1; + } + + if (OK != close(fd)) { + PX4_ERR("Failed to close partition"); return 1; } @@ -286,14 +324,16 @@ int mtd_rwtest(const mtd_instance_s &instance) } offset += sizeof(v); + + if (count >= expected_size) { + run = false; + } } if (count != expected_size) { PX4_ERR("Failed to read partition - got %zd/%zd bytes", count, expected_size); return 1; } - - close(fd); } printf("rwtest OK\n");