blob: d0dee32a19678c1b6ba97b451d7b99cd97447f50 [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 Dutta9765a032010-12-18 21:31:02 -080046#include "flashutils/flashutils.h"
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080047
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048static const struct option OPTIONS[] = {
49 { "send_intent", required_argument, NULL, 's' },
50 { "update_package", required_argument, NULL, 'u' },
51 { "wipe_data", no_argument, NULL, 'w' },
52 { "wipe_cache", no_argument, NULL, 'c' },
Oscar Montemayor52219a62010-02-25 16:47:02 -080053 { "set_encrypted_filesystems", required_argument, NULL, 'e' },
Doug Zongker4bc98062010-09-03 11:00:13 -070054 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070055 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056};
57
Doug Zongkerd4208f92010-09-20 12:16:13 -070058static const char *COMMAND_FILE = "/cache/recovery/command";
59static const char *INTENT_FILE = "/cache/recovery/intent";
60static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070061static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070062static const char *SDCARD_ROOT = "/sdcard";
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -080063static int allow_display_toggle = 1;
KalimochoAzd646a6f2011-01-10 06:14:21 +010064static int poweroff = 0;
Koushik Duttadf1e4062010-12-18 17:42:31 -080065static const char *SDCARD_PACKAGE_FILE = "/sdcard/update.zip";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070067static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068
69/*
70 * The recovery tool communicates with the main system through /cache files.
71 * /cache/recovery/command - INPUT - command line for tool, one arg per line
72 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
73 * /cache/recovery/intent - OUTPUT - intent that was passed in
74 *
75 * The arguments which may be supplied in the recovery.command file:
76 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070077 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078 * --wipe_data - erase user data (and cache), then reboot
79 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080080 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 *
82 * After completing, we remove /cache/recovery/command and reboot.
83 * Arguments may also be supplied in the bootloader control block (BCB).
84 * These important scenarios must be safely restartable at any point:
85 *
86 * FACTORY RESET
87 * 1. user selects "factory reset"
88 * 2. main system writes "--wipe_data" to /cache/recovery/command
89 * 3. main system reboots into recovery
90 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
91 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070092 * 5. erase_volume() reformats /data
93 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094 * 7. finish_recovery() erases BCB
95 * -- after this, rebooting will restart the main system --
96 * 8. main() calls reboot() to boot main system
97 *
98 * OTA INSTALL
99 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700100 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 * 3. main system reboots into recovery
102 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
103 * -- after this, rebooting will attempt to reinstall the update --
104 * 5. install_package() attempts to install the update
105 * NOTE: the package install must itself be restartable from any point
106 * 6. finish_recovery() erases BCB
107 * -- after this, rebooting will (try to) restart the main system --
108 * 7. ** if install failed **
109 * 7a. prompt_and_wait() shows an error icon and waits for the user
110 * 7b; the user reboots (pulling the battery, etc) into the main system
111 * 8. main() calls maybe_install_firmware_update()
112 * ** if the update contained radio/hboot firmware **:
113 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
114 * -- after this, rebooting will reformat cache & restart main system --
115 * 8b. m_i_f_u() writes firmware image into raw cache partition
116 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
117 * -- after this, rebooting will attempt to reinstall firmware --
118 * 8d. bootloader tries to flash firmware
119 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
120 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700121 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122 * 8g. finish_recovery() erases BCB
123 * -- after this, rebooting will (try to) restart the main system --
124 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800125 *
Oscar Montemayor52219a62010-02-25 16:47:02 -0800126 * SECURE FILE SYSTEMS ENABLE/DISABLE
Oscar Montemayor05231562009-11-30 08:40:57 -0800127 * 1. user selects "enable encrypted file systems"
Oscar Montemayor52219a62010-02-25 16:47:02 -0800128 * 2. main system writes "--set_encrypted_filesystems=on|off" to
Oscar Montemayor05231562009-11-30 08:40:57 -0800129 * /cache/recovery/command
130 * 3. main system reboots into recovery
131 * 4. get_args() writes BCB with "boot-recovery" and
132 * "--set_encrypted_filesystems=on|off"
133 * -- after this, rebooting will restart the transition --
134 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
135 * Settings include: property to specify the Encrypted FS istatus and
136 * FS encryption key if enabled (not yet implemented)
Doug Zongkerd4208f92010-09-20 12:16:13 -0700137 * 6. erase_volume() reformats /data
138 * 7. erase_volume() reformats /cache
Oscar Montemayor05231562009-11-30 08:40:57 -0800139 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
140 * Settings include: property to specify the Encrypted FS status and
141 * FS encryption key if enabled (not yet implemented)
142 * 9. finish_recovery() erases BCB
143 * -- after this, rebooting will restart the main system --
144 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145 */
146
147static const int MAX_ARG_LENGTH = 4096;
148static const int MAX_ARGS = 100;
149
Doug Zongkerd4208f92010-09-20 12:16:13 -0700150// open a given path, mounting partitions as necessary
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151static FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700152fopen_path(const char *path, const char *mode) {
153 if (ensure_path_mounted(path) != 0) {
154 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 return NULL;
156 }
157
158 // When writing, try to create the containing directory, if necessary.
159 // Use generous permissions, the system (init.rc) will reset them.
160 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
161
162 FILE *fp = fopen(path, mode);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800163 if (fp == NULL && path != COMMAND_FILE) LOGE("Can't open %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164 return fp;
165}
166
167// close a file, log an error if the error indicator is set
168static void
169check_and_fclose(FILE *fp, const char *name) {
170 fflush(fp);
171 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
172 fclose(fp);
173}
174
175// command line args come from, in decreasing precedence:
176// - the actual command line
177// - the bootloader control block (one per line, after "recovery")
178// - the contents of COMMAND_FILE (one per line)
179static void
180get_args(int *argc, char ***argv) {
181 struct bootloader_message boot;
182 memset(&boot, 0, sizeof(boot));
Koushik Dutta9765a032010-12-18 21:31:02 -0800183 if (device_flash_type() == MTD) {
184 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
185 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186
187 if (boot.command[0] != 0 && boot.command[0] != 255) {
188 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
189 }
190
191 if (boot.status[0] != 0 && boot.status[0] != 255) {
192 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
193 }
194
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700195 struct stat file_info;
196
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700198 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
200 const char *arg = strtok(boot.recovery, "\n");
201 if (arg != NULL && !strcmp(arg, "recovery")) {
202 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
203 (*argv)[0] = strdup(arg);
204 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
205 if ((arg = strtok(NULL, "\n")) == NULL) break;
206 (*argv)[*argc] = strdup(arg);
207 }
208 LOGI("Got arguments from boot message\n");
209 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
210 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
211 }
212 }
213
214 // --- if that doesn't work, try the command file
215 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700216 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 if (fp != NULL) {
218 char *argv0 = (*argv)[0];
219 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
220 (*argv)[0] = argv0; // use the same program name
221
222 char buf[MAX_ARG_LENGTH];
223 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
224 if (!fgets(buf, sizeof(buf), fp)) break;
225 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
226 }
227
228 check_and_fclose(fp, COMMAND_FILE);
229 LOGI("Got arguments from %s\n", COMMAND_FILE);
230 }
231 }
232
233 // --> write the arguments we have back into the bootloader control block
234 // always boot into recovery after this (until finish_recovery() is called)
235 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
236 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
237 int i;
238 for (i = 1; i < *argc; ++i) {
239 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
240 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
241 }
Koushik Dutta9765a032010-12-18 21:31:02 -0800242 if (device_flash_type() == MTD) {
243 set_bootloader_message(&boot);
244 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245}
246
Koushik K. Duttae9234872010-02-12 00:43:24 -0800247void
Oscar Montemayor05231562009-11-30 08:40:57 -0800248set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700249 struct bootloader_message boot;
250 memset(&boot, 0, sizeof(boot));
251 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
252 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
253 set_bootloader_message(&boot);
254}
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 Dutta9765a032010-12-18 21:31:02 -0800306 if (device_flash_type() == MTD) {
307 // Reset to mormal system boot so recovery won't cycle indefinitely.
308 struct bootloader_message boot;
309 memset(&boot, 0, sizeof(boot));
310 set_bootloader_message(&boot);
311 }
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 Dutta5460f0c2010-12-18 22:37:49 -0800684 if (has_datadata()) {
685 erase_volume("/datadata");
686 }
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:
KalimochoAzd646a6f2011-01-10 06:14:21 +0100711 poweroff=0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700712 return;
713
714 case ITEM_WIPE_DATA:
715 wipe_data(ui_text_visible());
716 if (!ui_text_visible()) return;
717 break;
718
719 case ITEM_WIPE_CACHE:
Koushik Dutta95654242010-07-14 21:01:21 -0700720 if (confirm_selection("Confirm wipe?", "Yes - Wipe Cache"))
721 {
722 ui_print("\n-- Wiping cache...\n");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800723 erase_volume("/cache");
Koushik Dutta95654242010-07-14 21:01:21 -0700724 ui_print("Cache wipe complete.\n");
725 if (!ui_text_visible()) return;
726 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700727 break;
728
729 case ITEM_APPLY_SDCARD:
Koushik Dutta95654242010-07-14 21:01:21 -0700730 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
731 {
732 ui_print("\n-- Install from sdcard...\n");
Koushik Dutta95654242010-07-14 21:01:21 -0700733 int status = install_package(SDCARD_PACKAGE_FILE);
Doug Zongker8674a722010-09-15 11:08:23 -0700734 if (status != INSTALL_SUCCESS) {
735 ui_set_background(BACKGROUND_ICON_ERROR);
736 ui_print("Installation aborted.\n");
737 } else if (!ui_text_visible()) {
738 return; // reboot if logs aren't visible
739 } else {
740 ui_print("\nInstall from sdcard complete.\n");
741 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700742 }
743 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800744 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800745 show_install_update_menu();
746 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700747 case ITEM_NANDROID:
748 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800749 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700750 case ITEM_PARTITION:
751 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700752 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700753 case ITEM_ADVANCED:
754 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800755 break;
KalimochoAzd646a6f2011-01-10 06:14:21 +0100756 case ITEM_POWEROFF:
757 poweroff=1;
758 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800759 }
760 }
761}
762
763static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800764print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700765 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800766}
767
768int
Oscar Montemayor05231562009-11-30 08:40:57 -0800769main(int argc, char **argv) {
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800770 if (strstr(argv[0], "recovery") == NULL)
771 {
772 if (strstr(argv[0], "flash_image") != NULL)
773 return flash_image_main(argc, argv);
Koushik Duttad8e21c32011-01-04 10:46:23 -0800774 if (strstr(argv[0], "volume") != NULL)
775 return volume_main(argc, argv);
Koushik Duttabec09952010-12-19 20:37:57 -0800776 if (strstr(argv[0], "edify") != NULL)
777 return edify_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800778 if (strstr(argv[0], "dump_image") != NULL)
779 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700780 if (strstr(argv[0], "erase_image") != NULL)
781 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800782 if (strstr(argv[0], "mkyaffs2image") != NULL)
783 return mkyaffs2image_main(argc, argv);
784 if (strstr(argv[0], "unyaffs") != NULL)
785 return unyaffs_main(argc, argv);
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700786 if (strstr(argv[0], "nandroid"))
787 return nandroid_main(argc, argv);
Koushik Dutta852bb422010-07-24 11:18:00 -0700788 if (strstr(argv[0], "reboot"))
789 return reboot_main(argc, argv);
Kolja Dummann6afbcdc2011-07-24 21:40:16 +0200790#ifdef BOARD_RECOVERY_HANDLES_MOUNT
791 if (strstr(argv[0], "mount") && argc == 2 && !strstr(argv[0], "umount"))
792 {
793 load_volume_table();
794 return ensure_path_mounted(argv[1]);
795 }
796#endif
KalimochoAzd646a6f2011-01-10 06:14:21 +0100797 if (strstr(argv[0], "poweroff")){
798 return reboot_main(argc, argv);
799 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700800 if (strstr(argv[0], "setprop"))
801 return setprop_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800802 return busybox_driver(argc, argv);
803 }
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700804 __system("/sbin/postrecoveryboot.sh");
Koushik Dutta4d8c0332011-06-09 14:19:04 -0700805
Koushik K. Dutta03173782010-02-26 14:14:23 -0800806 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800807 time_t start = time(NULL);
808
809 // If these fail, there's not really anywhere to complain...
810 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
811 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700812 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800813
814 ui_init();
Koushik Duttafdda0d62010-07-01 12:52:34 -0700815 ui_print(EXPAND(RECOVERY_VERSION)"\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700816 load_volume_table();
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800817 process_volumes();
Koushik Dutta9765a032010-12-18 21:31:02 -0800818 LOGI("Processing arguments.\n");
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;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800824 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800825 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800826 int toggle_secure_fs = 0;
827 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800828
Koushik Dutta9765a032010-12-18 21:31:02 -0800829 LOGI("Checking arguments.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800830 int arg;
831 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
832 switch (arg) {
833 case 'p': previous_runs = atoi(optarg); break;
834 case 's': send_intent = optarg; break;
835 case 'u': update_package = optarg; break;
Koushik Dutta94a4bab2011-07-13 10:34:23 -0700836 case 'w':
Ricardo Cerqueira0b06fc02011-07-07 01:09:52 +0100837#ifndef BOARD_RECOVERY_ALWAYS_WIPES
838 wipe_data = wipe_cache = 1;
839#endif
840 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800841 case 'c': wipe_cache = 1; break;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800842 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700843 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800844 case '?':
845 LOGE("Invalid command argument\n");
846 continue;
847 }
848 }
849
Koushik Dutta9765a032010-12-18 21:31:02 -0800850 LOGI("device_recovery_start()\n");
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800851 device_recovery_start();
852
Doug Zongker56c51052010-07-01 09:18:44 -0700853 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800854 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700855 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800856 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700857 printf("\n");
858
859 if (update_package) {
860 // For backwards compatibility on the cache partition only, if
861 // we're given an old 'root' path "CACHE:foo", change it to
862 // "/cache/foo".
863 if (strncmp(update_package, "CACHE:", 6) == 0) {
864 int len = strlen(update_package) + 10;
865 char* modified_path = malloc(len);
866 strlcpy(modified_path, "/cache/", len);
867 strlcat(modified_path, update_package+6, len);
868 printf("(replacing path \"%s\" with \"%s\")\n",
869 update_package, modified_path);
870 update_package = modified_path;
871 }
872 }
873 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800874
875 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700876 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800877
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800878 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800879
Oscar Montemayor52219a62010-02-25 16:47:02 -0800880 if (toggle_secure_fs) {
881 if (strcmp(encrypted_fs_mode,"on") == 0) {
882 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800883 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800884 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
885 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800886 ui_print("Disabling Encrypted FS.\n");
887 } else {
888 ui_print("Error: invalid Encrypted FS setting.\n");
889 status = INSTALL_ERROR;
890 }
891
892 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
893 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor52219a62010-02-25 16:47:02 -0800894 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
895 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800896 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800897 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800898 }
899
900 if (status != INSTALL_ERROR) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700901 if (erase_volume("/data")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800902 ui_print("Data wipe failed.\n");
903 status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700904 } else if (erase_volume("/cache")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800905 ui_print("Cache wipe failed.\n");
906 status = INSTALL_ERROR;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800907 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
908 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800909 ui_print("Encrypted FS change aborted.\n");
910 status = INSTALL_ERROR;
911 } else {
912 ui_print("Successfully updated Encrypted FS.\n");
913 status = INSTALL_SUCCESS;
914 }
915 }
916 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800917 status = install_package(update_package);
918 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700919 } else if (wipe_data) {
920 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700921 if (erase_volume("/data")) status = INSTALL_ERROR;
922 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800923 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700924 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700925 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700926 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800927 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700928 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800929 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800930 // we are starting up in user initiated recovery here
931 // let's set up some default options
932 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800933 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800934 is_user_initiated_recovery = 1;
935 ui_set_show_text(1);
Koushik Dutta5d808172010-12-18 22:29:27 -0800936 ui_set_background(BACKGROUND_ICON_CLOCKWORK);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800937
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800938 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700939 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700940 int ret;
941 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800942 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800943 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800944 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700945 else {
946 handle_failure(ret);
947 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700948 } else {
949 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800950 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800951 }
952
Koushik K. Dutta03173782010-02-26 14:14:23 -0800953 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700954 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700955 prompt_and_wait();
956 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800957
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800958 // Otherwise, get ready to boot the main system...
959 finish_recovery(send_intent);
KalimochoAzd646a6f2011-01-10 06:14:21 +0100960 if(!poweroff)
961 ui_print("Rebooting...\n");
962 else
963 ui_print("Shutting down...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800964 sync();
KalimochoAzd646a6f2011-01-10 06:14:21 +0100965 reboot((!poweroff) ? RB_AUTOBOOT : RB_POWER_OFF);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800966 return EXIT_SUCCESS;
967}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800968
969int get_allow_toggle_display() {
970 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500971}