blob: dc43290d5677c5b61d5a123ecc60906a3485afa0 [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));
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700180#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700182#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800183
184 if (boot.command[0] != 0 && boot.command[0] != 255) {
185 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
186 }
187
188 if (boot.status[0] != 0 && boot.status[0] != 255) {
189 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
190 }
191
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700192 struct stat file_info;
193
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700195 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800196 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
197 const char *arg = strtok(boot.recovery, "\n");
198 if (arg != NULL && !strcmp(arg, "recovery")) {
199 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
200 (*argv)[0] = strdup(arg);
201 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
202 if ((arg = strtok(NULL, "\n")) == NULL) break;
203 (*argv)[*argc] = strdup(arg);
204 }
205 LOGI("Got arguments from boot message\n");
206 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
207 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
208 }
209 }
210
211 // --- if that doesn't work, try the command file
212 if (*argc <= 1) {
213 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
214 if (fp != NULL) {
215 char *argv0 = (*argv)[0];
216 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
217 (*argv)[0] = argv0; // use the same program name
218
219 char buf[MAX_ARG_LENGTH];
220 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
221 if (!fgets(buf, sizeof(buf), fp)) break;
222 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
223 }
224
225 check_and_fclose(fp, COMMAND_FILE);
226 LOGI("Got arguments from %s\n", COMMAND_FILE);
227 }
228 }
229
230 // --> write the arguments we have back into the bootloader control block
231 // always boot into recovery after this (until finish_recovery() is called)
232 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
233 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
234 int i;
235 for (i = 1; i < *argc; ++i) {
236 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
237 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
238 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700239#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800240 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700241#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800242}
243
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700244#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik K. Duttae9234872010-02-12 00:43:24 -0800245void
Oscar Montemayor05231562009-11-30 08:40:57 -0800246set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700247 struct bootloader_message boot;
248 memset(&boot, 0, sizeof(boot));
249 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
250 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
251 set_bootloader_message(&boot);
252}
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700253#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800254
255// clear the recovery command and prepare to boot a (hopefully working) system,
256// copy our log file to cache as well (for the system to read), and
257// record any intent we were asked to communicate back to the system.
258// this function is idempotent: call it as many times as you like.
259static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800260finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 // By this point, we're ready to return to the main system...
262 if (send_intent != NULL) {
263 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000264 if (fp == NULL) {
265 LOGE("Can't open %s\n", INTENT_FILE);
266 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800267 fputs(send_intent, fp);
268 check_and_fclose(fp, INTENT_FILE);
269 }
270 }
271
272 // Copy logs to cache so the system can find out what happened.
273 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000274 if (log == NULL) {
275 LOGE("Can't open %s\n", LOG_FILE);
276 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
278 if (tmplog == NULL) {
279 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
280 } else {
281 static long tmplog_offset = 0;
282 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
283 char buf[4096];
284 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
285 tmplog_offset = ftell(tmplog);
286 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
287 }
288 check_and_fclose(log, LOG_FILE);
289 }
290
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700291#ifndef BOARD_HAS_NO_MISC_PARTITION
Oscar Montemayor05231562009-11-30 08:40:57 -0800292 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293 struct bootloader_message boot;
294 memset(&boot, 0, sizeof(boot));
295 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700296#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297
298 // Remove the command file, so recovery won't repeat indefinitely.
299 char path[PATH_MAX] = "";
300 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
301 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
302 (unlink(path) && errno != ENOENT)) {
303 LOGW("Can't unlink %s\n", COMMAND_FILE);
304 }
305
306 sync(); // For good measure.
307}
308
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800309static int
Oscar Montemayor05231562009-11-30 08:40:57 -0800310erase_root(const char *root) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800311 ui_set_background(BACKGROUND_ICON_INSTALLING);
312 ui_show_indeterminate_progress();
313 ui_print("Formatting %s...\n", root);
314 return format_root_device(root);
315}
316
Doug Zongkerf93d8162009-09-22 15:16:02 -0700317static char**
318prepend_title(char** headers) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700319 char* title[] = { EXPAND(RECOVERY_VERSION),
Doug Zongkerd6837852009-06-17 22:07:13 -0700320 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321 NULL };
322
Doug Zongkerd6837852009-06-17 22:07:13 -0700323 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700324 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700325 int count = 0;
326 char** p;
327 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700328 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700329
Doug Zongkerf93d8162009-09-22 15:16:02 -0700330 char** new_headers = malloc((count+1) * sizeof(char*));
331 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700332 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700333 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700334 *h = NULL;
335
Doug Zongkerf93d8162009-09-22 15:16:02 -0700336 return new_headers;
337}
338
Koushik K. Duttae9234872010-02-12 00:43:24 -0800339int
Doug Zongkerf93d8162009-09-22 15:16:02 -0700340get_menu_selection(char** headers, char** items, int menu_only) {
341 // throw away keys pressed previously, so user doesn't
342 // accidentally trigger menu items.
343 ui_clear_key_queue();
344
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700345 int item_count = ui_start_menu(headers, items);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800346 int selected = 0;
347 int chosen_item = -1;
348
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700349 // Some users with dead enter keys need a way to turn on power to select.
350 // Jiggering across the wrapping menu is one "secret" way to enable it.
351 // We can't rely on /cache or /sdcard since they may not be available.
352 int wrap_count = 0;
353
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800354 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800355 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800356 int visible = ui_text_visible();
357
Doug Zongkerddd6a282009-06-09 12:22:33 -0700358 int action = device_handle_key(key, visible);
359
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700360 int old_selected = selected;
361
Doug Zongkerddd6a282009-06-09 12:22:33 -0700362 if (action < 0) {
363 switch (action) {
364 case HIGHLIGHT_UP:
365 --selected;
366 selected = ui_menu_select(selected);
367 break;
368 case HIGHLIGHT_DOWN:
369 ++selected;
370 selected = ui_menu_select(selected);
371 break;
372 case SELECT_ITEM:
373 chosen_item = selected;
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700374 if (ui_get_showing_back_button()) {
375 if (chosen_item == item_count) {
376 chosen_item = GO_BACK;
377 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700378 }
Doug Zongkerddd6a282009-06-09 12:22:33 -0700379 break;
380 case NO_ACTION:
381 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800382 case GO_BACK:
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800383 chosen_item = GO_BACK;
384 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800385 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700386 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700387 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800388 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700389
390 if (abs(selected - old_selected) > 1) {
391 wrap_count++;
392 if (wrap_count == 3) {
393 wrap_count = 0;
394 if (ui_get_showing_back_button()) {
Koushik Dutta95654242010-07-14 21:01:21 -0700395 ui_print("Back menu button disabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700396 ui_set_showing_back_button(0);
397 }
398 else {
Koushik Dutta95654242010-07-14 21:01:21 -0700399 ui_print("Back menu button enabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700400 ui_set_showing_back_button(1);
401 }
402 }
403 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700404 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800405
Doug Zongkerf93d8162009-09-22 15:16:02 -0700406 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800407 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700408 return chosen_item;
409}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800410
Doug Zongkerf93d8162009-09-22 15:16:02 -0700411static void
412wipe_data(int confirm) {
413 if (confirm) {
414 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700415
Doug Zongkerf93d8162009-09-22 15:16:02 -0700416 if (title_headers == NULL) {
417 char* headers[] = { "Confirm wipe of all user data?",
418 " THIS CAN NOT BE UNDONE.",
419 "",
420 NULL };
421 title_headers = prepend_title(headers);
422 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800423
Doug Zongkerf93d8162009-09-22 15:16:02 -0700424 char* items[] = { " No",
425 " No",
426 " No",
427 " No",
428 " No",
429 " No",
430 " No",
431 " Yes -- delete all user data", // [7]
432 " No",
433 " No",
434 " No",
435 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800436
Doug Zongkerf93d8162009-09-22 15:16:02 -0700437 int chosen_item = get_menu_selection(title_headers, items, 1);
438 if (chosen_item != 7) {
439 return;
440 }
441 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700442
Doug Zongkerf93d8162009-09-22 15:16:02 -0700443 ui_print("\n-- Wiping data...\n");
444 device_wipe_data();
445 erase_root("DATA:");
Koushik Dutta63e04762010-06-15 12:56:17 -0700446#ifdef HAS_DATADATA
447 erase_root("DATADATA:");
448#endif
Doug Zongkerf93d8162009-09-22 15:16:02 -0700449 erase_root("CACHE:");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700450 erase_root("SDEXT:");
Koushik Dutta82c2ca22010-07-03 13:54:21 -0700451 erase_root("SDCARD:/.android_secure");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700452 ui_print("Data wipe complete.\n");
453}
454
455static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800456prompt_and_wait() {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700457 char** headers = prepend_title(MENU_HEADERS);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800458
Doug Zongkerf93d8162009-09-22 15:16:02 -0700459 for (;;) {
460 finish_recovery(NULL);
461 ui_reset_progress();
462
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800463 allow_display_toggle = 1;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700464 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800465 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700466
467 // device-specific code may take some action here. It may
468 // return one of the core actions handled in the switch
469 // statement below.
470 chosen_item = device_perform_action(chosen_item);
471
472 switch (chosen_item) {
473 case ITEM_REBOOT:
474 return;
475
476 case ITEM_WIPE_DATA:
477 wipe_data(ui_text_visible());
478 if (!ui_text_visible()) return;
479 break;
480
481 case ITEM_WIPE_CACHE:
Koushik Dutta95654242010-07-14 21:01:21 -0700482 if (confirm_selection("Confirm wipe?", "Yes - Wipe Cache"))
483 {
484 ui_print("\n-- Wiping cache...\n");
485 erase_root("CACHE:");
486 ui_print("Cache wipe complete.\n");
487 if (!ui_text_visible()) return;
488 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700489 break;
490
491 case ITEM_APPLY_SDCARD:
Koushik Dutta95654242010-07-14 21:01:21 -0700492 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
493 {
494 ui_print("\n-- Install from sdcard...\n");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700495#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik Dutta95654242010-07-14 21:01:21 -0700496 set_sdcard_update_bootloader_message();
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700497#endif
Koushik Dutta95654242010-07-14 21:01:21 -0700498 int status = install_package(SDCARD_PACKAGE_FILE);
499 if (status != INSTALL_SUCCESS) {
500 ui_set_background(BACKGROUND_ICON_ERROR);
501 ui_print("Installation aborted.\n");
502 } else if (!ui_text_visible()) {
503 return; // reboot if logs aren't visible
504 } else {
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700505#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik Duttad63eaef2010-07-14 21:01:21 -0700506 if (firmware_update_pending()) {
507 ui_print("\nReboot via menu to complete\n"
508 "installation.\n");
509 } else {
510 ui_print("\nInstall from sdcard complete.\n");
511 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700512#else
Koushik Dutta95654242010-07-14 21:01:21 -0700513 ui_print("\nInstall from sdcard complete.\n");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700514#endif
Koushik Duttaef83d3e2010-07-19 08:54:23 -0700515 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700516 }
517 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800518 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800519 show_install_update_menu();
520 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700521 case ITEM_NANDROID:
522 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800523 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700524 case ITEM_PARTITION:
525 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700526 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700527 case ITEM_ADVANCED:
528 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800529 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800530 }
531 }
532}
533
534static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800535print_property(const char *key, const char *name, void *cookie) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800536 fprintf(stderr, "%s=%s\n", key, name);
537}
538
539int
Oscar Montemayor05231562009-11-30 08:40:57 -0800540main(int argc, char **argv) {
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800541 if (strstr(argv[0], "recovery") == NULL)
542 {
543 if (strstr(argv[0], "flash_image") != NULL)
544 return flash_image_main(argc, argv);
545 if (strstr(argv[0], "dump_image") != NULL)
546 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700547 if (strstr(argv[0], "erase_image") != NULL)
548 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800549 if (strstr(argv[0], "mkyaffs2image") != NULL)
550 return mkyaffs2image_main(argc, argv);
551 if (strstr(argv[0], "unyaffs") != NULL)
552 return unyaffs_main(argc, argv);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800553 if (strstr(argv[0], "amend"))
554 return amend_main(argc, argv);
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700555 if (strstr(argv[0], "nandroid"))
556 return nandroid_main(argc, argv);
Koushik Dutta852bb422010-07-24 11:18:00 -0700557 if (strstr(argv[0], "reboot"))
558 return reboot_main(argc, argv);
559 if (strstr(argv[0], "setprop"))
560 return setprop_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800561 return busybox_driver(argc, argv);
562 }
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700563 __system("/sbin/postrecoveryboot.sh");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700564 create_fstab();
Koushik Duttafd1579b2010-05-01 12:46:55 -0700565
Koushik K. Dutta03173782010-02-26 14:14:23 -0800566 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800567 time_t start = time(NULL);
568
569 // If these fail, there's not really anywhere to complain...
570 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
571 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
572 fprintf(stderr, "Starting recovery on %s", ctime(&start));
573
574 ui_init();
Koushik Duttafdda0d62010-07-01 12:52:34 -0700575 ui_print(EXPAND(RECOVERY_VERSION)"\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800576 get_args(&argc, &argv);
577
578 int previous_runs = 0;
579 const char *send_intent = NULL;
580 const char *update_package = NULL;
581 int wipe_data = 0, wipe_cache = 0;
582
583 int arg;
584 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
585 switch (arg) {
586 case 'p': previous_runs = atoi(optarg); break;
587 case 's': send_intent = optarg; break;
588 case 'u': update_package = optarg; break;
589 case 'w': wipe_data = wipe_cache = 1; break;
590 case 'c': wipe_cache = 1; break;
591 case '?':
592 LOGE("Invalid command argument\n");
593 continue;
594 }
595 }
596
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800597 device_recovery_start();
598
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800599 fprintf(stderr, "Command:");
600 for (arg = 0; arg < argc; arg++) {
601 fprintf(stderr, " \"%s\"", argv[arg]);
602 }
603 fprintf(stderr, "\n\n");
604
605 property_list(print_property, NULL);
606 fprintf(stderr, "\n");
607
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800608 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800609
610 RecoveryCommandContext ctx = { NULL };
611 if (register_update_commands(&ctx)) {
612 LOGE("Can't install update commands\n");
613 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800614
615 if (update_package != NULL) {
Chris Soyarsa1749d92010-02-26 02:45:55 -0500616 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800617 status = install_package(update_package);
618 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700619 } else if (wipe_data) {
620 if (device_wipe_data()) status = INSTALL_ERROR;
621 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800622 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
623 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700624 } else if (wipe_cache) {
625 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
626 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800627 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700628 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800629 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800630 // we are starting up in user initiated recovery here
631 // let's set up some default options
632 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800633 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800634 is_user_initiated_recovery = 1;
635 ui_set_show_text(1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800636
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800637 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700638 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700639 int ret;
640 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800641 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800642 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800643 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700644 else {
645 handle_failure(ret);
646 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700647 } else {
648 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800649 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800650 }
651
Koushik K. Dutta03173782010-02-26 14:14:23 -0800652 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800653 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
654
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700655#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800656 // If there is a radio image pending, reboot now to install it.
657 maybe_install_firmware_update(send_intent);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700658#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800659
660 // Otherwise, get ready to boot the main system...
661 finish_recovery(send_intent);
662 ui_print("Rebooting...\n");
663 sync();
664 reboot(RB_AUTOBOOT);
665 return EXIT_SUCCESS;
666}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800667
668int get_allow_toggle_display() {
669 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500670}