• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <ctype.h>
16 #include <errno.h>
17 
18 #include "param_manager.h"
19 #include "param_trie.h"
20 
LoadSecurityLabel(const char * fileName)21 static int LoadSecurityLabel(const char *fileName)
22 {
23     ParamWorkSpace *paramSpace = GetParamWorkSpace();
24     PARAM_CHECK(paramSpace != NULL, return -1, "Invalid paramSpace");
25     PARAM_WORKSPACE_CHECK(paramSpace, return -1, "Invalid space");
26     PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load");
27 #if !(defined __LITEOS_A__ || defined __LITEOS_M__)
28     // load security label
29     ParamSecurityOps *ops = GetParamSecurityOps(PARAM_SECURITY_DAC);
30     if (ops != NULL && ops->securityGetLabel != NULL) {
31         ops->securityGetLabel(fileName);
32     }
33 #endif
34     return 0;
35 }
36 
GetParamValueFromBuffer(const char * name,const char * buffer,char * value,int length)37 static int GetParamValueFromBuffer(const char *name, const char *buffer, char *value, int length)
38 {
39     size_t bootLen = strlen(OHOS_BOOT);
40     const char *tmpName = name + bootLen;
41     int ret = GetProcCmdlineValue(tmpName, buffer, value, length);
42     return ret;
43 }
44 
CommonDealFun(const char * name,const char * value,int res)45 static int CommonDealFun(const char *name, const char *value, int res)
46 {
47     int ret = 0;
48     if (res == 0) {
49         PARAM_LOGV("Add param from cmdline %s %s", name, value);
50         ret = CheckParamName(name, 0);
51         PARAM_CHECK(ret == 0, return ret, "Invalid param name %s", name);
52         PARAM_LOGV("Param name %s, value %s", name, value);
53         ret = WriteParam(name, value, NULL, 0);
54         PARAM_CHECK(ret == 0, return ret, "Failed to write param %s %s", name, value);
55     } else {
56         PARAM_LOGE("Get %s parameter value is null.", name);
57     }
58     return ret;
59 }
60 
ReadSnFromFile(const char * name,const char * file)61 static int ReadSnFromFile(const char *name, const char *file)
62 {
63     char *data = ReadFileData(file);
64     PARAM_CHECK(data != NULL, return -1, "Read sn from %s file failed!", file);
65 
66     int index = 0;
67     for (size_t i = 0; i < strlen(data); i++) {
68         // cancel \r\n
69         if (*(data + i) == '\r' || *(data + i) == '\n') {
70             break;
71         }
72         if (*(data + i) != ':') {
73             *(data + index) = *(data + i);
74             index++;
75         }
76     }
77     data[index] = '\0';
78     PARAM_LOGV("**** name %s, value %s", name, data);
79     int ret = WriteParam(name, data, NULL, 0);
80     free(data);
81     PARAM_CHECK(ret == 0, return ret, "Failed to write param %s", name);
82     return ret;
83 }
84 
SnDealFun(const char * name,const char * value,int res)85 static int SnDealFun(const char *name, const char *value, int res)
86 {
87     const char *snFileList [] = {
88         "/sys/block/mmcblk0/device/cid",
89         "/proc/bootdevice/cid"
90     };
91     int ret = CheckParamName(name, 0);
92     PARAM_CHECK(ret == 0, return ret, "Invalid name %s", name);
93     if (value != NULL && res == 0 && value[0] != '/') {
94         PARAM_LOGV("**** name %s, value %s", name, value);
95         ret = WriteParam(name, value, NULL, 0);
96         PARAM_CHECK(ret == 0, return ret, "Failed to write param %s %s", name, value);
97         return ret;
98     }
99     if (value != NULL && value[0] == '/') {
100         ret = ReadSnFromFile(name, value);
101         if (ret == 0) {
102             return ret;
103         }
104     }
105     for (size_t i = 0; i < ARRAY_LENGTH(snFileList); i++) {
106         ret = ReadSnFromFile(name, snFileList[i]);
107         if (ret == 0) {
108             break;
109         }
110     }
111     return ret;
112 }
113 
LoadParamFromCmdLine(void)114 INIT_LOCAL_API int LoadParamFromCmdLine(void)
115 {
116     static const cmdLineInfo cmdLines[] = {
117         {OHOS_BOOT"hardware", CommonDealFun
118         },
119         {OHOS_BOOT"bootgroup", CommonDealFun
120         },
121         {OHOS_BOOT"reboot_reason", CommonDealFun
122         },
123         {OHOS_BOOT"bootslots", CommonDealFun
124         },
125         {OHOS_BOOT"sn", SnDealFun
126         }
127     };
128     char *data = ReadFileData(BOOT_CMD_LINE);
129     PARAM_CHECK(data != NULL, return -1, "Failed to read file %s", BOOT_CMD_LINE);
130     char *value = calloc(1, PARAM_CONST_VALUE_LEN_MAX + 1);
131     PARAM_CHECK(value != NULL, free(data);
132         return -1, "Failed to read file %s", BOOT_CMD_LINE);
133 
134     for (size_t i = 0; i < ARRAY_LENGTH(cmdLines); i++) {
135         int ret = 0;
136 #ifdef BOOT_EXTENDED_CMDLINE
137         ret = GetParamValueFromBuffer(cmdLines[i].name, BOOT_EXTENDED_CMDLINE, value, PARAM_CONST_VALUE_LEN_MAX);
138         if (ret != 0) {
139             ret = GetParamValueFromBuffer(cmdLines[i].name, data, value, PARAM_CONST_VALUE_LEN_MAX);
140         }
141 #else
142         ret = GetParamValueFromBuffer(cmdLines[i].name, data, value, PARAM_CONST_VALUE_LEN_MAX);
143 #endif
144 
145         cmdLines[i].processor(cmdLines[i].name, value, ret);
146     }
147     PARAM_LOGV("Parse cmdline finish %s", BOOT_CMD_LINE);
148     free(data);
149     free(value);
150     return 0;
151 }
152 
LoadOneParam_(const uint32_t * context,const char * name,const char * value)153 static int LoadOneParam_(const uint32_t *context, const char *name, const char *value)
154 {
155     uint32_t mode = *(uint32_t *)context;
156     int ret = CheckParamName(name, 0);
157     if (ret != 0) {
158         return 0;
159     }
160     PARAM_LOGV("Add default parameter [%s] [%s]", name, value);
161     return WriteParam(name, value, NULL, mode & LOAD_PARAM_ONLY_ADD);
162 }
163 
LoadDefaultParam_(const char * fileName,uint32_t mode,const char * exclude[],uint32_t count,int (* loadOneParam)(const uint32_t *,const char *,const char *))164 static int LoadDefaultParam_(const char *fileName, uint32_t mode,
165     const char *exclude[], uint32_t count, int (*loadOneParam)(const uint32_t *, const char *, const char *))
166 {
167     uint32_t paramNum = 0;
168     FILE *fp = fopen(fileName, "r");
169     PARAM_CHECK(fp != NULL, return -1, "Failed to open file '%s' error:%d ", fileName, errno);
170 
171     const int buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10;  // 10 max len
172     char *buffer = malloc(buffSize);
173     if (buffer == NULL) {
174         (void)fclose(fp);
175         return -1;
176     }
177     while (fgets(buffer, buffSize, fp) != NULL) {
178         buffer[buffSize - 1] = '\0';
179         int ret = SplitParamString(buffer, exclude, count, loadOneParam, &mode);
180         PARAM_CHECK(ret == 0, continue, "Failed to set param '%s' error:%d ", buffer, ret);
181         paramNum++;
182     }
183     (void)fclose(fp);
184     free(buffer);
185     PARAM_LOGV("Load %u default parameters success from %s.", paramNum, fileName);
186     return 0;
187 }
188 
ProcessParamFile(const char * fileName,void * context)189 static int ProcessParamFile(const char *fileName, void *context)
190 {
191     static const char *exclude[] = {"ctl.", "selinux.restorecon_recursive"};
192     uint32_t mode = *(int *)context;
193     return LoadDefaultParam_(fileName, mode, exclude, ARRAY_LENGTH(exclude), LoadOneParam_);
194 }
195 
LoadParamsFile(const char * fileName,bool onlyAdd)196 int LoadParamsFile(const char *fileName, bool onlyAdd)
197 {
198     return LoadDefaultParams(fileName, onlyAdd ? LOAD_PARAM_ONLY_ADD : LOAD_PARAM_NORMAL);
199 }
200 
LoadDefaultParams(const char * fileName,uint32_t mode)201 int LoadDefaultParams(const char *fileName, uint32_t mode)
202 {
203     PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load");
204     PARAM_LOGI("Load default parameters from %s.", fileName);
205     struct stat st;
206     if ((stat(fileName, &st) == 0) && !S_ISDIR(st.st_mode)) {
207         (void)ProcessParamFile(fileName, &mode);
208     } else {
209         (void)ReadFileInDir(fileName, ".para", ProcessParamFile, &mode);
210     }
211 
212     // load security label
213     return LoadSecurityLabel(fileName);
214 }
215 
LoadParamFromBuild(void)216 INIT_LOCAL_API void LoadParamFromBuild(void)
217 {
218     PARAM_LOGI("load parameters from build ");
219 #ifdef INCREMENTAL_VERSION
220     if (strlen(INCREMENTAL_VERSION) > 0) {
221         WriteParam("const.product.incremental.version", INCREMENTAL_VERSION, NULL, LOAD_PARAM_NORMAL);
222     }
223 #endif
224 #ifdef BUILD_TYPE
225     if (strlen(BUILD_TYPE) > 0) {
226         WriteParam("const.product.build.type", BUILD_TYPE, NULL, LOAD_PARAM_NORMAL);
227     }
228 #endif
229 #ifdef BUILD_USER
230     if (strlen(BUILD_USER) > 0) {
231         WriteParam("const.product.build.user", BUILD_USER, NULL, LOAD_PARAM_NORMAL);
232     }
233 #endif
234 #ifdef BUILD_TIME
235     if (strlen(BUILD_TIME) > 0) {
236         WriteParam("const.product.build.date", BUILD_TIME, NULL, LOAD_PARAM_NORMAL);
237     }
238 #endif
239 #ifdef BUILD_HOST
240     if (strlen(BUILD_HOST) > 0) {
241         WriteParam("const.product.build.host", BUILD_HOST, NULL, LOAD_PARAM_NORMAL);
242     }
243 #endif
244 #ifdef BUILD_ROOTHASH
245     if (strlen(BUILD_ROOTHASH) > 0) {
246         WriteParam("const.ohos.buildroothash", BUILD_ROOTHASH, NULL, LOAD_PARAM_NORMAL);
247     }
248 #endif
249 }
250 
LoadOneParamAreaSize_(const uint32_t * context,const char * name,const char * value)251 static int LoadOneParamAreaSize_(const uint32_t *context, const char *name, const char *value)
252 {
253     int ret = CheckParamName(name, 0);
254     if (ret != 0) {
255         return 0;
256     }
257     ret = CheckParamValue(NULL, name, value, PARAM_TYPE_INT);
258     PARAM_CHECK(ret == 0, return 0, "Invalid value %s for %s", value, name);
259     PARAM_LOGV("LoadOneParamAreaSize_ [%s] [%s]", name, value);
260 
261     WorkSpace *workSpace = GetWorkSpace(WORKSPACE_NAME_DAC);
262     ParamTrieNode *node = AddTrieNode(workSpace, name, strlen(name));
263     PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX, "Failed to add node");
264     ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
265     if (entry == NULL) {
266         uint32_t offset = AddParamNode(workSpace, PARAM_TYPE_INT, name, strlen(name), value, strlen(value));
267         PARAM_CHECK(offset > 0, return PARAM_CODE_REACHED_MAX, "Failed to allocate name %s", name);
268         SaveIndex(&node->dataIndex, offset);
269     }
270     return 0;
271 }
272 
LoadParamAreaSize(void)273 INIT_LOCAL_API void LoadParamAreaSize(void)
274 {
275     LoadDefaultParam_(PARAM_AREA_SIZE_CFG, 0, NULL, 0, LoadOneParamAreaSize_);
276 }
277