#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#define SIZE_HEADER 128*1024
bool isHiChip=1;
int g_blocksize=0;
typedef struct BLOCK
{
char dev[20];
long long size;
long int erasesize;
char name[20];
}BLOCK;
bool isNand=false;
bool isN5064=false;
FILE *fb_txt=NULL;
void WriteFlash(long long FlashSize,int num);
void Copy_BlockFile(char *SourcePath,long ReadSize)
{
long long size=0;
long readlen=0;
long writelen=0;
long long writesize=0;
char Buffer[1024*1024]={};
long long pos1=0,pos2=0;
long long Realsize=ReadSize;
if(isN5064)
Realsize=1024LL*1024*ReadSize;
FILE *fb_block=fopen(SourcePath,"rb");
if(fb_block == NULL)
{
perror("fopen block:");
return;
}
while(1)
{
bzero(Buffer,sizeof(Buffer));
pos1=ftell(fb_block);
if((readlen=fread(Buffer,1,sizeof(Buffer),fb_block))<sizeof(Buffer))
{
pos2=ftell(fb_block);
if(feof(fb_block))
{
writelen=fwrite(Buffer,1,pos2-pos1,fb_txt);
fflush(fb_txt);
writesize+=writelen;
size+=(pos2-pos1);
break;
}
else if (ferror(fb_block))
{
perror("fread");
break;
}
}
size+=readlen;
writelen=fwrite(Buffer,1,sizeof(Buffer),fb_txt);
fflush(fb_txt);
if(writelen<1048576)
{
fflush(fb_txt);
writelen=fwrite(Buffer,1,sizeof(Buffer),fb_txt);
}
writesize+=writelen;
}
printf("\033[0;31m =====zyj Realsize=%lld,readsize=%lld,writesize=%lld\033[0;39m \n",Realsize,size,writesize);
if(Realsize>size)
{
if(isN5064==true)
WriteFlash(Realsize-size,0x00);
else
WriteFlash(Realsize-size,0xff);
}
fclose(fb_block);
}
void WriteFlash(long long FlashSize,int num)
{
char Buffer[1024*1024]={};
int i=0,writesize=0;
int a = 0;
int times=FlashSize/sizeof(Buffer);
for(i=0;i<times;i++)
{
memset(Buffer,num,sizeof(Buffer));
a = fwrite(Buffer,sizeof(num),sizeof(Buffer)/sizeof(num),fb_txt);
fflush(fb_txt);
writesize += a;
}
memset(Buffer,num,sizeof(Buffer));
if(FlashSize>writesize)
fwrite(Buffer,sizeof(num),FlashSize-writesize,fb_txt);
fflush(fb_txt);
}
void Read_Mtdsize(BLOCK *mtd_block)
{
char *ptr=NULL;
char ptr_size[20]={};
char ptr_erasize[20]={};
char *arr_ptr[20]={};
char * token=NULL;
char readbuf[200]={};
if(isHiChip==false)
{
FILE *fb_mtd=fopen("/proc/mtd","r");
if(fb_mtd==NULL)
{
perror("fopen mtd:");
return ;
}
fgets(readbuf,sizeof(readbuf),fb_mtd);
while(1)
{
if(fgets(readbuf,sizeof(readbuf),fb_mtd)==NULL)
{
break;
}
sscanf(readbuf,"%s %s %s %s",mtd_block[g_blocksize].dev,ptr_size,ptr_erasize,mtd_block[g_blocksize].name);
mtd_block[g_blocksize].size = strtol(ptr_size,&ptr,16);
mtd_block[g_blocksize].erasesize = strtol(ptr_erasize,&ptr,16);
g_blocksize++;
}
fclose(fb_mtd);
}
else if(isHiChip==true)
{
FILE *fb_cmdline=fopen("/proc/cmdline","r");
if(fb_cmdline==NULL)
{
perror("fopen mtd:");
return ;
}
bzero(readbuf,sizeof(readbuf));
fread(readbuf,1,sizeof(readbuf),fb_cmdline);
ptr = strstr(readbuf,"mtdparts=");
if(strstr(ptr,"hinand")!=NULL)
isNand=true;
else
isNand=false;
if(ptr)
{
token = strtok(ptr,":,");
}
while(token!=NULL)
{
token = strtok(NULL,":,");
if(token!=NULL){
arr_ptr[g_blocksize]=token;
g_blocksize++;
if(strstr(token,"p2p"))
break;
}
}
int i=0;
for(i=0;i<g_blocksize;i++)
{
token = strtok(arr_ptr[i],"()");
if(strstr(token,"M")!=NULL)
{
mtd_block[i].size =1024*1024*strtol(token,&ptr,10);
}
else if(strstr(token,"K")!=NULL)
{
mtd_block[i].size =1024*strtol(token,&ptr,10);
}
token = strtok(NULL,"()");
strcpy(mtd_block[i].name,token);
if(isNand)
sprintf(mtd_block[i].dev,"/dev/mtd%d",i);
else if(!isNand)
sprintf(mtd_block[i].dev,"/dev/mtdblock%d",i);
}
fclose(fb_cmdline);
}
}
void Get_CPlatform()
{
char *ptr=NULL;
char readbuf[200]={};
FILE *fb_cmdline=fopen("/proc/cmdline","r");
if(fb_cmdline==NULL)
{
perror("fopen mtd:");
return ;
}
bzero(readbuf,sizeof(readbuf));
if(fread(readbuf,1,sizeof(readbuf),fb_cmdline)==0)
return;
if(strstr(readbuf,"hinand")||strstr(readbuf,"hi_sfc"))
isHiChip=true;
else
isHiChip=false;
printf("\033[0;31m =====zyj isHiChip=%d\033[0;39m \n",isHiChip);
fclose(fb_cmdline);
return ;
}
void Read_Mmcblksize(BLOCK *mtd_block)
{
char readbuf[500]={};
char *ptr=NULL;
char *arr_ptr[25]={};
char * token=NULL;
FILE *fb_cmdline=fopen("/proc/cmdline","r");
if(fb_cmdline==NULL)
{
perror("fopen mtd:");
return ;
}
bzero(readbuf,sizeof(readbuf));
fread(readbuf,1,sizeof(readbuf),fb_cmdline);
ptr = strstr(readbuf,"mmcblk0:");
token = strtok(ptr,":,");
while(token!=NULL)
{
token = strtok(NULL,":,");
if(strstr(token,"vmalloc"))
break;
arr_ptr[g_blocksize]=token;
g_blocksize++;
}
int i=0;
for(i=0;i<g_blocksize;i++)
{
token = strtok(arr_ptr[i],"()");
if(strstr(token,"M")!=NULL)
{
mtd_block[i].size =strtol(token,&ptr,10);
}
else if(strstr(token,"K")!=NULL)
{
mtd_block[i].size =strtol(token,&ptr,10)/1024;
}
sprintf(mtd_block[i].dev,"/dev/mmcblk0p%d",i+1);
}
}
void Get_boardid(char boardid[])
{
char buf[128]={};
char *ptr=NULL;
FILE *fb=popen("readkeyboardid","r");
if(fb == NULL)
{
perror("popen");
return;
}
fread(buf,1,sizeof(buf),fb);
ptr=strstr(buf,"boardid =");
sscanf(ptr,"boardid = %s",boardid);
printf("\033[0;31m =====zyj BoardId=%s\033[0;39m \n",boardid);
pclose(fb);
return ;
}
int main(int argc,char**argv)
{
clock_t a,b;
a=time(NULL);
int i=0;
char mtdpath[50]={};
char boardid[20]={};
BLOCK mtd_block[23]={};
fb_txt= fopen("./ReadBlock","wb+");
if(fb_txt == NULL)
{
perror("fopen txt:");
return -1;
}
Get_boardid(boardid);
if(strcmp(boardid,"N5064PN-EMMC")==0)
{
isN5064=true;
long long blocksize=0;
Read_Mmcblksize(mtd_block);
for(i=0;i<g_blocksize;i++)
{
Copy_BlockFile(mtd_block[i].dev,mtd_block[i].size);
blocksize+=1024*1024*mtd_block[i].size;
}
WriteFlash(8LL*1024*1024*1024-blocksize,0x00);
}
else
{
Get_CPlatform();
if(isHiChip==false)
{
if(strcmp(boardid,"D7108NR")==0||strcmp(boardid,"D8116NR")==0){
Copy_BlockFile("/tmp/usb/header.bin",SIZE_HEADER);
}
Read_Mtdsize(mtd_block);
for(i=0;i<g_blocksize-1;i++)
{
sprintf(mtdpath,"/dev/mtd%d",i);
Copy_BlockFile(mtdpath,mtd_block[i].size);
}
}
else if(isHiChip==true)
{
memset(mtdpath,0,sizeof(mtdpath));
Read_Mtdsize(mtd_block);
for(i=0;i<g_blocksize;i++)
{
Copy_BlockFile(mtd_block[i].dev,mtd_block[i].size);
}
}
}
fclose(fb_txt);
b=time(NULL);
printf("\033[0;31m =====*zyj end time=%f \033[0;39m \n",(float)(b-a));
return 0;
}