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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 34 | (0x19b+1027-0x59e);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",(0x33c+3222-0xfd2), |
| 36 | (0x2e2+6744-0x1d3a),(0x1418+1152-0x1898));memset(&rcvBuf,(0x11ed+2790-0x1cd3), |
| 37 | sizeof(rcvBuf));memset(&sndBuf,(0x1f43+1667-0x25c6),sizeof(sndBuf));memset(& |
| 38 | msgInfo,(0x125c+79-0x12ab),sizeof(msgInfo));msgId=msgget(MODULE_ID_NV,IPC_CREAT| |
| 39 | (0x1908+1341-0x1cc5));if(-(0x9d6+1345-0xf16)==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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 41 | ,errno);return-(0x2277+571-0x24b1);}if(-(0x5bb+488-0x7a2)!=msgctl(msgId,IPC_STAT |
| 42 | ,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0x2d9+8813-0x2545)==msgctl(msgId, |
| 43 | 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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 45 | ,errno);}if(nvDirInit()!=(0x238+6738-0x1c8a)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 47 | (0x4ca+5864-0x1bb1);}nvConfig();nvInit();while((0x20e2+894-0x245f)){if(- |
| 48 | (0x1790+885-0x1b04)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long), |
| 49 | MSG_TYPE_NV,(0x2fd+3597-0x110a))){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 51 | ,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0x1033+2951-0x1bb9)==msgsnd( |
| 52 | msgId,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0x59d+8029-0x24fa))){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 54 | ,errno);continue;}}return((0x13d9+4251-0x2474));}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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 59 | d_name,&statbuf);if(ret<(0xf03+681-0x11ac)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 62 | strcmp("\x2e",entry->d_name)==(0x10e3+5248-0x2563)||strcmp("\x2e\x2e",entry-> |
| 63 | d_name)==(0x1eda+849-0x222b))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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 67 | val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x1b4f+31-0x1b6e)};fp=fopen( |
xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 71 | (0x1e1a+2046-0x2618)]=='\n'||buf[(0x432+5709-0x1a7f)]== |
| 72 | ((char)(0xbdc+5966-0x2307)))continue;val=strchr(buf,((char)(0x53+1678-0x6a4))); |
| 73 | 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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 75 | ,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0x1576+348-0x16d1)]='\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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 79 | NV_FS_PATH,F_OK)!=(0x1156+227-0x1239)){if(mkdir(NV_FS_PATH,(0x696+534-0x6bf))!= |
| 80 | (0x480+7802-0x22fa)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 82 | ,NV_FS_PATH,errno);return-(0xa17+7295-0x2695);}if(mkdir(NV_FS_MAIN_PATH, |
| 83 | (0x7f4+1760-0xce7))!=(0x1591+1974-0x1d47)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 85 | ,NV_FS_MAIN_PATH,errno);return-(0xd66+2657-0x17c6);}if(mkdir(NV_FS_BACKUP_PATH, |
| 86 | (0x91f+967-0xaf9))!=(0x14d+5668-0x1771)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 88 | ,NV_FS_BACKUP_PATH,errno);return-(0x9f9+3433-0x1761);}}else{if(access( |
| 89 | NV_FS_MAIN_PATH,F_OK)!=(0x134c+423-0x14f3)){if(mkdir(NV_FS_MAIN_PATH, |
| 90 | (0x2514+5-0x232c))!=(0x175+7655-0x1f5c)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 92 | ,NV_FS_MAIN_PATH,errno);return-(0xd27+5868-0x2412);}}if(access(NV_FS_BACKUP_PATH |
| 93 | ,F_OK)!=(0x13b7+3985-0x2348)){if(mkdir(NV_FS_BACKUP_PATH,(0xbaa+5689-0x1ff6))!= |
| 94 | (0x1573+1805-0x1c80)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 96 | ,NV_FS_BACKUP_PATH,errno);return-(0x6e1+2050-0xee2);}}}return(0xb8d+491-0xd78);} |
| 97 | static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[NV_PATH_LEN]={ |
| 98 | (0x8bd+7048-0x2445)};char nvBackupFile[NV_PATH_LEN]={(0xfe3+4840-0x22cb)}; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 99 | #ifdef FOTA_AB |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 100 | T_FLAGS_INFO flags_info={(0x7ac+3792-0x167c)};int ret=(0x51f+46-0x54d); |
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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 108 | if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0x10c+4463-0x1279)){ |
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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 112 | (0x1f09+1628-0x2565);ret=flags_set(&flags_info); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 113 | #endif |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 114 | }}}uint hash(const char*s){uint hash=(0x4e8+1196-0x994);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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 117 | (0x7ca+6531-0x214d)};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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 121 | NV_MAX_ITEM_LEN,fp)){if(buf[(0x1c6b+147-0x1cfe)]=='\n'||buf[(0x89a+6841-0x2353)] |
| 122 | ==((char)(0xe9c+5504-0x23f9)))continue;val=strchr(buf, |
| 123 | ((char)(0x1ba1+1225-0x202d)));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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 125 | ,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x14f+5205-0x15a3)]=='\n' |
| 126 | )buf[strlen(buf)-(0xe2b+6073-0x25e3)]='\0';*val++='\0';nvset(list->nvFile,buf, |
| 127 | val,(0x7cf+5713-0x1e1f));}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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 130 | char*file){int len=(0x994+480-0xb74);int cnt=(0xad4+3715-0x1957);FILE*fp=NULL; |
| 131 | char*buf=NULL;struct stat statbuff={(0x4b9+5506-0x1a3b)};if(stat(file,&statbuff) |
| 132 | <(0x4f9+8331-0x2584))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE) |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 133 | return false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 134 | );if(!buf){fclose(fp);return false;}cnt=(0x24b+3316-0xf3f);while(cnt<len){cnt= |
| 135 | cnt+fread(buf+cnt,(0x753+2474-0x10fc),len-cnt,fp);if(ferror(fp)){clearerr(fp); |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 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; |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 140 | char buf[(0x1cfd+5586-0x22cf)];ssize_t nread;int ret=-(0x14e4+1342-0x1a21); |
| 141 | fd_from=open(from,O_RDONLY);if(fd_from<(0x80a+1267-0xcfd))return- |
| 142 | (0xd49+6342-0x260d);fd_to=open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC, |
| 143 | (0x11b9+1014-0x140f));if(fd_to<(0x5a4+5838-0x1c72)){ret=-(0x78f+5005-0x1b19); |
| 144 | goto out_error;}while((0x1805+1563-0x1e1f)){char*out_ptr;ssize_t nwritten;nread= |
| 145 | read(fd_from,buf,sizeof(buf));if(nread==(0x185d+165-0x1902)){break;}else{if( |
| 146 | nread<(0x77c+3842-0x167e)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=- |
| 147 | (0x86f+7027-0x23de);goto out_error;}}}out_ptr=buf;do{nwritten=write(fd_to, |
| 148 | out_ptr,nread);if(nwritten>(0x1447+1652-0x1abb)){nread-=nwritten;out_ptr+= |
| 149 | nwritten;}else{if(nwritten<(0x8e1+3335-0x15e8)){if(errno==EINTR||errno==EAGAIN){ |
| 150 | continue;}else{ret=-(0x1c55+1217-0x2111);goto out_error;}}}}while(nread> |
| 151 | (0x383+5266-0x1815));}ret=fsync(fd_to);if(ret<(0x845+1310-0xd63)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 153 | ,strerror(errno),to);goto out_error;}if(close(fd_to)<(0xd07+2168-0x157f)){fd_to= |
| 154 | -(0xb90+3308-0x187b);ret=-(0x7f+5964-0x17c5);goto out_error;}close(fd_from); |
| 155 | return(0xf04+228-0xfe8);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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 157 | ,from,to,ret);close(fd_from);if(fd_to>=(0x1229+3095-0x1e40))close(fd_to);return |
| 158 | ret;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile, |
| 159 | dstFile)!=(0xe85+5500-0x2401))return RESULT_FAIL;return RESULT_SUCCESS;}static |
| 160 | int loadNvFs(char*file){int len=(0x39a+5632-0x199a);int cnt=(0xfcb+3355-0x1ce6); |
| 161 | FILE*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct |
| 162 | stat statbuff={(0x439+8009-0x2382)};char nvFile[NV_PATH_LEN]={ |
| 163 | (0x15e9+1690-0x1c83)};sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file |
| 164 | );if(stat(nvFile,&statbuff)<(0xb3f+2843-0x165a))return RESULT_FAIL;len=statbuff. |
| 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+ |
| 167 | (0x126d+230-0x1352));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf, |
| 168 | (0x10a+6361-0x19e3),len+(0x17a1+3278-0x246e));cnt=(0x8f+4690-0x12e1);while(cnt< |
| 169 | len){cnt=cnt+fread(buf+cnt,(0xa7d+3693-0x18e9),len-cnt,fp);if(ferror(fp)){ |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 172 | *name){if(!(eq=strchr(name,((char)(0x1a41+2496-0x23c4))))){break;}*eq='\0';value |
| 173 | =eq+(0xf+4560-0x11de);nvset(file,name,value,(0xf40+4852-0x2233));*eq= |
| 174 | ((char)(0xe2+8910-0x2373));name=value+strlen(value)+(0x1de0+2069-0x25f4);}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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 190 | configFile,configFile,NV_PATH_LEN-(0x1e39+1636-0x249c));newConfig->configFile[ |
| 191 | NV_PATH_LEN-(0x5c8+7085-0x2174)]='\0';newConfig->next=NULL;}for(list=nv_list; |
| 192 | list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x6e9+6638-0x20d7))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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 195 | newList->nvFile,nvFile,NV_PATH_LEN-(0x3d5+3633-0x1205));newList->nvFile[ |
| 196 | NV_PATH_LEN-(0xe8+1092-0x52b)]='\0';memset(newList->nvTable,(0x1f8+616-0x460), |
| 197 | NV_HASH_LEN*(0xba4+3437-0x190d));newList->fileList=newConfig;if(!nv_list)nv_list |
xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [diff] [blame] | 198 | =newList;else{newList->next=nv_list->next;nv_list->next=newList;}}else if(!list |
| 199 | ->fileList)list->fileList=newConfig;else{if(newConfig==NULL)return RESULT_FAIL; |
| 200 | newConfig->next=list->fileList->next;list->fileList->next=newConfig;}return |
| 201 | RESULT_SUCCESS;}static bool isCfgConfiged(char*configFile){T_NV_NODE*list=NULL; |
| 202 | T_NV_CONFIG*config=NULL;for(list=nv_list;list;list=list->next){for(config=list-> |
| 203 | fileList;config;config=config->next){if(!strcmp(config->configFile,configFile)) |
| 204 | return true;}}return false;}static bool isNvConfiged(char*nvFile){T_NV_NODE*list |
| 205 | =NULL;for(list=nv_list;list;list=list->next){if(!strcmp(list->nvFile,nvFile)) |
| 206 | return true;}return false;}static uint getSum(const char*s,int len){uint sum= |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 207 | (0x165+6562-0x1b07);char*data=(char*)s;while(len-- >(0x13a+7369-0x1e03)){sum+=(* |
xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [diff] [blame] | 208 | data++);}return sum;}static int saveNvFs(char*nvName,char*nvFile){int i= |
xf.li | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 209 | (0x50f+5021-0x18ac);int sum=(0x225+3552-0x1005);int bufSize=(0xd6c+6179-0x258f); |
| 210 | int itemSize=(0x715+5925-0x1e3a);int ret=(0xf6+8335-0x2185);int fp= |
| 211 | (0x529+1968-0xcd9);char*buf=NULL;T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for( |
| 212 | list=nv_list;list;list=list->next){if(strcmp(list->nvFile,nvName))continue;fp= |
| 213 | open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC,(0xf5d+1815-0x14d4));if(fp==- |
| 214 | (0x99b+3934-0x18f8)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 217 | (!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0x22a+7316-0x1ebe);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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 220 | item->value)+(0x42c+5383-0x1931);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp, |
| 221 | buf,bufSize)<(0x455+2058-0xc5f)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 224 | +=getSum(buf,bufSize);bufSize=(0x15cd+3700-0x2441);}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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 226 | (0x17c3+3734-0x2659)){if(write(fp,buf,bufSize)<(0x5a9+1704-0xc51)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 229 | bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x1c3c+259-0x1d3f)) |
xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 233 | buf);close(fp);if(ret<(0x368+3154-0xfba)){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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 237 | (0x1774+191-0x1833);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list; |
xf.li | f1aed28 | 2024-02-06 00:31:51 -0800 | [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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 240 | key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0x2034+731-0x230e)); |
| 241 | value[NV_MAX_VAL_LEN-(0x60b+4377-0x1723)]='\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 | 7ccf837 | 2024-03-07 00:08:02 -0800 | [diff] [blame] | 243 | saveFlag){int index=(0x2ec+952-0x6a4);int ret=(0x34c+3613-0x1169);int |
| 244 | key_buf_len=(0x6+576-0x246);int value_buf_len=(0xbfb+5526-0x2191);T_NV_NODE*list |
| 245 | =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)+(0xdec+2218-0x1695);value_buf_len= |
| 247 | strlen(value)+(0x1cbb+947-0x206d);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 | (0xcca+2556-0x16c5));item->value[value_buf_len-(0x16e3+3168-0x2342)]='\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( |
| 257 | value_buf_len);if(!newItem->value){free(newItem->key);free(newItem);return |
| 258 | RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len-(0x677+2186-0xf00)); |
| 259 | newItem->key[key_buf_len-(0xc67+6276-0x24ea)]='\0';strncpy(newItem->value,value, |
| 260 | value_buf_len-(0x19af+2829-0x24bb));newItem->value[value_buf_len- |
| 261 | (0x59+2690-0xada)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem-> |
| 262 | update_flag=(0x216+3937-0x1177);if(!list->nvTable[index])list->nvTable[index]= |
| 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 |
| 266 | nvunset(char*file,char*key){int index=(0x209c+1544-0x26a4);T_NV_NODE*list=NULL; |
| 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 |
| 272 | RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0x5a2+6070-0x1d58); |
| 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); |
| 276 | }return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0x2c2+3286-0xf98); |
| 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=(0x11+8410-0x20eb) |
| 279 | ;i<NV_HASH_LEN;i++){for(item=list->nvTable[i];item;){cur=item;item=item->next; |
| 280 | free(cur->key);free(cur->value);free(cur);}list->nvTable[i]=NULL;}return |
| 281 | RESULT_SUCCESS;}return RESULT_NO_FILE;}static int nvcommit(char*file){int ret= |
| 282 | (0x514+7252-0x2168);char nvMainFile[NV_PATH_LEN]={(0x20f9+1461-0x26ae)};char |
| 283 | nvBackupFile[NV_PATH_LEN]={(0x1cca+1765-0x23af)};sprintf(nvMainFile, |
| 284 | "\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf(nvBackupFile, |
| 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 | |