linuxembedded-linuxflash-memory

Write data to mtd and mtdblock


I'm trying to write data to MTD device like this topic: Write on a mtd block device

But when I use /dev/mtd0, I just read right first byte, another byte still 0xFF, like:

MTD Type: 3
MTD total size: 200000 bytes
MTD erase size: 1000 bytes
buf[0] = 0xde
buf[1] = 0xff
buf[2] = 0xff
buf[3] = 0xff
...
buf[18] = 0xff
buf[19] = 0xff

When I use /dev/mtdblock0, this working right.

MTD Type: 10
MTD total size: befcfb60 bytes
MTD erase size: 0 bytes
buf[0] = 0xde
buf[1] = 0xad
buf[2] = 0xbe
buf[3] = 0xef
...
buf[18] = 0xbe
buf[19] = 0xef

Furthermore, when I check flash content by hexdump /dev/mtd0 -n64 -C and hexdump /dev/mtdblock0 -n64 -C then the contents of both are same.

Final purpose is use /dev/mtd0 not mtdblock, please help me


Solution

  • I was researched and realize this issue occurs due to my flash device

    flash: flash@0 {
        compatible = "sst,sst25vf016b", "jedec,spi-nor";
        spi-max-frequency = <50000000>;
    };
    

    I saw where performing multi-byte writes to a SST25VF016B would fail. To fix it, we can fix the application from write multi-byte to write every byte like:

    lseek(fd, 0x150000, SEEK_SET);
    for (i = 0; i < 32; i++)
        result = write(fd, writedata[i], sizeof(writedata[i]));
    

    Or we can fix into kernel with reference patches at: https://lore.kernel.org/lkml/20200911144703.25409-2-m.felsch@pengutronix.de/T/