u-boot-2009.08在mini2440上的移植(五)---增加yaffs2文件系統(tǒng)
移植環(huán)境
1,主機環(huán)境:VMare下CentOS 5.5 ,1G內(nèi)存。
2,集成開發(fā)環(huán)境:Elipse IDE
3,編譯編譯環(huán)境:arm-linux-gcc v4.4.3,arm-none-eabi-gcc v4.5.1。
4,開發(fā)板: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鏡像燒寫功能
由于現(xiàn)在很多使用Nand Flash 的系統(tǒng),在Linux 下都用Yaffs2作為存儲數(shù)據(jù)的文件系統(tǒng),甚至是根文件系統(tǒng)。所以在BootLoader 下能夠燒寫Yaffs2 映像文件變得很必要。對于Yaffs2 映像燒寫的支持其實就是在燒寫時,寫入數(shù)據(jù)的同時,將鏡像文件中的oob數(shù)據(jù)也寫入到Nand Flash 的Spare 區(qū)。這和Yaffs 文件系統(tǒng)原理以及Nand Flash 的結(jié)構(gòu)有關(guān),注意:這里對Nand的操作是基于MTD架構(gòu)方式。
通常一個Nnad Flash存儲設(shè)備由若干塊組成,1個塊由若干頁組成。一般128MB以下容量的Nand Flash芯片,一頁大小為528B,被依次分為2個256B的主數(shù)據(jù)區(qū)和16B的額外空間;128MB以上容量的Nand Flash芯片,一頁大小通常為2KB。由于Nand Flash出現(xiàn)位反轉(zhuǎn)的概率較大,一般在讀寫時需要使用ECC進(jìn)行錯誤檢驗和恢復(fù)。
Yaffs/yaffs2文件系統(tǒng)的設(shè)計充分考慮到Nand Flash以頁為存取單位等的特點,將文件組織成固定大小的段(Chunk)。以528B的頁為例,Yaffs/yaffs2文件系統(tǒng)使用前512B存儲 數(shù)據(jù)和16B的額外空間存放數(shù)據(jù)的ECC和文件系統(tǒng)的組織信息等(稱為OOB數(shù)據(jù))。通過OOB數(shù)據(jù),不但能實現(xiàn)錯誤檢測和壞塊處理,同時還可以避免加載 時對整個存儲介質(zhì)的掃描,加快了文件系統(tǒng)的加載速度。以下是Yaffs/yaffs2文件系統(tǒng)頁的結(jié)構(gòu)說明:
Yaffs頁結(jié)構(gòu)說明
==============================================
字節(jié) 用途
==============================================
0 - 511 存儲數(shù)據(jù)(分為兩個半部)
512 - 515 系統(tǒng)信息
516 數(shù)據(jù)狀態(tài)字
517 塊狀態(tài)字
518 - 519 系統(tǒng)信息
520 - 522 后半部256字節(jié)的ECC
523 - 524 系統(tǒng)信息
525 - 527 前半部256字節(jié)的ECC
==============================================
好了,在了解Nand Flash組成和Yaffs/yaffs2文件系統(tǒng)結(jié)構(gòu)后,我們再回到u-boot中。目前,在u-boot中已經(jīng)有對Cramfs、Jffs2等文件系統(tǒng)的讀寫支持,但與帶有數(shù)據(jù)校驗等功能的OOB區(qū)的Yaffs/Yaffs2文件系統(tǒng)相比,他們是將所有文件數(shù)據(jù)簡單的以線性表形式組織的。所以,我們只要在此基礎(chǔ)上通過修改u-boot的Nand Flash讀寫命令,增加處理00B區(qū)域數(shù)據(jù)的功能,即可以實現(xiàn)對Yaffs/Yaffs2文件系統(tǒng)的讀寫支持。
實現(xiàn)u-boot對Yaffs或者Yaffs2文件系統(tǒng)的讀寫支持步驟如下:
【1】下面是需要修改的 4 個文件的補?。?br/>(1)打開/common/cmd_nand.c,定位到392行,在nand操作的do_nand函數(shù)中,修改如下:
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結(jié)構(gòu)體中添加上面用到rw_oob和skipfirstblk數(shù)據(jù)成員,修改如下:
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)在第一步關(guān)聯(lián)的nand_write_skip_bad函數(shù)中添加對Nand OOB的相關(guān)操作,修改如下:
打開/drivers/mtd/nand/nand_util.c,定位到483行,在nand_write_skip_bad函數(shù)中添加代碼,修改如下:
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;