blob: 9308eec04c66281224f85f506feb3846299f501b [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"
51}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052
Dees_Troy7d15c252012-09-05 20:47:21 -040053extern "C" {
Dees_Troy7d15c252012-09-05 20:47:21 -040054#include "data.h"
55#include "gui/gui.h"
56}
57#include "partitions.hpp"
Dees_Troy51127312012-09-08 13:08:49 -040058#include "variables.h"
Dees_Troy812660f2012-09-20 09:55:17 -040059#include "openrecoveryscript.hpp"
Dees_Troya58bead2012-09-27 09:49:29 -040060#include "twrp-functions.hpp"
Dees_Troy7d15c252012-09-05 20:47:21 -040061
Dees_Troy5bf43922012-09-07 16:07:55 -040062TWPartitionManager PartitionManager;
Dees_Troy7d15c252012-09-05 20:47:21 -040063
Stephen Smalley779701d2012-02-09 14:13:23 -050064struct selabel_handle *sehandle;
65
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050066
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067static const struct option OPTIONS[] = {
68 { "send_intent", required_argument, NULL, 's' },
69 { "update_package", required_argument, NULL, 'u' },
70 { "wipe_data", no_argument, NULL, 'w' },
71 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070072 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070073 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070074 { "locale", required_argument, NULL, 'l' },
Doug Zongker988500b2009-10-06 14:41:38 -070075 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076};
77
Doug Zongkerda1ebae2013-05-16 11:23:48 -070078#define LAST_LOG_FILE "/cache/recovery/last_log"
79
Doug Zongkerd4208f92010-09-20 12:16:13 -070080static const char *COMMAND_FILE = "/cache/recovery/command";
81static const char *INTENT_FILE = "/cache/recovery/intent";
82static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070083static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070084static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070085static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070086static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080087static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070088static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070089static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090
Doug Zongker211aebc2011-10-28 15:13:10 -070091RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070092char* locale = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070093
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094/*
95 * The recovery tool communicates with the main system through /cache files.
96 * /cache/recovery/command - INPUT - command line for tool, one arg per line
97 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
98 * /cache/recovery/intent - OUTPUT - intent that was passed in
99 *
100 * The arguments which may be supplied in the recovery.command file:
101 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -0700102 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 * --wipe_data - erase user data (and cache), then reboot
104 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -0800105 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700106 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800107 *
108 * After completing, we remove /cache/recovery/command and reboot.
109 * Arguments may also be supplied in the bootloader control block (BCB).
110 * These important scenarios must be safely restartable at any point:
111 *
112 * FACTORY RESET
113 * 1. user selects "factory reset"
114 * 2. main system writes "--wipe_data" to /cache/recovery/command
115 * 3. main system reboots into recovery
116 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
117 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700118 * 5. erase_volume() reformats /data
119 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800120 * 7. finish_recovery() erases BCB
121 * -- after this, rebooting will restart the main system --
122 * 8. main() calls reboot() to boot main system
123 *
124 * OTA INSTALL
125 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700126 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800127 * 3. main system reboots into recovery
128 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
129 * -- after this, rebooting will attempt to reinstall the update --
130 * 5. install_package() attempts to install the update
131 * NOTE: the package install must itself be restartable from any point
132 * 6. finish_recovery() erases BCB
133 * -- after this, rebooting will (try to) restart the main system --
134 * 7. ** if install failed **
135 * 7a. prompt_and_wait() shows an error icon and waits for the user
136 * 7b; the user reboots (pulling the battery, etc) into the main system
137 * 8. main() calls maybe_install_firmware_update()
138 * ** if the update contained radio/hboot firmware **:
139 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
140 * -- after this, rebooting will reformat cache & restart main system --
141 * 8b. m_i_f_u() writes firmware image into raw cache partition
142 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
143 * -- after this, rebooting will attempt to reinstall firmware --
144 * 8d. bootloader tries to flash firmware
145 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
146 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700147 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148 * 8g. finish_recovery() erases BCB
149 * -- after this, rebooting will (try to) restart the main system --
150 * 9. main() calls reboot() to boot main system
151 */
152
153static const int MAX_ARG_LENGTH = 4096;
154static const int MAX_ARGS = 100;
155
Doug Zongkerd4208f92010-09-20 12:16:13 -0700156// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700157FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700158fopen_path(const char *path, const char *mode) {
159 if (ensure_path_mounted(path) != 0) {
160 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 return NULL;
162 }
163
164 // When writing, try to create the containing directory, if necessary.
165 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500166 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167
168 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800169 return fp;
170}
171
172// close a file, log an error if the error indicator is set
173static void
174check_and_fclose(FILE *fp, const char *name) {
175 fflush(fp);
176 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
177 fclose(fp);
178}
179
180// command line args come from, in decreasing precedence:
181// - the actual command line
182// - the bootloader control block (one per line, after "recovery")
183// - the contents of COMMAND_FILE (one per line)
184static void
185get_args(int *argc, char ***argv) {
186 struct bootloader_message boot;
187 memset(&boot, 0, sizeof(boot));
188 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
189
190 if (boot.command[0] != 0 && boot.command[0] != 255) {
191 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
192 }
193
194 if (boot.status[0] != 0 && boot.status[0] != 255) {
195 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
196 }
197
198 // --- if arguments weren't supplied, look in the bootloader control block
199 if (*argc <= 1) {
200 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
201 const char *arg = strtok(boot.recovery, "\n");
202 if (arg != NULL && !strcmp(arg, "recovery")) {
203 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
204 (*argv)[0] = strdup(arg);
205 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
206 if ((arg = strtok(NULL, "\n")) == NULL) break;
207 (*argv)[*argc] = strdup(arg);
208 }
209 LOGI("Got arguments from boot message\n");
210 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
211 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
212 }
213 }
214
215 // --- if that doesn't work, try the command file
216 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700217 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800219 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220 char *argv0 = (*argv)[0];
221 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
222 (*argv)[0] = argv0; // use the same program name
223
224 char buf[MAX_ARG_LENGTH];
225 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
226 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800227 token = strtok(buf, "\r\n");
228 if (token != NULL) {
229 (*argv)[*argc] = strdup(token); // Strip newline.
230 } else {
231 --*argc;
232 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800233 }
234
235 check_and_fclose(fp, COMMAND_FILE);
236 LOGI("Got arguments from %s\n", COMMAND_FILE);
237 }
238 }
239
240 // --> write the arguments we have back into the bootloader control block
241 // always boot into recovery after this (until finish_recovery() is called)
242 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
243 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
244 int i;
245 for (i = 1; i < *argc; ++i) {
246 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
247 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
248 }
249 set_bootloader_message(&boot);
250}
251
Doug Zongker34c98df2009-08-18 12:05:45 -0700252static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800253set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700254 struct bootloader_message boot;
255 memset(&boot, 0, sizeof(boot));
256 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
257 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
258 set_bootloader_message(&boot);
259}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800260
Doug Zongker2c3539e2010-09-29 13:21:30 -0700261// How much of the temp log we have copied to the copy in cache.
Dees_Troy7d15c252012-09-05 20:47:21 -0400262//static long tmplog_offset = 0;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700263
264static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700265copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700266 FILE *log = fopen_path(destination, append ? "a" : "w");
267 if (log == NULL) {
268 LOGE("Can't open %s\n", destination);
269 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700270 FILE *tmplog = fopen(source, "r");
271 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700272 if (append) {
273 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
274 }
275 char buf[4096];
276 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
277 if (append) {
278 tmplog_offset = ftell(tmplog);
279 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700280 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700281 }
282 check_and_fclose(log, destination);
283 }
284}
285
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700286// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
287// Overwrites any existing last_log.$max.
288static void
289rotate_last_logs(int max) {
290 char oldfn[256];
291 char newfn[256];
292
293 int i;
294 for (i = max-1; i >= 0; --i) {
295 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
296 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
297 // ignore errors
298 rename(oldfn, newfn);
299 }
300}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700301
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302// clear the recovery command and prepare to boot a (hopefully working) system,
303// copy our log file to cache as well (for the system to read), and
304// record any intent we were asked to communicate back to the system.
305// this function is idempotent: call it as many times as you like.
306static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800307finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 // By this point, we're ready to return to the main system...
309 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700310 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000311 if (fp == NULL) {
312 LOGE("Can't open %s\n", INTENT_FILE);
313 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800314 fputs(send_intent, fp);
315 check_and_fclose(fp, INTENT_FILE);
316 }
317 }
318
Doug Zongker4f33e552012-08-23 13:16:12 -0700319 // Save the locale to cache, so if recovery is next started up
320 // without a --locale argument (eg, directly from the bootloader)
321 // it will use the last-known locale.
322 if (locale != NULL) {
323 LOGI("Saving locale \"%s\"\n", locale);
324 FILE* fp = fopen_path(LOCALE_FILE, "w");
325 fwrite(locale, 1, strlen(locale), fp);
326 fflush(fp);
327 fsync(fileno(fp));
328 check_and_fclose(fp, LOCALE_FILE);
329 }
330
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700332 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
333 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
334 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
335 chmod(LOG_FILE, 0600);
336 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700337 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700338 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800339
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700340 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800341 struct bootloader_message boot;
342 memset(&boot, 0, sizeof(boot));
343 set_bootloader_message(&boot);
344
345 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700346 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
347 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800348 LOGW("Can't unlink %s\n", COMMAND_FILE);
349 }
350
Doug Zongkerd0181b82011-10-19 10:51:12 -0700351 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352 sync(); // For good measure.
353}
354
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800355static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700356erase_volume(const char *volume) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700357 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700358 ui->SetProgressType(RecoveryUI::INDETERMINATE);
359 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700360
Doug Zongkerd0181b82011-10-19 10:51:12 -0700361 ensure_path_unmounted(volume);
362
Doug Zongker2c3539e2010-09-29 13:21:30 -0700363 if (strcmp(volume, "/cache") == 0) {
364 // Any part of the log we'd copied to cache is now gone.
365 // Reset the pointer so we copy from the beginning of the temp
366 // log.
367 tmplog_offset = 0;
368 }
369
Doug Zongkerd4208f92010-09-20 12:16:13 -0700370 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800371}
372
Doug Zongker23ceeea2010-07-08 17:27:55 -0700373static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700374copy_sideloaded_package(const char* original_path) {
375 if (ensure_path_mounted(original_path) != 0) {
376 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700377 return NULL;
378 }
379
Doug Zongkerd4208f92010-09-20 12:16:13 -0700380 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700381 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
382 return NULL;
383 }
384
Doug Zongkerd4208f92010-09-20 12:16:13 -0700385 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700386 if (errno != EEXIST) {
387 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
388 return NULL;
389 }
390 }
391
Doug Zongkerd4208f92010-09-20 12:16:13 -0700392 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
393 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700394 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700395 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
396 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700397 return NULL;
398 }
399 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700400 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700401 return NULL;
402 }
403 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700404 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700405 return NULL;
406 }
407 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700408 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700409 return NULL;
410 }
411
Doug Zongkerd4208f92010-09-20 12:16:13 -0700412 char copy_path[PATH_MAX];
413 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700414 strcat(copy_path, "/package.zip");
415
Doug Zongker28ce47c2011-10-28 10:33:05 -0700416 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700417 if (buffer == NULL) {
418 LOGE("Failed to allocate buffer\n");
419 return NULL;
420 }
421
422 size_t read;
423 FILE* fin = fopen(original_path, "rb");
424 if (fin == NULL) {
425 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
426 return NULL;
427 }
428 FILE* fout = fopen(copy_path, "wb");
429 if (fout == NULL) {
430 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
431 return NULL;
432 }
433
434 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
435 if (fwrite(buffer, 1, read, fout) != read) {
436 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
437 return NULL;
438 }
439 }
440
441 free(buffer);
442
443 if (fclose(fout) != 0) {
444 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
445 return NULL;
446 }
447
448 if (fclose(fin) != 0) {
449 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
450 return NULL;
451 }
452
453 // "adb push" is happy to overwrite read-only files when it's
454 // running as root, but we'll try anyway.
455 if (chmod(copy_path, 0400) != 0) {
456 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
457 return NULL;
458 }
459
Doug Zongkerd4208f92010-09-20 12:16:13 -0700460 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700461}
462
Doug Zongker28ce47c2011-10-28 10:33:05 -0700463static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700464prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700465 const char* title[] = { "Android system recovery <"
466 EXPAND(RECOVERY_API_VERSION) "e>",
467 "",
468 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800469
Doug Zongkerd6837852009-06-17 22:07:13 -0700470 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700471 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700472 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700473 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700474 for (p = title; *p; ++p, ++count);
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 Zongkerd6837852009-06-17 22:07:13 -0700479 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700480 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700481 *h = NULL;
482
Doug Zongkerf93d8162009-09-22 15:16:02 -0700483 return new_headers;
484}
485
486static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700487get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700488 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700489 // throw away keys pressed previously, so user doesn't
490 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700491 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700492
Doug Zongker211aebc2011-10-28 15:13:10 -0700493 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700494 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495 int chosen_item = -1;
496
Doug Zongkerf93d8162009-09-22 15:16:02 -0700497 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700498 int key = ui->WaitKey();
499 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800500
Doug Zongker5cae4452011-01-25 13:15:30 -0800501 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700502 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800503 continue;
504 } else {
505 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700506 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700507 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800508 }
509 }
510
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700511 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700512
513 if (action < 0) {
514 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700515 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700516 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700517 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700518 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700519 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700520 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700521 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700522 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700523 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700524 chosen_item = selected;
525 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700526 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700527 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800528 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700529 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700530 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800531 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700532 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800533
Doug Zongker211aebc2011-10-28 15:13:10 -0700534 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700535 return chosen_item;
536}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800537
Doug Zongker8674a722010-09-15 11:08:23 -0700538static int compare_string(const void* a, const void* b) {
539 return strcmp(*(const char**)a, *(const char**)b);
540}
541
542static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700543update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700544 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700545 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700546
Doug Zongker8674a722010-09-15 11:08:23 -0700547 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700548 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700549 "",
550 NULL };
551 DIR* d;
552 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700553 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700554 if (d == NULL) {
555 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700556 if (unmount_when_done != NULL) {
557 ensure_path_unmounted(unmount_when_done);
558 }
Doug Zongker8674a722010-09-15 11:08:23 -0700559 return 0;
560 }
561
Doug Zongker28ce47c2011-10-28 10:33:05 -0700562 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700563
564 int d_size = 0;
565 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700566 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700567 int z_size = 1;
568 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700569 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700570 zips[0] = strdup("../");
571
572 while ((de = readdir(d)) != NULL) {
573 int name_len = strlen(de->d_name);
574
575 if (de->d_type == DT_DIR) {
576 // skip "." and ".." entries
577 if (name_len == 1 && de->d_name[0] == '.') continue;
578 if (name_len == 2 && de->d_name[0] == '.' &&
579 de->d_name[1] == '.') continue;
580
581 if (d_size >= d_alloc) {
582 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700583 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700584 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700585 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700586 strcpy(dirs[d_size], de->d_name);
587 dirs[d_size][name_len] = '/';
588 dirs[d_size][name_len+1] = '\0';
589 ++d_size;
590 } else if (de->d_type == DT_REG &&
591 name_len >= 4 &&
592 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
593 if (z_size >= z_alloc) {
594 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700595 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700596 }
597 zips[z_size++] = strdup(de->d_name);
598 }
599 }
600 closedir(d);
601
602 qsort(dirs, d_size, sizeof(char*), compare_string);
603 qsort(zips, z_size, sizeof(char*), compare_string);
604
605 // append dirs to the zips list
606 if (d_size + z_size + 1 > z_alloc) {
607 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700608 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700609 }
610 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
611 free(dirs);
612 z_size += d_size;
613 zips[z_size] = NULL;
614
615 int result;
616 int chosen_item = 0;
617 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700618 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700619
620 char* item = zips[chosen_item];
621 int item_len = strlen(item);
622 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700623 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700624 result = -1;
625 break;
626 } else if (item[item_len-1] == '/') {
627 // recurse down into a subdirectory
628 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700629 strlcpy(new_path, path, PATH_MAX);
630 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700631 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700632 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700633 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700634 if (result >= 0) break;
635 } else {
636 // selected a zip file: attempt to install it, and return
637 // the status to the caller.
638 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700639 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700640 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700641 strlcat(new_path, item, PATH_MAX);
642
Doug Zongker211aebc2011-10-28 15:13:10 -0700643 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700644 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700645 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700646 if (unmount_when_done != NULL) {
647 ensure_path_unmounted(unmount_when_done);
648 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700649 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700650 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700651 free(copy);
652 } else {
653 result = INSTALL_ERROR;
654 }
Doug Zongker8674a722010-09-15 11:08:23 -0700655 break;
656 }
657 } while (true);
658
659 int i;
660 for (i = 0; i < z_size; ++i) free(zips[i]);
661 free(zips);
662 free(headers);
663
Michael Ward9d2629c2011-06-23 22:14:24 -0700664 if (unmount_when_done != NULL) {
665 ensure_path_unmounted(unmount_when_done);
666 }
Doug Zongker8674a722010-09-15 11:08:23 -0700667 return result;
668}
669
Doug Zongkerf93d8162009-09-22 15:16:02 -0700670static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700671wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700672 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700673 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700674
Doug Zongkerf93d8162009-09-22 15:16:02 -0700675 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700676 const char* headers[] = { "Confirm wipe of all user data?",
677 " THIS CAN NOT BE UNDONE.",
678 "",
679 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700680 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700681 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800682
Doug Zongker28ce47c2011-10-28 10:33:05 -0700683 const char* items[] = { " No",
684 " No",
685 " No",
686 " No",
687 " No",
688 " No",
689 " No",
690 " Yes -- delete all user data", // [7]
691 " No",
692 " No",
693 " No",
694 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800695
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700696 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700697 if (chosen_item != 7) {
698 return;
699 }
700 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700701
Doug Zongker211aebc2011-10-28 15:13:10 -0700702 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700703 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700704 erase_volume("/data");
705 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700706 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700707}
708
709static void
Doug Zongker6c8553d2012-09-24 10:40:47 -0700710prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700711 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700712
713 for (;;) {
714 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700715 switch (status) {
716 case INSTALL_SUCCESS:
717 case INSTALL_NONE:
718 ui->SetBackground(RecoveryUI::NO_COMMAND);
719 break;
720
721 case INSTALL_ERROR:
722 case INSTALL_CORRUPT:
723 ui->SetBackground(RecoveryUI::ERROR);
724 break;
725 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700726 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700727
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700728 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700729
730 // device-specific code may take some action here. It may
731 // return one of the core actions handled in the switch
732 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700733 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700734
Doug Zongkerd0181b82011-10-19 10:51:12 -0700735 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700736 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700737 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700738 return;
739
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700740 case Device::WIPE_DATA:
741 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700742 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700743 break;
744
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700745 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700746 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700747 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700748 ui->Print("Cache wipe complete.\n");
749 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700750 break;
751
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700752 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800753 // Some packages expect /cache to be mounted (eg,
754 // standard incremental packages expect to use /cache
755 // as scratch space).
756 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700757 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700758 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700759 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700760 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700761 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700762 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700763 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700764 }
765 }
Doug Zongker8674a722010-09-15 11:08:23 -0700766 if (status >= 0) {
767 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700768 ui->SetBackground(RecoveryUI::ERROR);
769 ui->Print("Installation aborted.\n");
770 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700771 return; // reboot if logs aren't visible
772 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700773 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700774 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700775 }
776 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700777
778 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700779 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700780 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700781 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700782 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700783 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700784 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700785 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700786 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700787 }
788 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700789 if (status >= 0) {
790 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700791 ui->SetBackground(RecoveryUI::ERROR);
792 ui->Print("Installation aborted.\n");
793 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700794 return; // reboot if logs aren't visible
795 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700796 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700797 }
798 }
799 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800800
801 case Device::APPLY_ADB_SIDELOAD:
802 ensure_path_mounted(CACHE_ROOT);
803 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
804 if (status >= 0) {
805 if (status != INSTALL_SUCCESS) {
806 ui->SetBackground(RecoveryUI::ERROR);
807 ui->Print("Installation aborted.\n");
808 } else if (!ui->IsTextVisible()) {
809 return; // reboot if logs aren't visible
810 } else {
811 ui->Print("\nInstall from ADB complete.\n");
812 }
813 }
814 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800815 }
816 }
817}
818
819static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800820print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700821 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800822}
823
Doug Zongker02ec6b82012-08-22 17:26:40 -0700824static void
825load_locale_from_cache() {
826 FILE* fp = fopen_path(LOCALE_FILE, "r");
827 char buffer[80];
828 if (fp != NULL) {
829 fgets(buffer, sizeof(buffer), fp);
830 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700831 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700832 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
833 if (!isspace(buffer[i])) {
834 buffer[j++] = buffer[i];
835 }
836 }
837 buffer[j] = 0;
838 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700839 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700840 }
841}
842
Doug Zongker7c3ae452013-05-14 11:03:02 -0700843static RecoveryUI* gCurrentUI = NULL;
844
845void
846ui_print(const char* format, ...) {
847 char buffer[256];
848
849 va_list ap;
850 va_start(ap, format);
851 vsnprintf(buffer, sizeof(buffer), format, ap);
852 va_end(ap);
853
854 if (gCurrentUI != NULL) {
855 gCurrentUI->Print("%s", buffer);
856 } else {
857 fputs(buffer, stdout);
858 }
859}
860
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800861int
Oscar Montemayor05231562009-11-30 08:40:57 -0800862main(int argc, char **argv) {
Dees_Troycfb63ae2012-09-19 14:30:17 -0400863 // Recovery needs to install world-readable files, so clear umask
864 // set by init
865 umask(0);
866
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800867 time_t start = time(NULL);
868
869 // If these fail, there's not really anywhere to complain...
870 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
871 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800872
873 // If this binary is started with the single argument "--adbd",
874 // instead of being the normal recovery binary, it turns into kind
875 // of a stripped-down version of adbd that only supports the
876 // 'sideload' command. Note this must be a real argument, not
877 // anything in the command file or bootloader control block; the
878 // only way recovery should be run with this argument is when it
879 // starts a copy of itself from the apply_from_adb() function.
Dees_Troy9a4b5692012-09-19 15:09:45 -0400880 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
881 adb_main(argv[2]);
Doug Zongker9270a202012-01-09 15:16:13 -0800882 return 0;
883 }
884
Dees_Troy51127312012-09-08 13:08:49 -0400885 printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800886
Dees_Troy32c8eb82012-09-11 15:28:06 -0400887 Device* device = make_device();
888 ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700889
Dees_Troy7d15c252012-09-05 20:47:21 -0400890 //ui->Init();
891 //ui->SetBackground(RecoveryUI::NONE);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400892 //load_volume_table();
Dees_Troy7d15c252012-09-05 20:47:21 -0400893
894 // Load default values to set DataManager constants and handle ifdefs
895 DataManager_LoadDefaults();
896 printf("Starting the UI...");
Dees_Troy51127312012-09-08 13:08:49 -0400897 gui_init();
Dees_Troy7d15c252012-09-05 20:47:21 -0400898 printf("=> Linking mtab\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500899 symlink("/proc/mounts", "/etc/mtab");
Dees_Troy7d15c252012-09-05 20:47:21 -0400900 printf("=> Processing recovery.fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400901 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400902 LOGE("Failing out of recovery due to problem with recovery.fstab.\n");
903 //return -1;
904 }
Dees_Troy8170a922012-09-18 15:40:25 -0400905 PartitionManager.Output_Partition_Logging();
Dees_Troy7d15c252012-09-05 20:47:21 -0400906 // Load up all the resources
907 gui_loadResources();
908
Dees_Troy01b6d0c2013-01-22 01:41:09 +0000909 PartitionManager.Mount_By_Path("/cache", true);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800910
Doug Zongkerd4208f92010-09-20 12:16:13 -0700911 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700912 ensure_path_mounted(LAST_LOG_FILE);
913 rotate_last_logs(5);
Dees_Troy1669f892013-09-04 18:35:08 +0000914
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800915 get_args(&argc, &argv);
916
917 int previous_runs = 0;
918 const char *send_intent = NULL;
919 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700920 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700921 bool just_exit = false;
Dees_Troy83a3b122012-10-01 14:16:43 -0400922 bool perform_backup = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800923
924 int arg;
925 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
926 switch (arg) {
927 case 'p': previous_runs = atoi(optarg); break;
928 case 's': send_intent = optarg; break;
929 case 'u': update_package = optarg; break;
930 case 'w': wipe_data = wipe_cache = 1; break;
931 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700932 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700933 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700934 case 'l': locale = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800935 case '?':
936 LOGE("Invalid command argument\n");
937 continue;
938 }
939 }
940
Doug Zongker02ec6b82012-08-22 17:26:40 -0700941 if (locale == NULL) {
942 load_locale_from_cache();
943 }
944 printf("locale is [%s]\n", locale);
945
946 Device* device = make_device();
947 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -0700948 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700949
950 ui->Init();
Doug Zongker5fa8c232012-09-18 12:37:02 -0700951 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700952 ui->SetBackground(RecoveryUI::NONE);
953 if (show_text) ui->ShowText(true);
954
Stephen Smalley779701d2012-02-09 14:13:23 -0500955 struct selinux_opt seopts[] = {
956 { SELABEL_OPT_PATH, "/file_contexts" }
957 };
958
959 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
960
961 if (!sehandle) {
962 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700963 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500964 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500965
Dees_Troy7d15c252012-09-05 20:47:21 -0400966 //device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800967
Doug Zongker56c51052010-07-01 09:18:44 -0700968 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800969 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700970 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800971 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700972 printf("\n");
973
974 if (update_package) {
975 // For backwards compatibility on the cache partition only, if
976 // we're given an old 'root' path "CACHE:foo", change it to
977 // "/cache/foo".
978 if (strncmp(update_package, "CACHE:", 6) == 0) {
979 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700980 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700981 strlcpy(modified_path, "/cache/", len);
982 strlcat(modified_path, update_package+6, len);
983 printf("(replacing path \"%s\" with \"%s\")\n",
984 update_package, modified_path);
985 update_package = modified_path;
986 }
987 }
988 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800989
990 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700991 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800992
Dees_Troy7d15c252012-09-05 20:47:21 -0400993 // Check for and run startup script if script exists
Dees_Troya58bead2012-09-27 09:49:29 -0400994 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
995 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
Dees_Troy7d15c252012-09-05 20:47:21 -0400996
997#ifdef TW_INCLUDE_INJECTTWRP
998 // Back up TWRP Ramdisk if needed:
Dees_Troy06b4fe92012-10-16 11:43:20 -0400999 TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001000 string result;
Dees_Troy7d15c252012-09-05 20:47:21 -04001001 LOGI("Backing up TWRP ramdisk...\n");
Dees_Troy06b4fe92012-10-16 11:43:20 -04001002 if (Boot == NULL || Boot->Current_File_System != "emmc")
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001003 TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img", result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001004 else {
1005 string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001006 TWFunc::Exec_Cmd(injectcmd, result);
Dees_Troy06b4fe92012-10-16 11:43:20 -04001007 }
Dees_Troy7d15c252012-09-05 20:47:21 -04001008 LOGI("Backup of TWRP ramdisk done.\n");
1009#endif
1010
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001011 int status = INSTALL_SUCCESS;
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001012 string ORSCommand;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001013
Dees_Troy83a3b122012-10-01 14:16:43 -04001014 if (perform_backup) {
1015 char empt[50];
1016 gui_console_only();
1017 strcpy(empt, "(Current Date)");
1018 DataManager_SetStrValue(TW_BACKUP_NAME, empt);
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001019 if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n"))
Dees_Troy83a3b122012-10-01 14:16:43 -04001020 status = INSTALL_ERROR;
1021 }
1022 if (status == INSTALL_SUCCESS) { // Prevent other actions if backup failed
Doug Zongker540d57f2011-01-18 13:36:58 -08001023 if (update_package != NULL) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001024 ORSCommand = "install ";
1025 ORSCommand += update_package;
1026 ORSCommand += "\n";
1027
1028 if (OpenRecoveryScript::Insert_ORS_Command(ORSCommand))
Dees_Troy83a3b122012-10-01 14:16:43 -04001029 status = INSTALL_SUCCESS;
1030 else
1031 status = INSTALL_ERROR;
1032 /*
Doug Zongkerd0181b82011-10-19 10:51:12 -07001033 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
1034 if (status == INSTALL_SUCCESS && wipe_cache) {
1035 if (erase_volume("/cache")) {
1036 LOGE("Cache wipe (requested by package) failed.");
1037 }
1038 }
Dees_Troy1669f892013-09-04 18:35:08 +00001039
Doug Zongker7c3ae452013-05-14 11:03:02 -07001040 if (status != INSTALL_SUCCESS) {
1041 ui->Print("Installation aborted.\n");
1042
1043 // If this is an eng or userdebug build, then automatically
1044 // turn the text display on if the script fails so the error
1045 // message is visible.
1046 char buffer[PROPERTY_VALUE_MAX+1];
1047 property_get("ro.build.fingerprint", buffer, "");
1048 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
1049 ui->ShowText(true);
1050 }
1051 }
Doug Zongkerb128f542009-06-18 15:07:14 -07001052 } else if (wipe_data) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001053 if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n"))
1054 status = INSTALL_ERROR;
Dees_Troy83a3b122012-10-01 14:16:43 -04001055 /*
1056 if (device->WipeData()) status = INSTALL_ERROR;
1057 if (erase_volume("/data")) status = INSTALL_ERROR;
1058 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
1059 */
Doug Zongker211aebc2011-10-28 15:13:10 -07001060 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -07001061 } else if (wipe_cache) {
Dees_Troy01b6d0c2013-01-22 01:41:09 +00001062 if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n"))
1063 status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001064 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001065 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001066 status = INSTALL_NONE; // No command specified
1067 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001068 }
Dees_Troy83a3b122012-10-01 14:16:43 -04001069 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001070
Dees_Troyd15e3f72013-02-21 14:28:29 -06001071 finish_recovery(NULL);
1072 // Offer to decrypt if the device is encrypted
1073 if (DataManager_GetIntValue(TW_IS_ENCRYPTED) != 0) {
1074 LOGI("Is encrypted, do decrypt page first\n");
1075 if (gui_startPage("decrypt") != 0) {
1076 LOGE("Failed to start decrypt GUI page.\n");
Dees_Troyc7c43412012-10-02 23:03:01 -04001077 }
Dees_Troyd15e3f72013-02-21 14:28:29 -06001078 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001079
Dees_Troyd15e3f72013-02-21 14:28:29 -06001080 // Read the settings file
1081 DataManager_ReadSettingsFile();
1082 // Run any outstanding OpenRecoveryScript
1083 if (DataManager_GetIntValue(TW_IS_ENCRYPTED) == 0 && (TWFunc::Path_Exists(SCRIPT_FILE_TMP) || TWFunc::Path_Exists(SCRIPT_FILE_CACHE))) {
1084 OpenRecoveryScript::Run_OpenRecoveryScript();
1085 }
1086 // Launch the main GUI
1087 gui_start();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001088
Dees_Troy6ef66352013-02-21 08:26:57 -06001089 // Check for su to see if the device is rooted or not
1090 if (PartitionManager.Mount_By_Path("/system", false)) {
Dees_Troyd15e3f72013-02-21 14:28:29 -06001091 // Disable flashing of stock recovery
1092 if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
1093 rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
1094 ui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
1095 }
jt1134113ee732013-02-22 23:26:10 -06001096 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 -06001097 // Device doesn't have su installed
1098 DataManager_SetIntValue("tw_busy", 1);
1099 if (gui_startPage("installsu") != 0) {
1100 LOGE("Failed to start decrypt GUI page.\n");
1101 }
1102 } else if (TWFunc::Check_su_Perms() > 0) {
1103 // su perms are set incorrectly
1104 DataManager_SetIntValue("tw_busy", 1);
1105 if (gui_startPage("fixsu") != 0) {
1106 LOGE("Failed to start decrypt GUI page.\n");
1107 }
1108 }
Dees_Troyd15e3f72013-02-21 14:28:29 -06001109 sync();
1110 PartitionManager.UnMount_By_Path("/system", false);
Dees_Troy6ef66352013-02-21 08:26:57 -06001111 }
1112
Doug Zongker02ec6b82012-08-22 17:26:40 -07001113 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
1114 ui->SetBackground(RecoveryUI::ERROR);
1115 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001116 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker6c8553d2012-09-24 10:40:47 -07001117 prompt_and_wait(device, status);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001118 }
1119
1120 // Otherwise, get ready to boot the main system...
1121 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -07001122 ui->Print("Rebooting...\n");
Dees_Troy6ef66352013-02-21 08:26:57 -06001123 char backup_arg_char[50];
1124 strcpy(backup_arg_char, DataManager_GetStrValue("tw_reboot_arg"));
1125 string backup_arg = backup_arg_char;
1126 if (backup_arg == "recovery")
1127 TWFunc::tw_reboot(rb_recovery);
1128 else if (backup_arg == "poweroff")
1129 TWFunc::tw_reboot(rb_poweroff);
1130 else if (backup_arg == "bootloader")
1131 TWFunc::tw_reboot(rb_bootloader);
1132 else if (backup_arg == "download")
1133 TWFunc::tw_reboot(rb_download);
1134 else
1135 TWFunc::tw_reboot(rb_system);
1136
Dees_Troy38bd7602012-09-14 13:33:53 -04001137#ifdef ANDROID_RB_RESTART
Ken Sumrall6e4472a2011-03-07 23:37:27 -08001138 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troy38bd7602012-09-14 13:33:53 -04001139#else
1140 reboot(RB_AUTOBOOT);
1141#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001142 return EXIT_SUCCESS;
1143}