u-boot-2009.08在mini2440上的移植(五)---增加yaffs2文件系统
扫描二维码
随时随地手机看文章
移植环境
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
6,参考文章:
http://blogold.chinaunix.net/u3/101649/showart.php?id=2105215
http://blog.chinaunix.net/space.php?uid=23787856&do=blog&id=115382
http://blogimg.chinaunix.net/blog/upfile2/100811115954.pdf
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 devicesn"
"nand device [dev] - show or set current devicen"
"nand read - addr off|partition sizen"
"nand write - addr off|partition sizen"
" 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 fromn"
" 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 imagen"
" starting at offset off' from memory address addr' (.yaffs2 for 512+16 NAND)n"
#endif
"nand bad - show bad blocksn"
"nand dump[.oob] off - dump pagen"
"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 pagesn"
"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 datan");
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 arean");
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 %dn",
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%08llxn",
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 %llxn", 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;