Nandwrite vs. "mtd write"?

As a few board targets seem to use "nandwrite", but the majority uses package/base-files/files/lib/upgrade/nand.sh for NAND flashes which uses "mtd write" I was wondering if there is a difference between these two commands or if one is preferred or discouraged in specific use-cases. Does this decision make a difference if the underlying device is a UBI partition (default for OpenWrt NAND based devices these days I think) or JFFS2 on an mtd device directly, without UBI?

I checked and saw that "mtd write" seems to support some bad block handling these days, but it seems that was not always the case, right? Is it less safe to use "mtd write" on old/ancient OpenWrts?

(I also have to work with an old industry device with OpenWrt Attitude Adjustment... which hopefully will be updated to a recent OpenWrt one day... but for now we are trying to revive access to and some first polishing. And it seems it was using JFFS2 for both the rootfs /rom and rootfs_data /overlay partitions, without UBI. And was using nandwrite from the mtd-utils package. And that's where basically the "nandwrite" vs. "mtd write" question popped up in my mind. Both using "nandwrite" and "mtd write" seemed to work, but I didn't see any bad block handling code in OpenWrt's "mtd write" in Attitude Adjustment.)

1 Like

I try to answer this question.

After check the source code, I will say there is no substantial difference between them, especially for NAND flash:

  1. Both can skip bad block in NAND, and use the same MEMGETBADBLOCK ioctl.
  2. Both can write data to a mtd partition.

There are some differences AFAIK:

  1. "mtd write" supports other type of flash other than NAND.
  2. You must erase the mtd ("flash_erase") first before use "nandwrite", but "mtd write" does it for you.
  3. "nandwrite" can mark bad block.

In a word, If you code is only run on NAND, "nandwrite" should be more suitable. Otherwise, you can use "mtd write"