blob: 53241acf89ad7faf17e8edff67b9a0ab44c2adbd [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>
28#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
Koushik Dutta1741dcd2010-06-11 00:49:49 -070031#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
33#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "common.h"
35#include "cutils/properties.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#include "install.h"
37#include "minui/minui.h"
38#include "minzip/DirUtil.h"
39#include "roots.h"
Doug Zongkerddd6a282009-06-09 12:22:33 -070040#include "recovery_ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080042#include "extendedcommands.h"
43#include "commands.h"
44
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045static const struct option OPTIONS[] = {
46 { "send_intent", required_argument, NULL, 's' },
47 { "update_package", required_argument, NULL, 'u' },
48 { "wipe_data", no_argument, NULL, 'w' },
49 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker988500b2009-10-06 14:41:38 -070050 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051};
52
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -080053static int allow_display_toggle = 1;
54
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055static const char *COMMAND_FILE = "CACHE:recovery/command";
56static const char *INTENT_FILE = "CACHE:recovery/intent";
57static const char *LOG_FILE = "CACHE:recovery/log";
58static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
59static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
60
61/*
62 * The recovery tool communicates with the main system through /cache files.
63 * /cache/recovery/command - INPUT - command line for tool, one arg per line
64 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
65 * /cache/recovery/intent - OUTPUT - intent that was passed in
66 *
67 * The arguments which may be supplied in the recovery.command file:
68 * --send_intent=anystring - write the text out to recovery.intent
69 * --update_package=root:path - verify install an OTA package file
70 * --wipe_data - erase user data (and cache), then reboot
71 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080072 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 *
74 * After completing, we remove /cache/recovery/command and reboot.
75 * Arguments may also be supplied in the bootloader control block (BCB).
76 * These important scenarios must be safely restartable at any point:
77 *
78 * FACTORY RESET
79 * 1. user selects "factory reset"
80 * 2. main system writes "--wipe_data" to /cache/recovery/command
81 * 3. main system reboots into recovery
82 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
83 * -- after this, rebooting will restart the erase --
84 * 5. erase_root() reformats /data
85 * 6. erase_root() reformats /cache
86 * 7. finish_recovery() erases BCB
87 * -- after this, rebooting will restart the main system --
88 * 8. main() calls reboot() to boot main system
89 *
90 * OTA INSTALL
91 * 1. main system downloads OTA package to /cache/some-filename.zip
92 * 2. main system writes "--update_package=CACHE:some-filename.zip"
93 * 3. main system reboots into recovery
94 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
95 * -- after this, rebooting will attempt to reinstall the update --
96 * 5. install_package() attempts to install the update
97 * NOTE: the package install must itself be restartable from any point
98 * 6. finish_recovery() erases BCB
99 * -- after this, rebooting will (try to) restart the main system --
100 * 7. ** if install failed **
101 * 7a. prompt_and_wait() shows an error icon and waits for the user
102 * 7b; the user reboots (pulling the battery, etc) into the main system
103 * 8. main() calls maybe_install_firmware_update()
104 * ** if the update contained radio/hboot firmware **:
105 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
106 * -- after this, rebooting will reformat cache & restart main system --
107 * 8b. m_i_f_u() writes firmware image into raw cache partition
108 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
109 * -- after this, rebooting will attempt to reinstall firmware --
110 * 8d. bootloader tries to flash firmware
111 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
112 * -- after this, rebooting will reformat cache & restart main system --
113 * 8f. erase_root() reformats /cache
114 * 8g. finish_recovery() erases BCB
115 * -- after this, rebooting will (try to) restart the main system --
116 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800117 *
118 * ENCRYPTED FILE SYSTEMS ENABLE/DISABLE
119 * 1. user selects "enable encrypted file systems"
120 * 2. main system writes "--set_encrypted_filesystem=on|off" to
121 * /cache/recovery/command
122 * 3. main system reboots into recovery
123 * 4. get_args() writes BCB with "boot-recovery" and
124 * "--set_encrypted_filesystems=on|off"
125 * -- after this, rebooting will restart the transition --
126 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
127 * Settings include: property to specify the Encrypted FS istatus and
128 * FS encryption key if enabled (not yet implemented)
129 * 6. erase_root() reformats /data
130 * 7. erase_root() reformats /cache
131 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
132 * Settings include: property to specify the Encrypted FS status and
133 * FS encryption key if enabled (not yet implemented)
134 * 9. finish_recovery() erases BCB
135 * -- after this, rebooting will restart the main system --
136 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 */
138
139static const int MAX_ARG_LENGTH = 4096;
140static const int MAX_ARGS = 100;
141
142// open a file given in root:path format, mounting partitions as necessary
143static FILE*
144fopen_root_path(const char *root_path, const char *mode) {
145 if (ensure_root_path_mounted(root_path) != 0) {
146 LOGE("Can't mount %s\n", root_path);
147 return NULL;
148 }
149
150 char path[PATH_MAX] = "";
151 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
152 LOGE("Bad path %s\n", root_path);
153 return NULL;
154 }
155
156 // When writing, try to create the containing directory, if necessary.
157 // Use generous permissions, the system (init.rc) will reset them.
158 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
159
160 FILE *fp = fopen(path, mode);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800161 if (fp == NULL && root_path != COMMAND_FILE) LOGE("Can't open %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800162 return fp;
163}
164
165// close a file, log an error if the error indicator is set
166static void
167check_and_fclose(FILE *fp, const char *name) {
168 fflush(fp);
169 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
170 fclose(fp);
171}
172
173// command line args come from, in decreasing precedence:
174// - the actual command line
175// - the bootloader control block (one per line, after "recovery")
176// - the contents of COMMAND_FILE (one per line)
177static void
178get_args(int *argc, char ***argv) {
179 struct bootloader_message boot;
180 memset(&boot, 0, sizeof(boot));
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700181#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700183#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800184
185 if (boot.command[0] != 0 && boot.command[0] != 255) {
186 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
187 }
188
189 if (boot.status[0] != 0 && boot.status[0] != 255) {
190 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
191 }
192
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700193 struct stat file_info;
194
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700196 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
198 const char *arg = strtok(boot.recovery, "\n");
199 if (arg != NULL && !strcmp(arg, "recovery")) {
200 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
201 (*argv)[0] = strdup(arg);
202 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
203 if ((arg = strtok(NULL, "\n")) == NULL) break;
204 (*argv)[*argc] = strdup(arg);
205 }
206 LOGI("Got arguments from boot message\n");
207 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
208 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
209 }
210 }
211
212 // --- if that doesn't work, try the command file
213 if (*argc <= 1) {
214 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
215 if (fp != NULL) {
216 char *argv0 = (*argv)[0];
217 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
218 (*argv)[0] = argv0; // use the same program name
219
220 char buf[MAX_ARG_LENGTH];
221 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
222 if (!fgets(buf, sizeof(buf), fp)) break;
223 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
224 }
225
226 check_and_fclose(fp, COMMAND_FILE);
227 LOGI("Got arguments from %s\n", COMMAND_FILE);
228 }
229 }
230
231 // --> write the arguments we have back into the bootloader control block
232 // always boot into recovery after this (until finish_recovery() is called)
233 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
234 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
235 int i;
236 for (i = 1; i < *argc; ++i) {
237 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
238 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
239 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700240#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700242#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243}
244
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700245#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik K. Duttae9234872010-02-12 00:43:24 -0800246void
Oscar Montemayor05231562009-11-30 08:40:57 -0800247set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700248 struct bootloader_message boot;
249 memset(&boot, 0, sizeof(boot));
250 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
251 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
252 set_bootloader_message(&boot);
253}
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700254#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255
256// clear the recovery command and prepare to boot a (hopefully working) system,
257// copy our log file to cache as well (for the system to read), and
258// record any intent we were asked to communicate back to the system.
259// this function is idempotent: call it as many times as you like.
260static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800261finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800262 // By this point, we're ready to return to the main system...
263 if (send_intent != NULL) {
264 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000265 if (fp == NULL) {
266 LOGE("Can't open %s\n", INTENT_FILE);
267 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268 fputs(send_intent, fp);
269 check_and_fclose(fp, INTENT_FILE);
270 }
271 }
272
273 // Copy logs to cache so the system can find out what happened.
274 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000275 if (log == NULL) {
276 LOGE("Can't open %s\n", LOG_FILE);
277 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
279 if (tmplog == NULL) {
280 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
281 } else {
282 static long tmplog_offset = 0;
283 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
284 char buf[4096];
285 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
286 tmplog_offset = ftell(tmplog);
287 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
288 }
289 check_and_fclose(log, LOG_FILE);
290 }
291
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700292#ifndef BOARD_HAS_NO_MISC_PARTITION
Oscar Montemayor05231562009-11-30 08:40:57 -0800293 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 struct bootloader_message boot;
295 memset(&boot, 0, sizeof(boot));
296 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700297#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800298
299 // Remove the command file, so recovery won't repeat indefinitely.
300 char path[PATH_MAX] = "";
301 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
302 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
303 (unlink(path) && errno != ENOENT)) {
304 LOGW("Can't unlink %s\n", COMMAND_FILE);
305 }
306
307 sync(); // For good measure.
308}
309
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310static int
Oscar Montemayor05231562009-11-30 08:40:57 -0800311erase_root(const char *root) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312 ui_set_background(BACKGROUND_ICON_INSTALLING);
313 ui_show_indeterminate_progress();
314 ui_print("Formatting %s...\n", root);
315 return format_root_device(root);
316}
317
Doug Zongkerf93d8162009-09-22 15:16:02 -0700318static char**
319prepend_title(char** headers) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700320 char* title[] = { EXPAND(RECOVERY_VERSION),
Doug Zongkerd6837852009-06-17 22:07:13 -0700321 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322 NULL };
323
Doug Zongkerd6837852009-06-17 22:07:13 -0700324 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700325 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700326 int count = 0;
327 char** p;
328 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700329 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700330
Doug Zongkerf93d8162009-09-22 15:16:02 -0700331 char** new_headers = malloc((count+1) * sizeof(char*));
332 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700333 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700334 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700335 *h = NULL;
336
Doug Zongkerf93d8162009-09-22 15:16:02 -0700337 return new_headers;
338}
339
Koushik K. Duttae9234872010-02-12 00:43:24 -0800340int
Doug Zongkerf93d8162009-09-22 15:16:02 -0700341get_menu_selection(char** headers, char** items, int menu_only) {
342 // throw away keys pressed previously, so user doesn't
343 // accidentally trigger menu items.
344 ui_clear_key_queue();
345
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700346 int item_count = ui_start_menu(headers, items);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347 int selected = 0;
348 int chosen_item = -1;
349
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700350 // Some users with dead enter keys need a way to turn on power to select.
351 // Jiggering across the wrapping menu is one "secret" way to enable it.
352 // We can't rely on /cache or /sdcard since they may not be available.
353 int wrap_count = 0;
354
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800355 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800356 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800357 int visible = ui_text_visible();
358
Doug Zongkerddd6a282009-06-09 12:22:33 -0700359 int action = device_handle_key(key, visible);
360
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700361 int old_selected = selected;
362
Doug Zongkerddd6a282009-06-09 12:22:33 -0700363 if (action < 0) {
364 switch (action) {
365 case HIGHLIGHT_UP:
366 --selected;
367 selected = ui_menu_select(selected);
368 break;
369 case HIGHLIGHT_DOWN:
370 ++selected;
371 selected = ui_menu_select(selected);
372 break;
373 case SELECT_ITEM:
374 chosen_item = selected;
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700375 if (ui_get_showing_back_button()) {
376 if (chosen_item == item_count) {
377 chosen_item = GO_BACK;
378 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700379 }
Doug Zongkerddd6a282009-06-09 12:22:33 -0700380 break;
381 case NO_ACTION:
382 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800383 case GO_BACK:
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800384 chosen_item = GO_BACK;
385 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800386 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700387 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700388 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800389 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700390
391 if (abs(selected - old_selected) > 1) {
392 wrap_count++;
393 if (wrap_count == 3) {
394 wrap_count = 0;
395 if (ui_get_showing_back_button()) {
Koushik Dutta95654242010-07-14 21:01:21 -0700396 ui_print("Back menu button disabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700397 ui_set_showing_back_button(0);
398 }
399 else {
Koushik Dutta95654242010-07-14 21:01:21 -0700400 ui_print("Back menu button enabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700401 ui_set_showing_back_button(1);
402 }
403 }
404 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700405 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800406
Doug Zongkerf93d8162009-09-22 15:16:02 -0700407 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800408 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700409 return chosen_item;
410}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800411
Doug Zongkerf93d8162009-09-22 15:16:02 -0700412static void
413wipe_data(int confirm) {
414 if (confirm) {
415 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700416
Doug Zongkerf93d8162009-09-22 15:16:02 -0700417 if (title_headers == NULL) {
418 char* headers[] = { "Confirm wipe of all user data?",
419 " THIS CAN NOT BE UNDONE.",
420 "",
421 NULL };
422 title_headers = prepend_title(headers);
423 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800424
Doug Zongkerf93d8162009-09-22 15:16:02 -0700425 char* items[] = { " No",
426 " No",
427 " No",
428 " No",
429 " No",
430 " No",
431 " No",
432 " Yes -- delete all user data", // [7]
433 " No",
434 " No",
435 " No",
436 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800437
Doug Zongkerf93d8162009-09-22 15:16:02 -0700438 int chosen_item = get_menu_selection(title_headers, items, 1);
439 if (chosen_item != 7) {
440 return;
441 }
442 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700443
Doug Zongkerf93d8162009-09-22 15:16:02 -0700444 ui_print("\n-- Wiping data...\n");
445 device_wipe_data();
446 erase_root("DATA:");
Koushik Dutta63e04762010-06-15 12:56:17 -0700447#ifdef HAS_DATADATA
448 erase_root("DATADATA:");
449#endif
Doug Zongkerf93d8162009-09-22 15:16:02 -0700450 erase_root("CACHE:");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700451 erase_root("SDEXT:");
Koushik Dutta82c2ca22010-07-03 13:54:21 -0700452 erase_root("SDCARD:/.android_secure");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700453 ui_print("Data wipe complete.\n");
454}
455
456static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800457prompt_and_wait() {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700458 char** headers = prepend_title(MENU_HEADERS);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800459
Doug Zongkerf93d8162009-09-22 15:16:02 -0700460 for (;;) {
461 finish_recovery(NULL);
462 ui_reset_progress();
463
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800464 allow_display_toggle = 1;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700465 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800466 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700467
468 // device-specific code may take some action here. It may
469 // return one of the core actions handled in the switch
470 // statement below.
471 chosen_item = device_perform_action(chosen_item);
472
473 switch (chosen_item) {
474 case ITEM_REBOOT:
475 return;
476
477 case ITEM_WIPE_DATA:
478 wipe_data(ui_text_visible());
479 if (!ui_text_visible()) return;
480 break;
481
482 case ITEM_WIPE_CACHE:
Koushik Dutta95654242010-07-14 21:01:21 -0700483 if (confirm_selection("Confirm wipe?", "Yes - Wipe Cache"))
484 {
485 ui_print("\n-- Wiping cache...\n");
486 erase_root("CACHE:");
487 ui_print("Cache wipe complete.\n");
488 if (!ui_text_visible()) return;
489 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700490 break;
491
492 case ITEM_APPLY_SDCARD:
Koushik Dutta95654242010-07-14 21:01:21 -0700493 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
494 {
495 ui_print("\n-- Install from sdcard...\n");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700496#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik Dutta95654242010-07-14 21:01:21 -0700497 set_sdcard_update_bootloader_message();
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700498#endif
Koushik Dutta95654242010-07-14 21:01:21 -0700499 int status = install_package(SDCARD_PACKAGE_FILE);
500 if (status != INSTALL_SUCCESS) {
501 ui_set_background(BACKGROUND_ICON_ERROR);
502 ui_print("Installation aborted.\n");
503 } else if (!ui_text_visible()) {
504 return; // reboot if logs aren't visible
505 } else {
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700506#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik Duttad63eaef2010-07-14 21:01:21 -0700507 if (firmware_update_pending()) {
508 ui_print("\nReboot via menu to complete\n"
509 "installation.\n");
510 } else {
511 ui_print("\nInstall from sdcard complete.\n");
512 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700513#else
Koushik Dutta95654242010-07-14 21:01:21 -0700514 ui_print("\nInstall from sdcard complete.\n");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700515#endif
Koushik Duttaef83d3e2010-07-19 08:54:23 -0700516 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700517 }
518 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800519 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800520 show_install_update_menu();
521 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700522 case ITEM_NANDROID:
523 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800524 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700525 case ITEM_PARTITION:
526 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700527 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700528 case ITEM_ADVANCED:
529 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800530 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800531 }
532 }
533}
534
535static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800536print_property(const char *key, const char *name, void *cookie) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800537 fprintf(stderr, "%s=%s\n", key, name);
538}
539
540int
Oscar Montemayor05231562009-11-30 08:40:57 -0800541main(int argc, char **argv) {
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800542 if (strstr(argv[0], "recovery") == NULL)
543 {
544 if (strstr(argv[0], "flash_image") != NULL)
545 return flash_image_main(argc, argv);
546 if (strstr(argv[0], "dump_image") != NULL)
547 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700548 if (strstr(argv[0], "erase_image") != NULL)
549 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800550 if (strstr(argv[0], "mkyaffs2image") != NULL)
551 return mkyaffs2image_main(argc, argv);
552 if (strstr(argv[0], "unyaffs") != NULL)
553 return unyaffs_main(argc, argv);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800554 if (strstr(argv[0], "amend"))
555 return amend_main(argc, argv);
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700556 if (strstr(argv[0], "nandroid"))
557 return nandroid_main(argc, argv);
Koushik Dutta852bb422010-07-24 11:18:00 -0700558 if (strstr(argv[0], "reboot"))
559 return reboot_main(argc, argv);
560 if (strstr(argv[0], "setprop"))
561 return setprop_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800562 return busybox_driver(argc, argv);
563 }
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700564 __system("/sbin/postrecoveryboot.sh");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700565 create_fstab();
Koushik Duttafd1579b2010-05-01 12:46:55 -0700566
Koushik K. Dutta03173782010-02-26 14:14:23 -0800567 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800568 time_t start = time(NULL);
569
570 // If these fail, there's not really anywhere to complain...
571 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
572 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
573 fprintf(stderr, "Starting recovery on %s", ctime(&start));
574
575 ui_init();
Koushik Duttafdda0d62010-07-01 12:52:34 -0700576 ui_print(EXPAND(RECOVERY_VERSION)"\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800577 get_args(&argc, &argv);
578
579 int previous_runs = 0;
580 const char *send_intent = NULL;
581 const char *update_package = NULL;
582 int wipe_data = 0, wipe_cache = 0;
583
584 int arg;
585 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
586 switch (arg) {
587 case 'p': previous_runs = atoi(optarg); break;
588 case 's': send_intent = optarg; break;
589 case 'u': update_package = optarg; break;
590 case 'w': wipe_data = wipe_cache = 1; break;
591 case 'c': wipe_cache = 1; break;
592 case '?':
593 LOGE("Invalid command argument\n");
594 continue;
595 }
596 }
597
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800598 device_recovery_start();
599
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800600 fprintf(stderr, "Command:");
601 for (arg = 0; arg < argc; arg++) {
602 fprintf(stderr, " \"%s\"", argv[arg]);
603 }
604 fprintf(stderr, "\n\n");
605
606 property_list(print_property, NULL);
607 fprintf(stderr, "\n");
608
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800609 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800610
611 RecoveryCommandContext ctx = { NULL };
612 if (register_update_commands(&ctx)) {
613 LOGE("Can't install update commands\n");
614 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800615
616 if (update_package != NULL) {
Chris Soyarsa1749d92010-02-26 02:45:55 -0500617 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800618 status = install_package(update_package);
619 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700620 } else if (wipe_data) {
621 if (device_wipe_data()) status = INSTALL_ERROR;
622 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800623 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
624 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700625 } else if (wipe_cache) {
626 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
627 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800628 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700629 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800630 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800631 // we are starting up in user initiated recovery here
632 // let's set up some default options
633 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800634 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800635 is_user_initiated_recovery = 1;
636 ui_set_show_text(1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800637
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800638 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700639 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700640 int ret;
641 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800642 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800643 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800644 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700645 else {
646 handle_failure(ret);
647 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700648 } else {
649 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800650 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800651 }
652
Koushik K. Dutta03173782010-02-26 14:14:23 -0800653 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800654 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
655
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700656#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800657 // If there is a radio image pending, reboot now to install it.
658 maybe_install_firmware_update(send_intent);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700659#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800660
661 // Otherwise, get ready to boot the main system...
662 finish_recovery(send_intent);
663 ui_print("Rebooting...\n");
664 sync();
665 reboot(RB_AUTOBOOT);
666 return EXIT_SUCCESS;
667}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800668
669int get_allow_toggle_display() {
670 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500671}