blob: c6e12702d15cca3af31c1340f3b6793f6f3688c3 [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>
Doug Zongker23ceeea2010-07-08 17:27:55 -070028#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
32
33#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "common.h"
35#include "cutils/properties.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040036#ifdef ANDROID_RB_RESTART
Ken Sumrall6e4472a2011-03-07 23:37:27 -080037#include "cutils/android_reboot.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040038#else
39#include <sys/reboot.h>
40#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041#include "install.h"
42#include "minui/minui.h"
43#include "minzip/DirUtil.h"
44#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070045#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070046#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070047#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080048#include "adb_install.h"
49extern "C" {
50#include "minadbd/adb.h"
Doug Zongker945fc682014-07-10 10:50:39 -070051#include "fuse_sideload.h"
52#include "fuse_sdcard_provider.h"
Doug Zongker9270a202012-01-09 15:16:13 -080053}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054
Dees_Troy7d15c252012-09-05 20:47:21 -040055extern "C" {
Dees_Troy7d15c252012-09-05 20:47:21 -040056#include "data.h"
57#include "gui/gui.h"
58}
59#include "partitions.hpp"
Dees_Troy51127312012-09-08 13:08:49 -040060#include "variables.h"
Dees_Troy812660f2012-09-20 09:55:17 -040061#include "openrecoveryscript.hpp"
Dees_Troya58bead2012-09-27 09:49:29 -040062#include "twrp-functions.hpp"
Dees_Troy7d15c252012-09-05 20:47:21 -040063
Dees_Troy5bf43922012-09-07 16:07:55 -040064TWPartitionManager PartitionManager;
Dees_Troy7d15c252012-09-05 20:47:21 -040065
Stephen Smalley779701d2012-02-09 14:13:23 -050066struct selabel_handle *sehandle;
67
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050068
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069static const struct option OPTIONS[] = {
70 { "send_intent", required_argument, NULL, 's' },
71 { "update_package", required_argument, NULL, 'u' },
72 { "wipe_data", no_argument, NULL, 'w' },
73 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070074 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070075 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070076 { "locale", required_argument, NULL, 'l' },
Doug Zongkerc87bab12013-11-25 13:53:25 -080077 { "stages", required_argument, NULL, 'g' },
Doug Zongkerb1d12632014-03-18 10:32:12 -070078 { "shutdown_after", no_argument, NULL, 'p' },
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070079 { "reason", required_argument, NULL, 'r' },
Doug Zongker988500b2009-10-06 14:41:38 -070080 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081};
82
Doug Zongkerda1ebae2013-05-16 11:23:48 -070083#define LAST_LOG_FILE "/cache/recovery/last_log"
84
Doug Zongker6d0d7ac2013-07-09 13:34:55 -070085static const char *CACHE_LOG_DIR = "/cache/recovery";
Doug Zongkerd4208f92010-09-20 12:16:13 -070086static const char *COMMAND_FILE = "/cache/recovery/command";
87static const char *INTENT_FILE = "/cache/recovery/intent";
88static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070089static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070090static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070091static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070092static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070094static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095
Nick Kralevichb8344b62014-10-22 18:38:48 -070096#define KEEP_LOG_COUNT 10
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097
Doug Zongker211aebc2011-10-28 15:13:10 -070098RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070099char* locale = NULL;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700100char recovery_version[PROPERTY_VALUE_MAX+1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800101char* stage = NULL;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -0700102char* reason = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -0700103
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104/*
105 * The recovery tool communicates with the main system through /cache files.
106 * /cache/recovery/command - INPUT - command line for tool, one arg per line
107 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
108 * /cache/recovery/intent - OUTPUT - intent that was passed in
109 *
110 * The arguments which may be supplied in the recovery.command file:
111 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -0700112 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113 * --wipe_data - erase user data (and cache), then reboot
114 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -0800115 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700116 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 *
118 * After completing, we remove /cache/recovery/command and reboot.
119 * Arguments may also be supplied in the bootloader control block (BCB).
120 * These important scenarios must be safely restartable at any point:
121 *
122 * FACTORY RESET
123 * 1. user selects "factory reset"
124 * 2. main system writes "--wipe_data" to /cache/recovery/command
125 * 3. main system reboots into recovery
126 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
127 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700128 * 5. erase_volume() reformats /data
129 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 * 7. finish_recovery() erases BCB
131 * -- after this, rebooting will restart the main system --
132 * 8. main() calls reboot() to boot main system
133 *
134 * OTA INSTALL
135 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700136 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 * 3. main system reboots into recovery
138 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
139 * -- after this, rebooting will attempt to reinstall the update --
140 * 5. install_package() attempts to install the update
141 * NOTE: the package install must itself be restartable from any point
142 * 6. finish_recovery() erases BCB
143 * -- after this, rebooting will (try to) restart the main system --
144 * 7. ** if install failed **
145 * 7a. prompt_and_wait() shows an error icon and waits for the user
146 * 7b; the user reboots (pulling the battery, etc) into the main system
147 * 8. main() calls maybe_install_firmware_update()
148 * ** if the update contained radio/hboot firmware **:
149 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
150 * -- after this, rebooting will reformat cache & restart main system --
151 * 8b. m_i_f_u() writes firmware image into raw cache partition
152 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
153 * -- after this, rebooting will attempt to reinstall firmware --
154 * 8d. bootloader tries to flash firmware
155 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
156 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700157 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 * 8g. finish_recovery() erases BCB
159 * -- after this, rebooting will (try to) restart the main system --
160 * 9. main() calls reboot() to boot main system
161 */
162
163static const int MAX_ARG_LENGTH = 4096;
164static const int MAX_ARGS = 100;
165
Doug Zongkerd4208f92010-09-20 12:16:13 -0700166// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700167FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700168fopen_path(const char *path, const char *mode) {
169 if (ensure_path_mounted(path) != 0) {
170 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171 return NULL;
172 }
173
174 // When writing, try to create the containing directory, if necessary.
175 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500176 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177
178 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179 return fp;
180}
181
Nick Kralevichb8344b62014-10-22 18:38:48 -0700182static void redirect_stdio(const char* filename) {
183 // If these fail, there's not really anywhere to complain...
184 freopen(filename, "a", stdout); setbuf(stdout, NULL);
185 freopen(filename, "a", stderr); setbuf(stderr, NULL);
186}
187
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188// close a file, log an error if the error indicator is set
189static void
190check_and_fclose(FILE *fp, const char *name) {
191 fflush(fp);
192 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
193 fclose(fp);
194}
195
196// command line args come from, in decreasing precedence:
197// - the actual command line
198// - the bootloader control block (one per line, after "recovery")
199// - the contents of COMMAND_FILE (one per line)
200static void
201get_args(int *argc, char ***argv) {
202 struct bootloader_message boot;
203 memset(&boot, 0, sizeof(boot));
204 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Doug Zongkerc87bab12013-11-25 13:53:25 -0800205 stage = strndup(boot.stage, sizeof(boot.stage));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206
207 if (boot.command[0] != 0 && boot.command[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700208 LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 }
210
211 if (boot.status[0] != 0 && boot.status[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700212 LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213 }
214
215 // --- if arguments weren't supplied, look in the bootloader control block
216 if (*argc <= 1) {
217 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
218 const char *arg = strtok(boot.recovery, "\n");
219 if (arg != NULL && !strcmp(arg, "recovery")) {
220 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
221 (*argv)[0] = strdup(arg);
222 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
223 if ((arg = strtok(NULL, "\n")) == NULL) break;
224 (*argv)[*argc] = strdup(arg);
225 }
226 LOGI("Got arguments from boot message\n");
227 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
228 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
229 }
230 }
231
232 // --- if that doesn't work, try the command file
233 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700234 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800235 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800236 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237 char *argv0 = (*argv)[0];
238 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
239 (*argv)[0] = argv0; // use the same program name
240
241 char buf[MAX_ARG_LENGTH];
242 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
243 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800244 token = strtok(buf, "\r\n");
245 if (token != NULL) {
246 (*argv)[*argc] = strdup(token); // Strip newline.
247 } else {
248 --*argc;
249 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250 }
251
252 check_and_fclose(fp, COMMAND_FILE);
253 LOGI("Got arguments from %s\n", COMMAND_FILE);
254 }
255 }
256
257 // --> write the arguments we have back into the bootloader control block
258 // always boot into recovery after this (until finish_recovery() is called)
259 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
260 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
261 int i;
262 for (i = 1; i < *argc; ++i) {
263 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
264 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
265 }
266 set_bootloader_message(&boot);
267}
268
Doug Zongker34c98df2009-08-18 12:05:45 -0700269static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800270set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700271 struct bootloader_message boot;
272 memset(&boot, 0, sizeof(boot));
273 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
274 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
275 set_bootloader_message(&boot);
276}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277
Doug Zongker2c3539e2010-09-29 13:21:30 -0700278// How much of the temp log we have copied to the copy in cache.
Dees_Troy7d15c252012-09-05 20:47:21 -0400279//static long tmplog_offset = 0;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700280
281static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700282copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700283 FILE *log = fopen_path(destination, append ? "a" : "w");
284 if (log == NULL) {
285 LOGE("Can't open %s\n", destination);
286 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700287 FILE *tmplog = fopen(source, "r");
288 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700289 if (append) {
290 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
291 }
292 char buf[4096];
293 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
294 if (append) {
295 tmplog_offset = ftell(tmplog);
296 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700297 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700298 }
299 check_and_fclose(log, destination);
300 }
301}
302
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700303// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
304// Overwrites any existing last_log.$max.
305static void
306rotate_last_logs(int max) {
307 char oldfn[256];
308 char newfn[256];
309
310 int i;
311 for (i = max-1; i >= 0; --i) {
312 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
313 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
314 // ignore errors
315 rename(oldfn, newfn);
316 }
317}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700318
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700319static void
320copy_logs() {
321 // Copy logs to cache so the system can find out what happened.
322 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
323 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
324 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
325 chmod(LOG_FILE, 0600);
326 chown(LOG_FILE, 1000, 1000); // system user
327 chmod(LAST_LOG_FILE, 0640);
328 chmod(LAST_INSTALL_FILE, 0644);
329 sync();
330}
331
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800332// clear the recovery command and prepare to boot a (hopefully working) system,
333// copy our log file to cache as well (for the system to read), and
334// record any intent we were asked to communicate back to the system.
335// this function is idempotent: call it as many times as you like.
336static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800337finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338 // By this point, we're ready to return to the main system...
339 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700340 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000341 if (fp == NULL) {
342 LOGE("Can't open %s\n", INTENT_FILE);
343 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344 fputs(send_intent, fp);
345 check_and_fclose(fp, INTENT_FILE);
346 }
347 }
348
Doug Zongker4f33e552012-08-23 13:16:12 -0700349 // Save the locale to cache, so if recovery is next started up
350 // without a --locale argument (eg, directly from the bootloader)
351 // it will use the last-known locale.
352 if (locale != NULL) {
353 LOGI("Saving locale \"%s\"\n", locale);
354 FILE* fp = fopen_path(LOCALE_FILE, "w");
355 fwrite(locale, 1, strlen(locale), fp);
356 fflush(fp);
357 fsync(fileno(fp));
358 check_and_fclose(fp, LOCALE_FILE);
359 }
360
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700361 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800362
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700363 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800364 struct bootloader_message boot;
365 memset(&boot, 0, sizeof(boot));
366 set_bootloader_message(&boot);
367
368 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700369 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
370 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800371 LOGW("Can't unlink %s\n", COMMAND_FILE);
372 }
373
Doug Zongkerd0181b82011-10-19 10:51:12 -0700374 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800375 sync(); // For good measure.
376}
377
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700378typedef struct _saved_log_file {
379 char* name;
380 struct stat st;
381 unsigned char* data;
382 struct _saved_log_file* next;
383} saved_log_file;
384
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800385static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700386erase_volume(const char *volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700387 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
388
Doug Zongker02ec6b82012-08-22 17:26:40 -0700389 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700390 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700391
392 saved_log_file* head = NULL;
393
394 if (is_cache) {
395 // If we're reformatting /cache, we load any
396 // "/cache/recovery/last*" files into memory, so we can restore
397 // them after the reformat.
398
399 ensure_path_mounted(volume);
400
401 DIR* d;
402 struct dirent* de;
403 d = opendir(CACHE_LOG_DIR);
404 if (d) {
405 char path[PATH_MAX];
406 strcpy(path, CACHE_LOG_DIR);
407 strcat(path, "/");
408 int path_len = strlen(path);
409 while ((de = readdir(d)) != NULL) {
410 if (strncmp(de->d_name, "last", 4) == 0) {
411 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
412 strcpy(path+path_len, de->d_name);
413 p->name = strdup(path);
414 if (stat(path, &(p->st)) == 0) {
415 // truncate files to 512kb
416 if (p->st.st_size > (1 << 19)) {
417 p->st.st_size = 1 << 19;
418 }
419 p->data = (unsigned char*) malloc(p->st.st_size);
420 FILE* f = fopen(path, "rb");
421 fread(p->data, 1, p->st.st_size, f);
422 fclose(f);
423 p->next = head;
424 head = p;
425 } else {
426 free(p);
427 }
428 }
429 }
430 closedir(d);
431 } else {
432 if (errno != ENOENT) {
433 printf("opendir failed: %s\n", strerror(errno));
434 }
435 }
436 }
437
Doug Zongker211aebc2011-10-28 15:13:10 -0700438 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700439
Doug Zongkerd0181b82011-10-19 10:51:12 -0700440 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700441 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700442
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700443 if (is_cache) {
444 while (head) {
445 FILE* f = fopen_path(head->name, "wb");
446 if (f) {
447 fwrite(head->data, 1, head->st.st_size, f);
448 fclose(f);
449 chmod(head->name, head->st.st_mode);
450 chown(head->name, head->st.st_uid, head->st.st_gid);
451 }
452 free(head->name);
453 free(head->data);
454 saved_log_file* temp = head->next;
455 free(head);
456 head = temp;
457 }
458
Doug Zongker2c3539e2010-09-29 13:21:30 -0700459 // Any part of the log we'd copied to cache is now gone.
460 // Reset the pointer so we copy from the beginning of the temp
461 // log.
462 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700463 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700464 }
465
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700466 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800467}
468
Doug Zongker28ce47c2011-10-28 10:33:05 -0700469static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700470prepend_title(const char* const* headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700471 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700472 // caller-provided headers.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700473 int count = 3; // our title has 3 lines
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700474 const char* const* p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700475 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700476
Doug Zongker28ce47c2011-10-28 10:33:05 -0700477 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
478 const char** h = new_headers;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700479 *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>";
480 *(h++) = recovery_version;
481 *(h++) = "";
Doug Zongkerf93d8162009-09-22 15:16:02 -0700482 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700483 *h = NULL;
484
Doug Zongkerf93d8162009-09-22 15:16:02 -0700485 return new_headers;
486}
487
488static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700489get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700490 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700491 // throw away keys pressed previously, so user doesn't
492 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700493 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700494
Doug Zongker211aebc2011-10-28 15:13:10 -0700495 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700496 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800497 int chosen_item = -1;
498
Doug Zongkerf93d8162009-09-22 15:16:02 -0700499 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700500 int key = ui->WaitKey();
501 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800502
Doug Zongker5cae4452011-01-25 13:15:30 -0800503 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700504 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800505 continue;
506 } else {
507 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700508 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700509 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800510 }
511 }
512
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700513 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700514
515 if (action < 0) {
516 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700517 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700518 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700519 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700520 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700521 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700522 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700523 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700524 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700525 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700526 chosen_item = selected;
527 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700528 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700529 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800530 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700531 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700532 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800533 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700534 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800535
Doug Zongker211aebc2011-10-28 15:13:10 -0700536 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700537 return chosen_item;
538}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800539
Doug Zongker8674a722010-09-15 11:08:23 -0700540static int compare_string(const void* a, const void* b) {
541 return strcmp(*(const char**)a, *(const char**)b);
542}
543
Doug Zongker93950222014-07-08 14:10:23 -0700544// Returns a malloc'd path, or NULL.
545static char*
546browse_directory(const char* path, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700547 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700548
Doug Zongker8674a722010-09-15 11:08:23 -0700549 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700550 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700551 "",
552 NULL };
553 DIR* d;
554 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700555 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700556 if (d == NULL) {
557 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongker93950222014-07-08 14:10:23 -0700558 return NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700559 }
560
Doug Zongker28ce47c2011-10-28 10:33:05 -0700561 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700562
563 int d_size = 0;
564 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700565 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700566 int z_size = 1;
567 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700568 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700569 zips[0] = strdup("../");
570
571 while ((de = readdir(d)) != NULL) {
572 int name_len = strlen(de->d_name);
573
574 if (de->d_type == DT_DIR) {
575 // skip "." and ".." entries
576 if (name_len == 1 && de->d_name[0] == '.') continue;
577 if (name_len == 2 && de->d_name[0] == '.' &&
578 de->d_name[1] == '.') continue;
579
580 if (d_size >= d_alloc) {
581 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700582 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700583 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700584 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700585 strcpy(dirs[d_size], de->d_name);
586 dirs[d_size][name_len] = '/';
587 dirs[d_size][name_len+1] = '\0';
588 ++d_size;
589 } else if (de->d_type == DT_REG &&
590 name_len >= 4 &&
591 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
592 if (z_size >= z_alloc) {
593 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700594 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700595 }
596 zips[z_size++] = strdup(de->d_name);
597 }
598 }
599 closedir(d);
600
601 qsort(dirs, d_size, sizeof(char*), compare_string);
602 qsort(zips, z_size, sizeof(char*), compare_string);
603
604 // append dirs to the zips list
605 if (d_size + z_size + 1 > z_alloc) {
606 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700607 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700608 }
609 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
610 free(dirs);
611 z_size += d_size;
612 zips[z_size] = NULL;
613
Doug Zongker93950222014-07-08 14:10:23 -0700614 char* result;
Doug Zongker8674a722010-09-15 11:08:23 -0700615 int chosen_item = 0;
Doug Zongker93950222014-07-08 14:10:23 -0700616 while (true) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700617 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700618
619 char* item = zips[chosen_item];
620 int item_len = strlen(item);
621 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700622 // go up but continue browsing (if the caller is update_directory)
Doug Zongker93950222014-07-08 14:10:23 -0700623 result = NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700624 break;
625 }
Doug Zongker93950222014-07-08 14:10:23 -0700626
627 char new_path[PATH_MAX];
628 strlcpy(new_path, path, PATH_MAX);
629 strlcat(new_path, "/", PATH_MAX);
630 strlcat(new_path, item, PATH_MAX);
631
632 if (item[item_len-1] == '/') {
633 // recurse down into a subdirectory
634 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
635 result = browse_directory(new_path, device);
636 if (result) break;
637 } else {
638 // selected a zip file: return the malloc'd path to the caller.
639 result = strdup(new_path);
640 break;
641 }
642 }
Doug Zongker8674a722010-09-15 11:08:23 -0700643
644 int i;
645 for (i = 0; i < z_size; ++i) free(zips[i]);
646 free(zips);
647 free(headers);
648
649 return result;
650}
651
Doug Zongkerf93d8162009-09-22 15:16:02 -0700652static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700653wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700654 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700655 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700656
Doug Zongkerf93d8162009-09-22 15:16:02 -0700657 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700658 const char* headers[] = { "Confirm wipe of all user data?",
659 " THIS CAN NOT BE UNDONE.",
660 "",
661 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700662 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700663 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800664
Doug Zongker28ce47c2011-10-28 10:33:05 -0700665 const char* items[] = { " No",
666 " No",
667 " No",
668 " No",
669 " No",
670 " No",
671 " No",
672 " Yes -- delete all user data", // [7]
673 " No",
674 " No",
675 " No",
676 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800677
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700678 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700679 if (chosen_item != 7) {
680 return;
681 }
682 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700683
Doug Zongker211aebc2011-10-28 15:13:10 -0700684 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700685 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700686 erase_volume("/data");
687 erase_volume("/cache");
Andres Moralesee193872014-08-05 19:49:09 -0700688 erase_persistent_partition();
Doug Zongker211aebc2011-10-28 15:13:10 -0700689 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700690}
691
Nick Kralevichb8344b62014-10-22 18:38:48 -0700692static void file_to_ui(const char* fn) {
693 FILE *fp = fopen_path(fn, "re");
694 if (fp == NULL) {
695 ui->Print(" Unable to open %s: %s\n", fn, strerror(errno));
696 return;
697 }
698 char line[1024];
699 int ct = 0;
700 redirect_stdio("/dev/null");
701 while(fgets(line, sizeof(line), fp) != NULL) {
702 ui->Print("%s", line);
703 ct++;
704 if (ct % 30 == 0) {
705 // give the user time to glance at the entries
706 ui->WaitKey();
707 }
708 }
709 redirect_stdio(TEMPORARY_LOG_FILE);
710 fclose(fp);
711}
712
713static void choose_recovery_file(Device* device) {
714 int i;
715 static const char** title_headers = NULL;
716 char *filename;
717 const char* headers[] = { "Select file to view",
718 "",
719 NULL };
720 char* entries[KEEP_LOG_COUNT + 2];
721 memset(entries, 0, sizeof(entries));
722
723 for (i = 0; i < KEEP_LOG_COUNT; i++) {
724 char *filename;
725 if (asprintf(&filename, (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i) == -1) {
726 // memory allocation failure - return early. Should never happen.
727 return;
728 }
729 if ((ensure_path_mounted(filename) != 0) || (access(filename, R_OK) == -1)) {
730 free(filename);
731 entries[i+1] = NULL;
732 break;
733 }
734 entries[i+1] = filename;
735 }
736
737 entries[0] = strdup("Go back");
738 title_headers = prepend_title((const char**)headers);
739
740 while(1) {
741 int chosen_item = get_menu_selection(title_headers, entries, 1, 0, device);
742 if (chosen_item == 0) break;
743 file_to_ui(entries[chosen_item]);
744 }
745
746 for (i = 0; i < KEEP_LOG_COUNT + 1; i++) {
747 free(entries[i]);
748 }
749}
750
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700751// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
752// means to take the default, which is to reboot or shutdown depending
753// on if the --shutdown_after flag was passed to recovery.
754static Device::BuiltinAction
Doug Zongker6c8553d2012-09-24 10:40:47 -0700755prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700756 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700757
758 for (;;) {
759 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700760 switch (status) {
761 case INSTALL_SUCCESS:
762 case INSTALL_NONE:
763 ui->SetBackground(RecoveryUI::NO_COMMAND);
764 break;
765
766 case INSTALL_ERROR:
767 case INSTALL_CORRUPT:
768 ui->SetBackground(RecoveryUI::ERROR);
769 break;
770 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700771 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700772
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700773 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700774
775 // device-specific code may take some action here. It may
776 // return one of the core actions handled in the switch
777 // statement below.
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700778 Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700779
Doug Zongker93950222014-07-08 14:10:23 -0700780 int wipe_cache = 0;
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700781 switch (chosen_action) {
782 case Device::NO_ACTION:
783 break;
784
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700785 case Device::REBOOT:
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700786 case Device::SHUTDOWN:
787 case Device::REBOOT_BOOTLOADER:
788 return chosen_action;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700789
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700790 case Device::WIPE_DATA:
791 wipe_data(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700792 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700793 break;
794
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700795 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700796 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700797 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700798 ui->Print("Cache wipe complete.\n");
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700799 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700800 break;
801
Doug Zongker93950222014-07-08 14:10:23 -0700802 case Device::APPLY_EXT: {
803 ensure_path_mounted(SDCARD_ROOT);
804 char* path = browse_directory(SDCARD_ROOT, device);
805 if (path == NULL) {
806 ui->Print("\n-- No package file selected.\n", path);
807 break;
808 }
809
810 ui->Print("\n-- Install %s ...\n", path);
811 set_sdcard_update_bootloader_message();
Doug Zongker945fc682014-07-10 10:50:39 -0700812 void* token = start_sdcard_fuse(path);
Doug Zongker93950222014-07-08 14:10:23 -0700813
Doug Zongker945fc682014-07-10 10:50:39 -0700814 int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache,
815 TEMPORARY_INSTALL_FILE, false);
816
817 finish_sdcard_fuse(token);
818 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker93950222014-07-08 14:10:23 -0700819
Doug Zongkerd0181b82011-10-19 10:51:12 -0700820 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700821 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700822 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700823 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700824 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700825 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700826 }
827 }
Doug Zongker945fc682014-07-10 10:50:39 -0700828
Doug Zongker8674a722010-09-15 11:08:23 -0700829 if (status >= 0) {
830 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700831 ui->SetBackground(RecoveryUI::ERROR);
832 ui->Print("Installation aborted.\n");
833 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700834 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker8674a722010-09-15 11:08:23 -0700835 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700836 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700837 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700838 }
839 break;
Doug Zongker93950222014-07-08 14:10:23 -0700840 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700841
842 case Device::APPLY_CACHE:
Doug Zongker93950222014-07-08 14:10:23 -0700843 ui->Print("\nAPPLY_CACHE is deprecated.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700844 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800845
Nick Kralevichb8344b62014-10-22 18:38:48 -0700846 case Device::READ_RECOVERY_LASTLOG:
847 choose_recovery_file(device);
Michael Ward9d2629c2011-06-23 22:14:24 -0700848 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800849
850 case Device::APPLY_ADB_SIDELOAD:
Doug Zongker9270a202012-01-09 15:16:13 -0800851 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
852 if (status >= 0) {
853 if (status != INSTALL_SUCCESS) {
854 ui->SetBackground(RecoveryUI::ERROR);
855 ui->Print("Installation aborted.\n");
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700856 copy_logs();
Doug Zongker9270a202012-01-09 15:16:13 -0800857 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700858 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker9270a202012-01-09 15:16:13 -0800859 } else {
860 ui->Print("\nInstall from ADB complete.\n");
861 }
862 }
863 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800864 }
865 }
866}
867
868static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800869print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700870 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800871}
872
Doug Zongker02ec6b82012-08-22 17:26:40 -0700873static void
874load_locale_from_cache() {
875 FILE* fp = fopen_path(LOCALE_FILE, "r");
876 char buffer[80];
877 if (fp != NULL) {
878 fgets(buffer, sizeof(buffer), fp);
879 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700880 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700881 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
882 if (!isspace(buffer[i])) {
883 buffer[j++] = buffer[i];
884 }
885 }
886 buffer[j] = 0;
887 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700888 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700889 }
890}
891
Doug Zongker7c3ae452013-05-14 11:03:02 -0700892static RecoveryUI* gCurrentUI = NULL;
893
894void
895ui_print(const char* format, ...) {
896 char buffer[256];
897
898 va_list ap;
899 va_start(ap, format);
900 vsnprintf(buffer, sizeof(buffer), format, ap);
901 va_end(ap);
902
903 if (gCurrentUI != NULL) {
904 gCurrentUI->Print("%s", buffer);
905 } else {
906 fputs(buffer, stdout);
907 }
908}
909
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800910int
Oscar Montemayor05231562009-11-30 08:40:57 -0800911main(int argc, char **argv) {
Dees_Troycfb63ae2012-09-19 14:30:17 -0400912 // Recovery needs to install world-readable files, so clear umask
913 // set by init
914 umask(0);
915
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800916 time_t start = time(NULL);
917
Nick Kralevichb8344b62014-10-22 18:38:48 -0700918 redirect_stdio(TEMPORARY_LOG_FILE);
Doug Zongker9270a202012-01-09 15:16:13 -0800919
920 // If this binary is started with the single argument "--adbd",
921 // instead of being the normal recovery binary, it turns into kind
922 // of a stripped-down version of adbd that only supports the
923 // 'sideload' command. Note this must be a real argument, not
924 // anything in the command file or bootloader control block; the
925 // only way recovery should be run with this argument is when it
926 // starts a copy of itself from the apply_from_adb() function.
Dees_Troy9a4b5692012-09-19 15:09:45 -0400927 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
928 adb_main(argv[2]);
Doug Zongker9270a202012-01-09 15:16:13 -0800929 return 0;
930 }
931
Ethan Yonkera1674162014-11-06 08:35:10 -0600932<<<<<<< HEAD
Dees_Troy51127312012-09-08 13:08:49 -0400933 printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800934
Dees_Troy32c8eb82012-09-11 15:28:06 -0400935 Device* device = make_device();
936 ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700937
Dees_Troy7d15c252012-09-05 20:47:21 -0400938 //ui->Init();
939 //ui->SetBackground(RecoveryUI::NONE);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400940 //load_volume_table();
Dees_Troy7d15c252012-09-05 20:47:21 -0400941
942 // Load default values to set DataManager constants and handle ifdefs
943 DataManager_LoadDefaults();
944 printf("Starting the UI...");
Dees_Troy51127312012-09-08 13:08:49 -0400945 gui_init();
Dees_Troy7d15c252012-09-05 20:47:21 -0400946 printf("=> Linking mtab\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500947 symlink("/proc/mounts", "/etc/mtab");
Dees_Troy7d15c252012-09-05 20:47:21 -0400948 printf("=> Processing recovery.fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400949 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400950 LOGE("Failing out of recovery due to problem with recovery.fstab.\n");
951 //return -1;
952 }
Dees_Troy8170a922012-09-18 15:40:25 -0400953 PartitionManager.Output_Partition_Logging();
Dees_Troy7d15c252012-09-05 20:47:21 -0400954 // Load up all the resources
955 gui_loadResources();
956
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000957 PartitionManager.Mount_By_Path("/cache", true);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800958
Doug Zongkerd4208f92010-09-20 12:16:13 -0700959 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700960 ensure_path_mounted(LAST_LOG_FILE);
Dees_Troy1669f892013-09-04 18:35:08 +0000961
Nick Kralevichb8344b62014-10-22 18:38:48 -0700962 rotate_last_logs(KEEP_LOG_COUNT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800963 get_args(&argc, &argv);
964
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800965 const char *send_intent = NULL;
966 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700967 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700968 bool just_exit = false;
Dees_Troy83a3b122012-10-01 14:16:43 -0400969 bool perform_backup = false;
Doug Zongkerb1d12632014-03-18 10:32:12 -0700970 bool shutdown_after = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800971
972 int arg;
973 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
974 switch (arg) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800975 case 's': send_intent = optarg; break;
976 case 'u': update_package = optarg; break;
977 case 'w': wipe_data = wipe_cache = 1; break;
978 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700979 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700980 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700981 case 'l': locale = optarg; break;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800982 case 'g': {
983 if (stage == NULL || *stage == '\0') {
984 char buffer[20] = "1/";
985 strncat(buffer, optarg, sizeof(buffer)-3);
986 stage = strdup(buffer);
987 }
988 break;
989 }
Doug Zongkerb1d12632014-03-18 10:32:12 -0700990 case 'p': shutdown_after = true; break;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -0700991 case 'r': reason = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800992 case '?':
993 LOGE("Invalid command argument\n");
994 continue;
995 }
996 }
997
Doug Zongker02ec6b82012-08-22 17:26:40 -0700998 if (locale == NULL) {
999 load_locale_from_cache();
1000 }
1001 printf("locale is [%s]\n", locale);
Doug Zongker0d32f252014-02-13 15:07:56 -08001002 printf("stage is [%s]\n", stage);
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -07001003 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001004
1005 Device* device = make_device();
1006 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -07001007 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -07001008
Doug Zongker5fa8c232012-09-18 12:37:02 -07001009 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001010 ui->Init();
Doug Zongkerc87bab12013-11-25 13:53:25 -08001011
1012 int st_cur, st_max;
1013 if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
1014 ui->SetStage(st_cur, st_max);
1015 }
1016
Doug Zongker02ec6b82012-08-22 17:26:40 -07001017 ui->SetBackground(RecoveryUI::NONE);
1018 if (show_text) ui->ShowText(true);
1019
Stephen Smalley779701d2012-02-09 14:13:23 -05001020 struct selinux_opt seopts[] = {
1021 { SELABEL_OPT_PATH, "/file_contexts" }
1022 };
1023
1024 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
1025
1026 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001027 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -05001028 }
Stephen Smalley779701d2012-02-09 14:13:23 -05001029
Dees_Troy7d15c252012-09-05 20:47:21 -04001030 //device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -08001031
Doug Zongker56c51052010-07-01 09:18:44 -07001032 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001033 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -07001034 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001035 }
Doug Zongker9b125b02010-09-22 12:01:37 -07001036 printf("\n");
1037
1038 if (update_package) {
1039 // For backwards compatibility on the cache partition only, if
1040 // we're given an old 'root' path "CACHE:foo", change it to
1041 // "/cache/foo".
1042 if (strncmp(update_package, "CACHE:", 6) == 0) {
1043 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -07001044 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -07001045 strlcpy(modified_path, "/cache/", len);
1046 strlcat(modified_path, update_package+6, len);
1047 printf("(replacing path \"%s\" with \"%s\")\n",
1048 update_package, modified_path);
1049 update_package = modified_path;
1050 }
1051 }
1052 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001053
1054 property_list(print_property, NULL);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001055 property_get("ro.build.display.id", recovery_version, "");
Doug Zongker56c51052010-07-01 09:18:44 -07001056 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001057
Dees_Troy7d15c252012-09-05 20:47:21 -04001058 // Check for and run startup script if script exists
Dees_Troya58bead2012-09-27 09:49:29 -04001059 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
1060 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
Dees_Troy7d15c252012-09-05 20:47:21 -04001061
1062#ifdef TW_INCLUDE_INJECTTWRP
1063 // Back up TWRP Ramdisk if needed:
Dees_Troy06b4fe92012-10-16 11:43:20 -04001064 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
Dees_Troy7d15c252012-09-05 20:47:21 -04001065 LOGI("Backing up TWRP ramdisk...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001066 if (Boot == NULL || Boot->Current_File_System != "emmc")
Vojtech Bocek05534202013-09-11 08:11:56 +02001067 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001068 else {
1069 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001070 TWFunc::Exec_Cmd(injectcmd);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001071 }
Dees_Troy7d15c252012-09-05 20:47:21 -04001072 LOGI("Backup of TWRP ramdisk done.\n");
1073#endif
1074
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001075 int status = INSTALL_SUCCESS;
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001076 string ORSCommand;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001077
Dees_Troy83a3b122012-10-01 14:16:43 -04001078 if (perform_backup) {
1079 char empt[50];
1080 gui_console_only();
1081 strcpy(empt, "(Current Date)");
1082 DataManager_SetStrValue(TW_BACKUP_NAME, empt);
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001083 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
Dees_Troy83a3b122012-10-01 14:16:43 -04001084 status = INSTALL_ERROR;
1085 }
1086 if (status == INSTALL_SUCCESS) { // Prevent other actions if backup failed
Doug Zongker540d57f2011-01-18 13:36:58 -08001087 if (update_package != NULL) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001088 ORSCommand = "install ";
1089 ORSCommand += update_package;
1090 ORSCommand += "\n";
1091
1092 if (OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
Dees_Troy83a3b122012-10-01 14:16:43 -04001093 status = INSTALL_SUCCESS;
1094 else
1095 status = INSTALL_ERROR;
1096 /*
Doug Zongker075ad802014-06-26 15:35:51 -07001097 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE, true);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001098 if (status == INSTALL_SUCCESS && wipe_cache) {
1099 if (erase_volume("/cache")) {
1100 LOGE("Cache wipe (requested by package) failed.");
1101 }
1102 }
Dees_Troy1669f892013-09-04 18:35:08 +00001103
Doug Zongker7c3ae452013-05-14 11:03:02 -07001104 if (status != INSTALL_SUCCESS) {
1105 ui->Print("Installation aborted.\n");
1106
1107 // If this is an eng or userdebug build, then automatically
1108 // turn the text display on if the script fails so the error
1109 // message is visible.
1110 char buffer[PROPERTY_VALUE_MAX+1];
1111 property_get("ro.build.fingerprint", buffer, "");
1112 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
1113 ui->ShowText(true);
1114 }
1115 }
Doug Zongkerb128f542009-06-18 15:07:14 -07001116 } else if (wipe_data) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001117 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
1118 status = INSTALL_ERROR;
Dees_Troy83a3b122012-10-01 14:16:43 -04001119 /*
1120 if (device->WipeData()) status = INSTALL_ERROR;
1121 if (erase_volume("/data")) status = INSTALL_ERROR;
1122 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Andres Moralesee193872014-08-05 19:49:09 -07001123 if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR;
Dees_Troy83a3b122012-10-01 14:16:43 -04001124 */
Doug Zongker211aebc2011-10-28 15:13:10 -07001125 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -07001126 } else if (wipe_cache) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001127 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
1128 status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001129 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001130 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001131 status = INSTALL_NONE; // No command specified
1132 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001133 }
Dees_Troy83a3b122012-10-01 14:16:43 -04001134 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001135
Dees_Troyd15e3f72013-02-21 14:28:29 -06001136 finish_recovery(NULL);
1137 // Offer to decrypt if the device is encrypted
1138 if (DataManager_GetIntValue(TW_IS_ENCRYPTED) != 0) {
1139 LOGI("Is encrypted, do decrypt page first\n");
1140 if (gui_startPage("decrypt") != 0) {
1141 LOGE("Failed to start decrypt GUI page.\n");
Dees_Troyc7c43412012-10-02 23:03:01 -04001142 }
Dees_Troyd15e3f72013-02-21 14:28:29 -06001143 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001144
Dees_Troyd15e3f72013-02-21 14:28:29 -06001145 // Read the settings file
1146 DataManager_ReadSettingsFile();
1147 // Run any outstanding OpenRecoveryScript
1148 if (DataManager_GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
1149 OpenRecoveryScript::Run_OpenRecoveryScript();
1150 }
1151 // Launch the main GUI
1152 gui_start();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001153
Dees_Troy6ef66352013-02-21 08:26:57 -06001154 // Check for su to see if the device is rooted or not
1155 if (PartitionManager.Mount_By_Path("/system", false)) {
Dees_Troyd15e3f72013-02-21 14:28:29 -06001156 // Disable flashing of stock recovery
dhacker2930f100e2014-07-15 21:33:29 -04001157 if (TWFunc::Path_Exists("/system/recovery-from-boot.p") && TWFunc::Path_Exists("/system/etc/install-recovery.sh")) {
Dees_Troyd15e3f72013-02-21 14:28:29 -06001158 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
1159 ui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
1160 }
jt1134113ee732013-02-22 23:26:10 -06001161 if (TWFunc::Path_Exists("/supersu/su") && !TWFunc::Path_Exists("/system/bin/su") && !TWFunc::Path_Exists("/system/xbin/su") && !TWFunc::Path_Exists("/system/bin/.ext/.su")) {
Dees_Troy6ef66352013-02-21 08:26:57 -06001162 // Device doesn't have su installed
1163 DataManager_SetIntValue("tw_busy", 1);
1164 if (gui_startPage("installsu") != 0) {
1165 LOGE("Failed to start decrypt GUI page.\n");
1166 }
1167 } else if (TWFunc::Check_su_Perms() > 0) {
1168 // su perms are set incorrectly
1169 DataManager_SetIntValue("tw_busy", 1);
1170 if (gui_startPage("fixsu") != 0) {
1171 LOGE("Failed to start decrypt GUI page.\n");
1172 }
1173 }
Dees_Troyd15e3f72013-02-21 14:28:29 -06001174 sync();
1175 PartitionManager.UnMount_By_Path("/system", false);
Dees_Troy6ef66352013-02-21 08:26:57 -06001176 }
1177
Doug Zongker02ec6b82012-08-22 17:26:40 -07001178 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001179 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001180 ui->SetBackground(RecoveryUI::ERROR);
1181 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001182 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001183 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001184 Device::BuiltinAction temp = prompt_and_wait(device, status);
1185 if (temp != Device::NO_ACTION) after = temp;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001186 }
1187
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001188 // Save logs and clean up before rebooting or shutting down.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001189 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -07001190 ui->Print("Rebooting...\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001191 char backup_arg_char[50];
1192 strcpy(backup_arg_char, DataManager_GetStrValue("tw_reboot_arg"));
1193 string backup_arg = backup_arg_char;
1194 if (backup_arg == "recovery")
1195 TWFunc::tw_reboot(rb_recovery);
1196 else if (backup_arg == "poweroff")
1197 TWFunc::tw_reboot(rb_poweroff);
1198 else if (backup_arg == "bootloader")
1199 TWFunc::tw_reboot(rb_bootloader);
1200 else if (backup_arg == "download")
1201 TWFunc::tw_reboot(rb_download);
1202 else
1203 TWFunc::tw_reboot(rb_system);
1204
Dees_Troy38bd7602012-09-14 13:33:53 -04001205#ifdef ANDROID_RB_RESTART
Ken Sumrall6e4472a2011-03-07 23:37:27 -08001206 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troy38bd7602012-09-14 13:33:53 -04001207#else
1208 reboot(RB_AUTOBOOT);
1209#endif
Doug Zongker3b5a9872013-09-03 14:29:54 -07001210 property_set(ANDROID_RB_PROPERTY, "reboot,");
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001211
1212 switch (after) {
1213 case Device::SHUTDOWN:
1214 ui->Print("Shutting down...\n");
1215 property_set(ANDROID_RB_PROPERTY, "shutdown,");
1216 break;
1217
1218 case Device::REBOOT_BOOTLOADER:
1219 ui->Print("Rebooting to bootloader...\n");
1220 property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");
1221 break;
1222
1223 default:
1224 ui->Print("Rebooting...\n");
1225 property_set(ANDROID_RB_PROPERTY, "reboot,");
1226 break;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001227 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001228 sleep(5); // should reboot before this finishes
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001229 return EXIT_SUCCESS;
1230}