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= |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 33 | (0xf9d+1777-0x168e);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",(0x692+5200-0x1ae2) |
| 35 | ,(0x37d+8962-0x267f),(0xe2a+4064-0x1e0a));memset(&rcvBuf,(0x2bf+4784-0x156f), |
| 36 | sizeof(rcvBuf));memset(&sndBuf,(0x135+7123-0x1d08),sizeof(sndBuf));memset(& |
| 37 | msgInfo,(0x1335+1600-0x1975),sizeof(msgInfo));msgId=msgget(MODULE_ID_NV, |
| 38 | IPC_CREAT|(0x1cf7+159-0x1c16));if(-(0x550+6473-0x1e98)==msgId){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 39 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 40 | ,errno);return-(0x1e2+4748-0x146d);}if(-(0x15fb+3897-0x2533)!=msgctl(msgId, |
| 41 | IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0x4a0+8126-0x245d)==msgctl( |
| 42 | msgId,IPC_SET,&msgInfo))printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 43 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 44 | ,errno);}if(nvDirInit()!=(0x1386+1034-0x1790)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 45 | "\x6e\x76\x44\x69\x72\x49\x6e\x69\x74\x20\x66\x61\x69\x6c\x65\x21" "\n");return- |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 46 | (0x103+4607-0x1301);}nvConfig();nvInit();while((0x92a+6724-0x236d)){if(- |
| 47 | (0x713+565-0x947)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| 48 | MSG_TYPE_NV,(0x160d+1056-0x1a2d))){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 49 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 50 | ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0x1723+2233-0x1fdb)==msgsnd( |
| 51 | msgId,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0xc5a+11-0xc65))){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 52 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 53 | ,errno);continue;}}return((0x3b8+8720-0x25c8));}static void configdir(char*dir){ |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 54 | DIR*dp;int ret;struct dirent*entry;struct stat statbuf;if((dp=opendir(dir))== |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 55 | NULL){fprintf(stderr, |
| 56 | "\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" |
| 57 | ,dir);return;}chdir(dir);while((entry=readdir(dp))!=NULL){ret=lstat(entry-> |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 58 | d_name,&statbuf);if(ret<(0x409+5808-0x1ab9)){fprintf(stderr, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 59 | "\x6c\x73\x74\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x25\x73" "\n",strerror( |
| 60 | errno));chdir("\x2e\x2e");closedir(dp);return;}if(!S_ISDIR(statbuf.st_mode)){if( |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 61 | strcmp("\x2e",entry->d_name)==(0xb93+2285-0x1480)||strcmp("\x2e\x2e",entry-> |
| 62 | d_name)==(0xce3+3622-0x1b09))continue;if(!isNvConfiged(entry->d_name)){if( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 63 | addConfigFile(entry->d_name,NULL)!=RESULT_SUCCESS)printf( |
| 64 | "\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" |
| 65 | ,entry->d_name);}}}chdir("\x2e\x2e");closedir(dp);}static void nvConfig(){char* |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 66 | val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x97b+272-0xa8b)};fp=fopen( |
| 67 | NV_CONFIG_FILE,"\x72\x6f");if(!fp){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 68 | "\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" |
| 69 | ,NV_CONFIG_FILE,errno);return;}while(fgets(buf,NV_MAX_CONFIG_LEN,fp)){if(buf[ |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 70 | (0xf7d+5635-0x2580)]=='\n'||buf[(0x572+6671-0x1f81)]==((char)(0x377+1667-0x9d7)) |
| 71 | )continue;val=strchr(buf,((char)(0x17ab+3339-0x2479)));if(!val){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 72 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 73 | ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0x1cb9+1681-0x2349)]='\0';*val++ |
| 74 | ='\0';if(!isCfgConfiged(buf)){if(addConfigFile(val,buf)!=RESULT_SUCCESS)printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 75 | "\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" |
| 76 | ,buf);}}fclose(fp);configdir(NV_FS_MAIN_PATH);}static int nvDirInit(){if(access( |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 77 | NV_FS_PATH,F_OK)!=(0x967+3017-0x1530)){if(mkdir(NV_FS_PATH,(0xaba+2657-0x132e)) |
| 78 | !=(0xbc7+1659-0x1242)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 79 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 80 | ,NV_FS_PATH,errno);return-(0x8d6+3871-0x17f4);}if(mkdir(NV_FS_MAIN_PATH, |
| 81 | (0xe1a+713-0xef6))!=(0x1124+2701-0x1bb1)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 82 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 83 | ,NV_FS_MAIN_PATH,errno);return-(0xadc+3711-0x195a);}if(mkdir(NV_FS_BACKUP_PATH, |
| 84 | (0x1133+5774-0x25d4))!=(0x10ef+1653-0x1764)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 85 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 86 | ,NV_FS_BACKUP_PATH,errno);return-(0x5b1+3287-0x1287);}}else{if(access( |
| 87 | NV_FS_MAIN_PATH,F_OK)!=(0x838+2209-0x10d9)){if(mkdir(NV_FS_MAIN_PATH, |
| 88 | (0x25a+3379-0xda0))!=(0x63d+3509-0x13f2)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 89 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 90 | ,NV_FS_MAIN_PATH,errno);return-(0xd01+6301-0x259d);}}if(access(NV_FS_BACKUP_PATH |
| 91 | ,F_OK)!=(0x1a7f+1439-0x201e)){if(mkdir(NV_FS_BACKUP_PATH,(0xd63+905-0xeff))!= |
| 92 | (0x13a9+1438-0x1947)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 93 | "\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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 94 | ,NV_FS_BACKUP_PATH,errno);return-(0x14c2+732-0x179d);}}}return |
| 95 | (0xdc8+1270-0x12be);}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[ |
| 96 | NV_PATH_LEN]={(0x1067+1826-0x1789)};char nvBackupFile[NV_PATH_LEN]={ |
| 97 | (0x15bd+2250-0x1e87)};for(list=nv_list;list;list=list->next){snprintf(nvMainFile |
| 98 | ,NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,list->nvFile);snprintf( |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 99 | nvBackupFile,NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,list->nvFile); |
| 100 | if(checkNvFs(nvMainFile)){if(!checkNvFs(nvBackupFile))restoreNvFs(nvBackupFile, |
| 101 | nvMainFile);}else if(checkNvFs(nvBackupFile)){restoreNvFs(nvMainFile, |
| 102 | nvBackupFile);}else{loadFactroyParam(list);nvcommit(list->nvFile);continue;} |
| 103 | loadNvFs(list->nvFile);if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()== |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 104 | (0x658+3729-0x14e7)){reloadFactroyParam(list);delete_not_needed(list);nvcommit( |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 105 | list->nvFile); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 106 | #ifdef FOTA_AB |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 107 | dual_AB_set_fota_status_for_nv((0x10f+5819-0x17ca)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 108 | #endif |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 109 | }}}uint hash(const char*s){uint hash=(0x506+7684-0x230a);while(*s){hash= |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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]={ |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 112 | (0x1810+1389-0x1d7d)};for(config=list->fileList;config;config=config->next){fp= |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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, |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 116 | NV_MAX_ITEM_LEN,fp)){if(buf[(0x519+663-0x7b0)]=='\n'||buf[(0x17dd+2775-0x22b4)] |
| 117 | ==((char)(0x5b+5361-0x1529)))continue;val=strchr(buf,((char)(0x210+1561-0x7ec))) |
| 118 | ;if(!val){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 120 | ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x1d91+1330-0x22c2)]== |
| 121 | '\n')buf[strlen(buf)-(0x3ca+3390-0x1107)]='\0';*val++='\0';nvset(list->nvFile, |
| 122 | buf,val,(0x1faa+588-0x21f5));}printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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( |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 125 | char*file){int len=(0x203+5707-0x184e);int cnt=(0xd2+428-0x27e);FILE*fp=NULL; |
| 126 | char*buf=NULL;struct stat statbuff={(0xec2+4197-0x1f27)};if(stat(file,&statbuff) |
| 127 | <(0x12e6+1104-0x1736))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 128 | return false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 129 | );if(!buf){fclose(fp);return false;}cnt=(0x256+5568-0x1816);while(cnt<len){cnt= |
| 130 | cnt+fread(buf+cnt,(0x1865+293-0x1989),len-cnt,fp);if(ferror(fp)){clearerr(fp); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 131 | free(buf);fclose(fp);return false;}}if(len!=cnt){free(buf);fclose(fp);return |
| 132 | false;}if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*)(buf+len- |
| 133 | NV_CHECK_SIZE)){free(buf);fclose(fp);return false;}free(buf);fclose(fp);return |
| 134 | true;}static int copyfile(const char*from,const char*to){int fd_to;int fd_from; |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 135 | char buf[(0x246b+2358-0x1da1)];ssize_t nread;int ret=-(0xb67+718-0xe34);fd_from= |
| 136 | open(from,O_RDONLY);if(fd_from<(0x44a+113-0x4bb))return-(0x451+1685-0xae4);fd_to |
| 137 | =open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC,(0x73d+8248-0x25d5));if(fd_to< |
| 138 | (0x102b+4386-0x214d)){ret=-(0xd21+6588-0x26da);goto out_error;}while( |
| 139 | (0x677+4919-0x19ad)){char*out_ptr;ssize_t nwritten;nread=read(fd_from,buf,sizeof |
| 140 | (buf));if(nread==(0x407+5708-0x1a53)){break;}else{if(nread<(0x1e38+2156-0x26a4)) |
| 141 | {if(errno==EINTR||errno==EAGAIN){continue;}else{ret=-(0x8b9+5996-0x2021);goto |
| 142 | out_error;}}}out_ptr=buf;do{nwritten=write(fd_to,out_ptr,nread);if(nwritten> |
| 143 | (0x1ad4+690-0x1d86)){nread-=nwritten;out_ptr+=nwritten;}else{if(nwritten< |
| 144 | (0x13d5+2399-0x1d34)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| 145 | (0x4ad+5382-0x19ae);goto out_error;}}}}while(nread>(0x935+2807-0x142c));}ret= |
| 146 | fsync(fd_to);if(ret<(0xf8c+3864-0x1ea4)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 148 | ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0x1182+5388-0x268e)){fd_to |
| 149 | =-(0x1fc9+961-0x2389);ret=-(0x13c5+1023-0x17be);goto out_error;}close(fd_from); |
| 150 | return(0x1137+617-0x13a0);out_error:printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 152 | ,from,to,ret);close(fd_from);if(fd_to>=(0x9fd+3476-0x1791))close(fd_to);return |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 153 | ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 154 | dstFile)!=(0x1721+2720-0x21c1))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| 155 | int loadNvFs(char*file){int len=(0xf3f+654-0x11cd);int cnt=(0x1630+323-0x1773); |
| 156 | FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
| 157 | stat statbuff={(0x70a+3788-0x15d6)};char nvFile[NV_PATH_LEN]={ |
| 158 | (0xe23+2248-0x16eb)};sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file) |
| 159 | ;if(stat(nvFile,&statbuff)<(0x502+149-0x597))return RESULT_FAIL;len=statbuff. |
| 160 | st_size;if(NV_CHECK_SIZE>len)return RESULT_FAIL;fp=fopen(nvFile,"\x72\x6f");if(! |
| 161 | fp)return RESULT_FILE_OPEN_FAIL;len=len-NV_CHECK_SIZE;buf=(char*)malloc(len+ |
| 162 | (0xb1b+3129-0x1753));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| 163 | (0xd63+4577-0x1f44),len+(0x1a7b+645-0x1cff));cnt=(0x8ef+2116-0x1133);while(cnt< |
| 164 | len){cnt=cnt+fread(buf+cnt,(0x496+3505-0x1246),len-cnt,fp);if(ferror(fp)){ |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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( |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 167 | *name){if(!(eq=strchr(name,((char)(0x1ab1+653-0x1d01))))){break;}*eq='\0';value= |
| 168 | eq+(0x50b+5471-0x1a69);nvset(file,name,value,(0xeb1+3329-0x1bb1));*eq= |
| 169 | ((char)(0x1805+782-0x1ad6));name=value+strlen(value)+(0x141+9350-0x25c6);}free( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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-> |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 185 | configFile,configFile,NV_PATH_LEN-(0x8e2+877-0xc4e));newConfig->configFile[ |
| 186 | NV_PATH_LEN-(0x827+4579-0x1a09)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| 187 | list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x7ff+236-0x8eb))break;} |
| 188 | if(!list){newList=(T_NV_NODE*)malloc(sizeof(T_NV_NODE));if(!newList){if( |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 189 | newConfig)free(newConfig);return RESULT_MALLOC_FAIL;}newList->next=NULL;strncpy( |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 190 | newList->nvFile,nvFile,NV_PATH_LEN-(0x6d1+2315-0xfdb));newList->nvFile[ |
| 191 | NV_PATH_LEN-(0xcc4+406-0xe59)]='\0';memset(newList->nvTable,(0xca0+493-0xe8d), |
| 192 | NV_HASH_LEN*(0xdd1+1493-0x13a2));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 | (0x1424+4397-0x2551);char*data=(char*)s;while(len-- >(0x109d+4867-0x23a0)){sum+= |
| 203 | (*data++);}return sum;}static int saveNvFs(char*nvName,char*nvFile){int i= |
| 204 | (0xcaf+3241-0x1958);int sum=(0x7b1+3097-0x13ca);int bufSize=(0x38+2128-0x888); |
| 205 | int itemSize=(0x194+5992-0x18fc);int ret=(0x11a+7134-0x1cf8);int fp= |
| 206 | (0x779+6093-0x1f46);char*buf=NULL;T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for( |
| 207 | list=nv_list;list;list=list->next){if(strcmp(list->nvFile,nvName))continue;fp= |
| 208 | open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC,(0x590+8888-0x26a8));if(fp==- |
| 209 | (0x7c2+6181-0x1fe6)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 212 | (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0x855+4427-0x19a0);i< |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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( |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 215 | item->value)+(0xaed+3813-0x19d0);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp, |
| 216 | buf,bufSize)<(0x207+773-0x50c)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 219 | +=getSum(buf,bufSize);bufSize=(0x72c+3029-0x1301);}sprintf(buf+bufSize, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 220 | "\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!= |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 221 | (0x9e0+6384-0x22d0)){if(write(fp,buf,bufSize)<(0x757+853-0xaac)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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, |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 224 | bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x127a+1921-0x19fb) |
| 225 | ){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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( |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 228 | buf);close(fp);if(ret<(0x69c+591-0x8eb)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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= |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 232 | (0x1718+29-0x1735);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list |
| 233 | ;list=list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)% |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 234 | NV_HASH_LEN;for(item=list->nvTable[index];item;item=item->next){if(strcmp(item-> |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 235 | key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0x124d+3545-0x2025)) |
| 236 | ;value[NV_MAX_VAL_LEN-(0x181c+268-0x1927)]='\0';return RESULT_SUCCESS;}}return |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 237 | RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 238 | saveFlag){int index=(0x15ff+1341-0x1b3c);int ret=(0x153a+1644-0x1ba6);int |
| 239 | key_buf_len=(0x67+8383-0x2126);int value_buf_len=(0x7ad+5162-0x1bd7);T_NV_NODE* |
| 240 | list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key||NULL==value) |
| 241 | return RESULT_FAIL;key_buf_len=strlen(key)+(0x200b+73-0x2053);value_buf_len= |
| 242 | strlen(value)+(0x54d+4926-0x188a);for(list=nv_list;list;list=list->next){if( |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 243 | strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list-> |
| 244 | nvTable[index];item;item=item->next){if(strcmp(item->key,key))continue;if( |
| 245 | saveFlag)item->saveFlag=saveFlag;if(!strcmp(item->value,value))return |
| 246 | RESULT_SUCCESS;free(item->value);item->value=(char*)malloc(value_buf_len);if(! |
| 247 | item->value)return RESULT_MALLOC_FAIL;strncpy(item->value,value,value_buf_len- |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 248 | (0xd1f+3066-0x1918));item->value[value_buf_len-(0x814+2114-0x1055)]='\0';return |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 249 | RESULT_SUCCESS;}newItem=(T_NV_ITEM*)malloc(sizeof(T_NV_ITEM));if(!newItem)return |
| 250 | RESULT_MALLOC_FAIL;newItem->key=(char*)malloc(key_buf_len);if(!newItem->key){ |
| 251 | free(newItem);return RESULT_MALLOC_FAIL;}newItem->value=(char*)malloc( |
| 252 | value_buf_len);if(!newItem->value){free(newItem->key);free(newItem);return |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 253 | RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len-(0xa8+6175-0x18c6)); |
| 254 | newItem->key[key_buf_len-(0x7d2+1506-0xdb3)]='\0';strncpy(newItem->value,value, |
| 255 | value_buf_len-(0xfa0+3677-0x1dfc));newItem->value[value_buf_len- |
| 256 | (0x8c9+7665-0x26b9)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem |
| 257 | ->update_flag=(0x179+2470-0xb1f);if(!list->nvTable[index])list->nvTable[index]= |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 258 | newItem;else{newItem->next=list->nvTable[index]->next;list->nvTable[index]->next |
| 259 | =newItem;}return RESULT_SUCCESS;}ret=addConfigFile(file,NULL);if(ret== |
| 260 | RESULT_SUCCESS)return nvset(file,key,value,saveFlag);else return ret;}static int |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 261 | nvunset(char*file,char*key){int index=(0x825+7553-0x25a6);T_NV_NODE*list=NULL; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 262 | T_NV_ITEM*item=NULL;T_NV_ITEM*prev=NULL;for(list=nv_list;list;list=list->next){ |
| 263 | if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list |
| 264 | ->nvTable[index];item;prev=item,item=item->next){if(strcmp(item->key,key)) |
| 265 | continue;if(!prev)list->nvTable[index]=item->next;else prev->next=item->next; |
| 266 | free(item->key);free(item->value);free(item);return RESULT_SUCCESS;}}return |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 267 | RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0x5ca+4630-0x17e0); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 268 | T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
| 269 | nvFile,file))continue;ret=nvclear(file);if(ret!=RESULT_SUCCESS)return ret;if( |
| 270 | loadFactroyParam(list)!=RESULT_SUCCESS)return RESULT_FAIL;return nvcommit(file); |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 271 | }return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0x821+6218-0x206b); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 272 | T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list |
| 273 | ;list=list->next){if(strcmp(list->nvFile,file))continue;for(i= |
xf.li | 742dd02 | 2023-06-08 01:43:32 -0700 | [diff] [blame^] | 274 | (0x1772+1118-0x1bd0);i<NV_HASH_LEN;i++){for(item=list->nvTable[i];item;){cur= |
| 275 | item;item=item->next;free(cur->key);free(cur->value);free(cur);}list->nvTable[i] |
| 276 | =NULL;}return RESULT_SUCCESS;}return RESULT_NO_FILE;}static int nvcommit(char* |
| 277 | file){int ret=(0x125b+636-0x14d7);char nvMainFile[NV_PATH_LEN]={ |
| 278 | (0x689+7094-0x223f)};char nvBackupFile[NV_PATH_LEN]={(0xff4+613-0x1259)};sprintf |
| 279 | (nvMainFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf(nvBackupFile, |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 280 | "\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret=saveNvFs(file,nvMainFile);if( |
| 281 | ret!=RESULT_SUCCESS)return ret;return restoreNvFs(nvBackupFile,nvMainFile);} |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 282 | #ifdef __cplusplus |
| 283 | } |
| 284 | #endif |
| 285 | |