blob: 33b54badc900ded11815a8dd022ed9a0bc2518ae [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
Shashank Mittal815ca5d2010-07-27 11:09:19 -07003 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <limits.h>
23#include <linux/input.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/reboot.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070028#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
Doug Zongker8674a722010-09-15 11:08:23 -070032#include <dirent.h>
Koushik Dutta1741dcd2010-06-11 00:49:49 -070033#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034
35#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#include "common.h"
37#include "cutils/properties.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#include "install.h"
39#include "minui/minui.h"
40#include "minzip/DirUtil.h"
41#include "roots.h"
Doug Zongkerddd6a282009-06-09 12:22:33 -070042#include "recovery_ui.h"
Oscar Montemayor52219a62010-02-25 16:47:02 -080043#include "encryptedfs_provisioning.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080045#include "extendedcommands.h"
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080046
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047static const struct option OPTIONS[] = {
48 { "send_intent", required_argument, NULL, 's' },
49 { "update_package", required_argument, NULL, 'u' },
50 { "wipe_data", no_argument, NULL, 'w' },
51 { "wipe_cache", no_argument, NULL, 'c' },
Oscar Montemayor52219a62010-02-25 16:47:02 -080052 { "set_encrypted_filesystems", required_argument, NULL, 'e' },
Doug Zongker4bc98062010-09-03 11:00:13 -070053 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070054 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055};
56
Doug Zongkerd4208f92010-09-20 12:16:13 -070057static const char *COMMAND_FILE = "/cache/recovery/command";
58static const char *INTENT_FILE = "/cache/recovery/intent";
59static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070060static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070061static const char *SDCARD_ROOT = "/sdcard";
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -080062static int allow_display_toggle = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080063static const char *SDCARD_PACKAGE_FILE = "/sdcard/update.zip";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070065static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066
67/*
68 * The recovery tool communicates with the main system through /cache files.
69 * /cache/recovery/command - INPUT - command line for tool, one arg per line
70 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
71 * /cache/recovery/intent - OUTPUT - intent that was passed in
72 *
73 * The arguments which may be supplied in the recovery.command file:
74 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070075 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 * --wipe_data - erase user data (and cache), then reboot
77 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080078 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079 *
80 * After completing, we remove /cache/recovery/command and reboot.
81 * Arguments may also be supplied in the bootloader control block (BCB).
82 * These important scenarios must be safely restartable at any point:
83 *
84 * FACTORY RESET
85 * 1. user selects "factory reset"
86 * 2. main system writes "--wipe_data" to /cache/recovery/command
87 * 3. main system reboots into recovery
88 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
89 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070090 * 5. erase_volume() reformats /data
91 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092 * 7. finish_recovery() erases BCB
93 * -- after this, rebooting will restart the main system --
94 * 8. main() calls reboot() to boot main system
95 *
96 * OTA INSTALL
97 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -070098 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 * 3. main system reboots into recovery
100 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
101 * -- after this, rebooting will attempt to reinstall the update --
102 * 5. install_package() attempts to install the update
103 * NOTE: the package install must itself be restartable from any point
104 * 6. finish_recovery() erases BCB
105 * -- after this, rebooting will (try to) restart the main system --
106 * 7. ** if install failed **
107 * 7a. prompt_and_wait() shows an error icon and waits for the user
108 * 7b; the user reboots (pulling the battery, etc) into the main system
109 * 8. main() calls maybe_install_firmware_update()
110 * ** if the update contained radio/hboot firmware **:
111 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
112 * -- after this, rebooting will reformat cache & restart main system --
113 * 8b. m_i_f_u() writes firmware image into raw cache partition
114 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
115 * -- after this, rebooting will attempt to reinstall firmware --
116 * 8d. bootloader tries to flash firmware
117 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
118 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700119 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800120 * 8g. finish_recovery() erases BCB
121 * -- after this, rebooting will (try to) restart the main system --
122 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800123 *
Oscar Montemayor52219a62010-02-25 16:47:02 -0800124 * SECURE FILE SYSTEMS ENABLE/DISABLE
Oscar Montemayor05231562009-11-30 08:40:57 -0800125 * 1. user selects "enable encrypted file systems"
Oscar Montemayor52219a62010-02-25 16:47:02 -0800126 * 2. main system writes "--set_encrypted_filesystems=on|off" to
Oscar Montemayor05231562009-11-30 08:40:57 -0800127 * /cache/recovery/command
128 * 3. main system reboots into recovery
129 * 4. get_args() writes BCB with "boot-recovery" and
130 * "--set_encrypted_filesystems=on|off"
131 * -- after this, rebooting will restart the transition --
132 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
133 * Settings include: property to specify the Encrypted FS istatus and
134 * FS encryption key if enabled (not yet implemented)
Doug Zongkerd4208f92010-09-20 12:16:13 -0700135 * 6. erase_volume() reformats /data
136 * 7. erase_volume() reformats /cache
Oscar Montemayor05231562009-11-30 08:40:57 -0800137 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
138 * Settings include: property to specify the Encrypted FS status and
139 * FS encryption key if enabled (not yet implemented)
140 * 9. finish_recovery() erases BCB
141 * -- after this, rebooting will restart the main system --
142 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143 */
144
145static const int MAX_ARG_LENGTH = 4096;
146static const int MAX_ARGS = 100;
147
Doug Zongkerd4208f92010-09-20 12:16:13 -0700148// open a given path, mounting partitions as necessary
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149static FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700150fopen_path(const char *path, const char *mode) {
151 if (ensure_path_mounted(path) != 0) {
152 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153 return NULL;
154 }
155
156 // When writing, try to create the containing directory, if necessary.
157 // Use generous permissions, the system (init.rc) will reset them.
158 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
159
160 FILE *fp = fopen(path, mode);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800161 if (fp == NULL && path != COMMAND_FILE) LOGE("Can't open %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162 return fp;
163}
164
165// close a file, log an error if the error indicator is set
166static void
167check_and_fclose(FILE *fp, const char *name) {
168 fflush(fp);
169 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
170 fclose(fp);
171}
172
173// command line args come from, in decreasing precedence:
174// - the actual command line
175// - the bootloader control block (one per line, after "recovery")
176// - the contents of COMMAND_FILE (one per line)
177static void
178get_args(int *argc, char ***argv) {
179 struct bootloader_message boot;
180 memset(&boot, 0, sizeof(boot));
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700181#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700183#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800184
185 if (boot.command[0] != 0 && boot.command[0] != 255) {
186 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
187 }
188
189 if (boot.status[0] != 0 && boot.status[0] != 255) {
190 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
191 }
192
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700193 struct stat file_info;
194
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700196 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
198 const char *arg = strtok(boot.recovery, "\n");
199 if (arg != NULL && !strcmp(arg, "recovery")) {
200 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
201 (*argv)[0] = strdup(arg);
202 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
203 if ((arg = strtok(NULL, "\n")) == NULL) break;
204 (*argv)[*argc] = strdup(arg);
205 }
206 LOGI("Got arguments from boot message\n");
207 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
208 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
209 }
210 }
211
212 // --- if that doesn't work, try the command file
213 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700214 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 if (fp != NULL) {
216 char *argv0 = (*argv)[0];
217 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
218 (*argv)[0] = argv0; // use the same program name
219
220 char buf[MAX_ARG_LENGTH];
221 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
222 if (!fgets(buf, sizeof(buf), fp)) break;
223 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
224 }
225
226 check_and_fclose(fp, COMMAND_FILE);
227 LOGI("Got arguments from %s\n", COMMAND_FILE);
228 }
229 }
230
231 // --> write the arguments we have back into the bootloader control block
232 // always boot into recovery after this (until finish_recovery() is called)
233 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
234 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
235 int i;
236 for (i = 1; i < *argc; ++i) {
237 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
238 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
239 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700240#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700242#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243}
244
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700245#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik K. Duttae9234872010-02-12 00:43:24 -0800246void
Oscar Montemayor05231562009-11-30 08:40:57 -0800247set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700248 struct bootloader_message boot;
249 memset(&boot, 0, sizeof(boot));
250 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
251 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
252 set_bootloader_message(&boot);
253}
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700254#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255
Doug Zongker2c3539e2010-09-29 13:21:30 -0700256// How much of the temp log we have copied to the copy in cache.
257static long tmplog_offset = 0;
258
259static void
260copy_log_file(const char* destination, int append) {
261 FILE *log = fopen_path(destination, append ? "a" : "w");
262 if (log == NULL) {
263 LOGE("Can't open %s\n", destination);
264 } else {
265 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
266 if (tmplog == NULL) {
267 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
268 } else {
269 if (append) {
270 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
271 }
272 char buf[4096];
273 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
274 if (append) {
275 tmplog_offset = ftell(tmplog);
276 }
277 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
278 }
279 check_and_fclose(log, destination);
280 }
281}
282
283
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800284// clear the recovery command and prepare to boot a (hopefully working) system,
285// copy our log file to cache as well (for the system to read), and
286// record any intent we were asked to communicate back to the system.
287// this function is idempotent: call it as many times as you like.
288static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800289finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290 // By this point, we're ready to return to the main system...
291 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700292 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000293 if (fp == NULL) {
294 LOGE("Can't open %s\n", INTENT_FILE);
295 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296 fputs(send_intent, fp);
297 check_and_fclose(fp, INTENT_FILE);
298 }
299 }
300
301 // Copy logs to cache so the system can find out what happened.
Doug Zongker2c3539e2010-09-29 13:21:30 -0700302 copy_log_file(LOG_FILE, true);
303 copy_log_file(LAST_LOG_FILE, false);
304 chmod(LAST_LOG_FILE, 0640);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700306#ifndef BOARD_HAS_NO_MISC_PARTITION
Oscar Montemayor05231562009-11-30 08:40:57 -0800307 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 struct bootloader_message boot;
309 memset(&boot, 0, sizeof(boot));
310 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700311#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312
313 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700314 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
315 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800316 LOGW("Can't unlink %s\n", COMMAND_FILE);
317 }
318
319 sync(); // For good measure.
320}
321
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700323erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800324 ui_set_background(BACKGROUND_ICON_INSTALLING);
325 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700326 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700327
328 if (strcmp(volume, "/cache") == 0) {
329 // Any part of the log we'd copied to cache is now gone.
330 // Reset the pointer so we copy from the beginning of the temp
331 // log.
332 tmplog_offset = 0;
333 }
334
Doug Zongkerd4208f92010-09-20 12:16:13 -0700335 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800336}
337
Doug Zongker23ceeea2010-07-08 17:27:55 -0700338static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700339copy_sideloaded_package(const char* original_path) {
340 if (ensure_path_mounted(original_path) != 0) {
341 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700342 return NULL;
343 }
344
Doug Zongkerd4208f92010-09-20 12:16:13 -0700345 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700346 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
347 return NULL;
348 }
349
Doug Zongkerd4208f92010-09-20 12:16:13 -0700350 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700351 if (errno != EEXIST) {
352 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
353 return NULL;
354 }
355 }
356
Doug Zongkerd4208f92010-09-20 12:16:13 -0700357 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
358 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700359 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700360 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
361 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700362 return NULL;
363 }
364 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700365 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700366 return NULL;
367 }
368 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700369 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700370 return NULL;
371 }
372 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700373 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700374 return NULL;
375 }
376
Doug Zongkerd4208f92010-09-20 12:16:13 -0700377 char copy_path[PATH_MAX];
378 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700379 strcat(copy_path, "/package.zip");
380
381 char* buffer = malloc(BUFSIZ);
382 if (buffer == NULL) {
383 LOGE("Failed to allocate buffer\n");
384 return NULL;
385 }
386
387 size_t read;
388 FILE* fin = fopen(original_path, "rb");
389 if (fin == NULL) {
390 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
391 return NULL;
392 }
393 FILE* fout = fopen(copy_path, "wb");
394 if (fout == NULL) {
395 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
396 return NULL;
397 }
398
399 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
400 if (fwrite(buffer, 1, read, fout) != read) {
401 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
402 return NULL;
403 }
404 }
405
406 free(buffer);
407
408 if (fclose(fout) != 0) {
409 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
410 return NULL;
411 }
412
413 if (fclose(fin) != 0) {
414 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
415 return NULL;
416 }
417
418 // "adb push" is happy to overwrite read-only files when it's
419 // running as root, but we'll try anyway.
420 if (chmod(copy_path, 0400) != 0) {
421 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
422 return NULL;
423 }
424
Doug Zongkerd4208f92010-09-20 12:16:13 -0700425 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700426}
427
Doug Zongkerf93d8162009-09-22 15:16:02 -0700428static char**
429prepend_title(char** headers) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700430 char* title[] = { EXPAND(RECOVERY_VERSION),
Doug Zongkerd6837852009-06-17 22:07:13 -0700431 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800432 NULL };
433
Doug Zongkerd6837852009-06-17 22:07:13 -0700434 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700435 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700436 int count = 0;
437 char** p;
438 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700439 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700440
Doug Zongkerf93d8162009-09-22 15:16:02 -0700441 char** new_headers = malloc((count+1) * sizeof(char*));
442 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700443 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700444 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700445 *h = NULL;
446
Doug Zongkerf93d8162009-09-22 15:16:02 -0700447 return new_headers;
448}
449
Koushik K. Duttae9234872010-02-12 00:43:24 -0800450int
Doug Zongker8674a722010-09-15 11:08:23 -0700451get_menu_selection(char** headers, char** items, int menu_only,
452 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700453 // throw away keys pressed previously, so user doesn't
454 // accidentally trigger menu items.
455 ui_clear_key_queue();
456
Koushik Duttadf1e4062010-12-18 17:42:31 -0800457 int item_count = ui_start_menu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700458 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800459 int chosen_item = -1;
460
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700461 // Some users with dead enter keys need a way to turn on power to select.
462 // Jiggering across the wrapping menu is one "secret" way to enable it.
463 // We can't rely on /cache or /sdcard since they may not be available.
464 int wrap_count = 0;
465
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800466 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800467 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468 int visible = ui_text_visible();
469
Doug Zongkerddd6a282009-06-09 12:22:33 -0700470 int action = device_handle_key(key, visible);
471
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700472 int old_selected = selected;
473
Doug Zongkerddd6a282009-06-09 12:22:33 -0700474 if (action < 0) {
475 switch (action) {
476 case HIGHLIGHT_UP:
477 --selected;
478 selected = ui_menu_select(selected);
479 break;
480 case HIGHLIGHT_DOWN:
481 ++selected;
482 selected = ui_menu_select(selected);
483 break;
484 case SELECT_ITEM:
485 chosen_item = selected;
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700486 if (ui_get_showing_back_button()) {
487 if (chosen_item == item_count) {
488 chosen_item = GO_BACK;
489 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700490 }
Doug Zongkerddd6a282009-06-09 12:22:33 -0700491 break;
492 case NO_ACTION:
493 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800494 case GO_BACK:
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800495 chosen_item = GO_BACK;
496 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800497 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700498 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700499 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800500 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700501
502 if (abs(selected - old_selected) > 1) {
503 wrap_count++;
504 if (wrap_count == 3) {
505 wrap_count = 0;
506 if (ui_get_showing_back_button()) {
Koushik Dutta95654242010-07-14 21:01:21 -0700507 ui_print("Back menu button disabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700508 ui_set_showing_back_button(0);
509 }
510 else {
Koushik Dutta95654242010-07-14 21:01:21 -0700511 ui_print("Back menu button enabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700512 ui_set_showing_back_button(1);
513 }
514 }
515 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700516 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800517
Doug Zongkerf93d8162009-09-22 15:16:02 -0700518 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800519 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700520 return chosen_item;
521}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800522
Doug Zongker8674a722010-09-15 11:08:23 -0700523static int compare_string(const void* a, const void* b) {
524 return strcmp(*(const char**)a, *(const char**)b);
525}
526
527static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700528sdcard_directory(const char* path) {
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700529 ensure_path_mounted(SDCARD_ROOT);
530
Doug Zongker8674a722010-09-15 11:08:23 -0700531 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700532 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700533 "",
534 NULL };
535 DIR* d;
536 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700537 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700538 if (d == NULL) {
539 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700540 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700541 return 0;
542 }
543
544 char** headers = prepend_title(MENU_HEADERS);
545
546 int d_size = 0;
547 int d_alloc = 10;
548 char** dirs = malloc(d_alloc * sizeof(char*));
549 int z_size = 1;
550 int z_alloc = 10;
551 char** zips = malloc(z_alloc * sizeof(char*));
552 zips[0] = strdup("../");
553
554 while ((de = readdir(d)) != NULL) {
555 int name_len = strlen(de->d_name);
556
557 if (de->d_type == DT_DIR) {
558 // skip "." and ".." entries
559 if (name_len == 1 && de->d_name[0] == '.') continue;
560 if (name_len == 2 && de->d_name[0] == '.' &&
561 de->d_name[1] == '.') continue;
562
563 if (d_size >= d_alloc) {
564 d_alloc *= 2;
565 dirs = realloc(dirs, d_alloc * sizeof(char*));
566 }
567 dirs[d_size] = malloc(name_len + 2);
568 strcpy(dirs[d_size], de->d_name);
569 dirs[d_size][name_len] = '/';
570 dirs[d_size][name_len+1] = '\0';
571 ++d_size;
572 } else if (de->d_type == DT_REG &&
573 name_len >= 4 &&
574 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
575 if (z_size >= z_alloc) {
576 z_alloc *= 2;
577 zips = realloc(zips, z_alloc * sizeof(char*));
578 }
579 zips[z_size++] = strdup(de->d_name);
580 }
581 }
582 closedir(d);
583
584 qsort(dirs, d_size, sizeof(char*), compare_string);
585 qsort(zips, z_size, sizeof(char*), compare_string);
586
587 // append dirs to the zips list
588 if (d_size + z_size + 1 > z_alloc) {
589 z_alloc = d_size + z_size + 1;
590 zips = realloc(zips, z_alloc * sizeof(char*));
591 }
592 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
593 free(dirs);
594 z_size += d_size;
595 zips[z_size] = NULL;
596
597 int result;
598 int chosen_item = 0;
599 do {
600 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
601
602 char* item = zips[chosen_item];
603 int item_len = strlen(item);
604 if (chosen_item == 0) { // item 0 is always "../"
605 // go up but continue browsing (if the caller is sdcard_directory)
606 result = -1;
607 break;
608 } else if (item[item_len-1] == '/') {
609 // recurse down into a subdirectory
610 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700611 strlcpy(new_path, path, PATH_MAX);
612 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700613 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700614 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700615 result = sdcard_directory(new_path);
616 if (result >= 0) break;
617 } else {
618 // selected a zip file: attempt to install it, and return
619 // the status to the caller.
620 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700621 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700622 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700623 strlcat(new_path, item, PATH_MAX);
624
Doug Zongkerd4208f92010-09-20 12:16:13 -0700625 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700626 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700627 char* copy = copy_sideloaded_package(new_path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700628 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700629 if (copy) {
630 result = install_package(copy);
631 free(copy);
632 } else {
633 result = INSTALL_ERROR;
634 }
Doug Zongker8674a722010-09-15 11:08:23 -0700635 break;
636 }
637 } while (true);
638
639 int i;
640 for (i = 0; i < z_size; ++i) free(zips[i]);
641 free(zips);
642 free(headers);
643
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700644 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700645 return result;
646}
647
Doug Zongkerf93d8162009-09-22 15:16:02 -0700648static void
649wipe_data(int confirm) {
650 if (confirm) {
651 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700652
Doug Zongkerf93d8162009-09-22 15:16:02 -0700653 if (title_headers == NULL) {
654 char* headers[] = { "Confirm wipe of all user data?",
655 " THIS CAN NOT BE UNDONE.",
656 "",
657 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700658 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700659 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800660
Doug Zongkerf93d8162009-09-22 15:16:02 -0700661 char* items[] = { " No",
662 " No",
663 " No",
664 " No",
665 " No",
666 " No",
667 " No",
668 " Yes -- delete all user data", // [7]
669 " No",
670 " No",
671 " No",
672 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800673
Doug Zongker8674a722010-09-15 11:08:23 -0700674 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700675 if (chosen_item != 7) {
676 return;
677 }
678 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700679
Doug Zongkerf93d8162009-09-22 15:16:02 -0700680 ui_print("\n-- Wiping data...\n");
681 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700682 erase_volume("/data");
683 erase_volume("/cache");
Koushik Duttabf4444f2010-12-18 18:57:47 -0800684#ifdef BOARD_HAS_DATADATA
Koushik Duttadf1e4062010-12-18 17:42:31 -0800685 erase_volume("/datadata");
Koushik Duttabf4444f2010-12-18 18:57:47 -0800686#endif
Koushik Duttadf1e4062010-12-18 17:42:31 -0800687 erase_volume("/sd-ext");
688 erase_volume("/sdcard/.android_secure");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700689 ui_print("Data wipe complete.\n");
690}
691
692static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800693prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700694 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700695
696 for (;;) {
697 finish_recovery(NULL);
698 ui_reset_progress();
699
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800700 allow_display_toggle = 1;
Doug Zongker8674a722010-09-15 11:08:23 -0700701 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800702 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700703
704 // device-specific code may take some action here. It may
705 // return one of the core actions handled in the switch
706 // statement below.
707 chosen_item = device_perform_action(chosen_item);
708
709 switch (chosen_item) {
710 case ITEM_REBOOT:
711 return;
712
713 case ITEM_WIPE_DATA:
714 wipe_data(ui_text_visible());
715 if (!ui_text_visible()) return;
716 break;
717
718 case ITEM_WIPE_CACHE:
Koushik Dutta95654242010-07-14 21:01:21 -0700719 if (confirm_selection("Confirm wipe?", "Yes - Wipe Cache"))
720 {
721 ui_print("\n-- Wiping cache...\n");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800722 erase_volume("/cache");
Koushik Dutta95654242010-07-14 21:01:21 -0700723 ui_print("Cache wipe complete.\n");
724 if (!ui_text_visible()) return;
725 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700726 break;
727
728 case ITEM_APPLY_SDCARD:
Koushik Dutta95654242010-07-14 21:01:21 -0700729 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
730 {
731 ui_print("\n-- Install from sdcard...\n");
Koushik Dutta95654242010-07-14 21:01:21 -0700732 int status = install_package(SDCARD_PACKAGE_FILE);
Doug Zongker8674a722010-09-15 11:08:23 -0700733 if (status != INSTALL_SUCCESS) {
734 ui_set_background(BACKGROUND_ICON_ERROR);
735 ui_print("Installation aborted.\n");
736 } else if (!ui_text_visible()) {
737 return; // reboot if logs aren't visible
738 } else {
739 ui_print("\nInstall from sdcard complete.\n");
740 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700741 }
742 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800743 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800744 show_install_update_menu();
745 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700746 case ITEM_NANDROID:
747 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800748 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700749 case ITEM_PARTITION:
750 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700751 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700752 case ITEM_ADVANCED:
753 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800754 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800755 }
756 }
757}
758
759static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800760print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700761 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800762}
763
764int
Oscar Montemayor05231562009-11-30 08:40:57 -0800765main(int argc, char **argv) {
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800766 if (strstr(argv[0], "recovery") == NULL)
767 {
768 if (strstr(argv[0], "flash_image") != NULL)
769 return flash_image_main(argc, argv);
770 if (strstr(argv[0], "dump_image") != NULL)
771 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700772 if (strstr(argv[0], "erase_image") != NULL)
773 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800774 if (strstr(argv[0], "mkyaffs2image") != NULL)
775 return mkyaffs2image_main(argc, argv);
776 if (strstr(argv[0], "unyaffs") != NULL)
777 return unyaffs_main(argc, argv);
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700778 if (strstr(argv[0], "nandroid"))
779 return nandroid_main(argc, argv);
Koushik Dutta852bb422010-07-24 11:18:00 -0700780 if (strstr(argv[0], "reboot"))
781 return reboot_main(argc, argv);
782 if (strstr(argv[0], "setprop"))
783 return setprop_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800784 return busybox_driver(argc, argv);
785 }
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700786 __system("/sbin/postrecoveryboot.sh");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700787
Koushik K. Dutta03173782010-02-26 14:14:23 -0800788 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800789 time_t start = time(NULL);
790
791 // If these fail, there's not really anywhere to complain...
792 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
793 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700794 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800795
796 ui_init();
Koushik Duttafdda0d62010-07-01 12:52:34 -0700797 ui_print(EXPAND(RECOVERY_VERSION)"\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700798 load_volume_table();
Koushik Duttab643e4f2010-12-18 18:39:39 -0800799 create_fstab();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800800 get_args(&argc, &argv);
801
802 int previous_runs = 0;
803 const char *send_intent = NULL;
804 const char *update_package = NULL;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800805 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800806 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800807 int toggle_secure_fs = 0;
808 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800809
810 int arg;
811 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
812 switch (arg) {
813 case 'p': previous_runs = atoi(optarg); break;
814 case 's': send_intent = optarg; break;
815 case 'u': update_package = optarg; break;
816 case 'w': wipe_data = wipe_cache = 1; break;
817 case 'c': wipe_cache = 1; break;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800818 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700819 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800820 case '?':
821 LOGE("Invalid command argument\n");
822 continue;
823 }
824 }
825
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800826 device_recovery_start();
827
Doug Zongker56c51052010-07-01 09:18:44 -0700828 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800829 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700830 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800831 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700832 printf("\n");
833
834 if (update_package) {
835 // For backwards compatibility on the cache partition only, if
836 // we're given an old 'root' path "CACHE:foo", change it to
837 // "/cache/foo".
838 if (strncmp(update_package, "CACHE:", 6) == 0) {
839 int len = strlen(update_package) + 10;
840 char* modified_path = malloc(len);
841 strlcpy(modified_path, "/cache/", len);
842 strlcat(modified_path, update_package+6, len);
843 printf("(replacing path \"%s\" with \"%s\")\n",
844 update_package, modified_path);
845 update_package = modified_path;
846 }
847 }
848 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800849
850 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700851 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800852
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800853 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800854
Oscar Montemayor52219a62010-02-25 16:47:02 -0800855 if (toggle_secure_fs) {
856 if (strcmp(encrypted_fs_mode,"on") == 0) {
857 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800858 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800859 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
860 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800861 ui_print("Disabling Encrypted FS.\n");
862 } else {
863 ui_print("Error: invalid Encrypted FS setting.\n");
864 status = INSTALL_ERROR;
865 }
866
867 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
868 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor52219a62010-02-25 16:47:02 -0800869 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
870 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800871 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800872 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800873 }
874
875 if (status != INSTALL_ERROR) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700876 if (erase_volume("/data")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800877 ui_print("Data wipe failed.\n");
878 status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700879 } else if (erase_volume("/cache")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800880 ui_print("Cache wipe failed.\n");
881 status = INSTALL_ERROR;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800882 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
883 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800884 ui_print("Encrypted FS change aborted.\n");
885 status = INSTALL_ERROR;
886 } else {
887 ui_print("Successfully updated Encrypted FS.\n");
888 status = INSTALL_SUCCESS;
889 }
890 }
891 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800892 status = install_package(update_package);
893 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700894 } else if (wipe_data) {
895 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700896 if (erase_volume("/data")) status = INSTALL_ERROR;
897 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800898 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700899 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700900 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700901 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800902 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700903 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800904 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800905 // we are starting up in user initiated recovery here
906 // let's set up some default options
907 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800908 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800909 is_user_initiated_recovery = 1;
910 ui_set_show_text(1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800911
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800912 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700913 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700914 int ret;
915 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800916 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800917 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800918 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700919 else {
920 handle_failure(ret);
921 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700922 } else {
923 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800924 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800925 }
926
Koushik K. Dutta03173782010-02-26 14:14:23 -0800927 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700928 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700929 prompt_and_wait();
930 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800931
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800932 // Otherwise, get ready to boot the main system...
933 finish_recovery(send_intent);
934 ui_print("Rebooting...\n");
935 sync();
936 reboot(RB_AUTOBOOT);
937 return EXIT_SUCCESS;
938}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800939
940int get_allow_toggle_display() {
941 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500942}