新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 嵌入式Linux之我行——u-boot-2009.08在2440上的移植詳解(五)

嵌入式Linux之我行——u-boot-2009.08在2440上的移植詳解(五)

作者: 時(shí)間:2016-11-20 來源:網(wǎng)絡(luò) 收藏
一、移植環(huán)境
  • 機(jī):VMWare--Fedora 9
  • 開發(fā)板:Mini2440--64MB Nand,Kernel:2.6.30.4
  • 編譯器:arm123.com.cn/linux/arm-linux-gcc-4.3.2.tgz" target="_blank">arm-linux-gcc-4.3.2.tgz
  • u-boot:u-boot-2009.08.tar.bz2

二、移植步驟

本文引用地址:http://butianyuan.cn/article/201611/319003.htm
9)實(shí)現(xiàn)u-boot對(duì)yaffs/yaffs2文件系統(tǒng)下載的支持。
注意:此篇對(duì)Nand的操作是基于MTD架構(gòu)方式,在“u-boot-2009.08在2440上的移植詳解(三)”中講到過。
通常一個(gè)Nnad Flash存儲(chǔ)設(shè)備由若干塊組成,1個(gè)塊由若干頁組成。一般128MB以下容量的Nand Flash芯片,一頁大小為528B,被依次分為2個(gè)256B的主數(shù)據(jù)區(qū)和16B的額外空間;128MB以上容量的Nand Flash芯片,一頁大小通常為2KB。由于Nand Flash出現(xiàn)位反轉(zhuǎn)的概率較大,一般在讀寫時(shí)需要使用ECC進(jìn)行錯(cuò)誤檢驗(yàn)和恢復(fù)。
Yaffs/yaffs2文件系統(tǒng)的設(shè)計(jì)充分考慮到Nand Flash以頁為存取單位等的特點(diǎn),將文件組織成固定大小的段(Chunk)。以528B的頁為例,Yaffs/yaffs2文件系統(tǒng)使用前512B存儲(chǔ)數(shù)據(jù)和16B的額外空間存放數(shù)據(jù)的ECC和文件系統(tǒng)的組織信息等(稱為OOB數(shù)據(jù))。通過OOB數(shù)據(jù),不但能實(shí)現(xiàn)錯(cuò)誤檢測和壞塊處理,同時(shí)還可以避免加載時(shí)對(duì)整個(gè)存儲(chǔ)介質(zhì)的掃描,加快了文件系統(tǒng)的加載速度。以下是Yaffs/yaffs2文件系統(tǒng)頁的結(jié)構(gòu)說明:

Yaffs頁結(jié)構(gòu)說明
==============================================
字節(jié) 用途
==============================================
0 - 511 存儲(chǔ)數(shù)據(jù)(分為兩個(gè)半部)
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)后,我們?cè)倩氐絬-boot中。目前,在u-boot中已經(jīng)有對(duì)Cramfs、Jffs2等文件系統(tǒng)的讀寫支持,但與帶有數(shù)據(jù)校驗(yàn)等功能的OOB區(qū)的Yaffs/Yaffs2文件系統(tǒng)相比,他們是將所有文件數(shù)據(jù)簡單的以線性表形式組織的。所以,我們只要在此基礎(chǔ)上通過修改u-boot的Nand Flash讀寫命令,增加處理00B區(qū)域數(shù)據(jù)的功能,即可以實(shí)現(xiàn)對(duì)Yaffs/Yaffs2文件系統(tǒng)的讀寫支持。

實(shí)現(xiàn)對(duì)Yaffs或者Yaffs2文件系統(tǒng)的讀寫支持步驟如下:
①、在include/configs/my2440.h頭文件中定義一個(gè)管理對(duì)Yaffs2支持的宏和開啟u-boot中對(duì)Nand Flash默認(rèn)分區(qū)的宏,如下:

#gedit include/configs/my2440.h//添加到文件末尾即可

#define CONFIG_MTD_NAND_YAFFS21//定義一個(gè)管理對(duì)Yaffs2支持的宏

//開啟Nand Flash默認(rèn)分區(qū),注意此處的分區(qū)要和你的內(nèi)核中的分區(qū)保持一致
#define MTDIDS_DEFAULT"nand0=nandflash0"
#define MTDPARTS_DEFAULT"mtdparts=nandflash0:192k(bootloader),"
"64k(params),"
"2m(kernel),"
"-(root)"

②、在原來對(duì)Nand操作的命令集列表中添加Yaffs2對(duì)Nand的寫命令,如下:

#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 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 offn"
" to/from memory address addr, skipping bad blocks.n"

//注意:這里只添加了yaffs2的寫命令,因?yàn)槲覀冎挥胾-boot下載(即寫)功能,所以我們沒有添加yaffs2讀的命令
#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 erase [clean] [off size] - erase size bytes fromn"
" offset off (entire device if not specified)n"
"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
);


