分类: 嵌入式
2012-05-17 14:39:52
一、 步骤六:U-boot支持yaffs格式的文件下载
前面我们已经移植、修改好了基于cramfs格式的根文件系统,本节我们来修改u-boot的源码,使之支持yaffs格式的根文件系统。
cramfs与yaffs文件系统的区别:
通常一个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文件系统的读写支持。
我们需要按照以下步骤修改:
1) 在include/configs/unsp2440.h中添加yaffs2烧写宏定义
#define CONFIG_MTD_NAND_YAFFS 1 //定义一个管理对Yaffs2支持的宏 |
2) 增加yaffs烧写命令:
#gedit common/cmd_nand.c //在U_BOOT_CMD中添加
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" #if defined(CONFIG_MTD_NAND_YAFFS) "nand_write[.yaffs2] -addr of | partition size - write 'size' byte yaffs image\n" "starting at offset off'from memory address addr'(.yaffs2 for 2048+64 NAND)\n" #endif "nand erase [clean] [off size] - erase 'size' bytes from\n" "offset 'off' (entire device if not specified)\n" 。。。。 #endif ); |
3) 在该文件中对nand操作的do_nand函数中添加yaffs2对nand的操作,如下
if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { int read; if (argc < 4) goto usage; addr = (ulong)simple_strtoul(argv[2], NULL, 16); read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ printf("\nNAND %s: ", read ? "read" : "write"); if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0) return 1; s = strchr(cmd, '.'); if (!s || !strcmp(s, ".jffs2") ||!strcmp(s, ".e") || !strcmp(s, ".i")) { 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_YAFFS) else if(s!=NULL & (!strcmp(s,".yaffs2"))) { nand->rw_oob = 1; nand->skipfirstblk = 1; //写入yaffs,不支持读入 ret = nand_write_skip_bad(nand,off,&size,(u_char *)addr); nand->skipfirstblk = 0; nand->rw_oob = 0; } #endif else if (!strcmp(s, ".oob")) 。。。。 |
4) 在include/linux/mtd/mtd.h头文件的mtd_info结构体中添加上面用到rw_oob和skipfirstblk数据成员,如下:
struct mtd_info { u_char type; u_int32_t flags; uint64_t size; /* Total size of the MTD */ #if defined(CONFIG_MTD_NAND_YAFFS) u_char rw_oob; u_char skipfirstblk; #endif 。。。。 } |
5) 在nand_write_skip_bad函数中添加对Nand OOB的相关操作,如下:
#gedit drivers/mtd/nand/nand_util.c //在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_YAFFS) //addr yaffs2 file system support if(nand->rw_oob == 1) { size_t oobsize = nand->oobsize; //定义oobsize的大小 size_t datasize = nand->writesize;//可用的数据的大小 int datapages = 0; //长度不是528整数倍,认为数据出错。文件大小必须要是(512+16)的整数倍 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 (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; } #if !defined(CONFIG_MTD_NAND_YAFFS) if(len_ind_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_YAFFS) if(nand->skipfirstblk==1) { nand->skipfirstblk =0; printf("skip 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; 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; offset += write_size; //p_buffer += write_size; #if defined(CONFIG_MTD_NAND_YAFFS) 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; } |
6) 在nand_write_skip_bad函数中我们看到又对nand_write函数进行了访问,所以这一步是到nand_write函数中添加对yaffs2的支持,如下
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_YAFFS) int oldopsmode = 0; if(mtd->rw_oob==1) { int i=0; int datapages = 0; size_t oobsize = mtd->oobsize;//定义oobsize的大小
size_t datasize = mtd->writesize;//定义正常的数据区的大小 uint8_t oobtemp[oobsize]; datapages = len /(datasize); //传进来的len是没有包括oob的数据长度
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 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; #if defined(CONFIG_MTD_NAND_YAFFS) if(mtd->rw_oob!=1) { chip->ops.oobbuf = NULL; } else { chip->ops.oobbuf = (uint8_t *)(buf+len); //将oob缓存的指针指向buf的后段,即oob数据区的起始地址。 chip->ops.ooblen = mtd->oobsize; oldopsmode = chip->ops.mode; chip->ops.mode = MTD_OOB_RAW; //将写入模式改为直接书写oob区,即写入数据时,不进行ECC校验的计算和写入。 //(yaffs映像的oob数据中,本身就带有ECC校验) } #else chip->ops.oobbuf = NULL; #endif //chip->ops.oobbuf = NULL; ret = nand_do_write_ops(mtd, to, &chip->ops); *retlen = chip->ops.retlen; nand_release_device(mtd); #if defined(CONFIG_MTD_NAND_YAFFS) //add yaffs2 file system support chip->ops.mode = oldopsmode; return ret; } |
OK,对yaffs2支持的代码已修改完毕,重新编译u-boot并下载到nand中,启动开发板,在u-boot的命令行输入:nand help查看nand的命令,可以看到多了一个nand write[.yaffs2]的命令,这个就是用来下载yaffs2文件系统到nand中的命令了。
7) 使用nand write[.yaffs2]命令把事前制作好的yaffs2文件系统下载到Nand Flash中
#tftp 0x33000000 172.20.223.63:rootfs.yaffs
//用tftp将yaffs2文件系统下载到内存的0x33000000位置
#nand erase 0x350000 0x3cac000
//擦除Nand的文件系统分区
#nand write.yaffs2 0x30000000 0x250000 0x658170
//将内存中的yaffs2文件系统写入Nand的文件系统分区,注意这里的0x658170是yaffs2文件系统的实际大小,且必须能被528整除才可以