• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "init_reboot.h"
16 
17 #include <string.h>
18 #include <unistd.h>
19 
20 #include "beget_ext.h"
21 #include "securec.h"
22 #include "init_param.h"
23 
24 // Refer to parameter limit, value size should not bigger than 96
25 #define MAX_REBOOT_OPTION_SIZE PARAM_VALUE_LEN_MAX
26 
27 #ifndef STARTUP_INIT_TEST
28 #define DOREBOOT_PARAM "ohos.startup.powerctrl"
29 #else
30 #define DOREBOOT_PARAM "reboot.ut"
31 #endif
32 
DoRebootByInitPlugin(const char * mode,const char * option)33 static int DoRebootByInitPlugin(const char *mode, const char *option)
34 {
35     char value[MAX_REBOOT_OPTION_SIZE];
36     int ret = 0;
37     if (mode != NULL) {
38         if (option != NULL) {
39             ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s:%s", mode, option);
40         } else {
41             ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", mode);
42         }
43     } else {
44         if (option != NULL) {
45             ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option);
46         } else {
47             ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "%s", "reboot");
48         }
49     }
50     BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to format boot mode");
51     BEGET_LOGI("Reboot cmd %s", value);
52     ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
53     BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
54     ret = SystemSetParameter(DOREBOOT_PARAM, value);
55     BEGET_ERROR_CHECK(ret == 0, return -1, "Set parameter to trigger reboot command \" %s \" failed", value);
56     return 0;
57 }
58 
ExecReboot(const char * mode,const char * option)59 static int ExecReboot(const char *mode, const char *option)
60 {
61     // check if param set ok
62 #ifndef STARTUP_INIT_TEST
63     const int maxCount = 10;
64 #else
65     const int maxCount = 1;
66 #endif
67     int status = DoRebootByInitPlugin(mode, option);
68 
69     int count = 0;
70     while (count < maxCount) {
71         usleep(100 * 1000); // 100 * 1000 wait 100ms
72         char result[10] = {0};
73         uint32_t len = sizeof(result);
74         int ret = SystemGetParameter(STARTUP_DEVICE_CTL, result, &len);
75         if (ret == 0 && strcmp(result, DEVICE_CMD_STOP) == 0) {
76             BEGET_LOGE("Success to reboot system");
77             return 0;
78         }
79         count++;
80         status = DoRebootByInitPlugin(mode, option);
81     }
82     BEGET_LOGE("Failed to reboot system");
83     return status;
84 }
85 
DoReboot(const char * option)86 int DoReboot(const char *option)
87 {
88     return ExecReboot(NULL, option);
89 }
90 
DoRebootExt(const char * mode,const char * option)91 int DoRebootExt(const char *mode, const char *option)
92 {
93     return ExecReboot(mode, option);
94 }