Chinaunix首页 | 论坛 | 博客
  • 博客访问: 179689
  • 博文数量: 31
  • 博客积分: 471
  • 博客等级: 下士
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-07 12:53
文章分类

全部博文(31)

文章存档

2015年(1)

2013年(1)

2012年(1)

2011年(28)

分类: LINUX

2011-08-17 17:42:41

上接:《4 添加DM9000支持

http://blog.chinaunix.net/space.php?uid=24473044&do=blog&id=2211587

5添加yaffs2文件系统(续)

参考说明:

1)本文参考了singleboy

2)参考了Tekkaman Ninjablog文章,http://blog.chinaunix.net/space.php?uid=20543672&do=blog&cuid=2085154

3)参考了黄刚的blog

http://blogold.chinaunix.net:80/u3/101649/showart.php?id=2276917

向上述作者表示感谢!

一、移植环境说明

1,主机环境:VMWareRHEL5

 

2,编译器:arm-linux-gcc v4.4.3

 

3,开发板:mini24402M nor flash256M nand flash(自改,原64M),NEC屏;

 

4u-boot版本:u-boot-2009.08

 

二、移植任务描述

为开发板添加Yaffs2文件系统烧写功能

 

三、移植步骤

首先,添加Yaffs2镜像烧写功能,由于现在很多使用Nand Flash 的系统,在Linux 下都用Yaffs2作为存储数据的文件系统,甚至是根文件系统。所以在BootLoader 下能够烧写Yaffs2 映像文件变得很必要。对于Yaffs2 映像烧写的支持其实就是在烧写时,写入数据的同时,将镜像文件中的oob数据也写入到Nand Flash Spare 区。这和Yaffs 文件系统原理以及Nand Flash 的结构有关,注意:这里对Nand的操作是基于MTD架构方式。

 

通常一个Nnad Flash存储设备由若干块组成,1个块由若干页组成。一般128MB以下容量的Nand Flash芯片,一页大小为528B,被依次分为2256B的主数据区和16B的额外空间;128MB以上容量的Nand Flash芯片,一页大小通常为2KB。由于Nand Flash出现位反转的概率较大,一般在读写时需要使用ECC进行错误检验和恢复。

 

Yaffs/yaffs2文件系统的设计充分考虑到Nand Flash以页为存取单位等的特点,将文件组织成固定大小的段(Chunk)。以528B的页为例,Yaffs/yaffs2文件系统使用前512B存储数据和16B的额外空间存放数据的ECC和文件系统的组织信息等(称为OOB数据)。通过OOB数据,不但能实现错误检测和坏块处理,同时还可以避免加载时对整个存储介质的扫描,加快了文件系统的加载速度。以下是Yaffs/yaffs2文件系统页的结构说明:

Yaffs页结构说明

==============================================

   字节                   用途

==============================================

 0 - 511                存储数据(分为两个半部)

512 - 515               系统信息

   516                  数据状态字

   517                  块状态字

518 - 519               系统信息

520 - 522               后半部256字节的ECC

523 - 524               系统信息

525 - 527               前半部256字节的ECC

==============================================

 

好了,在了解Nand Flash组成和Yaffs/yaffs2文件系统结构后,我们再回到u-boot中。目前,在u-boot中已经有对CramfsJffs2等文件系统的读写支持,但与带有数据校验等功能的OOB区的Yaffs/Yaffs2文件系统相比,他们是将所有文件数据简单的以线性表形式组织的。所以,我们只要在此基础上通过修改u-bootNand Flash读写命令,增加处理00B区域数据的功能,即可以实现对Yaffs/Yaffs2文件系统的读写支持。

 

实现u-bootYaffs或者Yaffs2文件系统的读写支持步骤如下:

1】下面是需要修改的 4 个文件的补丁:

(1)gedit common/cmd_nand.c,定位到392行,在nand操作的do_nand函数中,修改如下:

   if (read)

    ret = nand_read_skip_bad(nand, off, &size,

        (u_char *)addr);

   else

    ret = nand_write_skip_bad(nand, off, &size,

         (u_char *)addr);

 

}

#if defined(CONFIG_MTD_NAND_YAFFS2)

    else if (s != NULL && (!strcmp(s, ".yaffs2")))

    {

         nand->rw_oob = 1;

         nand->skipfirstblk = 1;

         ret = nand_write_skip_bad(nand,off,&size,(u_char *)addr);

         nand->skipfirstblk = 0;

         nand->rw_oob = 0;

    }

