• 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 
DoReboot_(const char * option)33 static int DoReboot_(const char *option)
34 {
35     char value[MAX_REBOOT_OPTION_SIZE];
36     int ret = 0;
37     if (option == NULL || strlen(option) == 0) {
38         ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
39         BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
40         ret = SystemSetParameter(DOREBOOT_PARAM, "reboot");
41         BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set reboot param");
42         return 0;
43     }
44     int length = strlen(option);
45     BEGET_ERROR_CHECK(length <= MAX_REBOOT_OPTION_SIZE, return -1,
46         "Reboot option \" %s \" is too large, overflow", option);
47     ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option);
48     BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to copy boot option \" %s \"", option);
49 
50     ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
51     BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
52     ret = SystemSetParameter(DOREBOOT_PARAM, value);
53     BEGET_ERROR_CHECK(ret == 0, return -1, "Set parameter to trigger reboot command \" %s \" failed", value);
54     return 0;
55 }
56 
DoReboot(const char * option)57 int DoReboot(const char *option)
58 {
59     // check if param set ok
60 #ifndef STARTUP_INIT_TEST
61     const int maxCount = 10;
62 #else
63     const int maxCount = 1;
64 #endif
65     int count = 0;
66     DoReboot_(option);
67     while (count < maxCount) {
68         usleep(100 * 1000); // 100 * 1000 wait 100ms
69         char result[10] = {0}; // 10 stop len
70         uint32_t len = sizeof(result);
71         int ret = SystemGetParameter(STARTUP_DEVICE_CTL, result, &len);
72         if (ret == 0 && strcmp(result, DEVICE_CMD_STOP) == 0) {
73             BEGET_LOGE("Success to reboot system");
74             return 0;
75         }
76         count++;
77         DoReboot_(option);
78     }
79     BEGET_LOGE("Failed to reboot system");
80     return 0;
81 }
82