2 ** Copyright 2013, Kevin Cernekee <cernekee@gmail.com>
4 ** This was reverse engineered from an HTC "reboot" binary and is an attempt
5 ** to remain bug-compatible with the original.
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
13 ** Unless required by applicable law or agreed to in writing, software
14 ** distributed under the License is distributed on an "AS IS" BASIS,
15 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ** See the License for the specific language governing permissions and
17 ** limitations under the License.
25 #include <sys/reboot.h>
26 #include <android/log.h>
28 #define SETTINGS_DB "/data/data/com.android.providers.settings/databases/settings.db"
29 #define SCREEN_LOCK_STATUS "/data/misc/screen_lock_status"
33 int sqlcallback(void *private, int n_columns, char **col_values, char **col_names)
37 if (n_columns == 0 || !col_values[0])
40 if (!strcmp(col_values[0], "1"))
43 __android_log_print(ANDROID_LOG_INFO, NULL,
44 "sqlcallback %s = %s, pattern_locks= %d\n",
45 col_names[0], col_values[0], pattern_lock);
49 int checkPatternLock(void)
54 if (sqlite3_open(SETTINGS_DB, &pDb) != 0) {
55 __android_log_print(ANDROID_LOG_ERROR, NULL,
56 "sqlite3_open error");
57 /* BUG? probably shouldn't call sqlite3_close() if open failed */
62 "select value from system where name= \"lock_pattern_autolock\"",
63 sqlcallback, "checkPatternLock", &errmsg) != 0) {
64 __android_log_print(ANDROID_LOG_ERROR, NULL,
65 "SQL error: %s\n", errmsg);
75 int main(int argc, char **argv)
77 int no_sync = 0, power_off = 0;
81 while ((ret = getopt(argc, argv, "np")) != -1) {
90 fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n",
97 if (argc > (optind + 1)) {
98 fprintf(stderr, "%s: too many arguments\n", argv[0]);
102 /* BUG: this should use optind */
103 if (argc > 1 && !strcmp(argv[1], "oem-78")) {
104 /* HTC RUU mode: "reboot oem-78" */
110 f = fopen(SCREEN_LOCK_STATUS, "r");
112 fputs("5\n", stderr);
116 if (atoi(buf) == 1) {
117 if (pattern_lock != 0) {
118 fputs("1\n", stderr);
122 fputs("0\n", stderr);
129 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
130 LINUX_REBOOT_CMD_POWER_OFF, 0);
131 } else if (optind >= argc) {
132 ret = reboot(LINUX_REBOOT_CMD_RESTART);
134 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
135 LINUX_REBOOT_CMD_RESTART2, argv[optind]);
142 fputs("reboot returned\n", stderr);