Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4471723
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-04-17 10:20:56

参考:http://blog.csdn.net/IT_114

在自己2.6.33 内核中,分区是:

  1. 1. static struct mtd_partition smdk_default_nand_part[] = {
  2.    2. #if defined (CONFIG_256MB_NAND)
  3.    3. [0] = {
  4.    4. .name = "ywx-uboot",
  5.    5. .size = 0x00040000, ##256KB
  6.    6. .offset = 0x0,
  7.    7. },
  8.    8. [1] = {
  9.    9. .name = "ywx-kernel",
  10.   10. .offset = 0x00200000,
  11.   11. .size = 0x00200000, ##2MB
  12.   12. },
  13.   13. [2] = {
  14.   14. .name = "ywx-yaffs2",
  15.   15. .offset = 0x00400000,
  16.   16. .size = 0x0fb80000, ##251.5MB
  17.   17. }
  18.   18. #endif
  19.   19. };


在 uboot 中分区如下:

0~0x40000 u-boot (256K)

0x200000~0x400000 kernel  (2M)

0x400000~~ (剩余的)


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

    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文件系统的读写支持。
 
2、在include/configs/smdk2440.h头文件中定义一个管理对Yaffs2支持的宏和开启u-boot中对Nand Flash默认分区的宏,如下:

vi include/configs/smdk2440.h

  1. //定义一个管理对Yaffs2支持的宏
  2. #define CONFIG_MTD_NAND_YAFFS2 1
  3. //开启Nand Flash默认分区,注意此处的分区要和你的内核中的分区保持一致
  4. #define MTDIDS_DEFAULT "nand0=nandflash0"
  5. #define MTDPARTS_DEFAULT "mtdparts=nandflash0:256k(bootloader)," \
  6.                                                             "2m(kernel)," \
  7.                                                             "-(root)"

3、在原来对Nand操作的命令集列表中添加Yaffs2对Nand的写命令,如下:

vi common/cmd_nand.c

在函数U_BOOT_CMD中添加如下:
//注意:这里只添加了yaffs2的写命令,因为我们只用u-boot下载(即写)功能,所以我们没有添加yaffs2读的命令

  1. U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
  2.     "NAND sub-system",
  3.     "info - show available NAND devices\n"
  4.     "nand device [dev] - show or set current device\n"
  5.     "nand read - addr off|partition size\n"
  6.     "nand write - addr off|partition size\n"
  7.     " read/write 'size' bytes starting at offset 'off'\n"
  8.     " to/from memory address 'addr', skipping bad blocks.\n"

/*添加 开始*/
  1. /*注意:这里只添加了yaffs2的写命令,因为我们只用u-boot下载(即写)功能,所以我们没有添加yaffs2读的命令*/
  2. #if defined(CONFIG_MTD_NAND_YAFFS2)
  3.         "nand write[.yaffs2] - addr off|partition size - write `size' byte yaffs image\n"
  4.         " starting at offset off' from memory address addr' (.yaffs2 for 512+16 NAND)\n"
  5. #endif
/*添加结束*/

  1.     "nand erase [clean] [off size] - erase 'size' bytes from\n"
  2.     " offset 'off' (entire device if not specified)\n"
  3.     "nand bad - show bad blocks\n"
  4.     "nand dump[.oob] off - dump page\n"
  5.     "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
  6.     "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
  7.     "nand biterr off - make a bit error at offset (UNSAFE)"
  8. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  9.     "\n"
  10.     "nand lock [tight] [status]\n"
  11.     " bring nand to lock state or display locked pages\n"
  12.     "nand unlock [offset] [size] - unlock section"
  13. #endif
  14. );

接着,在该文件中对nand操作的do_nand函数中添加yaffs2对nand的操作,如下:
  1. if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
  2.         int read;

  3.         if (argc < 4)
  4.             goto usage;

  5.         addr = (ulong)simple_strtoul(argv[2], NULL, 16);

  6.         read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
  7.         printf("\nNAND %s: ", read ? "read" : "write");
  8.         if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
  9.             return 1;

  10.         s = strchr(cmd, '.');
  11.         if (!s || !strcmp(s, ".jffs2") ||
  12.          !strcmp(s, ".e") || !strcmp(s, ".i")) {
  13.             if (read)
  14.                 ret = nand_read_skip_bad(nand, off, &size,
  15.                              (u_char *)addr);
  16.             else
  17.                 ret = nand_write_skip_bad(nand, off, &size,
  18.                              (u_char *)addr);
  19.         } 

