Application manipulation NorFlash sample code share of NorFlash interface using methods
- 2020-04-02 02:05:48
- OfStack
int dealwithnor()
{
// glob_t mtdbuf;
struct mtd_info_user mtd;
struct erase_info_user erase;
int blocks = 0;
int i = 0; //Controls the number of blocks erased
int k = 0;
int written = 0; //Number of bytes written, initialized only once
unsigned int size = StateOfImage.st_size; //Should be the actual size of the mirror, because the contents of the memory space larger than the mirror are unpredictable
unsigned int result = 0;
unsigned int DevNum = 0; //Number of equipment
unsigned int StartDev = 0; //Erase from startDev
char DevName[20] = {0};
unsigned int AllSize = 0;
#define MAXPARTITIONS 40
struct DeviceInfo
{
int fd;
char dir[20];
uint32_t size; // Total size of the MTD
uint32_t erasesize;
}DevInfo[MAXPARTITIONS];//Used to store device information
bzero(DevInfo, sizeof(struct DeviceInfo));
// if(searchmtd(&mtdbuf) != 0){
// DEBUG("Sorry! Can not find mtd devicen");
// return 1; // Returns a number greater than zero, indicating that the upgrade failed
// }
// else
// {
// int fd;
//
// DEBUG("find %d mtd devices n",mtdbuf.gl_pathc);
// DevNum = mtdbuf.gl_pathc;
// for(i=0; i<DevNum; i++)
// {
// fd = safeOpen (mtdbuf.gl_pathv[i],O_SYNC | O_RDWR);
// if(fd < 0)
// {
// printf("failt to openn");
// return 1;
// }
// bzero(&mtd, sizeof(struct mtd_info_user));
// if (ioctl(fd, MEMGETINFO,&mtd) < 0)
// {
// DEBUG("ioctl(): %mn");
// DEBUG("This doesn't seem to be a valid MTD flash device!n");
// return 1;
// }
//
// strcpy(DevInfo[i].dir, mtdbuf.gl_pathv[i]);
// DevInfo[i].fd = fd;
// DevInfo[i].size = mtd.size;
// DevInfo[i].erasesize = mtd.erasesize;
// }
// globfree(&mtdbuf);
// for(i=0; i<DevNum; i++)
// {
// printf("ntinfo of %sn",DevInfo[i].dir);
// printf("%s.fd: %dn",DevInfo[i].dir, DevInfo[i].fd);
// printf("%s.size: %dn",DevInfo[i].dir,DevInfo[i].size);
// printf("%s.erasesize: %dn",DevInfo[i].dir,DevInfo[i].erasesize);
// }
// }
for(i=0; i<MAXPARTITIONS; i++)
{
int fd;
sprintf(DevName, "%s%d", "/dev/mtd",i);
if((fd = open (DevName,O_SYNC | O_RDWR)) > 0)
{
bzero(&mtd, sizeof(struct mtd_info_user));
if (ioctl(fd, MEMGETINFO,&mtd) < 0)
{
DEBUG("ioctl(): %mn");
DEBUG("This doesn't seem to be a valid MTD flash device!n");
return 1;
}
strcpy(DevInfo[i].dir, DevName);
DevInfo[i].fd = fd;
DevInfo[i].size = mtd.size;
DevInfo[i].erasesize = mtd.erasesize;
}
else
{
DevNum = i;
break;
}
}
for(i=0; i<DevNum; i++)
{
printf("ntinfo of %sn",DevInfo[i].dir);
printf("%s.fd: %dn",DevInfo[i].dir, DevInfo[i].fd);
printf("%s.size: %dn",DevInfo[i].dir,DevInfo[i].size);
printf("%s.erasesize: %dn",DevInfo[i].dir,DevInfo[i].erasesize);
AllSize += DevInfo[i].size;
}
if(AllSize < StateOfImage.st_size)
{
DEBUG("ERROR!! all device size is less than ImageSizen");
return 1;
}
for(i=StartDev; i<DevNum; i++)
{
int j = 0;
g_AllImgSize = DevInfo[i].size;
g_AllImgWrite = 0;
erase.start = 0;
blocks = DevInfo[i].size / mtd.erasesize; //Count the number of blocks to erase
erase.length = mtd.erasesize;
printf ("nbegin to erase block %sn", DevInfo[i].dir);
for (j= 1; j <= blocks; j++)
{
fprintf(stderr, "rErasing blocks: %d/%d (%d%%)", j, blocks, (int)PERCENTAGE (j, blocks));
g_percentage = 100 * ((float)g_AllImgWrite / g_AllImgSize);
if (ioctl(DevInfo[i].fd, MEMERASE, &erase) < 0)
{
DEBUG("n");
DEBUG("While erasing blocks 0x%.8x-0x%.8x on %sn",
(unsigned int) erase.start, (unsigned int) (erase.start + erase.length), DevInfo[i].dir);
return 1;
}
g_AllImgWrite += erase.length;
erase.start += mtd.erasesize;
}
printf("nrErased blocks: %d/%d (100%%)n", blocks, blocks);
printf ("nbegin to write block %snn", DevInfo[i].dir);
g_AllImgWrite = 0;
k = BUFSIZE;
while (size)
{
if (size < BUFSIZE)
{
k = size;
}
printf("033[1A");
printf("r%s usage: %dk/%dk (%d%%)n",
DevInfo[i].dir, KB (g_AllImgWrite + k), KB (DevInfo[i].size), (int)PERCENTAGE (g_AllImgWrite + k, DevInfo[i].size));
fprintf(stderr, "Writing data: %dk/%ldk (%d%%)", KB (written + k), KB (StateOfImage.st_size), (int)PERCENTAGE (written + k, StateOfImage.st_size));
result = write(DevInfo[i].fd, &upPack[written], k);
if (k != result)
{
DEBUG ("n");
if (result < 0)
{
DEBUG("While writing data to 0x%.8x-0x%.8x on %sn", written, written + k, DevInfo[i].dir);
return 1;
}
DEBUG("Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%d bytes written to flashn",
written,written + k, DevInfo[i].dir, written + result, DevInfo[i].size);
return 1;
}
written += k;
size -= k;
g_AllImgWrite += k;
if(g_AllImgWrite >= DevInfo[i].size)
{
g_AllImgWrite = 0;
printf("n");
break;
}
}
printf("Wrote %d / %ldk bytesn", written, (unsigned long int)(StateOfImage.st_size));
}
munmap(upPack, UPGRADE_SHM_SIZE);
for(i=0; i<DevNum; i++)
{
close (DevInfo[i].fd);
printf("%s is closed!n",DevInfo[i].dir);
}
return 0;
}