Modifying the root image

(Kernel memory emulating an MTD device: Fixed link)
(kernel config notes)
Line 7: Line 7:
#Have a block device emulate a Memory Technology Device (MTD) via block2mtd.
#Have a block device emulate a Memory Technology Device (MTD) via block2mtd.
#Have kernel memory emulate a MTD via mtdram.
#Have kernel memory emulate a MTD via mtdram.
 +
 +
Note that for a host kernel config you need to enable mtd device support to access/enable jffs2 filesystem support.
== Getting the JFFS2 image ==
== Getting the JFFS2 image ==

Revision as of 16:20, 3 August 2008

This article describes two methods for extending and modifying an existing tablet JFFS2 root image.

Contents

Background

The root filesystem (rootfs) of the tablets is stored in a Journal Flash File System version 2 (JFFS2) format. It resides on one of the partitions on the flash chip in the device. Additionally, a pristine version of the rootfs can be obtained from the Nokia official firmware site.

There are two ways to mount the JFFS2 image:

  1. Have a block device emulate a Memory Technology Device (MTD) via block2mtd.
  2. Have kernel memory emulate a MTD via mtdram.

Note that for a host kernel config you need to enable mtd device support to access/enable jffs2 filesystem support.

Getting the JFFS2 image

First of all, we need to get the rootfs.jffs2 from the official Nokia FIASCO image. After downloading the FIASCO image, run this on it to unpack it (this will give you all of the individual parts of the image):

sudo ./flasher-3.0 --unpack  -F <FIASCO image>

Mounting the JFFS2 image

These commands will be executed with root privileges on the host machine and not on the device itself.

Block device emulating an MTD device

You must have the following kernel modules compiled and installed:

  • CONFIG_MTD (mtdcore)
  • CONFIG_MTD_PARITIONS (mtdpart)
  • CONFIG_MTD_MTDRAM (mtdram)
  • CONFIG_MTD_BLOCK2MTD (block2mtd)
  • CONFIG_BLK_DEV_LOOP (loop)

I suggest you do not make these modules statically linked into the kernel. It is more practical to be able to unload these modules when not needed or when you want to start over from scratch.

You will use the loopback device (/dev/loop[0-15]) to simulate a block device whose contents are from the JFFS2 image. To mount the JFFS2 image, you perform the following steps:

mknod /tmp/mtdblock0 b 31 0
modprobe loop
losetup /dev/loop0 rootfs.jffs2
modprobe mtdblock
modprobe block2mtd
echo "/dev/loop0" > /sys/module/block2mtd/parameters/block2mtd
modprobe jffs2
mount -t jffs2 /tmp/mtdblock0 /media/jffs2

To unmount and cleanup:

umount /media/jffs2
modprobe -r block2mtd
modprobe -r mtdblock
losetup -d /dev/loop0

Or use the automated shell script.

Kernel memory emulating an MTD device

To mount the JFFS2 image, you perform the following steps:

mknod /tmp/mtdblock0 b 31 0
modprobe mtdblock
modprobe mtdram total_size=65536 erase_size=256
modprobe jffs2
dd if=/pathtoimage/rootfs.jffs2 of=/tmp/mtdblock0
mkdir /media/jffs2
mount -t jffs2 /tmp/mtdblock0 /media/jffs2

To unmount and cleanup:

umount /media/jffs2
modprobe -r jffs2
modprobe -r mtdram
modprobe -r mtdblock

Or use the automated shell script.

Archiving and extracting the rootfs image

The image is now accessible under /media/jffs2. Copy the whole image to another directory. This new directory will be used for modifying the image. Extending the currently mounted JFFS2 image is not suggested. Using cp for copying the image to a working directory won't work due to special files in /media/jffs2/dev, which is why we use tar.

cd /media/jffs2
tar cvzf /my_path/myRootImage.tar.gz .
cd $HOME
mkdir myRootImage
cd myRootImage
tar xvpzf /my_path/myRootImage.tar.gz

Repacking the image

Using the flasher utility and creating the reference root file system

Modifying the copy of the image

Now the image is successfully archived and copied. The working directory $HOME/myRootImage can now be used for adding, changing or modifying packages.

cd $HOME/myRootImage
dpkg -x $HOME/arm_debs/mypackage_arm.deb .

Now we create a new tarball from the working directory of the image.

cd $HOME/myRootImage
tar cvzf $HOME/myNewRootImage.tar.gz .

