param_server.c 8.2 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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>

#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");
25
    PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load");
M
Mupceet 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#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) {
        PARAM_LOGI("Add param from cmdline %s %s", name, value);
        ret = CheckParamName(name, 0);
        PARAM_CHECK(ret == 0, return ret, "Invalid name %s", name);
        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);
    } else {
        PARAM_LOGE("Can not find arrt %s", name);
    }
    return ret;
}

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

    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 已提交
78
    int ret = WriteParam(name, data, NULL, 0);
M
Mupceet 已提交
79
    free(data);
M
Mupceet 已提交
80 81 82
    PARAM_CHECK(ret == 0, return ret, "Failed to write param %s %s", name, data);
    return ret;
}
M
Mupceet 已提交
83

M
Mupceet 已提交
84 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
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 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
    return ret;
}

INIT_LOCAL_API int LoadParamFromCmdLine(void)
{
    int ret;
    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 135 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        {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++) {
#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);
}

static int LoadDefaultParam_(const char *fileName, uint32_t mode, const char *exclude[], uint32_t count)
{
    uint32_t paramNum = 0;
    FILE *fp = fopen(fileName, "r");
    if (fp == NULL) {
        return -1;
    }
    const int buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10;  // 10 max len
    char *buffer = malloc(buffSize);
    if (buffer == NULL) {
        (void)fclose(fp);
        return -1;
    }
    while (fgets(buffer, buffSize, fp) != NULL) {
        buffer[buffSize - 1] = '\0';
M
Mupceet 已提交
178
        int ret = SplitParamString(buffer, exclude, count, LoadOneParam_, &mode);
M
Mupceet 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        PARAM_CHECK(ret == 0, continue, "Failed to set param '%s' error:%d ", buffer, ret);
        paramNum++;
    }
    (void)fclose(fp);
    free(buffer);
    PARAM_LOGI("Load parameters success %s total %u", fileName, paramNum);
    return 0;
}

static int ProcessParamFile(const char *fileName, void *context)
{
    static const char *exclude[] = {"ctl.", "selinux.restorecon_recursive"};
    uint32_t mode = *(int *)context;
    return LoadDefaultParam_(fileName, mode, exclude, ARRAY_LENGTH(exclude));
}

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 203 204 205
int LoadDefaultParams(const char *fileName, uint32_t mode)
{
    PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load");
    PARAM_LOGI("load default parameters %s.", fileName);
    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
}