blob: 101a49972331e086ec8ca263e006083b69b748f5 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001
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.li6c8fc1e2023-08-12 00:11:09 -070017#include "pub_flags.h"
18#include "flags_api.h"
lh9ed821d2023-04-07 01:36:19 -070019#endif
20#ifdef __cplusplus
21extern"C"{
22#endif
23static void nvConfig();static void nvInit();static int nvDirInit();static void
24analyMsg(T_NV_MSG_INFO*msgrecv,T_NV_MSG_RESULT*msgsnd);static bool checkNvFs(
25char*file);static bool isCfgConfiged(char*configFile);static bool isNvConfiged(
26char*configFile);static uint getSum(const char*s,int len);static int
27loadFactroyParam(T_NV_NODE*list);static int restoreNvFs(char*dstFile,char*
28srcFile);static int loadNvFs(char*file);static int addConfigFile(char*nvFile,
29char*configFile);static int saveNvFs(char*nvName,char*nvFile);static int nvset(
30char*file,const char*key,const char*value,int saveFlag);static int nvget(char*
31file,char*key,char*value);static int nvunset(char*file,char*key);static int
32nvclear(char*file);static int nvreset(char*file);static int nvcommit(char*file);
33T_NV_NODE*nv_list;int nvserver_main(int argc,char*argv[]){int msgId=
xf.li0f7a0362024-09-30 02:46:48 -070034(0xe7a+2066-0x168c);T_NV_MSG_INFO rcvBuf;T_NV_MSG_RESULT sndBuf;struct msqid_ds
35msgInfo;prctl(PR_SET_NAME,"\x6e\x76\x73\x65\x72\x76\x65\x72",(0x721+7163-0x231c)
36,(0x77a+1071-0xba9),(0x22b+6826-0x1cd5));memset(&rcvBuf,(0x12dd+715-0x15a8),
37sizeof(rcvBuf));memset(&sndBuf,(0xa8a+3067-0x1685),sizeof(sndBuf));memset(&
38msgInfo,(0xdcf+2206-0x166d),sizeof(msgInfo));msgId=msgget(MODULE_ID_NV,IPC_CREAT
39|(0xae7+5318-0x1e2d));if(-(0x1755+3387-0x248f)==msgId){printf(
lh9ed821d2023-04-07 01:36:19 -070040"\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.li0f7a0362024-09-30 02:46:48 -070041,errno);return-(0x151f+3866-0x2438);}if(-(0x694+8073-0x261c)!=msgctl(msgId,
42IPC_STAT,&msgInfo)){msgInfo.msg_qbytes=262144;if(-(0x6e0+5543-0x1c86)==msgctl(
xf.li84027492024-04-09 00:17:51 -070043msgId,IPC_SET,&msgInfo))printf(
lh9ed821d2023-04-07 01:36:19 -070044"\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.li0f7a0362024-09-30 02:46:48 -070045,errno);}if(nvDirInit()!=(0x1916+2322-0x2228)){printf(
lh9ed821d2023-04-07 01:36:19 -070046"\x6e\x76\x44\x69\x72\x49\x6e\x69\x74\x20\x66\x61\x69\x6c\x65\x21" "\n");return-
xf.li0f7a0362024-09-30 02:46:48 -070047(0x6d7+7153-0x22c7);}nvConfig();nvInit();while((0x45f+958-0x81c)){if(-
48(0x301+3995-0x129b)==msgrcv(msgId,&rcvBuf,sizeof(T_NV_MSG_INFO)-sizeof(long),
49MSG_TYPE_NV,(0xc26+3801-0x1aff))){printf(
lh9ed821d2023-04-07 01:36:19 -070050"\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.li0f7a0362024-09-30 02:46:48 -070051,errno);continue;}analyMsg(&rcvBuf,&sndBuf);if(-(0x6c0+5642-0x1cc9)==msgsnd(
52msgId,&sndBuf,sizeof(T_NV_MSG_RESULT)-sizeof(long),(0x57f+3769-0x1438))){printf(
lh9ed821d2023-04-07 01:36:19 -070053"\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.li0f7a0362024-09-30 02:46:48 -070054,errno);continue;}}return((0x10c9+920-0x1461));}static void configdir(char*dir){
55DIR*dp;int ret;struct dirent*entry;struct stat statbuf;if((dp=opendir(dir))==
lh9ed821d2023-04-07 01:36:19 -070056NULL){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.li0f7a0362024-09-30 02:46:48 -070059d_name,&statbuf);if(ret<(0x93f+3551-0x171e)){fprintf(stderr,
lh9ed821d2023-04-07 01:36:19 -070060"\x6c\x73\x74\x61\x74\x20\x65\x72\x72\x6f\x72\x3a\x20\x25\x73" "\n",strerror(
61errno));chdir("\x2e\x2e");closedir(dp);return;}if(!S_ISDIR(statbuf.st_mode)){if(
xf.li0f7a0362024-09-30 02:46:48 -070062strcmp("\x2e",entry->d_name)==(0x82d+7150-0x241b)||strcmp("\x2e\x2e",entry->
63d_name)==(0xae0+824-0xe18))continue;if(!isNvConfiged(entry->d_name)){if(
lh9ed821d2023-04-07 01:36:19 -070064addConfigFile(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.li0f7a0362024-09-30 02:46:48 -070067val=NULL;FILE*fp=NULL;char buf[NV_MAX_CONFIG_LEN]={(0x510+4322-0x15f2)};fp=fopen
xf.li84027492024-04-09 00:17:51 -070068(NV_CONFIG_FILE,"\x72\x6f");if(!fp){printf(
lh9ed821d2023-04-07 01:36:19 -070069"\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.li0f7a0362024-09-30 02:46:48 -070071(0x84f+7727-0x267e)]=='\n'||buf[(0xf2a+4513-0x20cb)]==
72((char)(0x601+4850-0x18d0)))continue;val=strchr(buf,((char)(0xf85+3266-0x1c0a)))
73;if(!val){printf(
lh9ed821d2023-04-07 01:36:19 -070074"\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.li0f7a0362024-09-30 02:46:48 -070075,NV_CONFIG_FILE,buf);continue;}buf[strlen(buf)-(0xb39+4270-0x1be6)]='\0';*val++=
xf.libe704612024-05-28 19:09:12 -070076'\0';if(!isCfgConfiged(buf)){if(addConfigFile(val,buf)!=RESULT_SUCCESS)printf(
lh9ed821d2023-04-07 01:36:19 -070077"\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.li0f7a0362024-09-30 02:46:48 -070079NV_FS_PATH,F_OK)!=(0x1ca9+1419-0x2234)){if(mkdir(NV_FS_PATH,(0x29d+142-0x13e))!=
80(0x1e29+652-0x20b5)){printf(
lh9ed821d2023-04-07 01:36:19 -070081"\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.li0f7a0362024-09-30 02:46:48 -070082,NV_FS_PATH,errno);return-(0x194d+264-0x1a54);}if(mkdir(NV_FS_MAIN_PATH,
83(0x249a+1070-0x26db))!=(0x14f+2741-0xc04)){printf(
lh9ed821d2023-04-07 01:36:19 -070084"\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.li0f7a0362024-09-30 02:46:48 -070085,NV_FS_MAIN_PATH,errno);return-(0xd8f+5331-0x2261);}if(mkdir(NV_FS_BACKUP_PATH,
86(0x1c92+1041-0x1eb6))!=(0x122c+168-0x12d4)){printf(
lh9ed821d2023-04-07 01:36:19 -070087"\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.li0f7a0362024-09-30 02:46:48 -070088,NV_FS_BACKUP_PATH,errno);return-(0xa12+30-0xa2f);}}else{if(access(
89NV_FS_MAIN_PATH,F_OK)!=(0x14e8+1394-0x1a5a)){if(mkdir(NV_FS_MAIN_PATH,
90(0x1ff7+2203-0x26a5))!=(0x23eb+788-0x26ff)){printf(
lh9ed821d2023-04-07 01:36:19 -070091"\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.li0f7a0362024-09-30 02:46:48 -070092,NV_FS_MAIN_PATH,errno);return-(0xc98+1801-0x13a0);}}if(access(NV_FS_BACKUP_PATH
93,F_OK)!=(0x3b7+6332-0x1c73)){if(mkdir(NV_FS_BACKUP_PATH,(0xd77+6373-0x246f))!=
94(0x1557+2974-0x20f5)){printf(
lh9ed821d2023-04-07 01:36:19 -070095"\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.li0f7a0362024-09-30 02:46:48 -070096,NV_FS_BACKUP_PATH,errno);return-(0x73b+7107-0x22fd);}}}return
97(0x10c+5360-0x15fc);}static void nvInit(){T_NV_NODE*list=NULL;char nvMainFile[
98NV_PATH_LEN]={(0x471+321-0x5b2)};char nvBackupFile[NV_PATH_LEN]={
99(0xdbb+722-0x108d)};
lh9ed821d2023-04-07 01:36:19 -0700100#ifdef FOTA_AB
xf.li0f7a0362024-09-30 02:46:48 -0700101T_FLAGS_INFO flags_info={(0xc88+3690-0x1af2)};int ret=(0x2+587-0x24d);
lh9ed821d2023-04-07 01:36:19 -0700102#endif
xf.li6c8fc1e2023-08-12 00:11:09 -0700103for(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,
105NV_PATH_LEN,"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,list->nvFile);if(checkNvFs(
106nvMainFile)){if(!checkNvFs(nvBackupFile))restoreNvFs(nvBackupFile,nvMainFile);}
107else if(checkNvFs(nvBackupFile)){restoreNvFs(nvMainFile,nvBackupFile);}else{
108loadFactroyParam(list);nvcommit(list->nvFile);continue;}loadNvFs(list->nvFile);
xf.li0f7a0362024-09-30 02:46:48 -0700109if(!strcmp(list->nvFile,NV_CFG)&&get_update_status()==(0x89+9653-0x263c)){
xf.li6c8fc1e2023-08-12 00:11:09 -0700110reloadFactroyParam(list);delete_not_needed(list);nvcommit(list->nvFile);
111#ifdef FOTA_AB
112ret=flags_get(&flags_info);flags_info.boot_fota_flag.fota_status=
xf.li0f7a0362024-09-30 02:46:48 -0700113(0xfc6+4638-0x21e4);ret=flags_set(&flags_info);
xf.li6c8fc1e2023-08-12 00:11:09 -0700114#endif
xf.li0f7a0362024-09-30 02:46:48 -0700115}}}uint hash(const char*s){uint hash=(0x537+6036-0x1ccb);while(*s){hash=
xf.lie31de8b2023-12-26 23:38:58 -0800116NV_HASH_MUL*hash+*s++;}return hash;}static int loadFactroyParam(T_NV_NODE*list){
117char*val=NULL;FILE*fp=NULL;T_NV_CONFIG*config=NULL;char buf[NV_MAX_ITEM_LEN]={
xf.li0f7a0362024-09-30 02:46:48 -0700118(0x1d8+9324-0x2644)};for(config=list->fileList;config;config=config->next){fp=
lh9ed821d2023-04-07 01:36:19 -0700119fopen(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.li0f7a0362024-09-30 02:46:48 -0700122NV_MAX_ITEM_LEN,fp)){if(buf[(0x1277+4187-0x22d2)]=='\n'||buf[(0x6ff+8141-0x26cc)
123]==((char)(0xd0a+3970-0x1c69)))continue;val=strchr(buf,
124((char)(0x1486+1381-0x19ae)));if(!val){printf(
lh9ed821d2023-04-07 01:36:19 -0700125"\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.li0f7a0362024-09-30 02:46:48 -0700126,config->configFile,buf);continue;}if(buf[strlen(buf)-(0x75c+239-0x84a)]=='\n')
127buf[strlen(buf)-(0x104+6728-0x1b4b)]='\0';*val++='\0';nvset(list->nvFile,buf,val
128,(0x134a+1615-0x1998));}printf(
lh9ed821d2023-04-07 01:36:19 -0700129"\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.li0f7a0362024-09-30 02:46:48 -0700131char*file){int len=(0xb72+5012-0x1f06);int cnt=(0x354+6751-0x1db3);FILE*fp=NULL;
132char*buf=NULL;struct stat statbuff={(0xdb8+3546-0x1b92)};if(stat(file,&statbuff)
133<(0x244+1296-0x754))return false;len=statbuff.st_size;if(len<NV_CHECK_SIZE)
134return false;fp=fopen(file,"\x72\x6f");if(!fp)return false;buf=(char*)malloc(len
135);if(!buf){fclose(fp);return false;}cnt=(0x2075+1098-0x24bf);while(cnt<len){cnt=
136cnt+fread(buf+cnt,(0xc34+187-0xcee),len-cnt,fp);if(ferror(fp)){clearerr(fp);free
137(buf);fclose(fp);return false;}}if(len!=cnt){free(buf);fclose(fp);return false;}
138if(getSum(buf,len-NV_CHECK_SIZE)+NV_FILE_FLAG!=*(uint*)(buf+len-NV_CHECK_SIZE)){
139free(buf);fclose(fp);return false;}free(buf);fclose(fp);return true;}static int
140copyfile(const char*from,const char*to){int fd_to;int fd_from;char buf[
141(0x12b8+6770-0x1d2a)];ssize_t nread;int ret=-(0x480+3392-0x11bf);fd_from=open(
142from,O_RDONLY);if(fd_from<(0x70+5260-0x14fc))return-(0x15ec+4222-0x2668);fd_to=
143open(to,O_RDWR|O_CREAT|O_TRUNC|O_SYNC,(0xb21+4555-0x1b4c));if(fd_to<
144(0x795+6002-0x1f07)){ret=-(0x1562+3795-0x2432);goto out_error;}while(
145(0x434+8843-0x26be)){char*out_ptr;ssize_t nwritten;nread=read(fd_from,buf,sizeof
146(buf));if(nread==(0x6d5+8193-0x26d6)){break;}else{if(nread<(0x7bb+3192-0x1433)){
147if(errno==EINTR||errno==EAGAIN){continue;}else{ret=-(0x1588+717-0x1851);goto
148out_error;}}}out_ptr=buf;do{nwritten=write(fd_to,out_ptr,nread);if(nwritten>
149(0x9aa+1698-0x104c)){nread-=nwritten;out_ptr+=nwritten;}else{if(nwritten<
150(0xe7d+77-0xeca)){if(errno==EINTR||errno==EAGAIN){continue;}else{ret=-
151(0xc34+1310-0x114d);goto out_error;}}}}while(nread>(0x726+5923-0x1e49));}ret=
152fsync(fd_to);if(ret<(0x48f+5872-0x1b7f)){printf(
lh9ed821d2023-04-07 01:36:19 -0700153"\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.li0f7a0362024-09-30 02:46:48 -0700154,strerror(errno),to);goto out_error;}if(close(fd_to)<(0x1306+2532-0x1cea)){fd_to
155=-(0x10e4+4316-0x21bf);ret=-(0x2df+1211-0x794);goto out_error;}close(fd_from);
156return(0x1cd4+1409-0x2255);out_error:printf(
lh9ed821d2023-04-07 01:36:19 -0700157"\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.li0f7a0362024-09-30 02:46:48 -0700158,from,to,ret);close(fd_from);if(fd_to>=(0xc25+185-0xcde))close(fd_to);return ret
159;}static int restoreNvFs(char*dstFile,char*srcFile){if(copyfile(srcFile,dstFile)
160!=(0xf14+2971-0x1aaf))return RESULT_FAIL;return RESULT_SUCCESS;}static int
161loadNvFs(char*file){int len=(0x2dc+4597-0x14d1);int cnt=(0x2d4+9022-0x2612);FILE
162*fp=NULL;char*buf=NULL;char*name=NULL;char*value=NULL;char*eq=NULL;struct stat
163statbuff={(0xbf6+3098-0x1810)};char nvFile[NV_PATH_LEN]={(0x1971+3000-0x2529)};
xf.lidf7f8ba2024-09-12 23:53:34 -0700164sprintf(nvFile,"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);if(stat(nvFile,&
xf.li0f7a0362024-09-30 02:46:48 -0700165statbuff)<(0x762+4454-0x18c8))return RESULT_FAIL;len=statbuff.st_size;if(
xf.lidf7f8ba2024-09-12 23:53:34 -0700166NV_CHECK_SIZE>len)return RESULT_FAIL;fp=fopen(nvFile,"\x72\x6f");if(!fp)return
167RESULT_FILE_OPEN_FAIL;len=len-NV_CHECK_SIZE;buf=(char*)malloc(len+
xf.li0f7a0362024-09-30 02:46:48 -0700168(0x1ba2+1756-0x227d));if(!buf){fclose(fp);return RESULT_MALLOC_FAIL;}memset(buf,
169(0xc11+5775-0x22a0),len+(0xf89+3474-0x1d1a));cnt=(0xa33+4352-0x1b33);while(cnt<
170len){cnt=cnt+fread(buf+cnt,(0x1477+4008-0x241e),len-cnt,fp);if(ferror(fp)){
xf.lif2330622024-05-15 18:17:18 -0700171clearerr(fp);fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}}if(cnt!=len){
172fclose(fp);free(buf);return RESULT_FILE_READ_FAIL;}buf[len]='\0';name=buf;while(
xf.li0f7a0362024-09-30 02:46:48 -0700173*name){if(!(eq=strchr(name,((char)(0x2277+449-0x23fb))))){break;}*eq='\0';value=
174eq+(0x1898+637-0x1b14);nvset(file,name,value,(0x14cc+904-0x1853));*eq=
175((char)(0x323+4632-0x14fe));name=value+strlen(value)+(0x383+4424-0x14ca);}free(
176buf);fclose(fp);return RESULT_SUCCESS;}static void analyMsg(T_NV_MSG_INFO*
177msgrecv,T_NV_MSG_RESULT*msgsnd){switch(msgrecv->nvType){case MSG_GET:msgsnd->
178result=nvget(msgrecv->file,msgrecv->key,msgsnd->value);break;case MSG_SET:msgsnd
179->result=nvset(msgrecv->file,msgrecv->key,msgrecv->value,msgrecv->saveflag);
180break;case MSG_UNSET:msgsnd->result=nvunset(msgrecv->file,msgrecv->key);break;
181case MSG_CLEAR:msgsnd->result=nvclear(msgrecv->file);break;case MSG_RESET:msgsnd
182->result=nvreset(msgrecv->file);break;case MSG_SHOW:msgsnd->result=saveNvFs(
lh9ed821d2023-04-07 01:36:19 -0700183msgrecv->file,NV_FS_SHOW);;break;case MSG_COMMIT:msgsnd->result=nvcommit(msgrecv
184->file);break;default:break;}msgsnd->msgType=msgrecv->pid;msgsnd->msgIndex=
185msgrecv->msgIndex;}static int addConfigFile(char*nvFile,char*configFile){
186T_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(
190T_NV_CONFIG));if(!newConfig)return RESULT_MALLOC_FAIL;strncpy(newConfig->
xf.li0f7a0362024-09-30 02:46:48 -0700191configFile,configFile,NV_PATH_LEN-(0x3d9+364-0x544));newConfig->configFile[
192NV_PATH_LEN-(0x17d1+2559-0x21cf)]='\0';newConfig->next=NULL;}for(list=nv_list;
193list;list=list->next){if(strcmp(list->nvFile,nvFile)==(0x1e00+1484-0x23cc))break
194;}if(!list){newList=(T_NV_NODE*)malloc(sizeof(T_NV_NODE));if(!newList){if(
195newConfig)free(newConfig);return RESULT_MALLOC_FAIL;}newList->next=NULL;strncpy(
196newList->nvFile,nvFile,NV_PATH_LEN-(0x324+1980-0xadf));newList->nvFile[
197NV_PATH_LEN-(0x955+3249-0x1605)]='\0';memset(newList->nvTable,
198(0xc52+4853-0x1f47),NV_HASH_LEN*(0x73a+6839-0x21ed));newList->fileList=newConfig
199;if(!nv_list)nv_list=newList;else{newList->next=nv_list->next;nv_list->next=
200newList;}}else if(!list->fileList)list->fileList=newConfig;else{if(newConfig==
201NULL)return RESULT_FAIL;newConfig->next=list->fileList->next;list->fileList->
202next=newConfig;}return RESULT_SUCCESS;}static bool isCfgConfiged(char*configFile
203){T_NV_NODE*list=NULL;T_NV_CONFIG*config=NULL;for(list=nv_list;list;list=list->
204next){for(config=list->fileList;config;config=config->next){if(!strcmp(config->
205configFile,configFile))return true;}}return false;}static bool isNvConfiged(char
206*nvFile){T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(!strcmp(
207list->nvFile,nvFile))return true;}return false;}static uint getSum(const char*s,
208int len){uint sum=(0x1ddf+172-0x1e8b);char*data=(char*)s;while(len-- >
209(0x549+1618-0xb9b)){sum+=(*data++);}return sum;}static int saveNvFs(char*nvName,
210char*nvFile){int i=(0x826+6755-0x2289);int sum=(0x752+3072-0x1352);int bufSize=
211(0x153+1357-0x6a0);int itemSize=(0x1b65+2062-0x2373);int ret=(0x93d+3433-0x16a6)
212;int fp=(0x454+794-0x76e);char*buf=NULL;T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;
213for(list=nv_list;list;list=list->next){if(strcmp(list->nvFile,nvName))continue;
214fp=open(nvFile,O_SYNC|O_RDWR|O_CREAT|O_TRUNC,(0x43c+8005-0x21e1));if(fp==-
215(0xa35+228-0xb18)){printf(
lh9ed821d2023-04-07 01:36:19 -0700216"\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.li0f7a0362024-09-30 02:46:48 -0700218(!buf){close(fp);return RESULT_MALLOC_FAIL;}for(i=(0xf16+2821-0x1a1b);i<
lh9ed821d2023-04-07 01:36:19 -0700219NV_HASH_LEN;i++){for(item=list->nvTable[i];item;item=item->next){if(strcmp(
220nvFile,NV_FS_SHOW)&&!item->saveFlag)continue;itemSize=strlen(item->key)+strlen(
xf.li0f7a0362024-09-30 02:46:48 -0700221item->value)+(0x13b4+2864-0x1ee2);if(bufSize+itemSize>NV_BLOCK_SIZE){if(write(fp
222,buf,bufSize)<(0x284+1627-0x8df)){printf(
lh9ed821d2023-04-07 01:36:19 -0700223"\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.li0f7a0362024-09-30 02:46:48 -0700225+=getSum(buf,bufSize);bufSize=(0x17b2+3190-0x2428);}sprintf(buf+bufSize,
lh9ed821d2023-04-07 01:36:19 -0700226"\x25\x73\x3d\x25\x73",item->key,item->value);bufSize+=itemSize;}}if(bufSize!=
xf.li0f7a0362024-09-30 02:46:48 -0700227(0x983+5619-0x1f76)){if(write(fp,buf,bufSize)<(0x3c2+4379-0x14dd)){printf(
lh9ed821d2023-04-07 01:36:19 -0700228"\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.li0f7a0362024-09-30 02:46:48 -0700230bufSize);}sum+=NV_FILE_FLAG;if(write(fp,&sum,NV_CHECK_SIZE)<(0x181b+3703-0x2692)
xf.lif2330622024-05-15 18:17:18 -0700231){printf(
lh9ed821d2023-04-07 01:36:19 -0700232"\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.li0f7a0362024-09-30 02:46:48 -0700234buf);close(fp);if(ret<(0x1837+246-0x192d)){printf(
lh9ed821d2023-04-07 01:36:19 -0700235"\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
237RESULT_NO_FILE;}static int nvget(char*file,char*key,char*value){int index=
xf.li0f7a0362024-09-30 02:46:48 -0700238(0x1d3+4764-0x146f);T_NV_NODE*list=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;
xf.lidf7f8ba2024-09-12 23:53:34 -0700239list;list=list->next){if(strcmp(list->nvFile,file))continue;index=hash(key)%
lh9ed821d2023-04-07 01:36:19 -0700240NV_HASH_LEN;for(item=list->nvTable[index];item;item=item->next){if(strcmp(item->
xf.li0f7a0362024-09-30 02:46:48 -0700241key,key))continue;strncpy(value,item->value,NV_MAX_VAL_LEN-(0xd5a+4074-0x1d43));
242value[NV_MAX_VAL_LEN-(0x9b3+2600-0x13da)]='\0';return RESULT_SUCCESS;}}return
lh9ed821d2023-04-07 01:36:19 -0700243RESULT_NO_ITEM;}static int nvset(char*file,const char*key,const char*value,int
xf.li0f7a0362024-09-30 02:46:48 -0700244saveFlag){int index=(0xd0c+552-0xf34);int ret=(0x8a5+3844-0x17a9);int
245key_buf_len=(0x10ea+785-0x13fb);int value_buf_len=(0x465+6781-0x1ee2);T_NV_NODE*
246list=NULL;T_NV_ITEM*item=NULL;T_NV_ITEM*newItem=NULL;if(NULL==key||NULL==value)
247return RESULT_FAIL;key_buf_len=strlen(key)+(0x4c8+3774-0x1385);value_buf_len=
248strlen(value)+(0xd44+3765-0x1bf8);for(list=nv_list;list;list=list->next){if(
249strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list->
250nvTable[index];item;item=item->next){if(strcmp(item->key,key))continue;if(
251saveFlag)item->saveFlag=saveFlag;if(!strcmp(item->value,value))return
252RESULT_SUCCESS;free(item->value);item->value=(char*)malloc(value_buf_len);if(!
253item->value)return RESULT_MALLOC_FAIL;strncpy(item->value,value,value_buf_len-
254(0x293+6898-0x1d84));item->value[value_buf_len-(0xdfb+2094-0x1628)]='\0';return
255RESULT_SUCCESS;}newItem=(T_NV_ITEM*)malloc(sizeof(T_NV_ITEM));if(!newItem)return
256 RESULT_MALLOC_FAIL;newItem->key=(char*)malloc(key_buf_len);if(!newItem->key){
257free(newItem);return RESULT_MALLOC_FAIL;}newItem->value=(char*)malloc(
258value_buf_len);if(!newItem->value){free(newItem->key);free(newItem);return
259RESULT_MALLOC_FAIL;}strncpy(newItem->key,key,key_buf_len-(0x68f+1599-0xccd));
260newItem->key[key_buf_len-(0x237+1412-0x7ba)]='\0';strncpy(newItem->value,value,
261value_buf_len-(0x10b8+4596-0x22ab));newItem->value[value_buf_len-
262(0x13a4+4644-0x25c7)]='\0';newItem->next=NULL;newItem->saveFlag=saveFlag;newItem
263->update_flag=(0xca1+6650-0x269b);if(!list->nvTable[index])list->nvTable[index]=
264newItem;else{newItem->next=list->nvTable[index]->next;list->nvTable[index]->next
265=newItem;}return RESULT_SUCCESS;}ret=addConfigFile(file,NULL);if(ret==
266RESULT_SUCCESS)return nvset(file,key,value,saveFlag);else return ret;}static int
267 nvunset(char*file,char*key){int index=(0x16f7+1158-0x1b7d);T_NV_NODE*list=NULL;
268T_NV_ITEM*item=NULL;T_NV_ITEM*prev=NULL;for(list=nv_list;list;list=list->next){
269if(strcmp(list->nvFile,file))continue;index=hash(key)%NV_HASH_LEN;for(item=list
270->nvTable[index];item;prev=item,item=item->next){if(strcmp(item->key,key))
271continue;if(!prev)list->nvTable[index]=item->next;else prev->next=item->next;
272free(item->key);free(item->value);free(item);return RESULT_SUCCESS;}}return
273RESULT_NO_ITEM;}static int nvreset(char*file){int ret=(0x7c0+7258-0x241a);
274T_NV_NODE*list=NULL;for(list=nv_list;list;list=list->next){if(strcmp(list->
xf.li7ccf8372024-03-07 00:08:02 -0800275nvFile,file))continue;ret=nvclear(file);if(ret!=RESULT_SUCCESS)return ret;if(
276loadFactroyParam(list)!=RESULT_SUCCESS)return RESULT_FAIL;return nvcommit(file);
xf.li0f7a0362024-09-30 02:46:48 -0700277}return RESULT_NO_FILE;}static int nvclear(char*file){int i=(0xdd0+17-0xde1);
xf.lidf7f8ba2024-09-12 23:53:34 -0700278T_NV_NODE*list=NULL;T_NV_ITEM*cur=NULL;T_NV_ITEM*item=NULL;for(list=nv_list;list
xf.li0f7a0362024-09-30 02:46:48 -0700279;list=list->next){if(strcmp(list->nvFile,file))continue;for(i=(0x104+605-0x361);
280i<NV_HASH_LEN;i++){for(item=list->nvTable[i];item;){cur=item;item=item->next;
281free(cur->key);free(cur->value);free(cur);}list->nvTable[i]=NULL;}return
282RESULT_SUCCESS;}return RESULT_NO_FILE;}static int nvcommit(char*file){int ret=
283(0x115c+3316-0x1e50);char nvMainFile[NV_PATH_LEN]={(0x21f+8420-0x2303)};char
284nvBackupFile[NV_PATH_LEN]={(0xdf0+4084-0x1de4)};sprintf(nvMainFile,
285"\x25\x73\x2f\x25\x73",NV_FS_MAIN_PATH,file);sprintf(nvBackupFile,
286"\x25\x73\x2f\x25\x73",NV_FS_BACKUP_PATH,file);ret=saveNvFs(file,nvMainFile);if(
287ret!=RESULT_SUCCESS)return ret;return restoreNvFs(nvBackupFile,nvMainFile);}
lh9ed821d2023-04-07 01:36:19 -0700288#ifdef __cplusplus
289}
290#endif
291