Difference between revisions of "Repartitioning NAND Flash with JFFS2 for Linux"

From wiki.emacinc.com
Jump to: navigation, search
(added outline of remaining content)
(adding process steps)
Line 74: Line 74:
  
 
== Modifying the Partition Table ==
 
== Modifying the Partition Table ==
 +
If you need to specify additional partitions or change the sizes of the existing partitions, the process is more involved. To do this, you will need to archive the root filesystem, change the command line partition table specification, reprogram the root filesystem, and configure the system. Each of these steps are outlined below.
  
=== Backup the Root Filesystem ===
+
=== Archive the Root Filesystem ===
 +
If you do not already have an archive of the root filesystem on the board, it will be necessary to create an archive now so that the root filesystem can be restored after the flash partitions have been modified.
 +
 
 +
# Follow the instructions in [[Archiving JFFS2 Images]] to create a binary copy of the partition.
 +
# Use the procedure in [[Mounting JFFS2 Images on a Linux PC]] to mount the JFFS2 partition on a Linux PC.
 +
# Finally, create a new JFFS2 and summary image following the [[Creating JFFS2 Images]] procedure.
  
 
=== Specify the new Partition Table ===
 
=== Specify the new Partition Table ===
 +
 +
The new partition table must be specified through the kernel command line passed from the bootloader. U-boot uses the <code>bootargs</code> environment variable to control this setting. In this example, the root partition will be resized from 128MB to 100MB, an additional data partition of 64MB will be created, and the rest of the flash will be reserved for an auxiliary partition. The resulting partition table specification would look like this:
 +
mtdparts=atmel_nand:100M(root),64M(data),-(aux)
 +
 +
Follow the steps below to modify the <code>bootargs</code> variable with the new partition information.
 +
# Connect to the target board through the serial console.
 +
# Reset the board and press Enter when the U-Boot messages appear on the serial terminal to interrupt the boot process. A <code>U-Boot></code> prompt should appear.
 +
# View the current <code>bootargs</code> variable using the <code>printenv</code> as shown below: <syntaxhighlight lang=bash>
 +
U-Boot> printenv bootargs
 +
console=ttyS0,115200 root=/dev/mtdblock0 mtdparts=atmel_nand:128M(root),-(aux) ro rootfstype=jffs2 video=AMPIRE
 +
</syntaxhighlight>
 +
# Modify the bootargs variable and replace the <code>mtdparts</code> definition with the new settings:<syntaxhighlight lang=bash>
 +
U-Boot> setenv bootargs ' console=ttyS0,115200 root=/dev/mtdblock0 mtdparts=atmel_nand:100M(root),64M(data),-(aux) ro rootfstype=jffs2 video=AMPIRE'
 +
U-Boot> saveenv
 +
</syntaxhighlight>
 +
# Erase the NAND flash to clear the existing partitions on the flash:<syntaxhighlight lang=bash>U-Boot> nand erase
 +
</syntaxhighlight>
  
 
=== Reload the Root Partition ===
 
=== Reload the Root Partition ===
 +
Follow the instructions in [[Loading Images with U-Boot]] to load the archived root partition back onto the NAND flash. You will not need to reload the kernel. Once this is complete, restart the board and verify that it boots properly.
  
 
=== Configure the System ===
 
=== Configure the System ===
  
 
+
The final step in the
  
 
[[Category:Linux]]
 
[[Category:Linux]]
 
[[Category:Filesystems]]
 
[[Category:Filesystems]]

Revision as of 09:18, 25 April 2013

Many of EMAC's ARM processor-based systems utilize NAND flash with JFFS2 filesystems. On these systems, the NAND flash is partitioned using the Linux MTD "Command line partition table parsing" feature. This allows the partition table for the flash to be specified on the kernel command line passed from the bootloader. By default, EMAC OE NAND flash images provide one partition for the root filesystem mounted read-only, and a second read/write auxiliary partition mounted on /root. This article describes how to modify the partition table and how to utilize and configure additional partitions.

Command Line Partition Table

EMAC OE NAND flash-based systems utilize MTD command line partition table parsing to specify the partition table for the flash. This is different from what you may be used to with other disks where the partition table is stored in the MBR. When the system is booted, the kernel looks at the partition table specified and creates device nodes corresponding to each partition at the given size or offset. These can be accessed like any other partition, but no information about the actual partition table is stored on the disk itself.

A brief description of the format and options for the partition table command line taken from the drivers/mtd/Kconfig is listed below:

The format for the command line is as follows:

mtdparts=<mtddef>[;<mtddef]
<mtddef>  := <mtd-id>:<partdef>[,<partdef>]
<partdef> := <size>[@offset][<name>][ro]
<mtd-id>  := unique id used in mapping driver/device
<size>    := standard linux memsize OR "-" to denote all
remaining space
<name>    := (NAME)

Due to the way Linux handles the command line, no spaces are
allowed in the partition definition, including mtd id's and partition
names.

Although the kernel command line is specified in the bootloader (the bootargs variable in U-Boot), the command line can be viewed through the /proc/cmdline file on a running Linux system. The example below illustrates the command line on a system. Note that the settings on your system will most likely differ from the configuration here depending on the required size for the root filesystem, processor type, and other variables.

root@emac-oe:~# cat /proc/cmdline
console=ttyS0,115200 root=/dev/mtdblock0 mtdparts=atmel_nand:128M(root),-(aux) ro rootfstype=jffs2 video=AMPIRE

