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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 34 | (0x209+4095-0x1208);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",(0x5ef+953-0x9a8), |
| 36 | (0x89a+1705-0xf43),(0x243c+329-0x2585));memset(&rcvBuf,(0x1b54+1510-0x213a), |
| 37 | sizeof(rcvBuf));memset(&sndBuf,(0x1a1d+3270-0x26e3),sizeof(sndBuf));memset(& |
| 38 | msgInfo,(0x12d7+429-0x1484),sizeof(msgInfo));msgId=msgget(MODULE_ID_NV,IPC_CREAT |
| 39 | |(0x878+5660-0x1d14));if(-(0x792+243-0x884)==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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 41 | ,errno);return-(0x674+7929-0x256c);}if(-(0xfc7+4456-0x212e)!=msgctl(msgId, |
| 42 | IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0xcd2+5966-0x241f)==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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 45 | ,errno);}if(nvDirInit()!=(0x1ab1+1541-0x20b6)){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 47 | (0x1c4b+589-0x1e97);}nvConfig();nvInit();while((0x280+4929-0x15c0)){if(- |
| 48 | (0x196b+1134-0x1dd8)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| 49 | MSG_TYPE_NV,(0xbe1+2889-0x172a))){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 51 | ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0x950+4098-0x1951)==msgsnd( |
| 52 | msgId,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0x1ca2+7-0x1ca9))){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 54 | ,errno);continue;}}return((0x15b3+2369-0x1ef4));}static void configdir(char*dir) |
| 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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 59 | d_name,&statbuf);if(ret<(0x11e1+1995-0x19ac)){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 62 | strcmp("\x2e",entry->d_name)==(0x10b3+2384-0x1a03)||strcmp("\x2e\x2e",entry-> |
| 63 | d_name)==(0x117f+2241-0x1a40))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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 67 | val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x8f5+2539-0x12e0)};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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 71 | (0x23ac+484-0x2590)]=='\n'||buf[(0xcd2+4917-0x2007)]== |
| 72 | ((char)(0x19d7+1471-0x1f73)))continue;val=strchr(buf, |
| 73 | ((char)(0x1e34+1374-0x2355)));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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 75 | ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0xc9a+2263-0x1570)]='\0';*val++= |
xf.li | be70461 | 2024-05-28 19:09:12 -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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 79 | NV_FS_PATH,F_OK)!=(0x699+3173-0x12fe)){if(mkdir(NV_FS_PATH,(0x1451+1846-0x199a)) |
| 80 | !=(0x55a+2656-0xfba)){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 82 | ,NV_FS_PATH,errno);return-(0x6d1+6521-0x2049);}if(mkdir(NV_FS_MAIN_PATH, |
| 83 | (0x3ea+8060-0x2179))!=(0x2257+547-0x247a)){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 85 | ,NV_FS_MAIN_PATH,errno);return-(0x13d7+2433-0x1d57);}if(mkdir(NV_FS_BACKUP_PATH, |
| 86 | (0x5f3+5231-0x1875))!=(0x635+6984-0x217d)){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 88 | ,NV_FS_BACKUP_PATH,errno);return-(0x1310+1671-0x1996);}}else{if(access( |
| 89 | NV_FS_MAIN_PATH,F_OK)!=(0xed2+597-0x1127)){if(mkdir(NV_FS_MAIN_PATH, |
| 90 | (0xd16+3236-0x17cd))!=(0x497+2723-0xf3a)){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 92 | ,NV_FS_MAIN_PATH,errno);return-(0x1e87+1561-0x249f);}}if(access( |
| 93 | NV_FS_BACKUP_PATH,F_OK)!=(0x2219+104-0x2281)){if(mkdir(NV_FS_BACKUP_PATH, |
| 94 | (0x1c51+1571-0x2087))!=(0x1438+447-0x15f7)){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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 96 | ,NV_FS_BACKUP_PATH,errno);return-(0xda8+4876-0x20b3);}}}return |
| 97 | (0x19cf+1549-0x1fdc);}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[ |
| 98 | NV_PATH_LEN]={(0x15d+9159-0x2524)};char nvBackupFile[NV_PATH_LEN]={ |
| 99 | (0x18cd+2765-0x239a)}; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 100 | #ifdef FOTA_AB |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 101 | T_FLAGS_INFO flags_info={(0xfbc+989-0x1399)};int ret=(0x3f9+5150-0x1817); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 102 | #endif |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 103 | for(list=nv_list;list;list=list->next){snprintf(nvMainFile,NV_PATH_LEN, |
| 104 | "\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,list->nvFile);snprintf(nvBackupFile, |
| 105 | NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,list->nvFile);if(checkNvFs( |
| 106 | nvMainFile)){if(!checkNvFs(nvBackupFile))restoreNvFs(nvBackupFile,nvMainFile);} |
| 107 | else if(checkNvFs(nvBackupFile)){restoreNvFs(nvMainFile,nvBackupFile);}else{ |
| 108 | loadFactroyParam(list);nvcommit(list->nvFile);continue;}loadNvFs(list->nvFile); |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 109 | if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0xc7+3572-0xeb9)){ |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 110 | reloadFactroyParam(list);delete_not_needed(list);nvcommit(list->nvFile); |
| 111 | #ifdef FOTA_AB |
| 112 | ret=flags_get(&flags_info);flags_info.boot_fota_flag.fota_status= |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 113 | (0x38a+170-0x434);ret=flags_set(&flags_info); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 114 | #endif |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 115 | }}}uint hash(const char*s){uint hash=(0x731+4220-0x17ad);while(*s){hash= |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 116 | NV_HASH_MUL*hash+*s++;}return hash;}static int loadFactroyParam(T_NV_NODE*list){ |
| 117 | char*val=NULL;FILE*fp=NULL;T_NV_CONFIG*config=NULL;char buf[NV_MAX_ITEM_LEN]={ |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 118 | (0xcab+2015-0x148a)};for(config=list->fileList;config;config=config->next){fp= |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 119 | fopen(config->configFile,"\x72\x6f");if(!fp){printf( |
| 120 | "\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" |
| 121 | ,config->configFile,errno);return RESULT_FILE_OPEN_FAIL;}while(fgets(buf, |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 122 | NV_MAX_ITEM_LEN,fp)){if(buf[(0x207+8575-0x2386)]=='\n'||buf[(0x1a03+810-0x1d2d)] |
| 123 | ==((char)(0x6cf+6487-0x2003)))continue;val=strchr(buf, |
| 124 | ((char)(0x741+2705-0x1195)));if(!val){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 125 | "\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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 126 | ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x131d+1243-0x17f7)]== |
| 127 | '\n')buf[strlen(buf)-(0x1df2+849-0x2142)]='\0';*val++='\0';nvset(list->nvFile, |
| 128 | buf,val,(0x8b7+4145-0x18e7));}printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 129 | "\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" |
| 130 | ,config->configFile);fclose(fp);}return RESULT_SUCCESS;}static bool checkNvFs( |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 131 | char*file){int len=(0x1d51+1210-0x220b);int cnt=(0x125c+3616-0x207c);FILE*fp= |
| 132 | NULL;char*buf=NULL;struct stat statbuff={(0x186a+1396-0x1dde)};if(stat(file,& |
| 133 | statbuff)<(0x184+2278-0xa6a))return false;len=statbuff.st_size;if(len< |
| 134 | NV_CHECK_SIZE)return false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=( |
| 135 | char*)malloc(len);if(!buf){fclose(fp);return false;}cnt=(0xf14+4502-0x20aa); |
| 136 | while(cnt<len){cnt=cnt+fread(buf+cnt,(0x6b2+7964-0x25cd),len-cnt,fp);if(ferror( |
| 137 | fp)){clearerr(fp);free(buf);fclose(fp);return false;}}if(len!=cnt){free(buf); |
| 138 | fclose(fp);return false;}if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*) |
| 139 | (buf+len-NV_CHECK_SIZE)){free(buf);fclose(fp);return false;}free(buf);fclose(fp) |
| 140 | ;return true;}static int copyfile(const char*from,const char*to){int fd_to;int |
| 141 | fd_from;char buf[(0x126c+1519-0x85b)];ssize_t nread;int ret=-(0x893+7553-0x2613) |
| 142 | ;fd_from=open(from,O_RDONLY);if(fd_from<(0xafa+6485-0x244f))return- |
| 143 | (0x621+1739-0xcea);fd_to=open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC, |
| 144 | (0x1cf9+964-0x1f1d));if(fd_to<(0x340+6746-0x1d9a)){ret=-(0x57+884-0x3c8);goto |
| 145 | out_error;}while((0x2385+790-0x269a)){char*out_ptr;ssize_t nwritten;nread=read( |
| 146 | fd_from,buf,sizeof(buf));if(nread==(0x4c3+7723-0x22ee)){break;}else{if(nread< |
| 147 | (0x20cf+283-0x21ea)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| 148 | (0x86a+7595-0x2611);goto out_error;}}}out_ptr=buf;do{nwritten=write(fd_to, |
| 149 | out_ptr,nread);if(nwritten>(0xc75+2449-0x1606)){nread-=nwritten;out_ptr+= |
| 150 | nwritten;}else{if(nwritten<(0xa34+6034-0x21c6)){if(errno==EINTR||errno==EAGAIN){ |
| 151 | continue;}else{ret=-(0xdf8+743-0x10da);goto out_error;}}}}while(nread> |
| 152 | (0x26c0+32-0x26e0));}ret=fsync(fd_to);if(ret<(0xe54+2082-0x1676)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 153 | "\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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 154 | ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0x20f6+1175-0x258d)){fd_to |
| 155 | =-(0x1abc+749-0x1da8);ret=-(0x1c90+2508-0x2656);goto out_error;}close(fd_from); |
| 156 | return(0xe04+1599-0x1443);out_error:printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 157 | "\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 | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 158 | ,from,to,ret);close(fd_from);if(fd_to>=(0x1ebc+741-0x21a1))close(fd_to);return |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 159 | ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 160 | dstFile)!=(0x16f+3128-0xda7))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| 161 | int loadNvFs(char*file){int len=(0x2032+756-0x2326);int cnt=(0x1c14+676-0x1eb8); |
| 162 | FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
| 163 | stat statbuff={(0xbc9+611-0xe2c)};char nvFile[NV_PATH_LEN]={(0x9b+1245-0x578)}; |
| 164 | sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);if(stat(nvFile,& |
| 165 | statbuff)<(0x1374+3443-0x20e7))return RESULT_FAIL;len=statbuff.st_size;if( |
| 166 | NV_CHECK_SIZE>len)return RESULT_FAIL;fp=fopen(nvFile,"\x72\x6f");if(!fp)return |
| 167 | RESULT_FILE_OPEN_FAIL;len=len-NV_CHECK_SIZE;buf=(char*)malloc(len+ |
| 168 | (0x1898+0-0x1897));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| 169 | (0xe61+4699-0x20bc),len+(0x10dd+4748-0x2368));cnt=(0x29c+8750-0x24ca);while(cnt< |
| 170 | len){cnt=cnt+fread(buf+cnt,(0xec7+3911-0x1e0d),len-cnt,fp);if(ferror(fp)){ |
xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 171 | clearerr(fp);fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}}if(cnt!=len){ |
| 172 | fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}buf[len]='\0';name=buf;while( |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 173 | *name){if(!(eq=strchr(name,((char)(0x59f+1260-0xa4e))))){break;}*eq='\0';value= |
| 174 | eq+(0x1493+1684-0x1b26);nvset(file,name,value,(0x126+3821-0x1012));*eq= |
| 175 | ((char)(0xd5c+3981-0x1cac));name=value+strlen(value)+(0x22f+521-0x437);}free(buf |
| 176 | );fclose(fp);return RESULT_SUCCESS;}static void analyMsg(T_NV_MSG_INFO*msgrecv, |
| 177 | T_NV_MSG_RESULT*msgsnd){switch(msgrecv->nvType){case MSG_GET:msgsnd->result= |
| 178 | nvget(msgrecv->file,msgrecv->key,msgsnd->value);break;case MSG_SET:msgsnd-> |
| 179 | result=nvset(msgrecv->file,msgrecv->key,msgrecv->value,msgrecv->saveflag);break; |
| 180 | case MSG_UNSET:msgsnd->result=nvunset(msgrecv->file,msgrecv->key);break;case |
| 181 | MSG_CLEAR:msgsnd->result=nvclear(msgrecv->file);break;case MSG_RESET:msgsnd-> |
| 182 | result=nvreset(msgrecv->file);break;case MSG_SHOW:msgsnd->result=saveNvFs( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 183 | msgrecv->file,NV_FS_SHOW);;break;case MSG_COMMIT:msgsnd->result=nvcommit(msgrecv |
| 184 | ->file);break;default:break;}msgsnd->msgType=msgrecv->pid;msgsnd->msgIndex= |
| 185 | msgrecv->msgIndex;}static int addConfigFile(char*nvFile,char*configFile){ |
| 186 | T_NV_NODE*list=NULL;T_NV_NODE*newList=NULL;T_NV_CONFIG*newConfig=NULL;if(!nvFile |
| 187 | ){printf( |
| 188 | "\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" |
| 189 | );return RESULT_INVAL;}if(configFile){newConfig=(T_NV_CONFIG*)malloc(sizeof( |
| 190 | T_NV_CONFIG));if(!newConfig)return RESULT_MALLOC_FAIL;strncpy(newConfig-> |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 191 | configFile,configFile,NV_PATH_LEN-(0x1f28+709-0x21ec));newConfig->configFile[ |
| 192 | NV_PATH_LEN-(0x291+345-0x3e9)]='\0';newConfig->next=NULL;}for(list=nv_list;list; |
| 193 | list=list->next){if(strcmp(list->nvFile,nvFile)==(0x21b+3089-0xe2c))break;}if(! |
| 194 | list){newList=(T_NV_NODE*)malloc(sizeof(T_NV_NODE));if(!newList){if(newConfig) |
| 195 | free(newConfig);return RESULT_MALLOC_FAIL;}newList->next=NULL;strncpy(newList-> |
| 196 | nvFile,nvFile,NV_PATH_LEN-(0xe61+5060-0x2224));newList->nvFile[NV_PATH_LEN- |
| 197 | (0x704+3236-0x13a7)]='\0';memset(newList->nvTable,(0x887+1381-0xdec),NV_HASH_LEN |
| 198 | *(0x1858+1842-0x1f86));newList->fileList=newConfig;if(!nv_list)nv_list=newList; |
| 199 | else{newList->next=nv_list->next;nv_list->next=newList;}}else if(!list->fileList |
| 200 | )list->fileList=newConfig;else{if(newConfig==NULL)return RESULT_FAIL;newConfig-> |
| 201 | next=list->fileList->next;list->fileList->next=newConfig;}return RESULT_SUCCESS; |
| 202 | }static bool isCfgConfiged(char*configFile){T_NV_NODE*list=NULL;T_NV_CONFIG* |
| 203 | config=NULL;for(list=nv_list;list;list=list->next){for(config=list->fileList; |
| 204 | config;config=config->next){if(!strcmp(config->configFile,configFile))return |
| 205 | true;}}return false;}static bool isNvConfiged(char*nvFile){T_NV_NODE*list=NULL; |
| 206 | for(list=nv_list;list;list=list->next){if(!strcmp(list->nvFile,nvFile))return |
| 207 | true;}return false;}static uint getSum(const char*s,int len){uint sum= |
| 208 | (0x20b9+662-0x234f);char*data=(char*)s;while(len-- >(0x93d+6263-0x21b4)){sum+=(* |
| 209 | data++);}return sum;}static int saveNvFs(char*nvName,char*nvFile){int i= |
| 210 | (0x8ab+5763-0x1f2e);int sum=(0xfec+4575-0x21cb);int bufSize=(0xeda+5983-0x2639); |
| 211 | int itemSize=(0x1468+2076-0x1c84);int ret=(0xa40+6819-0x24e3);int fp= |
| 212 | (0xe4+8683-0x22cf);char*buf=NULL;T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for( |
| 213 | list=nv_list;list;list=list->next){if(strcmp(list->nvFile,nvName))continue;fp= |
| 214 | open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC,(0x4a4+4533-0x14b9));if(fp==- |
| 215 | (0x1036+753-0x1326)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 216 | "\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x61\x69\x6c\x2c\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 217 | ,nvFile,errno);return RESULT_FILE_OPEN_FAIL;}buf=(char*)malloc(NV_BLOCK_SIZE);if |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 218 | (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0xf86+1867-0x16d1);i< |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 219 | NV_HASH_LEN;i++){for(item=list->nvTable[i];item;item=item->next){if(strcmp( |
| 220 | nvFile,NV_FS_SHOW)&&!item->saveFlag)continue;itemSize=strlen(item->key)+strlen( |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 221 | item->value)+(0x20d0+1126-0x2534);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp |
| 222 | ,buf,bufSize)<(0x1cf5+385-0x1e76)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 223 | "\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" |
| 224 | ,__FILE__,__LINE__,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 225 | +=getSum(buf,bufSize);bufSize=(0x159d+4280-0x2655);}sprintf(buf+bufSize, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 226 | "\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!= |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 227 | (0x202b+304-0x215b)){if(write(fp,buf,bufSize)<(0x613+4423-0x175a)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 228 | "\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" |
| 229 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum+=getSum(buf, |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 230 | bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x1eb5+1324-0x23e1) |
xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 231 | ){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 232 | "\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" |
| 233 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}ret=fsync(fp);free( |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 234 | buf);close(fp);if(ret<(0x2da+6151-0x1ae1)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 235 | "\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" |
| 236 | ,strerror(errno),nvFile);return ret;}return RESULT_SUCCESS;}return |
| 237 | RESULT_NO_FILE;}static int nvget(char*file,char*key,char*value){int index= |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 238 | (0x1800+1825-0x1f21);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list; |
| 239 | list;list=list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)% |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 240 | NV_HASH_LEN;for(item=list->nvTable[index];item;item=item->next){if(strcmp(item-> |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 241 | key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0xaa1+6608-0x2470)); |
| 242 | value[NV_MAX_VAL_LEN-(0x122f+791-0x1545)]='\0';return RESULT_SUCCESS;}}return |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 243 | RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 244 | saveFlag){int index=(0xdd6+4499-0x1f69);int ret=(0x92+5360-0x1582);int |
| 245 | key_buf_len=(0x1702+2510-0x20d0);int value_buf_len=(0x14ff+4295-0x25c6); |
| 246 | T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key|| |
| 247 | NULL==value)return RESULT_FAIL;key_buf_len=strlen(key)+(0x6f9+6894-0x21e6); |
| 248 | value_buf_len=strlen(value)+(0x9f0+4052-0x19c3);for(list=nv_list;list;list=list |
| 249 | ->next){if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for( |
| 250 | item=list->nvTable[index];item;item=item->next){if(strcmp(item->key,key)) |
| 251 | continue;if(saveFlag)item->saveFlag=saveFlag;if(!strcmp(item->value,value)) |
| 252 | return RESULT_SUCCESS;free(item->value);item->value=(char*)malloc(value_buf_len) |
| 253 | ;if(!item->value)return RESULT_MALLOC_FAIL;strncpy(item->value,value, |
| 254 | value_buf_len-(0x303+1556-0x916));item->value[value_buf_len-(0x4ca+1362-0xa1b)]= |
| 255 | '\0';return RESULT_SUCCESS;}newItem=(T_NV_ITEM*)malloc(sizeof(T_NV_ITEM));if(! |
| 256 | newItem)return RESULT_MALLOC_FAIL;newItem->key=(char*)malloc(key_buf_len);if(! |
| 257 | newItem->key){free(newItem);return RESULT_MALLOC_FAIL;}newItem->value=(char*) |
| 258 | malloc(value_buf_len);if(!newItem->value){free(newItem->key);free(newItem); |
| 259 | return RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len- |
| 260 | (0x1454+709-0x1718));newItem->key[key_buf_len-(0x1a19+2342-0x233e)]='\0';strncpy |
| 261 | (newItem->value,value,value_buf_len-(0x10fc+1816-0x1813));newItem->value[ |
| 262 | value_buf_len-(0x12e2+1287-0x17e8)]='\0';newItem->next=NULL;newItem->saveFlag= |
| 263 | saveFlag;newItem->update_flag=(0x9+7491-0x1d4c);if(!list->nvTable[index])list-> |
| 264 | nvTable[index]=newItem;else{newItem->next=list->nvTable[index]->next;list-> |
| 265 | nvTable[index]->next=newItem;}return RESULT_SUCCESS;}ret=addConfigFile(file,NULL |
| 266 | );if(ret==RESULT_SUCCESS)return nvset(file,key,value,saveFlag);else return ret;} |
| 267 | static int nvunset(char*file,char*key){int index=(0xc21+2692-0x16a5);T_NV_NODE* |
| 268 | list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*prev=NULL;for(list=nv_list;list;list= |
| 269 | list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN; |
| 270 | for(item=list->nvTable[index];item;prev=item,item=item->next){if(strcmp(item-> |
| 271 | key,key))continue;if(!prev)list->nvTable[index]=item->next;else prev->next=item |
| 272 | ->next;free(item->key);free(item->value);free(item);return RESULT_SUCCESS;}} |
| 273 | return RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0xd28+4620-0x1f34) |
| 274 | ;T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 275 | nvFile,file))continue;ret=nvclear(file);if(ret!=RESULT_SUCCESS)return ret;if( |
| 276 | loadFactroyParam(list)!=RESULT_SUCCESS)return RESULT_FAIL;return nvcommit(file); |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 277 | }return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0x19f3+341-0x1b48); |
| 278 | T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list |
| 279 | ;list=list->next){if(strcmp(list->nvFile,file))continue;for(i= |
| 280 | (0x1423+2484-0x1dd7);i<NV_HASH_LEN;i++){for(item=list->nvTable[i];item;){cur= |
xf.li | be70461 | 2024-05-28 19:09:12 -0700 | [diff] [blame] | 281 | item;item=item->next;free(cur->key);free(cur->value);free(cur);}list->nvTable[i] |
| 282 | =NULL;}return RESULT_SUCCESS;}return RESULT_NO_FILE;}static int nvcommit(char* |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 283 | file){int ret=(0x20f1+734-0x23cf);char nvMainFile[NV_PATH_LEN]={ |
| 284 | (0x206f+1215-0x252e)};char nvBackupFile[NV_PATH_LEN]={(0x1d20+1365-0x2275)}; |
| 285 | sprintf(nvMainFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf( |
| 286 | nvBackupFile,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret=saveNvFs(file, |
| 287 | nvMainFile);if(ret!=RESULT_SUCCESS)return ret;return restoreNvFs(nvBackupFile, |
| 288 | nvMainFile);} |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 289 | #ifdef __cplusplus |
| 290 | } |
| 291 | #endif |
| 292 | |