运行成功的读写MTD的测试程序

#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>

struct BSL
{
 char  booslink[10];  //a fixed string idefined as booslink
 //char  serialNumber[24];
//++++add a Serial Number
 //char  PCBA_serialNumber[24];
 //char  reserved_0[24];
};

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;
 
    char *cmd;
    struct mtd_info_user info;   
    int regcount;
    int ret;
    int res;

    if(3>argc)
    {
        printf("You must specify a device!\n");
        return 16;
    }
    cmd=argv[1];

    // Open 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*/
    struct BSL *bsl_write =(struct BSL*)malloc(sizeof(struct BSL));
    if (strcmp(cmd, "write") == 0)
    {
        printf("write ok!\n");
        strcpy(bsl_write->booslink,argv[3]);
        ret = write(fd, bsl_write, sizeof(struct BSL) );

        return 0;
    }

    /*read file from this device*/
    struct BSL *bsl_read =(struct BSL*)malloc(sizeof(struct BSL));
    if (strcmp(cmd, "read") == 0)
    {
      read(fd, bsl_read, sizeof(struct BSL));
      printf("%s\n", bsl_read);
        printf("read ok!\n");
    }
   
    return 1;
}

你可能感兴趣的:(struct,测试,cmd,File,Flash,include)