blob: dace52f9831fb893d48edf0bfc940fb4d63b972c [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070018#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <limits.h>
23#include <linux/input.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070024#include <stdarg.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Patrick Tjincd055ee2014-12-09 11:26:40 -080028#include <sys/klog.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070029#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include <sys/types.h>
31#include <time.h>
32#include <unistd.h>
33
Tao Bao04ca4262015-09-10 15:32:24 -070034#include <chrono>
35
Tao Bao75238632015-05-27 14:46:17 -070036#include <adb.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080037#include <android-base/file.h>
38#include <android-base/stringprintf.h>
Tao Bao75238632015-05-27 14:46:17 -070039#include <cutils/android_reboot.h>
40#include <cutils/properties.h>
Tao Baobef39712015-05-04 18:50:27 -070041
Tao Bao75238632015-05-27 14:46:17 -070042#include "adb_install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044#include "common.h"
Tao Bao75238632015-05-27 14:46:17 -070045#include "device.h"
46#include "fuse_sdcard_provider.h"
47#include "fuse_sideload.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048#include "install.h"
49#include "minui/minui.h"
50#include "minzip/DirUtil.h"
51#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070052#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070053#include "screen_ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054
Stephen Smalley779701d2012-02-09 14:13:23 -050055struct selabel_handle *sehandle;
56
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057static const struct option OPTIONS[] = {
Tao Baoc679f932015-03-30 09:43:49 -070058 { "send_intent", required_argument, NULL, 'i' },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059 { "update_package", required_argument, NULL, 'u' },
60 { "wipe_data", no_argument, NULL, 'w' },
61 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070062 { "show_text", no_argument, NULL, 't' },
Tao Baoc679f932015-03-30 09:43:49 -070063 { "sideload", no_argument, NULL, 's' },
64 { "sideload_auto_reboot", no_argument, NULL, 'a' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070065 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070066 { "locale", required_argument, NULL, 'l' },
Doug Zongkerc87bab12013-11-25 13:53:25 -080067 { "stages", required_argument, NULL, 'g' },
Doug Zongkerb1d12632014-03-18 10:32:12 -070068 { "shutdown_after", no_argument, NULL, 'p' },
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070069 { "reason", required_argument, NULL, 'r' },
Doug Zongker988500b2009-10-06 14:41:38 -070070 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071};
72
Doug Zongker6d0d7ac2013-07-09 13:34:55 -070073static const char *CACHE_LOG_DIR = "/cache/recovery";
Doug Zongkerd4208f92010-09-20 12:16:13 -070074static const char *COMMAND_FILE = "/cache/recovery/command";
75static const char *INTENT_FILE = "/cache/recovery/intent";
76static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070077static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070078static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070079static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070080static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070082static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Tao Baof0124322015-04-11 02:04:11 +000083static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
Tao Baobef39712015-05-04 18:50:27 -070084static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
85static const int KEEP_LOG_COUNT = 10;
Nick Kralevicha9ad0322014-10-22 18:38:48 -070086
Doug Zongker211aebc2011-10-28 15:13:10 -070087RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070088char* locale = NULL;
Doug Zongkerc87bab12013-11-25 13:53:25 -080089char* stage = NULL;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070090char* reason = NULL;
Tao Bao682c34b2015-04-07 17:16:35 -070091bool modified_flash = false;
Doug Zongker211aebc2011-10-28 15:13:10 -070092
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093/*
94 * The recovery tool communicates with the main system through /cache files.
95 * /cache/recovery/command - INPUT - command line for tool, one arg per line
96 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
97 * /cache/recovery/intent - OUTPUT - intent that was passed in
98 *
99 * The arguments which may be supplied in the recovery.command file:
100 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102 * --wipe_data - erase user data (and cache), then reboot
103 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -0800104 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700105 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800106 *
107 * After completing, we remove /cache/recovery/command and reboot.
108 * Arguments may also be supplied in the bootloader control block (BCB).
109 * These important scenarios must be safely restartable at any point:
110 *
111 * FACTORY RESET
112 * 1. user selects "factory reset"
113 * 2. main system writes "--wipe_data" to /cache/recovery/command
114 * 3. main system reboots into recovery
115 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
116 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700117 * 5. erase_volume() reformats /data
118 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800119 * 7. finish_recovery() erases BCB
120 * -- after this, rebooting will restart the main system --
121 * 8. main() calls reboot() to boot main system
122 *
123 * OTA INSTALL
124 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700125 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126 * 3. main system reboots into recovery
127 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
128 * -- after this, rebooting will attempt to reinstall the update --
129 * 5. install_package() attempts to install the update
130 * NOTE: the package install must itself be restartable from any point
131 * 6. finish_recovery() erases BCB
132 * -- after this, rebooting will (try to) restart the main system --
133 * 7. ** if install failed **
134 * 7a. prompt_and_wait() shows an error icon and waits for the user
135 * 7b; the user reboots (pulling the battery, etc) into the main system
136 * 8. main() calls maybe_install_firmware_update()
137 * ** if the update contained radio/hboot firmware **:
138 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
139 * -- after this, rebooting will reformat cache & restart main system --
140 * 8b. m_i_f_u() writes firmware image into raw cache partition
141 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
142 * -- after this, rebooting will attempt to reinstall firmware --
143 * 8d. bootloader tries to flash firmware
144 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
145 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700146 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147 * 8g. finish_recovery() erases BCB
148 * -- after this, rebooting will (try to) restart the main system --
149 * 9. main() calls reboot() to boot main system
150 */
151
152static const int MAX_ARG_LENGTH = 4096;
153static const int MAX_ARGS = 100;
154
Doug Zongkerd4208f92010-09-20 12:16:13 -0700155// open a given path, mounting partitions as necessary
Tao Bao04ca4262015-09-10 15:32:24 -0700156FILE* fopen_path(const char *path, const char *mode) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700157 if (ensure_path_mounted(path) != 0) {
158 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159 return NULL;
160 }
161
162 // When writing, try to create the containing directory, if necessary.
163 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500164 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165
166 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167 return fp;
168}
169
Tao Bao04ca4262015-09-10 15:32:24 -0700170// close a file, log an error if the error indicator is set
171static void check_and_fclose(FILE *fp, const char *name) {
172 fflush(fp);
173 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
174 fclose(fp);
175}
176
Elliott Hughesf14af802015-02-10 14:46:14 -0800177bool is_ro_debuggable() {
178 char value[PROPERTY_VALUE_MAX+1];
179 return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1');
180}
181
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700182static void redirect_stdio(const char* filename) {
Tao Bao04ca4262015-09-10 15:32:24 -0700183 int pipefd[2];
184 if (pipe(pipefd) == -1) {
185 LOGE("pipe failed: %s\n", strerror(errno));
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700186
Tao Bao04ca4262015-09-10 15:32:24 -0700187 // Fall back to traditional logging mode without timestamps.
188 // If these fail, there's not really anywhere to complain...
189 freopen(filename, "a", stdout); setbuf(stdout, NULL);
190 freopen(filename, "a", stderr); setbuf(stderr, NULL);
191
192 return;
193 }
194
195 pid_t pid = fork();
196 if (pid == -1) {
197 LOGE("fork failed: %s\n", strerror(errno));
198
199 // Fall back to traditional logging mode without timestamps.
200 // If these fail, there's not really anywhere to complain...
201 freopen(filename, "a", stdout); setbuf(stdout, NULL);
202 freopen(filename, "a", stderr); setbuf(stderr, NULL);
203
204 return;
205 }
206
207 if (pid == 0) {
208 /// Close the unused write end.
209 close(pipefd[1]);
210
211 auto start = std::chrono::steady_clock::now();
212
213 // Child logger to actually write to the log file.
214 FILE* log_fp = fopen(filename, "a");
215 if (log_fp == nullptr) {
216 LOGE("fopen \"%s\" failed: %s\n", filename, strerror(errno));
217 close(pipefd[0]);
218 _exit(1);
219 }
220
221 FILE* pipe_fp = fdopen(pipefd[0], "r");
222 if (pipe_fp == nullptr) {
223 LOGE("fdopen failed: %s\n", strerror(errno));
224 check_and_fclose(log_fp, filename);
225 close(pipefd[0]);
226 _exit(1);
227 }
228
229 char* line = nullptr;
230 size_t len = 0;
231 while (getline(&line, &len, pipe_fp) != -1) {
232 auto now = std::chrono::steady_clock::now();
233 double duration = std::chrono::duration_cast<std::chrono::duration<double>>(
234 now - start).count();
235 if (line[0] == '\n') {
236 fprintf(log_fp, "[%12.6lf]\n", duration);
237 } else {
238 fprintf(log_fp, "[%12.6lf] %s", duration, line);
239 }
240 fflush(log_fp);
241 }
242
243 LOGE("getline failed: %s\n", strerror(errno));
244
245 free(line);
246 check_and_fclose(log_fp, filename);
247 close(pipefd[0]);
248 _exit(1);
249 } else {
250 // Redirect stdout/stderr to the logger process.
251 // Close the unused read end.
252 close(pipefd[0]);
253
254 setbuf(stdout, nullptr);
255 setbuf(stderr, nullptr);
256
257 if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
258 LOGE("dup2 stdout failed: %s\n", strerror(errno));
259 }
260 if (dup2(pipefd[1], STDERR_FILENO) == -1) {
261 LOGE("dup2 stderr failed: %s\n", strerror(errno));
262 }
263
264 close(pipefd[1]);
265 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800266}
267
268// command line args come from, in decreasing precedence:
269// - the actual command line
270// - the bootloader control block (one per line, after "recovery")
271// - the contents of COMMAND_FILE (one per line)
272static void
273get_args(int *argc, char ***argv) {
274 struct bootloader_message boot;
275 memset(&boot, 0, sizeof(boot));
276 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Doug Zongkerc87bab12013-11-25 13:53:25 -0800277 stage = strndup(boot.stage, sizeof(boot.stage));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278
279 if (boot.command[0] != 0 && boot.command[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700280 LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281 }
282
283 if (boot.status[0] != 0 && boot.status[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700284 LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 }
286
287 // --- if arguments weren't supplied, look in the bootloader control block
288 if (*argc <= 1) {
289 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
290 const char *arg = strtok(boot.recovery, "\n");
291 if (arg != NULL && !strcmp(arg, "recovery")) {
292 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
293 (*argv)[0] = strdup(arg);
294 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
295 if ((arg = strtok(NULL, "\n")) == NULL) break;
296 (*argv)[*argc] = strdup(arg);
297 }
298 LOGI("Got arguments from boot message\n");
299 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
300 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
301 }
302 }
303
304 // --- if that doesn't work, try the command file
305 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700306 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800308 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800309 char *argv0 = (*argv)[0];
310 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
311 (*argv)[0] = argv0; // use the same program name
312
313 char buf[MAX_ARG_LENGTH];
314 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
315 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800316 token = strtok(buf, "\r\n");
317 if (token != NULL) {
318 (*argv)[*argc] = strdup(token); // Strip newline.
319 } else {
320 --*argc;
321 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322 }
323
324 check_and_fclose(fp, COMMAND_FILE);
325 LOGI("Got arguments from %s\n", COMMAND_FILE);
326 }
327 }
328
329 // --> write the arguments we have back into the bootloader control block
330 // always boot into recovery after this (until finish_recovery() is called)
331 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
332 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
333 int i;
334 for (i = 1; i < *argc; ++i) {
335 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
336 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
337 }
338 set_bootloader_message(&boot);
339}
340
Doug Zongker34c98df2009-08-18 12:05:45 -0700341static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800342set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700343 struct bootloader_message boot;
344 memset(&boot, 0, sizeof(boot));
345 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
346 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
347 set_bootloader_message(&boot);
348}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800349
Tao Baobef39712015-05-04 18:50:27 -0700350// Read from kernel log into buffer and write out to file.
351static void save_kernel_log(const char* destination) {
352 int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800353 if (klog_buf_len <= 0) {
Tao Baobef39712015-05-04 18:50:27 -0700354 LOGE("Error getting klog size: %s\n", strerror(errno));
Patrick Tjincd055ee2014-12-09 11:26:40 -0800355 return;
356 }
357
Tao Baobef39712015-05-04 18:50:27 -0700358 std::string buffer(klog_buf_len, 0);
359 int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len);
360 if (n == -1) {
361 LOGE("Error in reading klog: %s\n", strerror(errno));
Patrick Tjincd055ee2014-12-09 11:26:40 -0800362 return;
363 }
Tao Baobef39712015-05-04 18:50:27 -0700364 buffer.resize(n);
365 android::base::WriteStringToFile(buffer, destination);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800366}
367
Tao Baof0124322015-04-11 02:04:11 +0000368// How much of the temp log we have copied to the copy in cache.
369static long tmplog_offset = 0;
370
Tao Baobef39712015-05-04 18:50:27 -0700371static void copy_log_file(const char* source, const char* destination, bool append) {
372 FILE* dest_fp = fopen_path(destination, append ? "a" : "w");
373 if (dest_fp == nullptr) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700374 LOGE("Can't open %s\n", destination);
375 } else {
Tao Baobef39712015-05-04 18:50:27 -0700376 FILE* source_fp = fopen(source, "r");
377 if (source_fp != nullptr) {
Tao Baof0124322015-04-11 02:04:11 +0000378 if (append) {
Tao Baobef39712015-05-04 18:50:27 -0700379 fseek(source_fp, tmplog_offset, SEEK_SET); // Since last write
Doug Zongker2c3539e2010-09-29 13:21:30 -0700380 }
381 char buf[4096];
Tao Baobef39712015-05-04 18:50:27 -0700382 size_t bytes;
383 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
384 fwrite(buf, 1, bytes, dest_fp);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700385 }
Tao Baobef39712015-05-04 18:50:27 -0700386 if (append) {
387 tmplog_offset = ftell(source_fp);
388 }
389 check_and_fclose(source_fp, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700390 }
Tao Baobef39712015-05-04 18:50:27 -0700391 check_and_fclose(dest_fp, destination);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700392 }
393}
394
Tao Baobef39712015-05-04 18:50:27 -0700395// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
396// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
397// Overwrite any existing last_log.$max and last_kmsg.$max.
398static void rotate_logs(int max) {
Tao Bao682c34b2015-04-07 17:16:35 -0700399 // Logs should only be rotated once.
400 static bool rotated = false;
401 if (rotated) {
402 return;
403 }
404 rotated = true;
405 ensure_path_mounted(LAST_LOG_FILE);
Tao Baobef39712015-05-04 18:50:27 -0700406 ensure_path_mounted(LAST_KMSG_FILE);
Tao Bao682c34b2015-04-07 17:16:35 -0700407
Tao Bao682c34b2015-04-07 17:16:35 -0700408 for (int i = max-1; i >= 0; --i) {
Tao Bao80e46e02015-06-03 10:49:29 -0700409 std::string old_log = android::base::StringPrintf("%s", LAST_LOG_FILE);
410 if (i > 0) {
411 old_log += "." + std::to_string(i);
412 }
Tao Baobef39712015-05-04 18:50:27 -0700413 std::string new_log = android::base::StringPrintf("%s.%d", LAST_LOG_FILE, i+1);
414 // Ignore errors if old_log doesn't exist.
415 rename(old_log.c_str(), new_log.c_str());
416
Tao Bao80e46e02015-06-03 10:49:29 -0700417 std::string old_kmsg = android::base::StringPrintf("%s", LAST_KMSG_FILE);
418 if (i > 0) {
419 old_kmsg += "." + std::to_string(i);
420 }
Tao Baobef39712015-05-04 18:50:27 -0700421 std::string new_kmsg = android::base::StringPrintf("%s.%d", LAST_KMSG_FILE, i+1);
422 rename(old_kmsg.c_str(), new_kmsg.c_str());
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700423 }
424}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700425
Tao Bao682c34b2015-04-07 17:16:35 -0700426static void copy_logs() {
427 // We only rotate and record the log of the current session if there are
428 // actual attempts to modify the flash, such as wipes, installs from BCB
429 // or menu selections. This is to avoid unnecessary rotation (and
430 // possible deletion) of log files, if it does not do anything loggable.
431 if (!modified_flash) {
432 return;
433 }
434
Tao Baobef39712015-05-04 18:50:27 -0700435 rotate_logs(KEEP_LOG_COUNT);
Tao Bao682c34b2015-04-07 17:16:35 -0700436
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700437 // Copy logs to cache so the system can find out what happened.
Tao Baof0124322015-04-11 02:04:11 +0000438 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
439 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
440 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
441 save_kernel_log(LAST_KMSG_FILE);
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700442 chmod(LOG_FILE, 0600);
443 chown(LOG_FILE, 1000, 1000); // system user
Tao Baof0124322015-04-11 02:04:11 +0000444 chmod(LAST_KMSG_FILE, 0600);
445 chown(LAST_KMSG_FILE, 1000, 1000); // system user
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700446 chmod(LAST_LOG_FILE, 0640);
447 chmod(LAST_INSTALL_FILE, 0644);
448 sync();
449}
450
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800451// clear the recovery command and prepare to boot a (hopefully working) system,
452// copy our log file to cache as well (for the system to read), and
453// record any intent we were asked to communicate back to the system.
454// this function is idempotent: call it as many times as you like.
455static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800456finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800457 // By this point, we're ready to return to the main system...
458 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700459 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000460 if (fp == NULL) {
461 LOGE("Can't open %s\n", INTENT_FILE);
462 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800463 fputs(send_intent, fp);
464 check_and_fclose(fp, INTENT_FILE);
465 }
466 }
467
Doug Zongker4f33e552012-08-23 13:16:12 -0700468 // Save the locale to cache, so if recovery is next started up
469 // without a --locale argument (eg, directly from the bootloader)
470 // it will use the last-known locale.
471 if (locale != NULL) {
472 LOGI("Saving locale \"%s\"\n", locale);
473 FILE* fp = fopen_path(LOCALE_FILE, "w");
474 fwrite(locale, 1, strlen(locale), fp);
475 fflush(fp);
476 fsync(fileno(fp));
477 check_and_fclose(fp, LOCALE_FILE);
478 }
479
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700480 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800481
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700482 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800483 struct bootloader_message boot;
484 memset(&boot, 0, sizeof(boot));
485 set_bootloader_message(&boot);
486
487 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700488 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
489 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800490 LOGW("Can't unlink %s\n", COMMAND_FILE);
491 }
492
Doug Zongkerd0181b82011-10-19 10:51:12 -0700493 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494 sync(); // For good measure.
495}
496
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700497typedef struct _saved_log_file {
498 char* name;
499 struct stat st;
500 unsigned char* data;
501 struct _saved_log_file* next;
502} saved_log_file;
503
Elliott Hughes945548e2015-06-05 17:59:56 -0700504static bool erase_volume(const char* volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700505 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
506
Doug Zongker02ec6b82012-08-22 17:26:40 -0700507 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700508 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700509
510 saved_log_file* head = NULL;
511
512 if (is_cache) {
Tao Baobef39712015-05-04 18:50:27 -0700513 // If we're reformatting /cache, we load any past logs
514 // (i.e. "/cache/recovery/last_*") and the current log
515 // ("/cache/recovery/log") into memory, so we can restore them after
516 // the reformat.
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700517
518 ensure_path_mounted(volume);
519
520 DIR* d;
521 struct dirent* de;
522 d = opendir(CACHE_LOG_DIR);
523 if (d) {
524 char path[PATH_MAX];
525 strcpy(path, CACHE_LOG_DIR);
526 strcat(path, "/");
527 int path_len = strlen(path);
528 while ((de = readdir(d)) != NULL) {
Tao Baobef39712015-05-04 18:50:27 -0700529 if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700530 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
531 strcpy(path+path_len, de->d_name);
532 p->name = strdup(path);
533 if (stat(path, &(p->st)) == 0) {
534 // truncate files to 512kb
535 if (p->st.st_size > (1 << 19)) {
536 p->st.st_size = 1 << 19;
537 }
538 p->data = (unsigned char*) malloc(p->st.st_size);
539 FILE* f = fopen(path, "rb");
540 fread(p->data, 1, p->st.st_size, f);
541 fclose(f);
542 p->next = head;
543 head = p;
544 } else {
545 free(p);
546 }
547 }
548 }
549 closedir(d);
550 } else {
551 if (errno != ENOENT) {
552 printf("opendir failed: %s\n", strerror(errno));
553 }
554 }
555 }
556
Doug Zongker211aebc2011-10-28 15:13:10 -0700557 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700558
Doug Zongkerd0181b82011-10-19 10:51:12 -0700559 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700560 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700561
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700562 if (is_cache) {
563 while (head) {
564 FILE* f = fopen_path(head->name, "wb");
565 if (f) {
566 fwrite(head->data, 1, head->st.st_size, f);
567 fclose(f);
568 chmod(head->name, head->st.st_mode);
569 chown(head->name, head->st.st_uid, head->st.st_gid);
570 }
571 free(head->name);
572 free(head->data);
573 saved_log_file* temp = head->next;
574 free(head);
575 head = temp;
576 }
577
Tao Baof0124322015-04-11 02:04:11 +0000578 // Any part of the log we'd copied to cache is now gone.
579 // Reset the pointer so we copy from the beginning of the temp
580 // log.
581 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700582 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700583 }
584
Elliott Hughes945548e2015-06-05 17:59:56 -0700585 return (result == 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800586}
587
Doug Zongkerf93d8162009-09-22 15:16:02 -0700588static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700589get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700590 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700591 // throw away keys pressed previously, so user doesn't
592 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700593 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700594
Doug Zongker211aebc2011-10-28 15:13:10 -0700595 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700596 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800597 int chosen_item = -1;
598
Doug Zongkerf93d8162009-09-22 15:16:02 -0700599 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700600 int key = ui->WaitKey();
601 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800602
Doug Zongker5cae4452011-01-25 13:15:30 -0800603 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700604 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800605 continue;
606 } else {
607 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700608 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700609 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800610 }
611 }
612
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700613 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700614
615 if (action < 0) {
616 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700617 case Device::kHighlightUp:
Elliott Hughes642aaa72015-04-10 12:47:46 -0700618 selected = ui->SelectMenu(--selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700619 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700620 case Device::kHighlightDown:
Elliott Hughes642aaa72015-04-10 12:47:46 -0700621 selected = ui->SelectMenu(++selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700622 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700623 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700624 chosen_item = selected;
625 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700626 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700627 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800628 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700629 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700630 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800631 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700632 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800633
Doug Zongker211aebc2011-10-28 15:13:10 -0700634 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700635 return chosen_item;
636}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800637
Doug Zongker8674a722010-09-15 11:08:23 -0700638static int compare_string(const void* a, const void* b) {
639 return strcmp(*(const char**)a, *(const char**)b);
640}
641
Doug Zongker93950222014-07-08 14:10:23 -0700642// Returns a malloc'd path, or NULL.
Elliott Hughes30694c92015-03-25 15:16:51 -0700643static char* browse_directory(const char* path, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700644 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700645
Elliott Hughes30694c92015-03-25 15:16:51 -0700646 DIR* d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700647 if (d == NULL) {
648 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongker93950222014-07-08 14:10:23 -0700649 return NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700650 }
651
Doug Zongker8674a722010-09-15 11:08:23 -0700652 int d_size = 0;
653 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700654 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700655 int z_size = 1;
656 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700657 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700658 zips[0] = strdup("../");
659
Elliott Hughes30694c92015-03-25 15:16:51 -0700660 struct dirent* de;
Doug Zongker8674a722010-09-15 11:08:23 -0700661 while ((de = readdir(d)) != NULL) {
662 int name_len = strlen(de->d_name);
663
664 if (de->d_type == DT_DIR) {
665 // skip "." and ".." entries
666 if (name_len == 1 && de->d_name[0] == '.') continue;
667 if (name_len == 2 && de->d_name[0] == '.' &&
668 de->d_name[1] == '.') continue;
669
670 if (d_size >= d_alloc) {
671 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700672 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700673 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700674 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700675 strcpy(dirs[d_size], de->d_name);
676 dirs[d_size][name_len] = '/';
677 dirs[d_size][name_len+1] = '\0';
678 ++d_size;
679 } else if (de->d_type == DT_REG &&
680 name_len >= 4 &&
681 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
682 if (z_size >= z_alloc) {
683 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700684 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700685 }
686 zips[z_size++] = strdup(de->d_name);
687 }
688 }
689 closedir(d);
690
691 qsort(dirs, d_size, sizeof(char*), compare_string);
692 qsort(zips, z_size, sizeof(char*), compare_string);
693
694 // append dirs to the zips list
695 if (d_size + z_size + 1 > z_alloc) {
696 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700697 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700698 }
699 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
700 free(dirs);
701 z_size += d_size;
702 zips[z_size] = NULL;
703
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700704 const char* headers[] = { "Choose a package to install:", path, NULL };
Elliott Hughes30694c92015-03-25 15:16:51 -0700705
Doug Zongker93950222014-07-08 14:10:23 -0700706 char* result;
Doug Zongker8674a722010-09-15 11:08:23 -0700707 int chosen_item = 0;
Doug Zongker93950222014-07-08 14:10:23 -0700708 while (true) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700709 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700710
711 char* item = zips[chosen_item];
712 int item_len = strlen(item);
713 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700714 // go up but continue browsing (if the caller is update_directory)
Doug Zongker93950222014-07-08 14:10:23 -0700715 result = NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700716 break;
717 }
Doug Zongker93950222014-07-08 14:10:23 -0700718
719 char new_path[PATH_MAX];
720 strlcpy(new_path, path, PATH_MAX);
721 strlcat(new_path, "/", PATH_MAX);
722 strlcat(new_path, item, PATH_MAX);
723
724 if (item[item_len-1] == '/') {
725 // recurse down into a subdirectory
726 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
727 result = browse_directory(new_path, device);
728 if (result) break;
729 } else {
730 // selected a zip file: return the malloc'd path to the caller.
731 result = strdup(new_path);
732 break;
733 }
734 }
Doug Zongker8674a722010-09-15 11:08:23 -0700735
Elliott Hughes30694c92015-03-25 15:16:51 -0700736 for (int i = 0; i < z_size; ++i) free(zips[i]);
Doug Zongker8674a722010-09-15 11:08:23 -0700737 free(zips);
Doug Zongker8674a722010-09-15 11:08:23 -0700738
739 return result;
740}
741
Elliott Hughes30694c92015-03-25 15:16:51 -0700742static bool yes_no(Device* device, const char* question1, const char* question2) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700743 const char* headers[] = { question1, question2, NULL };
Elliott Hughes30694c92015-03-25 15:16:51 -0700744 const char* items[] = { " No", " Yes", NULL };
Doug Zongkerddd6a282009-06-09 12:22:33 -0700745
Elliott Hughes30694c92015-03-25 15:16:51 -0700746 int chosen_item = get_menu_selection(headers, items, 1, 0, device);
747 return (chosen_item == 1);
748}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800749
Tao Baoe39a9bc2015-03-31 12:19:05 -0700750// Return true on success.
751static bool wipe_data(int should_confirm, Device* device) {
752 if (should_confirm && !yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!")) {
753 return false;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700754 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700755
Tao Bao682c34b2015-04-07 17:16:35 -0700756 modified_flash = true;
757
Doug Zongker211aebc2011-10-28 15:13:10 -0700758 ui->Print("\n-- Wiping data...\n");
Elliott Hughes945548e2015-06-05 17:59:56 -0700759 bool success =
760 device->PreWipeData() &&
761 erase_volume("/data") &&
762 erase_volume("/cache") &&
763 device->PostWipeData();
764 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
765 return success;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700766}
767
Tao Baoe39a9bc2015-03-31 12:19:05 -0700768// Return true on success.
769static bool wipe_cache(bool should_confirm, Device* device) {
Elliott Hughes30694c92015-03-25 15:16:51 -0700770 if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) {
Tao Baoe39a9bc2015-03-31 12:19:05 -0700771 return false;
Elliott Hughes30694c92015-03-25 15:16:51 -0700772 }
773
Tao Bao682c34b2015-04-07 17:16:35 -0700774 modified_flash = true;
775
Elliott Hughes30694c92015-03-25 15:16:51 -0700776 ui->Print("\n-- Wiping cache...\n");
Elliott Hughes945548e2015-06-05 17:59:56 -0700777 bool success = erase_volume("/cache");
778 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
779 return success;
Elliott Hughes30694c92015-03-25 15:16:51 -0700780}
781
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700782static void choose_recovery_file(Device* device) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700783 // "Back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry
784 char* entries[1 + KEEP_LOG_COUNT * 2 + 1];
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700785 memset(entries, 0, sizeof(entries));
786
Tao Baobef39712015-05-04 18:50:27 -0700787 unsigned int n = 0;
Patrick Tjincd055ee2014-12-09 11:26:40 -0800788
Patrick Tjincd055ee2014-12-09 11:26:40 -0800789 // Add LAST_LOG_FILE + LAST_LOG_FILE.x
Tao Baobef39712015-05-04 18:50:27 -0700790 // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
791 for (int i = 0; i < KEEP_LOG_COUNT; i++) {
792 char* log_file;
Tao Bao80e46e02015-06-03 10:49:29 -0700793 int ret;
794 ret = (i == 0) ? asprintf(&log_file, "%s", LAST_LOG_FILE) :
795 asprintf(&log_file, "%s.%d", LAST_LOG_FILE, i);
796 if (ret == -1) {
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700797 // memory allocation failure - return early. Should never happen.
798 return;
799 }
Tao Baobef39712015-05-04 18:50:27 -0700800 if ((ensure_path_mounted(log_file) != 0) || (access(log_file, R_OK) == -1)) {
801 free(log_file);
802 } else {
803 entries[n++] = log_file;
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700804 }
Tao Baobef39712015-05-04 18:50:27 -0700805
806 char* kmsg_file;
Tao Bao80e46e02015-06-03 10:49:29 -0700807 ret = (i == 0) ? asprintf(&kmsg_file, "%s", LAST_KMSG_FILE) :
808 asprintf(&kmsg_file, "%s.%d", LAST_KMSG_FILE, i);
809 if (ret == -1) {
Tao Baobef39712015-05-04 18:50:27 -0700810 // memory allocation failure - return early. Should never happen.
811 return;
812 }
813 if ((ensure_path_mounted(kmsg_file) != 0) || (access(kmsg_file, R_OK) == -1)) {
814 free(kmsg_file);
815 } else {
816 entries[n++] = kmsg_file;
817 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700818 }
819
Elliott Hughesc0491632015-05-06 12:40:05 -0700820 entries[n++] = strdup("Back");
821
Tao Baobef39712015-05-04 18:50:27 -0700822 const char* headers[] = { "Select file to view", nullptr };
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700823
Elliott Hughes8de52072015-04-08 20:06:50 -0700824 while (true) {
Elliott Hughes30694c92015-03-25 15:16:51 -0700825 int chosen_item = get_menu_selection(headers, entries, 1, 0, device);
Elliott Hughesc0491632015-05-06 12:40:05 -0700826 if (strcmp(entries[chosen_item], "Back") == 0) break;
Elliott Hughes8de52072015-04-08 20:06:50 -0700827
Elliott Hughes8de52072015-04-08 20:06:50 -0700828 ui->ShowFile(entries[chosen_item]);
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700829 }
830
Tao Baobef39712015-05-04 18:50:27 -0700831 for (size_t i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) {
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700832 free(entries[i]);
833 }
834}
835
Tao Bao145d8612015-03-25 15:51:15 -0700836static int apply_from_sdcard(Device* device, bool* wipe_cache) {
Tao Bao682c34b2015-04-07 17:16:35 -0700837 modified_flash = true;
838
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000839 if (ensure_path_mounted(SDCARD_ROOT) != 0) {
840 ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT);
841 return INSTALL_ERROR;
842 }
843
844 char* path = browse_directory(SDCARD_ROOT, device);
845 if (path == NULL) {
Elliott Hughes018ed312015-04-08 16:51:36 -0700846 ui->Print("\n-- No package file selected.\n");
caozhiyuanb4effb92015-06-10 16:46:38 +0800847 ensure_path_unmounted(SDCARD_ROOT);
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000848 return INSTALL_ERROR;
849 }
850
851 ui->Print("\n-- Install %s ...\n", path);
852 set_sdcard_update_bootloader_message();
853 void* token = start_sdcard_fuse(path);
854
855 int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache,
856 TEMPORARY_INSTALL_FILE, false);
857
858 finish_sdcard_fuse(token);
859 ensure_path_unmounted(SDCARD_ROOT);
860 return status;
861}
862
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700863// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
864// means to take the default, which is to reboot or shutdown depending
865// on if the --shutdown_after flag was passed to recovery.
866static Device::BuiltinAction
Doug Zongker6c8553d2012-09-24 10:40:47 -0700867prompt_and_wait(Device* device, int status) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700868 for (;;) {
869 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700870 switch (status) {
871 case INSTALL_SUCCESS:
872 case INSTALL_NONE:
873 ui->SetBackground(RecoveryUI::NO_COMMAND);
874 break;
875
876 case INSTALL_ERROR:
877 case INSTALL_CORRUPT:
878 ui->SetBackground(RecoveryUI::ERROR);
879 break;
880 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700881 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700882
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700883 int chosen_item = get_menu_selection(nullptr, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700884
885 // device-specific code may take some action here. It may
886 // return one of the core actions handled in the switch
887 // statement below.
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700888 Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700889
Elliott Hughes30694c92015-03-25 15:16:51 -0700890 bool should_wipe_cache = false;
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700891 switch (chosen_action) {
892 case Device::NO_ACTION:
893 break;
894
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700895 case Device::REBOOT:
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700896 case Device::SHUTDOWN:
897 case Device::REBOOT_BOOTLOADER:
898 return chosen_action;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700899
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700900 case Device::WIPE_DATA:
901 wipe_data(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700902 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700903 break;
904
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700905 case Device::WIPE_CACHE:
Elliott Hughes30694c92015-03-25 15:16:51 -0700906 wipe_cache(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700907 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700908 break;
909
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000910 case Device::APPLY_ADB_SIDELOAD:
Elliott Hughesec283402015-04-10 10:01:53 -0700911 case Device::APPLY_SDCARD:
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000912 {
913 bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
914 if (adb) {
Elliott Hughes30694c92015-03-25 15:16:51 -0700915 status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700916 } else {
Elliott Hughes30694c92015-03-25 15:16:51 -0700917 status = apply_from_sdcard(device, &should_wipe_cache);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700918 }
Doug Zongker945fc682014-07-10 10:50:39 -0700919
Elliott Hughes30694c92015-03-25 15:16:51 -0700920 if (status == INSTALL_SUCCESS && should_wipe_cache) {
Tao Baoe39a9bc2015-03-31 12:19:05 -0700921 if (!wipe_cache(false, device)) {
922 status = INSTALL_ERROR;
923 }
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000924 }
925
Tao Baoc679f932015-03-30 09:43:49 -0700926 if (status != INSTALL_SUCCESS) {
927 ui->SetBackground(RecoveryUI::ERROR);
928 ui->Print("Installation aborted.\n");
929 copy_logs();
930 } else if (!ui->IsTextVisible()) {
931 return Device::NO_ACTION; // reboot if logs aren't visible
932 } else {
933 ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
Doug Zongker8674a722010-09-15 11:08:23 -0700934 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700935 }
936 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700937
Elliott Hughesec283402015-04-10 10:01:53 -0700938 case Device::VIEW_RECOVERY_LOGS:
939 choose_recovery_file(device);
Michael Ward9d2629c2011-06-23 22:14:24 -0700940 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800941
Elliott Hughesec283402015-04-10 10:01:53 -0700942 case Device::MOUNT_SYSTEM:
Tao Baoabb8f772015-07-30 14:43:27 -0700943 char system_root_image[PROPERTY_VALUE_MAX];
944 property_get("ro.build.system_root_image", system_root_image, "");
945
946 // For a system image built with the root directory (i.e.
947 // system_root_image == "true"), we mount it to /system_root, and symlink /system
948 // to /system_root/system to make adb shell work (the symlink is created through
949 // the build system).
950 // Bug: 22855115
951 if (strcmp(system_root_image, "true") == 0) {
952 if (ensure_path_mounted_at("/", "/system_root") != -1) {
953 ui->Print("Mounted /system.\n");
954 }
955 } else {
956 if (ensure_path_mounted("/system") != -1) {
957 ui->Print("Mounted /system.\n");
958 }
Elliott Hughesec283402015-04-10 10:01:53 -0700959 }
Tao Baoabb8f772015-07-30 14:43:27 -0700960
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700961 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800962 }
963 }
964}
965
966static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800967print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700968 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800969}
970
Doug Zongker02ec6b82012-08-22 17:26:40 -0700971static void
972load_locale_from_cache() {
973 FILE* fp = fopen_path(LOCALE_FILE, "r");
974 char buffer[80];
975 if (fp != NULL) {
976 fgets(buffer, sizeof(buffer), fp);
977 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700978 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700979 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
980 if (!isspace(buffer[i])) {
981 buffer[j++] = buffer[i];
982 }
983 }
984 buffer[j] = 0;
985 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700986 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700987 }
988}
989
Doug Zongker7c3ae452013-05-14 11:03:02 -0700990static RecoveryUI* gCurrentUI = NULL;
991
992void
993ui_print(const char* format, ...) {
994 char buffer[256];
995
996 va_list ap;
997 va_start(ap, format);
998 vsnprintf(buffer, sizeof(buffer), format, ap);
999 va_end(ap);
1000
1001 if (gCurrentUI != NULL) {
1002 gCurrentUI->Print("%s", buffer);
1003 } else {
1004 fputs(buffer, stdout);
1005 }
1006}
1007
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001008int
Oscar Montemayor05231562009-11-30 08:40:57 -08001009main(int argc, char **argv) {
Doug Zongker9270a202012-01-09 15:16:13 -08001010 // If this binary is started with the single argument "--adbd",
1011 // instead of being the normal recovery binary, it turns into kind
1012 // of a stripped-down version of adbd that only supports the
1013 // 'sideload' command. Note this must be a real argument, not
1014 // anything in the command file or bootloader control block; the
1015 // only way recovery should be run with this argument is when it
1016 // starts a copy of itself from the apply_from_adb() function.
1017 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
Elliott Hughes9f4fdb32015-11-20 13:03:24 -08001018 adb_server_main(0, DEFAULT_ADB_PORT, -1);
Doug Zongker9270a202012-01-09 15:16:13 -08001019 return 0;
1020 }
1021
Tao Bao04ca4262015-09-10 15:32:24 -07001022 time_t start = time(NULL);
1023
1024 // redirect_stdio should be called only in non-sideload mode. Otherwise
1025 // we may have two logger instances with different timestamps.
1026 redirect_stdio(TEMPORARY_LOG_FILE);
1027
Doug Zongker19a8e242014-01-21 09:25:41 -08001028 printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001029
Doug Zongkerd4208f92010-09-20 12:16:13 -07001030 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001031 get_args(&argc, &argv);
1032
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001033 const char *send_intent = NULL;
1034 const char *update_package = NULL;
Tao Baoe39a9bc2015-03-31 12:19:05 -07001035 bool should_wipe_data = false;
Tao Baoc679f932015-03-30 09:43:49 -07001036 bool should_wipe_cache = false;
Tao Bao145d8612015-03-25 15:51:15 -07001037 bool show_text = false;
Tao Baoc679f932015-03-30 09:43:49 -07001038 bool sideload = false;
1039 bool sideload_auto_reboot = false;
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001040 bool just_exit = false;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001041 bool shutdown_after = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001042
1043 int arg;
1044 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
1045 switch (arg) {
Tao Baoc679f932015-03-30 09:43:49 -07001046 case 'i': send_intent = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001047 case 'u': update_package = optarg; break;
Tao Baoe39a9bc2015-03-31 12:19:05 -07001048 case 'w': should_wipe_data = true; break;
Tao Baoc679f932015-03-30 09:43:49 -07001049 case 'c': should_wipe_cache = true; break;
Tao Bao145d8612015-03-25 15:51:15 -07001050 case 't': show_text = true; break;
Tao Baoc679f932015-03-30 09:43:49 -07001051 case 's': sideload = true; break;
1052 case 'a': sideload = true; sideload_auto_reboot = true; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001053 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -07001054 case 'l': locale = optarg; break;
Doug Zongkerc87bab12013-11-25 13:53:25 -08001055 case 'g': {
1056 if (stage == NULL || *stage == '\0') {
1057 char buffer[20] = "1/";
1058 strncat(buffer, optarg, sizeof(buffer)-3);
1059 stage = strdup(buffer);
1060 }
1061 break;
1062 }
Doug Zongkerb1d12632014-03-18 10:32:12 -07001063 case 'p': shutdown_after = true; break;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -07001064 case 'r': reason = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001065 case '?':
1066 LOGE("Invalid command argument\n");
1067 continue;
1068 }
1069 }
1070
Doug Zongker02ec6b82012-08-22 17:26:40 -07001071 if (locale == NULL) {
1072 load_locale_from_cache();
1073 }
1074 printf("locale is [%s]\n", locale);
Doug Zongker0d32f252014-02-13 15:07:56 -08001075 printf("stage is [%s]\n", stage);
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -07001076 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001077
1078 Device* device = make_device();
1079 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -07001080 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -07001081
Doug Zongker5fa8c232012-09-18 12:37:02 -07001082 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001083 ui->Init();
Doug Zongkerc87bab12013-11-25 13:53:25 -08001084
1085 int st_cur, st_max;
1086 if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
1087 ui->SetStage(st_cur, st_max);
1088 }
1089
Doug Zongker02ec6b82012-08-22 17:26:40 -07001090 ui->SetBackground(RecoveryUI::NONE);
1091 if (show_text) ui->ShowText(true);
1092
Stephen Smalley779701d2012-02-09 14:13:23 -05001093 struct selinux_opt seopts[] = {
1094 { SELABEL_OPT_PATH, "/file_contexts" }
1095 };
1096
1097 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
1098
1099 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001100 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -05001101 }
Stephen Smalley779701d2012-02-09 14:13:23 -05001102
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001103 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -08001104
Doug Zongker56c51052010-07-01 09:18:44 -07001105 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001106 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -07001107 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001108 }
Doug Zongker9b125b02010-09-22 12:01:37 -07001109 printf("\n");
1110
1111 if (update_package) {
1112 // For backwards compatibility on the cache partition only, if
1113 // we're given an old 'root' path "CACHE:foo", change it to
1114 // "/cache/foo".
1115 if (strncmp(update_package, "CACHE:", 6) == 0) {
1116 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -07001117 char* modified_path = (char*)malloc(len);
Jeremy Compostella1b7d9b72015-07-29 17:17:03 +02001118 if (modified_path) {
1119 strlcpy(modified_path, "/cache/", len);
1120 strlcat(modified_path, update_package+6, len);
1121 printf("(replacing path \"%s\" with \"%s\")\n",
1122 update_package, modified_path);
1123 update_package = modified_path;
1124 }
1125 else
1126 printf("modified_path allocation failed\n");
Doug Zongker9b125b02010-09-22 12:01:37 -07001127 }
1128 }
1129 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001130
1131 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -07001132 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001133
Elliott Hughesbb78d622015-04-09 20:51:08 -07001134 ui->Print("Supported API: %d\n", RECOVERY_API_VERSION);
1135
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001136 int status = INSTALL_SUCCESS;
1137
Doug Zongker540d57f2011-01-18 13:36:58 -08001138 if (update_package != NULL) {
Tao Baoc679f932015-03-30 09:43:49 -07001139 status = install_package(update_package, &should_wipe_cache, TEMPORARY_INSTALL_FILE, true);
1140 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1141 wipe_cache(false, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001142 }
Doug Zongker7c3ae452013-05-14 11:03:02 -07001143 if (status != INSTALL_SUCCESS) {
1144 ui->Print("Installation aborted.\n");
1145
1146 // If this is an eng or userdebug build, then automatically
1147 // turn the text display on if the script fails so the error
1148 // message is visible.
Elliott Hughesf14af802015-02-10 14:46:14 -08001149 if (is_ro_debuggable()) {
Doug Zongker7c3ae452013-05-14 11:03:02 -07001150 ui->ShowText(true);
1151 }
1152 }
Tao Baoe39a9bc2015-03-31 12:19:05 -07001153 } else if (should_wipe_data) {
1154 if (!wipe_data(false, device)) {
1155 status = INSTALL_ERROR;
1156 }
Tao Baoc679f932015-03-30 09:43:49 -07001157 } else if (should_wipe_cache) {
Tao Baoe39a9bc2015-03-31 12:19:05 -07001158 if (!wipe_cache(false, device)) {
1159 status = INSTALL_ERROR;
1160 }
Tao Baoc679f932015-03-30 09:43:49 -07001161 } else if (sideload) {
1162 // 'adb reboot sideload' acts the same as user presses key combinations
1163 // to enter the sideload mode. When 'sideload-auto-reboot' is used, text
1164 // display will NOT be turned on by default. And it will reboot after
1165 // sideload finishes even if there are errors. Unless one turns on the
1166 // text display during the installation. This is to enable automated
1167 // testing.
1168 if (!sideload_auto_reboot) {
1169 ui->ShowText(true);
1170 }
1171 status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE);
1172 if (status == INSTALL_SUCCESS && should_wipe_cache) {
Tao Baoe39a9bc2015-03-31 12:19:05 -07001173 if (!wipe_cache(false, device)) {
1174 status = INSTALL_ERROR;
1175 }
Tao Baoc679f932015-03-30 09:43:49 -07001176 }
1177 ui->Print("\nInstall from ADB complete (status: %d).\n", status);
1178 if (sideload_auto_reboot) {
1179 ui->Print("Rebooting automatically.\n");
1180 }
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001181 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001182 status = INSTALL_NONE; // No command specified
1183 ui->SetBackground(RecoveryUI::NO_COMMAND);
Tao Bao785d22c2015-05-04 09:34:29 -07001184
1185 // http://b/17489952
1186 // If this is an eng or userdebug build, automatically turn on the
1187 // text display if no command is specified.
1188 if (is_ro_debuggable()) {
1189 ui->ShowText(true);
1190 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001191 }
1192
Tao Baoc679f932015-03-30 09:43:49 -07001193 if (!sideload_auto_reboot && (status == INSTALL_ERROR || status == INSTALL_CORRUPT)) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001194 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001195 ui->SetBackground(RecoveryUI::ERROR);
1196 }
Tao Baoc679f932015-03-30 09:43:49 -07001197
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001198 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
Tao Baoc679f932015-03-30 09:43:49 -07001199 if ((status != INSTALL_SUCCESS && !sideload_auto_reboot) || ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001200 Device::BuiltinAction temp = prompt_and_wait(device, status);
Tao Baoc679f932015-03-30 09:43:49 -07001201 if (temp != Device::NO_ACTION) {
1202 after = temp;
1203 }
Doug Zongker8674a722010-09-15 11:08:23 -07001204 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001205
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001206 // Save logs and clean up before rebooting or shutting down.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001207 finish_recovery(send_intent);
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001208
1209 switch (after) {
1210 case Device::SHUTDOWN:
1211 ui->Print("Shutting down...\n");
1212 property_set(ANDROID_RB_PROPERTY, "shutdown,");
1213 break;
1214
1215 case Device::REBOOT_BOOTLOADER:
1216 ui->Print("Rebooting to bootloader...\n");
1217 property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");
1218 break;
1219
1220 default:
1221 ui->Print("Rebooting...\n");
1222 property_set(ANDROID_RB_PROPERTY, "reboot,");
1223 break;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001224 }
Tao Bao75238632015-05-27 14:46:17 -07001225 while (true) {
1226 pause();
1227 }
1228 // Should be unreachable.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001229 return EXIT_SUCCESS;
1230}