#endif

  else if (!strcmp(s, ".oob")) {

   /* out-of-band data */

   mtd_oob_ops_t ops = {

    .oobbuf = (u8 *)addr,

    .ooblen = size,

    .mode = MTD_OOB_RAW

   };

... ...

 

定位到512行附近Nand操作的命令集列表中添加Yaffs2Nand的读写命令修改如下

U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,

 "NAND sub-system",

 "info - show available NAND devices\n"

 "nand device [dev] - show or set current device\n"

 "nand read - addr off|partition size\n"

 "nand write - addr off|partition size\n"

 "    read/write 'size' bytes starting at offset 'off'\n"

 "    to/from memory address 'addr', skipping bad blocks.\n"

 "nand erase [clean] [off size] - erase 'size' bytes from\n"

 "    offset 'off' (entire device if not specified)\n"

#if defined(CONFIG_MTD_NAND_YAFFS2)

 "nand write[.yaffs2] - addr off|partition size - write `size' byte yaffs image\n"

 " starting at offset off' from memory address addr' (.yaffs2 for 512+16 NAND)\n"

#endif

 "nand bad - show bad blocks\n"

 "nand dump[.oob] off - dump page\n"

 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"

 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"

 "nand biterr off - make a bit error at offset (UNSAFE)"

#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK

 "\n"

 "nand lock [tight] [status]\n"

 "    bring nand to lock state or display locked pages\n"

 "nand unlock [offset] [size] - unlock section"

#endif

);

 

注意:这里只添加了yaffs2的写命令,u-boot下载用的是写功能

 

(2)gedit include/linux/mtd/mtd.h头文件,定位到131行,在mtd_info结构体中添加上面用到rw_oobskipfirstblk数据成员,修改如下:

 

u_int32_t writesize;

#if defined(CONFIG_MTD_NAND_YAFFS2)

  /*Thanks for hugerat's code*/

 u_char rw_oob;

 u_char skipfirstblk;

#endif

 u_int32_t oobsize;   /* Amount of OOB data per block (e.g. 16) */

 u_int32_t oobavail;  /* Available OOB bytes per block */

 

(3)在第一步关联的nand_write_skip_bad函数中添加对Nand OOB的相关操作,修改如下:

gedit drivers/mtd/nand/nand_util.c,定位到483行,在nand_write_skip_bad函数中添加代码,修改如下:

int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,

   u_char *buffer)

{

 int rval;

 size_t left_to_write = *length;

 size_t len_incl_bad;

 u_char *p_buffer = buffer;

#if defined(CONFIG_MTD_NAND_YAFFS2)

/*Thanks for hugerat's code*/

 if(nand->rw_oob==1)

 {

   size_t oobsize = nand->oobsize;

   size_t datasize = nand->writesize;

   int datapages = 0;

   if (((*length)%(nand->oobsize+nand->writesize)) != 0)

   {

     printf ("Attempt to write error length data!\n");

     return -EINVAL;

   }

   datapages = *length/(datasize+oobsize);

   *length = datapages*datasize;

   left_to_write = *length;

 }

#endif

/* Reject writes, which are not page aligned */

 if ((offset & (nand->writesize - 1)) != 0 ||

     (*length & (nand->writesize - 1)) != 0) {

  printf ("Attempt to write non page aligned data\n");

  return -EINVAL;

 }

 len_incl_bad = get_len_incl_bad (nand, offset, *length);

 if ((offset + len_incl_bad) >= nand->size) {

  printf ("Attempt to write outside the flash area\n");

  return -EINVAL;

 }

#if !defined(CONFIG_MTD_NAND_YAFFS2)//add yaffs2 file system support

 if (len_incl_bad == *length) {

  rval = nand_write (nand, offset, length, buffer);

  if (rval != 0)

   printf ("NAND write to offset %llx failed %d\n",

    offset, rval);

  return rval;

 }

#endif

 while (left_to_write > 0) {

  size_t block_offset = offset & (nand->erasesize - 1);

  size_t write_size;

  WATCHDOG_RESET ();

  if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {

   printf ("Skip bad block 0x%08llx\n",

    offset & ~(nand->erasesize - 1));

   offset += nand->erasesize - block_offset;

   continue;

  }

#if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support

  if(nand->skipfirstblk==1)

  {

   nand->skipfirstblk=0;

   printf ("Skip the first good block %llx\n", offset & ~(nand->erasesize - 1));

   offset += nand->erasesize - block_offset;

   continue;

  }

#endif

 

  if (left_to_write < (nand->erasesize - block_offset))

   write_size = left_to_write;

  else

   write_size = nand->erasesize - block_offset;

 

  printf("\rWriting at 0x%llx -- ",offset); //add yaffs2 file system support

  rval = nand_write (nand, offset, &write_size, p_buffer);

  if (rval != 0) {

   printf ("NAND write to offset %llx failed %d\n",

    offset, rval);

   *length -= left_to_write;

   return rval;

  }

  left_to_write -= write_size;

  printf("%d%% is complete.",100-(left_to_write/(*length/100)));/*Thanks for hugerat's code*/

  offset        += write_size;

#if defined(CONFIG_MTD_NAND_YAFFS2)

  /*Thanks for hugerat's code*/

  if(nand->rw_oob==1) {

   p_buffer += write_size+(write_size/nand->writesize*nand->oobsize);

  } else {

   p_buffer += write_size;

  }

#else

  p_buffer      += write_size;

#endif

 }

 return 0;

}

 

