Using the NAND flash on NanosG20

Creating and using an UBI flash partition

The following steps are needed to use the internal flash:

  1. Boot into the Debian system.
  2. Attach mtd device 2 with ubi:
    ubiattach /dev/ubictrl -m 2

    You have now a new device called /dev/ubi0.

  3. Create a new ubi volume
    ubimkvol /dev/ubi0 -N data -m

    This create a volume with the name "data" which uses the complete flash space. If you want to create multiple volumes, replace "-m" with "-s size". See "ubimkvol --help" for more information. After that, you will have a new device call /dev/ubi0_0.

  4. Mount this device:
    mount /dev/ubi0_0 /mnt -t ubifs

    or by using the volume name

    mount ubi0:data /mnt -t ubifs

    Now you can write to /mnt or another path you use to mount the device to write files on the flash.

Repeat steps 2 and 4 on every boot to get access to the files. You can automate both steps:

  1. Add " ubi.mtd=2" to the end of the line in /boot/cmdline.
  2. Add the line
    /dev/ubi0_0 /mnt ubifs defaults 0 0

    or by using the volume name

    ubi0:data /mnt ubifs defaults 0 0

    to /etc/fstab

If you want to completely erase a volume run the following command:

ubiupdatevol device -t -

where device could again be the direct device file (e.g. /dev/ubi0_0) or the volume name description (e.g. ubi0:data).

It might make sense to use a different name and/or mount point, but that is your decision.

Using the flash as the root filesystem

It is possible to use the NAND partition just created as the root filesystem. First you have to copy the rootfs contents to partition. If you have a tar archive with the contents, it should be as easy as the following command (assumed the filesystem is mounted on /mnt and the tarball is called rootfs.tar):

tar xvf rootfs.tar -C /mnt

If you want to use the current content of your SD card, you have two options:

  • Create a tarball on a different Linux computer (make sure that you are root so that every file is accessible during the creation of the archive)
  • Directly copy the contents from the SD card to the flash. This should be done from the bootloader shell and not from the running system as there multiple (virtual) filesystems mounted on top of the rootfs which make it difficult to produce an correct copy. To do that, hold the power on button down while booting. A menu should appear on the serial console. Choose "4 - Drop to shell". Now you can mount both the NAND partition and the SD card and copy the contents. You might need to created new mount point. The following commands should do the trick:
    mkdir /mnt/flash
    mkdir /mnt/sd
    mount /dev/ubi0_0 /mnt/flash -t ubifs
    mount /dev/mmcblk0p1 /mnt/sd
    cp -a /mnt/sd/* /mnt/flash

Before this partition is really used as the root filesystem, make sure the boot parameters have been adjusted. Edit the file boot/cmdline in the flash partition. It should look like this if you named your partition "rootfs":

console=ttyS1,115200 mmc_core.removable=0 ubi.mtd=2 root=ubi0:rootfs rootfstype=ubifs

Now unmount the flash and the SD card if you are currently in the bootloader exit the shell and choose "0 - Continue with autoboot". If you have done the procedure in the Debian system, just reboot. If everything is correct, the system should now boot only from flash.

One additional note: You should try to strip down the system before puttin it to flash. Unneeded software, man pages and other documentation just waste memory, especially on the NanosG20 with only 128MB flash.