blob: 319cdc72aec40f798cea4d3b6bbd8bff48215cbb [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
Dees_Troy5bf43922012-09-07 16:07:55 -040055TWPartitionManager PartitionManager;
56char device_id[64];
Dees_Troy7d15c252012-09-05 20:47:21 -040057
Stephen Smalley779701d2012-02-09 14:13:23 -050058struct selabel_handle *sehandle;
59
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060static const struct option OPTIONS[] = {
61 { "send_intent", required_argument, NULL, 's' },
62 { "update_package", required_argument, NULL, 'u' },
63 { "wipe_data", no_argument, NULL, 'w' },
64 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070065 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070066 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker988500b2009-10-06 14:41:38 -070067 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068};
69
Doug Zongkerd4208f92010-09-20 12:16:13 -070070static const char *COMMAND_FILE = "/cache/recovery/command";
71static const char *INTENT_FILE = "/cache/recovery/intent";
72static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070073static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070074static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070075static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070076static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070078static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070079static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080
Doug Zongker211aebc2011-10-28 15:13:10 -070081RecoveryUI* ui = NULL;
82
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083/*
84 * The recovery tool communicates with the main system through /cache files.
85 * /cache/recovery/command - INPUT - command line for tool, one arg per line
86 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
87 * /cache/recovery/intent - OUTPUT - intent that was passed in
88 *
89 * The arguments which may be supplied in the recovery.command file:
90 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070091 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092 * --wipe_data - erase user data (and cache), then reboot
93 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080094 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070095 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096 *
97 * After completing, we remove /cache/recovery/command and reboot.
98 * Arguments may also be supplied in the bootloader control block (BCB).
99 * These important scenarios must be safely restartable at any point:
100 *
101 * FACTORY RESET
102 * 1. user selects "factory reset"
103 * 2. main system writes "--wipe_data" to /cache/recovery/command
104 * 3. main system reboots into recovery
105 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
106 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700107 * 5. erase_volume() reformats /data
108 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800109 * 7. finish_recovery() erases BCB
110 * -- after this, rebooting will restart the main system --
111 * 8. main() calls reboot() to boot main system
112 *
113 * OTA INSTALL
114 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700115 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116 * 3. main system reboots into recovery
117 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
118 * -- after this, rebooting will attempt to reinstall the update --
119 * 5. install_package() attempts to install the update
120 * NOTE: the package install must itself be restartable from any point
121 * 6. finish_recovery() erases BCB
122 * -- after this, rebooting will (try to) restart the main system --
123 * 7. ** if install failed **
124 * 7a. prompt_and_wait() shows an error icon and waits for the user
125 * 7b; the user reboots (pulling the battery, etc) into the main system
126 * 8. main() calls maybe_install_firmware_update()
127 * ** if the update contained radio/hboot firmware **:
128 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
129 * -- after this, rebooting will reformat cache & restart main system --
130 * 8b. m_i_f_u() writes firmware image into raw cache partition
131 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
132 * -- after this, rebooting will attempt to reinstall firmware --
133 * 8d. bootloader tries to flash firmware
134 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
135 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700136 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 * 8g. finish_recovery() erases BCB
138 * -- after this, rebooting will (try to) restart the main system --
139 * 9. main() calls reboot() to boot main system
140 */
141
142static const int MAX_ARG_LENGTH = 4096;
143static const int MAX_ARGS = 100;
144
Doug Zongkerd4208f92010-09-20 12:16:13 -0700145// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700146FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700147fopen_path(const char *path, const char *mode) {
148 if (ensure_path_mounted(path) != 0) {
149 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150 return NULL;
151 }
152
153 // When writing, try to create the containing directory, if necessary.
154 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500155 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156
157 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 return fp;
159}
160
161// close a file, log an error if the error indicator is set
162static void
163check_and_fclose(FILE *fp, const char *name) {
164 fflush(fp);
165 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
166 fclose(fp);
167}
168
169// command line args come from, in decreasing precedence:
170// - the actual command line
171// - the bootloader control block (one per line, after "recovery")
172// - the contents of COMMAND_FILE (one per line)
173static void
174get_args(int *argc, char ***argv) {
175 struct bootloader_message boot;
176 memset(&boot, 0, sizeof(boot));
177 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
178
179 if (boot.command[0] != 0 && boot.command[0] != 255) {
180 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
181 }
182
183 if (boot.status[0] != 0 && boot.status[0] != 255) {
184 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
185 }
186
187 // --- if arguments weren't supplied, look in the bootloader control block
188 if (*argc <= 1) {
189 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
190 const char *arg = strtok(boot.recovery, "\n");
191 if (arg != NULL && !strcmp(arg, "recovery")) {
192 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
193 (*argv)[0] = strdup(arg);
194 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
195 if ((arg = strtok(NULL, "\n")) == NULL) break;
196 (*argv)[*argc] = strdup(arg);
197 }
198 LOGI("Got arguments from boot message\n");
199 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
200 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
201 }
202 }
203
204 // --- if that doesn't work, try the command file
205 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700206 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207 if (fp != NULL) {
208 char *argv0 = (*argv)[0];
209 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
210 (*argv)[0] = argv0; // use the same program name
211
212 char buf[MAX_ARG_LENGTH];
213 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
214 if (!fgets(buf, sizeof(buf), fp)) break;
215 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
216 }
217
218 check_and_fclose(fp, COMMAND_FILE);
219 LOGI("Got arguments from %s\n", COMMAND_FILE);
220 }
221 }
222
223 // --> write the arguments we have back into the bootloader control block
224 // always boot into recovery after this (until finish_recovery() is called)
225 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
226 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
227 int i;
228 for (i = 1; i < *argc; ++i) {
229 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
230 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
231 }
232 set_bootloader_message(&boot);
233}
234
Doug Zongker34c98df2009-08-18 12:05:45 -0700235static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800236set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700237 struct bootloader_message boot;
238 memset(&boot, 0, sizeof(boot));
239 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
240 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
241 set_bootloader_message(&boot);
242}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243
Doug Zongker2c3539e2010-09-29 13:21:30 -0700244// How much of the temp log we have copied to the copy in cache.
Dees_Troy7d15c252012-09-05 20:47:21 -0400245//static long tmplog_offset = 0;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700246
247static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700248copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700249 FILE *log = fopen_path(destination, append ? "a" : "w");
250 if (log == NULL) {
251 LOGE("Can't open %s\n", destination);
252 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700253 FILE *tmplog = fopen(source, "r");
254 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700255 if (append) {
256 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
257 }
258 char buf[4096];
259 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
260 if (append) {
261 tmplog_offset = ftell(tmplog);
262 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700263 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700264 }
265 check_and_fclose(log, destination);
266 }
267}
268
269
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270// clear the recovery command and prepare to boot a (hopefully working) system,
271// copy our log file to cache as well (for the system to read), and
272// record any intent we were asked to communicate back to the system.
273// this function is idempotent: call it as many times as you like.
274static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800275finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800276 // By this point, we're ready to return to the main system...
277 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700278 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000279 if (fp == NULL) {
280 LOGE("Can't open %s\n", INTENT_FILE);
281 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 fputs(send_intent, fp);
283 check_and_fclose(fp, INTENT_FILE);
284 }
285 }
286
287 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700288 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
289 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
290 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
291 chmod(LOG_FILE, 0600);
292 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700293 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700294 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700296 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297 struct bootloader_message boot;
298 memset(&boot, 0, sizeof(boot));
299 set_bootloader_message(&boot);
300
301 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700302 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
303 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800304 LOGW("Can't unlink %s\n", COMMAND_FILE);
305 }
306
Doug Zongkerd0181b82011-10-19 10:51:12 -0700307 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 sync(); // For good measure.
309}
310
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800311static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700312erase_volume(const char *volume) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700313 ui->SetBackground(RecoveryUI::INSTALLING);
314 ui->SetProgressType(RecoveryUI::INDETERMINATE);
315 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700316
Doug Zongkerd0181b82011-10-19 10:51:12 -0700317 ensure_path_unmounted(volume);
318
Doug Zongker2c3539e2010-09-29 13:21:30 -0700319 if (strcmp(volume, "/cache") == 0) {
320 // Any part of the log we'd copied to cache is now gone.
321 // Reset the pointer so we copy from the beginning of the temp
322 // log.
323 tmplog_offset = 0;
324 }
325
Doug Zongkerd4208f92010-09-20 12:16:13 -0700326 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800327}
328
Doug Zongker23ceeea2010-07-08 17:27:55 -0700329static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700330copy_sideloaded_package(const char* original_path) {
331 if (ensure_path_mounted(original_path) != 0) {
332 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700333 return NULL;
334 }
335
Doug Zongkerd4208f92010-09-20 12:16:13 -0700336 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700337 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
338 return NULL;
339 }
340
Doug Zongkerd4208f92010-09-20 12:16:13 -0700341 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700342 if (errno != EEXIST) {
343 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
344 return NULL;
345 }
346 }
347
Doug Zongkerd4208f92010-09-20 12:16:13 -0700348 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
349 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700350 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700351 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
352 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700353 return NULL;
354 }
355 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700356 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700357 return NULL;
358 }
359 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700360 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700361 return NULL;
362 }
363 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700364 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700365 return NULL;
366 }
367
Doug Zongkerd4208f92010-09-20 12:16:13 -0700368 char copy_path[PATH_MAX];
369 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700370 strcat(copy_path, "/package.zip");
371
Doug Zongker28ce47c2011-10-28 10:33:05 -0700372 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700373 if (buffer == NULL) {
374 LOGE("Failed to allocate buffer\n");
375 return NULL;
376 }
377
378 size_t read;
379 FILE* fin = fopen(original_path, "rb");
380 if (fin == NULL) {
381 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
382 return NULL;
383 }
384 FILE* fout = fopen(copy_path, "wb");
385 if (fout == NULL) {
386 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
387 return NULL;
388 }
389
390 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
391 if (fwrite(buffer, 1, read, fout) != read) {
392 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
393 return NULL;
394 }
395 }
396
397 free(buffer);
398
399 if (fclose(fout) != 0) {
400 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
401 return NULL;
402 }
403
404 if (fclose(fin) != 0) {
405 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
406 return NULL;
407 }
408
409 // "adb push" is happy to overwrite read-only files when it's
410 // running as root, but we'll try anyway.
411 if (chmod(copy_path, 0400) != 0) {
412 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
413 return NULL;
414 }
415
Doug Zongkerd4208f92010-09-20 12:16:13 -0700416 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700417}
418
Doug Zongker28ce47c2011-10-28 10:33:05 -0700419static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700420prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700421 const char* title[] = { "Android system recovery <"
422 EXPAND(RECOVERY_API_VERSION) "e>",
423 "",
424 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800425
Doug Zongkerd6837852009-06-17 22:07:13 -0700426 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700427 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700428 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700429 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700430 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700431 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700432
Doug Zongker28ce47c2011-10-28 10:33:05 -0700433 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
434 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700435 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700436 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700437 *h = NULL;
438
Doug Zongkerf93d8162009-09-22 15:16:02 -0700439 return new_headers;
440}
441
442static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700443get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700444 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700445 // throw away keys pressed previously, so user doesn't
446 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700447 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700448
Doug Zongker211aebc2011-10-28 15:13:10 -0700449 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700450 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800451 int chosen_item = -1;
452
Doug Zongkerf93d8162009-09-22 15:16:02 -0700453 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700454 int key = ui->WaitKey();
455 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800456
Doug Zongker5cae4452011-01-25 13:15:30 -0800457 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700458 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800459 continue;
460 } else {
461 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700462 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700463 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800464 }
465 }
466
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700467 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700468
469 if (action < 0) {
470 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700471 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700472 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700473 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700474 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700475 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700476 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700477 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700478 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700479 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700480 chosen_item = selected;
481 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700482 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700483 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800484 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700485 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700486 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800487 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700488 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800489
Doug Zongker211aebc2011-10-28 15:13:10 -0700490 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700491 return chosen_item;
492}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800493
Doug Zongker8674a722010-09-15 11:08:23 -0700494static int compare_string(const void* a, const void* b) {
495 return strcmp(*(const char**)a, *(const char**)b);
496}
497
498static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700499update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700500 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700501 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700502
Doug Zongker8674a722010-09-15 11:08:23 -0700503 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700504 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700505 "",
506 NULL };
507 DIR* d;
508 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700509 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700510 if (d == NULL) {
511 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700512 if (unmount_when_done != NULL) {
513 ensure_path_unmounted(unmount_when_done);
514 }
Doug Zongker8674a722010-09-15 11:08:23 -0700515 return 0;
516 }
517
Doug Zongker28ce47c2011-10-28 10:33:05 -0700518 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700519
520 int d_size = 0;
521 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700522 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700523 int z_size = 1;
524 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700525 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700526 zips[0] = strdup("../");
527
528 while ((de = readdir(d)) != NULL) {
529 int name_len = strlen(de->d_name);
530
531 if (de->d_type == DT_DIR) {
532 // skip "." and ".." entries
533 if (name_len == 1 && de->d_name[0] == '.') continue;
534 if (name_len == 2 && de->d_name[0] == '.' &&
535 de->d_name[1] == '.') continue;
536
537 if (d_size >= d_alloc) {
538 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700539 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700540 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700541 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700542 strcpy(dirs[d_size], de->d_name);
543 dirs[d_size][name_len] = '/';
544 dirs[d_size][name_len+1] = '\0';
545 ++d_size;
546 } else if (de->d_type == DT_REG &&
547 name_len >= 4 &&
548 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
549 if (z_size >= z_alloc) {
550 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700551 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700552 }
553 zips[z_size++] = strdup(de->d_name);
554 }
555 }
556 closedir(d);
557
558 qsort(dirs, d_size, sizeof(char*), compare_string);
559 qsort(zips, z_size, sizeof(char*), compare_string);
560
561 // append dirs to the zips list
562 if (d_size + z_size + 1 > z_alloc) {
563 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700564 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700565 }
566 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
567 free(dirs);
568 z_size += d_size;
569 zips[z_size] = NULL;
570
571 int result;
572 int chosen_item = 0;
573 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700574 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700575
576 char* item = zips[chosen_item];
577 int item_len = strlen(item);
578 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700579 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700580 result = -1;
581 break;
582 } else if (item[item_len-1] == '/') {
583 // recurse down into a subdirectory
584 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700585 strlcpy(new_path, path, PATH_MAX);
586 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700587 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700588 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700589 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700590 if (result >= 0) break;
591 } else {
592 // selected a zip file: attempt to install it, and return
593 // the status to the caller.
594 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700595 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700596 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700597 strlcat(new_path, item, PATH_MAX);
598
Doug Zongker211aebc2011-10-28 15:13:10 -0700599 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700600 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700601 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700602 if (unmount_when_done != NULL) {
603 ensure_path_unmounted(unmount_when_done);
604 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700605 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700606 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700607 free(copy);
608 } else {
609 result = INSTALL_ERROR;
610 }
Doug Zongker8674a722010-09-15 11:08:23 -0700611 break;
612 }
613 } while (true);
614
615 int i;
616 for (i = 0; i < z_size; ++i) free(zips[i]);
617 free(zips);
618 free(headers);
619
Michael Ward9d2629c2011-06-23 22:14:24 -0700620 if (unmount_when_done != NULL) {
621 ensure_path_unmounted(unmount_when_done);
622 }
Doug Zongker8674a722010-09-15 11:08:23 -0700623 return result;
624}
625
Doug Zongkerf93d8162009-09-22 15:16:02 -0700626static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700627wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700628 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700629 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700630
Doug Zongkerf93d8162009-09-22 15:16:02 -0700631 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700632 const char* headers[] = { "Confirm wipe of all user data?",
633 " THIS CAN NOT BE UNDONE.",
634 "",
635 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700636 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700637 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800638
Doug Zongker28ce47c2011-10-28 10:33:05 -0700639 const char* items[] = { " No",
640 " No",
641 " No",
642 " No",
643 " No",
644 " No",
645 " No",
646 " Yes -- delete all user data", // [7]
647 " No",
648 " No",
649 " No",
650 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800651
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700652 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700653 if (chosen_item != 7) {
654 return;
655 }
656 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700657
Doug Zongker211aebc2011-10-28 15:13:10 -0700658 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700659 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700660 erase_volume("/data");
661 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700662 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700663}
664
665static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700666prompt_and_wait(Device* device) {
667 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700668
669 for (;;) {
670 finish_recovery(NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700671 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700672
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700673 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700674
675 // device-specific code may take some action here. It may
676 // return one of the core actions handled in the switch
677 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700678 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700679
Michael Ward9d2629c2011-06-23 22:14:24 -0700680 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700681 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700682 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700683 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700684 return;
685
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700686 case Device::WIPE_DATA:
687 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700688 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700689 break;
690
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700691 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700692 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700693 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700694 ui->Print("Cache wipe complete.\n");
695 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700696 break;
697
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700698 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800699 // Some packages expect /cache to be mounted (eg,
700 // standard incremental packages expect to use /cache
701 // as scratch space).
702 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700703 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700704 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700705 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700706 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700707 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700708 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700709 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700710 }
711 }
Doug Zongker8674a722010-09-15 11:08:23 -0700712 if (status >= 0) {
713 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700714 ui->SetBackground(RecoveryUI::ERROR);
715 ui->Print("Installation aborted.\n");
716 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700717 return; // reboot if logs aren't visible
718 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700719 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700720 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700721 }
722 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700723
724 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700725 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700726 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700727 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700728 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700729 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700730 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700731 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700732 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700733 }
734 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700735 if (status >= 0) {
736 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700737 ui->SetBackground(RecoveryUI::ERROR);
738 ui->Print("Installation aborted.\n");
739 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700740 return; // reboot if logs aren't visible
741 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700742 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700743 }
744 }
745 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800746
747 case Device::APPLY_ADB_SIDELOAD:
748 ensure_path_mounted(CACHE_ROOT);
749 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
750 if (status >= 0) {
751 if (status != INSTALL_SUCCESS) {
752 ui->SetBackground(RecoveryUI::ERROR);
753 ui->Print("Installation aborted.\n");
754 } else if (!ui->IsTextVisible()) {
755 return; // reboot if logs aren't visible
756 } else {
757 ui->Print("\nInstall from ADB complete.\n");
758 }
759 }
760 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800761 }
762 }
763}
764
765static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800766print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700767 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800768}
769
770int
Oscar Montemayor05231562009-11-30 08:40:57 -0800771main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800772 time_t start = time(NULL);
773
774 // If these fail, there's not really anywhere to complain...
775 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
776 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800777
778 // If this binary is started with the single argument "--adbd",
779 // instead of being the normal recovery binary, it turns into kind
780 // of a stripped-down version of adbd that only supports the
781 // 'sideload' command. Note this must be a real argument, not
782 // anything in the command file or bootloader control block; the
783 // only way recovery should be run with this argument is when it
784 // starts a copy of itself from the apply_from_adb() function.
785 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
786 adb_main();
787 return 0;
788 }
789
Dees_Troy7d15c252012-09-05 20:47:21 -0400790 printf("Starting TWRP %s on %s", EXPAND(TW_VERSION_STR), ctime(&start));
791 // Recovery needs to install world-readable files, so clear umask
792 // set by init
793 umask(0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800794
Dees_Troy7d15c252012-09-05 20:47:21 -0400795 //Device* device = make_device();
796 //ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700797
Dees_Troy7d15c252012-09-05 20:47:21 -0400798 //ui->Init();
799 //ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700800 load_volume_table();
Dees_Troy7d15c252012-09-05 20:47:21 -0400801
802 // Load default values to set DataManager constants and handle ifdefs
803 DataManager_LoadDefaults();
804 printf("Starting the UI...");
805 printf(" result was: %i\n", gui_init());
806 printf("=> Installing busybox into /sbin\n");
807 __system("/sbin/bbinstall.sh"); // Let's install busybox
808 printf("=> Linking mtab\n");
809 __system("ln -s /proc/mounts /etc/mtab"); // And link mtab for mke2fs
810 printf("=> Processing recovery.fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400811 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400812 LOGE("Failing out of recovery due to problem with recovery.fstab.\n");
813 //return -1;
814 }
815 // Load up all the resources
816 gui_loadResources();
817
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800818 get_args(&argc, &argv);
819
820 int previous_runs = 0;
821 const char *send_intent = NULL;
822 const char *update_package = NULL;
823 int wipe_data = 0, wipe_cache = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700824 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800825
826 int arg;
827 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
828 switch (arg) {
829 case 'p': previous_runs = atoi(optarg); break;
830 case 's': send_intent = optarg; break;
831 case 'u': update_package = optarg; break;
832 case 'w': wipe_data = wipe_cache = 1; break;
833 case 'c': wipe_cache = 1; break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700834 case 't': ui->ShowText(true); break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700835 case 'x': just_exit = true; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800836 case '?':
837 LOGE("Invalid command argument\n");
838 continue;
839 }
840 }
841
Stephen Smalley779701d2012-02-09 14:13:23 -0500842#ifdef HAVE_SELINUX
843 struct selinux_opt seopts[] = {
844 { SELABEL_OPT_PATH, "/file_contexts" }
845 };
846
847 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
848
849 if (!sehandle) {
850 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700851 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500852 }
853#endif
854
Dees_Troy7d15c252012-09-05 20:47:21 -0400855 //device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800856
Doug Zongker56c51052010-07-01 09:18:44 -0700857 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800858 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700859 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800860 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700861 printf("\n");
862
863 if (update_package) {
864 // For backwards compatibility on the cache partition only, if
865 // we're given an old 'root' path "CACHE:foo", change it to
866 // "/cache/foo".
867 if (strncmp(update_package, "CACHE:", 6) == 0) {
868 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700869 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700870 strlcpy(modified_path, "/cache/", len);
871 strlcat(modified_path, update_package+6, len);
872 printf("(replacing path \"%s\" with \"%s\")\n",
873 update_package, modified_path);
874 update_package = modified_path;
875 }
876 }
877 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800878
879 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700880 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800881
Dees_Troy7d15c252012-09-05 20:47:21 -0400882 // Check for and run startup script if script exists
883 check_and_run_script("/sbin/runatboot.sh", "boot");
884 check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
885
886#ifdef TW_INCLUDE_INJECTTWRP
887 // Back up TWRP Ramdisk if needed:
888 LOGI("Backing up TWRP ramdisk...\n");
889 __system("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
890 LOGI("Backup of TWRP ramdisk done.\n");
891#endif
892
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800893 int status = INSTALL_SUCCESS;
894
Doug Zongker540d57f2011-01-18 13:36:58 -0800895 if (update_package != NULL) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400896 gui_console_only();
Doug Zongkerd0181b82011-10-19 10:51:12 -0700897 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
898 if (status == INSTALL_SUCCESS && wipe_cache) {
899 if (erase_volume("/cache")) {
900 LOGE("Cache wipe (requested by package) failed.");
901 }
902 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700903 if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700904 } else if (wipe_data) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400905 gui_console_only();
Dees_Troy5bf43922012-09-07 16:07:55 -0400906 if (PartitionManager.Factory_Reset()) status = INSTALL_ERROR;
Dees_Troy7d15c252012-09-05 20:47:21 -0400907 //if (device->WipeData()) status = INSTALL_ERROR;
908 //if (erase_volume("/data")) status = INSTALL_ERROR;
909 //if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700910 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700911 } else if (wipe_cache) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400912 gui_console_only();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700913 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700914 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700915 } else if (!just_exit) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800916 status = INSTALL_ERROR; // No command specified
917 }
918
Dees_Troy7d15c252012-09-05 20:47:21 -0400919 //if (status != INSTALL_SUCCESS) ui->SetBackground(RecoveryUI::ERROR);
920 if (status != INSTALL_SUCCESS /*|| ui->IsTextVisible()*/) {
921 DataManager_ReadSettingsFile();
922
923 // Update some of the main data
924 update_tz_environment_variables();
925 gui_start();
926 //prompt_and_wait(device);
Doug Zongker8674a722010-09-15 11:08:23 -0700927 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800928
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800929 // Otherwise, get ready to boot the main system...
930 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -0700931 ui->Print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800932 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800933 return EXIT_SUCCESS;
934}