param_server.c 9.5 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <ctype.h>
C
cheng_jinsong 已提交
16
#include <errno.h>
M
Mupceet 已提交
17 18 19 20 21 22 23 24 25

#include "param_manager.h"
#include "param_trie.h"

static int LoadSecurityLabel(const char *fileName)
{
    ParamWorkSpace *paramSpace = GetParamWorkSpace();
    PARAM_CHECK(paramSpace != NULL, return -1, "Invalid paramSpace");
    PARAM_WORKSPACE_CHECK(paramSpace, return -1, "Invalid space");
26
    PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load");
M
Mupceet 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#if !(defined __LITEOS_A__ || defined __LITEOS_M__)
    // load security label
    ParamSecurityOps *ops = GetParamSecurityOps(PARAM_SECURITY_DAC);
    if (ops != NULL && ops->securityGetLabel != NULL) {
        ops->securityGetLabel(fileName);
    }
#endif
    return 0;
}

static int GetParamValueFromBuffer(const char *name, const char *buffer, char *value, int length)
{
    size_t bootLen = strlen(OHOS_BOOT);
    const char *tmpName = name + bootLen;
    int ret = GetProcCmdlineValue(tmpName, buffer, value, length);
    return ret;
}

static int CommonDealFun(const char *name, const char *value, int res)
{
    int ret = 0;
    if (res == 0) {
C
fix log  
cheng_jinsong 已提交
49
        PARAM_LOGV("Add param from cmdline %s %s", name, value);
M
Mupceet 已提交
50
        ret = CheckParamName(name, 0);
C
fix log  
cheng_jinsong 已提交
51 52
        PARAM_CHECK(ret == 0, return ret, "Invalid param name %s", name);
        PARAM_LOGV("Param name %s, value %s", name, value);
M
Mupceet 已提交
53 54 55
        ret = WriteParam(name, value, NULL, 0);
        PARAM_CHECK(ret == 0, return ret, "Failed to write param %s %s", name, value);
    } else {
C
fix log  
cheng_jinsong 已提交
56
        PARAM_LOGE("Get %s parameter value is null.", name);
M
Mupceet 已提交
57 58 59 60
    }
    return ret;
}

M
Mupceet 已提交
61
static int ReadSnFromFile(const char *name, const char *file)
M
Mupceet 已提交
62
{
M
Mupceet 已提交
63 64
    char *data = ReadFileData(file);
    PARAM_CHECK(data != NULL, return -1, "Read sn from %s file failed!", file);
M
Mupceet 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78

    int index = 0;
    for (size_t i = 0; i < strlen(data); i++) {
        // cancel \r\n
        if (*(data + i) == '\r' || *(data + i) == '\n') {
            break;
        }
        if (*(data + i) != ':') {
            *(data + index) = *(data + i);
            index++;
        }
    }
    data[index] = '\0';
    PARAM_LOGV("**** name %s, value %s", name, data);
M
Mupceet 已提交
79
    int ret = WriteParam(name, data, NULL, 0);
M
Mupceet 已提交
80
    free(data);
C
codex  
chengjinsong 已提交
81
    PARAM_CHECK(ret == 0, return ret, "Failed to write param %s", name);
M
Mupceet 已提交
82 83
    return ret;
}
M
Mupceet 已提交
84

M
Mupceet 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static int SnDealFun(const char *name, const char *value, int res)
{
    const char *snFileList [] = {
        "/sys/block/mmcblk0/device/cid",
        "/proc/bootdevice/cid"
    };
    int ret = CheckParamName(name, 0);
    PARAM_CHECK(ret == 0, return ret, "Invalid name %s", name);
    if (value != NULL && res == 0 && value[0] != '/') {
        PARAM_LOGV("**** name %s, value %s", name, value);
        ret = WriteParam(name, value, NULL, 0);
        PARAM_CHECK(ret == 0, return ret, "Failed to write param %s %s", name, value);
        return ret;
    }
    if (value != NULL && value[0] == '/') {
        ret = ReadSnFromFile(name, value);
        if (ret == 0) {
            return ret;
        }
    }
    for (size_t i = 0; i < ARRAY_LENGTH(snFileList); i++) {
        ret = ReadSnFromFile(name, snFileList[i]);
        if (ret == 0) {
            break;
        }
    }
M
Mupceet 已提交
111 112 113 114 115 116 117 118 119 120 121 122
    return ret;
}

