blob: d2c726acc9149b2601260ec7595861faeeafb56c [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <getopt.h>
21#include <limits.h>
22#include <linux/input.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/reboot.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
Doug Zongkerbe598882010-04-08 14:36:55 -070030#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
32#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "common.h"
34#include "cutils/properties.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "install.h"
36#include "minui/minui.h"
37#include "minzip/DirUtil.h"
38#include "roots.h"
Doug Zongkerddd6a282009-06-09 12:22:33 -070039#include "recovery_ui.h"
Oscar Montemayor31f6ee82010-02-25 16:47:02 -080040#include "encryptedfs_provisioning.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
42static const struct option OPTIONS[] = {
43 { "send_intent", required_argument, NULL, 's' },
44 { "update_package", required_argument, NULL, 'u' },
45 { "wipe_data", no_argument, NULL, 'w' },
46 { "wipe_cache", no_argument, NULL, 'c' },
Oscar Montemayor31f6ee82010-02-25 16:47:02 -080047 { "set_encrypted_filesystems", required_argument, NULL, 'e' },
Doug Zongker988500b2009-10-06 14:41:38 -070048 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049};
50
51static const char *COMMAND_FILE = "CACHE:recovery/command";
52static const char *INTENT_FILE = "CACHE:recovery/intent";
53static const char *LOG_FILE = "CACHE:recovery/log";
Doug Zongkerbe598882010-04-08 14:36:55 -070054static const char *SDCARD_ROOT = "SDCARD:";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
56
57/*
58 * The recovery tool communicates with the main system through /cache files.
59 * /cache/recovery/command - INPUT - command line for tool, one arg per line
60 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
61 * /cache/recovery/intent - OUTPUT - intent that was passed in
62 *
63 * The arguments which may be supplied in the recovery.command file:
64 * --send_intent=anystring - write the text out to recovery.intent
65 * --update_package=root:path - verify install an OTA package file
66 * --wipe_data - erase user data (and cache), then reboot
67 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080068 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 *
70 * After completing, we remove /cache/recovery/command and reboot.
71 * Arguments may also be supplied in the bootloader control block (BCB).
72 * These important scenarios must be safely restartable at any point:
73 *
74 * FACTORY RESET
75 * 1. user selects "factory reset"
76 * 2. main system writes "--wipe_data" to /cache/recovery/command
77 * 3. main system reboots into recovery
78 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
79 * -- after this, rebooting will restart the erase --
80 * 5. erase_root() reformats /data
81 * 6. erase_root() reformats /cache
82 * 7. finish_recovery() erases BCB
83 * -- after this, rebooting will restart the main system --
84 * 8. main() calls reboot() to boot main system
85 *
86 * OTA INSTALL
87 * 1. main system downloads OTA package to /cache/some-filename.zip
88 * 2. main system writes "--update_package=CACHE:some-filename.zip"
89 * 3. main system reboots into recovery
90 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
91 * -- after this, rebooting will attempt to reinstall the update --
92 * 5. install_package() attempts to install the update
93 * NOTE: the package install must itself be restartable from any point
94 * 6. finish_recovery() erases BCB
95 * -- after this, rebooting will (try to) restart the main system --
96 * 7. ** if install failed **
97 * 7a. prompt_and_wait() shows an error icon and waits for the user
98 * 7b; the user reboots (pulling the battery, etc) into the main system
99 * 8. main() calls maybe_install_firmware_update()
100 * ** if the update contained radio/hboot firmware **:
101 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
102 * -- after this, rebooting will reformat cache & restart main system --
103 * 8b. m_i_f_u() writes firmware image into raw cache partition
104 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
105 * -- after this, rebooting will attempt to reinstall firmware --
106 * 8d. bootloader tries to flash firmware
107 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
108 * -- after this, rebooting will reformat cache & restart main system --
109 * 8f. erase_root() reformats /cache
110 * 8g. finish_recovery() erases BCB
111 * -- after this, rebooting will (try to) restart the main system --
112 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800113 *
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800114 * SECURE FILE SYSTEMS ENABLE/DISABLE
Oscar Montemayor05231562009-11-30 08:40:57 -0800115 * 1. user selects "enable encrypted file systems"
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800116 * 2. main system writes "--set_encrypted_filesystems=on|off" to
Oscar Montemayor05231562009-11-30 08:40:57 -0800117 * /cache/recovery/command
118 * 3. main system reboots into recovery
119 * 4. get_args() writes BCB with "boot-recovery" and
120 * "--set_encrypted_filesystems=on|off"
121 * -- after this, rebooting will restart the transition --
122 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
123 * Settings include: property to specify the Encrypted FS istatus and
124 * FS encryption key if enabled (not yet implemented)
125 * 6. erase_root() reformats /data
126 * 7. erase_root() reformats /cache
127 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
128 * Settings include: property to specify the Encrypted FS status and
129 * FS encryption key if enabled (not yet implemented)
130 * 9. finish_recovery() erases BCB
131 * -- after this, rebooting will restart the main system --
132 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 */
134
135static const int MAX_ARG_LENGTH = 4096;
136static const int MAX_ARGS = 100;
137
138// open a file given in root:path format, mounting partitions as necessary
139static FILE*
140fopen_root_path(const char *root_path, const char *mode) {
141 if (ensure_root_path_mounted(root_path) != 0) {
142 LOGE("Can't mount %s\n", root_path);
143 return NULL;
144 }
145
146 char path[PATH_MAX] = "";
147 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
148 LOGE("Bad path %s\n", root_path);
149 return NULL;
150 }
151
152 // When writing, try to create the containing directory, if necessary.
153 // Use generous permissions, the system (init.rc) will reset them.
154 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
155
156 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157 return fp;
158}
159
160// close a file, log an error if the error indicator is set
161static void
162check_and_fclose(FILE *fp, const char *name) {
163 fflush(fp);
164 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
165 fclose(fp);
166}
167
168// command line args come from, in decreasing precedence:
169// - the actual command line
170// - the bootloader control block (one per line, after "recovery")
171// - the contents of COMMAND_FILE (one per line)
172static void
173get_args(int *argc, char ***argv) {
174 struct bootloader_message boot;
175 memset(&boot, 0, sizeof(boot));
176 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
177
178 if (boot.command[0] != 0 && boot.command[0] != 255) {
179 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
180 }
181
182 if (boot.status[0] != 0 && boot.status[0] != 255) {
183 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
184 }
185
186 // --- if arguments weren't supplied, look in the bootloader control block
187 if (*argc <= 1) {
188 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
189 const char *arg = strtok(boot.recovery, "\n");
190 if (arg != NULL && !strcmp(arg, "recovery")) {
191 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
192 (*argv)[0] = strdup(arg);
193 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
194 if ((arg = strtok(NULL, "\n")) == NULL) break;
195 (*argv)[*argc] = strdup(arg);
196 }
197 LOGI("Got arguments from boot message\n");
198 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
199 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
200 }
201 }
202
203 // --- if that doesn't work, try the command file
204 if (*argc <= 1) {
205 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
206 if (fp != NULL) {
207 char *argv0 = (*argv)[0];
208 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
209 (*argv)[0] = argv0; // use the same program name
210
211 char buf[MAX_ARG_LENGTH];
212 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
213 if (!fgets(buf, sizeof(buf), fp)) break;
214 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
215 }
216
217 check_and_fclose(fp, COMMAND_FILE);
218 LOGI("Got arguments from %s\n", COMMAND_FILE);
219 }
220 }
221
222 // --> write the arguments we have back into the bootloader control block
223 // always boot into recovery after this (until finish_recovery() is called)
224 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
225 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
226 int i;
227 for (i = 1; i < *argc; ++i) {
228 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
229 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
230 }
231 set_bootloader_message(&boot);
232}
233
Doug Zongker34c98df2009-08-18 12:05:45 -0700234static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800235set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700236 struct bootloader_message boot;
237 memset(&boot, 0, sizeof(boot));
238 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
239 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
240 set_bootloader_message(&boot);
241}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800242
243// clear the recovery command and prepare to boot a (hopefully working) system,
244// copy our log file to cache as well (for the system to read), and
245// record any intent we were asked to communicate back to the system.
246// this function is idempotent: call it as many times as you like.
247static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800248finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800249 // By this point, we're ready to return to the main system...
250 if (send_intent != NULL) {
251 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000252 if (fp == NULL) {
253 LOGE("Can't open %s\n", INTENT_FILE);
254 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 fputs(send_intent, fp);
256 check_and_fclose(fp, INTENT_FILE);
257 }
258 }
259
260 // Copy logs to cache so the system can find out what happened.
261 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000262 if (log == NULL) {
263 LOGE("Can't open %s\n", LOG_FILE);
264 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
266 if (tmplog == NULL) {
267 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
268 } else {
269 static long tmplog_offset = 0;
270 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
271 char buf[4096];
272 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
273 tmplog_offset = ftell(tmplog);
274 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
275 }
276 check_and_fclose(log, LOG_FILE);
277 }
278
Oscar Montemayor05231562009-11-30 08:40:57 -0800279 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800280 struct bootloader_message boot;
281 memset(&boot, 0, sizeof(boot));
282 set_bootloader_message(&boot);
283
284 // Remove the command file, so recovery won't repeat indefinitely.
285 char path[PATH_MAX] = "";
286 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
287 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
288 (unlink(path) && errno != ENOENT)) {
289 LOGW("Can't unlink %s\n", COMMAND_FILE);
290 }
291
292 sync(); // For good measure.
293}
294
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295static int
Oscar Montemayor05231562009-11-30 08:40:57 -0800296erase_root(const char *root) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297 ui_set_background(BACKGROUND_ICON_INSTALLING);
298 ui_show_indeterminate_progress();
299 ui_print("Formatting %s...\n", root);
300 return format_root_device(root);
301}
302
Doug Zongkerf93d8162009-09-22 15:16:02 -0700303static char**
Doug Zongkerbe598882010-04-08 14:36:55 -0700304prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700305 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700306 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700307 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 NULL };
309
Doug Zongkerd6837852009-06-17 22:07:13 -0700310 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700311 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700312 int count = 0;
313 char** p;
314 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700315 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700316
Doug Zongkerf93d8162009-09-22 15:16:02 -0700317 char** new_headers = malloc((count+1) * sizeof(char*));
318 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700319 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700320 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700321 *h = NULL;
322
Doug Zongkerf93d8162009-09-22 15:16:02 -0700323 return new_headers;
324}
325
326static int
Doug Zongkerbe598882010-04-08 14:36:55 -0700327get_menu_selection(char** headers, char** items, int menu_only,
328 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700329 // throw away keys pressed previously, so user doesn't
330 // accidentally trigger menu items.
331 ui_clear_key_queue();
332
Doug Zongkerbe598882010-04-08 14:36:55 -0700333 ui_start_menu(headers, items, initial_selection);
334 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335 int chosen_item = -1;
336
Doug Zongkerf93d8162009-09-22 15:16:02 -0700337 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800339 int visible = ui_text_visible();
340
Doug Zongkerddd6a282009-06-09 12:22:33 -0700341 int action = device_handle_key(key, visible);
342
343 if (action < 0) {
344 switch (action) {
345 case HIGHLIGHT_UP:
346 --selected;
347 selected = ui_menu_select(selected);
348 break;
349 case HIGHLIGHT_DOWN:
350 ++selected;
351 selected = ui_menu_select(selected);
352 break;
353 case SELECT_ITEM:
354 chosen_item = selected;
355 break;
356 case NO_ACTION:
357 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700359 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700360 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800361 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700362 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800363
Doug Zongkerf93d8162009-09-22 15:16:02 -0700364 ui_end_menu();
365 return chosen_item;
366}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800367
Doug Zongkerbe598882010-04-08 14:36:55 -0700368static int compare_string(const void* a, const void* b) {
369 return strcmp(*(const char**)a, *(const char**)b);
370}
371
372static int
373sdcard_directory(const char* root_path) {
374 const char* MENU_HEADERS[] = { "Choose a package to install:",
375 root_path,
376 "",
377 NULL };
378 DIR* d;
379 struct dirent* de;
380 char path[PATH_MAX];
381 d = opendir(translate_root_path(root_path, path, sizeof(path)));
382 if (d == NULL) {
383 LOGE("error opening %s: %s\n", path, strerror(errno));
384 return 0;
385 }
386
387 char** headers = prepend_title(MENU_HEADERS);
388
389 int d_size = 0;
390 int d_alloc = 10;
391 char** dirs = malloc(d_alloc * sizeof(char*));
392 int z_size = 1;
393 int z_alloc = 10;
394 char** zips = malloc(z_alloc * sizeof(char*));
395 zips[0] = strdup("../");
396
397 while ((de = readdir(d)) != NULL) {
398 int name_len = strlen(de->d_name);
399
400 if (de->d_type == DT_DIR) {
401 // skip "." and ".." entries
402 if (name_len == 1 && de->d_name[0] == '.') continue;
403 if (name_len == 2 && de->d_name[0] == '.' &&
404 de->d_name[1] == '.') continue;
405
406 if (d_size >= d_alloc) {
407 d_alloc *= 2;
408 dirs = realloc(dirs, d_alloc * sizeof(char*));
409 }
410 dirs[d_size] = malloc(name_len + 2);
411 strcpy(dirs[d_size], de->d_name);
412 dirs[d_size][name_len] = '/';
413 dirs[d_size][name_len+1] = '\0';
414 ++d_size;
415 } else if (de->d_type == DT_REG &&
416 name_len >= 4 &&
417 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
418 if (z_size >= z_alloc) {
419 z_alloc *= 2;
420 zips = realloc(zips, z_alloc * sizeof(char*));
421 }
422 zips[z_size++] = strdup(de->d_name);
423 }
424 }
425 closedir(d);
426
427 qsort(dirs, d_size, sizeof(char*), compare_string);
428 qsort(zips, z_size, sizeof(char*), compare_string);
429
430 // append dirs to the zips list
431 if (d_size + z_size + 1 > z_alloc) {
432 z_alloc = d_size + z_size + 1;
433 zips = realloc(zips, z_alloc * sizeof(char*));
434 }
435 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
436 free(dirs);
437 z_size += d_size;
438 zips[z_size] = NULL;
439
440 int result;
441 int chosen_item = 0;
442 do {
443 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
444
445 char* item = zips[chosen_item];
446 int item_len = strlen(item);
447 if (chosen_item == 0) { // item 0 is always "../"
448 // go up but continue browsing (if the caller is sdcard_directory)
449 result = -1;
450 break;
451 } else if (item[item_len-1] == '/') {
452 // recurse down into a subdirectory
453 char new_path[PATH_MAX];
454 strlcpy(new_path, root_path, PATH_MAX);
455 strlcat(new_path, item, PATH_MAX);
456 result = sdcard_directory(new_path);
457 if (result >= 0) break;
458 } else {
459 // selected a zip file: attempt to install it, and return
460 // the status to the caller.
461 char new_path[PATH_MAX];
462 strlcpy(new_path, root_path, PATH_MAX);
463 strlcat(new_path, item, PATH_MAX);
464
465 ui_print("\n-- Install %s ...\n", new_path);
466 set_sdcard_update_bootloader_message();
467 result = install_package(new_path);
468 break;
469 }
470 } while (true);
471
472 int i;
473 for (i = 0; i < z_size; ++i) free(zips[i]);
474 free(zips);
475 free(headers);
476
477 return result;
478}
479
Doug Zongkerf93d8162009-09-22 15:16:02 -0700480static void
481wipe_data(int confirm) {
482 if (confirm) {
483 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700484
Doug Zongkerf93d8162009-09-22 15:16:02 -0700485 if (title_headers == NULL) {
486 char* headers[] = { "Confirm wipe of all user data?",
487 " THIS CAN NOT BE UNDONE.",
488 "",
489 NULL };
Doug Zongkerbe598882010-04-08 14:36:55 -0700490 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700491 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800492
Doug Zongkerf93d8162009-09-22 15:16:02 -0700493 char* items[] = { " No",
494 " No",
495 " No",
496 " No",
497 " No",
498 " No",
499 " No",
500 " Yes -- delete all user data", // [7]
501 " No",
502 " No",
503 " No",
504 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800505
Doug Zongkerbe598882010-04-08 14:36:55 -0700506 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700507 if (chosen_item != 7) {
508 return;
509 }
510 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700511
Doug Zongkerf93d8162009-09-22 15:16:02 -0700512 ui_print("\n-- Wiping data...\n");
513 device_wipe_data();
514 erase_root("DATA:");
515 erase_root("CACHE:");
516 ui_print("Data wipe complete.\n");
517}
518
519static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800520prompt_and_wait() {
Doug Zongkerbe598882010-04-08 14:36:55 -0700521 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700522
523 for (;;) {
524 finish_recovery(NULL);
525 ui_reset_progress();
526
Doug Zongkerbe598882010-04-08 14:36:55 -0700527 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700528
529 // device-specific code may take some action here. It may
530 // return one of the core actions handled in the switch
531 // statement below.
532 chosen_item = device_perform_action(chosen_item);
533
534 switch (chosen_item) {
535 case ITEM_REBOOT:
536 return;
537
538 case ITEM_WIPE_DATA:
539 wipe_data(ui_text_visible());
540 if (!ui_text_visible()) return;
541 break;
542
543 case ITEM_WIPE_CACHE:
544 ui_print("\n-- Wiping cache...\n");
545 erase_root("CACHE:");
546 ui_print("Cache wipe complete.\n");
547 if (!ui_text_visible()) return;
548 break;
549
550 case ITEM_APPLY_SDCARD:
Doug Zongkerbe598882010-04-08 14:36:55 -0700551 ;
552 int status = sdcard_directory(SDCARD_ROOT);
553 if (status >= 0) {
554 if (status != INSTALL_SUCCESS) {
555 ui_set_background(BACKGROUND_ICON_ERROR);
556 ui_print("Installation aborted.\n");
557 } else if (!ui_text_visible()) {
558 return; // reboot if logs aren't visible
559 } else {
560 ui_print("\nInstall from sdcard complete.\n");
561 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700562 }
563 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800564 }
565 }
566}
567
568static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800569print_property(const char *key, const char *name, void *cookie) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800570 fprintf(stderr, "%s=%s\n", key, name);
571}
572
573int
Oscar Montemayor05231562009-11-30 08:40:57 -0800574main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800575 time_t start = time(NULL);
576
577 // If these fail, there's not really anywhere to complain...
578 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
579 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
580 fprintf(stderr, "Starting recovery on %s", ctime(&start));
581
582 ui_init();
583 get_args(&argc, &argv);
584
585 int previous_runs = 0;
586 const char *send_intent = NULL;
587 const char *update_package = NULL;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800588 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800589 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800590 int toggle_secure_fs = 0;
591 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800592
593 int arg;
594 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
595 switch (arg) {
596 case 'p': previous_runs = atoi(optarg); break;
597 case 's': send_intent = optarg; break;
598 case 'u': update_package = optarg; break;
599 case 'w': wipe_data = wipe_cache = 1; break;
600 case 'c': wipe_cache = 1; break;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800601 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800602 case '?':
603 LOGE("Invalid command argument\n");
604 continue;
605 }
606 }
607
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800608 device_recovery_start();
609
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800610 fprintf(stderr, "Command:");
611 for (arg = 0; arg < argc; arg++) {
612 fprintf(stderr, " \"%s\"", argv[arg]);
613 }
614 fprintf(stderr, "\n\n");
615
616 property_list(print_property, NULL);
617 fprintf(stderr, "\n");
618
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800619 int status = INSTALL_SUCCESS;
620
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800621 if (toggle_secure_fs) {
622 if (strcmp(encrypted_fs_mode,"on") == 0) {
623 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800624 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800625 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
626 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800627 ui_print("Disabling Encrypted FS.\n");
628 } else {
629 ui_print("Error: invalid Encrypted FS setting.\n");
630 status = INSTALL_ERROR;
631 }
632
633 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
634 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800635 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
636 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800637 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800638 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800639 }
640
641 if (status != INSTALL_ERROR) {
642 if (erase_root("DATA:")) {
643 ui_print("Data wipe failed.\n");
644 status = INSTALL_ERROR;
645 } else if (erase_root("CACHE:")) {
646 ui_print("Cache wipe failed.\n");
647 status = INSTALL_ERROR;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800648 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
649 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800650 ui_print("Encrypted FS change aborted.\n");
651 status = INSTALL_ERROR;
652 } else {
653 ui_print("Successfully updated Encrypted FS.\n");
654 status = INSTALL_SUCCESS;
655 }
656 }
657 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800658 status = install_package(update_package);
659 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700660 } else if (wipe_data) {
661 if (device_wipe_data()) status = INSTALL_ERROR;
662 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800663 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
664 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700665 } else if (wipe_cache) {
666 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
667 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800668 } else {
669 status = INSTALL_ERROR; // No command specified
670 }
671
672 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongkerbe598882010-04-08 14:36:55 -0700673 if (status != INSTALL_SUCCESS || ui_text_visible()) {
674 // Mount the sdcard when the menu is enabled so you can "adb
675 // push" packages to the sdcard and immediately install them.
676 ensure_root_path_mounted(SDCARD_ROOT);
677 prompt_and_wait();
678 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800679
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800680 // Otherwise, get ready to boot the main system...
681 finish_recovery(send_intent);
682 ui_print("Rebooting...\n");
683 sync();
684 reboot(RB_AUTOBOOT);
685 return EXIT_SUCCESS;
686}