blob: 4f07fd44cdac84d716a41d57c032b032a362d8ff [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>
18#include <errno.h>
19#include <fcntl.h>
20#include <getopt.h>
21#include <limits.h>
22#include <linux/input.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070026#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
Doug Zongker8674a722010-09-15 11:08:23 -070030#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
32#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "common.h"
34#include "cutils/properties.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040035#ifdef ANDROID_RB_RESTART
Ken Sumrall6e4472a2011-03-07 23:37:27 -080036#include "cutils/android_reboot.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040037#else
38#include <sys/reboot.h>
39#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "install.h"
41#include "minui/minui.h"
42#include "minzip/DirUtil.h"
43#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070044#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070045#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070046#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080047#include "adb_install.h"
48extern "C" {
49#include "minadbd/adb.h"
50}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Dees_Troy7d15c252012-09-05 20:47:21 -040052extern "C" {
Dees_Troy7d15c252012-09-05 20:47:21 -040053#include "data.h"
54#include "gui/gui.h"
55}
56#include "partitions.hpp"
Dees_Troy51127312012-09-08 13:08:49 -040057#include "variables.h"
Dees_Troy812660f2012-09-20 09:55:17 -040058#include "openrecoveryscript.hpp"
Dees_Troya58bead2012-09-27 09:49:29 -040059#include "twrp-functions.hpp"
Dees_Troy7d15c252012-09-05 20:47:21 -040060
Dees_Troy5bf43922012-09-07 16:07:55 -040061TWPartitionManager PartitionManager;
Dees_Troy7d15c252012-09-05 20:47:21 -040062
Stephen Smalley779701d2012-02-09 14:13:23 -050063struct selabel_handle *sehandle;
64
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050065
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066static const struct option OPTIONS[] = {
67 { "send_intent", required_argument, NULL, 's' },
68 { "update_package", required_argument, NULL, 'u' },
69 { "wipe_data", no_argument, NULL, 'w' },
70 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070071 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070072 { "just_exit", no_argument, NULL, 'x' },
Dees_Troy83a3b122012-10-01 14:16:43 -040073 { "nandroid", no_argument, NULL, 'n' },
Doug Zongker988500b2009-10-06 14:41:38 -070074 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075};
76
Doug Zongkerd4208f92010-09-20 12:16:13 -070077static const char *COMMAND_FILE = "/cache/recovery/command";
78static const char *INTENT_FILE = "/cache/recovery/intent";
79static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070080static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070081static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070082static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070083static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070085static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070086static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080087
Doug Zongker211aebc2011-10-28 15:13:10 -070088RecoveryUI* ui = NULL;
89
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090/*
91 * The recovery tool communicates with the main system through /cache files.
92 * /cache/recovery/command - INPUT - command line for tool, one arg per line
93 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
94 * /cache/recovery/intent - OUTPUT - intent that was passed in
95 *
96 * The arguments which may be supplied in the recovery.command file:
97 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070098 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 * --wipe_data - erase user data (and cache), then reboot
100 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -0800101 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700102 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 *
104 * After completing, we remove /cache/recovery/command and reboot.
105 * Arguments may also be supplied in the bootloader control block (BCB).
106 * These important scenarios must be safely restartable at any point:
107 *
108 * FACTORY RESET
109 * 1. user selects "factory reset"
110 * 2. main system writes "--wipe_data" to /cache/recovery/command
111 * 3. main system reboots into recovery
112 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
113 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700114 * 5. erase_volume() reformats /data
115 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116 * 7. finish_recovery() erases BCB
117 * -- after this, rebooting will restart the main system --
118 * 8. main() calls reboot() to boot main system
119 *
120 * OTA INSTALL
121 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700122 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123 * 3. main system reboots into recovery
124 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
125 * -- after this, rebooting will attempt to reinstall the update --
126 * 5. install_package() attempts to install the update
127 * NOTE: the package install must itself be restartable from any point
128 * 6. finish_recovery() erases BCB
129 * -- after this, rebooting will (try to) restart the main system --
130 * 7. ** if install failed **
131 * 7a. prompt_and_wait() shows an error icon and waits for the user
132 * 7b; the user reboots (pulling the battery, etc) into the main system
133 * 8. main() calls maybe_install_firmware_update()
134 * ** if the update contained radio/hboot firmware **:
135 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
136 * -- after this, rebooting will reformat cache & restart main system --
137 * 8b. m_i_f_u() writes firmware image into raw cache partition
138 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
139 * -- after this, rebooting will attempt to reinstall firmware --
140 * 8d. bootloader tries to flash firmware
141 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
142 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700143 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144 * 8g. finish_recovery() erases BCB
145 * -- after this, rebooting will (try to) restart the main system --
146 * 9. main() calls reboot() to boot main system
147 */
148
149static const int MAX_ARG_LENGTH = 4096;
150static const int MAX_ARGS = 100;
151
Doug Zongkerd4208f92010-09-20 12:16:13 -0700152// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700153FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700154fopen_path(const char *path, const char *mode) {
155 if (ensure_path_mounted(path) != 0) {
156 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157 return NULL;
158 }
159
160 // When writing, try to create the containing directory, if necessary.
161 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500162 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163
164 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165 return fp;
166}
167
168// close a file, log an error if the error indicator is set
169static void
170check_and_fclose(FILE *fp, const char *name) {
171 fflush(fp);
172 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
173 fclose(fp);
174}
175
176// command line args come from, in decreasing precedence:
177// - the actual command line
178// - the bootloader control block (one per line, after "recovery")
179// - the contents of COMMAND_FILE (one per line)
180static void
181get_args(int *argc, char ***argv) {
182 struct bootloader_message boot;
183 memset(&boot, 0, sizeof(boot));
184 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
185
186 if (boot.command[0] != 0 && boot.command[0] != 255) {
187 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
188 }
189
190 if (boot.status[0] != 0 && boot.status[0] != 255) {
191 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
192 }
193
194 // --- if arguments weren't supplied, look in the bootloader control block
195 if (*argc <= 1) {
196 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
197 const char *arg = strtok(boot.recovery, "\n");
198 if (arg != NULL && !strcmp(arg, "recovery")) {
199 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
200 (*argv)[0] = strdup(arg);
201 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
202 if ((arg = strtok(NULL, "\n")) == NULL) break;
203 (*argv)[*argc] = strdup(arg);
204 }
205 LOGI("Got arguments from boot message\n");
206 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
207 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
208 }
209 }
210
211 // --- if that doesn't work, try the command file
212 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700213 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214 if (fp != NULL) {
215 char *argv0 = (*argv)[0];
216 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
217 (*argv)[0] = argv0; // use the same program name
218
219 char buf[MAX_ARG_LENGTH];
220 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
221 if (!fgets(buf, sizeof(buf), fp)) break;
222 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
223 }
224
225 check_and_fclose(fp, COMMAND_FILE);
226 LOGI("Got arguments from %s\n", COMMAND_FILE);
227 }
228 }
229
230 // --> write the arguments we have back into the bootloader control block
231 // always boot into recovery after this (until finish_recovery() is called)
232 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
233 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
234 int i;
235 for (i = 1; i < *argc; ++i) {
236 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
237 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
238 }
239 set_bootloader_message(&boot);
240}
241
Doug Zongker34c98df2009-08-18 12:05:45 -0700242static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800243set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700244 struct bootloader_message boot;
245 memset(&boot, 0, sizeof(boot));
246 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
247 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
248 set_bootloader_message(&boot);
249}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250
Doug Zongker2c3539e2010-09-29 13:21:30 -0700251// How much of the temp log we have copied to the copy in cache.
Dees_Troy7d15c252012-09-05 20:47:21 -0400252//static long tmplog_offset = 0;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700253
254static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700255copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700256 FILE *log = fopen_path(destination, append ? "a" : "w");
257 if (log == NULL) {
258 LOGE("Can't open %s\n", destination);
259 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700260 FILE *tmplog = fopen(source, "r");
261 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700262 if (append) {
263 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
264 }
265 char buf[4096];
266 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
267 if (append) {
268 tmplog_offset = ftell(tmplog);
269 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700270 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700271 }
272 check_and_fclose(log, destination);
273 }
274}
275
276
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277// clear the recovery command and prepare to boot a (hopefully working) system,
278// copy our log file to cache as well (for the system to read), and
279// record any intent we were asked to communicate back to the system.
280// this function is idempotent: call it as many times as you like.
281static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800282finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283 // By this point, we're ready to return to the main system...
284 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700285 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000286 if (fp == NULL) {
287 LOGE("Can't open %s\n", INTENT_FILE);
288 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289 fputs(send_intent, fp);
290 check_and_fclose(fp, INTENT_FILE);
291 }
292 }
293
294 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700295 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
296 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
297 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
298 chmod(LOG_FILE, 0600);
299 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700300 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700301 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700303 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800304 struct bootloader_message boot;
305 memset(&boot, 0, sizeof(boot));
306 set_bootloader_message(&boot);
307
308 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700309 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
310 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800311 LOGW("Can't unlink %s\n", COMMAND_FILE);
312 }
313
Doug Zongkerd0181b82011-10-19 10:51:12 -0700314 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800315 sync(); // For good measure.
316}
317
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800318static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700319erase_volume(const char *volume) {
Dees_Troy4b20e772013-01-17 18:50:23 +0000320 return !PartitionManager.Wipe_By_Path(volume);
Doug Zongker211aebc2011-10-28 15:13:10 -0700321 ui->SetBackground(RecoveryUI::INSTALLING);
322 ui->SetProgressType(RecoveryUI::INDETERMINATE);
323 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700324
Doug Zongkerd0181b82011-10-19 10:51:12 -0700325 ensure_path_unmounted(volume);
326
Doug Zongker2c3539e2010-09-29 13:21:30 -0700327 if (strcmp(volume, "/cache") == 0) {
328 // Any part of the log we'd copied to cache is now gone.
329 // Reset the pointer so we copy from the beginning of the temp
330 // log.
331 tmplog_offset = 0;
332 }
333
Doug Zongkerd4208f92010-09-20 12:16:13 -0700334 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335}
336
Doug Zongker23ceeea2010-07-08 17:27:55 -0700337static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700338copy_sideloaded_package(const char* original_path) {
339 if (ensure_path_mounted(original_path) != 0) {
340 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700341 return NULL;
342 }
343
Doug Zongkerd4208f92010-09-20 12:16:13 -0700344 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700345 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
346 return NULL;
347 }
348
Doug Zongkerd4208f92010-09-20 12:16:13 -0700349 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700350 if (errno != EEXIST) {
351 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
352 return NULL;
353 }
354 }
355
Doug Zongkerd4208f92010-09-20 12:16:13 -0700356 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
357 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700358 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700359 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
360 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700361 return NULL;
362 }
363 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700364 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700365 return NULL;
366 }
367 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700368 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700369 return NULL;
370 }
371 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700372 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700373 return NULL;
374 }
375
Doug Zongkerd4208f92010-09-20 12:16:13 -0700376 char copy_path[PATH_MAX];
377 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700378 strcat(copy_path, "/package.zip");
379
Doug Zongker28ce47c2011-10-28 10:33:05 -0700380 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700381 if (buffer == NULL) {
382 LOGE("Failed to allocate buffer\n");
383 return NULL;
384 }
385
386 size_t read;
387 FILE* fin = fopen(original_path, "rb");
388 if (fin == NULL) {
389 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
390 return NULL;
391 }
392 FILE* fout = fopen(copy_path, "wb");
393 if (fout == NULL) {
394 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
395 return NULL;
396 }
397
398 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
399 if (fwrite(buffer, 1, read, fout) != read) {
400 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
401 return NULL;
402 }
403 }
404
405 free(buffer);
406
407 if (fclose(fout) != 0) {
408 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
409 return NULL;
410 }
411
412 if (fclose(fin) != 0) {
413 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
414 return NULL;
415 }
416
417 // "adb push" is happy to overwrite read-only files when it's
418 // running as root, but we'll try anyway.
419 if (chmod(copy_path, 0400) != 0) {
420 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
421 return NULL;
422 }
423
Doug Zongkerd4208f92010-09-20 12:16:13 -0700424 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700425}
426
Doug Zongker28ce47c2011-10-28 10:33:05 -0700427static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700428prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700429 const char* title[] = { "Android system recovery <"
430 EXPAND(RECOVERY_API_VERSION) "e>",
431 "",
432 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800433
Doug Zongkerd6837852009-06-17 22:07:13 -0700434 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700435 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700436 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700437 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700438 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700439 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700440
Doug Zongker28ce47c2011-10-28 10:33:05 -0700441 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
442 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700443 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700444 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700445 *h = NULL;
446
Doug Zongkerf93d8162009-09-22 15:16:02 -0700447 return new_headers;
448}
449
450static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700451get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700452 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700453 // throw away keys pressed previously, so user doesn't
454 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700455 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700456
Doug Zongker211aebc2011-10-28 15:13:10 -0700457 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700458 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800459 int chosen_item = -1;
460
Doug Zongkerf93d8162009-09-22 15:16:02 -0700461 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700462 int key = ui->WaitKey();
463 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464
Doug Zongker5cae4452011-01-25 13:15:30 -0800465 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700466 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800467 continue;
468 } else {
469 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700470 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700471 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800472 }
473 }
474
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700475 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700476
477 if (action < 0) {
478 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700479 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700480 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700481 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700482 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700483 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700484 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700485 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700486 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700487 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700488 chosen_item = selected;
489 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700490 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700491 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800492 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700493 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700494 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700496 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800497
Doug Zongker211aebc2011-10-28 15:13:10 -0700498 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700499 return chosen_item;
500}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800501
Doug Zongker8674a722010-09-15 11:08:23 -0700502static int compare_string(const void* a, const void* b) {
503 return strcmp(*(const char**)a, *(const char**)b);
504}
505
506static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700507update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700508 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700509 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700510
Doug Zongker8674a722010-09-15 11:08:23 -0700511 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700512 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700513 "",
514 NULL };
515 DIR* d;
516 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700517 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700518 if (d == NULL) {
519 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700520 if (unmount_when_done != NULL) {
521 ensure_path_unmounted(unmount_when_done);
522 }
Doug Zongker8674a722010-09-15 11:08:23 -0700523 return 0;
524 }
525
Doug Zongker28ce47c2011-10-28 10:33:05 -0700526 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700527
528 int d_size = 0;
529 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700530 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700531 int z_size = 1;
532 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700533 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700534 zips[0] = strdup("../");
535
536 while ((de = readdir(d)) != NULL) {
537 int name_len = strlen(de->d_name);
538
539 if (de->d_type == DT_DIR) {
540 // skip "." and ".." entries
541 if (name_len == 1 && de->d_name[0] == '.') continue;
542 if (name_len == 2 && de->d_name[0] == '.' &&
543 de->d_name[1] == '.') continue;
544
545 if (d_size >= d_alloc) {
546 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700547 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700548 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700549 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700550 strcpy(dirs[d_size], de->d_name);
551 dirs[d_size][name_len] = '/';
552 dirs[d_size][name_len+1] = '\0';
553 ++d_size;
554 } else if (de->d_type == DT_REG &&
555 name_len >= 4 &&
556 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
557 if (z_size >= z_alloc) {
558 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700559 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700560 }
561 zips[z_size++] = strdup(de->d_name);
562 }
563 }
564 closedir(d);
565
566 qsort(dirs, d_size, sizeof(char*), compare_string);
567 qsort(zips, z_size, sizeof(char*), compare_string);
568
569 // append dirs to the zips list
570 if (d_size + z_size + 1 > z_alloc) {
571 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700572 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700573 }
574 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
575 free(dirs);
576 z_size += d_size;
577 zips[z_size] = NULL;
578
579 int result;
580 int chosen_item = 0;
581 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700582 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700583
584 char* item = zips[chosen_item];
585 int item_len = strlen(item);
586 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700587 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700588 result = -1;
589 break;
590 } else if (item[item_len-1] == '/') {
591 // recurse down into a subdirectory
592 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700593 strlcpy(new_path, path, PATH_MAX);
594 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700595 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700596 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700597 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700598 if (result >= 0) break;
599 } else {
600 // selected a zip file: attempt to install it, and return
601 // the status to the caller.
602 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700603 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700604 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700605 strlcat(new_path, item, PATH_MAX);
606
Doug Zongker211aebc2011-10-28 15:13:10 -0700607 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700608 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700609 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700610 if (unmount_when_done != NULL) {
611 ensure_path_unmounted(unmount_when_done);
612 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700613 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700614 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700615 free(copy);
616 } else {
617 result = INSTALL_ERROR;
618 }
Doug Zongker8674a722010-09-15 11:08:23 -0700619 break;
620 }
621 } while (true);
622
623 int i;
624 for (i = 0; i < z_size; ++i) free(zips[i]);
625 free(zips);
626 free(headers);
627
Michael Ward9d2629c2011-06-23 22:14:24 -0700628 if (unmount_when_done != NULL) {
629 ensure_path_unmounted(unmount_when_done);
630 }
Doug Zongker8674a722010-09-15 11:08:23 -0700631 return result;
632}
633
Doug Zongkerf93d8162009-09-22 15:16:02 -0700634static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700635wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700636 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700637 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700638
Doug Zongkerf93d8162009-09-22 15:16:02 -0700639 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700640 const char* headers[] = { "Confirm wipe of all user data?",
641 " THIS CAN NOT BE UNDONE.",
642 "",
643 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700644 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700645 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800646
Doug Zongker28ce47c2011-10-28 10:33:05 -0700647 const char* items[] = { " No",
648 " No",
649 " No",
650 " No",
651 " No",
652 " No",
653 " No",
654 " Yes -- delete all user data", // [7]
655 " No",
656 " No",
657 " No",
658 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800659
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700660 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700661 if (chosen_item != 7) {
662 return;
663 }
664 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700665
Doug Zongker211aebc2011-10-28 15:13:10 -0700666 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700667 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700668 erase_volume("/data");
669 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700670 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700671}
672
673static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700674prompt_and_wait(Device* device) {
675 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700676
677 for (;;) {
678 finish_recovery(NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700679 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700680
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700681 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700682
683 // device-specific code may take some action here. It may
684 // return one of the core actions handled in the switch
685 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700686 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700687
Michael Ward9d2629c2011-06-23 22:14:24 -0700688 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700689 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700690 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700691 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700692 return;
693
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700694 case Device::WIPE_DATA:
695 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700696 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700697 break;
698
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700699 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700700 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700701 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700702 ui->Print("Cache wipe complete.\n");
703 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700704 break;
705
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700706 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800707 // Some packages expect /cache to be mounted (eg,
708 // standard incremental packages expect to use /cache
709 // as scratch space).
710 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700711 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700712 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700713 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700714 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700715 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700716 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700717 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700718 }
719 }
Doug Zongker8674a722010-09-15 11:08:23 -0700720 if (status >= 0) {
721 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700722 ui->SetBackground(RecoveryUI::ERROR);
723 ui->Print("Installation aborted.\n");
724 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700725 return; // reboot if logs aren't visible
726 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700727 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700728 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700729 }
730 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700731
732 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700733 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700734 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700735 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700736 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700737 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700738 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700739 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700740 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700741 }
742 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700743 if (status >= 0) {
744 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700745 ui->SetBackground(RecoveryUI::ERROR);
746 ui->Print("Installation aborted.\n");
747 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700748 return; // reboot if logs aren't visible
749 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700750 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700751 }
752 }
753 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800754
755 case Device::APPLY_ADB_SIDELOAD:
756 ensure_path_mounted(CACHE_ROOT);
757 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
758 if (status >= 0) {
759 if (status != INSTALL_SUCCESS) {
760 ui->SetBackground(RecoveryUI::ERROR);
761 ui->Print("Installation aborted.\n");
762 } else if (!ui->IsTextVisible()) {
763 return; // reboot if logs aren't visible
764 } else {
765 ui->Print("\nInstall from ADB complete.\n");
766 }
767 }
768 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800769 }
770 }
771}
772
773static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800774print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700775 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800776}
777
778int
Oscar Montemayor05231562009-11-30 08:40:57 -0800779main(int argc, char **argv) {
Dees_Troycfb63ae2012-09-19 14:30:17 -0400780 // Recovery needs to install world-readable files, so clear umask
781 // set by init
782 umask(0);
783
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800784 time_t start = time(NULL);
785
786 // If these fail, there's not really anywhere to complain...
787 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
788 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800789
790 // If this binary is started with the single argument "--adbd",
791 // instead of being the normal recovery binary, it turns into kind
792 // of a stripped-down version of adbd that only supports the
793 // 'sideload' command. Note this must be a real argument, not
794 // anything in the command file or bootloader control block; the
795 // only way recovery should be run with this argument is when it
796 // starts a copy of itself from the apply_from_adb() function.
Dees_Troy9a4b5692012-09-19 15:09:45 -0400797 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
798 adb_main(argv[2]);
Doug Zongker9270a202012-01-09 15:16:13 -0800799 return 0;
800 }
801
Dees_Troy51127312012-09-08 13:08:49 -0400802 printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800803
Dees_Troy32c8eb82012-09-11 15:28:06 -0400804 Device* device = make_device();
805 ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700806
Dees_Troy7d15c252012-09-05 20:47:21 -0400807 //ui->Init();
808 //ui->SetBackground(RecoveryUI::NONE);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400809 //load_volume_table();
Dees_Troy7d15c252012-09-05 20:47:21 -0400810
811 // Load default values to set DataManager constants and handle ifdefs
812 DataManager_LoadDefaults();
813 printf("Starting the UI...");
Dees_Troy51127312012-09-08 13:08:49 -0400814 gui_init();
Dees_Troy7d15c252012-09-05 20:47:21 -0400815 printf("=> Linking mtab\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500816 symlink("/proc/mounts", "/etc/mtab");
Dees_Troy7d15c252012-09-05 20:47:21 -0400817 printf("=> Processing recovery.fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400818 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400819 LOGE("Failing out of recovery due to problem with recovery.fstab.\n");
820 //return -1;
821 }
Dees_Troy8170a922012-09-18 15:40:25 -0400822 PartitionManager.Output_Partition_Logging();
Dees_Troy7d15c252012-09-05 20:47:21 -0400823 // Load up all the resources
824 gui_loadResources();
825
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000826 PartitionManager.Mount_By_Path("/cache", true);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800827 get_args(&argc, &argv);
828
829 int previous_runs = 0;
830 const char *send_intent = NULL;
831 const char *update_package = NULL;
832 int wipe_data = 0, wipe_cache = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700833 bool just_exit = false;
Dees_Troy83a3b122012-10-01 14:16:43 -0400834 bool perform_backup = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800835
836 int arg;
837 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
838 switch (arg) {
839 case 'p': previous_runs = atoi(optarg); break;
840 case 's': send_intent = optarg; break;
841 case 'u': update_package = optarg; break;
842 case 'w': wipe_data = wipe_cache = 1; break;
843 case 'c': wipe_cache = 1; break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700844 case 't': ui->ShowText(true); break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700845 case 'x': just_exit = true; break;
Dees_Troy83a3b122012-10-01 14:16:43 -0400846 case 'n': perform_backup = true; LOGI("nandroid\n"); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800847 case '?':
848 LOGE("Invalid command argument\n");
849 continue;
850 }
851 }
852
Stephen Smalley779701d2012-02-09 14:13:23 -0500853#ifdef HAVE_SELINUX
854 struct selinux_opt seopts[] = {
855 { SELABEL_OPT_PATH, "/file_contexts" }
856 };
857
858 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
859
860 if (!sehandle) {
861 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700862 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500863 }
864#endif
865
Dees_Troy7d15c252012-09-05 20:47:21 -0400866 //device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800867
Doug Zongker56c51052010-07-01 09:18:44 -0700868 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800869 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700870 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800871 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700872 printf("\n");
873
874 if (update_package) {
875 // For backwards compatibility on the cache partition only, if
876 // we're given an old 'root' path "CACHE:foo", change it to
877 // "/cache/foo".
878 if (strncmp(update_package, "CACHE:", 6) == 0) {
879 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700880 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700881 strlcpy(modified_path, "/cache/", len);
882 strlcat(modified_path, update_package+6, len);
883 printf("(replacing path \"%s\" with \"%s\")\n",
884 update_package, modified_path);
885 update_package = modified_path;
886 }
887 }
888 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800889
890 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700891 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800892
Dees_Troy7d15c252012-09-05 20:47:21 -0400893 // Check for and run startup script if script exists
Dees_Troya58bead2012-09-27 09:49:29 -0400894 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
895 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
Dees_Troy7d15c252012-09-05 20:47:21 -0400896
897#ifdef TW_INCLUDE_INJECTTWRP
898 // Back up TWRP Ramdisk if needed:
Dees_Troy06b4fe92012-10-16 11:43:20 -0400899 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500900 string result;
Dees_Troy7d15c252012-09-05 20:47:21 -0400901 LOGI("Backing up TWRP ramdisk...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -0400902 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500903 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400904 else {
905 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500906 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -0400907 }
Dees_Troy7d15c252012-09-05 20:47:21 -0400908 LOGI("Backup of TWRP ramdisk done.\n");
909#endif
910
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800911 int status = INSTALL_SUCCESS;
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000912 string ORSCommand;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800913
Dees_Troy83a3b122012-10-01 14:16:43 -0400914 if (perform_backup) {
915 char empt[50];
916 gui_console_only();
917 strcpy(empt, "(Current Date)");
918 DataManager_SetStrValue(TW_BACKUP_NAME, empt);
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000919 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
Dees_Troy83a3b122012-10-01 14:16:43 -0400920 status = INSTALL_ERROR;
921 }
922 if (status == INSTALL_SUCCESS) { // Prevent other actions if backup failed
Doug Zongker540d57f2011-01-18 13:36:58 -0800923 if (update_package != NULL) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000924 ORSCommand = "install ";
925 ORSCommand += update_package;
926 ORSCommand += "\n";
927
928 if (OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
Dees_Troy83a3b122012-10-01 14:16:43 -0400929 status = INSTALL_SUCCESS;
930 else
931 status = INSTALL_ERROR;
932 /*
Doug Zongkerd0181b82011-10-19 10:51:12 -0700933 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
934 if (status == INSTALL_SUCCESS && wipe_cache) {
935 if (erase_volume("/cache")) {
936 LOGE("Cache wipe (requested by package) failed.");
937 }
938 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700939 if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
Dees_Troy83a3b122012-10-01 14:16:43 -0400940 */
Doug Zongkerb128f542009-06-18 15:07:14 -0700941 } else if (wipe_data) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000942 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
943 status = INSTALL_ERROR;
Dees_Troy83a3b122012-10-01 14:16:43 -0400944 /*
945 if (device->WipeData()) status = INSTALL_ERROR;
946 if (erase_volume("/data")) status = INSTALL_ERROR;
947 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
948 */
Doug Zongker211aebc2011-10-28 15:13:10 -0700949 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700950 } else if (wipe_cache) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000951 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
952 status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700953 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700954 } else if (!just_exit) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800955 status = INSTALL_ERROR; // No command specified
956 }
Dees_Troy83a3b122012-10-01 14:16:43 -0400957 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800958
Dees_Troyd15e3f72013-02-21 14:28:29 -0600959 finish_recovery(NULL);
960 // Offer to decrypt if the device is encrypted
961 if (DataManager_GetIntValue(TW_IS_ENCRYPTED) != 0) {
962 LOGI("Is encrypted, do decrypt page first\n");
963 if (gui_startPage("decrypt") != 0) {
964 LOGE("Failed to start decrypt GUI page.\n");
Dees_Troyc7c43412012-10-02 23:03:01 -0400965 }
Dees_Troyd15e3f72013-02-21 14:28:29 -0600966 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500967
Dees_Troyd15e3f72013-02-21 14:28:29 -0600968 // Read the settings file
969 DataManager_ReadSettingsFile();
970 // Run any outstanding OpenRecoveryScript
971 if (DataManager_GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
972 OpenRecoveryScript::Run_OpenRecoveryScript();
973 }
974 // Launch the main GUI
975 gui_start();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800976
Dees_Troy6ef66352013-02-21 08:26:57 -0600977 // Check for su to see if the device is rooted or not
978 if (PartitionManager.Mount_By_Path("/system", false)) {
Dees_Troyd15e3f72013-02-21 14:28:29 -0600979 // Disable flashing of stock recovery
980 if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
981 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
982 ui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
983 }
jt1134113ee732013-02-22 23:26:10 -0600984 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 -0600985 // Device doesn't have su installed
986 DataManager_SetIntValue("tw_busy", 1);
987 if (gui_startPage("installsu") != 0) {
988 LOGE("Failed to start decrypt GUI page.\n");
989 }
990 } else if (TWFunc::Check_su_Perms() > 0) {
991 // su perms are set incorrectly
992 DataManager_SetIntValue("tw_busy", 1);
993 if (gui_startPage("fixsu") != 0) {
994 LOGE("Failed to start decrypt GUI page.\n");
995 }
996 }
Dees_Troyd15e3f72013-02-21 14:28:29 -0600997 sync();
998 PartitionManager.UnMount_By_Path("/system", false);
Dees_Troy6ef66352013-02-21 08:26:57 -0600999 }
1000
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001001 // Otherwise, get ready to boot the main system...
1002 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -07001003 ui->Print("Rebooting...\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001004 char backup_arg_char[50];
1005 strcpy(backup_arg_char, DataManager_GetStrValue("tw_reboot_arg"));
1006 string backup_arg = backup_arg_char;
1007 if (backup_arg == "recovery")
1008 TWFunc::tw_reboot(rb_recovery);
1009 else if (backup_arg == "poweroff")
1010 TWFunc::tw_reboot(rb_poweroff);
1011 else if (backup_arg == "bootloader")
1012 TWFunc::tw_reboot(rb_bootloader);
1013 else if (backup_arg == "download")
1014 TWFunc::tw_reboot(rb_download);
1015 else
1016 TWFunc::tw_reboot(rb_system);
1017
Dees_Troy38bd7602012-09-14 13:33:53 -04001018#ifdef ANDROID_RB_RESTART
Ken Sumrall6e4472a2011-03-07 23:37:27 -08001019 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troy38bd7602012-09-14 13:33:53 -04001020#else
1021 reboot(RB_AUTOBOOT);
1022#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001023 return EXIT_SUCCESS;
1024}