Create a JFFS2 Target Image

From Texas Instruments Wiki
Jump to: navigation, search

Introduction to JFFS2

What is JFFS2?

JFFS2 (Journaled Flash File System v2) is a file system designed for use on Flash devices such as those commonly found in embedded systems. Unlike some other file systems which may be stored on the Flash device and then copied into RAM during boot (i.e. ramdisk) JFFS2 actually resides on the Flash device and allows the user to read/write data to Flash. This is particularly useful in embedded devices that wish to save some persistent data between reboots. For more information on JFFS2 please see the JFFS2 wikipedia page.

JFFS2 is mainly designed for use with NOR Flash. While JFFS2 can be used on NAND Flash devices it is not the best optimized file system for NAND. For NAND devices it is often best to use the YAFFS2 file system which was specifically designed for NAND devices. You can find more information on YAFFS2 at http://www.yaffs.net and in file systems in NOR or NAND article. See the http://www.yaffs.net/comparison-yaffs-vs-jffs for comparison information.

Benefits of JFFS2

There are several reasons why JFFS2 is useful in an embedded system. Some of the more common are

  1. JFFS2 allows data to be written back to Flash for persistent storage
  2. JFFS2 is optimized for Flash memory which must be erased in block size segments
  3. JFFS2 provides wear leveling for longer Flash memory life
  4. JFFS2 supports compression
  5. JFFS2 file systems do not need to be copied from Flash to RAM to be used

Limitations of JFFS2

Since JFFS2 file systems reside on Flash which has a limited number of write cycles per block care should be taken when using processes that cause frequent writes. For example when using syslogd it should be configured to write its data somewhere other than the Flash device since the frequent write operations can cause the Flash device to wear out prematurely.

Methods for Creating the File System Image

If you have the contents for your flash file system ready (e.g. from your ramdisk, the file system you mount via NFS or hard drive file system), there are two primary methods to create a file system image for use with JFFS2. You can either create a JFFS2 image

or

  • create a tarball archive

of the file system to be written to Flash. Both methods have some benefits and drawbacks:

Using MTD Utilities

Benefits
  • Using MTD Utilities on development host (PC), a JFFS2 image can be flashed directly from boot loader (e.g. U-Boot) without requiring the use of an intermediate file system (e.g. ram disk, NFS or hard drive file system) in order to boot Linux and write the image. All other ways (below) first boot a Linux with an intermediate file system (e.g. ram disk, NFS or hard drive file system) and use this to create and/or write the JFFS2 file system (image) to NOR (NAND) Flash.
  • NOTE: If you are creating the JFFFS2 image on the target board rather than the Linux development host you WILL require an intermediate file system with the MTD utilities installed. So above advantage is gone then. You should consider then the Using a Tarball Archive way below.
Drawbacks
  • Requires a version of the MTD utilities in order to create the JFFS2 file system image, either for the development host (PC) or for the target Board (DaVinci ARM). For more information on obtaining the MTD utilities for the Linux host please see the MTD Utilities page.
    • If the image is created on the Linux development host (PC) you will need a version of the MTD utilities for your Linux host.
    • If the image is created on the target board (DaVinci ARM) you will need a version of the MTD utilities for the ARM target. Using MontaVista toolchain, you can use the pre-compiled version of the MTD utilities included in the MontaVista sample target file system.
  • Using U-Boot to write the JFFS2 created with MTD utilities on the development host (PC) to Flash, some users reported that U-Boot isn't always able to cleanly write the JFFS2 image. Then, while booting, kernel sometimes complains with something like "Empty flash at 0xXXXXXXXX ends at 0xXXXXXXXX"

Using a Tarball Archive

Benefits
  • Avoids possible "Empty flash at 0xXXXXXXXX ends at 0xXXXXXXXX" warning messages that can result when using a JFFS2 image. Using a tarball archive, the kernel itself creates and writes the file system to flash, so hopefully it will be able to correctly read the stuff he wrote himself without any warnings.
Drawbacks
  • Requires the use of an intermediate file system (e.g. ram disk, NFS or hard drive file system) with the target version (ARM) of MTD utilities installed in order to use the MTD subsystem to write the contents of the tarball archive to the Flash device

Prerequisites

To create a JFFS2 image in this example you will need the following:

Required
  • MTD utilities for creating and writing JFFS2 images to Flash (either host (PC) and or target (ARM) version)
    • NOTE: If using MontaVista toolchain, there is a version of the MTD Utilities compiled for the ARM target provided in the MontaVista tool chain. Else you can easily compile them yourself.
  • A target (ARM ) root file system which you want to create a JFFS2 image of (e.g. the content of a ram disk or from NFS)
  • A terminal application for communicating with the DVEVM. i.e. minicom or hyperterminal
  • A copy of the u-boot bootloader running on the target.
    • If you do not have U-Boot running on the target please see the RBL, UBL and host program article for how to get the u-boot bootloader installed on your board first.
