linux flash擦除命令,linux下擦除、写入flash的简单编程参考

linux下对dataflash设备操作(擦除、写入文件)的应用程序,注意,所有的操作必须要使用mtdx的字符设备。

mtdchar的字符设备驱动编程参考!

来自:

http://blog.csdn.net/shanzhizi

#include

#include

#include

#include

#include

#include

#include

#include

#include

int non_region_erase(int Fd, int start, int count, int unlock)

{

mtd_info_t meminfo;

if (ioctl(Fd,MEMGETINFO,&meminfo) == 0)

{

erase_info_t erase;

erase.start = start;

erase.length = meminfo.erasesize;

for (; count > 0; count--) {

printf("/rPerforming Flash Erase of length %u at offset 0x%x",

erase.length, erase.start);

fflush(stdout);

if(unlock != 0)

{

//Unlock the sector first.

printf("/rPerforming Flash unlock at offset 0x%x",erase.start);

if(ioctl(Fd, MEMUNLOCK, &erase) != 0)

{

perror("/nMTD Unlock failure");

close(Fd);

return 8;

}

}

if (ioctl(Fd,MEMERASE,&erase) != 0)

{

perror("/nMTD Erase failure");

close(Fd);

return 8;

}

erase.start += meminfo.erasesize;

}

printf(" done/n");

}

return 0;

}

int main(int argc, char *argv[])

{

int fd,ifd;

int i,j;

char *cmd;

char *img;

struct mtd_info_user info;

int regcount;

int res = 0,cnt,write_cnt;

int imglen,imglen1;

int readlen=0;

char *write_buf;

if(3>argc)

{

printf("You must specify a device!/n");

return 16;

}

cmd=argv[1];

if (argc > 3)

{

img=argv[3];

}

// Open and size the device

if ((fd = open(argv[2],O_RDWR)) < 0)

{

fprintf(stderr,"File open error/n");

return 8;

}

else

{

ioctl(fd,MEMGETINFO,&info);

printf("info.size=%d/n info.erasesize=%d/n info.writesize=%d/n info.oobsize=%d                /n",info.size,info.erasesize,info.writesize,info.oobsize);

}

if (ioctl(fd,MEMGETREGIONCOUNT,&regcount) == 0)

{

printf("regcount=%d/n",regcount);

}

/*erase the device*/

if (strcmp(cmd, "erase") == 0)

{

if(regcount == 0)

{

res = non_region_erase(fd,0,(info.size/info.erasesize),0);

}

printf("erase!/n");

}

/*write file to this device*/

if (strcmp(cmd, "write") == 0)

{

printf("write!/n");

if((ifd=open(img,O_RDONLY))==-1)

{

printf("open input file error!/n");

return 9;

}

else

{

imglen=lseek(ifd,0,SEEK_END);

lseek(ifd,0,SEEK_SET);

printf("The input file image len is %d!/n",imglen);

}

readlen=info.writesize;

write_buf=malloc(readlen);

if(write_buf==NULL)

{

printf("can't allocate memory!/n");

return 10;

}

else

printf("Write buf is ok!/n");

imglen1=imglen;

while(imglen)

{

memset(write_buf,0xff,readlen);

if((cnt=read(ifd,write_buf,readlen))!=readlen)

{

if(cnt

printf("the file is end!/n");

else

{

printf("File I/O error on input file");

return 11;

}

}

if((write_cnt=write(fd,write_buf,readlen))!=readlen)

{

printf("write mtd device error! startaddr=%d/n",(imglen1-imglen));

return 12;

}

else

printf("/rwrite mtd device ok!startaddr=%d",(imglen1-imglen));

imglen-=cnt;

}

printf("/n");

return 0;

}

if (strcmp(cmd, "read") == 0)

{

printf("read!/n");

}

return 1;

}

命令格式:

dataflash_ioctl erase /dev/mtd6

info.size=1968384

info.erasesize=528

info.writesize=528

info.oobsize=0

regcount=0

Performing Flash Erase of length 528 at offset 0x1e06f0 done

erase!

写入文件命令:

dataflash_ioctl write /dev/mtd6 /modules/uImage2.6.21

info.size=1968384

info.erasesize=528

info.writesize=528

info.oobsize=0

regcount=0

write!

The input file image len is 1406740!

Write buf is ok!

write mtd device ok!startaddr=1406064the file is write ok!

write mtd device ok!startaddr=1406592

修改u-boot的启动参数

cp.b d023f700 20400000 158000

其中d023f700是mtd6在

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值