接著,在該文件中對(duì)nand操作的do_nand函數(shù)中添加yaffs2對(duì)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;
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);
}

//添加yaffs2相關(guān)操作,注意該處又關(guān)聯(lián)到nand_write_skip_bad函數(shù)

#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"))
{

mtd_oob_ops_t ops =
{
.oobbuf = (u8 *)addr,
.ooblen = size,
.mode = MTD_OOB_RAW
};

if (read)
ret = nand->read_oob(nand, off, &ops);
else
ret = nand->write_oob(nand, off, &ops);
}
else
{
printf("Unknown nand command suffix %s.n", s);
return 1;
}

printf(" %zu bytes %s: %sn", size, read ? "read" : "written", ret ? "ERROR" : "OK");

return ret == 0 ? 0 : 1;
}


③、在include/linux/mtd/mtd.h頭文件的mtd_info結(jié)構(gòu)體中添加上面用到rw_oob和skipfirstblk數(shù)據(jù)成員,如下:

#gedit include/linux/mtd/mtd.h//在mtd_info結(jié)構(gòu)體中添加

#if defined(CONFIG_MTD_NAND_YAFFS2)
u_char rw_oob;
u_char skipfirstblk;
#endif


④、在第二步關(guān)聯(lián)的nand_write_skip_bad函數(shù)中添加對(duì)Nand OOB的相關(guān)操作,如下:

#gedit drivers/mtd/nand/nand_util.c//在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)//add yaffs2 file system support
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


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 0xllxn",
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;

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 %dn",
offset, rval);
*length -= left_to_write;
return rval;
}

left_to_write -= write_size;
printf("%d%% is complete.",100-(left_to_write/(*length/100)));
offset += write_size;

#if defined(CONFIG_MTD_NAND_YAFFS2)//add yaffs2 file system support
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;
}


⑤、在第四步nand_write_skip_bad函數(shù)中我們看到又對(duì)nand_write函數(shù)進(jìn)行了訪問,所以這一步是到nand_write函數(shù)中添加對(duì)yaffs2的支持,如下:

#gedit drivers/mtd/nand/nand_base.c//在nand_write函數(shù)中添加

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)//add yaffs2 file system support

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


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_YAFFS2)//add yaffs2 file system support

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

chip->ops.mode = oldopsmode;
#endif

return ret;
}


OK,對(duì)yaffs2支持的代碼已修改完畢,重新編譯u-boot并下載到nand中,啟動(dòng)開發(fā)板,在u-boot的命令行輸入:nand help查看nand的命令,可以看到多了一個(gè)nand write[.yaffs2]的命令,這個(gè)就是用來下載yaffs2文件系統(tǒng)到nand中的命令了。

⑥、使用nand write[.yaffs2]命令把事前制作好的yaffs2文件系統(tǒng)下載到Nand Flash中(yaffs2文件系統(tǒng)的制作請(qǐng)參考:Linux-2.6.30.4在2440上的移植之文件系統(tǒng)),下載操作步驟和效果圖如下:

tftp 0x30000000 root-2.6.30.4.bin//用tftp將yaffs2文件系統(tǒng)下載到內(nèi)存的0x30000000位置

nand erase 0x250000 0x3dac000//擦除Nand的文件系統(tǒng)分區(qū)

nand write.yaffs2 0x30000000 0x250000 0x658170//將內(nèi)存中的yaffs2文件系統(tǒng)寫入Nand的文件系統(tǒng)分區(qū),注意這里的0x658170是yaffs2文件系統(tǒng)的實(shí)際大小(可以在tftp傳送完后可以看到),要寫正確,否則會(huì)形成假壞塊




⑦、結(jié)合u-boot和內(nèi)核來測試啟動(dòng)下載的yaffs2文件系統(tǒng)
設(shè)置u-boot啟動(dòng)參數(shù)bootargs,注意:這一長串參數(shù)要與內(nèi)核配置里面的Boot options-->Default kernel command string的設(shè)置要一致。特別是mtdblock3要根據(jù)內(nèi)核具體的分區(qū)來設(shè),在上一篇中講到了內(nèi)核中Nand的分區(qū)情況,u-boot屬于mtdblock0,param屬于mtdblock1,kernel屬于mtdblock2,root就屬于mtdblock3,所以這里要設(shè)置成root=/dev/mtdblock3,否則文件系統(tǒng)無法啟動(dòng)成功,會(huì)出現(xiàn)一些什么I/O之類的錯(cuò)誤

好了,最后重啟開發(fā)板,內(nèi)核引導(dǎo)成功,yaffs2文件系統(tǒng)也掛載成功,效果圖如下:



關(guān)鍵詞: Linuxu-boot244

評(píng)論


技術(shù)專區(qū)

關(guān)閉