In the example above, the mtdparts parameter specifies the partition table for one device: atmel_nand. This device name will vary depending on the type of processor on the board and the flash driver name. The first partition is 128MB and is named "root". Following that is a partition named "aux" which takes up the remaining space on the flash.

One method for viewing which device nodes correspond to each of these partitions is to view the /proc/mtd file. The partition names specified in the command line are reflected in the /proc/mtd contents as shown below. In this example, you can see that mtd0 corresponds to "root", and mtd1 corresponds to "aux". The remaining devices on this system are on the SPI DataFlash device, which uses a hard-coded partition table.

root@emac-oe:~# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 08000000 00020000 "root"
mtd1: 08000000 00020000 "aux"
mtd2: 00042000 00000210 "df_boot"
mtd3: 00210000 00000210 "df_kernel"
mtd4: 001ce000 00000210 "df_aux"

The /etc/fstab file specifies how the NAND flash partitions should be mounted. For example, the system used for this example contains the following entries for the NAND flash partitions:

/dev/mtdblock0       /                    jffs2      ro                    0  0
/dev/mtdblock1       /root                jffs2      rw                    0  0

This configures the system to mount the root filesystem read-only, and to mount the "aux" partition read/write on /root.

The usage statistics for each filesystem can be viewed through the df command as listed below. This information can be helpful in determining the size requirements for the root partition.

root@emac-oe:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root               128.0M     90.8M     37.2M  71% /
udev                     61.5M     76.0K     61.4M   0% /dev
media                     2.0M         0      2.0M   0% /media
/dev/mmcblk0p1            1.8G      2.3M      1.8G   0% /media/mmcblk0p1
/dev/mtdblock1          128.0M      3.0M    125.0M   2% /root
tmpfs                    61.5M     88.0K     61.4M   0% /var/volatile
tmpfs                    61.5M         0     61.5M   0% /dev/shm

Changing the Mount Point of the Auxiliary Partition

If existing partition scheme is acceptable for your application but you want to use the auxiliary partition for data logging rather than general storage and development, the easiest solution is to simply change the mount point of the second partition. The following steps can be used to do this:

  1. Mount the flash read/write and create the new mount point, /mnt/data in this example.
    root@emac-oe:~# mount -o remount,rw /
    root@emac-oe:~# mkdir /mnt/data
    
  2. Edit the /etc/fstab file to change the mount point of the auxiliary partition. The device node for the data partition will vary depending on the hardware. Simply find the entry for /root and change it to /mnt/data. (See the EMAC OE Getting Started Guide for more information on how to edit a file.) After modifying the system used for this example, the entry for the data partition is as follows:
    /dev/mtdblock1       /mnt/data            jffs2      rw                    0  0
    
  3. To complete the process, remount the root flash read only, and test the new fstab configuration for the data partition.
    root@emac-oe:~# mount -o remount,ro /
    root@emac-oe:~# cd /
    root@emac-oe:~# umount /root
    root@emac-oe:~# mount /mnt/data
    root@emac-oe:~# mount | grep mtdblock1
    /dev/mtdblock1 on /mnt/data type jffs2 (rw,relatime)
    

Modifying the Partition Table

If you need to specify additional partitions or change the sizes of the existing partitions, the process is more involved. To do this, you will need to archive the root filesystem, change the command line partition table specification, reprogram the root filesystem, and configure the system. Each of these steps are outlined below.

Archive the Root Filesystem

If you do not already have an archive of the root filesystem on the board, it will be necessary to create an archive now so that the root filesystem can be restored after the flash partitions have been modified.

  1. Follow the instructions in Archiving JFFS2 Images to create a binary copy of the partition.
  2. Use the procedure in Mounting JFFS2 Images on a Linux PC to mount the JFFS2 partition on a Linux PC.
  3. Finally, create a new JFFS2 and summary image following the Creating JFFS2 Images procedure.

Specify the new Partition Table

The new partition table must be specified through the kernel command line passed from the bootloader. U-boot uses the bootargs environment variable to control this setting. In this example, the root partition will be resized from 128MB to 100MB, an additional data partition of 64MB will be created, and the rest of the flash will be reserved for an auxiliary partition. The resulting partition table specification would look like this:

mtdparts=atmel_nand:100M(root),64M(data),-(aux)

Follow the steps below to modify the bootargs variable with the new partition information.

  1. Connect to the target board through the serial console.
  2. Reset the board and press Enter when the U-Boot messages appear on the serial terminal to interrupt the boot process. A U-Boot> prompt should appear.
  3. View the current bootargs variable using the printenv as shown below:
    U-Boot> printenv bootargs
    console=ttyS0,115200 root=/dev/mtdblock0 mtdparts=atmel_nand:128M(root),-(aux) ro rootfstype=jffs2 video=AMPIRE
    
  4. Modify the bootargs variable and replace the mtdparts definition with the new settings:
    U-Boot> setenv bootargs ' console=ttyS0,115200 root=/dev/mtdblock0 mtdparts=atmel_nand:100M(root),64M(data),-(aux) ro rootfstype=jffs2 video=AMPIRE'
    U-Boot> saveenv
    
  5. Erase the NAND flash to clear the existing partitions on the flash:
    U-Boot> nand erase
    

Reload the Root Partition

Follow the instructions in Loading Images with U-Boot to load the archived root partition back onto the NAND flash. You will not need to reload the kernel. Once this is complete, restart the board and verify that it boots properly.

Configure the System

The final step in the