init_reboot.c 6.6 KB
Newer Older
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 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 <errno.h>
16
#include <limits.h>
17 18 19 20
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/reboot.h>

21
#include "fs_manager/fs_manager.h"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "init_log.h"
#include "init_service.h"
#include "init_service_manager.h"
#include "init_utils.h"
#include "securec.h"

#define MAX_VALUE_LENGTH 500
#define MAX_COMMAND_SIZE 20
#define MAX_UPDATE_SIZE 100

struct RBMiscUpdateMessage {
    char command[MAX_COMMAND_SIZE];
    char update[MAX_UPDATE_SIZE];
};

static int RBMiscWriteUpdaterMessage(const char *path, const struct RBMiscUpdateMessage *boot)
{
    char *realPath = GetRealPath(path);
X
add ut  
xionglei6 已提交
40
    INIT_CHECK_RETURN_VALUE(realPath != NULL, -1);
41 42 43 44
    int ret = 0;
    FILE *fp = fopen(realPath, "rb+");
    if (fp != NULL) {
        size_t writeLen = fwrite(boot, sizeof(struct RBMiscUpdateMessage), 1, fp);
X
add ut  
xionglei6 已提交
45
        INIT_ERROR_CHECK(writeLen == 1, ret = -1, "Failed to write misc for reboot");
46 47 48 49
    } else {
        ret = -1;
        INIT_LOGE("Failed to open %s", path);
    }
50 51 52
    free(realPath);
    realPath = NULL;
    (void)fclose(fp);
53 54 55 56 57 58
    return ret;
}

static int RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessage *boot)
{
    char *realPath = GetRealPath(path);
X
add ut  
xionglei6 已提交
59
    INIT_CHECK_RETURN_VALUE(realPath != NULL, -1);
60 61 62 63
    int ret = 0;
    FILE *fp = fopen(realPath, "rb");
    if (fp != NULL) {
        size_t readLen = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp);
X
add ut  
xionglei6 已提交
64
        INIT_ERROR_CHECK(readLen > 0, ret = -1, "Failed to read misc for reboot");
65 66 67 68 69
    } else {
        ret = -1;
        INIT_LOGE("Failed to open %s", path);
    }

70 71
    free(realPath);
    realPath = NULL;
72
    (void)fclose(fp);
73
    return ret;
74 75
}

76 77
static int CheckAndRebootToUpdater(const char *valueData, const char *cmd,
    const char *cmdExt, const char *boot, const char *miscFile)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
    // "updater" or "updater:"
    struct RBMiscUpdateMessage msg;
    int ret = RBMiscReadUpdaterMessage(miscFile, &msg);
    INIT_ERROR_CHECK(ret == 0, return -1, "Failed to get misc info for %s.", cmd);

    if (boot != NULL) {
        ret = snprintf_s(msg.command, MAX_COMMAND_SIZE, MAX_COMMAND_SIZE - 1, "%s", boot);
        INIT_ERROR_CHECK(ret > 0, return -1, "Failed to format cmd for %s.", cmd);
        msg.command[MAX_COMMAND_SIZE - 1] = 0;
    } else {
        ret = memset_s(msg.command, MAX_COMMAND_SIZE, 0, MAX_COMMAND_SIZE);
        INIT_ERROR_CHECK(ret == 0, return -1, "Failed to format cmd for %s.", cmd);
    }

    if ((cmdExt != NULL) && (valueData != NULL) && (strncmp(valueData, cmdExt, strlen(cmdExt)) == 0)) {
        const char *p = valueData + strlen(cmdExt);
        ret = snprintf_s(msg.update, MAX_UPDATE_SIZE, MAX_UPDATE_SIZE - 1, "%s", p);
        INIT_ERROR_CHECK(ret > 0, return -1, "Failed to format param for %s.", cmd);
        msg.update[MAX_UPDATE_SIZE - 1] = 0;
X
xionglei6 已提交
98 99 100
    } else {
        ret = memset_s(msg.update, MAX_UPDATE_SIZE, 0, MAX_UPDATE_SIZE);
        INIT_ERROR_CHECK(ret == 0, return -1, "Failed to format update for %s.", cmd);
101 102
    }

X
add ut  
xionglei6 已提交
103
    ret = -1;
104
    if (RBMiscWriteUpdaterMessage(miscFile, &msg) == 0) {
X
add ut  
xionglei6 已提交
105 106 107 108
        ret = 0;
#ifndef STARTUP_INIT_TEST
        ret = reboot(RB_AUTOBOOT);
#endif
109
    }