(4)在第三步的nand_write_skip_bad函数中我们看到又对nand_write函数进行了访问,所以这一步是到nand_write函数中添加对yaffs2的支持。

 

gedit drivers/mtd/nand/nand_base.c,定位到1961行,修改如下:

static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,

     size_t *retlen, const uint8_t *buf)

{

 struct nand_chip *chip = mtd->priv;

 int ret;

#if defined(CONFIG_MTD_NAND_YAFFS2)

 /*Thanks for hugerat's code!*/

   int oldopsmode = 0;

   if(mtd->rw_oob==1)   

     {

      int i = 0;

      int datapages = 0;

 

      size_t oobsize = mtd->oobsize;

      size_t datasize = mtd->writesize;

 

      uint8_t oobtemp[oobsize];

      datapages = len / (datasize);

 

      for(i = 0; i < (datapages); i++)   

          {

          memcpy((void *)oobtemp, (void *)(buf + datasize * (i + 1)), oobsize);

          memmove((void *)(buf + datasize * (i + 1)),

            (void *)(buf + datasize * (i + 1) + oobsize),

            (datapages - (i + 1)) * (datasize) + (datapages - 1) * oobsize);

          memcpy((void *)(buf+(datapages) * (datasize + oobsize) - oobsize),

            (void *)(oobtemp), oobsize);

          }

   }

#endif

 

/* Do not allow reads past end of device */

 if ((to + len) > mtd->size)

  return -EINVAL;

 if (!len)

  return 0;

 nand_get_device(chip, mtd, FL_WRITING);

 chip->ops.len = len;

 chip->ops.datbuf = (uint8_t *)buf;

 //chip->ops.oobbuf = NULL;

#if defined(CONFIG_MTD_NAND_YAFFS2)//add yaffs2 file system support

 /*Thanks for hugerat's code!*/

 if(mtd->rw_oob!=1) {

   chip->ops.oobbuf = NULL;

 } else {

   chip->ops.oobbuf = (uint8_t *)(buf+len);

   chip->ops.ooblen = mtd->oobsize;

   oldopsmode = chip->ops.mode;

   chip->ops.mode = MTD_OOB_RAW;

 }

#else

 chip->ops.oobbuf = NULL;

#endif

 ret = nand_do_write_ops(mtd, to, &chip->ops);

 *retlen = chip->ops.retlen;

  nand_release_device(mtd);

#if defined(CONFIG_MTD_NAND_YAFFS2)//add yaffs2 file system support

 /*Thanks for hugerat's code!*/

 chip->ops.mode = oldopsmode;

#endif

 return ret;

}

 

2】在mini2440.h里添加yaffs2相关宏定义

gedit include/configs/mini2440.h

在文件末尾处加入下列代码:

/*-----------------------------------------------------------------------

 * NAND flash settings

 */

#if defined(CONFIG_CMD_NAND)

#define CONFIG_NAND_S3C2410

#define CONFIG_SYS_NAND_BASE 0x4E000000

#define CONFIG_SYS_MAX_NAND_DEVICE 1 /* Max number of NAND devices  */

#define SECTORSIZE 512

#define SECTORSIZE_2K 2048

#define NAND_SECTOR_SIZE SECTORSIZE

#define NAND_SECTOR_SIZE_2K SECTORSIZE_2K

#define NAND_BLOCK_MASK 511

#define NAND_BLOCK_MASK_2K 2047

#define NAND_MAX_CHIPS 1

#define CONFIG_MTD_NAND_VERIFY_WRITE

#define CONFIG_SYS_64BIT_VSPRINTF  /* needed for nand_util.c */

#endif /* CONFIG_CMD_NAND */

