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= |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 34 | (0x12f7+3751-0x219e);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", |
| 36 | (0x1b19+3016-0x26e1),(0x1537+569-0x1770),(0x104f+123-0x10ca));memset(&rcvBuf, |
| 37 | (0x11f1+2613-0x1c26),sizeof(rcvBuf));memset(&sndBuf,(0x13ed+2776-0x1ec5),sizeof( |
| 38 | sndBuf));memset(&msgInfo,(0x124+6019-0x18a7),sizeof(msgInfo));msgId=msgget( |
| 39 | MODULE_ID_NV,IPC_CREAT|(0x1d84+2640-0x2654));if(-(0x159b+1393-0x1b0b)==msgId){ |
| 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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 42 | ,errno);return-(0x1053+3811-0x1f35);}if(-(0x57b+4811-0x1845)!=msgctl(msgId, |
| 43 | IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0x186f+926-0x1c0c)==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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 46 | ,errno);}if(nvDirInit()!=(0xe8f+3530-0x1c59)){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- |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 48 | (0x5a2+7666-0x2393);}nvConfig();nvInit();while((0x1a6b+3090-0x267c)){if(- |
| 49 | (0x25ef+225-0x26cf)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| 50 | MSG_TYPE_NV,(0x263+8625-0x2414))){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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 52 | ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0x11d+5118-0x151a)==msgsnd( |
| 53 | msgId,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0x19dd+3290-0x26b7))){printf |
| 54 | ( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 55 | "\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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 56 | ,errno);continue;}}return((0xaa+6507-0x1a15));}static void configdir(char*dir){ |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 57 | 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] | 58 | NULL){fprintf(stderr, |
| 59 | "\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" |
| 60 | ,dir);return;}chdir(dir);while((entry=readdir(dp))!=NULL){ret=lstat(entry-> |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 61 | d_name,&statbuf);if(ret<(0x88b+3070-0x1489)){fprintf(stderr, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 62 | "\x6c\x73\x74\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x25\x73" "\n",strerror( |
| 63 | errno));chdir("\x2e\x2e");closedir(dp);return;}if(!S_ISDIR(statbuf.st_mode)){if( |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 64 | strcmp("\x2e",entry->d_name)==(0x11bb+1718-0x1871)||strcmp("\x2e\x2e",entry-> |
| 65 | d_name)==(0x4ab+2378-0xdf5))continue;if(!isNvConfiged(entry->d_name)){if( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 66 | addConfigFile(entry->d_name,NULL)!=RESULT_SUCCESS)printf( |
| 67 | "\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" |
| 68 | ,entry->d_name);}}}chdir("\x2e\x2e");closedir(dp);}static void nvConfig(){char* |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 69 | val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0xdd6+1142-0x124c)};fp=fopen |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 70 | (NV_CONFIG_FILE,"\x72\x6f");if(!fp){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 71 | "\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" |
| 72 | ,NV_CONFIG_FILE,errno);return;}while(fgets(buf,NV_MAX_CONFIG_LEN,fp)){if(buf[ |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 73 | (0xaf8+2382-0x1446)]=='\n'||buf[(0x4ed+3799-0x13c4)]== |
| 74 | ((char)(0x1bd9+239-0x1ca5)))continue;val=strchr(buf,((char)(0xab+3859-0xf81))); |
| 75 | if(!val){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\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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 77 | ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0x211b+506-0x2314)]='\0';*val++= |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 78 | '\0';if(!isCfgConfiged(buf)){if(addConfigFile(val,buf)!=RESULT_SUCCESS)printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 79 | "\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" |
| 80 | ,buf);}}fclose(fp);configdir(NV_FS_MAIN_PATH);}static int nvDirInit(){if(access( |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 81 | NV_FS_PATH,F_OK)!=(0xef0+351-0x104f)){if(mkdir(NV_FS_PATH,(0xe45+2189-0x14e5))!= |
| 82 | (0xdd7+2382-0x1725)){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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 84 | ,NV_FS_PATH,errno);return-(0x21c+1486-0x7e9);}if(mkdir(NV_FS_MAIN_PATH, |
| 85 | (0x9a5+4367-0x18c7))!=(0xb67+4670-0x1da5)){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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 87 | ,NV_FS_MAIN_PATH,errno);return-(0x1332+4518-0x24d7);}if(mkdir(NV_FS_BACKUP_PATH, |
| 88 | (0xc4b+1819-0x1179))!=(0x116+948-0x4ca)){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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 90 | ,NV_FS_BACKUP_PATH,errno);return-(0xa21+3359-0x173f);}}else{if(access( |
| 91 | NV_FS_MAIN_PATH,F_OK)!=(0x12a4+3900-0x21e0)){if(mkdir(NV_FS_MAIN_PATH, |
| 92 | (0x19b3+2345-0x20ef))!=(0xb1d+6035-0x22b0)){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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 94 | ,NV_FS_MAIN_PATH,errno);return-(0xa7d+6228-0x22d0);}}if(access(NV_FS_BACKUP_PATH |
| 95 | ,F_OK)!=(0xb67+1885-0x12c4)){if(mkdir(NV_FS_BACKUP_PATH,(0xc78+1969-0x123c))!= |
| 96 | (0x1684+306-0x17b6)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 97 | "\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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 98 | ,NV_FS_BACKUP_PATH,errno);return-(0x4ca+7443-0x21dc);}}}return |
| 99 | (0x18d+8017-0x20de);}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[ |
| 100 | NV_PATH_LEN]={(0x8f8+2022-0x10de)};char nvBackupFile[NV_PATH_LEN]={ |
| 101 | (0x533+1185-0x9d4)}; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 102 | #ifdef FOTA_AB |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 103 | T_FLAGS_INFO flags_info={(0x792+2805-0x1287)};int ret=(0xdd7+4915-0x210a); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 104 | #endif |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 105 | for(list=nv_list;list;list=list->next){snprintf(nvMainFile,NV_PATH_LEN, |
| 106 | "\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,list->nvFile);snprintf(nvBackupFile, |
| 107 | NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,list->nvFile);if(checkNvFs( |
| 108 | nvMainFile)){if(!checkNvFs(nvBackupFile))restoreNvFs(nvBackupFile,nvMainFile);} |
| 109 | else if(checkNvFs(nvBackupFile)){restoreNvFs(nvMainFile,nvBackupFile);}else{ |
| 110 | loadFactroyParam(list);nvcommit(list->nvFile);continue;}loadNvFs(list->nvFile); |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 111 | if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0x1391+3374-0x20bd)){ |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 112 | reloadFactroyParam(list);delete_not_needed(list);nvcommit(list->nvFile); |
| 113 | #ifdef FOTA_AB |
| 114 | ret=flags_get(&flags_info);flags_info.boot_fota_flag.fota_status= |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 115 | (0xd9d+2967-0x1934);ret=flags_set(&flags_info); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 116 | #endif |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 117 | }}}uint hash(const char*s){uint hash=(0x1398+1810-0x1aaa);while(*s){hash= |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 118 | NV_HASH_MUL*hash+*s++;}return hash;}static int loadFactroyParam(T_NV_NODE*list){ |
| 119 | char*val=NULL;FILE*fp=NULL;T_NV_CONFIG*config=NULL;char buf[NV_MAX_ITEM_LEN]={ |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 120 | (0x1656+2747-0x2111)};for(config=list->fileList;config;config=config->next){fp= |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 121 | fopen(config->configFile,"\x72\x6f");if(!fp){printf( |
| 122 | "\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" |
| 123 | ,config->configFile,errno);return RESULT_FILE_OPEN_FAIL;}while(fgets(buf, |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 124 | NV_MAX_ITEM_LEN,fp)){if(buf[(0x124d+5276-0x26e9)]=='\n'||buf[(0x2df+8093-0x227c) |
| 125 | ]==((char)(0x147+9511-0x264b)))continue;val=strchr(buf, |
| 126 | ((char)(0x163+3723-0xfb1)));if(!val){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 127 | "\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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 128 | ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x95f+1394-0xed0)]=='\n') |
| 129 | buf[strlen(buf)-(0x161a+1921-0x1d9a)]='\0';*val++='\0';nvset(list->nvFile,buf, |
| 130 | val,(0x1276+3233-0x1f16));}printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 131 | "\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" |
| 132 | ,config->configFile);fclose(fp);}return RESULT_SUCCESS;}static bool checkNvFs( |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 133 | char*file){int len=(0x1339+1926-0x1abf);int cnt=(0x610+976-0x9e0);FILE*fp=NULL; |
| 134 | char*buf=NULL;struct stat statbuff={(0x360+2587-0xd7b)};if(stat(file,&statbuff)< |
| 135 | (0x6a5+1081-0xade))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE)return |
| 136 | false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len);if(! |
| 137 | buf){fclose(fp);return false;}cnt=(0x1d50+1685-0x23e5);while(cnt<len){cnt=cnt+ |
| 138 | fread(buf+cnt,(0x641+6863-0x210f),len-cnt,fp);if(ferror(fp)){clearerr(fp);free( |
| 139 | buf);fclose(fp);return false;}}if(len!=cnt){free(buf);fclose(fp);return false;} |
| 140 | if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*)(buf+len-NV_CHECK_SIZE)){ |
| 141 | free(buf);fclose(fp);return false;}free(buf);fclose(fp);return true;}static int |
| 142 | copyfile(const char*from,const char*to){int fd_to;int fd_from;char buf[ |
| 143 | (0x11f3+7679-0x1ff2)];ssize_t nread;int ret=-(0x387+8070-0x230c);fd_from=open( |
| 144 | from,O_RDONLY);if(fd_from<(0x1a92+3130-0x26cc))return-(0x2e4+6521-0x1c5b);fd_to= |
| 145 | open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC,(0x899+7759-0x2548));if(fd_to< |
| 146 | (0x1030+1273-0x1529)){ret=-(0x419+8950-0x270c);goto out_error;}while( |
| 147 | (0x5f0+5802-0x1c99)){char*out_ptr;ssize_t nwritten;nread=read(fd_from,buf,sizeof |
| 148 | (buf));if(nread==(0x9eb+6879-0x24ca)){break;}else{if(nread<(0x60a+110-0x678)){if |
| 149 | (errno==EINTR||errno==EAGAIN){continue;}else{ret=-(0xcb5+5442-0x21f3);goto |
| 150 | out_error;}}}out_ptr=buf;do{nwritten=write(fd_to,out_ptr,nread);if(nwritten> |
| 151 | (0xe7f+5947-0x25ba)){nread-=nwritten;out_ptr+=nwritten;}else{if(nwritten< |
| 152 | (0x1170+2190-0x19fe)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| 153 | (0x1cb9+299-0x1ddf);goto out_error;}}}}while(nread>(0x260+9380-0x2704));}ret= |
| 154 | fsync(fd_to);if(ret<(0x525+1403-0xaa0)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 155 | "\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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 156 | ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0x1f3d+1530-0x2537)){fd_to |
| 157 | =-(0x75f+6431-0x207d);ret=-(0x1e16+1971-0x25c3);goto out_error;}close(fd_from); |
| 158 | return(0x1989+2493-0x2346);out_error:printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 159 | "\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" |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 160 | ,from,to,ret);close(fd_from);if(fd_to>=(0xfbd+3801-0x1e96))close(fd_to);return |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 161 | ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 162 | dstFile)!=(0x1141+5552-0x26f1))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| 163 | int loadNvFs(char*file){int len=(0x715+4869-0x1a1a);int cnt=(0xb39+3384-0x1871); |
| 164 | FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
| 165 | stat statbuff={(0x1297+1527-0x188e)};char nvFile[NV_PATH_LEN]={ |
| 166 | (0xd67+5218-0x21c9)};sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file) |
| 167 | ;if(stat(nvFile,&statbuff)<(0x975+3867-0x1890))return RESULT_FAIL;len=statbuff. |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 168 | st_size;if(NV_CHECK_SIZE>len)return RESULT_FAIL;fp=fopen(nvFile,"\x72\x6f");if(! |
| 169 | fp)return RESULT_FILE_OPEN_FAIL;len=len-NV_CHECK_SIZE;buf=(char*)malloc(len+ |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 170 | (0x1cb9+2360-0x25f0));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| 171 | (0x1040+3256-0x1cf8),len+(0x105a+3083-0x1c64));cnt=(0x709+4120-0x1721);while(cnt |
| 172 | <len){cnt=cnt+fread(buf+cnt,(0x1f0c+584-0x2153),len-cnt,fp);if(ferror(fp)){ |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 173 | clearerr(fp);fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}}if(cnt!=len){ |
| 174 | fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}buf[len]='\0';name=buf;while( |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 175 | *name){if(!(eq=strchr(name,((char)(0x1dc8+1952-0x252b))))){break;}*eq='\0';value |
| 176 | =eq+(0xe5b+1394-0x13cc);nvset(file,name,value,(0x904+4166-0x1949));*eq= |
| 177 | ((char)(0x2d5+5417-0x17c1));name=value+strlen(value)+(0x11+4583-0x11f7);}free( |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 178 | buf);fclose(fp);return RESULT_SUCCESS;}static void analyMsg(T_NV_MSG_INFO* |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 179 | msgrecv,T_NV_MSG_RESULT*msgsnd){switch(msgrecv->nvType){case MSG_GET:msgsnd-> |
| 180 | result=nvget(msgrecv->file,msgrecv->key,msgsnd->value);break;case MSG_SET:msgsnd |
| 181 | ->result=nvset(msgrecv->file,msgrecv->key,msgrecv->value,msgrecv->saveflag); |
| 182 | break;case MSG_UNSET:msgsnd->result=nvunset(msgrecv->file,msgrecv->key);break; |
| 183 | case MSG_CLEAR:msgsnd->result=nvclear(msgrecv->file);break;case MSG_RESET:msgsnd |
| 184 | ->result=nvreset(msgrecv->file);break;case MSG_SHOW:msgsnd->result=saveNvFs( |
| 185 | msgrecv->file,NV_FS_SHOW);;break;case MSG_COMMIT:msgsnd->result=nvcommit(msgrecv |
| 186 | ->file);break;default:break;}msgsnd->msgType=msgrecv->pid;msgsnd->msgIndex= |
| 187 | msgrecv->msgIndex;}static int addConfigFile(char*nvFile,char*configFile){ |
| 188 | T_NV_NODE*list=NULL;T_NV_NODE*newList=NULL;T_NV_CONFIG*newConfig=NULL;if(!nvFile |
| 189 | ){printf( |
| 190 | "\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" |
| 191 | );return RESULT_INVAL;}if(configFile){newConfig=(T_NV_CONFIG*)malloc(sizeof( |
| 192 | T_NV_CONFIG));if(!newConfig)return RESULT_MALLOC_FAIL;strncpy(newConfig-> |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 193 | configFile,configFile,NV_PATH_LEN-(0x5e1+2259-0xeb3));newConfig->configFile[ |
| 194 | NV_PATH_LEN-(0x150+8368-0x21ff)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| 195 | list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x543+1630-0xba1))break;} |
| 196 | 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] | 197 | newConfig)free(newConfig);return RESULT_MALLOC_FAIL;}newList->next=NULL;strncpy( |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 198 | newList->nvFile,nvFile,NV_PATH_LEN-(0x279+4152-0x12b0));newList->nvFile[ |
| 199 | NV_PATH_LEN-(0x211+357-0x375)]='\0';memset(newList->nvTable,(0x1410+3588-0x2214) |
| 200 | ,NV_HASH_LEN*(0xf3f+5903-0x264a));newList->fileList=newConfig;if(!nv_list) |
| 201 | nv_list=newList;else{newList->next=nv_list->next;nv_list->next=newList;}}else if |
| 202 | (!list->fileList)list->fileList=newConfig;else{if(newConfig==NULL)return |
| 203 | RESULT_FAIL;newConfig->next=list->fileList->next;list->fileList->next=newConfig; |
| 204 | }return RESULT_SUCCESS;}static bool isCfgConfiged(char*configFile){T_NV_NODE* |
| 205 | list=NULL;T_NV_CONFIG*config=NULL;for(list=nv_list;list;list=list->next){for( |
| 206 | config=list->fileList;config;config=config->next){if(!strcmp(config->configFile, |
| 207 | configFile))return true;}}return false;}static bool isNvConfiged(char*nvFile){ |
| 208 | T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(!strcmp(list-> |
| 209 | nvFile,nvFile))return true;}return false;}static uint getSum(const char*s,int |
| 210 | len){uint sum=(0x19a0+2329-0x22b9);char*data=(char*)s;while(len-- > |
| 211 | (0x7c5+6614-0x219b)){sum+=(*data++);}return sum;}static int saveNvFs(char*nvName |
| 212 | ,char*nvFile){int i=(0x105f+3818-0x1f49);int sum=(0xad7+7136-0x26b7);int bufSize |
| 213 | =(0x14f0+4020-0x24a4);int itemSize=(0x966+63-0x9a5);int ret=(0x504+6213-0x1d49); |
| 214 | int fp=(0x515+8326-0x259b);char*buf=NULL;T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL |
| 215 | ;for(list=nv_list;list;list=list->next){if(strcmp(list->nvFile,nvName))continue; |
| 216 | fp=open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC,(0x28b+7592-0x1e93));if(fp==- |
| 217 | (0xc47+1419-0x11d1)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 218 | "\x6f\x70\x65\x6e\x20\x25\x73\x20\x66\x61\x69\x6c\x2c\x65\x72\x72\x6e\x6f\x20\x3d\x20\x25\x64" "\n" |
| 219 | ,nvFile,errno);return RESULT_FILE_OPEN_FAIL;}buf=(char*)malloc(NV_BLOCK_SIZE);if |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 220 | (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0x2db+4563-0x14ae);i< |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 221 | NV_HASH_LEN;i++){for(item=list->nvTable[i];item;item=item->next){if(strcmp( |
| 222 | nvFile,NV_FS_SHOW)&&!item->saveFlag)continue;itemSize=strlen(item->key)+strlen( |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 223 | item->value)+(0x3f1+8945-0x26e0);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp, |
| 224 | buf,bufSize)<(0xeab+6060-0x2657)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 225 | "\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" |
| 226 | ,__FILE__,__LINE__,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 227 | +=getSum(buf,bufSize);bufSize=(0x39b+535-0x5b2);}sprintf(buf+bufSize, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 228 | "\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!= |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 229 | (0x1b8+6729-0x1c01)){if(write(fp,buf,bufSize)<(0xe39+2723-0x18dc)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 230 | "\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" |
| 231 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}sum+=getSum(buf, |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 232 | bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x2011+387-0x2194)) |
| 233 | {printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 234 | "\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" |
| 235 | ,errno);close(fp);free(buf);return RESULT_FILE_WRITE_FAIL;}ret=fsync(fp);free( |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 236 | buf);close(fp);if(ret<(0xf17+905-0x12a0)){printf( |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 237 | "\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" |
| 238 | ,strerror(errno),nvFile);return ret;}return RESULT_SUCCESS;}return |
| 239 | RESULT_NO_FILE;}static int nvget(char*file,char*key,char*value){int index= |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 240 | (0x16e0+1509-0x1cc5);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] | 241 | list;list=list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)% |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 242 | NV_HASH_LEN;for(item=list->nvTable[index];item;item=item->next){if(strcmp(item-> |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 243 | key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0xcf7+779-0x1001)); |
| 244 | value[NV_MAX_VAL_LEN-(0x1bc+8288-0x221b)]='\0';return RESULT_SUCCESS;}}return |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 245 | RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 246 | saveFlag){int index=(0xd61+5268-0x21f5);int ret=(0xaaa+2793-0x1593);int |
| 247 | key_buf_len=(0x135b+3339-0x2066);int value_buf_len=(0x4b+3162-0xca5);T_NV_NODE* |
| 248 | list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key||NULL==value) |
| 249 | return RESULT_FAIL;key_buf_len=strlen(key)+(0x644+4271-0x16f2);value_buf_len= |
| 250 | strlen(value)+(0xce5+642-0xf66);for(list=nv_list;list;list=list->next){if(strcmp |
| 251 | (list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list->nvTable[ |
| 252 | index];item;item=item->next){if(strcmp(item->key,key))continue;if(saveFlag)item |
| 253 | ->saveFlag=saveFlag;if(!strcmp(item->value,value))return RESULT_SUCCESS;free( |
| 254 | item->value);item->value=(char*)malloc(value_buf_len);if(!item->value)return |
| 255 | RESULT_MALLOC_FAIL;strncpy(item->value,value,value_buf_len-(0x11e4+4978-0x2555)) |
| 256 | ;item->value[value_buf_len-(0x140f+1407-0x198d)]='\0';return RESULT_SUCCESS;} |
| 257 | newItem=(T_NV_ITEM*)malloc(sizeof(T_NV_ITEM));if(!newItem)return |
| 258 | RESULT_MALLOC_FAIL;newItem->key=(char*)malloc(key_buf_len);if(!newItem->key){ |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 259 | free(newItem);return RESULT_MALLOC_FAIL;}newItem->value=(char*)malloc( |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 260 | value_buf_len);if(!newItem->value){free(newItem->key);free(newItem);return |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 261 | RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len-(0xbb9+1232-0x1088)); |
| 262 | newItem->key[key_buf_len-(0x178+2228-0xa2b)]='\0';strncpy(newItem->value,value, |
| 263 | value_buf_len-(0x1bb1+497-0x1da1));newItem->value[value_buf_len- |
| 264 | (0xe07+3106-0x1a28)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem |
| 265 | ->update_flag=(0xd4d+750-0x103b);if(!list->nvTable[index])list->nvTable[index]= |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 266 | newItem;else{newItem->next=list->nvTable[index]->next;list->nvTable[index]->next |
| 267 | =newItem;}return RESULT_SUCCESS;}ret=addConfigFile(file,NULL);if(ret== |
| 268 | RESULT_SUCCESS)return nvset(file,key,value,saveFlag);else return ret;}static int |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 269 | nvunset(char*file,char*key){int index=(0x5ea+3796-0x14be);T_NV_NODE*list=NULL; |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 270 | T_NV_ITEM*item=NULL;T_NV_ITEM*prev=NULL;for(list=nv_list;list;list=list->next){ |
| 271 | if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list |
| 272 | ->nvTable[index];item;prev=item,item=item->next){if(strcmp(item->key,key)) |
| 273 | continue;if(!prev)list->nvTable[index]=item->next;else prev->next=item->next; |
| 274 | free(item->key);free(item->value);free(item);return RESULT_SUCCESS;}}return |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 275 | RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0x347+6086-0x1b0d); |
xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 276 | T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list-> |
| 277 | nvFile,file))continue;ret=nvclear(file);if(ret!=RESULT_SUCCESS)return ret;if( |
| 278 | loadFactroyParam(list)!=RESULT_SUCCESS)return RESULT_FAIL;return nvcommit(file); |
rita | 7189f7e | 2023-11-03 10:46:32 +0800 | [diff] [blame] | 279 | }return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0x1de8+954-0x21a2); |
| 280 | T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list |
| 281 | ;list=list->next){if(strcmp(list->nvFile,file))continue;for(i= |
| 282 | (0xd1d+3193-0x1996);i<NV_HASH_LEN;i++){for(item=list->nvTable[i];item;){cur=item |
| 283 | ;item=item->next;free(cur->key);free(cur->value);free(cur);}list->nvTable[i]= |
| 284 | NULL;}return RESULT_SUCCESS;}return RESULT_NO_FILE;}static int nvcommit(char* |
| 285 | file){int ret=(0x1286+2707-0x1d19);char nvMainFile[NV_PATH_LEN]={ |
| 286 | (0x1d0c+1656-0x2384)};char nvBackupFile[NV_PATH_LEN]={(0x101c+1513-0x1605)}; |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 287 | sprintf(nvMainFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf( |
| 288 | nvBackupFile,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret=saveNvFs(file, |
| 289 | nvMainFile);if(ret!=RESULT_SUCCESS)return ret;return restoreNvFs(nvBackupFile, |
| 290 | nvMainFile);} |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 291 | #ifdef __cplusplus |
| 292 | } |
| 293 | #endif |
| 294 | |