2009.08在mini2440上的移植(5)

1,主机环境:VMare下CentOS 5.5 ,1G内存。

2,集成开发环境:Elipse IDE

3,编译编译环境:arm-linux-gcc v4.4.3,arm-none-eabi-gcc v4.5.1。

4,开发板:mini2440,2M nor flash,128M nand flash。

5,u-boot版本:u-boot-2009.08

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

通常一个Nnad Flash存储设备由若干块组成,1个块由若干页组成。一般128MB以下容量的Nand Flash芯片,一页大小为528B,被依次分为2个256B的主数据区和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中已经有对Cramfs、Jffs2等文件系统的读写支持,但与带有数据校验等功能的OOB区的Yaffs/Yaffs2文件系统相比,他们是将所有文件数据简单的以线性表形式组织的。所以,我们只要在此基础上通过修改u-boot的Nand Flash读写命令,增加处理00B区域数据的功能,即可以实现对Yaffs/Yaffs2文件系统的读写支持。

实现u-boot对Yaffs或者Yaffs2文件系统的读写支持步骤如下:
【1】下面是需要修改的 4 个文件的补丁:
(1)打开/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
   };
... ...

定位到509行附近,在Nand操作的命令集列表中添加Yaffs2对Nand的读写命令,修改如下
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)打开include/linux/mtd/mtd.h头文件,定位到131行,在mtd_info结构体中添加上面用到rw_oob和skipfirstblk数据成员,修改如下:

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的相关操作,修改如下:
打开/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)
 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的支持。

打开/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相关宏定义
打开include/configs/mini2440.h,定位到98行附近,加入下列代码:
/*
 * Command line configuration.
 */
#include <config_cmd_default.h>
#define CONFIG_CMD_CACHE
#define CONFIG_CMD_DATE
#define CONFIG_CMD_ELF
#define CONFIG_CMD_NAND
#define CONFIG_CMD_JFFS2  /* JFFS2 Support*/
... ...
在文件末尾处加入下列代码:
/*-----------------------------------------------------------------------
 * 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_JFFS2_NAND 1
//#undef CONFIG_JFFS2_CMDLINE
#define CONFIG_JFFS2_DEV "nand0"
#define CONFIG_JFFS2_PART_SIZE 0x480000
#define CONFIG_JFFS2_PART_OFFSET 0x80000
#define CONFIG_JFFS2_CMDLINE 1
#define MTDIDS_DEFAULT "nand0=nandflash0"
#define MTDPARTS_DEFAULT "mtdparts=nandflash0:384k(bootloader)," \
           "128k(params)," \
           "5m(kernel)," \
           "-(root)"
#define CONFIG_MTD_NAND_YAFFS2   1
#define ENABLE_CMD_LOADB_X 1
 

5.2, 烧录yaffs2文件系统测试

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

[u-boot@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)
[u-boot@MINI2440]#

这个就是用来下载yaffs2文件系统到nand中的命令了。首先需要将友善官方提供的有关mini2440的yaffs2文件系统文件root_qtopia-128M.img复制到linux宿主机的/tftpboot目录下,然后执行:

tf[u-boot@MINI2440]# tp 0x30000000 root_qtopia-128M.img
Filename 'root_qtopia-128M.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: 12:34:56:78:9a:bc
operating at 100M full duplex mode
Using dm9000 device
TFTP from server 10.1.0.128; our IP address is 10.1.0.129
Filename 'root_qtopia-128M.img'.
Load address: 0x30000000
Loading: T ###T ###T T ########

一直无休止循环,经网上搜索得知Uboot 端 tftp 程序传过来的Timeout参数不符合服务器端定义引起的,原文链接地址

Retry count exceeded; starting again 解决方法:tftp客户端传过来的timeout是7810,而服务器端定义的范围在1-255秒之间,不是服务器的问题,而是uboot中tftp参数设置的问题,参见TFTP Unsupported option(s) requested 问题详细分析及解决。
打开/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

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

#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;
  }

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

#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;

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

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

然后重新编译后下载测试:

[u-boot@MINI2440]# tftp 0x30000000 root_qtopia-128M.img
dm9000 i/o: 0x20000300, id: 0x90000a46
DM9000: running in 16 bit mode
MAC: 12:34:56:78:9a:bc
operating at 100M full duplex mode
Using dm9000 device
TFTP from server 10.1.0.128; our IP address is 10.1.0.129
Filename 'root_qtopia-128M.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: 12:34:56:78:9a:bc
operating at 100M full duplex mode
Using dm9000 device
TFTP from server 10.1.0.128; our IP address is 10.1.0.129
Filename 'root_qtopia-128M.img'.
Load address: 0x30000000
Loading: #################################################################
         ################################################################# 

... ...
         #################################################################
         ####################
done
Bytes transferred = 58487616 (37c7340 hex)
[u-boot@MINI2440]#

在yaffs2文件系统镜像下载到内存后:

[u-boot@MINI2440]# nand erase 0 0x40000

NAND erase: device 0 offset 0x0, size 0x40000
Erasing at 0x2000000000004 --   0% complete.
OK
[u-boot@MINI2440]# nand write 0x30000000 0 0x40000

NAND write: device 0 offset 0x0, size 0x40000
Writing at 0x2000000020000 -- 100% is complete. 262144 bytes written: OK
[u-boot@MINI2440]#

可以看到,yaffs2文件系统烧录成功。下一步需要加载内核来挂载yaffs2文件系统。

接下来将进入u-boot的第六阶段,为u-boot-2009.08 增加引导内核功能。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwfpsx.html