Optional (if the JFFS2 image will be created on the target)
  • An intermediate file system with the MTD utilities installed for booting Linux on the board (i.e. ram disk, NFS or hard drive file system)
  • A Linux kernel image for booting the board (i.e. a kernel stored in flash or downloaded via tftp)

Making the File System Image

As already mentioned, there are two ways in which the file system image can be created. The image can be created

  • as a JFFS2 image on your Linux host or target system

or

  • as a tarball archive

The sections below detail how to create the file system image for both cases. If you currently boot you kernel with and intermediate file system (i.e. ram disk, NFS or hard drive file system) and you have NFS running, then using the tarball image way should be the easiest way you can go. If you are unable or don't want to boot your Linux kernel with intermediate file system and have to write flash file system with boot loader (e.g. U-Boot) then you should use the JFFS2 image way.

Making a JFFS2 image

This section will detail how to create a JFFS2 image using the MTD utilities. For this example I will be using an existing ramdisk from the MontaVista target package located at /opt/mv_pro_4.0/montavista/pro/devkit/arm/v5t_le/images/

NOTE: In later releases of the DVSDK software package the sample ramdisk image is located at <dvsdk install dir>/<PSP dir>/bin

NOTE2: If you are creating the JFFS2 image on the target board you will need to have booted the board with a file system which has the MTD utilities installed. You can either use the MontaVista target file system, located at <MV install dir>/montavista/pro/devkit/arm/v5t_le/target, mounted via NFS or for boards with a hard drive you can use the file system which was loaded on the hard drive from the factory. For information on using NFS or the hard drive please refer to the DVEVM Getting Started Guide for your product.

  • Create a working directory
   host$ mkdir -p /home/user/workdir
  • Copy the example ramdisk.gz file to the working directory
   host$ cd /home/user/workdir
   host$ cp <ramdisk location>/ramdisk.gz .
  • Gunzip and mount the ramdisk image to a temporary directory
   host$ mkdir ram
   host$ gunzip ramdisk.gz
   host$ mount ramdisk ram -o loop

NOTE: At this point you can add any additional files or applications you wish to the file system image at /home/user/workdir/ram before creating the JFFS2 image.

  • Create the JFFS2 image of the file system mounted at /home/user/workdir/ram

The mkfs.jffs2 command uses following flags

   host$ mkfs.jffs2 -r <root file system> -e <erase_block_size>  -s <page_size>  [-p]  [-n]  -o <output file> 

In this command the various flags are:

  • -r : Build the file system from directory <root file system>
  • -e : Use <erase_block_size> as Erase-Block size parameter of underlying NAND device (specified in KiB)
  • -s : Use <page_size> as Page size parameter of the underlying NAND device (specified in KiB)
  • -n : Used to avoid adding a cleanmarker to every erase block. This is because on NAND this information is stored in the out of band area.
  • -p : Used to specify padding of 0xFF for data non-aligned to erase_block_size boundary.
  • -o : Output to <output file>

For more information on determining the erase block size for you Flash device see the Get the Flash Erase Block Size page.

For example to create a JFFS2 image for DaVinci AMD NOR Flash the command would be:

   host$ mkfs.jffs2 -r ram -e 64 -o rootfs.jffs2

To create a JFFS2 image for DaVinci NAND Flash the command would be:

   host$ mkfs.jffs2 -n -r ram -e 16 -o rootfs.jffs2

To create a JFFS2 image for DaVinci devices with big block NAND (i.e. DM355 or DM6467) the command would be:

   host$ mkfs.jffs2 -n -r ram -e 128 -o rootfs.jffs2
   Note: In the above examples for creating JFFS2 image for NAND '-n' option is used.

Making a Tarball Archive

This section will detail how to create a tarball file system image on the Linux development host. For this example I will be using an existing ramdisk from the MontaVista target package located at /opt/mv_pro_4.0/montavista/pro/devkit/arm/v5t_le/images NOTE: In later releases of the DVSDK (DVSDK 1.30 or later) this sample image has been moved to <DVSDK install dir>/<PSP directory>/bin

  • Create a working directory
   host$ mkdir -p /home/user/workdir
  • Copy the example ramdisk.gz file to the working directory
   host$ cd /home/user/workdir
   host$ cp /opt/mv_pro_4.0/montavista/pro/devkit/arm/v5t_le/images/ramdisk.gz .
  • Gunzip and mount the ramdisk image to a temporary directory
   host$ mkdir ram
   host$ gunzip ramdisk.gz
   host$ mount ramdisk ram -o loop

NOTE: At this point you can add any additional files or applications you wish to the file system image at /home/user/workdir/ram before creating the tarball file system image.

  • Create the tarball image of the file system mounted at /home/user/workdir/ram
   host$ cd ram
   host$ tar czf ../rootfs.tar.gz *

Conclusion

By following this example you should now be able to create a file system image as either a JFFS2 image or a tarball archive. For information on how to write this image to NAND and NOR Flash please see the Put JFFS2 Image to Flash Page

Further reading

See Filesystem_in_NOR_or_NAND article as well.