user space utility to count NAND bad block

navigator.atreides at gmail.com navigator.atreides at gmail.com
Thu Nov 8 11:00:09 EST 2007


I want to count number of the bad blocks on the nand device, so being
inspired by ~/mtd/util/flash_eraseall.c I use follow call

int ret = ioctl(fd, MEMGETBADBLOCK, &offset);

as badness measure for block ( if (ret > 0) ).

Could you please point me out if I'm in the right direction?

Thank you in advance!
Valentine R.

p.s. scratch code is below



int main(int argc, char *argv[])
{
    int md_fd;
    int iNumOfBadBlocks = 0;
    mtd_info_t  mtd_meminfo;

    ...

    // open & get meminfo for "/dev/mtd4"
    if ((md_fd = open(mtd_device, O_RDONLY)) < 0) {
        printf("\n\n%s: %s: %s\n", exe_name, mtd4_device, strerror(errno));
        exit(1);
    }

    if (ioctl(md_fd, MEMGETINFO, &mtd_meminfo) != 0) {
        printf("\n\n%s: %s: unable to get MTD device info\n",
exe_name, mtd4_device);
        exit(1);
    }
    iNumOfBadBlocks = get_bb_number( md_fd, mtd_meminfo);
    printf("Number of the bad blocks is %d\n", iNumOfBadBlocks);
    return 0;
}

 int get_bb_number(int fd, const mtd_info_t *meminfo)
{
    int isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0;
    int ibbCounter = 0;
    erase_info_t erase;
    erase.length = meminfo->erasesize;

    for (erase.start = 0;
         erase.start < meminfo->size;
         erase.start += meminfo->erasesize)
    {

        loff_t offset = erase.start;
        int ret = ioctl(fd, MEMGETBADBLOCK, &offset);
        if (ret > 0)
        {
            ibbCounter++;
            continue;
        }
        else if (ret < 0)
        {
            if (errno == EOPNOTSUPP)
            {
                if (isNAND)
                {
                    printf("\n\n%s: %s: Bad block check not
available\n", exe_name, mtd4_device);
                    exit(1);
                }
            }
            else
            {
                printf("\n%s: %s: MTD get bad block failed: %s\n",
exe_name, mtd4_device, strerror(errno));
                exit(1);
            }
        }
    }

    return ibbCounter;
}



More information about the linux-mtd mailing list