Monday, October 17, 2011

【转】 制作JFFS2文件系统 (including u-boot)


Ref: http://hi.baidu.com/freshwater2009/blog/item/98515ead5651a0ecfbed50af.html

  JFFS2是Flash上应用最广的一个日志结构文件系统。它提供的垃圾回收机 制,不需要马上对擦写越界的块进行擦写,而只需要将其设置一个标志,标明为脏块,当可用的块数不足时,垃圾回收机制才开始回收这些节点。同时,由于 JFFS2基于日志结构,在意外掉电后仍然可以保持数据的完整性,而不会丢失数据。
  本文阐述如何在nand flash上实现JFFS2根文件文件系统。实验环境是:FS2410开发平台及ubantu7.04主机环境。
  由于使用的ubantu7.04的环境没有安装制作JFFS2文件系统镜像的工具mkfs.jffs2,所以首先在Linux系统中安装mkfs.jffs2工具,安装的过程如下:
  1.下载MTD工具包
  本处使用的是mtd-snapshot-20050519.tar.bz2
  farsight#tar –jxvf mtd-snapshot-20050519.tar.bz2
  farsight#cd mtd
  farsight#./configure
  farsight#make
  farsight#make install
  如果系统中没有安装ZLIB库,那么首先安装ZLIB库。具体的安装过程如下:
  farsight#tar –zxvf zlib-1.2.3.tar.gz
  farsight#cd zlib-1.2.3
  farsight#./configure
  farsight#make
  farsight#make install
  完成此步骤后,系统中就有了mkfs.jffs2的工具。注意:这个工具不同于 mkfs.ext2工具,它只能制作相应的JFFS2文件系统的镜像,而不具有进行格式化的功能,而mkfs.ext2具备这以上两种功能。然后用这个工 具就可以制作JFFS2文件系统的镜像了。

  2、制作JFFS2文件镜像
  实验平台用到的nand flash 是K9F1208,在制作镜像过程添加的参数需要和它对应。
  farsight#mkfs.jffs2 -r /source/rootfs  -o rootfs.jffs2   -e  0x4000 --pad=0x800000  -n
 ==============================================================================================
实例:
mkfs.jffs2 -r rootfs -o rootfs.jffs2 -e 0x4000 --pad=0x1000000 -s 0x200 -n

mkfs.jffs2: Usage: mkfs.jffs2 [OPTIONS]
Make a JFFS2 file system image from an existing directory tree
Options:
-p, --pad[=SIZE]       
用16進制表示所要輸出檔案的大小,也就是root.jffs2的size。很重要的是,為了不浪費flash空間,這個值最好符合flash driver的區塊大小。如果不足则使用0xff来填充补满。
-r, -d, --root=DIR      指定要做成image的源資料夾.(默认:当前文件夹)
-s, --pagesize=SIZE     节点页大小(默认: 4KiB)
-e, --eraseblock=SIZE   设定擦除块的大小为(默认: 64KiB),一般为(16K + 512)
-c, --cleanmarker=SIZE  Size of cleanmarker (default 12)
-m, --compr-mode=MODE   Select compression mode (default: priortiry)
-x, --disable-compressor=COMPRESSOR_NAME Disable a compressor
-X, --enable-compressor=COMPRESSOR_NAME Enable a compressor
-y, --compressor-priority=PRIORITY:COMPRESSOR_NAME Set the priority of a compressor
-L, --list-compressors Show the list of the avaiable compressors
-t, --test-compression Call decompress and compare with the original (for test)
-n, --no-cleanmarkers   指明不添加清楚标记(nand flash 有自己的校检块,存放相关的信息。)如果挂载后会出现类似:CLEANMARKER node found at 0x0042c000
has totlen 0xc != normal 0x0 的警告,则加上-n 就会消失。
-o, --output=FILE       指定輸出image檔案的文件名.(default: stdout)
-l, --little-endian     指定使用小端格式
-b, --big-endian        指定使用大端格式
-D, --devtable=FILE     Use the named FILE as a device table file
-f, --faketime          Change all file times to '0' for regression testing
-q, --squash            Squash permissions and owners making all files be owned byroot
-U, --squash-uids       将所有文件的拥有者设为root用户
-P, --squash-perms      Squash permissions on all files
      --with-xattr      stuff all xattr entries into image
      --with-selinux    stuff only SELinux Labels into jffs2 image
      --with-posix-acl  stuff only POSIX ACL entries into jffs2 image
-h, --help              显示这些文字
-v, --verbose           Verbose operation
-V, --version           显示版本
-i, --incremental=FILE  Parse FILE and generate appendage output for it
 
 
生成的rootfs.img可通过u-boot直接烧写至nand flash:
              nand erase 0x200000 0x3e00000//擦写
              tftp 0x30000000 rootfs.img//下载
              nand write.jffs2 0x200000 $(filesize)//烧入
==================================================================================================================
  这样就会生成一个8M大小的rootfs.jffs2的镜像,它也正是JFFS2文件系统的镜像,关于这个命令行里的选项的内容,可以用man a mkfs.jffs2命令来查看内容。

  JFFS2 维护了几个链表来管理擦写块,根据擦写块上的内容,一个擦写块会在不同的链表上。具体来说,
当一个擦写块上都是合法(valid)的节点时,它会在 clean_list 上;
当一个擦写块包含至少一个过时(obsolete)的节点时,它会在 dirty_list 上;
当一个擦写块被擦写完毕,并被写入 CLEANMARKER 节点后,它会在 free_list 上。而当你在挂载这个文件系统的时候,如果出现CLEANMARKER node found at 0x0042c000 has totlen 0xc != normal 0x0的警告的时候,可以加一个“-n”的选项,这个主要是由于针对Nand Flash不需要在每个擦除块的开始写入CLEANMARKER 节点。

  3、设置内核启动参数
  本处用的bootloader是U-BOOT.所以在U-BOOT的命令终端设置如下:
