1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include "init_reboot.h"
16d9f0492fSopenharmony_ci
17d9f0492fSopenharmony_ci#include <string.h>
18d9f0492fSopenharmony_ci#include <unistd.h>
19d9f0492fSopenharmony_ci
20d9f0492fSopenharmony_ci#include "beget_ext.h"
21d9f0492fSopenharmony_ci#include "securec.h"
22d9f0492fSopenharmony_ci#include "init_param.h"
23d9f0492fSopenharmony_ci
24d9f0492fSopenharmony_ci// Refer to parameter limit, value size should not bigger than 96
25d9f0492fSopenharmony_ci#define MAX_REBOOT_OPTION_SIZE PARAM_VALUE_LEN_MAX
26d9f0492fSopenharmony_ci
27d9f0492fSopenharmony_ci#ifndef STARTUP_INIT_TEST
28d9f0492fSopenharmony_ci#define DOREBOOT_PARAM "ohos.startup.powerctrl"
29d9f0492fSopenharmony_ci#else
30d9f0492fSopenharmony_ci#define DOREBOOT_PARAM "reboot.ut"
31d9f0492fSopenharmony_ci#endif
32d9f0492fSopenharmony_ci
33d9f0492fSopenharmony_cistatic int DoRebootByInitPlugin(const char *mode, const char *option)
34d9f0492fSopenharmony_ci{
35d9f0492fSopenharmony_ci    char value[MAX_REBOOT_OPTION_SIZE];
36d9f0492fSopenharmony_ci    int ret = 0;
37d9f0492fSopenharmony_ci    if (mode != NULL) {
38d9f0492fSopenharmony_ci        if (option != NULL) {
39d9f0492fSopenharmony_ci            ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s:%s", mode, option);
40d9f0492fSopenharmony_ci        } else {
41d9f0492fSopenharmony_ci            ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", mode);
42d9f0492fSopenharmony_ci        }
43d9f0492fSopenharmony_ci    } else {
44d9f0492fSopenharmony_ci        if (option != NULL) {
45d9f0492fSopenharmony_ci            ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option);
46d9f0492fSopenharmony_ci        } else {
47d9f0492fSopenharmony_ci            ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "%s", "reboot");
48d9f0492fSopenharmony_ci        }
49d9f0492fSopenharmony_ci    }
50d9f0492fSopenharmony_ci    BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to format boot mode ");
51d9f0492fSopenharmony_ci    BEGET_LOGV("Reboot cmd %s", value);
52d9f0492fSopenharmony_ci    ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
53d9f0492fSopenharmony_ci    BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
54d9f0492fSopenharmony_ci    ret = SystemSetParameter(DOREBOOT_PARAM, value);
55d9f0492fSopenharmony_ci    BEGET_ERROR_CHECK(ret == 0, return -1, "Set parameter to trigger reboot command \" %s \" failed", value);
56d9f0492fSopenharmony_ci    return 0;
57d9f0492fSopenharmony_ci}
58d9f0492fSopenharmony_ci
59d9f0492fSopenharmony_cistatic int ExecReboot(const char *mode, const char *option)
60d9f0492fSopenharmony_ci{
61d9f0492fSopenharmony_ci    // check if param set ok
62d9f0492fSopenharmony_ci#ifndef STARTUP_INIT_TEST
63d9f0492fSopenharmony_ci    const int maxCount = 10;
64d9f0492fSopenharmony_ci#else
65d9f0492fSopenharmony_ci    const int maxCount = 1;
66d9f0492fSopenharmony_ci#endif
67d9f0492fSopenharmony_ci    int count = 0;
68d9f0492fSopenharmony_ci    DoRebootByInitPlugin(mode, option);
69d9f0492fSopenharmony_ci    while (count < maxCount) {
70d9f0492fSopenharmony_ci        usleep(100 * 1000); // 100 * 1000 wait 100ms
71d9f0492fSopenharmony_ci        char result[10] = {0}; // 10 stop len
72d9f0492fSopenharmony_ci        uint32_t len = sizeof(result);
73d9f0492fSopenharmony_ci        int ret = SystemGetParameter(STARTUP_DEVICE_CTL, result, &len);
74d9f0492fSopenharmony_ci        if (ret == 0 && strcmp(result, DEVICE_CMD_STOP) == 0) {
75d9f0492fSopenharmony_ci            BEGET_LOGE("Success to reboot system");
76d9f0492fSopenharmony_ci            return 0;
77d9f0492fSopenharmony_ci        }
78d9f0492fSopenharmony_ci        count++;
79d9f0492fSopenharmony_ci        DoRebootByInitPlugin(mode, option);
80d9f0492fSopenharmony_ci    }
81d9f0492fSopenharmony_ci    BEGET_LOGE("Failed to reboot system");
82d9f0492fSopenharmony_ci    return 0;
83d9f0492fSopenharmony_ci}
84d9f0492fSopenharmony_ci
85d9f0492fSopenharmony_ciint DoReboot(const char *option)
86d9f0492fSopenharmony_ci{
87d9f0492fSopenharmony_ci    return ExecReboot(NULL, option);
88d9f0492fSopenharmony_ci}
89d9f0492fSopenharmony_ci
90d9f0492fSopenharmony_ciint DoRebootExt(const char *mode, const char *option)
91d9f0492fSopenharmony_ci{
92d9f0492fSopenharmony_ci    return ExecReboot(mode, option);
93d9f0492fSopenharmony_ci}