/*添加开始*/
  1. /*添加yaffs2相关操作,注意该处又关联到nand_write_skip_bad函数*/

  2. #if defined(CONFIG_MTD_NAND_YAFFS2)
  3.         else if (s != NULL && (!strcmp(s, ".yaffs2")))
  4.         {
  5.             nand->rw_oob = 1;
  6.             nand->skipfirstblk = 1;
  7.             ret = nand_write_skip_bad(nand,off,&size,(u_char *)addr);
  8.             nand->skipfirstblk = 0;
  9.             nand->rw_oob = 0;
  10.         }
  11. #endif
  12. /*添加结束*/

  13.         else if (!strcmp(s, ".oob")) {
  14.             /* out-of-band data */
  15.             mtd_oob_ops_t ops = {
  16.                 .oobbuf = (u8 *)addr,
  17.                 .ooblen = size,
  18.                 .mode = MTD_OOB_RAW
  19.             };

  20.             if (read)
  21.                 ret = nand->read_oob(nand, off, &ops);
  22.             else
  23.                 ret = nand->write_oob(nand, off, &ops);
  24.         } else {
  25.             printf("Unknown nand command suffix '%s'.\n", s);
  26.             return 1;
  27.         }

  28.         printf(" %zu bytes %s: %s\n", size,
  29.          read ? "read" : "written", ret ? "ERROR" : "OK");

  30.         return ret == 0 ? 0 : 1;
  31.     }

在include/linux/mtd/mtd.h头文件的mtd_info结构体中添加上面用到rw_oob和skipfirstblk数据成员,如下:


#vi include/linux/mtd/mtd.h   //在mtd_info结构体中添加

  1. #if defined(CONFIG_MTD_NAND_YAFFS2)
  2.         u_char rw_oob;
  3.         u_char skipfirstblk;
  4. #endif

在第二步关联的nand_write_skip_bad函数中添加对Nand OOB的相关操作,如下:

#vi drivers/mtd/nand/nand_util.c  //在nand_write_skip_bad函数中添加


