int erase_partition(const char *partition, const char *filesystem) { int type = detect_partition(NULL, partition); switch (type) { case MTD: return cmd_mtd_erase_partition(partition, filesystem); case MMC: return cmd_mmc_erase_partition(partition, filesystem); case BML: return cmd_bml_erase_partition(partition, filesystem); default: return -1; } }
int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) { int type = detect_partition(NULL, partition); switch (type) { case MTD: return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only); case MMC: return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only); case BML: return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only); default: return -1; } }
int restore_raw_partition(const char* partitionType, const char *partition, const char *filename) { int type = detect_partition(partitionType, partition); switch (type) { case MTD: return cmd_mtd_restore_raw_partition(partition, filename); case MMC: return cmd_mmc_restore_raw_partition(partition, filename); case BML: return cmd_bml_restore_raw_partition(partition, filename); default: return -1; } }
int erase_raw_partition(const char* partitionType, const char *partition) { int type = detect_partition(partitionType, partition); switch (type) { case MTD: return cmd_mtd_erase_raw_partition(partition); case MMC: return cmd_mmc_erase_raw_partition(partition); case BML: return cmd_bml_erase_raw_partition(partition); default: return -1; } }
int backup_raw_partition(const char* partitionType, const char *partition, const char *filename) { int type = detect_partition(partitionType, partition); switch (type) { case MTD: return cmd_mtd_backup_raw_partition(partition, filename); case MMC: return cmd_mmc_backup_raw_partition(partition, filename); case BML: return cmd_bml_backup_raw_partition(partition, filename); default: printf("unable to detect device type"); return -1; } }
/* Function:Display the environment of redboot. */ int display_redboot_config(){ int ret,index_part,i; FILE *file_dev; UINT8 str[32]; ERASE_INFO_USER eraseInfo; ret=detect_partition(); if(ret!=0){ printf("Failed to retrive the redboot config .\n"); return ret; } //check the partion name. index_part=-1; for(i=0;i<partition_num;i++){ if(strcmp("RedBoot config",partion_table[i].partion_name)==0){ index_part=i; break; } } if(-1==index_part){ printf("Failed to retrive the redboot config .\n"); return -1; } memset(str,0x00,sizeof(str)); sprintf(str,"/dev/mtd%d",index_part); file_dev=fopen(str,"r"); if(NULL==file_dev){ perror(NULL); printf("Failed to open the device file \"%s\".\n",str); return -1; } memset(tempBuf,0x00,sizeof(tempBuf)); ret=fread(tempBuf,1,MAX_CONFIG_DATA,file_dev); if(EOF==ret){ printf("Failed to read \"%s\" device file .\n",str); return -1; } for(i=0;i<MAX_CONFIG_DATA;i++){ printf("%02x",tempBuf[i]); } fclose(file_dev); return 0; }
//Return value :0 ,success;-1 illegal parameter .-2 the other error. int main(int argc,char **argv){ int ret,i; UINT8 partition_name[32],image_name[64]; if(1==argc){ usage(argv[0]); return -1; } if(2==argc){ if(strcmp("-l",argv[1])==0){ ret=detect_partition(); if(0!=ret){ printf("Failed to detect partition infomation.\n"); return -1; } printf("----------------------------------------------------------------------\n"); printf("Part Name\t\t\tStart Addr\t\tLength\n"); printf("----------------------------------------------------------------------\n"); for(i=0;i<partition_num;i++){ printf("%-16s\t\t0x%08X\t\t0x%08X\n",partion_table[i].partion_name,partion_table[i].start_addr,partion_table[i].len); } printf("----------------------------------------------------------------------\n"); return 0; } #if 0 if(strcmp("-d",argv[1])==0){ ret=display_redboot_config(); return ret; } #endif usage(argv[0]); return -1; } if(4==argc){ if(strcmp("-w",argv[1])==0){//write image into partion. strcpy(image_name,argv[2]); strcpy(partition_name,argv[3]); ret=write_image_to_partition(image_name,partition_name); return ret; } if(strcmp("-r",argv[1])==0){ strcpy(partition_name,argv[2]); strcpy(image_name,argv[3]); ret=read_image_from_partition(image_name,partition_name); return ret; } } usage(argv[0]); return -1; #if 0 //analyse the input parameter. if(argc==1){ usage(argv[0]); } //check the input parameter. if(argc!=2){ usage(); return -1; // illegal input parameter. } strcpy(imageName,argv[1]); //update the partion ret=update(imageName); if(ret!=0){ printf("Error:failed to update %s\n",imageName); return -2; } printf("Sir ,%s is upgraded!\n",imageName); #endif return 0; }
/* Function: snatch the image from pation into a file . Return value: 0:Success. -1:Error. */ int read_image_from_partition(UINT8 *image,UINT8 *partition){ int i,j,index_part,ret,counts; INT8 option; UINT8 str[32],md5sum1[64],md5sum2[64],*ptr; FILE *dst_file,*src_file,*tst_file,*pipe_file; ERASE_INFO_USER eraseInfo; int block=BLOCK_SIZES_FOR_WRITE; long totalLen,backLen,len; ret=detect_partition(); if(0!=ret){ printf("Failed to detect partition infomation.\n"); return -1; } //check the partion name. index_part=-1; for(i=0;i<partition_num;i++){ if(strcmp(partition,partion_table[i].partion_name)==0){ index_part=i; break; } } if(-1==index_part){ printf("index_part:%d\n",index_part); printf("The partition \"%s\" is not existent.please check your input.\n",partition); return -1; } printf("Snatch the flash image .This will overwrite \"%s\" image file . Are you sure?[y/n]:",image); scanf("%c",&option); if('y'!=option){ printf("Acting only after careful consideration.:)\n"); return -1; } //add patch for special mtdblock3 of v300 if(strcmp(partition,"Usr")==0){ return snatch_usr_v300(index_part,image); } //ok, now update the partion. dst_file=fopen(image,"w"); if(NULL==dst_file){ printf("Failed to open the image file \"%s\".\n",image); return -1; } memset(str,0x00,sizeof(str)); sprintf(str,"/dev/mtd%d",index_part); src_file=fopen(str,"r+"); if(NULL==src_file){ printf("Failed to open the device file \"%s\".\n",str); return -1; } totalLen=partion_table[index_part].len; backLen=totalLen; counts=(totalLen+block-1)/block; i=0; while(totalLen>0){ if(totalLen>block){ len=block; totalLen-=block; } else{ len=totalLen; totalLen=0; } ret=fread(tempBuf,1,len,src_file); if(ret!=len){ printf("Error: fail to read data from source file \"%s\".\n",image); goto EXIT; } ret=fwrite(tempBuf,1,len,dst_file); if(ret!=len){ printf("Error:failed to write dev file \"%s\".\n",str); goto EXIT; } } SUCC: fclose(src_file); fclose(dst_file); printf("Congratulation,the \"%s\" partition has been wroten into \"%s\" image successfully.\n",partition,image); return 0; EXIT: fclose(src_file); fclose(dst_file); printf("Sorry, failed .Try again.\n"); return -1; }
/* Function: write image context into the flash partion. Return Value: 0:Success. -1:Error. */ int write_image_to_partition(UINT8 *image,UINT8 * partition){ #if 1 int i,j,index_part,ret,counts; INT8 option; UINT8 str[32],*ptr; FILE *dst_file,*src_file,*tst_file,*pipe_file; ERASE_INFO_USER eraseInfo; int block=BLOCK_SIZES_FOR_WRITE; long totalLen,backLen,len; ret=detect_partition(); if(0!=ret){ printf("Failed to detect partition infomation.\n"); return -1; } //check the partion name. index_part=-1; for(i=0;i<partition_num;i++){ if(strcmp(partition,partion_table[i].partion_name)==0){ index_part=i; break; } } if(-1==index_part){ printf("index_part:%d\n",index_part); printf("The partition \"%s\" is not existent.please check your input.\n",partition); return -1; } printf("This will overwrite \"%s\" partion .This may destroy your unit .Are you sure?[y/n]:",partition); scanf("%c",&option); if('y'!=option){ printf("Acting only after careful consideration.:)\n"); return -1; } //add patch for special mtdblock3 of v300 if(strcmp(partition,"Usr")==0){ return update_usr_v300(index_part,image); } //ok, now update the partion. src_file=fopen(image,"r"); if(NULL==src_file){ printf("Failed to open the image file \"%s\".\n",image); return -1; } memset(str,0x00,sizeof(str)); sprintf(str,"/dev/mtd%d",index_part); dst_file=fopen(str,"r+"); if(NULL==dst_file){ printf("Failed to open the device file \"%s\".\n",str); return -1; } //erase the device partion before write. eraseInfo.start=0; eraseInfo.length=partion_table[i].len; ret=ioctl(fileno(dst_file),MEMERASE,&eraseInfo); if(0!=ret){ printf("Failed to erase memory.\n"); return -1; } fseek(src_file,0,SEEK_END); totalLen=ftell(src_file); fseek(src_file,0,SEEK_SET); backLen=totalLen; counts=(totalLen+block-1)/block; i=0; while(totalLen>0){ if(totalLen>block){ len=block; totalLen-=block; } else{ len=totalLen; totalLen=0; } ret=fread(tempBuf,1,len,src_file); if(ret!=len){ printf("Error: fail to read data from source file \"%s\".\n",image); goto EXIT; } ret=fwrite(tempBuf,1,len,dst_file); if(ret!=len){ printf("Error:failed to write dev file \"%s\".\n",str); goto EXIT; } } //check the integrity of partition file. tst_file=fopen("tmp","w"); if(NULL==tst_file){ printf("Failed to create temp file for test image \"%\"",image); return -1; } totalLen=backLen; fflush(dst_file); fseek(dst_file,0,SEEK_SET); while(totalLen>0){ if(totalLen>block){ len=block; totalLen-=block; } else{ len=totalLen; totalLen=0; } //printf("%d ",totalLen); ret=fread(tempBuf,1,len,dst_file); if(ret!=len){ printf("Error: fail to read data from source file \"%s\".\n",image); goto EXIT; } ret=fwrite(tempBuf,1,len,tst_file); if(ret!=len){ printf("Error:failed to write to file tmp\n"); goto EXIT; } } if(NULL!=tst_file){ fclose(tst_file); tst_file=NULL; } //get the number of partition memset(str,0x00,sizeof(str)); sprintf(str,"md5sum %s",image); pipe_file=popen(str,"r"); if(NULL==pipe_file){ printf("Failed to retrive the partition information.\n"); return -1; } fgets(md5sum1,sizeof(md5sum1),pipe_file); pclose(pipe_file); memset(str,0x00,sizeof(str)); sprintf(str,"md5sum tmp"); pipe_file=popen(str,"r"); if(NULL==pipe_file){ printf("Failed to retrive the partition information.\n"); return -1; } fgets(md5sum2,sizeof(md5sum2),pipe_file); pclose(pipe_file); ptr=index(md5sum1,' '); i=ptr-md5sum1; md5sum1[i]=0x00; md5sum2[i]=0x00; //printf("%s \n%s\n",md5sum1,md5sum2); if(strcmp(md5sum1,md5sum2)!=0){ printf("The context in \"%s\" partition is not same with \"%s\".Please try again.\n",partition,image); return -1; } SUCC: fclose(src_file); fclose(dst_file); if(NULL!=tst_file){ fclose(tst_file); tst_file=NULL; } printf("Congratulation,the \"%s\" image has been wroten into \"%s\" partition successfully.\n",image,partition); return 0; EXIT: fclose(src_file); fclose(dst_file); if(NULL!=tst_file){ fclose(tst_file); tst_file=NULL; } #endif return -1; }