Sample Header Ad - 728x90

How to read NAND OOB (including ECC) data in Linux?

0 votes
1 answer
729 views
I am working on an embedded Linux system (5.10.24), where there is a NAND FLASH as storage. The NAND FLASH supports internal ECC and has 128Bytes OOB. Its DS says if the ECC is enabled the last 64Bytes are used as ECC, and the whole 128Bytes OOB could be read. So I wrote a C program in Linux to read the OOB as follows,
fd = open(argv, O_RDWR | O_SYNC);
    if (fd < 0) {
        perror("Failed to open MTD device");
        return -1;
    }

    if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
        perror("Failed to get MTD device info");
        close(fd);
        return -1;
    }

    printf("OOBSIZE: %d\n", mtd_info.oobsize);

    oob_buf.start = atoi(argv);
    oob_buf.length = 128;                 // Hard code to 128B
    oob_buf.ptr = malloc(oob_buf.length);

    rc = ioctl(fd, MEMREADOOB, &oob_buf);
    if (rc < 0) {
        perror("Failed to read OOB data");
        close(fd);
        return -1;
    }

    for (int i = 0; i < oob_buf.length; i++) {
        printf("%02X ", oob_buf.ptr[i]);
        if ((i%0x10) == 0xf)
           printf("\n");
    }
    printf("\n");
Then, I ran the code as follows,
# /tmp/nandoob /dev/mtd1 2048
OOBSIZE: 64
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
91 FF 19 A7 2E E2 62 C0 0D 36 24 42 78 86 FF FF
20 FF 53 0A 08 23 3B 06 CF BD 31 E7 B0 75 FF FF
BB FF 35 83 F3 AD 39 08 7A 9A 05 9B 98 8E FF FF
E1 FF F7 C1 23 58 A7 DD 8F 26 DC 3A 69 72 FF FF
I am not sure if the data read is correct or NOT. I tried the nand dump.oob 0x2048 in u-boot, but I got 64 Bytes of 0xFF, no ECC data read. So is my code to dump/read the whole 128Byte OOB correct? If not, how to read the whole NAND OOB in Linux?
Asked by wangt13 (631 rep)
Nov 1, 2023, 08:58 AM
Last activity: Nov 1, 2023, 12:48 PM