Skip to content

Commit 1568cbf

Browse files
committed
tests: disk: disk_access: test disk_access_erase
Test the disk erase functionality. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent bcfd8d8 commit 1568cbf

File tree

2 files changed

+111
-14
lines changed

2 files changed

+111
-14
lines changed

tests/drivers/disk/disk_access/README.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ disk devices as well. The test has the following phases:
2525
of various length to various sectors (once again, the driver must reject
2626
writes that would be outside the bounds of the disk), then performs multiple
2727
writes to the same location.
28+
29+
* Erase test: Verifies that the driver can consistently erase sectors. This test
30+
follows the same flow as the write test, but at each step erases the data
31+
written to the disk and reads it back to ensure all data is 0x00 or 0xFF. The
32+
test first performs writes of various length to various sectors (once again,
33+
the driver must reject erases that would be outside the bounds of the disk),
34+
then performs multiple erases to the same location.

tests/drivers/disk/disk_access/src/main.c

Lines changed: 104 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@
4747
#define SECTOR_SIZE 512
4848

4949
/* 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
5455

5556
#define OVERFLOW_CANARY 0xDE
5657

@@ -59,10 +60,10 @@ static uint32_t disk_sector_count;
5960
static uint32_t disk_sector_size;
6061

6162
/* + 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];
6364

6465
#ifdef CONFIG_DISK_DRIVER_LOOPBACK
65-
#define BACKING_PATH "/"DISK_NAME_PHYS":"
66+
#define BACKING_PATH "/" DISK_NAME_PHYS ":"
6667

6768
static struct loopback_disk_access lo_access;
6869
static FATFS fat_fs;
@@ -126,7 +127,7 @@ static void test_setup(void)
126127
* just verify our assumed maximum size
127128
*/
128129
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");
130131
}
131132

132133
/* 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)
139140
rc = disk_access_read(disk_pdrv, buf, start, num_sectors);
140141
/* Check canary */
141142
zassert_equal(buf[num_sectors * disk_sector_size], OVERFLOW_CANARY,
142-
"Read overflowed requested length");
143+
"Read overflowed requested length");
143144
return rc; /* Let calling function check return code */
144145
}
145146

@@ -173,8 +174,7 @@ static void test_sector_read(uint8_t *buf, uint32_t num_sectors)
173174
/* Write sector of disk, and check the data to ensure it is valid
174175
* WARNING: this test is destructive- it will overwrite data on the disk!
175176
*/
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)
178178
{
179179
int rc, i;
180180

@@ -195,10 +195,34 @@ static int write_sector_checked(uint8_t *wbuf, uint8_t *rbuf,
195195
}
196196
/* Check the read data versus the written data */
197197
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");
199199
return rc;
200200
}
201201

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+
202226
/* Tests writing to a variety of sectors
203227
* WARNING: this test is destructive- it will overwrite data on the disk!
204228
*/
@@ -228,6 +252,49 @@ static void test_sector_write(uint8_t *wbuf, uint8_t *rbuf, uint32_t num_sectors
228252
}
229253
}
230254

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+
231298
/* Test multiple reads in series, and reading from a variety of blocks */
232299
ZTEST(disk_driver, test_read)
233300
{
@@ -248,9 +315,8 @@ ZTEST(disk_driver, test_read)
248315
memset(scratch_buf[1], 0xff, SECTOR_COUNT1 * disk_sector_size);
249316
rc = read_sector(scratch_buf[1], 0, SECTOR_COUNT1);
250317
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");
254320
}
255321
}
256322

@@ -275,6 +341,30 @@ ZTEST(disk_driver, test_write)
275341
}
276342
}
277343

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+
278368
static void *disk_driver_setup(void)
279369
{
280370
#ifdef CONFIG_DISK_DRIVER_LOOPBACK

0 commit comments

Comments
 (0)