blob: 8d0e4d902bdaa8f8a0f831e8d90bd5876690422b [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>
Koushik Dutta1741dcd2010-06-11 00:49:49 -070030#include <sys/stat.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"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080041#include "extendedcommands.h"
42#include "commands.h"
43
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044static const struct option OPTIONS[] = {
45 { "send_intent", required_argument, NULL, 's' },
46 { "update_package", required_argument, NULL, 'u' },
47 { "wipe_data", no_argument, NULL, 'w' },
48 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker988500b2009-10-06 14:41:38 -070049 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050};
51
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -080052static int allow_display_toggle = 1;
53
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054static const char *COMMAND_FILE = "CACHE:recovery/command";
55static const char *INTENT_FILE = "CACHE:recovery/intent";
56static const char *LOG_FILE = "CACHE:recovery/log";
57static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
58static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
59
60/*
61 * The recovery tool communicates with the main system through /cache files.
62 * /cache/recovery/command - INPUT - command line for tool, one arg per line
63 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
64 * /cache/recovery/intent - OUTPUT - intent that was passed in
65 *
66 * The arguments which may be supplied in the recovery.command file:
67 * --send_intent=anystring - write the text out to recovery.intent
68 * --update_package=root:path - verify install an OTA package file
69 * --wipe_data - erase user data (and cache), then reboot
70 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080071 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072 *
73 * After completing, we remove /cache/recovery/command and reboot.
74 * Arguments may also be supplied in the bootloader control block (BCB).
75 * These important scenarios must be safely restartable at any point:
76 *
77 * FACTORY RESET
78 * 1. user selects "factory reset"
79 * 2. main system writes "--wipe_data" to /cache/recovery/command
80 * 3. main system reboots into recovery
81 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
82 * -- after this, rebooting will restart the erase --
83 * 5. erase_root() reformats /data
84 * 6. erase_root() reformats /cache
85 * 7. finish_recovery() erases BCB
86 * -- after this, rebooting will restart the main system --
87 * 8. main() calls reboot() to boot main system
88 *
89 * OTA INSTALL
90 * 1. main system downloads OTA package to /cache/some-filename.zip
91 * 2. main system writes "--update_package=CACHE:some-filename.zip"
92 * 3. main system reboots into recovery
93 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
94 * -- after this, rebooting will attempt to reinstall the update --
95 * 5. install_package() attempts to install the update
96 * NOTE: the package install must itself be restartable from any point
97 * 6. finish_recovery() erases BCB
98 * -- after this, rebooting will (try to) restart the main system --
99 * 7. ** if install failed **
100 * 7a. prompt_and_wait() shows an error icon and waits for the user
101 * 7b; the user reboots (pulling the battery, etc) into the main system
102 * 8. main() calls maybe_install_firmware_update()
103 * ** if the update contained radio/hboot firmware **:
104 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
105 * -- after this, rebooting will reformat cache & restart main system --
106 * 8b. m_i_f_u() writes firmware image into raw cache partition
107 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
108 * -- after this, rebooting will attempt to reinstall firmware --
109 * 8d. bootloader tries to flash firmware
110 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
111 * -- after this, rebooting will reformat cache & restart main system --
112 * 8f. erase_root() reformats /cache
113 * 8g. finish_recovery() erases BCB
114 * -- after this, rebooting will (try to) restart the main system --
115 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800116 *
117 * ENCRYPTED FILE SYSTEMS ENABLE/DISABLE
118 * 1. user selects "enable encrypted file systems"
119 * 2. main system writes "--set_encrypted_filesystem=on|off" to
120 * /cache/recovery/command
121 * 3. main system reboots into recovery
122 * 4. get_args() writes BCB with "boot-recovery" and
123 * "--set_encrypted_filesystems=on|off"
124 * -- after this, rebooting will restart the transition --
125 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
126 * Settings include: property to specify the Encrypted FS istatus and
127 * FS encryption key if enabled (not yet implemented)
128 * 6. erase_root() reformats /data
129 * 7. erase_root() reformats /cache
130 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
131 * Settings include: property to specify the Encrypted FS status and
132 * FS encryption key if enabled (not yet implemented)
133 * 9. finish_recovery() erases BCB
134 * -- after this, rebooting will restart the main system --
135 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 */
137
138static const int MAX_ARG_LENGTH = 4096;
139static const int MAX_ARGS = 100;
140
141// open a file given in root:path format, mounting partitions as necessary
142static FILE*
143fopen_root_path(const char *root_path, const char *mode) {
144 if (ensure_root_path_mounted(root_path) != 0) {
145 LOGE("Can't mount %s\n", root_path);
146 return NULL;
147 }
148
149 char path[PATH_MAX] = "";
150 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
151 LOGE("Bad path %s\n", root_path);
152 return NULL;
153 }
154
155 // When writing, try to create the containing directory, if necessary.
156 // Use generous permissions, the system (init.rc) will reset them.
157 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
158
159 FILE *fp = fopen(path, mode);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800160 if (fp == NULL && root_path != COMMAND_FILE) LOGE("Can't open %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 return fp;
162}
163
164// close a file, log an error if the error indicator is set
165static void
166check_and_fclose(FILE *fp, const char *name) {
167 fflush(fp);
168 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
169 fclose(fp);
170}
171
172// command line args come from, in decreasing precedence:
173// - the actual command line
174// - the bootloader control block (one per line, after "recovery")
175// - the contents of COMMAND_FILE (one per line)
176static void
177get_args(int *argc, char ***argv) {
178 struct bootloader_message boot;
179 memset(&boot, 0, sizeof(boot));
180 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
181
182 if (boot.command[0] != 0 && boot.command[0] != 255) {
183 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
184 }
185
186 if (boot.status[0] != 0 && boot.status[0] != 255) {
187 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
188 }
189
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700190 struct stat file_info;
191
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700193 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
195 const char *arg = strtok(boot.recovery, "\n");
196 if (arg != NULL && !strcmp(arg, "recovery")) {
197 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
198 (*argv)[0] = strdup(arg);
199 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
200 if ((arg = strtok(NULL, "\n")) == NULL) break;
201 (*argv)[*argc] = strdup(arg);
202 }
203 LOGI("Got arguments from boot message\n");
204 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
205 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
206 }
207 }
208
209 // --- if that doesn't work, try the command file
210 if (*argc <= 1) {
211 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
212 if (fp != NULL) {
213 char *argv0 = (*argv)[0];
214 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
215 (*argv)[0] = argv0; // use the same program name
216
217 char buf[MAX_ARG_LENGTH];
218 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
219 if (!fgets(buf, sizeof(buf), fp)) break;
220 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
221 }
222
223 check_and_fclose(fp, COMMAND_FILE);
224 LOGI("Got arguments from %s\n", COMMAND_FILE);
225 }
226 }
227
228 // --> write the arguments we have back into the bootloader control block
229 // always boot into recovery after this (until finish_recovery() is called)
230 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
231 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
232 int i;
233 for (i = 1; i < *argc; ++i) {
234 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
235 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
236 }
237 set_bootloader_message(&boot);
238}
239
Koushik K. Duttae9234872010-02-12 00:43:24 -0800240void
Oscar Montemayor05231562009-11-30 08:40:57 -0800241set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700242 struct bootloader_message boot;
243 memset(&boot, 0, sizeof(boot));
244 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
245 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
246 set_bootloader_message(&boot);
247}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248
249// clear the recovery command and prepare to boot a (hopefully working) system,
250// copy our log file to cache as well (for the system to read), and
251// record any intent we were asked to communicate back to the system.
252// this function is idempotent: call it as many times as you like.
253static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800254finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 // By this point, we're ready to return to the main system...
256 if (send_intent != NULL) {
257 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000258 if (fp == NULL) {
259 LOGE("Can't open %s\n", INTENT_FILE);
260 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 fputs(send_intent, fp);
262 check_and_fclose(fp, INTENT_FILE);
263 }
264 }
265
266 // Copy logs to cache so the system can find out what happened.
267 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000268 if (log == NULL) {
269 LOGE("Can't open %s\n", LOG_FILE);
270 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
272 if (tmplog == NULL) {
273 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
274 } else {
275 static long tmplog_offset = 0;
276 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
277 char buf[4096];
278 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
279 tmplog_offset = ftell(tmplog);
280 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
281 }
282 check_and_fclose(log, LOG_FILE);
283 }
284
Oscar Montemayor05231562009-11-30 08:40:57 -0800285 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800286 struct bootloader_message boot;
287 memset(&boot, 0, sizeof(boot));
288 set_bootloader_message(&boot);
289
290 // Remove the command file, so recovery won't repeat indefinitely.
291 char path[PATH_MAX] = "";
292 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
293 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
294 (unlink(path) && errno != ENOENT)) {
295 LOGW("Can't unlink %s\n", COMMAND_FILE);
296 }
297
298 sync(); // For good measure.
299}
300
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301static int
Oscar Montemayor05231562009-11-30 08:40:57 -0800302erase_root(const char *root) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303 ui_set_background(BACKGROUND_ICON_INSTALLING);
304 ui_show_indeterminate_progress();
305 ui_print("Formatting %s...\n", root);
306 return format_root_device(root);
307}
308
Doug Zongkerf93d8162009-09-22 15:16:02 -0700309static char**
310prepend_title(char** headers) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700311 char* title[] = { EXPAND(RECOVERY_VERSION),
Doug Zongkerd6837852009-06-17 22:07:13 -0700312 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313 NULL };
314
Doug Zongkerd6837852009-06-17 22:07:13 -0700315 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700316 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700317 int count = 0;
318 char** p;
319 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700320 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700321
Doug Zongkerf93d8162009-09-22 15:16:02 -0700322 char** new_headers = malloc((count+1) * sizeof(char*));
323 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700324 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700325 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700326 *h = NULL;
327
Doug Zongkerf93d8162009-09-22 15:16:02 -0700328 return new_headers;
329}
330
Koushik K. Duttae9234872010-02-12 00:43:24 -0800331int
Doug Zongkerf93d8162009-09-22 15:16:02 -0700332get_menu_selection(char** headers, char** items, int menu_only) {
333 // throw away keys pressed previously, so user doesn't
334 // accidentally trigger menu items.
335 ui_clear_key_queue();
336
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700337 int item_count = ui_start_menu(headers, items);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338 int selected = 0;
339 int chosen_item = -1;
340
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700341 // Some users with dead enter keys need a way to turn on power to select.
342 // Jiggering across the wrapping menu is one "secret" way to enable it.
343 // We can't rely on /cache or /sdcard since they may not be available.
344 int wrap_count = 0;
345
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800346 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800348 int visible = ui_text_visible();
349
Doug Zongkerddd6a282009-06-09 12:22:33 -0700350 int action = device_handle_key(key, visible);
351
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700352 int old_selected = selected;
353
Doug Zongkerddd6a282009-06-09 12:22:33 -0700354 if (action < 0) {
355 switch (action) {
356 case HIGHLIGHT_UP:
357 --selected;
358 selected = ui_menu_select(selected);
359 break;
360 case HIGHLIGHT_DOWN:
361 ++selected;
362 selected = ui_menu_select(selected);
363 break;
364 case SELECT_ITEM:
365 chosen_item = selected;
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700366 if (ui_get_showing_back_button()) {
367 if (chosen_item == item_count) {
368 chosen_item = GO_BACK;
369 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700370 }
Doug Zongkerddd6a282009-06-09 12:22:33 -0700371 break;
372 case NO_ACTION:
373 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800374 case GO_BACK:
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800375 chosen_item = GO_BACK;
376 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800377 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700378 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700379 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800380 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700381
382 if (abs(selected - old_selected) > 1) {
383 wrap_count++;
384 if (wrap_count == 3) {
385 wrap_count = 0;
386 if (ui_get_showing_back_button()) {
387 ui_print("Back button disabled.\n");
388 ui_set_showing_back_button(0);
389 }
390 else {
391 ui_print("Back button enabled.\n");
392 ui_set_showing_back_button(1);
393 }
394 }
395 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700396 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800397
Doug Zongkerf93d8162009-09-22 15:16:02 -0700398 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800399 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700400 return chosen_item;
401}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800402
Doug Zongkerf93d8162009-09-22 15:16:02 -0700403static void
404wipe_data(int confirm) {
405 if (confirm) {
406 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700407
Doug Zongkerf93d8162009-09-22 15:16:02 -0700408 if (title_headers == NULL) {
409 char* headers[] = { "Confirm wipe of all user data?",
410 " THIS CAN NOT BE UNDONE.",
411 "",
412 NULL };
413 title_headers = prepend_title(headers);
414 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800415
Doug Zongkerf93d8162009-09-22 15:16:02 -0700416 char* items[] = { " No",
417 " No",
418 " No",
419 " No",
420 " No",
421 " No",
422 " No",
423 " Yes -- delete all user data", // [7]
424 " No",
425 " No",
426 " No",
427 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800428
Doug Zongkerf93d8162009-09-22 15:16:02 -0700429 int chosen_item = get_menu_selection(title_headers, items, 1);
430 if (chosen_item != 7) {
431 return;
432 }
433 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700434
Doug Zongkerf93d8162009-09-22 15:16:02 -0700435 ui_print("\n-- Wiping data...\n");
436 device_wipe_data();
437 erase_root("DATA:");
Koushik Dutta63e04762010-06-15 12:56:17 -0700438#ifdef HAS_DATADATA
439 erase_root("DATADATA:");
440#endif
Doug Zongkerf93d8162009-09-22 15:16:02 -0700441 erase_root("CACHE:");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700442 erase_root("SDEXT:");
Koushik Dutta82c2ca22010-07-03 13:54:21 -0700443 erase_root("SDCARD:/.android_secure");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700444 ui_print("Data wipe complete.\n");
445}
446
447static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800448prompt_and_wait() {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700449 char** headers = prepend_title(MENU_HEADERS);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800450
Doug Zongkerf93d8162009-09-22 15:16:02 -0700451 for (;;) {
452 finish_recovery(NULL);
453 ui_reset_progress();
454
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800455 allow_display_toggle = 1;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700456 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800457 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700458
459 // device-specific code may take some action here. It may
460 // return one of the core actions handled in the switch
461 // statement below.
462 chosen_item = device_perform_action(chosen_item);
463
464 switch (chosen_item) {
465 case ITEM_REBOOT:
466 return;
467
468 case ITEM_WIPE_DATA:
469 wipe_data(ui_text_visible());
470 if (!ui_text_visible()) return;
471 break;
472
473 case ITEM_WIPE_CACHE:
474 ui_print("\n-- Wiping cache...\n");
475 erase_root("CACHE:");
476 ui_print("Cache wipe complete.\n");
477 if (!ui_text_visible()) return;
478 break;
479
480 case ITEM_APPLY_SDCARD:
481 ui_print("\n-- Install from sdcard...\n");
482 set_sdcard_update_bootloader_message();
483 int status = install_package(SDCARD_PACKAGE_FILE);
484 if (status != INSTALL_SUCCESS) {
485 ui_set_background(BACKGROUND_ICON_ERROR);
486 ui_print("Installation aborted.\n");
487 } else if (!ui_text_visible()) {
488 return; // reboot if logs aren't visible
489 } else {
Doug Zongkere08991e2010-02-02 13:09:52 -0800490 ui_print("\nInstall from sdcard complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700491 }
492 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800493 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800494 show_install_update_menu();
495 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700496 case ITEM_NANDROID:
497 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800498 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700499 case ITEM_PARTITION:
500 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700501 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700502 case ITEM_ADVANCED:
503 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800504 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800505 }
506 }
507}
508
509static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800510print_property(const char *key, const char *name, void *cookie) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800511 fprintf(stderr, "%s=%s\n", key, name);
512}
513
514int
Oscar Montemayor05231562009-11-30 08:40:57 -0800515main(int argc, char **argv) {
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800516 if (strstr(argv[0], "recovery") == NULL)
517 {
518 if (strstr(argv[0], "flash_image") != NULL)
519 return flash_image_main(argc, argv);
520 if (strstr(argv[0], "dump_image") != NULL)
521 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700522 if (strstr(argv[0], "erase_image") != NULL)
523 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800524 if (strstr(argv[0], "mkyaffs2image") != NULL)
525 return mkyaffs2image_main(argc, argv);
526 if (strstr(argv[0], "unyaffs") != NULL)
527 return unyaffs_main(argc, argv);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800528 if (strstr(argv[0], "amend"))
529 return amend_main(argc, argv);
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700530 if (strstr(argv[0], "nandroid"))
531 return nandroid_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800532 return busybox_driver(argc, argv);
533 }
Koushik Duttaa6522b32010-06-20 13:16:06 -0700534 create_fstab();
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700535 __system("/sbin/postrecoveryboot.sh");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700536
Koushik K. Dutta03173782010-02-26 14:14:23 -0800537 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800538 time_t start = time(NULL);
539
540 // If these fail, there's not really anywhere to complain...
541 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
542 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
543 fprintf(stderr, "Starting recovery on %s", ctime(&start));
544
545 ui_init();
Koushik Duttafdda0d62010-07-01 12:52:34 -0700546 ui_print(EXPAND(RECOVERY_VERSION)"\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800547 get_args(&argc, &argv);
548
549 int previous_runs = 0;
550 const char *send_intent = NULL;
551 const char *update_package = NULL;
552 int wipe_data = 0, wipe_cache = 0;
553
554 int arg;
555 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
556 switch (arg) {
557 case 'p': previous_runs = atoi(optarg); break;
558 case 's': send_intent = optarg; break;
559 case 'u': update_package = optarg; break;
560 case 'w': wipe_data = wipe_cache = 1; break;
561 case 'c': wipe_cache = 1; break;
562 case '?':
563 LOGE("Invalid command argument\n");
564 continue;
565 }
566 }
567
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800568 device_recovery_start();
569
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800570 fprintf(stderr, "Command:");
571 for (arg = 0; arg < argc; arg++) {
572 fprintf(stderr, " \"%s\"", argv[arg]);
573 }
574 fprintf(stderr, "\n\n");
575
576 property_list(print_property, NULL);
577 fprintf(stderr, "\n");
578
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800579 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800580
581 RecoveryCommandContext ctx = { NULL };
582 if (register_update_commands(&ctx)) {
583 LOGE("Can't install update commands\n");
584 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800585
586 if (update_package != NULL) {
Chris Soyarsa1749d92010-02-26 02:45:55 -0500587 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800588 status = install_package(update_package);
589 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700590 } else if (wipe_data) {
591 if (device_wipe_data()) status = INSTALL_ERROR;
592 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800593 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
594 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700595 } else if (wipe_cache) {
596 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
597 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800598 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700599 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800600 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800601 // we are starting up in user initiated recovery here
602 // let's set up some default options
603 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800604 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800605 is_user_initiated_recovery = 1;
606 ui_set_show_text(1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800607
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800608 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700609 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700610 int ret;
611 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800612 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800613 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800614 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700615 else {
616 handle_failure(ret);
617 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700618 } else {
619 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800620 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800621 }
622
Koushik K. Dutta03173782010-02-26 14:14:23 -0800623 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800624 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
625
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800626 // Otherwise, get ready to boot the main system...
627 finish_recovery(send_intent);
628 ui_print("Rebooting...\n");
629 sync();
630 reboot(RB_AUTOBOOT);
631 return EXIT_SUCCESS;
632}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800633
634int get_allow_toggle_display() {
635 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500636}