S3C6410使用---11uboot写yaffs2文件系统过程分析
扫描二维码
随时随地手机看文章
一、介绍
Nand flash K9GAG08U0D (2G Byte)
在u-boot的shell里面执行如下命令: 把 rootfs.yaffs从SD卡的第一个分区读取出来,并写到nand flash中去.
SMDK6401>fatload mmc 0:1 50008000 rootfs.yaffs
SMDK6401>nand erase 600000 $(filesize)
SMDK6401>nand write.yaffs2 50008000 600000 $(filesize)
这儿分析一下最后一条命令:将数据写入到yaffs2分区的过程
二、 过程分析
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内存
opts.length=size;// length是文件长度
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这个文件的长度
while(imglen&&(mtdoffset
size)){ //下面这个 while判断要写入的块是不是坏块,如果是坏块继续查找直到找到一个好块
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;//这个地方还要设个标志,直接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是坏块,要写入的起始位置指向下一个块
}
offs+= erasesize_blockalign
/opts->blockalign;
}while(offs
}
readlen=meminfo->writesize;
memcpy(data_buf,buffer,readlen); //初始时:buffer=(u_char*)0x50008000
buffer+=readlen;//meminfo->writesize= 4096
if (opts->writeoob) {