Sunday 3 February 2019

Common MTD commands : Boot EEPROM/ NAND flash access from Linux


Common MTD Commands use wisely

  •  cat /proc/mtd

dev:    size   erasesize  name
mtd0: 000c0000 00001000 "U-Boot"
mtd1: 00002000 00001000 "U-Boot Env"
mtd2: 00001000 00001000 "JFFS2"
mtd3: 0013d000 00001000 "my-data"
mtd4: 0013e000 00001000 "U-image/Kernel"
mtd5: 0013f000 00001000 "rootfs"
mtd6: 00140000 00001000 "mylog"
mtd7: 00141000 00001000 "mydata"

  •  mtdinfo -a
  • ls -l /dev/mtd*
  •  flash_unlock could be used to unlock the appropriate sectors.
  • flash_eraseall /dev/mtdchar0 
    •  cat /dev/mtdchar0 < new.bin  ( Erase and load new file)
  • cat /dev/mtdchar0 > /tmp/flash.bin ( Entire flash to file) 
  • flashcp to write to NOR flash
  • flash_eraseall -j /dev/mtd2
    mount -t jffs2 /dev/mtdblock2 /mnt/flash
  • flash_eraseall /dev/mtd0 ( erases everything, use rarely)
IOCTL Commands
  • MEMGETINFO Get layout and capabilities struct mtd_info_user *
  • MEMGETREGIONINFO struct region_info_user *
  • MEMWRITEOOB NAND only: write out-of-band info (ECC) struct mtd_oob_buf *
  • MEMERASE Erase flash blocks struct erase_info_user *
  • MEMLOCK Lock flash blocks to disallow changes struct erase_info_user *
  • MEMUNLOCK Unlock flash to allow changes struct erase_info_user *
  • MEMGETREGIONCOUNT Return number of erase block regions int *
  • MEMREADOOB NAND only: read out-of-band info (ECC) struct mtd_oob_buf *
  • MEMSETOOBSEL NAND only: set default OOB info struct nand_oobinfo*
  •  The exact flash layout may be determined using the MEMGETREGIONCOUNT and MEMGETREGIONINFO commands.
  • MEMERASE command may be issued to erase the desired blocks.