apps/utils/mtdutils-IR1_0_1-3/flash_erase.c

Go to the documentation of this file.
00001 /*
00002  * flash_erase.c -- erase parts of a MTD device
00003 */
00004 
00005 #include <unistd.h>
00006 #include <stdlib.h>
00007 #include <stdio.h>
00008 #include <fcntl.h>
00009 #include <time.h>
00010 #include <string.h>
00011 #include <sys/ioctl.h>
00012 #include <sys/mount.h>
00013 #include <mtd/mtd-user.h>
00014 
00015 int region_erase(int Fd, int start, int count, int unlock, int regcount)
00016 {
00017         int i, j;
00018         region_info_t * reginfo;
00019 
00020         reginfo = calloc(regcount, sizeof(region_info_t));
00021 
00022         for(i = 0; i < regcount; i++)
00023         {
00024                 reginfo[i].regionindex = i;
00025                 if(ioctl(Fd,MEMGETREGIONINFO,&(reginfo[i])) != 0)
00026                         return 8;
00027                 else
00028                         printf("Region %d is at %d of %d sector and with sector "
00029                                         "size %x\n", i, reginfo[i].offset, reginfo[i].numblocks,
00030                                         reginfo[i].erasesize);
00031         }
00032 
00033         // We have all the information about the chip we need.
00034 
00035         for(i = 0; i < regcount; i++)
00036         { //Loop through the regions
00037                 region_info_t * r = &(reginfo[i]);
00038 
00039                 if((start >= reginfo[i].offset) &&
00040                                 (start < (r->offset + r->numblocks*r->erasesize)))
00041                         break;
00042         }
00043 
00044         if(i >= regcount)
00045         {
00046                 printf("Starting offset %x not within chip.\n", start);
00047                 return 8;
00048         }
00049 
00050         //We are now positioned within region i of the chip, so start erasing
00051         //count sectors from there.
00052 
00053         for(j = 0; (j < count)&&(i < regcount); j++)
00054         {
00055                 erase_info_t erase;
00056                 region_info_t * r = &(reginfo[i]);
00057 
00058                 erase.start = start;
00059                 erase.length = r->erasesize;
00060 
00061                 if(unlock != 0)
00062                 { //Unlock the sector first.
00063                         if(ioctl(Fd, MEMUNLOCK, &erase) != 0)
00064                         {
00065                                 perror("\nMTD Unlock failure");
00066                                 close(Fd);
00067                                 return 8;
00068                         }
00069                 }
00070                 printf("\rPerforming Flash Erase of length %u at offset 0x%x",
00071                                 erase.length, erase.start);
00072                 fflush(stdout);
00073                 if(ioctl(Fd, MEMERASE, &erase) != 0)
00074                 {
00075                         perror("\nMTD Erase failure");
00076                         close(Fd);
00077                         return 8;
00078                 }
00079 
00080 
00081                 start += erase.length;
00082                 if(start >= (r->offset + r->numblocks*r->erasesize))
00083                 { //We finished region i so move to region i+1
00084                         printf("\nMoving to region %d\n", i+1);
00085                         i++;
00086                 }
00087         }
00088 
00089         printf(" done\n");
00090 
00091         return 0;
00092 }
00093 
00094 int non_region_erase(int Fd, int start, int count, int unlock)
00095 {
00096         mtd_info_t meminfo;
00097 
00098         if (ioctl(Fd,MEMGETINFO,&meminfo) == 0)
00099         {
00100                 erase_info_t erase;
00101 
00102                 erase.start = start;
00103 
00104                 erase.length = meminfo.erasesize;
00105 
00106                 for (; count > 0; count--) {
00107                         printf("\rPerforming Flash Erase of length %u at offset 0x%x",
00108                                         erase.length, erase.start);
00109                         fflush(stdout);
00110 
00111                         if(unlock != 0)
00112                         {
00113                                 //Unlock the sector first.
00114                                 printf("\rPerforming Flash unlock at offset 0x%x",erase.start);
00115                                 if(ioctl(Fd, MEMUNLOCK, &erase) != 0)
00116                                 {
00117                                         perror("\nMTD Unlock failure");
00118                                         close(Fd);
00119                                         return 8;
00120                                 }
00121                         }
00122 
00123                         if (ioctl(Fd,MEMERASE,&erase) != 0)
00124                         {
00125                                 printf("\nMTD Erase failure at: 0x%08X", erase.start);
00126 //                              perror("\nMTD Erase failure");
00127 //                              close(Fd);
00128 //                              return 8;
00129                         }
00130                         erase.start += meminfo.erasesize;
00131                 }
00132                 printf(" done\n");
00133         }
00134         return 0;
00135 }
00136 
00137 int main(int argc,char *argv[])
00138 {
00139         int regcount;
00140         int Fd;
00141         int start;
00142         int count;
00143         int unlock;
00144         int res = 0;
00145 
00146         if (1 >= argc ||  !strcmp(argv[1], "-h") || !strcmp (argv[1], "--help") ) {
00147                 printf("Usage: flash_erase MTD-device [start] [cnt (# erase blocks)] [lock]\n"
00148                        "       flash_erase -h | --help\n") ;
00149                 return 16 ;
00150         }
00151 
00152         if (argc > 2)
00153                 start = strtol(argv[2], NULL, 0);
00154         else
00155                 start = 0;
00156 
00157         if (argc > 3)
00158                 count = strtol(argv[3], NULL, 0);
00159         else
00160                 count = 1;
00161 
00162         if(argc > 4)
00163                 unlock = strtol(argv[4], NULL, 0);
00164         else
00165                 unlock = 0;
00166 
00167 
00168         // Open and size the device
00169         if ((Fd = open(argv[1],O_RDWR)) < 0)
00170         {
00171                 fprintf(stderr,"File open error\n");
00172                 return 8;
00173         }
00174 
00175         printf("Erase Total %d Units\n", count);
00176 
00177         if (ioctl(Fd,MEMGETREGIONCOUNT,&regcount) == 0)
00178         {
00179                 if(regcount == 0)
00180                 {
00181                         res = non_region_erase(Fd, start, count, unlock);
00182                 }
00183                 else
00184                 {
00185                         res = region_erase(Fd, start, count, unlock, regcount);
00186                 }
00187         }
00188 
00189         return res;
00190 }

Generated on Fri Nov 28 00:06:23 2008 for elphel by  doxygen 1.5.1