#define CONFIG_MTD_NAND_YAFFS2   1

 

其次, 烧录yaffs2文件系统测试

重新编译后下载,nand方式启动,在u-boot的命令行输入nand help查看nand的命令,可以看到多了一个nand write[.yaffs2]的命令

 

[mini2440] #nand help

nand - NAND sub-system

 

Usage:

nand info - show available NAND devices

nand device [dev] - show or set current device

nand read - addr off|partition size

nand write - addr off|partition size

    read/write 'size' bytes starting at offset 'off'

    to/from memory address 'addr', skipping bad blocks.

nand erase [clean] [off size] - erase 'size' bytes from

    offset 'off' (entire device if not specified)

nand write[.yaffs2] - addr off|partition size - write `size' byte yaffs image

 starting at offset off' from memory address addr' (.yaffs2 for 512+16 NAND)

nand bad - show bad blocks

nand dump[.oob] off - dump page

nand scrub - really clean NAND erasing bad blocks (UNSAFE)

nand markbad off [...] - mark bad block(s) at offset (UNSAFE)

nand biterr off - make a bit error at offset (UNSAFE)

[mini2440] #

 

这个就是用来下载yaffs2文件系统到nand中的命令了。首先需要将需要下载的yaffs2文件系统文件rootfs.img复制到linux宿主机的/tftpboot目录下,然后执行:

[mini2440] # tftp 30000000 rootfs.img

dm9000 i/o: 0x20000300, id: 0x90000a46

DM9000: running in 16 bit mode

MAC: 01:02:03:04:05:06

operating at 100M full duplex mode

Using dm9000 device

TFTP from server 222.197.173.99; our IP address is 222.197.173.10

Filename 'rootfs.img'.

Load address: 0x30000000

Loading: T T T T T T T T T T

Retry count exceeded; starting again

dm9000 i/o: 0x20000300, id: 0x90000a46

DM9000: running in 16 bit mode

MAC: 01:02:03:04:05:06

operating at 100M full duplex mode

Using dm9000 device

TFTP from server 222.197.173.99; our IP address is 222.197.173.10

Filename 'rootfs.img'.

Load address: 0x30000000

Loading: ######################################T ###########################

         ######T ############T T T T T T T T

Retry count exceeded; starting again

dm9000 i/o: 0x20000300, id: 0x90000a46

DM9000: running in 16 bit mode

MAC: 01:02:03:04:05:06

operating at 100M full duplex mode

Using dm9000 device

TFTP from server 222.197.173.99; our IP address is 222.197.173.10

Filename 'rootfs.img'.

Load address: 0x30000000

Loading: T T T T T T T T T T

Retry count exceeded; starting again

dm9000 i/o: 0x20000300, id: 0x90000a46

DM9000: running in 16 bit mode

MAC: 01:02:03:04:05:06

operating at 100M full duplex mode

Using dm9000 device

TFTP from server 222.197.173.99; our IP address is 222.197.173.10

Filename 'rootfs.img'.

Load address: 0x30000000

Loading: #########T #T #######################################################

         ######T #####################################T ######################

         #################################################################

         #########T ############################T T T T T

Retry count exceeded; starting again

dm9000 i/o: 0x20000300, id: 0x90000a46

DM9000: running in 16 bit mode

MAC: 01:02:03:04:05:06

……………………………………………………不断循环!……………………………….

 

Retry count exceeded; starting again 解决方法:tftp客户端传过来的timeout7810,而服务器端定义的范围在1-255秒之间,不是服务器的问题,而是uboottftp参数设置的问题,参见《TFTP Unsupported option(s) requested 问题详细分析及解决原文链接地址:

一、问题原因分析:
    1
、下载与我操作系统版本(fedora 10)一致的tftp server源代码tftp-hpa-0.48.tar.bz2,编译通过后,替换系统tftpd程序,通过在源代码中添加调试信息,发现是由于 Uboot tftp 程序传过来的Timeout参数不符合服务器端定义引起的:

Nov 11 10:46:12 HardWare in.tftpd[18275]: client timeout = 7810 , server timeout = 1-255
    tftp
客户端传过来的timeout7810,而服务器端定义的范围在1-255秒之间,不是服务器的问题,而是uboottftp参数设置的问题。
二、解决方案
   
可以有两种方式解决:
    1
、修改服务器端源代码(此种方案修改较为繁琐,因为fedora 发行版中不包含tftp源代码,需要自己下载、编译、替换,所以不建议采用此方案)
    2
、修改Uboottftp代码
   
U-boot 1.3.4源代码中定义的是5
#define TIMEOUT        5UL        /* Seconds to timeout for a lost pkt    */
    u-boot-2009.08
中定义如下
#define TIMEOUT        (CONFIG_SYS_HZ/1000*5000UL)
    TIMEOUT
的值是跟据板子配置文件中的 CONFIG_SYS_HZ 值计算出来的,我的配置文件时从 Uboot目录 include/configs/smdk2410.h 复制过来的,值为 1562500,算出来后就等于7810
   
可以直接修改 uboot 目录 net/tftp.c TIMEOUT 定义为5000注意是5000而不是5,因为u-boot.2009.08版本该宏定义改为以毫秒为单位了)

#define TIMEOUT 5000UL  /*UBOOT 2009.08
定义为毫秒值*/

 

gedit net/net.c,定位到104行附近,修改如下:

 

#if defined(CONFIG_CMD_NET)

 

DECLARE_GLOBAL_DATA_PTR;

 

#ifndef CONFIG_ARP_TIMEOUT

# define ARP_TIMEOUT  10000UL*CONFIG_SYS_HZ/1000 //5000UL /* Milliseconds before trying ARP again */

#else

# define ARP_TIMEOUT  CONFIG_ARP_TIMEOUT

#endif

 

定位到575行附近,修改如下:

 

#ifndef CONFIG_NET_MULTI

 //NetSetTimeout (10000UL, startAgainTimeout);

 NetSetTimeout (10000UL*CONFIG_SYS_HZ/1000, startAgainTimeout);

 NetSetHandler (startAgainHandler);

#else /* !CONFIG_NET_MULTI*/

 

定位到585行附近,修改如下:

 

eth_init (gd->bd);

 if (NetRestartWrap) {

  NetRestartWrap = 0;

  if (NetDevExists && !once) {

   //NetSetTimeout (10000UL, startAgainTimeout);

   NetSetTimeout (10000UL*CONFIG_SYS_HZ/1000, startAgainTimeout);

   NetSetHandler (startAgainHandler);

  } else {

   NetState = NETLOOP_FAIL;

  }

 

定位到783行附近,修改如下:

 

#define CDP_SYSOBJECT_TLV  0x0015

#define CDP_MANAGEMENT_ADDRESS_TLV 0x0016

 

#define CDP_TIMEOUT   (250UL*CONFIG_SYS_HZ/1000) //250UL /* one packet every 250ms */

 

static int CDPSeq;

static int CDPOK;

 

gedit net/tftp.c,定位到16行,修改如下:

 

#define TIMEOUT  60000UL //5000UL /* Millisecs to timeout for lost pkt */

 

另外还一种方法是修改开发板对应的配置文件 include/configs/utu2440.h(未详细测试此修改是否会影响Uboot其他功能,所以不建议采用此方案,有兴趣的兄弟姐妹可以详细测试一下,^_^

#define    CONFIG_SYS_HZ            1000

Uboot2009.08不行,试了。

 

修改延时后:

U-Boot 2009.08 (Aug 05 2011 - 02:42:06)

 

 modified by gy (robinfit01@163.com)

 Love pore and Linux forever!!!

 

DRAM:  64 MB

Flash:  2 MB

NAND:  256 MiB

In:    serial

Out:   serial

Err:   serial

Net:   dm9000

[mini2440] # tftp 31000000 rootfs.img

dm9000 i/o: 0x20000300, id: 0x90000a46

DM9000: running in 16 bit mode

MAC: 01:02:03:04:05:06

operating at 100M full duplex mode

Using dm9000 device

TFTP from server 222.197.173.99; our IP address is 222.197.173.10

Filename 'rootfs.img'.

Load address: 0x31000000

Loading: #################################################################

        。。。。。。。

         ############################################################undefined instruction

pc : [<0d100004>]          lr : [<33f9d364>]

sp : 33f3fb04  ip : 054b76c5     fp : 33f3fb28

r10: 0e0f0200  r9 : 33fa6d7e     r8 : 33f3ffdc

r7 : 0d100000  r6 : 0e0f0200     r5 : 0e100000  r4 : 0f0f0200

r3 : 000005bc  r2 : 00000004     r1 : 054b76c8  r0 : 00000000

Flags: nZCv  IRQs off  FIQs off  Mode SVC_32

Resetting CPU ...

 

resetting ...

 

接下来,实现增加引导内核功能与挂载yaffs2文件系统,大家可以看到yaffs2的烧写命令可用(未完待续)。

阅读(3272) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~