1 /*
2  * Copyright (c) 2021 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 
16 #ifndef BASE_STARTUP_PARAM_UTILS_H
17 #define BASE_STARTUP_PARAM_UTILS_H
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include "beget_ext.h"
22 #include "init_param.h"
23 #include "init_utils.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 typedef enum {
32     PARAM_CODE_NOT_INIT = PARAM_CODE_MAX + 1,
33     PARAM_CODE_ERROR_MAP_FILE,
34 } PARAM_INNER_CODE;
35 
36 #ifndef PARAM_BUFFER_MAX
37 #define PARAM_BUFFER_MAX (0x01 << 16)
38 #endif
39 
40 #define FILENAME_LEN_MAX 255
41 #define MS_UNIT 1000
42 #ifndef UNUSED
43 #define UNUSED(x) (void)(x)
44 #endif
45 #define PARAM_ALIGN(len) (((len) + 0x03) & (~0x03))
46 #define PARAM_IS_ALIGNED(x) (((x) & 0x03) == 0)
47 #define PARAM_ENTRY(ptr, type, member) (type *)((char *)(ptr)-offsetof(type, member))
48 
49 #define IS_READY_ONLY(name) \
50     ((strncmp((name), "const.", strlen("const.")) == 0) ||  \
51     (strncmp((name), "ro.", strlen("ro.")) == 0) || \
52     (strncmp((name), "ohos.boot.", strlen("ohos.boot.")) == 0))
53 
54 #define PARAM_PERSIST_PREFIX "persist."
55 
56 #define SYS_POWER_CTRL "ohos.startup.powerctrl="
57 #define OHOS_CTRL_START "ohos.ctl.start="
58 #define OHOS_CTRL_STOP "ohos.ctl.stop="
59 #define OHOS_SERVICE_CTRL_PREFIX "ohos.servicectrl."
60 
61 #ifdef STARTUP_INIT_TEST
62 #define PARAM_STATIC
63 #else
64 #define PARAM_STATIC static
65 #endif
66 
67 #ifdef __LITEOS_M__
68 #ifndef DATA_PATH
69 #define DATA_PATH          "/"
70 #endif
71 #elif defined __LITEOS_A__
72 #define DATA_PATH          STARTUP_INIT_UT_PATH"/storage/data/system/param/"
73 #elif defined __LINUX__
74 #define DATA_PATH          STARTUP_INIT_UT_PATH"/storage/data/system/param/"
75 #else
76 #define DATA_PATH          STARTUP_INIT_UT_PATH"/data/service/el1/startup/parameters/"
77 #endif
78 #define PARAM_AREA_SIZE_CFG STARTUP_INIT_UT_PATH"/etc/param/ohos.para.size"
79 #define CLIENT_PIPE_NAME "/dev/unix/socket/paramservice"
80 #define PIPE_NAME STARTUP_INIT_UT_PATH "/dev/unix/socket/paramservice"
81 #define PARAM_STORAGE_PATH STARTUP_INIT_UT_PATH "/dev/__parameters__"
82 #define PARAM_PERSIST_SAVE_PATH DATA_PATH "persist_parameters"
83 #define PARAM_PERSIST_SAVE_TMP_PATH DATA_PATH "tmp_persist_parameters"
84 #define PRIVATE_DATA_PATH STARTUP_INIT_UT_PATH "/data/service/el1/public/startup/parameters/"
85 #define PARAM_OLD_PERSIST_SAVE_PATH PRIVATE_DATA_PATH "persist_parameters"
86 #define PARAM_PUBLIC_PERSIST_SAVE_PATH DATA_PATH "public_persist_parameters"
87 #define PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH DATA_PATH "tmp_public_persist_parameters"
88 #define PARAM_PRIVATE_PERSIST_SAVE_PATH PRIVATE_DATA_PATH "private_persist_parameters"
89 #define PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH PRIVATE_DATA_PATH "tmp_private_persist_parameters"
90 #define PERSIST_HANDLE_MAX 2
91 
92 #define WORKSPACE_FLAGS_INIT 0x01
93 #define WORKSPACE_FLAGS_LOADED 0x02
94 #define WORKSPACE_FLAGS_UPDATE 0x04
95 #define WORKSPACE_FLAGS_LABEL_LOADED 0x08
96 #define WORKSPACE_FLAGS_NEED_ACCESS 0x10
97 #define WORKSPACE_FLAGS_FOR_INIT 0x20
98 
99 #define PARAM_SET_FLAG(node, flag) ((node) |= (flag))
100 #define PARAM_CLEAR_FLAG(node, flag) ((node) &= ~(flag))
101 #define PARAM_TEST_FLAG(node, flag) (((node) & (flag)) == (flag))
102 
103 #ifndef PARAN_DOMAIN
104 #define PARAN_DOMAIN (BASE_DOMAIN + 2)
105 #endif
106 #define PARAN_LABEL "PARAM"
107 #ifdef PARAM_BASE
108 INIT_LOCAL_API void ParamWorBaseLog(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...);
109 #define PARAM_LOGV(fmt, ...) \
110     ParamWorBaseLog(INIT_DEBUG, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, \
111         (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
112 #define PARAM_LOGI(fmt, ...) \
113     ParamWorBaseLog(INIT_INFO, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, \
114         (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
115 #define PARAM_LOGW(fmt, ...) \
116     ParamWorBaseLog(INIT_WARN, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, \
117         (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
118 #define PARAM_LOGE(fmt, ...) \
119     ParamWorBaseLog(INIT_ERROR, PARAN_DOMAIN, PARAN_LABEL, "[%s:%d]" fmt, \
120         (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
121 #else
122 #define PARAM_LOGI(fmt, ...) STARTUP_LOGI(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__)
123 #define PARAM_LOGE(fmt, ...) STARTUP_LOGE(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__)
124 #define PARAM_LOGV(fmt, ...) STARTUP_LOGV(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__)
125 #define PARAM_LOGW(fmt, ...) STARTUP_LOGW(PARAN_DOMAIN, PARAN_LABEL, fmt, ##__VA_ARGS__)
126 #endif
127 
128 #define PARAM_CHECK(retCode, exper, ...) \
129     if (!(retCode)) {                \
130         PARAM_LOGE(__VA_ARGS__);     \
131         exper;                       \
132     }
133 
134 #define PARAM_INFO_CHECK(retCode, exper, ...) \
135     if (!(retCode)) {                \
136         PARAM_LOGI(__VA_ARGS__);     \
137         exper;                       \
138     }
139 
140 #define PARAM_WARNING_CHECK(retCode, exper, ...) \
141     if (!(retCode)) {                \
142         PARAM_LOGW(__VA_ARGS__);     \
143         exper;                       \
144     }
145 
146 #define PARAM_ONLY_CHECK(retCode, exper) \
147     if (!(retCode)) {                \
148         exper;                       \
149     }
150 
151 typedef int (*DUMP_PRINTF)(const char *fmt, ...);
152 #define PARAM_DUMP g_printf
153 #define MAX_LABEL_LEN 256
154 #define PARAM_BUFFER_SIZE 256
155 
156 #define SUBSTR_INFO_NAME 0
157 #define SUBSTR_INFO_VALUE 1
158 #ifdef PARAM_SUPPORT_SELINUX
159 #define SUBSTR_INFO_LABEL 1
160 #define SUBSTR_INFO_DAC 2
161 #else
162 #define SUBSTR_INFO_LABEL 1
163 #define SUBSTR_INFO_DAC 1
164 #endif
165 
166 INIT_LOCAL_API int SplitParamString(char *line, const char *exclude[], uint32_t count,
167     int (*result)(const uint32_t *context, const char *name, const char *value), const uint32_t *context);
168 
169 #ifdef __cplusplus
170 #if __cplusplus
171 }
172 #endif
173 #endif
174 #endif