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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 34 | (0x15cd+48-0x15fd);T_NV_MSG_INFO rcvBuf;T_NV_MSG_RESULT sndBuf;struct msqid_ds |
| 35 | msgInfo;prctl(PR_SET_NAME,"\x6e\x76\x73\x65\x72\x76\x65\x72",(0xb65+2390-0x14bb) |
| 36 | ,(0x1268+5196-0x26b4),(0x1892+1004-0x1c7e));memset(&rcvBuf,(0x8b3+6103-0x208a), |
| 37 | sizeof(rcvBuf));memset(&sndBuf,(0x6ed+6339-0x1fb0),sizeof(sndBuf));memset(& |
| 38 | msgInfo,(0x4e6+2797-0xfd3),sizeof(msgInfo));msgId=msgget(MODULE_ID_NV,IPC_CREAT| |
| 39 | (0xc79+1358-0x1047));if(-(0x1369+4232-0x23f0)==msgId){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 41 | ,errno);return-(0x849+5528-0x1de0);}if(-(0x1f3b+1533-0x2537)!=msgctl(msgId, |
| 42 | IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0x80+5832-0x1747)==msgctl( |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame] | 43 | msgId,IPC_SET,&msgInfo))printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 45 | ,errno);}if(nvDirInit()!=(0xb9f+2462-0x153d)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 46 | "\x6e\x76\x44\x69\x72\x49\x6e\x69\x74\x20\x66\x61\x69\x6c\x65\x21" "\n");return- |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 47 | (0xefa+129-0xf7a);}nvConfig();nvInit();while((0x406+7286-0x207b)){if(- |
| 48 | (0x458+5767-0x1ade)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| 49 | MSG_TYPE_NV,(0x161+4916-0x1495))){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 51 | ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0xebf+2501-0x1883)==msgsnd( |
| 52 | msgId,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0xb17+5242-0x1f91))){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 54 | ,errno);continue;}}return((0x234+4198-0x129a));}static void configdir(char*dir){ |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame] | 55 | 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] | 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-> |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 59 | d_name,&statbuf);if(ret<(0x6a1+704-0x961)){fprintf(stderr, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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( |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 62 | strcmp("\x2e",entry->d_name)==(0xfe2+3489-0x1d83)||strcmp("\x2e\x2e",entry-> |
| 63 | d_name)==(0x6f1+4219-0x176c))continue;if(!isNvConfiged(entry->d_name)){if( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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* |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 67 | val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x230+4857-0x1529)};fp=fopen |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame] | 68 | (NV_CONFIG_FILE,"\x72\x6f");if(!fp){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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[ |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 71 | (0xaf1+693-0xda6)]=='\n'||buf[(0xeff+1152-0x137f)]==((char)(0xdf+3679-0xf1b))) |
| 72 | continue;val=strchr(buf,((char)(0xf7f+5610-0x252c)));if(!val){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 74 | ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0x149+2404-0xaac)]='\0';*val++= |
| 75 | '\0';if(!isCfgConfiged(buf)){if(addConfigFile(val,buf)!=RESULT_SUCCESS)printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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( |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 78 | NV_FS_PATH,F_OK)!=(0xaed+1482-0x10b7)){if(mkdir(NV_FS_PATH,(0x1d53+2464-0x2506)) |
| 79 | !=(0x1ae2+1512-0x20ca)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 81 | ,NV_FS_PATH,errno);return-(0x254+20-0x267);}if(mkdir(NV_FS_MAIN_PATH, |
| 82 | (0x15b5+4595-0x25bb))!=(0x12f2+1618-0x1944)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 84 | ,NV_FS_MAIN_PATH,errno);return-(0x6e9+3085-0x12f5);}if(mkdir(NV_FS_BACKUP_PATH, |
| 85 | (0x1ba9+223-0x1a9b))!=(0x340+5637-0x1945)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 87 | ,NV_FS_BACKUP_PATH,errno);return-(0x1257+1860-0x199a);}}else{if(access( |
| 88 | NV_FS_MAIN_PATH,F_OK)!=(0x8ba+1000-0xca2)){if(mkdir(NV_FS_MAIN_PATH, |
| 89 | (0xd8b+6018-0x2320))!=(0x3e6+8854-0x267c)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 91 | ,NV_FS_MAIN_PATH,errno);return-(0xccf+6635-0x26b9);}}if(access(NV_FS_BACKUP_PATH |
| 92 | ,F_OK)!=(0x15c9+2109-0x1e06)){if(mkdir(NV_FS_BACKUP_PATH,(0x151d+3948-0x229c))!= |
| 93 | (0x1718+3056-0x2308)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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" |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 95 | ,NV_FS_BACKUP_PATH,errno);return-(0x1dc5+266-0x1ece);}}}return |
| 96 | (0x84d+4533-0x1a02);}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[ |
| 97 | NV_PATH_LEN]={(0xcb0+2803-0x17a3)};char nvBackupFile[NV_PATH_LEN]={ |
| 98 | (0x3ba+4577-0x159b)}; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 99 | #ifdef FOTA_AB |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 100 | T_FLAGS_INFO flags_info={(0x1dd0+1819-0x24eb)};int ret=(0x1e9+8986-0x2503); |
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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 108 | if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0xe09+2214-0x16ad)){ |
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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 112 | (0x10b9+5466-0x2613);ret=flags_set(&flags_info); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 113 | #endif |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 114 | }}}uint hash(const char*s){uint hash=(0x1d27+2230-0x25dd);while(*s){hash= |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 115 | NV_HASH_MUL*hash+*s++;}return hash;}static int loadFactroyParam(T_NV_NODE*list){ |
| 116 | char*val=NULL;FILE*fp=NULL;T_NV_CONFIG*config=NULL;char buf[NV_MAX_ITEM_LEN]={ |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 117 | (0x20d5+793-0x23ee)};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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 121 | NV_MAX_ITEM_LEN,fp)){if(buf[(0xe47+839-0x118e)]=='\n'||buf[(0x57c+5470-0x1ada)] |
| 122 | ==((char)(0x16d6+2217-0x1f5c)))continue;val=strchr(buf, |
| 123 | ((char)(0xa67+3601-0x183b)));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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 125 | ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x26b+4750-0x14f8)]=='\n' |
| 126 | )buf[strlen(buf)-(0x1325+4694-0x257a)]='\0';*val++='\0';nvset(list->nvFile,buf, |
| 127 | val,(0x89+13-0x95));}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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 130 | char*file){int len=(0x57d+4288-0x163d);int cnt=(0x12c8+958-0x1686);FILE*fp=NULL; |
| 131 | char*buf=NULL;struct stat statbuff={(0x198+4813-0x1465)};if(stat(file,&statbuff) |
| 132 | <(0x2f1+479-0x4d0))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE)return |
| 133 | false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len);if(! |
| 134 | buf){fclose(fp);return false;}cnt=(0x6c5+5213-0x1b22);while(cnt<len){cnt=cnt+ |
| 135 | fread(buf+cnt,(0x20f+2715-0xca9),len-cnt,fp);if(ferror(fp)){clearerr(fp);free( |
| 136 | buf);fclose(fp);return false;}}if(len!=cnt){free(buf);fclose(fp);return false;} |
| 137 | if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*)(buf+len-NV_CHECK_SIZE)){ |
| 138 | free(buf);fclose(fp);return false;}free(buf);fclose(fp);return true;}static int |
| 139 | copyfile(const char*from,const char*to){int fd_to;int fd_from;char buf[ |
| 140 | (0x1336+1687-0x9cd)];ssize_t nread;int ret=-(0x361+6069-0x1b15);fd_from=open( |
| 141 | from,O_RDONLY);if(fd_from<(0x162d+34-0x164f))return-(0x893+6456-0x21c9);fd_to= |
| 142 | open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC,(0xad4+2400-0x1294));if(fd_to< |
| 143 | (0x458+622-0x6c6)){ret=-(0x100b+4749-0x2295);goto out_error;}while( |
| 144 | (0xc46+4307-0x1d18)){char*out_ptr;ssize_t nwritten;nread=read(fd_from,buf,sizeof |
| 145 | (buf));if(nread==(0x303+4220-0x137f)){break;}else{if(nread<(0x94c+3405-0x1699)){ |
| 146 | if(errno==EINTR||errno==EAGAIN){continue;}else{ret=-(0xc6a+1868-0x13b2);goto |
| 147 | out_error;}}}out_ptr=buf;do{nwritten=write(fd_to,out_ptr,nread);if(nwritten> |
| 148 | (0xcd6+3953-0x1c47)){nread-=nwritten;out_ptr+=nwritten;}else{if(nwritten< |
| 149 | (0x1dbf+753-0x20b0)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| 150 | (0xbcd+6812-0x2664);goto out_error;}}}}while(nread>(0x10e8+5513-0x2671));}ret= |
| 151 | fsync(fd_to);if(ret<(0x7a4+7319-0x243b)){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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 153 | ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0x685+3210-0x130f)){fd_to= |
| 154 | -(0x1499+4606-0x2696);ret=-(0x1370+3670-0x21c0);goto out_error;}close(fd_from); |
| 155 | return(0x107a+2026-0x1864);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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 157 | ,from,to,ret);close(fd_from);if(fd_to>=(0x131c+1698-0x19be))close(fd_to);return |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 158 | ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 159 | dstFile)!=(0x155f+660-0x17f3))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| 160 | int loadNvFs(char*file){int len=(0x104a+2804-0x1b3e);int cnt=(0xeb9+5370-0x23b3) |
xf.li | 8402749 | 2024-04-09 00:17:51 -0700 | [diff] [blame] | 161 | ;FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 162 | stat statbuff={(0xb22+1985-0x12e3)};char nvFile[NV_PATH_LEN]={ |
| 163 | (0x150c+3912-0x2454)};sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file |
| 164 | );if(stat(nvFile,&statbuff)<(0xbf1+1644-0x125d))return RESULT_FAIL;len=statbuff. |
xf.li | f233062 | 2024-05-15 18:17:18 -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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 167 | (0x15c0+1956-0x1d63));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| 168 | (0x18f4+1530-0x1eee),len+(0xe17+2864-0x1946));cnt=(0xc8a+6442-0x25b4);while(cnt< |
| 169 | len){cnt=cnt+fread(buf+cnt,(0x80c+1995-0xfd6),len-cnt,fp);if(ferror(fp)){ |
xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 170 | clearerr(fp);fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}}if(cnt!=len){ |
| 171 | fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}buf[len]='\0';name=buf;while( |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 172 | *name){if(!(eq=strchr(name,((char)(0x4a9+1762-0xb4e))))){break;}*eq='\0';value= |
| 173 | eq+(0x116f+2420-0x1ae2);nvset(file,name,value,(0x2605+153-0x269d));*eq= |
| 174 | ((char)(0x134c+2559-0x1d0e));name=value+strlen(value)+(0x459+1748-0xb2c);}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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 190 | configFile,configFile,NV_PATH_LEN-(0x9a+8039-0x2000));newConfig->configFile[ |
| 191 | NV_PATH_LEN-(0x282+5892-0x1985)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| 192 | list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x472+937-0x81b))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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 195 | newList->nvFile,nvFile,NV_PATH_LEN-(0x19dd+2814-0x24da));newList->nvFile[ |
| 196 | NV_PATH_LEN-(0x15fa+787-0x190c)]='\0';memset(newList->nvTable,(0x3a4+1780-0xa98) |
| 197 | ,NV_HASH_LEN*(0x69b+7648-0x2477));newList->fileList=newConfig;if(!nv_list) |
| 198 | nv_list=newList;else{newList->next=nv_list->next;nv_list->next=newList;}}else if |
| 199 | (!list->fileList)list->fileList=newConfig;else{if(newConfig==NULL)return |
| 200 | RESULT_FAIL;newConfig->next=list->fileList->next;list->fileList->next=newConfig; |
| 201 | }return RESULT_SUCCESS;}static bool isCfgConfiged(char*configFile){T_NV_NODE* |
| 202 | list=NULL;T_NV_CONFIG*config=NULL;for(list=nv_list;list;list=list->next){for( |
| 203 | config=list->fileList;config;config=config->next){if(!strcmp(config->configFile, |
| 204 | configFile))return true;}}return false;}static bool isNvConfiged(char*nvFile){ |
| 205 | T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(!strcmp(list-> |
| 206 | nvFile,nvFile))return true;}return false;}static uint getSum(const char*s,int |
| 207 | len){uint sum=(0xf5a+5130-0x2364);char*data=(char*)s;while(len-- > |
| 208 | (0xbc5+3820-0x1ab1)){sum+=(*data++);}return sum;}static int saveNvFs(char*nvName |
| 209 | ,char*nvFile){int i=(0x5b2+3256-0x126a);int sum=(0x8ca+928-0xc6a);int bufSize= |
| 210 | (0x17ef+1694-0x1e8d);int itemSize=(0x73a+5468-0x1c96);int ret= |
| 211 | (0x206+9172-0x25da);int fp=(0x1da7+1116-0x2203);char*buf=NULL;T_NV_NODE*list= |
| 212 | NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
| 213 | nvFile,nvName))continue;fp=open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC, |
| 214 | (0x19a4+1256-0x1cec));if(fp==-(0x13a5+252-0x14a0)){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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 217 | (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0x1c3+3723-0x104e);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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 220 | item->value)+(0x11e3+2346-0x1b0b);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp |
| 221 | ,buf,bufSize)<(0x17d4+504-0x19cc)){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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 224 | +=getSum(buf,bufSize);bufSize=(0x36b+7537-0x20dc);}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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 226 | (0x536+5659-0x1b51)){if(write(fp,buf,bufSize)<(0x182a+2748-0x22e6)){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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 229 | bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x1c67+1975-0x241e) |
xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 233 | buf);close(fp);if(ret<(0x35b+2705-0xdec)){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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 237 | (0x42+9521-0x2573);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list |
| 238 | ;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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 240 | key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0x102d+3648-0x1e6c)) |
| 241 | ;value[NV_MAX_VAL_LEN-(0x972+3298-0x1653)]='\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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 243 | saveFlag){int index=(0xfe8+4900-0x230c);int ret=(0x482+2156-0xcee);int |
| 244 | key_buf_len=(0x18a0+1134-0x1d0e);int value_buf_len=(0x812+5445-0x1d57);T_NV_NODE |
| 245 | *list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key||NULL==value) |
| 246 | return RESULT_FAIL;key_buf_len=strlen(key)+(0xb34+1635-0x1196);value_buf_len= |
| 247 | strlen(value)+(0x1c65+1764-0x2348);for(list=nv_list;list;list=list->next){if( |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 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- |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 253 | (0xa02+2718-0x149f));item->value[value_buf_len-(0x18fa+276-0x1a0d)]='\0';return |
| 254 | RESULT_SUCCESS;}newItem=(T_NV_ITEM*)malloc(sizeof(T_NV_ITEM));if(!newItem)return |
| 255 | RESULT_MALLOC_FAIL;newItem->key=(char*)malloc(key_buf_len);if(!newItem->key){ |
| 256 | free(newItem);return RESULT_MALLOC_FAIL;}newItem->value=(char*)malloc( |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 257 | value_buf_len);if(!newItem->value){free(newItem->key);free(newItem);return |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 258 | RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len-(0xcf5+6118-0x24da)); |
| 259 | newItem->key[key_buf_len-(0x108+3907-0x104a)]='\0';strncpy(newItem->value,value, |
| 260 | value_buf_len-(0x6ad+341-0x801));newItem->value[value_buf_len- |
| 261 | (0x18f3+1930-0x207c)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem |
| 262 | ->update_flag=(0x1575+20-0x1589);if(!list->nvTable[index])list->nvTable[index]= |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 263 | newItem;else{newItem->next=list->nvTable[index]->next;list->nvTable[index]->next |
| 264 | =newItem;}return RESULT_SUCCESS;}ret=addConfigFile(file,NULL);if(ret== |
| 265 | RESULT_SUCCESS)return nvset(file,key,value,saveFlag);else return ret;}static int |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 266 | nvunset(char*file,char*key){int index=(0x7e9+5098-0x1bd3);T_NV_NODE*list=NULL; |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 272 | RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0x631+3161-0x128a); |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [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 | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 276 | }return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0x123f+1425-0x17d0) |
xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 277 | ;T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL;T_NV_ITEM*item=NULL;for(list=nv_list; |
| 278 | list;list=list->next){if(strcmp(list->nvFile,file))continue;for(i= |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 279 | (0x149a+1387-0x1a05);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=(0xc53+5517-0x21e0);char nvMainFile[NV_PATH_LEN]={ |
| 283 | (0xc4c+617-0xeb5)};char nvBackupFile[NV_PATH_LEN]={(0x7bb+6200-0x1ff3)};sprintf( |
| 284 | nvMainFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf(nvBackupFile, |
xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 285 | "\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret=saveNvFs(file,nvMainFile);if( |
| 286 | ret!=RESULT_SUCCESS)return ret;return restoreNvFs(nvBackupFile,nvMainFile);} |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 287 | #ifdef __cplusplus |
| 288 | } |
| 289 | #endif |
| 290 | |