u-boot/test/dm/nand.c

105 lines
2.6 KiB
C
Raw Normal View History

nand: Add sandbox driver Add a sandbox NAND flash driver to facilitate testing. This driver supports any number of devices, each using a single chip-select. The OOB data is stored in-band, with the separation enforced through the API. For now, create two devices to test with. The first is a very small device with basic ECC. The second is an 8G device (chosen to be larger than 32 bits). It uses ONFI, with the values copied from the datasheet. It also doesn't need too strong ECC, which speeds things up. Although the nand subsystem determines the parameters of a chip based on the ID, the driver itself requires devicetree properties for each parameter. We do not derive parameters from the ID because parsing the ID is non-trivial. We do not just use the parameters that the nand subsystem has calculated since that is something we should be testing. An exception is made for the ECC layout, since that is difficult to encode in the device tree and is not a property of the device itself. Despite using file I/O to access the backing data, we do not support using external files. In my experience, these are unnecessary for testing since tests can generally be written to write their expected data beforehand. Additionally, we would need to store the "programmed" information somewhere (complicating the format and the programming process) or try to detect whether block are erased at runtime (degrading probe speeds). Information about whether each page has been programmed is stored in an in-memory buffer. To simplify the implementation, we only support a single program per erase. While this is accurate for many larger flashes, some smaller flashes (512 byte) support multiple programs and/or subpage programs. Support for this could be added later as I believe some filesystems expect this. To test ECC, we support error-injection. Surprisingly, only ECC bytes in the OOB area are protected, even though all bytes are equally susceptible to error. Because of this, we take care to only corrupt ECC bytes. Similarly, because ECC covers "steps" and not the whole page, we must take care to corrupt data in the same way. Signed-off-by: Sean Anderson <seanga2@gmail.com>
2023-11-04 20:37:52 +00:00
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
*/
#include <nand.h>
#include <part.h>
#include <rand.h>
#include <dm/test.h>
#include <test/test.h>
#include <test/ut.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/rawnand.h>
static int dm_test_nand(struct unit_test_state *uts, int dev, bool end)
{
nand_erase_options_t opts = { };
struct mtd_info *mtd;
size_t length;
loff_t size;
char *buf;
int *gold;
u8 oob[NAND_MAX_OOBSIZE];
int i;
loff_t off = 0;
mtd_oob_ops_t ops = { };
/* Seed RNG for bit errors */
srand((off >> 32) ^ off ^ ~dev);
mtd = get_nand_dev_by_index(dev);
ut_assertnonnull(mtd);
size = mtd->erasesize * 4;
length = size;
buf = malloc(size);
ut_assertnonnull(buf);
gold = malloc(size);
ut_assertnonnull(gold);
/* Mark a block as bad */
ut_assertok(mtd_block_markbad(mtd, off + mtd->erasesize));
/* Erase some stuff */
if (end)
off = mtd->size - size - mtd->erasesize;
opts.offset = off;
opts.length = size;
opts.spread = 1;
opts.lim = U32_MAX;
ut_assertok(nand_erase_opts(mtd, &opts));
/* Make sure everything is erased */
memset(gold, 0xff, size);
ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
ut_asserteq(size, length);
ut_asserteq_mem(gold, buf, size);
/* ...but our bad block marker is still there */
ops.oobbuf = oob;
ops.ooblen = mtd->oobsize;
ut_assertok(mtd_read_oob(mtd, mtd->erasesize, &ops));
ut_asserteq(0, oob[mtd_to_nand(mtd)->badblockpos]);
/* Generate some data and write it */
for (i = 0; i < size / sizeof(int); i++)
gold[i] = rand();
ut_assertok(nand_write_skip_bad(mtd, off, &length, NULL, U64_MAX,
(void *)gold, 0));
ut_asserteq(size, length);
/* Verify */
ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
ut_asserteq(size, length);
ut_asserteq_mem(gold, buf, size);
/* Erase some blocks */
memset(((char *)gold) + mtd->erasesize, 0xff, mtd->erasesize * 2);
opts.offset = off + mtd->erasesize;
opts.length = mtd->erasesize * 2;
ut_assertok(nand_erase_opts(mtd, &opts));
/* Verify */
ut_assertok(nand_read_skip_bad(mtd, off, &length, NULL, U64_MAX, buf));
ut_asserteq(size, length);
ut_asserteq_mem(gold, buf, size);
return 0;
}
#define DM_NAND_TEST(dev) \
static int dm_test_nand##dev##_start(struct unit_test_state *uts) \
{ \
return dm_test_nand(uts, dev, false); \
} \
DM_TEST(dm_test_nand##dev##_start, UT_TESTF_SCAN_FDT); \
static int dm_test_nand##dev##_end(struct unit_test_state *uts) \
{ \
return dm_test_nand(uts, dev, true); \
} \
DM_TEST(dm_test_nand##dev##_end, UT_TESTF_SCAN_FDT)
DM_NAND_TEST(0);
DM_NAND_TEST(1);