INIT_LOCAL_API int LoadParamFromCmdLine(void)
{
    static const cmdLineInfo cmdLines[] = {
        {OHOS_BOOT"hardware", CommonDealFun
        },
        {OHOS_BOOT"bootgroup", CommonDealFun
        },
        {OHOS_BOOT"reboot_reason", CommonDealFun
        },
C
slot  
chengjinsong 已提交
123 124
        {OHOS_BOOT"bootslots", CommonDealFun
        },
M
Mupceet 已提交
125 126 127 128 129 130 131 132 133 134
        {OHOS_BOOT"sn", SnDealFun
        }
    };
    char *data = ReadFileData(BOOT_CMD_LINE);
    PARAM_CHECK(data != NULL, return -1, "Failed to read file %s", BOOT_CMD_LINE);
    char *value = calloc(1, PARAM_CONST_VALUE_LEN_MAX + 1);
    PARAM_CHECK(value != NULL, free(data);
        return -1, "Failed to read file %s", BOOT_CMD_LINE);

    for (size_t i = 0; i < ARRAY_LENGTH(cmdLines); i++) {
C
codex  
chengjinsong 已提交
135
        int ret = 0;
M
Mupceet 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#ifdef BOOT_EXTENDED_CMDLINE
        ret = GetParamValueFromBuffer(cmdLines[i].name, BOOT_EXTENDED_CMDLINE, value, PARAM_CONST_VALUE_LEN_MAX);
        if (ret != 0) {
            ret = GetParamValueFromBuffer(cmdLines[i].name, data, value, PARAM_CONST_VALUE_LEN_MAX);
        }
#else
        ret = GetParamValueFromBuffer(cmdLines[i].name, data, value, PARAM_CONST_VALUE_LEN_MAX);
#endif

        cmdLines[i].processor(cmdLines[i].name, value, ret);
    }
    PARAM_LOGV("Parse cmdline finish %s", BOOT_CMD_LINE);
    free(data);
    free(value);
    return 0;
}

static int LoadOneParam_(const uint32_t *context, const char *name, const char *value)
{
    uint32_t mode = *(uint32_t *)context;
    int ret = CheckParamName(name, 0);
    if (ret != 0) {
        return 0;
    }
    PARAM_LOGV("Add default parameter [%s] [%s]", name, value);
    return WriteParam(name, value, NULL, mode & LOAD_PARAM_ONLY_ADD);
}

C
cheng_jinsong 已提交
164 165
static int LoadDefaultParam_(const char *fileName, uint32_t mode,
    const char *exclude[], uint32_t count, int (*loadOneParam)(const uint32_t *, const char *, const char *))
M
Mupceet 已提交
166 167 168
{
    uint32_t paramNum = 0;
    FILE *fp = fopen(fileName, "r");
C
cheng_jinsong 已提交
169 170
    PARAM_CHECK(fp != NULL, return -1, "Failed to open file '%s' error:%d ", fileName, errno);

M
Mupceet 已提交
171 172
    const int buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10;  // 10 max len
    char *buffer = malloc(buffSize);
C
cheng_jinsong 已提交
173 174 175
    PARAM_CHECK(buffer != NULL, (void)fclose(fp);
        return -1, "Failed to alloc memory");

M
Mupceet 已提交
176 177
    while (fgets(buffer, buffSize, fp) != NULL) {
        buffer[buffSize - 1] = '\0';
C
cheng_jinsong 已提交
178
        int ret = SplitParamString(buffer, exclude, count, loadOneParam, &mode);
M
Mupceet 已提交
179 180 181 182 183
        PARAM_CHECK(ret == 0, continue, "Failed to set param '%s' error:%d ", buffer, ret);
        paramNum++;
    }
    (void)fclose(fp);
    free(buffer);
C
fix log  
cheng_jinsong 已提交
184
    PARAM_LOGV("Load %u default parameters success from %s.", paramNum, fileName);
M
Mupceet 已提交
185 186 187 188 189 190 191
    return 0;
}

static int ProcessParamFile(const char *fileName, void *context)
{
    static const char *exclude[] = {"ctl.", "selinux.restorecon_recursive"};
    uint32_t mode = *(int *)context;
C
cheng_jinsong 已提交
192
    return LoadDefaultParam_(fileName, mode, exclude, ARRAY_LENGTH(exclude), LoadOneParam_);
M
Mupceet 已提交
193 194
}

