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 <sys/types.h>
19 #include <unistd.h>
20
21 #include "beget_ext.h"
22 #include "param.h"
23 #include "securec.h"
24 #include "sys_param.h"
25
26 // Refer to parameter limit, value size should not bigger than 96
27 #define MAX_REBOOT_OPTION_SIZE PARAM_VALUE_LEN_MAX
28
29 #ifndef STARTUP_INIT_TEST
30 #define DOREBOOT_PARAM "ohos.startup.powerctrl"
31 #else
32 #define DOREBOOT_PARAM "reboot.ut"
33 #endif
34
DoReboot(const char * option)35 int DoReboot(const char *option)
36 {
37 char value[MAX_REBOOT_OPTION_SIZE];
38 int ret = 0;
39 if (option == NULL || strlen(option) == 0) {
40 ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
41 BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
42 ret = SystemSetParameter(DOREBOOT_PARAM, "reboot");
43 BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set reboot param");
44 return 0;
45 }
46 int length = strlen(option);
47 BEGET_ERROR_CHECK(length <= MAX_REBOOT_OPTION_SIZE, return -1,
48 "Reboot option \" %s \" is too large, overflow", option);
49 ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option);
50 BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to copy boot option \" %s \"", option);
51
52 if (strcmp(option, DEVICE_CMD_SUSPEND) == 0) {
53 ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
54 BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
55 } else if (strcmp(option, DEVICE_CMD_FREEZE) == 0) {
56 ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
57 BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
58 } else {
59 ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
60 BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
61 }
62 ret = SystemSetParameter(DOREBOOT_PARAM, value);
63 BEGET_ERROR_CHECK(ret == 0, return -1, "Set parameter to trigger reboot command \" %s \" failed", value);
64 return 0;
65 }
66