blob: 450f3633015911914afe659d35b8eb7dc0882592 [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"
Dees_Troy51127312012-09-08 13:08:49 -040054#include "variables.h"
Dees_Troy7d15c252012-09-05 20:47:21 -040055
Dees_Troy5bf43922012-09-07 16:07:55 -040056TWPartitionManager PartitionManager;
57char device_id[64];
Dees_Troy7d15c252012-09-05 20:47:21 -040058
Stephen Smalley779701d2012-02-09 14:13:23 -050059struct selabel_handle *sehandle;
60
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061static const struct option OPTIONS[] = {
62 { "send_intent", required_argument, NULL, 's' },
63 { "update_package", required_argument, NULL, 'u' },
64 { "wipe_data", no_argument, NULL, 'w' },
65 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070066 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070067 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker988500b2009-10-06 14:41:38 -070068 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069};
70
Doug Zongkerd4208f92010-09-20 12:16:13 -070071static const char *COMMAND_FILE = "/cache/recovery/command";
72static const char *INTENT_FILE = "/cache/recovery/intent";
73static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070074static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070075static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070076static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070077static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070079static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070080static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081
Doug Zongker211aebc2011-10-28 15:13:10 -070082RecoveryUI* ui = NULL;
83
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084/*
85 * The recovery tool communicates with the main system through /cache files.
86 * /cache/recovery/command - INPUT - command line for tool, one arg per line
87 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
88 * /cache/recovery/intent - OUTPUT - intent that was passed in
89 *
90 * The arguments which may be supplied in the recovery.command file:
91 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070092 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093 * --wipe_data - erase user data (and cache), then reboot
94 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080095 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070096 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 *
98 * After completing, we remove /cache/recovery/command and reboot.
99 * Arguments may also be supplied in the bootloader control block (BCB).
100 * These important scenarios must be safely restartable at any point:
101 *
102 * FACTORY RESET
103 * 1. user selects "factory reset"
104 * 2. main system writes "--wipe_data" to /cache/recovery/command
105 * 3. main system reboots into recovery
106 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
107 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700108 * 5. erase_volume() reformats /data
109 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800110 * 7. finish_recovery() erases BCB
111 * -- after this, rebooting will restart the main system --
112 * 8. main() calls reboot() to boot main system
113 *
114 * OTA INSTALL
115 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700116 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 * 3. main system reboots into recovery
118 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
119 * -- after this, rebooting will attempt to reinstall the update --
120 * 5. install_package() attempts to install the update
121 * NOTE: the package install must itself be restartable from any point
122 * 6. finish_recovery() erases BCB
123 * -- after this, rebooting will (try to) restart the main system --
124 * 7. ** if install failed **
125 * 7a. prompt_and_wait() shows an error icon and waits for the user
126 * 7b; the user reboots (pulling the battery, etc) into the main system
127 * 8. main() calls maybe_install_firmware_update()
128 * ** if the update contained radio/hboot firmware **:
129 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
130 * -- after this, rebooting will reformat cache & restart main system --
131 * 8b. m_i_f_u() writes firmware image into raw cache partition
132 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
133 * -- after this, rebooting will attempt to reinstall firmware --
134 * 8d. bootloader tries to flash firmware
135 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
136 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700137 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138 * 8g. finish_recovery() erases BCB
139 * -- after this, rebooting will (try to) restart the main system --
140 * 9. main() calls reboot() to boot main system
141 */
142
143static const int MAX_ARG_LENGTH = 4096;
144static const int MAX_ARGS = 100;
145
Doug Zongkerd4208f92010-09-20 12:16:13 -0700146// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700147FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700148fopen_path(const char *path, const char *mode) {
149 if (ensure_path_mounted(path) != 0) {
150 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151 return NULL;
152 }
153
154 // When writing, try to create the containing directory, if necessary.
155 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500156 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157
158 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159 return fp;
160}
161
162// close a file, log an error if the error indicator is set
163static void
164check_and_fclose(FILE *fp, const char *name) {
165 fflush(fp);
166 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
167 fclose(fp);
168}
169
170// command line args come from, in decreasing precedence:
171// - the actual command line
172// - the bootloader control block (one per line, after "recovery")
173// - the contents of COMMAND_FILE (one per line)
174static void
175get_args(int *argc, char ***argv) {
176 struct bootloader_message boot;
177 memset(&boot, 0, sizeof(boot));
178 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
179
180 if (boot.command[0] != 0 && boot.command[0] != 255) {
181 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
182 }
183
184 if (boot.status[0] != 0 && boot.status[0] != 255) {
185 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
186 }
187
188 // --- if arguments weren't supplied, look in the bootloader control block
189 if (*argc <= 1) {
190 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
191 const char *arg = strtok(boot.recovery, "\n");
192 if (arg != NULL && !strcmp(arg, "recovery")) {
193 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
194 (*argv)[0] = strdup(arg);
195 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
196 if ((arg = strtok(NULL, "\n")) == NULL) break;
197 (*argv)[*argc] = strdup(arg);
198 }
199 LOGI("Got arguments from boot message\n");
200 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
201 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
202 }
203 }
204
205 // --- if that doesn't work, try the command file
206 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700207 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 if (fp != NULL) {
209 char *argv0 = (*argv)[0];
210 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
211 (*argv)[0] = argv0; // use the same program name
212
213 char buf[MAX_ARG_LENGTH];
214 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
215 if (!fgets(buf, sizeof(buf), fp)) break;
216 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
217 }
218
219 check_and_fclose(fp, COMMAND_FILE);
220 LOGI("Got arguments from %s\n", COMMAND_FILE);
221 }
222 }
223
224 // --> write the arguments we have back into the bootloader control block
225 // always boot into recovery after this (until finish_recovery() is called)
226 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
227 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
228 int i;
229 for (i = 1; i < *argc; ++i) {
230 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
231 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
232 }
233 set_bootloader_message(&boot);
234}
235
Doug Zongker34c98df2009-08-18 12:05:45 -0700236static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800237set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700238 struct bootloader_message boot;
239 memset(&boot, 0, sizeof(boot));
240 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
241 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
242 set_bootloader_message(&boot);
243}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244
Doug Zongker2c3539e2010-09-29 13:21:30 -0700245// How much of the temp log we have copied to the copy in cache.
Dees_Troy7d15c252012-09-05 20:47:21 -0400246//static long tmplog_offset = 0;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700247
248static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700249copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700250 FILE *log = fopen_path(destination, append ? "a" : "w");
251 if (log == NULL) {
252 LOGE("Can't open %s\n", destination);
253 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700254 FILE *tmplog = fopen(source, "r");
255 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700256 if (append) {
257 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
258 }
259 char buf[4096];
260 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
261 if (append) {
262 tmplog_offset = ftell(tmplog);
263 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700264 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700265 }
266 check_and_fclose(log, destination);
267 }
268}
269
270
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271// clear the recovery command and prepare to boot a (hopefully working) system,
272// copy our log file to cache as well (for the system to read), and
273// record any intent we were asked to communicate back to the system.
274// this function is idempotent: call it as many times as you like.
275static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800276finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 // By this point, we're ready to return to the main system...
278 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700279 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000280 if (fp == NULL) {
281 LOGE("Can't open %s\n", INTENT_FILE);
282 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283 fputs(send_intent, fp);
284 check_and_fclose(fp, INTENT_FILE);
285 }
286 }
287
288 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700289 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
290 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
291 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
292 chmod(LOG_FILE, 0600);
293 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700294 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700295 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700297 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800298 struct bootloader_message boot;
299 memset(&boot, 0, sizeof(boot));
300 set_bootloader_message(&boot);
301
302 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700303 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
304 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305 LOGW("Can't unlink %s\n", COMMAND_FILE);
306 }
307
Doug Zongkerd0181b82011-10-19 10:51:12 -0700308 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800309 sync(); // For good measure.
310}
311
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700313erase_volume(const char *volume) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700314 ui->SetBackground(RecoveryUI::INSTALLING);
315 ui->SetProgressType(RecoveryUI::INDETERMINATE);
316 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700317
Doug Zongkerd0181b82011-10-19 10:51:12 -0700318 ensure_path_unmounted(volume);
319
Doug Zongker2c3539e2010-09-29 13:21:30 -0700320 if (strcmp(volume, "/cache") == 0) {
321 // Any part of the log we'd copied to cache is now gone.
322 // Reset the pointer so we copy from the beginning of the temp
323 // log.
324 tmplog_offset = 0;
325 }
326
Doug Zongkerd4208f92010-09-20 12:16:13 -0700327 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800328}
329
Doug Zongker23ceeea2010-07-08 17:27:55 -0700330static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700331copy_sideloaded_package(const char* original_path) {
332 if (ensure_path_mounted(original_path) != 0) {
333 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700334 return NULL;
335 }
336
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700338 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
339 return NULL;
340 }
341
Doug Zongkerd4208f92010-09-20 12:16:13 -0700342 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700343 if (errno != EEXIST) {
344 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
345 return NULL;
346 }
347 }
348
Doug Zongkerd4208f92010-09-20 12:16:13 -0700349 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
350 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700351 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700352 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
353 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700354 return NULL;
355 }
356 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700357 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700358 return NULL;
359 }
360 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700361 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700362 return NULL;
363 }
364 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700365 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700366 return NULL;
367 }
368
Doug Zongkerd4208f92010-09-20 12:16:13 -0700369 char copy_path[PATH_MAX];
370 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700371 strcat(copy_path, "/package.zip");
372
Doug Zongker28ce47c2011-10-28 10:33:05 -0700373 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700374 if (buffer == NULL) {
375 LOGE("Failed to allocate buffer\n");
376 return NULL;
377 }
378
379 size_t read;
380 FILE* fin = fopen(original_path, "rb");
381 if (fin == NULL) {
382 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
383 return NULL;
384 }
385 FILE* fout = fopen(copy_path, "wb");
386 if (fout == NULL) {
387 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
388 return NULL;
389 }
390
391 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
392 if (fwrite(buffer, 1, read, fout) != read) {
393 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
394 return NULL;
395 }
396 }
397
398 free(buffer);
399
400 if (fclose(fout) != 0) {
401 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
402 return NULL;
403 }
404
405 if (fclose(fin) != 0) {
406 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
407 return NULL;
408 }
409
410 // "adb push" is happy to overwrite read-only files when it's
411 // running as root, but we'll try anyway.
412 if (chmod(copy_path, 0400) != 0) {
413 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
414 return NULL;
415 }
416
Doug Zongkerd4208f92010-09-20 12:16:13 -0700417 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700418}
419
Doug Zongker28ce47c2011-10-28 10:33:05 -0700420static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700421prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700422 const char* title[] = { "Android system recovery <"
423 EXPAND(RECOVERY_API_VERSION) "e>",
424 "",
425 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800426
Doug Zongkerd6837852009-06-17 22:07:13 -0700427 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700428 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700429 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700430 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700431 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700432 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700433
Doug Zongker28ce47c2011-10-28 10:33:05 -0700434 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
435 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700436 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700437 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700438 *h = NULL;
439
Doug Zongkerf93d8162009-09-22 15:16:02 -0700440 return new_headers;
441}
442
443static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700444get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700445 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700446 // throw away keys pressed previously, so user doesn't
447 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700448 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700449
Doug Zongker211aebc2011-10-28 15:13:10 -0700450 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700451 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800452 int chosen_item = -1;
453
Doug Zongkerf93d8162009-09-22 15:16:02 -0700454 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700455 int key = ui->WaitKey();
456 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800457
Doug Zongker5cae4452011-01-25 13:15:30 -0800458 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700459 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800460 continue;
461 } else {
462 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700463 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700464 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800465 }
466 }
467
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700468 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700469
470 if (action < 0) {
471 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700472 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700473 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700474 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700475 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700476 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700477 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700478 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700479 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700480 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700481 chosen_item = selected;
482 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700483 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700484 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800485 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700486 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700487 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800488 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700489 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800490
Doug Zongker211aebc2011-10-28 15:13:10 -0700491 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700492 return chosen_item;
493}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494
Doug Zongker8674a722010-09-15 11:08:23 -0700495static int compare_string(const void* a, const void* b) {
496 return strcmp(*(const char**)a, *(const char**)b);
497}
498
499static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700500update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700501 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700502 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700503
Doug Zongker8674a722010-09-15 11:08:23 -0700504 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700505 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700506 "",
507 NULL };
508 DIR* d;
509 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700510 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700511 if (d == NULL) {
512 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700513 if (unmount_when_done != NULL) {
514 ensure_path_unmounted(unmount_when_done);
515 }
Doug Zongker8674a722010-09-15 11:08:23 -0700516 return 0;
517 }
518
Doug Zongker28ce47c2011-10-28 10:33:05 -0700519 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700520
521 int d_size = 0;
522 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700523 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700524 int z_size = 1;
525 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700526 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700527 zips[0] = strdup("../");
528
529 while ((de = readdir(d)) != NULL) {
530 int name_len = strlen(de->d_name);
531
532 if (de->d_type == DT_DIR) {
533 // skip "." and ".." entries
534 if (name_len == 1 && de->d_name[0] == '.') continue;
535 if (name_len == 2 && de->d_name[0] == '.' &&
536 de->d_name[1] == '.') continue;
537
538 if (d_size >= d_alloc) {
539 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700540 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700541 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700542 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700543 strcpy(dirs[d_size], de->d_name);
544 dirs[d_size][name_len] = '/';
545 dirs[d_size][name_len+1] = '\0';
546 ++d_size;
547 } else if (de->d_type == DT_REG &&
548 name_len >= 4 &&
549 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
550 if (z_size >= z_alloc) {
551 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700552 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700553 }
554 zips[z_size++] = strdup(de->d_name);
555 }
556 }
557 closedir(d);
558
559 qsort(dirs, d_size, sizeof(char*), compare_string);
560 qsort(zips, z_size, sizeof(char*), compare_string);
561
562 // append dirs to the zips list
563 if (d_size + z_size + 1 > z_alloc) {
564 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700565 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700566 }
567 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
568 free(dirs);
569 z_size += d_size;
570 zips[z_size] = NULL;
571
572 int result;
573 int chosen_item = 0;
574 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700575 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700576
577 char* item = zips[chosen_item];
578 int item_len = strlen(item);
579 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700580 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700581 result = -1;
582 break;
583 } else if (item[item_len-1] == '/') {
584 // recurse down into a subdirectory
585 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700586 strlcpy(new_path, path, PATH_MAX);
587 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700588 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700589 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700590 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700591 if (result >= 0) break;
592 } else {
593 // selected a zip file: attempt to install it, and return
594 // the status to the caller.
595 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700596 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700597 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700598 strlcat(new_path, item, PATH_MAX);
599
Doug Zongker211aebc2011-10-28 15:13:10 -0700600 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700601 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700602 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700603 if (unmount_when_done != NULL) {
604 ensure_path_unmounted(unmount_when_done);
605 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700606 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700607 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700608 free(copy);
609 } else {
610 result = INSTALL_ERROR;
611 }
Doug Zongker8674a722010-09-15 11:08:23 -0700612 break;
613 }
614 } while (true);
615
616 int i;
617 for (i = 0; i < z_size; ++i) free(zips[i]);
618 free(zips);
619 free(headers);
620
Michael Ward9d2629c2011-06-23 22:14:24 -0700621 if (unmount_when_done != NULL) {
622 ensure_path_unmounted(unmount_when_done);
623 }
Doug Zongker8674a722010-09-15 11:08:23 -0700624 return result;
625}
626
Doug Zongkerf93d8162009-09-22 15:16:02 -0700627static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700628wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700629 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700630 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700631
Doug Zongkerf93d8162009-09-22 15:16:02 -0700632 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700633 const char* headers[] = { "Confirm wipe of all user data?",
634 " THIS CAN NOT BE UNDONE.",
635 "",
636 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700637 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700638 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800639
Doug Zongker28ce47c2011-10-28 10:33:05 -0700640 const char* items[] = { " No",
641 " No",
642 " No",
643 " No",
644 " No",
645 " No",
646 " No",
647 " Yes -- delete all user data", // [7]
648 " No",
649 " No",
650 " No",
651 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800652
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700653 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700654 if (chosen_item != 7) {
655 return;
656 }
657 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700658
Doug Zongker211aebc2011-10-28 15:13:10 -0700659 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700660 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700661 erase_volume("/data");
662 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700663 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700664}
665
666static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700667prompt_and_wait(Device* device) {
668 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700669
670 for (;;) {
671 finish_recovery(NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700672 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700673
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700674 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700675
676 // device-specific code may take some action here. It may
677 // return one of the core actions handled in the switch
678 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700679 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700680
Michael Ward9d2629c2011-06-23 22:14:24 -0700681 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700682 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700683 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700684 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700685 return;
686
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700687 case Device::WIPE_DATA:
688 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700689 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700690 break;
691
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700692 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700693 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700694 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700695 ui->Print("Cache wipe complete.\n");
696 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700697 break;
698
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700699 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800700 // Some packages expect /cache to be mounted (eg,
701 // standard incremental packages expect to use /cache
702 // as scratch space).
703 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700704 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700705 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700706 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700707 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700708 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700709 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700710 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700711 }
712 }
Doug Zongker8674a722010-09-15 11:08:23 -0700713 if (status >= 0) {
714 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700715 ui->SetBackground(RecoveryUI::ERROR);
716 ui->Print("Installation aborted.\n");
717 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700718 return; // reboot if logs aren't visible
719 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700720 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700721 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700722 }
723 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700724
725 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700726 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700727 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700728 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700729 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700730 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700731 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700732 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700733 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700734 }
735 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700736 if (status >= 0) {
737 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700738 ui->SetBackground(RecoveryUI::ERROR);
739 ui->Print("Installation aborted.\n");
740 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700741 return; // reboot if logs aren't visible
742 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700743 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700744 }
745 }
746 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800747
748 case Device::APPLY_ADB_SIDELOAD:
749 ensure_path_mounted(CACHE_ROOT);
750 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
751 if (status >= 0) {
752 if (status != INSTALL_SUCCESS) {
753 ui->SetBackground(RecoveryUI::ERROR);
754 ui->Print("Installation aborted.\n");
755 } else if (!ui->IsTextVisible()) {
756 return; // reboot if logs aren't visible
757 } else {
758 ui->Print("\nInstall from ADB complete.\n");
759 }
760 }
761 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800762 }
763 }
764}
765
766static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800767print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700768 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800769}
770
771int
Oscar Montemayor05231562009-11-30 08:40:57 -0800772main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800773 time_t start = time(NULL);
774
775 // If these fail, there's not really anywhere to complain...
776 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
777 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800778
779 // If this binary is started with the single argument "--adbd",
780 // instead of being the normal recovery binary, it turns into kind
781 // of a stripped-down version of adbd that only supports the
782 // 'sideload' command. Note this must be a real argument, not
783 // anything in the command file or bootloader control block; the
784 // only way recovery should be run with this argument is when it
785 // starts a copy of itself from the apply_from_adb() function.
786 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
787 adb_main();
788 return 0;
789 }
790
Dees_Troy51127312012-09-08 13:08:49 -0400791 printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&start));
Dees_Troy7d15c252012-09-05 20:47:21 -0400792 // Recovery needs to install world-readable files, so clear umask
793 // set by init
794 umask(0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800795
Dees_Troy7d15c252012-09-05 20:47:21 -0400796 //Device* device = make_device();
797 //ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700798
Dees_Troy7d15c252012-09-05 20:47:21 -0400799 //ui->Init();
800 //ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700801 load_volume_table();
Dees_Troy7d15c252012-09-05 20:47:21 -0400802
803 // Load default values to set DataManager constants and handle ifdefs
804 DataManager_LoadDefaults();
805 printf("Starting the UI...");
Dees_Troy51127312012-09-08 13:08:49 -0400806 gui_init();
Dees_Troy7d15c252012-09-05 20:47:21 -0400807 printf("=> Installing busybox into /sbin\n");
808 __system("/sbin/bbinstall.sh"); // Let's install busybox
809 printf("=> Linking mtab\n");
810 __system("ln -s /proc/mounts /etc/mtab"); // And link mtab for mke2fs
811 printf("=> Processing recovery.fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400812 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400813 LOGE("Failing out of recovery due to problem with recovery.fstab.\n");
814 //return -1;
815 }
816 // Load up all the resources
817 gui_loadResources();
818
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800819 get_args(&argc, &argv);
820
821 int previous_runs = 0;
822 const char *send_intent = NULL;
823 const char *update_package = NULL;
824 int wipe_data = 0, wipe_cache = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700825 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800826
827 int arg;
828 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
829 switch (arg) {
830 case 'p': previous_runs = atoi(optarg); break;
831 case 's': send_intent = optarg; break;
832 case 'u': update_package = optarg; break;
833 case 'w': wipe_data = wipe_cache = 1; break;
834 case 'c': wipe_cache = 1; break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700835 case 't': ui->ShowText(true); break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700836 case 'x': just_exit = true; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800837 case '?':
838 LOGE("Invalid command argument\n");
839 continue;
840 }
841 }
842
Stephen Smalley779701d2012-02-09 14:13:23 -0500843#ifdef HAVE_SELINUX
844 struct selinux_opt seopts[] = {
845 { SELABEL_OPT_PATH, "/file_contexts" }
846 };
847
848 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
849
850 if (!sehandle) {
851 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700852 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500853 }
854#endif
855
Dees_Troy7d15c252012-09-05 20:47:21 -0400856 //device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800857
Doug Zongker56c51052010-07-01 09:18:44 -0700858 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800859 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700860 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800861 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700862 printf("\n");
863
864 if (update_package) {
865 // For backwards compatibility on the cache partition only, if
866 // we're given an old 'root' path "CACHE:foo", change it to
867 // "/cache/foo".
868 if (strncmp(update_package, "CACHE:", 6) == 0) {
869 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700870 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700871 strlcpy(modified_path, "/cache/", len);
872 strlcat(modified_path, update_package+6, len);
873 printf("(replacing path \"%s\" with \"%s\")\n",
874 update_package, modified_path);
875 update_package = modified_path;
876 }
877 }
878 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800879
880 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700881 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800882
Dees_Troy7d15c252012-09-05 20:47:21 -0400883 // Check for and run startup script if script exists
884 check_and_run_script("/sbin/runatboot.sh", "boot");
885 check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
886
887#ifdef TW_INCLUDE_INJECTTWRP
888 // Back up TWRP Ramdisk if needed:
889 LOGI("Backing up TWRP ramdisk...\n");
890 __system("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
891 LOGI("Backup of TWRP ramdisk done.\n");
892#endif
893
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800894 int status = INSTALL_SUCCESS;
895
Doug Zongker540d57f2011-01-18 13:36:58 -0800896 if (update_package != NULL) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400897 gui_console_only();
Doug Zongkerd0181b82011-10-19 10:51:12 -0700898 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
899 if (status == INSTALL_SUCCESS && wipe_cache) {
900 if (erase_volume("/cache")) {
901 LOGE("Cache wipe (requested by package) failed.");
902 }
903 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700904 if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700905 } else if (wipe_data) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400906 gui_console_only();
Dees_Troy5bf43922012-09-07 16:07:55 -0400907 if (PartitionManager.Factory_Reset()) status = INSTALL_ERROR;
Dees_Troy7d15c252012-09-05 20:47:21 -0400908 //if (device->WipeData()) status = INSTALL_ERROR;
909 //if (erase_volume("/data")) status = INSTALL_ERROR;
910 //if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700911 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700912 } else if (wipe_cache) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400913 gui_console_only();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700914 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700915 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700916 } else if (!just_exit) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800917 status = INSTALL_ERROR; // No command specified
918 }
919
Dees_Troy7d15c252012-09-05 20:47:21 -0400920 //if (status != INSTALL_SUCCESS) ui->SetBackground(RecoveryUI::ERROR);
921 if (status != INSTALL_SUCCESS /*|| ui->IsTextVisible()*/) {
922 DataManager_ReadSettingsFile();
923
924 // Update some of the main data
925 update_tz_environment_variables();
926 gui_start();
927 //prompt_and_wait(device);
Doug Zongker8674a722010-09-15 11:08:23 -0700928 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800929
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800930 // Otherwise, get ready to boot the main system...
931 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -0700932 ui->Print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800933 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800934 return EXIT_SUCCESS;
935}