Installing on the tablet

When the new tarball is created, Using flasher and the reference root filesystem for creating JFFS2 image from the myNewRootImage.tar.gz.

Use flasher to install the image on the tablet.

./flasher-3.0 -r rootfs.jffs2 --f -R

Shell script to mount/unmount JFFS2 using block device emulating MTD

Create a shell script (mount_jffs2.sh) from the following:

 #!/bin/sh
 JFFSIMG=$1 # jffs image
 LOOP="/dev/loop1" # loop device
 MP="/media/jffs2" # mount point
 MTDBLOCK="/tmp/mtdblock0" # MTD device file
 KVER="2.6"
 BLKMTD="block2mtd"
 UMNT=""

 echo "$0" | grep unmount_ >/dev/null 2>&1
 [ $? -eq 0 ] && UMNT=1
 if [ $# -gt 1 -a x"$2"x = x"unmount"x ]; then
   UMNT=1
 fi

 uname -r | egrep '^2\.6' >/dev/null 2>&1
 if [ $? -ne 0 ]; then
   KVER="2.4"
   BLKMTD=blkmtd
 fi 

 if [ x"${UMNT}"x = x""x ]; then
   if [ ! -b ${MTDBLOCK} ] ; then
     mknod ${MTDBLOCK} b 31 0 || exit 1
   fi
   lsmod | grep loop >/dev/null 2>&1
   if [ $? -ne 0 [; then
     modprobe loop
     [ $? -ne 0 ] && echo "loopback loading failed" && exit 1
     sleep 1
   fi
   losetup ${LOOP} ${JFFSIMG} || exit 1
   sleep 1
   modprobe mtdblock
   if [ x"${KVER}"x = x"2.4"x [; then
     modprobe ${BLKMTD} device=${LOOP} || exit 1
   else
     modprobe ${BLKMTD} || exit 1
     echo "${LOOP}" > /sys/module/block2mtd/parameters/block2mtd
   fi
   sleep 1
   modprobe jffs2
   [ ! -d ${MP} ] && mkdir -p ${MP}
   mount -t jffs2 ${MTDBLOCK} ${MP} || exit 1
 else
   umount ${MP}
   if [ $? -ne 0 ]; then
     echo "Cannot unmount JFFS2 at $MP" && exit 1
   fi
   modprobe -r jffs2
   modprobe -r ${BLKMTD}
   modprobe -r mtdblock
   sleep 1
   losetup -d ${LOOP}
 fi 

Make sure you chmod a+x mount_jffs2.sh to make the shell script executable.

Usage:

./mount_jffs2.sh rootfs.jffs2

You can also use this script to unmount and unload the non-utilized kernel modules and loopback reference:

./mount_jffs2.sh rootfs.jffs2 unmount

Shell script to mount/unmount JFFS2 using kernel memory emulating MTD

Create a shell script (mount_jffs2.sh) from the following:

 #!/bin/sh
 JFFSIMG=$1 # jffs image
 MP="/media/jffs2" # mount point
 MTDBLOCK="/tmp/mtdblock0" # MTD device file
 UMNT=""

 echo "$0" | grep unmount_ >/dev/null 2>&1
 [ $? -eq 0 ] && UMNT=1
 if [ $# -gt 1 -a x"$2"x = x"unmount"x ]; then
   UMNT=1
 fi

 if [ x"${UMNT}"x = x""x ]; then
   if [ ! -b ${MTDBLOCK} ] ; then
     mknod ${MTDBLOCK} b 31 0 || exit 1
   fi
   modprobe mtdblock
   modprobe mtdram total_size=65536 erase_size=256
   modprobe jffs2
   dd if=${JFFSIMG} of=${MTDBLOCK}
   [ ! -d ${MP} ] && mkdir -p ${MP}
   mount -t jffs2 ${MTDBLOCK} ${MP}
 else
   umount ${MP}
   if [ $? -ne 0 ]; then
     echo "Cannot unmount JFFS2 at $MP" && exit 1
   fi
   modprobe -r jffs2
   modprobe -r mtdram
   modprobe -r mtdblock
 fi 

Make sure you chmod a+x mount_jffs2.sh to make the shell script executable.

Usage:

./mount_jffs2.sh rootfs.jffs2

You can also use this script to unmount and unload the non-utilized kernel modules:

./mount_jffs2.sh rootfs.jffs2 unmount