C
chengjinsong2 已提交
195 196 197 198 199
int LoadParamsFile(const char *fileName, bool onlyAdd)
{
    return LoadDefaultParams(fileName, onlyAdd ? LOAD_PARAM_ONLY_ADD : LOAD_PARAM_NORMAL);
}

M
Mupceet 已提交
200 201 202
int LoadDefaultParams(const char *fileName, uint32_t mode)
{
    PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load");
C
fix log  
cheng_jinsong 已提交
203
    PARAM_LOGI("Load default parameters from %s.", fileName);
M
Mupceet 已提交
204 205
    struct stat st;
    if ((stat(fileName, &st) == 0) && !S_ISDIR(st.st_mode)) {
206
        (void)ProcessParamFile(fileName, &mode);
M
Mupceet 已提交
207
    } else {
208
        (void)ReadFileInDir(fileName, ".para", ProcessParamFile, &mode);
M
Mupceet 已提交
209 210 211 212 213 214 215 216 217 218
    }

    // load security label
    return LoadSecurityLabel(fileName);
}

INIT_LOCAL_API void LoadParamFromBuild(void)
{
    PARAM_LOGI("load parameters from build ");
#ifdef INCREMENTAL_VERSION
M
Mupceet 已提交
219 220 221
    if (strlen(INCREMENTAL_VERSION) > 0) {
        WriteParam("const.product.incremental.version", INCREMENTAL_VERSION, NULL, LOAD_PARAM_NORMAL);
    }
M
Mupceet 已提交
222 223
#endif
#ifdef BUILD_TYPE
M
Mupceet 已提交
224 225 226
    if (strlen(BUILD_TYPE) > 0) {
        WriteParam("const.product.build.type", BUILD_TYPE, NULL, LOAD_PARAM_NORMAL);
    }
M
Mupceet 已提交
227 228
#endif
#ifdef BUILD_USER
M
Mupceet 已提交
229 230 231
    if (strlen(BUILD_USER) > 0) {
        WriteParam("const.product.build.user", BUILD_USER, NULL, LOAD_PARAM_NORMAL);
    }
M
Mupceet 已提交
232 233
#endif
#ifdef BUILD_TIME
M
Mupceet 已提交
234 235 236
    if (strlen(BUILD_TIME) > 0) {
        WriteParam("const.product.build.date", BUILD_TIME, NULL, LOAD_PARAM_NORMAL);
    }
M
Mupceet 已提交
237 238
#endif
#ifdef BUILD_HOST
M
Mupceet 已提交
239 240 241
    if (strlen(BUILD_HOST) > 0) {
        WriteParam("const.product.build.host", BUILD_HOST, NULL, LOAD_PARAM_NORMAL);
    }
M
Mupceet 已提交
242 243
#endif
#ifdef BUILD_ROOTHASH
M
Mupceet 已提交
244 245 246
    if (strlen(BUILD_ROOTHASH) > 0) {
        WriteParam("const.ohos.buildroothash", BUILD_ROOTHASH, NULL, LOAD_PARAM_NORMAL);
    }
M
Mupceet 已提交
247 248 249
#endif
}

C
cheng_jinsong 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static int LoadOneParamAreaSize_(const uint32_t *context, const char *name, const char *value)
{
    int ret = CheckParamName(name, 0);
    if (ret != 0) {
        return 0;
    }
    ret = CheckParamValue(NULL, name, value, PARAM_TYPE_INT);
    PARAM_CHECK(ret == 0, return 0, "Invalid value %s for %s", value, name);
    PARAM_LOGV("LoadOneParamAreaSize_ [%s] [%s]", name, value);

    WorkSpace *workSpace = GetWorkSpace(WORKSPACE_NAME_DAC);
    ParamTrieNode *node = AddTrieNode(workSpace, name, strlen(name));
    PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX, "Failed to add node");
    ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
    if (entry == NULL) {
        uint32_t offset = AddParamNode(workSpace, PARAM_TYPE_INT, name, strlen(name), value, strlen(value));
        PARAM_CHECK(offset > 0, return PARAM_CODE_REACHED_MAX, "Failed to allocate name %s", name);
        SaveIndex(&node->dataIndex, offset);
    }
    return 0;
}

INIT_LOCAL_API void LoadParamAreaSize(void)
{
    LoadDefaultParam_(PARAM_AREA_SIZE_CFG, 0, NULL, 0, LoadOneParamAreaSize_);
}