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 |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 17 | #include "pub_flags.h" |
| 18 | #include "flags_api.h" |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 19 | #endif |
| 20 | #ifdef __cplusplus |
| 21 | extern"C"{ |
| 22 | #endif |
| 23 | static void nvConfig();static void nvInit();static int nvDirInit();static void |
| 24 | analyMsg(T_NV_MSG_INFO*msgrecv,T_NV_MSG_RESULT*msgsnd);static bool checkNvFs( |
| 25 | char*file);static bool isCfgConfiged(char*configFile);static bool isNvConfiged( |
| 26 | char*configFile);static uint getSum(const char*s,int len);static int |
| 27 | loadFactroyParam(T_NV_NODE*list);static int restoreNvFs(char*dstFile,char* |
| 28 | srcFile);static int loadNvFs(char*file);static int addConfigFile(char*nvFile, |
| 29 | char*configFile);static int saveNvFs(char*nvName,char*nvFile);static int nvset( |
| 30 | char*file,const char*key,const char*value,int saveFlag);static int nvget(char* |
| 31 | file,char*key,char*value);static int nvunset(char*file,char*key);static int |
| 32 | nvclear(char*file);static int nvreset(char*file);static int nvcommit(char*file); |
| 33 | T_NV_NODE*nv_list;int nvserver_main(int argc,char*argv[]){int msgId= |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 34 | (0x1264+3956-0x21d8);T_NV_MSG_INFO rcvBuf;T_NV_MSG_RESULT sndBuf;struct msqid_ds |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 35 | msgInfo;prctl(PR_SET_NAME,"\x6e\x76\x73\x65\x72\x76\x65\x72", |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 36 | (0x719+5411-0x1c3c),(0xbdc+2885-0x1721),(0x1078+2587-0x1a93));memset(&rcvBuf, |
| 37 | (0x8eb+7554-0x266d),sizeof(rcvBuf));memset(&sndBuf,(0x1092+167-0x1139),sizeof( |
| 38 | sndBuf));memset(&msgInfo,(0x551+6097-0x1d22),sizeof(msgInfo));msgId=msgget( |
| 39 | MODULE_ID_NV,IPC_CREAT|(0x450+8248-0x2308));if(-(0x125f+4961-0x25bf)==msgId){ |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 40 | printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 41 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 42 | ,errno);return-(0x3f7+8816-0x2666);}if(-(0x1f3f+838-0x2284)!=msgctl(msgId, |
| 43 | IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0xf26+1228-0x13f1)==msgctl( |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 44 | msgId,IPC_SET,&msgInfo))printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 45 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 46 | ,errno);}if(nvDirInit()!=(0xbd7+1403-0x1152)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 47 | "\x6e\x76\x44\x69\x72\x49\x6e\x69\x74\x20\x66\x61\x69\x6c\x65\x21" "\n");return- |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 48 | (0x1a44+1778-0x2135);}nvConfig();nvInit();while((0xf1a+5589-0x24ee)){if(- |
| 49 | (0x2088+16-0x2097)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| 50 | MSG_TYPE_NV,(0x1eb3+370-0x2025))){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 51 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 52 | ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0xc53+4627-0x1e65)==msgsnd( |
| 53 | msgId,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0x7e5+114-0x857))){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 54 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 55 | ,errno);continue;}}return((0x1665+984-0x1a3d));}static void configdir(char*dir){ |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 56 | 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] | 57 | NULL){fprintf(stderr, |
| 58 | "\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" |
| 59 | ,dir);return;}chdir(dir);while((entry=readdir(dp))!=NULL){ret=lstat(entry-> |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 60 | d_name,&statbuf);if(ret<(0x1dc0+309-0x1ef5)){fprintf(stderr, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 61 | "\x6c\x73\x74\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x25\x73" "\n",strerror( |
| 62 | errno));chdir("\x2e\x2e");closedir(dp);return;}if(!S_ISDIR(statbuf.st_mode)){if( |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 63 | strcmp("\x2e",entry->d_name)==(0xc1c+6093-0x23e9)||strcmp("\x2e\x2e",entry-> |
| 64 | d_name)==(0xa27+312-0xb5f))continue;if(!isNvConfiged(entry->d_name)){if( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 65 | addConfigFile(entry->d_name,NULL)!=RESULT_SUCCESS)printf( |
| 66 | "\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" |
| 67 | ,entry->d_name);}}}chdir("\x2e\x2e");closedir(dp);}static void nvConfig(){char* |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 68 | val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x2192+152-0x222a)};fp=fopen |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 69 | (NV_CONFIG_FILE,"\x72\x6f");if(!fp){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 70 | "\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" |
| 71 | ,NV_CONFIG_FILE,errno);return;}while(fgets(buf,NV_MAX_CONFIG_LEN,fp)){if(buf[ |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 72 | (0xa91+3923-0x19e4)]=='\n'||buf[(0x190+2562-0xb92)]==((char)(0x8f8+2485-0x128a)) |
| 73 | )continue;val=strchr(buf,((char)(0x124b+4342-0x2304)));if(!val){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 74 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 75 | ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0x10c+198-0x1d1)]='\0';*val++= |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 76 | '\0';if(!isCfgConfiged(buf)){if(addConfigFile(val,buf)!=RESULT_SUCCESS)printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 77 | "\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" |
| 78 | ,buf);}}fclose(fp);configdir(NV_FS_MAIN_PATH);}static int nvDirInit(){if(access( |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 79 | NV_FS_PATH,F_OK)!=(0xd75+5483-0x22e0)){if(mkdir(NV_FS_PATH,(0x1e15+2387-0x257b)) |
| 80 | !=(0x108+2110-0x946)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 81 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 82 | ,NV_FS_PATH,errno);return-(0x440+6970-0x1f79);}if(mkdir(NV_FS_MAIN_PATH, |
| 83 | (0xc54+106-0xad1))!=(0xd45+617-0xfae)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 84 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 85 | ,NV_FS_MAIN_PATH,errno);return-(0x14a+3087-0xd58);}if(mkdir(NV_FS_BACKUP_PATH, |
| 86 | (0xf23+5183-0x2175))!=(0xd7b+2919-0x18e2)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 87 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 88 | ,NV_FS_BACKUP_PATH,errno);return-(0x134f+2749-0x1e0b);}}else{if(access( |
| 89 | NV_FS_MAIN_PATH,F_OK)!=(0x150+79-0x19f)){if(mkdir(NV_FS_MAIN_PATH, |
| 90 | (0x1bd3+2160-0x2256))!=(0xd5d+582-0xfa3)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 91 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 92 | ,NV_FS_MAIN_PATH,errno);return-(0xa77+5594-0x2050);}}if(access(NV_FS_BACKUP_PATH |
| 93 | ,F_OK)!=(0xa02+7317-0x2697)){if(mkdir(NV_FS_BACKUP_PATH,(0xf57+3051-0x1955))!= |
| 94 | (0x4b8+694-0x76e)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 95 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 96 | ,NV_FS_BACKUP_PATH,errno);return-(0x3fc+7803-0x2276);}}}return(0x4b8+1097-0x901) |
| 97 | ;}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[NV_PATH_LEN]={ |
| 98 | (0x1726+3894-0x265c)};char nvBackupFile[NV_PATH_LEN]={(0xebc+4855-0x21b3)}; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 99 | #ifdef FOTA_AB |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 100 | T_FLAGS_INFO flags_info={(0x1d92+2161-0x2603)};int ret=(0xccd+2810-0x17c7); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 101 | #endif |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 102 | for(list=nv_list;list;list=list->next){snprintf(nvMainFile,NV_PATH_LEN, |
| 103 | "\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,list->nvFile);snprintf(nvBackupFile, |
| 104 | NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,list->nvFile);if(checkNvFs( |
| 105 | nvMainFile)){if(!checkNvFs(nvBackupFile))restoreNvFs(nvBackupFile,nvMainFile);} |
| 106 | else if(checkNvFs(nvBackupFile)){restoreNvFs(nvMainFile,nvBackupFile);}else{ |
| 107 | loadFactroyParam(list);nvcommit(list->nvFile);continue;}loadNvFs(list->nvFile); |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 108 | if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0x8eb+5436-0x1e25)){ |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 109 | reloadFactroyParam(list);delete_not_needed(list);nvcommit(list->nvFile); |
| 110 | #ifdef FOTA_AB |
| 111 | ret=flags_get(&flags_info);flags_info.boot_fota_flag.fota_status= |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 112 | (0x8a4+6695-0x22cb);ret=flags_set(&flags_info); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 113 | #endif |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 114 | }}}uint hash(const char*s){uint hash=(0x53+270-0x161);while(*s){hash=NV_HASH_MUL |
| 115 | *hash+*s++;}return hash;}static int loadFactroyParam(T_NV_NODE*list){char*val= |
| 116 | NULL;FILE*fp=NULL;T_NV_CONFIG*config=NULL;char buf[NV_MAX_ITEM_LEN]={ |
| 117 | (0x74c+5431-0x1c83)};for(config=list->fileList;config;config=config->next){fp= |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 118 | fopen(config->configFile,"\x72\x6f");if(!fp){printf( |
| 119 | "\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" |
| 120 | ,config->configFile,errno);return RESULT_FILE_OPEN_FAIL;}while(fgets(buf, |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 121 | NV_MAX_ITEM_LEN,fp)){if(buf[(0xa7d+4960-0x1ddd)]=='\n'||buf[(0x4b+7374-0x1d19)] |
| 122 | ==((char)(0x1b90+498-0x1d5f)))continue;val=strchr(buf, |
| 123 | ((char)(0x5ef+4774-0x1858)));if(!val){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 124 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 125 | ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x294+695-0x54a)]=='\n') |
| 126 | buf[strlen(buf)-(0x9f+2063-0x8ad)]='\0';*val++='\0';nvset(list->nvFile,buf,val, |
| 127 | (0xe9a+1174-0x132f));}printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 128 | "\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" |
| 129 | ,config->configFile);fclose(fp);}return RESULT_SUCCESS;}static bool checkNvFs( |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 130 | char*file){int len=(0x810+4646-0x1a36);int cnt=(0xf3d+5720-0x2595);FILE*fp=NULL; |
| 131 | char*buf=NULL;struct stat statbuff={(0x640+1450-0xbea)};if(stat(file,&statbuff)< |
| 132 | (0x13fa+2513-0x1dcb))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE) |
| 133 | return false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len |
| 134 | );if(!buf){fclose(fp);return false;}cnt=(0x6d5+3192-0x134d);while(cnt<len){cnt= |
| 135 | cnt+fread(buf+cnt,(0x307+8876-0x25b2),len-cnt,fp);if(ferror(fp)){clearerr(fp); |
| 136 | free(buf);fclose(fp);return false;}}if(len!=cnt){free(buf);fclose(fp);return |
| 137 | false;}if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*)(buf+len- |
| 138 | NV_CHECK_SIZE)){free(buf);fclose(fp);return false;}free(buf);fclose(fp);return |
| 139 | true;}static int copyfile(const char*from,const char*to){int fd_to;int fd_from; |
| 140 | char buf[(0x1ee2+5197-0x232f)];ssize_t nread;int ret=-(0x588+7180-0x2193); |
| 141 | fd_from=open(from,O_RDONLY);if(fd_from<(0x1f95+1128-0x23fd))return- |
| 142 | (0x245a+579-0x269b);fd_to=open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC, |
| 143 | (0x175d+162-0x165f));if(fd_to<(0x4c1+8532-0x2615)){ret=-(0x11d+8454-0x2220);goto |
| 144 | out_error;}while((0x1eb6+1269-0x23aa)){char*out_ptr;ssize_t nwritten;nread=read |
| 145 | (fd_from,buf,sizeof(buf));if(nread==(0xb4b+1322-0x1075)){break;}else{if(nread< |
| 146 | (0x45a+2475-0xe05)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| 147 | (0xe81+3639-0x1cb4);goto out_error;}}}out_ptr=buf;do{nwritten=write(fd_to, |
| 148 | out_ptr,nread);if(nwritten>(0x185d+834-0x1b9f)){nread-=nwritten;out_ptr+= |
| 149 | nwritten;}else{if(nwritten<(0x27b+4396-0x13a7)){if(errno==EINTR||errno==EAGAIN){ |
| 150 | continue;}else{ret=-(0x3c4+1288-0x8c7);goto out_error;}}}}while(nread> |
| 151 | (0x1127+2166-0x199d));}ret=fsync(fd_to);if(ret<(0x43+6050-0x17e5)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 152 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 153 | ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0x593+932-0x937)){fd_to=- |
| 154 | (0x303+8160-0x22e2);ret=-(0x840+5848-0x1f12);goto out_error;}close(fd_from); |
| 155 | return(0x9d9+6531-0x235c);out_error:printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 156 | "\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 | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 157 | ,from,to,ret);close(fd_from);if(fd_to>=(0xd89+4711-0x1ff0))close(fd_to);return |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 158 | ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 159 | dstFile)!=(0xf3+5077-0x14c8))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| 160 | int loadNvFs(char*file){int len=(0x11d0+47-0x11ff);int cnt=(0xb50+1938-0x12e2); |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 161 | FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 162 | stat statbuff={(0x2229+432-0x23d9)};char nvFile[NV_PATH_LEN]={ |
| 163 | (0x1019+355-0x117c)};sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file) |
| 164 | ;if(stat(nvFile,&statbuff)<(0x5bc+8384-0x267c))return RESULT_FAIL;len=statbuff. |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 165 | st_size;if(NV_CHECK_SIZE>len)return RESULT_FAIL;fp=fopen(nvFile,"\x72\x6f");if(! |
| 166 | fp)return RESULT_FILE_OPEN_FAIL;len=len-NV_CHECK_SIZE;buf=(char*)malloc(len+ |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 167 | (0x1063+1590-0x1698));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| 168 | (0x52b+8061-0x24a8),len+(0xc10+426-0xdb9));cnt=(0x9f5+4230-0x1a7b);while(cnt<len |
| 169 | ){cnt=cnt+fread(buf+cnt,(0x1f6b+403-0x20fd),len-cnt,fp);if(ferror(fp)){clearerr( |
| 170 | fp);fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}}if(cnt!=len){fclose(fp); |
| 171 | free(buf);return RESULT_FILE_READ_FAIL;}buf[len]='\0';name=buf;while(*name){if(! |
| 172 | (eq=strchr(name,((char)(0x1ad0+1417-0x201c))))){break;}*eq='\0';value=eq+ |
| 173 | (0x1285+3310-0x1f72);nvset(file,name,value,(0xb48+1256-0x102f));*eq= |
| 174 | ((char)(0x1cd6+531-0x1eac));name=value+strlen(value)+(0x1a24+443-0x1bde);}free( |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 175 | buf);fclose(fp);return RESULT_SUCCESS;}static void analyMsg(T_NV_MSG_INFO* |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 176 | msgrecv,T_NV_MSG_RESULT*msgsnd){switch(msgrecv->nvType){case MSG_GET:msgsnd-> |
| 177 | result=nvget(msgrecv->file,msgrecv->key,msgsnd->value);break;case MSG_SET:msgsnd |
| 178 | ->result=nvset(msgrecv->file,msgrecv->key,msgrecv->value,msgrecv->saveflag); |
| 179 | break;case MSG_UNSET:msgsnd->result=nvunset(msgrecv->file,msgrecv->key);break; |
| 180 | case MSG_CLEAR:msgsnd->result=nvclear(msgrecv->file);break;case MSG_RESET:msgsnd |
| 181 | ->result=nvreset(msgrecv->file);break;case MSG_SHOW:msgsnd->result=saveNvFs( |
| 182 | msgrecv->file,NV_FS_SHOW);;break;case MSG_COMMIT:msgsnd->result=nvcommit(msgrecv |
| 183 | ->file);break;default:break;}msgsnd->msgType=msgrecv->pid;msgsnd->msgIndex= |
| 184 | msgrecv->msgIndex;}static int addConfigFile(char*nvFile,char*configFile){ |
| 185 | T_NV_NODE*list=NULL;T_NV_NODE*newList=NULL;T_NV_CONFIG*newConfig=NULL;if(!nvFile |
| 186 | ){printf( |
| 187 | "\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" |
| 188 | );return RESULT_INVAL;}if(configFile){newConfig=(T_NV_CONFIG*)malloc(sizeof( |
| 189 | T_NV_CONFIG));if(!newConfig)return RESULT_MALLOC_FAIL;strncpy(newConfig-> |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 190 | configFile,configFile,NV_PATH_LEN-(0x8a2+3333-0x15a6));newConfig->configFile[ |
| 191 | NV_PATH_LEN-(0x16b3+1461-0x1c67)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| 192 | list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x1765+3600-0x2575))break |
| 193 | ;}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] | 194 | newConfig)free(newConfig);return RESULT_MALLOC_FAIL;}newList->next=NULL;strncpy( |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 195 | newList->nvFile,nvFile,NV_PATH_LEN-(0x15b+6161-0x196b));newList->nvFile[ |
| 196 | NV_PATH_LEN-(0x1696+2418-0x2007)]='\0';memset(newList->nvTable, |
| 197 | (0xcb2+6342-0x2578),NV_HASH_LEN*(0xb5a+981-0xf2b));newList->fileList=newConfig; |
| 198 | if(!nv_list)nv_list=newList;else{newList->next=nv_list->next;nv_list->next= |
| 199 | newList;}}else if(!list->fileList)list->fileList=newConfig;else{if(newConfig== |
| 200 | NULL)return RESULT_FAIL;newConfig->next=list->fileList->next;list->fileList-> |
| 201 | next=newConfig;}return RESULT_SUCCESS;}static bool isCfgConfiged(char*configFile |
| 202 | ){T_NV_NODE*list=NULL;T_NV_CONFIG*config=NULL;for(list=nv_list;list;list=list-> |
| 203 | next){for(config=list->fileList;config;config=config->next){if(!strcmp(config-> |
| 204 | configFile,configFile))return true;}}return false;}static bool isNvConfiged(char |
| 205 | *nvFile){T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(!strcmp( |
| 206 | list->nvFile,nvFile))return true;}return false;}static uint getSum(const char*s, |
| 207 | int len){uint sum=(0x1a7+2114-0x9e9);char*data=(char*)s;while(len-- > |
| 208 | (0x7b4+1064-0xbdc)){sum+=(*data++);}return sum;}static int saveNvFs(char*nvName, |
| 209 | char*nvFile){int i=(0xe4+4384-0x1204);int sum=(0xea6+5019-0x2241);int bufSize= |
| 210 | (0x74b+2658-0x11ad);int itemSize=(0x186+9374-0x2624);int ret=(0x192+1292-0x69e); |
| 211 | int fp=(0x49+3435-0xdb4);char*buf=NULL;T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL; |
| 212 | for(list=nv_list;list;list=list->next){if(strcmp(list->nvFile,nvName))continue; |
| 213 | fp=open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC,(0x13d3+3275-0x1efe));if(fp==- |
| 214 | (0x1c93+809-0x1fbb)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 215 | "\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x61\x69\x6c\x2c\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 216 | ,nvFile,errno);return RESULT_FILE_OPEN_FAIL;}buf=(char*)malloc(NV_BLOCK_SIZE);if |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 217 | (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0xb2+8813-0x231f);i< |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 218 | NV_HASH_LEN;i++){for(item=list->nvTable[i];item;item=item->next){if(strcmp( |
| 219 | nvFile,NV_FS_SHOW)&&!item->saveFlag)continue;itemSize=strlen(item->key)+strlen( |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 220 | item->value)+(0x353+439-0x508);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp, |
| 221 | buf,bufSize)<(0x926+5003-0x1cb1)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 222 | "\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" |
| 223 | ,__FILE__,__LINE__,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 224 | +=getSum(buf,bufSize);bufSize=(0x4f7+8209-0x2508);}sprintf(buf+bufSize, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 225 | "\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!= |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 226 | (0x15bc+2125-0x1e09)){if(write(fp,buf,bufSize)<(0xd99+3806-0x1c77)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 227 | "\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" |
| 228 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum+=getSum(buf, |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 229 | bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x13bd+2059-0x1bc8) |
| 230 | ){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 231 | "\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" |
| 232 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}ret=fsync(fp);free( |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 233 | buf);close(fp);if(ret<(0x2132+278-0x2248)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 234 | "\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" |
| 235 | ,strerror(errno),nvFile);return ret;}return RESULT_SUCCESS;}return |
| 236 | RESULT_NO_FILE;}static int nvget(char*file,char*key,char*value){int index= |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 237 | (0x178d+367-0x18fc);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list; |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 238 | list;list=list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)% |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 239 | NV_HASH_LEN;for(item=list->nvTable[index];item;item=item->next){if(strcmp(item-> |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 240 | key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0xba7+6145-0x23a7)); |
| 241 | value[NV_MAX_VAL_LEN-(0x973+6147-0x2175)]='\0';return RESULT_SUCCESS;}}return |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 242 | RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 243 | saveFlag){int index=(0x18a4+469-0x1a79);int ret=(0x1a4d+2893-0x259a);int |
| 244 | key_buf_len=(0x8dc+6981-0x2421);int value_buf_len=(0xecd+3035-0x1aa8);T_NV_NODE* |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 245 | list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key||NULL==value) |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 246 | return RESULT_FAIL;key_buf_len=strlen(key)+(0xdff+3428-0x1b62);value_buf_len= |
| 247 | strlen(value)+(0xdd3+764-0x10ce);for(list=nv_list;list;list=list->next){if( |
| 248 | strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list-> |
| 249 | nvTable[index];item;item=item->next){if(strcmp(item->key,key))continue;if( |
| 250 | saveFlag)item->saveFlag=saveFlag;if(!strcmp(item->value,value))return |
| 251 | RESULT_SUCCESS;free(item->value);item->value=(char*)malloc(value_buf_len);if(! |
| 252 | item->value)return RESULT_MALLOC_FAIL;strncpy(item->value,value,value_buf_len- |
| 253 | (0x145d+4451-0x25bf));item->value[value_buf_len-(0x41f+5275-0x18b9)]='\0';return |
| 254 | RESULT_SUCCESS;}newItem=(T_NV_ITEM*)malloc(sizeof(T_NV_ITEM));if(!newItem) |
| 255 | return RESULT_MALLOC_FAIL;newItem->key=(char*)malloc(key_buf_len);if(!newItem-> |
| 256 | key){free(newItem);return RESULT_MALLOC_FAIL;}newItem->value=(char*)malloc( |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 257 | value_buf_len);if(!newItem->value){free(newItem->key);free(newItem);return |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 258 | RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len-(0x620+1716-0xcd3)); |
| 259 | newItem->key[key_buf_len-(0x4c5+6324-0x1d78)]='\0';strncpy(newItem->value,value, |
| 260 | value_buf_len-(0x1962+852-0x1cb5));newItem->value[value_buf_len- |
| 261 | (0xa01+2559-0x13ff)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem |
| 262 | ->update_flag=(0x1dd7+1043-0x21ea);if(!list->nvTable[index])list->nvTable[index] |
| 263 | =newItem;else{newItem->next=list->nvTable[index]->next;list->nvTable[index]-> |
| 264 | next=newItem;}return RESULT_SUCCESS;}ret=addConfigFile(file,NULL);if(ret== |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 265 | RESULT_SUCCESS)return nvset(file,key,value,saveFlag);else return ret;}static int |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 266 | nvunset(char*file,char*key){int index=(0x22+4575-0x1201);T_NV_NODE*list=NULL; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 267 | T_NV_ITEM*item=NULL;T_NV_ITEM*prev=NULL;for(list=nv_list;list;list=list->next){ |
| 268 | if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list |
| 269 | ->nvTable[index];item;prev=item,item=item->next){if(strcmp(item->key,key)) |
| 270 | continue;if(!prev)list->nvTable[index]=item->next;else prev->next=item->next; |
| 271 | free(item->key);free(item->value);free(item);return RESULT_SUCCESS;}}return |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 272 | RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0xed2+2780-0x19ae); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 273 | T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
| 274 | nvFile,file))continue;ret=nvclear(file);if(ret!=RESULT_SUCCESS)return ret;if( |
| 275 | loadFactroyParam(list)!=RESULT_SUCCESS)return RESULT_FAIL;return nvcommit(file); |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 276 | }return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0xa59+6426-0x2373); |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 277 | T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list |
| 278 | ;list=list->next){if(strcmp(list->nvFile,file))continue;for(i= |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame^] | 279 | (0x1767+1796-0x1e6b);i<NV_HASH_LEN;i++){for(item=list->nvTable[i];item;){cur= |
| 280 | item;item=item->next;free(cur->key);free(cur->value);free(cur);}list->nvTable[i] |
| 281 | =NULL;}return RESULT_SUCCESS;}return RESULT_NO_FILE;}static int nvcommit(char* |
| 282 | file){int ret=(0xf60+313-0x1099);char nvMainFile[NV_PATH_LEN]={ |
| 283 | (0x17e7+731-0x1ac2)};char nvBackupFile[NV_PATH_LEN]={(0xb55+6344-0x241d)}; |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 284 | sprintf(nvMainFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf( |
| 285 | nvBackupFile,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret=saveNvFs(file, |
| 286 | nvMainFile);if(ret!=RESULT_SUCCESS)return ret;return restoreNvFs(nvBackupFile, |
| 287 | nvMainFile);} |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 288 | #ifdef __cplusplus |
| 289 | } |
| 290 | #endif |
| 291 | |