lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | |
| 2 | #include <unistd.h> |
| 3 | #include <errno.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <dirent.h> |
| 7 | #include <string.h> |
| 8 | #include <sys/file.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/ipc.h> |
| 12 | #include <sys/msg.h> |
| 13 | #include "nvserver.h" |
| 14 | #include "nv_typedef.h" |
| 15 | #include <message.h> |
| 16 | #ifdef FOTA_AB |
| 17 | #include "zxic_fota_ab_upgrade.h" |
| 18 | #endif |
| 19 | #ifdef __cplusplus |
| 20 | extern"C"{ |
| 21 | #endif |
| 22 | static void nvConfig();static void nvInit();static int nvDirInit();static void |
| 23 | analyMsg(T_NV_MSG_INFO*msgrecv,T_NV_MSG_RESULT*msgsnd);static bool checkNvFs( |
| 24 | char*file);static bool isCfgConfiged(char*configFile);static bool isNvConfiged( |
| 25 | char*configFile);static uint getSum(const char*s,int len);static int |
| 26 | loadFactroyParam(T_NV_NODE*list);static int restoreNvFs(char*dstFile,char* |
| 27 | srcFile);static int loadNvFs(char*file);static int addConfigFile(char*nvFile, |
| 28 | char*configFile);static int saveNvFs(char*nvName,char*nvFile);static int nvset( |
| 29 | char*file,const char*key,const char*value,int saveFlag);static int nvget(char* |
| 30 | file,char*key,char*value);static int nvunset(char*file,char*key);static int |
| 31 | nvclear(char*file);static int nvreset(char*file);static int nvcommit(char*file); |
| 32 | T_NV_NODE*nv_list;int nvserver_main(int argc,char*argv[]){int msgId= |
| 33 | (0xaea+4144-0x1b1a);T_NV_MSG_INFO rcvBuf;T_NV_MSG_RESULT sndBuf;struct msqid_ds |
| 34 | msgInfo;prctl(PR_SET_NAME,"\x6e\x76\x73\x65\x72\x76\x65\x72", |
| 35 | (0x1611+1505-0x1bf2),(0x1c14+191-0x1cd3),(0x153+9135-0x2502));memset(&rcvBuf, |
| 36 | (0x1f8+7402-0x1ee2),sizeof(rcvBuf));memset(&sndBuf,(0x96a+6783-0x23e9),sizeof( |
| 37 | sndBuf));memset(&msgInfo,(0x27+2084-0x84b),sizeof(msgInfo));msgId=msgget( |
| 38 | MODULE_ID_NV,IPC_CREAT|(0x1298+4145-0x2149));if(-(0x1c87+666-0x1f20)==msgId){ |
| 39 | printf( |
| 40 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6d\x73\x67\x67\x65\x74\x20\x6d\x73\x67\x49\x64\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 41 | ,errno);return-(0x84a+2941-0x13c6);}if(-(0x135+7698-0x1f46)!=msgctl(msgId, |
| 42 | IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0x18fd+2884-0x2440)==msgctl( |
| 43 | msgId,IPC_SET,&msgInfo))printf( |
| 44 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6d\x73\x67\x63\x74\x6c\x20\x6d\x73\x67\x49\x64\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 45 | ,errno);}if(nvDirInit()!=(0x209f+1040-0x24af)){printf( |
| 46 | "\x6e\x76\x44\x69\x72\x49\x6e\x69\x74\x20\x66\x61\x69\x6c\x65\x21" "\n");return- |
| 47 | (0x5e2+1420-0xb6d);}nvConfig();nvInit();while((0xa10+526-0xc1d)){if(- |
| 48 | (0x17ea+2632-0x2231)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| 49 | MSG_TYPE_NV,(0x1790+1510-0x1d76))){printf( |
| 50 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x6d\x73\x67\x72\x63\x76\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| 51 | ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0x481+1369-0x9d9)==msgsnd(msgId |
| 52 | ,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0xa0d+5724-0x2069))){printf( |
| 53 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x6d\x73\x67\x73\x6e\x64\x20\x66\x61\x69\x6c\x2c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| 54 | ,errno);continue;}}return((0x12e7+4257-0x2388));}static void configdir(char*dir) |
| 55 | {DIR*dp;int ret;struct dirent*entry;struct stat statbuf;if((dp=opendir(dir))== |
| 56 | NULL){fprintf(stderr, |
| 57 | "\x63\x61\x6e\x6e\x6f\x74\x20\x6f\x70\x65\x6e\x20\x64\x69\x72\x65\x63\x74\x6f\x72\x79\x3a\x20\x25\x73" "\n" |
| 58 | ,dir);return;}chdir(dir);while((entry=readdir(dp))!=NULL){ret=lstat(entry-> |
| 59 | d_name,&statbuf);if(ret<(0x1c2+3334-0xec8)){fprintf(stderr, |
| 60 | "\x6c\x73\x74\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x25\x73" "\n",strerror( |
| 61 | errno));chdir("\x2e\x2e");closedir(dp);return;}if(!S_ISDIR(statbuf.st_mode)){if( |
| 62 | strcmp("\x2e",entry->d_name)==(0x1d74+1056-0x2194)||strcmp("\x2e\x2e",entry-> |
| 63 | d_name)==(0xd8a+532-0xf9e))continue;if(!isNvConfiged(entry->d_name)){if( |
| 64 | addConfigFile(entry->d_name,NULL)!=RESULT_SUCCESS)printf( |
| 65 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x63\x6f\x6e\x66\x69\x67\x20\x25\x73\x20\x65\x72\x72\x6f\x72\x21" "\n" |
| 66 | ,entry->d_name);}}}chdir("\x2e\x2e");closedir(dp);}static void nvConfig(){char* |
| 67 | val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x70c+4185-0x1765)};fp=fopen |
| 68 | (NV_CONFIG_FILE,"\x72\x6f");if(!fp){printf( |
| 69 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| 70 | ,NV_CONFIG_FILE,errno);return;}while(fgets(buf,NV_MAX_CONFIG_LEN,fp)){if(buf[ |
| 71 | (0x894+93-0x8f1)]=='\n'||buf[(0x18ab+891-0x1c26)]==((char)(0x312+4389-0x1414))) |
| 72 | continue;val=strchr(buf,((char)(0x5e0+3374-0x12d1)));if(!val){printf( |
| 73 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x6f\x72\x6d\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x73\x74\x72\x20\x3d\x20\x25\x73\x21" "\n" |
| 74 | ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0xcec+6363-0x25c6)]='\0';*val++= |
| 75 | '\0';if(!isCfgConfiged(buf)){if(addConfigFile(val,buf)!=RESULT_SUCCESS)printf( |
| 76 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x63\x6f\x6e\x66\x69\x67\x20\x25\x73\x20\x65\x72\x72\x6f\x72\x21" "\n" |
| 77 | ,buf);}}fclose(fp);configdir(NV_FS_MAIN_PATH);}static int nvDirInit(){if(access( |
| 78 | NV_FS_PATH,F_OK)!=(0x2fd+1808-0xa0d)){if(mkdir(NV_FS_PATH,(0x2df+3050-0xcdc))!= |
| 79 | (0x7a7+3590-0x15ad)){printf( |
| 80 | "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| 81 | ,NV_FS_PATH,errno);return-(0x532+691-0x7e4);}if(mkdir(NV_FS_MAIN_PATH, |
| 82 | (0x459+3006-0xe2a))!=(0x217+5370-0x1711)){printf( |
| 83 | "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| 84 | ,NV_FS_MAIN_PATH,errno);return-(0x1463+3947-0x23cd);}if(mkdir(NV_FS_BACKUP_PATH, |
| 85 | (0x4a5+5946-0x19f2))!=(0x12f2+2574-0x1d00)){printf( |
| 86 | "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| 87 | ,NV_FS_BACKUP_PATH,errno);return-(0x223+1253-0x707);}}else{if(access( |
| 88 | NV_FS_MAIN_PATH,F_OK)!=(0xa5f+5243-0x1eda)){if(mkdir(NV_FS_MAIN_PATH, |
| 89 | (0x259+217-0x145))!=(0xeea+4988-0x2266)){printf( |
| 90 | "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| 91 | ,NV_FS_MAIN_PATH,errno);return-(0x1567+686-0x1814);}}if(access(NV_FS_BACKUP_PATH |
| 92 | ,F_OK)!=(0xcb3+249-0xdac)){if(mkdir(NV_FS_BACKUP_PATH,(0x1825+114-0x16aa))!= |
| 93 | (0x1574+4254-0x2612)){printf( |
| 94 | "\x6e\x65\x72\x76\x65\x72\x20\x6d\x6b\x64\x69\x72\x20\x25\x73\x20\x66\x61\x6c\x69\x2c\x65\x72\x72\x6e\x6f\x3d\x25\x64" "\n" |
| 95 | ,NV_FS_BACKUP_PATH,errno);return-(0x1a5a+656-0x1ce9);}}}return(0x29+4739-0x12ac) |
| 96 | ;}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[NV_PATH_LEN]={ |
| 97 | (0x1a5+93-0x202)};char nvBackupFile[NV_PATH_LEN]={(0x1139+761-0x1432)};for(list= |
| 98 | nv_list;list;list=list->next){snprintf(nvMainFile,NV_PATH_LEN, |
| 99 | "\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,list->nvFile);snprintf(nvBackupFile, |
| 100 | NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,list->nvFile);if(checkNvFs( |
| 101 | nvMainFile)){if(!checkNvFs(nvBackupFile))restoreNvFs(nvBackupFile,nvMainFile);} |
| 102 | else if(checkNvFs(nvBackupFile)){restoreNvFs(nvMainFile,nvBackupFile);}else{ |
| 103 | loadFactroyParam(list);nvcommit(list->nvFile);continue;}loadNvFs(list->nvFile); |
| 104 | if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0x1124+2190-0x19b0)){ |
| 105 | reloadFactroyParam(list);delete_not_needed(list);nvcommit(list->nvFile); |
| 106 | #ifdef FOTA_AB |
| 107 | dual_AB_set_fota_status_for_nv((0x16fc+1753-0x1dd5)); |
| 108 | #endif |
| 109 | }}}uint hash(const char*s){uint hash=(0x749+6614-0x211f);while(*s){hash= |
| 110 | NV_HASH_MUL*hash+*s++;}return hash;}static int loadFactroyParam(T_NV_NODE*list){ |
| 111 | char*val=NULL;FILE*fp=NULL;T_NV_CONFIG*config=NULL;char buf[NV_MAX_ITEM_LEN]={ |
| 112 | (0x7a6+3997-0x1743)};for(config=list->fileList;config;config=config->next){fp= |
| 113 | fopen(config->configFile,"\x72\x6f");if(!fp){printf( |
| 114 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64\x21" "\n" |
| 115 | ,config->configFile,errno);return RESULT_FILE_OPEN_FAIL;}while(fgets(buf, |
| 116 | NV_MAX_ITEM_LEN,fp)){if(buf[(0x1ff+8264-0x2247)]=='\n'||buf[(0xbf7+2145-0x1458)] |
| 117 | ==((char)(0x1715+230-0x17d8)))continue;val=strchr(buf, |
| 118 | ((char)(0x1936+837-0x1c3e)));if(!val){printf( |
| 119 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x25\x73\x20\x66\x69\x6c\x65\x20\x66\x6f\x72\x6d\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x73\x74\x72\x69\x6e\x67\x20\x3d\x20\x25\x73" "\n" |
| 120 | ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x1338+3956-0x22ab)]== |
| 121 | '\n')buf[strlen(buf)-(0x484+6087-0x1c4a)]='\0';*val++='\0';nvset(list->nvFile, |
| 122 | buf,val,(0x2b2+687-0x560));}printf( |
| 123 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x6c\x6f\x61\x64\x46\x61\x63\x74\x72\x6f\x79\x50\x61\x72\x61\x6d\x20\x25\x73\x21" "\n" |
| 124 | ,config->configFile);fclose(fp);}return RESULT_SUCCESS;}static bool checkNvFs( |
| 125 | char*file){int len=(0xec0+1923-0x1643);int cnt=(0x5a3+4403-0x16d6);FILE*fp=NULL; |
| 126 | char*buf=NULL;struct stat statbuff={(0x113d+2028-0x1929)};if(stat(file,&statbuff |
| 127 | )<(0x410+8528-0x2560))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE) |
| 128 | return false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len |
| 129 | );if(!buf){fclose(fp);return false;}cnt=(0x99b+51-0x9ce);while(cnt<len){cnt=cnt+ |
| 130 | fread(buf+cnt,(0x51c+7608-0x22d3),len-cnt,fp);if(ferror(fp)){clearerr(fp);free( |
| 131 | buf);fclose(fp);return false;}}if(len!=cnt){free(buf);fclose(fp);return false;} |
| 132 | if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*)(buf+len-NV_CHECK_SIZE)){ |
| 133 | free(buf);fclose(fp);return false;}free(buf);fclose(fp);return true;}static int |
| 134 | copyfile(const char*from,const char*to){int fd_to;int fd_from;char buf[ |
| 135 | (0x13d7+8497-0x2508)];ssize_t nread;int ret=-(0x16f0+2761-0x21b8);fd_from=open( |
| 136 | from,O_RDONLY);if(fd_from<(0x2db+799-0x5fa))return-(0x253c+264-0x2642);fd_to= |
| 137 | open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC,(0x500+6714-0x1d9a));if(fd_to< |
| 138 | (0x37d+6679-0x1d94)){ret=-(0x24a+2472-0xbef);goto out_error;}while( |
| 139 | (0x13a6+1524-0x1999)){char*out_ptr;ssize_t nwritten;nread=read(fd_from,buf, |
| 140 | sizeof(buf));if(nread==(0x37d+9025-0x26be)){break;}else{if(nread< |
| 141 | (0x360+5930-0x1a8a)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| 142 | (0x905+2791-0x13e8);goto out_error;}}}out_ptr=buf;do{nwritten=write(fd_to, |
| 143 | out_ptr,nread);if(nwritten>(0x5c9+1236-0xa9d)){nread-=nwritten;out_ptr+=nwritten |
| 144 | ;}else{if(nwritten<(0xc04+2808-0x16fc)){if(errno==EINTR||errno==EAGAIN){continue |
| 145 | ;}else{ret=-(0x2cd+6917-0x1dcd);goto out_error;}}}}while(nread> |
| 146 | (0x25a+7315-0x1eed));}ret=fsync(fd_to);if(ret<(0x11d3+1192-0x167b)){printf( |
| 147 | "\x53\x79\x6e\x63\x20\x46\x61\x69\x6c\x65\x64\x3a\x25\x73\x2c\x20\x66\x69\x6c\x65\x20\x70\x61\x74\x68\x3a\x25\x73" |
| 148 | ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0xf1c+2978-0x1abe)){fd_to= |
| 149 | -(0x505+5923-0x1c27);ret=-(0xbd5+787-0xee2);goto out_error;}close(fd_from); |
| 150 | return(0x1946+229-0x1a2b);out_error:printf( |
| 151 | "\x63\x6f\x70\x79\x66\x69\x6c\x65\x20\x25\x73\x20\x74\x6f\x20\x25\x73\x20\x65\x72\x72\x6f\x72\x3a\x25\x64" "\n" |
| 152 | ,from,to,ret);close(fd_from);if(fd_to>=(0x394+464-0x564))close(fd_to);return ret |
| 153 | ;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile,dstFile) |
| 154 | !=(0x13d8+4786-0x268a))return RESULT_FAIL;return RESULT_SUCCESS;}static int |
| 155 | loadNvFs(char*file){int len=(0xf5b+4836-0x223f);int cnt=(0x5d9+2332-0xef5);FILE* |
| 156 | fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct stat |
| 157 | statbuff={(0x509+5632-0x1b09)};char nvFile[NV_PATH_LEN]={(0x287+7854-0x2135)}; |
| 158 | sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);if(stat(nvFile,& |
| 159 | statbuff)<(0x9e4+2048-0x11e4))return RESULT_FAIL;len=statbuff.st_size;if( |
| 160 | NV_CHECK_SIZE>len)return RESULT_FAIL;fp=fopen(nvFile,"\x72\x6f");if(!fp)return |
| 161 | RESULT_FILE_OPEN_FAIL;len=len-NV_CHECK_SIZE;buf=(char*)malloc(len+ |
| 162 | (0xf9f+833-0x12df));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| 163 | (0x1184+1122-0x15e6),len+(0x1a68+1095-0x1eae));cnt=(0x5b7+1247-0xa96);while(cnt< |
| 164 | len){cnt=cnt+fread(buf+cnt,(0x52c+2229-0xde0),len-cnt,fp);if(ferror(fp)){ |
| 165 | clearerr(fp);fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}}if(cnt!=len){ |
| 166 | fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}buf[len]='\0';name=buf;while( |
| 167 | *name){if(!(eq=strchr(name,((char)(0x11ea+1499-0x1788))))){break;}*eq='\0';value |
| 168 | =eq+(0x1099+5334-0x256e);nvset(file,name,value,(0x1889+956-0x1c44));*eq= |
| 169 | ((char)(0x257a+412-0x26d9));name=value+strlen(value)+(0x13c9+1473-0x1989);}free( |
| 170 | buf);fclose(fp);return RESULT_SUCCESS;}static void analyMsg(T_NV_MSG_INFO* |
| 171 | msgrecv,T_NV_MSG_RESULT*msgsnd){switch(msgrecv->nvType){case MSG_GET:msgsnd-> |
| 172 | result=nvget(msgrecv->file,msgrecv->key,msgsnd->value);break;case MSG_SET:msgsnd |
| 173 | ->result=nvset(msgrecv->file,msgrecv->key,msgrecv->value,msgrecv->saveflag); |
| 174 | break;case MSG_UNSET:msgsnd->result=nvunset(msgrecv->file,msgrecv->key);break; |
| 175 | case MSG_CLEAR:msgsnd->result=nvclear(msgrecv->file);break;case MSG_RESET:msgsnd |
| 176 | ->result=nvreset(msgrecv->file);break;case MSG_SHOW:msgsnd->result=saveNvFs( |
| 177 | msgrecv->file,NV_FS_SHOW);;break;case MSG_COMMIT:msgsnd->result=nvcommit(msgrecv |
| 178 | ->file);break;default:break;}msgsnd->msgType=msgrecv->pid;msgsnd->msgIndex= |
| 179 | msgrecv->msgIndex;}static int addConfigFile(char*nvFile,char*configFile){ |
| 180 | T_NV_NODE*list=NULL;T_NV_NODE*newList=NULL;T_NV_CONFIG*newConfig=NULL;if(!nvFile |
| 181 | ){printf( |
| 182 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x70\x61\x72\x61\x6d\x20\x69\x6c\x6c\x65\x67\x61\x6c\x21" "\n" |
| 183 | );return RESULT_INVAL;}if(configFile){newConfig=(T_NV_CONFIG*)malloc(sizeof( |
| 184 | T_NV_CONFIG));if(!newConfig)return RESULT_MALLOC_FAIL;strncpy(newConfig-> |
| 185 | configFile,configFile,NV_PATH_LEN-(0x3a5+8399-0x2473));newConfig->configFile[ |
| 186 | NV_PATH_LEN-(0x9a6+2041-0x119e)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| 187 | list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0xf0+778-0x3fa))break;}if |
| 188 | (!list){newList=(T_NV_NODE*)malloc(sizeof(T_NV_NODE));if(!newList){if(newConfig) |
| 189 | free(newConfig);return RESULT_MALLOC_FAIL;}newList->next=NULL;strncpy(newList-> |
| 190 | nvFile,nvFile,NV_PATH_LEN-(0x21fc+960-0x25bb));newList->nvFile[NV_PATH_LEN- |
| 191 | (0xc26+1512-0x120d)]='\0';memset(newList->nvTable,(0xaf8+2366-0x1436), |
| 192 | NV_HASH_LEN*(0x404+3503-0x11af));newList->fileList=newConfig;if(!nv_list)nv_list |
| 193 | =newList;else{newList->next=nv_list->next;nv_list->next=newList;}}else if(!list |
| 194 | ->fileList)list->fileList=newConfig;else{if(newConfig==NULL)return RESULT_FAIL; |
| 195 | newConfig->next=list->fileList->next;list->fileList->next=newConfig;}return |
| 196 | RESULT_SUCCESS;}static bool isCfgConfiged(char*configFile){T_NV_NODE*list=NULL; |
| 197 | T_NV_CONFIG*config=NULL;for(list=nv_list;list;list=list->next){for(config=list-> |
| 198 | fileList;config;config=config->next){if(!strcmp(config->configFile,configFile)) |
| 199 | return true;}}return false;}static bool isNvConfiged(char*nvFile){T_NV_NODE*list |
| 200 | =NULL;for(list=nv_list;list;list=list->next){if(!strcmp(list->nvFile,nvFile)) |
| 201 | return true;}return false;}static uint getSum(const char*s,int len){uint sum= |
| 202 | (0x1469+1721-0x1b22);char*data=(char*)s;while(len-- >(0xd0c+5271-0x21a3)){sum+=( |
| 203 | *data++);}return sum;}static int saveNvFs(char*nvName,char*nvFile){int i= |
| 204 | (0xae6+4407-0x1c1d);int sum=(0x6a7+4794-0x1961);int bufSize=(0xd4a+2813-0x1847); |
| 205 | int itemSize=(0xe7c+205-0xf49);int ret=(0x8fa+2426-0x1274);int fp= |
| 206 | (0x246+334-0x394);char*buf=NULL;T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list |
| 207 | =nv_list;list;list=list->next){if(strcmp(list->nvFile,nvName))continue;fp=open( |
| 208 | nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC,(0x270+6353-0x19a1));if(fp==- |
| 209 | (0x11ed+4255-0x228b)){printf( |
| 210 | "\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x61\x69\x6c\x2c\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 211 | ,nvFile,errno);return RESULT_FILE_OPEN_FAIL;}buf=(char*)malloc(NV_BLOCK_SIZE);if |
| 212 | (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0x3c2+5458-0x1914);i< |
| 213 | NV_HASH_LEN;i++){for(item=list->nvTable[i];item;item=item->next){if(strcmp( |
| 214 | nvFile,NV_FS_SHOW)&&!item->saveFlag)continue;itemSize=strlen(item->key)+strlen( |
| 215 | item->value)+(0x1a95+1451-0x203e);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp |
| 216 | ,buf,bufSize)<(0x100c+2506-0x19d6)){printf( |
| 217 | "\x65\x72\x72\x6f\x72\x20\x25\x73\x20\x25\x64\x3a\x20\x77\x72\x69\x74\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 218 | ,__FILE__,__LINE__,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum |
| 219 | +=getSum(buf,bufSize);bufSize=(0xeb0+2591-0x18cf);}sprintf(buf+bufSize, |
| 220 | "\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!= |
| 221 | (0x1e77+809-0x21a0)){if(write(fp,buf,bufSize)<(0xfc7+1731-0x168a)){printf( |
| 222 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x77\x72\x69\x74\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 223 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum+=getSum(buf, |
| 224 | bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x82f+452-0x9f3)){ |
| 225 | printf( |
| 226 | "\x6e\x76\x73\x65\x72\x76\x65\x72\x20\x65\x72\x72\x6f\x72\x3a\x20\x77\x72\x69\x74\x65\x20\x66\x61\x69\x6c\x20\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 227 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}ret=fsync(fp);free( |
| 228 | buf);close(fp);if(ret<(0xf7a+1288-0x1482)){printf( |
| 229 | "\x53\x79\x6e\x63\x20\x46\x61\x69\x6c\x65\x64\x3a\x25\x73\x2c\x20\x66\x69\x6c\x65\x20\x70\x61\x74\x68\x3a\x25\x73" |
| 230 | ,strerror(errno),nvFile);return ret;}return RESULT_SUCCESS;}return |
| 231 | RESULT_NO_FILE;}static int nvget(char*file,char*key,char*value){int index= |
| 232 | (0xb29+4408-0x1c61);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list; |
| 233 | list;list=list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)% |
| 234 | NV_HASH_LEN;for(item=list->nvTable[index];item;item=item->next){if(strcmp(item-> |
| 235 | key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0xe37+2952-0x19be)); |
| 236 | value[NV_MAX_VAL_LEN-(0xac0+4480-0x1c3f)]='\0';return RESULT_SUCCESS;}}return |
| 237 | RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int |
| 238 | saveFlag){int index=(0x17cb+641-0x1a4c);int ret=(0x131c+320-0x145c);int |
| 239 | key_buf_len=(0x1afa+2437-0x247f);int value_buf_len=(0x162d+2323-0x1f40); |
| 240 | T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key|| |
| 241 | NULL==value)return RESULT_FAIL;key_buf_len=strlen(key)+(0xe3+879-0x451); |
| 242 | value_buf_len=strlen(value)+(0xa48+4827-0x1d22);for(list=nv_list;list;list=list |
| 243 | ->next){if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for( |
| 244 | item=list->nvTable[index];item;item=item->next){if(strcmp(item->key,key)) |
| 245 | continue;if(saveFlag)item->saveFlag=saveFlag;if(!strcmp(item->value,value)) |
| 246 | return RESULT_SUCCESS;free(item->value);item->value=(char*)malloc(value_buf_len) |
| 247 | ;if(!item->value)return RESULT_MALLOC_FAIL;strncpy(item->value,value, |
| 248 | value_buf_len-(0x114f+4319-0x222d));item->value[value_buf_len- |
| 249 | (0x6d9+5401-0x1bf1)]='\0';return RESULT_SUCCESS;}newItem=(T_NV_ITEM*)malloc( |
| 250 | sizeof(T_NV_ITEM));if(!newItem)return RESULT_MALLOC_FAIL;newItem->key=(char*) |
| 251 | malloc(key_buf_len);if(!newItem->key){free(newItem);return RESULT_MALLOC_FAIL;} |
| 252 | newItem->value=(char*)malloc(value_buf_len);if(!newItem->value){free(newItem-> |
| 253 | key);free(newItem);return RESULT_MALLOC_FAIL;}strncpy(newItem->key,key, |
| 254 | key_buf_len-(0x418+7161-0x2010));newItem->key[key_buf_len-(0x1218+5049-0x25d0)]= |
| 255 | '\0';strncpy(newItem->value,value,value_buf_len-(0xa36+512-0xc35));newItem-> |
| 256 | value[value_buf_len-(0x711+2882-0x1252)]='\0';newItem->next=NULL;newItem-> |
| 257 | saveFlag=saveFlag;newItem->update_flag=(0x1320+3434-0x208a);if(!list->nvTable[ |
| 258 | index])list->nvTable[index]=newItem;else{newItem->next=list->nvTable[index]-> |
| 259 | next;list->nvTable[index]->next=newItem;}return RESULT_SUCCESS;}ret= |
| 260 | addConfigFile(file,NULL);if(ret==RESULT_SUCCESS)return nvset(file,key,value, |
| 261 | saveFlag);else return ret;}static int nvunset(char*file,char*key){int index= |
| 262 | (0x2f2+6797-0x1d7f);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*prev=NULL; |
| 263 | for(list=nv_list;list;list=list->next){if(strcmp(list->nvFile,file))continue; |
| 264 | index=hash(key)%NV_HASH_LEN;for(item=list->nvTable[index];item;prev=item,item= |
| 265 | item->next){if(strcmp(item->key,key))continue;if(!prev)list->nvTable[index]=item |
| 266 | ->next;else prev->next=item->next;free(item->key);free(item->value);free(item); |
| 267 | return RESULT_SUCCESS;}}return RESULT_NO_ITEM;}static int nvreset(char*file){int |
| 268 | ret=(0x120a+3246-0x1eb8);T_NV_NODE*list=NULL;for(list=nv_list;list;list=list-> |
| 269 | next){if(strcmp(list->nvFile,file))continue;ret=nvclear(file);if(ret!= |
| 270 | RESULT_SUCCESS)return ret;if(loadFactroyParam(list)!=RESULT_SUCCESS)return |
| 271 | RESULT_FAIL;return nvcommit(file);}return RESULT_NO_FILE;}static int nvclear( |
| 272 | char*file){int i=(0x15e8+2409-0x1f51);T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL; |
| 273 | T_NV_ITEM*item=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
| 274 | nvFile,file))continue;for(i=(0xebc+2335-0x17db);i<NV_HASH_LEN;i++){for(item=list |
| 275 | ->nvTable[i];item;){cur=item;item=item->next;free(cur->key);free(cur->value); |
| 276 | free(cur);}list->nvTable[i]=NULL;}return RESULT_SUCCESS;}return RESULT_NO_FILE;} |
| 277 | static int nvcommit(char*file){int ret=(0xca8+173-0xd55);char nvMainFile[ |
| 278 | NV_PATH_LEN]={(0x705+1086-0xb43)};char nvBackupFile[NV_PATH_LEN]={ |
| 279 | (0x1256+3203-0x1ed9)};sprintf(nvMainFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH, |
| 280 | file);sprintf(nvBackupFile,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret= |
| 281 | saveNvFs(file,nvMainFile);if(ret!=RESULT_SUCCESS)return ret;return restoreNvFs( |
| 282 | nvBackupFile,nvMainFile);} |
| 283 | #ifdef __cplusplus |
| 284 | } |
| 285 | #endif |
| 286 | |