47
47
#define SECTOR_SIZE 512
48
48
49
49
/* Sector counts to read */
50
- #define SECTOR_COUNT1 8
51
- #define SECTOR_COUNT2 1
52
- #define SECTOR_COUNT3 29
53
- #define SECTOR_COUNT4 31
50
+ #define SECTOR_COUNT1 8
51
+ #define SECTOR_COUNT2 1
52
+ #define SECTOR_COUNT3 29
53
+ #define SECTOR_COUNT4 31
54
+ #define SECTOR_COUNT_MAX 32
54
55
55
56
#define OVERFLOW_CANARY 0xDE
56
57
@@ -59,10 +60,10 @@ static uint32_t disk_sector_count;
59
60
static uint32_t disk_sector_size ;
60
61
61
62
/* + 4 to make sure the second buffer is dword-aligned for NVME */
62
- static uint8_t scratch_buf [2 ][SECTOR_COUNT4 * SECTOR_SIZE + 4 ];
63
+ static uint8_t scratch_buf [2 ][SECTOR_COUNT_MAX * SECTOR_SIZE + 4 ];
63
64
64
65
#ifdef CONFIG_DISK_DRIVER_LOOPBACK
65
- #define BACKING_PATH "/"DISK_NAME_PHYS":"
66
+ #define BACKING_PATH "/" DISK_NAME_PHYS ":"
66
67
67
68
static struct loopback_disk_access lo_access ;
68
69
static FATFS fat_fs ;
@@ -126,7 +127,7 @@ static void test_setup(void)
126
127
* just verify our assumed maximum size
127
128
*/
128
129
zassert_true (cmd_buf <= SECTOR_SIZE ,
129
- "Test will fail, SECTOR_SIZE definition must be increased" );
130
+ "Test will fail, SECTOR_SIZE definition must be increased" );
130
131
}
131
132
132
133
/* Reads sectors, verifying overflow does not occur */
@@ -139,7 +140,7 @@ static int read_sector(uint8_t *buf, uint32_t start, uint32_t num_sectors)
139
140
rc = disk_access_read (disk_pdrv , buf , start , num_sectors );
140
141
/* Check canary */
141
142
zassert_equal (buf [num_sectors * disk_sector_size ], OVERFLOW_CANARY ,
142
- "Read overflowed requested length" );
143
+ "Read overflowed requested length" );
143
144
return rc ; /* Let calling function check return code */
144
145
}
145
146
@@ -173,8 +174,7 @@ static void test_sector_read(uint8_t *buf, uint32_t num_sectors)
173
174
/* Write sector of disk, and check the data to ensure it is valid
174
175
* WARNING: this test is destructive- it will overwrite data on the disk!
175
176
*/
176
- static int write_sector_checked (uint8_t * wbuf , uint8_t * rbuf ,
177
- uint32_t start , uint32_t num_sectors )
177
+ static int write_sector_checked (uint8_t * wbuf , uint8_t * rbuf , uint32_t start , uint32_t num_sectors )
178
178
{
179
179
int rc , i ;
180
180
@@ -195,10 +195,34 @@ static int write_sector_checked(uint8_t *wbuf, uint8_t *rbuf,
195
195
}
196
196
/* Check the read data versus the written data */
197
197
zassert_mem_equal (wbuf , rbuf , num_sectors * disk_sector_size ,
198
- "Read data did not match data written to disk" );
198
+ "Read data did not match data written to disk" );
199
199
return rc ;
200
200
}
201
201
202
+ static int erase_sector_checked (uint8_t * rbuf , uint32_t start , uint32_t num_sectors )
203
+ {
204
+ int rc , i ;
205
+
206
+ /* Erase the specified sectors */
207
+ rc = disk_access_erase (disk_pdrv , start , num_sectors );
208
+ if (rc ) {
209
+ return rc ; /* Let calling function handle disk error */
210
+ }
211
+
212
+ /* Read the erased sectors */
213
+ rc = read_sector (rbuf , start , num_sectors );
214
+ if (rc ) {
215
+ return rc ;
216
+ }
217
+
218
+ /* All data should be equal 0x00 or 0xFF */
219
+ for (i = 0 ; i < num_sectors * disk_sector_size ; i ++ ) {
220
+ zassert_true ((rbuf [i ] == 0x00 ) || (rbuf [i ] == 0xFF ),
221
+ "Data not erased from disk sector %u" , start + i );
222
+ }
223
+ return 0 ;
224
+ }
225
+
202
226
/* Tests writing to a variety of sectors
203
227
* WARNING: this test is destructive- it will overwrite data on the disk!
204
228
*/
@@ -228,6 +252,49 @@ static void test_sector_write(uint8_t *wbuf, uint8_t *rbuf, uint32_t num_sectors
228
252
}
229
253
}
230
254
255
+ /* Tests erasing a variety of sectors
256
+ * WARNING: this test is destructive- it will overwrite data on the disk!
257
+ */
258
+ static void test_sector_erase (uint8_t * wbuf , uint8_t * rbuf , uint32_t num_sectors )
259
+ {
260
+ int rc , sector ;
261
+
262
+ TC_PRINT ("Testing erase of %u sectors\n" , num_sectors );
263
+ /* Write and erase disk sector zero */
264
+ rc = write_sector_checked (wbuf , rbuf , 0 , num_sectors );
265
+ zassert_equal (rc , 0 , "Failed to write to sector zero" );
266
+ rc = erase_sector_checked (rbuf , 0 , num_sectors );
267
+ zassert_equal (rc , 0 , "Failed to erase sector zero" );
268
+
269
+ /* Write and erase sectors in the "middle" of the disk */
270
+ if (disk_sector_count / 2 > num_sectors ) {
271
+ sector = disk_sector_count / 2 - num_sectors ;
272
+ } else {
273
+ sector = 0 ;
274
+ }
275
+ rc = write_sector_checked (wbuf , rbuf , sector , num_sectors );
276
+ zassert_equal (rc , 0 , "Failed to write to mid disk sector" );
277
+ rc = erase_sector_checked (rbuf , sector , num_sectors );
278
+ zassert_equal (rc , 0 , "Failed to erase mid disk sector" );
279
+
280
+ /* Write and erase the last sector */
281
+ rc = write_sector_checked (wbuf , rbuf , disk_sector_count - num_sectors , num_sectors );
282
+ zassert_equal (rc , 0 , "Failed to write to last sector" );
283
+ rc = erase_sector_checked (rbuf , disk_sector_count - num_sectors , num_sectors );
284
+ zassert_equal (rc , 0 , "Failed to erase last sector" );
285
+
286
+ /* Try and erase past the last sector */
287
+ rc = erase_sector_checked (rbuf , disk_sector_count - num_sectors + 1 , num_sectors );
288
+ zassert_equal (rc , - EINVAL ,
289
+ "Unexpected error code when attempting to erase past end of disk" );
290
+ rc = erase_sector_checked (rbuf , disk_sector_count + 1 , num_sectors );
291
+ zassert_equal (rc , - EINVAL ,
292
+ "Unexpected error code when attempting to erase past end of disk" );
293
+ rc = erase_sector_checked (rbuf , UINT32_MAX , num_sectors );
294
+ zassert_equal (rc , - EINVAL ,
295
+ "Unexpected error code when attempting to erase past end of disk" );
296
+ }
297
+
231
298
/* Test multiple reads in series, and reading from a variety of blocks */
232
299
ZTEST (disk_driver , test_read )
233
300
{
@@ -248,9 +315,8 @@ ZTEST(disk_driver, test_read)
248
315
memset (scratch_buf [1 ], 0xff , SECTOR_COUNT1 * disk_sector_size );
249
316
rc = read_sector (scratch_buf [1 ], 0 , SECTOR_COUNT1 );
250
317
zassert_equal (rc , 0 , "Failed to read from disk at same sector location" );
251
- zassert_mem_equal (scratch_buf [1 ], scratch_buf [0 ],
252
- SECTOR_COUNT1 * disk_sector_size ,
253
- "Multiple reads mismatch" );
318
+ zassert_mem_equal (scratch_buf [1 ], scratch_buf [0 ], SECTOR_COUNT1 * disk_sector_size ,
319
+ "Multiple reads mismatch" );
254
320
}
255
321
}
256
322
@@ -275,6 +341,30 @@ ZTEST(disk_driver, test_write)
275
341
}
276
342
}
277
343
344
+ /* Test multiple erases in series, and erasing from a variety of blocks */
345
+ ZTEST (disk_driver , test_erase )
346
+ {
347
+ int rc , i ;
348
+
349
+ /* Skip the test if erasing not supported by the driver */
350
+ if (disk_access_erase (disk_pdrv , 0 , 1 ) == - EINVAL ) {
351
+ ztest_test_skip ();
352
+ return ;
353
+ }
354
+
355
+ /* Verify a range of erase sizes work */
356
+ test_sector_erase (scratch_buf [0 ], scratch_buf [1 ], 8 );
357
+ test_sector_erase (scratch_buf [0 ], scratch_buf [1 ], 16 );
358
+ test_sector_erase (scratch_buf [0 ], scratch_buf [1 ], 24 );
359
+ test_sector_erase (scratch_buf [0 ], scratch_buf [1 ], 32 );
360
+
361
+ /* Verify that multiple erases to the same location work */
362
+ for (i = 0 ; i < 10 ; i ++ ) {
363
+ rc = erase_sector_checked (scratch_buf [1 ], 0 , 16 );
364
+ zassert_equal (rc , 0 , "Failed to erase sector zero" );
365
+ }
366
+ }
367
+
278
368
static void * disk_driver_setup (void )
279
369
{
280
370
#ifdef CONFIG_DISK_DRIVER_LOOPBACK
0 commit comments