blob: 2c77271b4bf178ffab0f78736fcda575a29b8b02 [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"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080035#include "cutils/android_reboot.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#include "install.h"
37#include "minui/minui.h"
38#include "minzip/DirUtil.h"
39#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070040#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070041#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070042#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080043#include "adb_install.h"
44extern "C" {
45#include "minadbd/adb.h"
46}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Dees_Troy7d15c252012-09-05 20:47:21 -040048extern "C" {
49#include "extra-functions.h"
50#include "data.h"
51#include "gui/gui.h"
52}
53#include "partitions.hpp"
54
55int gui_init(void);
56int gui_loadResources(void);
57int gui_start(void);
58int gui_console_only(void);
59
Stephen Smalley779701d2012-02-09 14:13:23 -050060struct selabel_handle *sehandle;
61
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062static const struct option OPTIONS[] = {
63 { "send_intent", required_argument, NULL, 's' },
64 { "update_package", required_argument, NULL, 'u' },
65 { "wipe_data", no_argument, NULL, 'w' },
66 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070067 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070068 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker988500b2009-10-06 14:41:38 -070069 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070};
71
Doug Zongkerd4208f92010-09-20 12:16:13 -070072static const char *COMMAND_FILE = "/cache/recovery/command";
73static const char *INTENT_FILE = "/cache/recovery/intent";
74static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070075static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070076static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070077static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070078static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070080static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070081static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082
Doug Zongker211aebc2011-10-28 15:13:10 -070083RecoveryUI* ui = NULL;
84
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085/*
86 * The recovery tool communicates with the main system through /cache files.
87 * /cache/recovery/command - INPUT - command line for tool, one arg per line
88 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
89 * /cache/recovery/intent - OUTPUT - intent that was passed in
90 *
91 * The arguments which may be supplied in the recovery.command file:
92 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070093 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094 * --wipe_data - erase user data (and cache), then reboot
95 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080096 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070097 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098 *
99 * After completing, we remove /cache/recovery/command and reboot.
100 * Arguments may also be supplied in the bootloader control block (BCB).
101 * These important scenarios must be safely restartable at any point:
102 *
103 * FACTORY RESET
104 * 1. user selects "factory reset"
105 * 2. main system writes "--wipe_data" to /cache/recovery/command
106 * 3. main system reboots into recovery
107 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
108 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700109 * 5. erase_volume() reformats /data
110 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800111 * 7. finish_recovery() erases BCB
112 * -- after this, rebooting will restart the main system --
113 * 8. main() calls reboot() to boot main system
114 *
115 * OTA INSTALL
116 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700117 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 * 3. main system reboots into recovery
119 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
120 * -- after this, rebooting will attempt to reinstall the update --
121 * 5. install_package() attempts to install the update
122 * NOTE: the package install must itself be restartable from any point
123 * 6. finish_recovery() erases BCB
124 * -- after this, rebooting will (try to) restart the main system --
125 * 7. ** if install failed **
126 * 7a. prompt_and_wait() shows an error icon and waits for the user
127 * 7b; the user reboots (pulling the battery, etc) into the main system
128 * 8. main() calls maybe_install_firmware_update()
129 * ** if the update contained radio/hboot firmware **:
130 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
131 * -- after this, rebooting will reformat cache & restart main system --
132 * 8b. m_i_f_u() writes firmware image into raw cache partition
133 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
134 * -- after this, rebooting will attempt to reinstall firmware --
135 * 8d. bootloader tries to flash firmware
136 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
137 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700138 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 * 8g. finish_recovery() erases BCB
140 * -- after this, rebooting will (try to) restart the main system --
141 * 9. main() calls reboot() to boot main system
142 */
143
144static const int MAX_ARG_LENGTH = 4096;
145static const int MAX_ARGS = 100;
146
Doug Zongkerd4208f92010-09-20 12:16:13 -0700147// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700148FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700149fopen_path(const char *path, const char *mode) {
150 if (ensure_path_mounted(path) != 0) {
151 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 return NULL;
153 }
154
155 // When writing, try to create the containing directory, if necessary.
156 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500157 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158
159 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160 return fp;
161}
162
163// close a file, log an error if the error indicator is set
164static void
165check_and_fclose(FILE *fp, const char *name) {
166 fflush(fp);
167 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
168 fclose(fp);
169}
170
171// command line args come from, in decreasing precedence:
172// - the actual command line
173// - the bootloader control block (one per line, after "recovery")
174// - the contents of COMMAND_FILE (one per line)
175static void
176get_args(int *argc, char ***argv) {
177 struct bootloader_message boot;
178 memset(&boot, 0, sizeof(boot));
179 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
180
181 if (boot.command[0] != 0 && boot.command[0] != 255) {
182 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
183 }
184
185 if (boot.status[0] != 0 && boot.status[0] != 255) {
186 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
187 }
188
189 // --- if arguments weren't supplied, look in the bootloader control block
190 if (*argc <= 1) {
191 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
192 const char *arg = strtok(boot.recovery, "\n");
193 if (arg != NULL && !strcmp(arg, "recovery")) {
194 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
195 (*argv)[0] = strdup(arg);
196 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
197 if ((arg = strtok(NULL, "\n")) == NULL) break;
198 (*argv)[*argc] = strdup(arg);
199 }
200 LOGI("Got arguments from boot message\n");
201 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
202 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
203 }
204 }
205
206 // --- if that doesn't work, try the command file
207 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700208 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 if (fp != NULL) {
210 char *argv0 = (*argv)[0];
211 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
212 (*argv)[0] = argv0; // use the same program name
213
214 char buf[MAX_ARG_LENGTH];
215 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
216 if (!fgets(buf, sizeof(buf), fp)) break;
217 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
218 }
219
220 check_and_fclose(fp, COMMAND_FILE);
221 LOGI("Got arguments from %s\n", COMMAND_FILE);
222 }
223 }
224
225 // --> write the arguments we have back into the bootloader control block
226 // always boot into recovery after this (until finish_recovery() is called)
227 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
228 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
229 int i;
230 for (i = 1; i < *argc; ++i) {
231 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
232 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
233 }
234 set_bootloader_message(&boot);
235}
236
Doug Zongker34c98df2009-08-18 12:05:45 -0700237static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800238set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700239 struct bootloader_message boot;
240 memset(&boot, 0, sizeof(boot));
241 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
242 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
243 set_bootloader_message(&boot);
244}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245
Doug Zongker2c3539e2010-09-29 13:21:30 -0700246// How much of the temp log we have copied to the copy in cache.
Dees_Troy7d15c252012-09-05 20:47:21 -0400247//static long tmplog_offset = 0;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700248
249static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700250copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700251 FILE *log = fopen_path(destination, append ? "a" : "w");
252 if (log == NULL) {
253 LOGE("Can't open %s\n", destination);
254 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700255 FILE *tmplog = fopen(source, "r");
256 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700257 if (append) {
258 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
259 }
260 char buf[4096];
261 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
262 if (append) {
263 tmplog_offset = ftell(tmplog);
264 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700265 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700266 }
267 check_and_fclose(log, destination);
268 }
269}
270
271
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800272// clear the recovery command and prepare to boot a (hopefully working) system,
273// copy our log file to cache as well (for the system to read), and
274// record any intent we were asked to communicate back to the system.
275// this function is idempotent: call it as many times as you like.
276static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800277finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 // By this point, we're ready to return to the main system...
279 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700280 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000281 if (fp == NULL) {
282 LOGE("Can't open %s\n", INTENT_FILE);
283 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800284 fputs(send_intent, fp);
285 check_and_fclose(fp, INTENT_FILE);
286 }
287 }
288
289 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700290 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
291 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
292 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
293 chmod(LOG_FILE, 0600);
294 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700295 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700296 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700298 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299 struct bootloader_message boot;
300 memset(&boot, 0, sizeof(boot));
301 set_bootloader_message(&boot);
302
303 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700304 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
305 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 LOGW("Can't unlink %s\n", COMMAND_FILE);
307 }
308
Doug Zongkerd0181b82011-10-19 10:51:12 -0700309 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310 sync(); // For good measure.
311}
312
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700314erase_volume(const char *volume) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700315 ui->SetBackground(RecoveryUI::INSTALLING);
316 ui->SetProgressType(RecoveryUI::INDETERMINATE);
317 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700318
Doug Zongkerd0181b82011-10-19 10:51:12 -0700319 ensure_path_unmounted(volume);
320
Doug Zongker2c3539e2010-09-29 13:21:30 -0700321 if (strcmp(volume, "/cache") == 0) {
322 // Any part of the log we'd copied to cache is now gone.
323 // Reset the pointer so we copy from the beginning of the temp
324 // log.
325 tmplog_offset = 0;
326 }
327
Doug Zongkerd4208f92010-09-20 12:16:13 -0700328 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800329}
330
Doug Zongker23ceeea2010-07-08 17:27:55 -0700331static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700332copy_sideloaded_package(const char* original_path) {
333 if (ensure_path_mounted(original_path) != 0) {
334 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700335 return NULL;
336 }
337
Doug Zongkerd4208f92010-09-20 12:16:13 -0700338 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700339 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
340 return NULL;
341 }
342
Doug Zongkerd4208f92010-09-20 12:16:13 -0700343 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700344 if (errno != EEXIST) {
345 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
346 return NULL;
347 }
348 }
349
Doug Zongkerd4208f92010-09-20 12:16:13 -0700350 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
351 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700352 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700353 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
354 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700355 return NULL;
356 }
357 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700358 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700359 return NULL;
360 }
361 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700362 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700363 return NULL;
364 }
365 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700366 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700367 return NULL;
368 }
369
Doug Zongkerd4208f92010-09-20 12:16:13 -0700370 char copy_path[PATH_MAX];
371 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700372 strcat(copy_path, "/package.zip");
373
Doug Zongker28ce47c2011-10-28 10:33:05 -0700374 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700375 if (buffer == NULL) {
376 LOGE("Failed to allocate buffer\n");
377 return NULL;
378 }
379
380 size_t read;
381 FILE* fin = fopen(original_path, "rb");
382 if (fin == NULL) {
383 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
384 return NULL;
385 }
386 FILE* fout = fopen(copy_path, "wb");
387 if (fout == NULL) {
388 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
389 return NULL;
390 }
391
392 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
393 if (fwrite(buffer, 1, read, fout) != read) {
394 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
395 return NULL;
396 }
397 }
398
399 free(buffer);
400
401 if (fclose(fout) != 0) {
402 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
403 return NULL;
404 }
405
406 if (fclose(fin) != 0) {
407 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
408 return NULL;
409 }
410
411 // "adb push" is happy to overwrite read-only files when it's
412 // running as root, but we'll try anyway.
413 if (chmod(copy_path, 0400) != 0) {
414 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
415 return NULL;
416 }
417
Doug Zongkerd4208f92010-09-20 12:16:13 -0700418 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700419}
420
Doug Zongker28ce47c2011-10-28 10:33:05 -0700421static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700422prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700423 const char* title[] = { "Android system recovery <"
424 EXPAND(RECOVERY_API_VERSION) "e>",
425 "",
426 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800427
Doug Zongkerd6837852009-06-17 22:07:13 -0700428 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700429 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700430 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700431 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700432 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700433 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700434
Doug Zongker28ce47c2011-10-28 10:33:05 -0700435 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
436 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700437 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700438 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700439 *h = NULL;
440
Doug Zongkerf93d8162009-09-22 15:16:02 -0700441 return new_headers;
442}
443
444static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700445get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700446 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700447 // throw away keys pressed previously, so user doesn't
448 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700449 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700450
Doug Zongker211aebc2011-10-28 15:13:10 -0700451 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700452 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800453 int chosen_item = -1;
454
Doug Zongkerf93d8162009-09-22 15:16:02 -0700455 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700456 int key = ui->WaitKey();
457 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800458
Doug Zongker5cae4452011-01-25 13:15:30 -0800459 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700460 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800461 continue;
462 } else {
463 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700464 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700465 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800466 }
467 }
468
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700469 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700470
471 if (action < 0) {
472 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700473 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700474 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700475 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700476 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700477 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700478 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700479 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700480 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700481 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700482 chosen_item = selected;
483 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700484 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700485 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800486 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700487 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700488 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800489 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700490 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800491
Doug Zongker211aebc2011-10-28 15:13:10 -0700492 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700493 return chosen_item;
494}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495
Doug Zongker8674a722010-09-15 11:08:23 -0700496static int compare_string(const void* a, const void* b) {
497 return strcmp(*(const char**)a, *(const char**)b);
498}
499
500static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700501update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700502 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700503 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700504
Doug Zongker8674a722010-09-15 11:08:23 -0700505 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700506 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700507 "",
508 NULL };
509 DIR* d;
510 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700511 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700512 if (d == NULL) {
513 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700514 if (unmount_when_done != NULL) {
515 ensure_path_unmounted(unmount_when_done);
516 }
Doug Zongker8674a722010-09-15 11:08:23 -0700517 return 0;
518 }
519
Doug Zongker28ce47c2011-10-28 10:33:05 -0700520 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700521
522 int d_size = 0;
523 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700524 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700525 int z_size = 1;
526 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700527 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700528 zips[0] = strdup("../");
529
530 while ((de = readdir(d)) != NULL) {
531 int name_len = strlen(de->d_name);
532
533 if (de->d_type == DT_DIR) {
534 // skip "." and ".." entries
535 if (name_len == 1 && de->d_name[0] == '.') continue;
536 if (name_len == 2 && de->d_name[0] == '.' &&
537 de->d_name[1] == '.') continue;
538
539 if (d_size >= d_alloc) {
540 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700541 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700542 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700543 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700544 strcpy(dirs[d_size], de->d_name);
545 dirs[d_size][name_len] = '/';
546 dirs[d_size][name_len+1] = '\0';
547 ++d_size;
548 } else if (de->d_type == DT_REG &&
549 name_len >= 4 &&
550 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
551 if (z_size >= z_alloc) {
552 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700553 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700554 }
555 zips[z_size++] = strdup(de->d_name);
556 }
557 }
558 closedir(d);
559
560 qsort(dirs, d_size, sizeof(char*), compare_string);
561 qsort(zips, z_size, sizeof(char*), compare_string);
562
563 // append dirs to the zips list
564 if (d_size + z_size + 1 > z_alloc) {
565 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700566 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700567 }
568 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
569 free(dirs);
570 z_size += d_size;
571 zips[z_size] = NULL;
572
573 int result;
574 int chosen_item = 0;
575 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700576 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700577
578 char* item = zips[chosen_item];
579 int item_len = strlen(item);
580 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700581 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700582 result = -1;
583 break;
584 } else if (item[item_len-1] == '/') {
585 // recurse down into a subdirectory
586 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700587 strlcpy(new_path, path, PATH_MAX);
588 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700589 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700590 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700591 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700592 if (result >= 0) break;
593 } else {
594 // selected a zip file: attempt to install it, and return
595 // the status to the caller.
596 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700597 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700598 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700599 strlcat(new_path, item, PATH_MAX);
600
Doug Zongker211aebc2011-10-28 15:13:10 -0700601 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700602 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700603 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700604 if (unmount_when_done != NULL) {
605 ensure_path_unmounted(unmount_when_done);
606 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700607 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700608 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700609 free(copy);
610 } else {
611 result = INSTALL_ERROR;
612 }
Doug Zongker8674a722010-09-15 11:08:23 -0700613 break;
614 }
615 } while (true);
616
617 int i;
618 for (i = 0; i < z_size; ++i) free(zips[i]);
619 free(zips);
620 free(headers);
621
Michael Ward9d2629c2011-06-23 22:14:24 -0700622 if (unmount_when_done != NULL) {
623 ensure_path_unmounted(unmount_when_done);
624 }
Doug Zongker8674a722010-09-15 11:08:23 -0700625 return result;
626}
627
Doug Zongkerf93d8162009-09-22 15:16:02 -0700628static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700629wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700630 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700631 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700632
Doug Zongkerf93d8162009-09-22 15:16:02 -0700633 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700634 const char* headers[] = { "Confirm wipe of all user data?",
635 " THIS CAN NOT BE UNDONE.",
636 "",
637 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700638 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700639 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800640
Doug Zongker28ce47c2011-10-28 10:33:05 -0700641 const char* items[] = { " No",
642 " No",
643 " No",
644 " No",
645 " No",
646 " No",
647 " No",
648 " Yes -- delete all user data", // [7]
649 " No",
650 " No",
651 " No",
652 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800653
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700654 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700655 if (chosen_item != 7) {
656 return;
657 }
658 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700659
Doug Zongker211aebc2011-10-28 15:13:10 -0700660 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700661 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700662 erase_volume("/data");
663 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700664 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700665}
666
667static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700668prompt_and_wait(Device* device) {
669 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700670
671 for (;;) {
672 finish_recovery(NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700673 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700674
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700675 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700676
677 // device-specific code may take some action here. It may
678 // return one of the core actions handled in the switch
679 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700680 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700681
Michael Ward9d2629c2011-06-23 22:14:24 -0700682 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700683 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700684 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700685 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700686 return;
687
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700688 case Device::WIPE_DATA:
689 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700690 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700691 break;
692
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700693 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700694 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700695 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700696 ui->Print("Cache wipe complete.\n");
697 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700698 break;
699
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700700 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800701 // Some packages expect /cache to be mounted (eg,
702 // standard incremental packages expect to use /cache
703 // as scratch space).
704 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700705 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700706 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700707 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700708 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700709 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700710 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700711 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700712 }
713 }
Doug Zongker8674a722010-09-15 11:08:23 -0700714 if (status >= 0) {
715 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700716 ui->SetBackground(RecoveryUI::ERROR);
717 ui->Print("Installation aborted.\n");
718 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700719 return; // reboot if logs aren't visible
720 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700721 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700722 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700723 }
724 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700725
726 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700727 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700728 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700729 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700730 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700731 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700732 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700733 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700734 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700735 }
736 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700737 if (status >= 0) {
738 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700739 ui->SetBackground(RecoveryUI::ERROR);
740 ui->Print("Installation aborted.\n");
741 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700742 return; // reboot if logs aren't visible
743 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700744 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700745 }
746 }
747 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800748
749 case Device::APPLY_ADB_SIDELOAD:
750 ensure_path_mounted(CACHE_ROOT);
751 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
752 if (status >= 0) {
753 if (status != INSTALL_SUCCESS) {
754 ui->SetBackground(RecoveryUI::ERROR);
755 ui->Print("Installation aborted.\n");
756 } else if (!ui->IsTextVisible()) {
757 return; // reboot if logs aren't visible
758 } else {
759 ui->Print("\nInstall from ADB complete.\n");
760 }
761 }
762 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800763 }
764 }
765}
766
767static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800768print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700769 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800770}
771
772int
Oscar Montemayor05231562009-11-30 08:40:57 -0800773main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800774 time_t start = time(NULL);
775
776 // If these fail, there's not really anywhere to complain...
777 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
778 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800779
780 // If this binary is started with the single argument "--adbd",
781 // instead of being the normal recovery binary, it turns into kind
782 // of a stripped-down version of adbd that only supports the
783 // 'sideload' command. Note this must be a real argument, not
784 // anything in the command file or bootloader control block; the
785 // only way recovery should be run with this argument is when it
786 // starts a copy of itself from the apply_from_adb() function.
787 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
788 adb_main();
789 return 0;
790 }
791
Dees_Troy7d15c252012-09-05 20:47:21 -0400792 printf("Starting TWRP %s on %s", EXPAND(TW_VERSION_STR), ctime(&start));
793 // Recovery needs to install world-readable files, so clear umask
794 // set by init
795 umask(0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800796
Dees_Troy7d15c252012-09-05 20:47:21 -0400797 //Device* device = make_device();
798 //ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700799
Dees_Troy7d15c252012-09-05 20:47:21 -0400800 //ui->Init();
801 //ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700802 load_volume_table();
Dees_Troy7d15c252012-09-05 20:47:21 -0400803
804 // Load default values to set DataManager constants and handle ifdefs
805 DataManager_LoadDefaults();
806 printf("Starting the UI...");
807 printf(" result was: %i\n", gui_init());
808 printf("=> Installing busybox into /sbin\n");
809 __system("/sbin/bbinstall.sh"); // Let's install busybox
810 printf("=> Linking mtab\n");
811 __system("ln -s /proc/mounts /etc/mtab"); // And link mtab for mke2fs
812 printf("=> Processing recovery.fstab\n");
813 if (TWPartitionManager::Process_Fstab("/etc/recovery.fstab", 1)) {
814 LOGE("Failing out of recovery due to problem with recovery.fstab.\n");
815 //return -1;
816 }
817 // Load up all the resources
818 gui_loadResources();
819
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800820 get_args(&argc, &argv);
821
822 int previous_runs = 0;
823 const char *send_intent = NULL;
824 const char *update_package = NULL;
825 int wipe_data = 0, wipe_cache = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700826 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800827
828 int arg;
829 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
830 switch (arg) {
831 case 'p': previous_runs = atoi(optarg); break;
832 case 's': send_intent = optarg; break;
833 case 'u': update_package = optarg; break;
834 case 'w': wipe_data = wipe_cache = 1; break;
835 case 'c': wipe_cache = 1; break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700836 case 't': ui->ShowText(true); break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700837 case 'x': just_exit = true; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800838 case '?':
839 LOGE("Invalid command argument\n");
840 continue;
841 }
842 }
843
Stephen Smalley779701d2012-02-09 14:13:23 -0500844#ifdef HAVE_SELINUX
845 struct selinux_opt seopts[] = {
846 { SELABEL_OPT_PATH, "/file_contexts" }
847 };
848
849 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
850
851 if (!sehandle) {
852 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700853 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500854 }
855#endif
856
Dees_Troy7d15c252012-09-05 20:47:21 -0400857 //device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800858
Doug Zongker56c51052010-07-01 09:18:44 -0700859 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800860 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700861 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800862 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700863 printf("\n");
864
865 if (update_package) {
866 // For backwards compatibility on the cache partition only, if
867 // we're given an old 'root' path "CACHE:foo", change it to
868 // "/cache/foo".
869 if (strncmp(update_package, "CACHE:", 6) == 0) {
870 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700871 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700872 strlcpy(modified_path, "/cache/", len);
873 strlcat(modified_path, update_package+6, len);
874 printf("(replacing path \"%s\" with \"%s\")\n",
875 update_package, modified_path);
876 update_package = modified_path;
877 }
878 }
879 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800880
881 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700882 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800883
Dees_Troy7d15c252012-09-05 20:47:21 -0400884 // Check for and run startup script if script exists
885 check_and_run_script("/sbin/runatboot.sh", "boot");
886 check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
887
888#ifdef TW_INCLUDE_INJECTTWRP
889 // Back up TWRP Ramdisk if needed:
890 LOGI("Backing up TWRP ramdisk...\n");
891 __system("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
892 LOGI("Backup of TWRP ramdisk done.\n");
893#endif
894
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800895 int status = INSTALL_SUCCESS;
896
Doug Zongker540d57f2011-01-18 13:36:58 -0800897 if (update_package != NULL) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400898 gui_console_only();
Doug Zongkerd0181b82011-10-19 10:51:12 -0700899 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
900 if (status == INSTALL_SUCCESS && wipe_cache) {
901 if (erase_volume("/cache")) {
902 LOGE("Cache wipe (requested by package) failed.");
903 }
904 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700905 if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700906 } else if (wipe_data) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400907 gui_console_only();
908 if (TWPartitionManager::Factory_Reset()) status = INSTALL_ERROR;
909 //if (device->WipeData()) status = INSTALL_ERROR;
910 //if (erase_volume("/data")) status = INSTALL_ERROR;
911 //if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700912 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700913 } else if (wipe_cache) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400914 gui_console_only();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700915 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700916 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700917 } else if (!just_exit) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800918 status = INSTALL_ERROR; // No command specified
919 }
920
Dees_Troy7d15c252012-09-05 20:47:21 -0400921 //if (status != INSTALL_SUCCESS) ui->SetBackground(RecoveryUI::ERROR);
922 if (status != INSTALL_SUCCESS /*|| ui->IsTextVisible()*/) {
923 DataManager_ReadSettingsFile();
924
925 // Update some of the main data
926 update_tz_environment_variables();
927 gui_start();
928 //prompt_and_wait(device);
Doug Zongker8674a722010-09-15 11:08:23 -0700929 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800930
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800931 // Otherwise, get ready to boot the main system...
932 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -0700933 ui->Print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800934 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800935 return EXIT_SUCCESS;
936}