[PATCH rtems 09/14] FIX: Regions must be aligned with erase size

berndmoessner80 at gmail.com berndmoessner80 at gmail.com
Tue Jan 2 20:45:30 UTC 2024


From: Bernd Moessner <berndmoessner80 at gmail.com>

---
 cpukit/dev/flash/flashdev.c           | 35 ++++++++++++++
 testsuites/libtests/flashdev01/init.c | 66 ++++++++++++++++++++++++---
 2 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/cpukit/dev/flash/flashdev.c b/cpukit/dev/flash/flashdev.c
index 2e6a2e3c19..363d12e3ff 100644
--- a/cpukit/dev/flash/flashdev.c
+++ b/cpukit/dev/flash/flashdev.c
@@ -154,6 +154,11 @@ static int rtems_flashdev_update_and_return(
   size_t count
 );
 
+static int rtems_flashdev_check_region_valid(
+  rtems_flashdev *flash,
+  rtems_flashdev_region * region
+);
+
 static uint32_t rtems_flashdev_find_unallocated_region(
   rtems_flashdev_region_table *region_table
 );
@@ -679,6 +684,11 @@ static int rtems_flashdev_ioctl_erase(
 
   erase_args_1 = (rtems_flashdev_region *) arg;
   /* Check erasing valid region */
+  if ( 0 != rtems_flashdev_check_region_valid(flash, erase_args_1))
+  {
+    return EINVAL;
+  }
+
   new_offset = erase_args_1->offset;
   status = rtems_flashdev_get_abs_addr(flash, iop, erase_args_1->size, &new_offset);
   if ( status < 0 ) {
@@ -704,6 +714,11 @@ static int rtems_flashdev_ioctl_set_region(
     rtems_set_errno_and_return_minus_one( ENOMEM );
   }
 
+  if ( 0 != rtems_flashdev_check_region_valid(flash, region_in))
+  {
+    return EINVAL;
+  }
+
   if ( !rtems_flashdev_is_region_defined( iop ) ) {
     if (
       rtems_flashdev_find_unallocated_region(table)
@@ -925,6 +940,26 @@ static int rtems_flashdev_ioctl_get_erase_size(
   }
 }
 
+static int rtems_flashdev_check_region_valid(
+  rtems_flashdev *flash,
+  rtems_flashdev_region * region
+)
+{
+  size_t erase_size = 0;
+  int status = (flash)->get_erase_size(flash, &erase_size);
+
+  if (0 != status)
+  {
+    return status;
+  }
+  if (region->offset % erase_size || region->size % erase_size)
+  {
+    return -1;
+  }
+
+  return 0;
+}
+
 static uint32_t rtems_flashdev_find_unallocated_region(
   rtems_flashdev_region_table *region_table
 )
diff --git a/testsuites/libtests/flashdev01/init.c b/testsuites/libtests/flashdev01/init.c
index 71ec4ae765..118367a62f 100644
--- a/testsuites/libtests/flashdev01/init.c
+++ b/testsuites/libtests/flashdev01/init.c
@@ -39,7 +39,7 @@
 #define TEST_DATA_SIZE (PAGE_SIZE * PAGE_COUNT)
 #define PAGE_COUNT 16
 #define PAGE_SIZE 128
-#define ERASE_SIZE 4096
+#define ERASE_SIZE 1024
 
 const char rtems_test_name[] = "FLASHDEV 1";
 const char test_string[] = "My test string!";
@@ -115,12 +115,25 @@ static void run_test(void) {
     rtems_test_assert(!status);
     rtems_test_assert(ERASE_SIZE == erase_size);
 
-    /* Test Erasing */
+    /* Test Erasing - this one must fail*/
     e_args.offset = 0x0;
     e_args.size = PAGE_SIZE;
     status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_ERASE, &e_args);
+    rtems_test_assert(status);
+
+    /* Test Erasing - this one must fail*/
+    e_args.offset = 0x1;
+    e_args.size = ERASE_SIZE;
+    status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_ERASE, &e_args);
+    rtems_test_assert(status);
+
+    /* Test Erasing*/
+    e_args.offset = 0x0;
+    e_args.size = ERASE_SIZE;
+    status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_ERASE, &e_args);
     rtems_test_assert(!status);
 
+
     fseek(file, 0x0, SEEK_SET);
     fgets(buff, TEST_DATA_SIZE, file);
     rtems_test_assert(buff[0] == 0);
@@ -189,10 +202,41 @@ static void run_test(void) {
 
   fd = fileno(file);
 
-  /* Test Regions */
-  region.offset = 0x400;
+  /* Prepare the flash */
+  memset(buff,0x55,TEST_DATA_SIZE);
+  status = fwrite(buff, 1, TEST_DATA_SIZE, file);
+  rtems_test_assert(status == TEST_DATA_SIZE);
+  memset(buff,0x00,TEST_DATA_SIZE);
+
+  /* Fseek to start of flash and read again */
+  status = fseek(file, 0x0, SEEK_SET);
+  rtems_test_assert(!status);
+  bytes_read = fread(buff, 1, TEST_DATA_SIZE, file);
+  rtems_test_assert(bytes_read == TEST_DATA_SIZE);
+  memset(buff,0x00,TEST_DATA_SIZE);
+
+  /* Test Regions - this one must fail */
+  region.offset = ERASE_SIZE;
   region.size = 0x200;
   status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_SET_REGION, &region);
+  rtems_test_assert(status);
+
+  /* Test Regions - this one must fail*/
+  region.offset = 0x200;
+  region.size = ERASE_SIZE;
+  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_SET_REGION, &region);
+  rtems_test_assert(status);
+
+  /* Test Regions */
+  region.offset = ERASE_SIZE;
+  region.size = ERASE_SIZE;
+  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_SET_REGION, &region);
+  rtems_test_assert(!status);
+
+  /* Test Erasing*/
+  e_args.offset = 0x0;
+  e_args.size = ERASE_SIZE;
+  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_ERASE, &e_args);
   rtems_test_assert(!status);
 
   /* Test read within then region */
@@ -203,18 +247,28 @@ static void run_test(void) {
 
   /* Test read to larger then region */
   fseek(file, 0x0, SEEK_SET);
+  rtems_test_assert(!status);
   read_data = fgets(buff, 2048, file);
   rtems_test_assert(buff[0] == 0);
 
   /* Test fseek outside of region */
-  status = fseek(file, 0x201, SEEK_SET);
+  fseek(file, 0x0, SEEK_SET);
+  rtems_test_assert(!status);
+  status = fseek(file, ERASE_SIZE+1, SEEK_SET);
   rtems_test_assert(status);
 
   /* Write to base unset region and check the writes location */
   fseek(file, 0x0, SEEK_SET);
   fwrite("HELLO WORLD!!!!!", 1, 16, file);
   ioctl(fd, RTEMS_FLASHDEV_IOCTL_UNSET_REGION, NULL);
-  fseek(file, 0x400, SEEK_SET);
+  /* Test read within then region */
+  status = fseek(file, 0x0, SEEK_SET);
+  rtems_test_assert(!status);
+  bytes_read = fread(buff, 1, 0x200, file);
+  rtems_test_assert(bytes_read == 0x200);
+  rtems_test_assert(&buff[0] == memchr(buff, 0x55, 0x200));
+
+  fseek(file, ERASE_SIZE, SEEK_SET);
   fgets(buff, 16, file);
   rtems_test_assert(strncmp(buff, "HELLO WORLD!!!!!", 16));
 
-- 
2.34.1



More information about the devel mailing list