X
add ut  
xionglei6 已提交
110
    return ret;
111 112 113 114 115 116 117 118
}

static int CheckRebootParam(const char *valueData)
{
    if (valueData == NULL) {
        return 0;
    }
    static const char *cmdParams[] = {
X
xionglei6 已提交
119
        "shutdown", "updater", "updater:", "flash", "flash:", "NoArgument", "loader", "bootloader"
120 121 122 123 124 125 126
    };
    size_t i = 0;
    for (; i < ARRAY_LENGTH(cmdParams); i++) {
        if (strncmp(valueData, cmdParams[i], strlen(cmdParams[i])) == 0) {
            break;
        }
    }
X
add ut  
xionglei6 已提交
127
    INIT_CHECK_RETURN_VALUE(i < ARRAY_LENGTH(cmdParams), -1);
128 129 130 131 132 133 134 135 136 137 138 139 140
    return 0;
}

void ExecReboot(const char *value)
{
    INIT_ERROR_CHECK(value != NULL && strlen(value) <= MAX_VALUE_LENGTH, return, "Invalid arg");
    const char *valueData = NULL;
    if (strncmp(value, "reboot,", strlen("reboot,")) == 0) {
        valueData = value + strlen("reboot,");
    } else if (strcmp(value, "reboot") != 0) {
        INIT_LOGE("Reboot value = %s, must started with reboot", value);
        return;
    }
X
add ut  
xionglei6 已提交
141
    INIT_ERROR_CHECK(CheckRebootParam(valueData) == 0, return, "Invalid arg %s for reboot.", value);
142 143
    char *fstabFile = GetFstabFile();
    INIT_ERROR_CHECK(fstabFile != NULL, return, "Failed get fstab file");
X
xionglei6 已提交
144 145 146
    Fstab *fstab = NULL;
    INIT_ERROR_CHECK((fstab = ReadFstabFromFile(fstabFile, false)) != NULL, free(fstabFile); return,
        "Failed get fstab from %s", fstabFile);
147
    free(fstabFile);
X
xionglei6 已提交
148 149 150
    char miscDevice[PATH_MAX] = {0};
    int ret = GetBlockDeviceByMountPoint("/misc", fstab, miscDevice, PATH_MAX);
    ReleaseFstab(fstab);
151
    INIT_ERROR_CHECK(ret == 0, return, "Failed to get misc device name.");
152 153
    StopAllServices(SERVICE_ATTR_INVALID);
    sync();
X
add ut  
xionglei6 已提交
154 155
    INIT_CHECK_ONLY_ELOG(GetMountStatusForMountPoint("/vendor") == 0 || umount("/vendor") == 0,
        "Failed to umount vendor. errno = %d.", errno);
156 157 158 159 160 161 162 163
    if (GetMountStatusForMountPoint("/data") != 0) {
        if (umount("/data") != 0 && umount2("/data", MNT_FORCE) != 0) {
            INIT_LOGE("Failed umount data. errno = %d.", errno);
        }
    } else {
        INIT_LOGE("Failed to get mount point \"/data\"");
    }

164
    ret = 0;
165
    if (valueData == NULL) {
166
        ret = CheckAndRebootToUpdater(NULL, "reboot", NULL, NULL, miscDevice);
167
    } else if (strcmp(valueData, "shutdown") == 0) {
X
add ut  
xionglei6 已提交
168
#ifndef STARTUP_INIT_TEST
169
        ret = reboot(RB_POWER_OFF);
X
add ut  
xionglei6 已提交
170
#endif
171
    } else if (strcmp(valueData, "bootloader") == 0) {
X
add ut  
xionglei6 已提交
172
#ifndef STARTUP_INIT_TEST
173
        ret = reboot(RB_POWER_OFF);
X
add ut  
xionglei6 已提交
174
#endif
175
    } else if (strncmp(valueData, "updater", strlen("updater")) == 0) {
176
        ret = CheckAndRebootToUpdater(valueData, "updater", "updater:", "boot_updater", miscDevice);
177
    } else if (strncmp(valueData, "flash", strlen("flash")) == 0) {
178
        ret = CheckAndRebootToUpdater(valueData, "flash", "flash:", "boot_flash", miscDevice);
179 180 181 182
    }
    INIT_LOGI("Reboot %s %s.", value, (ret == 0) ? "success" : "fail");
    return;
}