S3C6410使用---11uboot寫yaffs2文件系統(tǒng)過(guò)程分析
一、介紹
Nand flash K9GAG08U0D (2G Byte)
在u-boot的shell里面執(zhí)行如下命令: 把 rootfs.yaffs從SD卡的第一個(gè)分區(qū)讀取出來(lái),并寫到nand flash中去.
SMDK6401>fatload mmc 0:1 50008000 rootfs.yaffs
SMDK6401>nand erase 600000 $(filesize)
SMDK6401>nand write.yaffs2 50008000 600000 $(filesize)
這兒分析一下最后一條命令:將數(shù)據(jù)寫入到y(tǒng)affs2分區(qū)的過(guò)程
二、 過(guò)程分析
1.1 u-boot/common/cmd_nand.c中
intdo_nand(cmd_tbl_t*cmdtp,intflag,intargc,char*argv[])
{
if(strncmp(cmd,"read",4)==0||strncmp(cmd,"write",5)==0){
addr=(ulong)simple_strtoul(argv[2],NULL,16);
read=strncmp(cmd,"read",4)==0;/*1=read,0=write*/
if(arg_off_size(argc-3,argv+3,nand,&off,&size)!=0)
return 1;
s=strchr(cmd,'.');
if(!read&&s!=NULL&&+(!strcmp(s,".yaffs2")||!strcmp(s,".yaffs1")))
{
nand_write_options_t opts;
memset(&opts,0,sizeof(opts));
opts.buffer=(u_char*)addr;//addr=0x50008000內(nèi)存
opts.length=size;// length是文件長(zhǎng)度
opts.offset=off;// offset 是要寫到nand flash的地址0x600000
opts.pad=0;
opts.blockalign=1;
opts.quiet=quiet;
opts.writeoob=1;
opts.autoplace=1;
ret=nand_write_opts(nand,&opts);
}
}
argv[0] argv[1] argv[2] argv[3] argv[4]
nand write.yaffs2 50008000 600000 $(filesize)
addr off size=0x420000
1.2 在文件driver/nand/nand_utils.c中
intnand_write_opts(nand_info_t*meminfo,constnand_write_options_t*opts)
{
ulong mtdoffset=opts->offset;//mtdoffset=nand_flash中的偏移0x600000
ulong erasesize_blockalign;
u_char*buffer=opts->buffer;//buffer=(u_char*)0x500080
imglen=opts->length;//imglen是rootfs.yaffs2這個(gè)文件的長(zhǎng)度
while(imglen&&(mtdoffset
size)){ //下面這個(gè) while判斷要寫入的塊是不是壞塊,如果是壞塊繼續(xù)查找直到找到一個(gè)好塊
while(blockstart!=(mtdoffset&(~erasesize_blockalign+1))){
blockstart=mtdoffset&(~erasesize_blockalign+1);
offs=blockstart;
baderaseblock=0;
do{
intret=meminfo->block_isbad(meminfo,offs);//判斷是不是塊壞
if(ret<0){
printf("Bad block check failedn");
goto restoreoob;
}
if(ret==1){//ret=1是壞塊
baderaseblock=1;//這個(gè)地方還要設(shè)個(gè)標(biāo)志,直接do_something不就得了?
if(!opts->quiet)
printf("rBad block at 0x%lx "
"in erase block from "
"0x%x will be skippedn",
(long)offs,
blockstart);
}
if(baderaseblock){
mtdoffset=blockstart+erasesize_blockalign;//如果ret=1是壞塊,要寫入的起始位置指向下一個(gè)塊
}
offs+= erasesize_blockalign
/opts->blockalign;
}while(offs
}
readlen=meminfo->writesize;
memcpy(data_buf,buffer,readlen); //初始時(shí):buffer=(u_char*)0x50008000
buffer+=readlen;//meminfo->writesize= 4096
if (opts->writeoob) {