上传 nand_util.c 附件    nand_util.rar  

  1. int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  2.             u_char *buffer)
  3. {
  4.     int rval;
  5.     size_t left_to_write = *length;
  6.     size_t len_incl_bad;
  7.     u_char *p_buffer = buffer;

/*添加 开始*/
  1. #if defined(CONFIG_MTD_NAND_YAFFS2) /*add yaffs2 file system support*/
  2.     if(nand->rw_oob==1)
  3.     {
  4.         size_t oobsize = nand->oobsize;
  5.         size_t datasize = nand->writesize;
  6.         int datapages = 0;

  7.         if (((*length)%(nand->oobsize+nand->writesize)) != 0)
  8.         {
  9.          printf ("Attempt to write error length data!\n");
  10.          return -EINVAL;
  11.      }

  12.         datapages = *length/(datasize+oobsize);
  13.         *length = datapages*datasize;
  14.         left_to_write = *length;
  15.     }
  16. #endif
/*添加 结束*/


  1.     /* Reject writes, which are not page aligned */
  2.     if ((offset & (nand->writesize - 1)) != 0 ||
  3.      (*length & (nand->writesize - 1)) != 0) {
  4.         printf ("Attempt to write non page aligned data\n");
  5.         return -EINVAL;
  6.     }

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

  8.     if ((offset + len_incl_bad) > nand->size) {
  9.         printf ("Attempt to write outside the flash area\n");
  10.         return -EINVAL;
  11.     }


  12. /*修改 开始*/
  13. #if !defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support

  14.     if (len_incl_bad == *length) {
  15.         rval = nand_write (nand, offset, length, buffer);
  16.         if (rval != 0)
  17.             printf ("NAND write to offset %llx failed %d\n",offset, rval);
  18.                 

  19.         return rval;
  20.     }


  21. #endif
  22. /*修改 结束*/


  23.     while (left_to_write > 0) {
  24.         size_t block_offset = offset & (nand->erasesize - 1);
  25.         size_t write_size;

  26.         WATCHDOG_RESET ();

  27.         if (nand_block_isbad (nand, offset & ~(nand->erasesize - 1))) {
  28.             printf ("Skip bad block 0x%08llx\n",
  29.                 offset & ~(nand->erasesize - 1));
  30.             offset += nand->erasesize - block_offset;
  31.             continue;
  32.         }


  33. /*添加 开始*/
  34. #if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support
  35.         if(nand->skipfirstblk==1)
  36.         {
  37.             nand->skipfirstblk=0;
  38.             printf ("Skip the first good block %llx\n", offset & ~(nand->erasesize - 1));
  39.             offset += nand->erasesize - block_offset;
  40.             continue;
  41.         }
  42. #endif

  43. /*添加 结束*/


  44.         if (left_to_write < (nand->erasesize - block_offset))
  45.             write_size = left_to_write;
  46.         else
  47.             write_size = nand->erasesize - block_offset;

  48. /*添加 开始*/

  49.         printf("\rWriting at 0x%llx -- ",offset); //add yaffs2 file system support
  50. /*添加 结束*/

  51.         rval = nand_write (nand, offset, &write_size, p_buffer);
  52.         if (rval != 0) {
  53.             printf ("NAND write to offset %llx failed %d\n",
  54.                 offset, rval);
  55.             *length -= left_to_write;
  56.             return rval;
  57.         }

  58.         left_to_write -= write_size;
  59.         offset += write_size;
  60.        
  61. /*添加 开始*/
  62. #if defined(CONFIG_MTD_NAND_YAFFS2) //add yaffs2 file system support
  63.         if(nand->rw_oob==1)
  64.         {
  65.             p_buffer += write_size+(write_size/nand->writesize*nand->oobsize);
  66.         }
  67.         else
  68.         {
  69.             p_buffer += write_size;
  70.         }
  71. #else
  72.            p_buffer += write_size;
  73. #endif
  74. /*添加 结束*/

  75.     }

  76.     return 0;
  77. }


OK,对yaffs2支持的代码已修改完毕,重新编译u-boot并下载到nand中,启动开发板,在u-boot的命令行输入:nand help查看nand的命令,可以看到多了一个nand write[.yaffs2]的命令,这个就是用来下载yaffs2文件系统到nand中的命令了。

make smdk2440_config && make all

然后设置,DNW的地址为 0x0把根目录下的u-boot.bin 下载到Nandflash的地址0处

1.选择 nor flash启动模式

2.进入 nor flash 下载模式界面

3.选择 “1”

   [1] Download u-boot or STEPLDR.nb1 or other bootloader to Nand Flash

4.下载成功后,关机。

5.拨到从 nand flash 启动模式

6.打开电源,开机


  1. U-Boot 2010.06-rc1 ( 4鏈?17 2011 - 10:15:21)

  2. DRAM: 64 MiB
  3. Flash: 2 MiB
  4. NAND: 256 MiB
  5. In: serial
  6. Out: serial
  7. Err: serial
  8. Net: dm9000
  9. [SMDK2440]# nand help
  10. nand - NAND sub-system

  11. Usage:
  12. nand info - show available NAND devices
  13. nand device [dev] - show or set current device
  14. nand read - addr off|partition size
  15. nand write - addr off|partition size
  16.     read/write 'size' bytes starting at offset 'off'
  17.     to/from memory address 'addr', skipping bad blocks.
  18. nand write[.yaffs2] - addr off|partition size - write `size' byte yaffs image
  19.  starting at offset off' from memory address addr' (.yaffs2 for 512+16 NAND)
  20. nand erase [clean] [off size] - erase 'size' bytes from
  21.     offset 'off' (entire device if not specified)
  22. nand bad - show bad blocks
  23. nand dump[.oob] off - dump page
  24. nand scrub - really clean NAND erasing bad blocks (UNSAFE)
  25. nand markbad off [...] - mark bad block(s) at offset (UNSAFE)
  26. nand biterr off - make a bit error at offset (UNSAFE)
  27. [SMDK2440]#




































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