Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41490
  • 博文数量: 21
  • 博客积分: 840
  • 博客等级: 准尉
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-24 00:24
文章分类
文章存档

2010年(21)

我的朋友

分类: LINUX

2010-06-08 21:18:17

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <linux/compiler.h>
#include <mtd/mtd-user.h>

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<readlen)
                    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)
    {
        char     *pReadBuf;
        int         ret,i;
        int        nReadBytes;

        printf("read!\n");

    nReadBytes = info.erasesize;
        pReadBuf = malloc(nReadBytes);
        if(pReadBuf==NULL)
        {
            printf("%s--no memory avaliable!\n",__FUNCTION__);
            return -1;
        }

    memset(pReadBuf,0,nReadBytes);
        ret = read(fd,pReadBuf,256);
        printf("%s--read %d bytes from mtd\n",__FUNCTION__,ret);
    for(i=0; i<ret; i++)
            printf("%02x,",*(pReadBuf+i));
        free(pReadBuf);
    }


    return 1;
}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/freenaut/archive/2009/06/26/4300293.aspx

阅读(1414) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~