FS2410#setenv bootargs root=/dev/mtdblock/2 rootfstype=jffs2rw console=ttySAC0, 115200  init=/linuxrc mem=64M

  4、配置内核支持JFFS2文件系统
  File systems ---> Miscellaneous filesystems --->
  <*>JournallingFlash File System v2 (JFFS2) support
  [*]JFFS2write-bufferingsupport
  [*]AdvancedcompressionoptionsforJFFS2
  [*]JFFS2ZLIBcompressionsupport
  [*]JFFS2RTIMEcompressionsupport
  [*] JFFS2 RUBIN compression support

  5、下载rootfs.jffs2镜像
  下载到Nand Flash第二个分区。
  FS2410#nand erase 200000 800000
  FS2410#nand write.jffs2 300008000 200000 800000

  这里说明下关于nand flash操作的几个常用命令的含义
  nand write:向Nand Flash写入数据,如果NandFlash相应的区域有坏块,则直接报错。
  nand write.jffs2:向Nand Flash写入数据,如果NandFlash相应的区域有坏块,可以跳过坏块。
  nand read:读取Nand Flash相应区域的数据,如果NandFlash相应的区域有坏块,则直接报错。
  nand read.jffs2s:读取Nand Flash相应区域的数据,如果NandFlash相应的区域有坏块,将对应坏块区域的缓冲填充0xff,然后跳过此坏块继续读取。
  nand read.jffs2:读取Nand Flash相应区域的数据,如果NandFlash相应的区域有坏块,直接跳过坏块。

  具体的参考代码参看U-BOOT源码:common/cmd_nand.c文件。
  下载完JFFS2文件系统镜像后,需要把Linux内核NandFlash的驱动关于第二个分区的大小改为8M(和镜像一样大),否则会出现类似如下错误:
  Freeing init memory: 124K
  Warning: unable to open an initial console.
  Argh. Special inode #171 with mode 0xa1ff had more than one node
  Kernel panic: No init found. Try passing init= option to kernel.
  Argh. Special inode #63 with mode 0xa1ff had more than one node
  Returned error for crccheck of ino #63. Expect badness...
  Argh. Special inode #67 with mode 0xa1ff had more than one node
  Returned error for crccheck of ino #67. Expect badness...
  Argh. Special inode #68 with mode 0xa1ff had more than one node
  到此,一个JFFS2文件系统的镜像制作成功。可以启动系统并测试JFFS2的性能了

  还有一种制作JFFS2文件系统镜像的方法,在制作镜像的参数中可以不加—pad选项,过程如下:
  farsight#mkfs.jffs2-r /source/rootfs -o rootfs.jffs2 -e 0x4000 -n
  // 之前对比:farsight#mkfs.jffs2 -r /source/rootfs  -o rootfs.jffs2   -e  0x4000--pad=0x800000  -n

  启动开发板烧写rootfs.jffs2镜像
  FS2410#nand erase 200000 800000    //(注意把整个存放文件系统的分区全部给擦除)。
  FS2410#nand write.jffs2 30008000 200000 31a28c    //(必须是rootfs.jffs2的实际大小。如果是你写成了4M,那么分区的其余部分JFFS2文件系统将无法识别)。




基于2440的一个实例:
=====================================================
移植文件系统写到nand flash
=====================================================
( 1 ) 通过网络文件系统加载开发板上根文件系统
        把主机上把文件系统打包:
[root@orat3000 nfs]# tar -cf ../nfsroot.tar *
( 2 ) 擦除:nand erase 400000 fc00000
( 3 ) 把/dev/mtdblock2 挂载到 /mnt下(会自动格式化)
[root: /]# mount -t yaffs /dev/mtdblock2 /mnt
( 4 ) 在开发板上进入/mnt解包:
[root: /mnt]# tar -xvf /nfsroot.tar
( 5 ) 备份原内核参数环境变量:
set nfs_boot $bootargs
( 6 ) 设置新的内核参数环境变量:
set nand_boot noinitrd root=/dev/mtdblock2 rw console=ttySAC0,115200 init=/linuxrc mem=64M
( 7 ) 把默认内核参数改为新的环境变量:
set bootargs $nand_boot
( 8 ) 保存设置:
save
(9)reset
修改完应该有三个环境变量: 
nfs_boot=noinitrd root=/dev/nfs rw nfsroot=192.168.1.3:/nfsroot ip=192.168.1.88 console=ttySAC0,115200 mem=64M    //这个只是保存修改前的变量,只是个副本,没有意义
nand_boot=noinitrd root=/dev/mtdblock2 rw console=ttySAC0,115200 init=/linuxrc mem=64M
bootargs=noinitrd root=/dev/mtdblock2 rw console=ttySAC0,115200 init=/linuxrc mem=64M

==============================改变之前的实例==================================
bootcmd=nand read 0x30008000 0x100000 0x220000;bootm 30008000
bootdelay=3
baudrate=115200
ethaddr=11:22:33:44:55:66
ipaddr=10.1.0.222
serverip=10.1.0.177
netmask=255.255.255.0
stdin=serial
stdout=serial
stderr=serial
ethact=dm9000
bootargs=noinitrd root=/dev/nfs rw nfsroot=10.1.0.177:/nfsroot ip=10.1.0.222 console=ttySAC0,115200 mem=64M
Environment size: 339/131068 bytes

1 comment: