blob: 593d1f9f2d9e09c35876368182e6545641f160ef [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 K. Duttaa3c2f732010-02-19 14:17:22 -0800341 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800342 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800343 int visible = ui_text_visible();
344
Doug Zongkerddd6a282009-06-09 12:22:33 -0700345 int action = device_handle_key(key, visible);
346
347 if (action < 0) {
348 switch (action) {
349 case HIGHLIGHT_UP:
350 --selected;
351 selected = ui_menu_select(selected);
352 break;
353 case HIGHLIGHT_DOWN:
354 ++selected;
355 selected = ui_menu_select(selected);
356 break;
357 case SELECT_ITEM:
358 chosen_item = selected;
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700359#ifdef KEY_POWER_IS_SELECT_ITEM
360 if (chosen_item == item_count) {
361 chosen_item = GO_BACK;
362 }
363#endif
Doug Zongkerddd6a282009-06-09 12:22:33 -0700364 break;
365 case NO_ACTION:
366 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800367 case GO_BACK:
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800368 chosen_item = GO_BACK;
369 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800370 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700371 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700372 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800373 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700374 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800375
Doug Zongkerf93d8162009-09-22 15:16:02 -0700376 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800377 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700378 return chosen_item;
379}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800380
Doug Zongkerf93d8162009-09-22 15:16:02 -0700381static void
382wipe_data(int confirm) {
383 if (confirm) {
384 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700385
Doug Zongkerf93d8162009-09-22 15:16:02 -0700386 if (title_headers == NULL) {
387 char* headers[] = { "Confirm wipe of all user data?",
388 " THIS CAN NOT BE UNDONE.",
389 "",
390 NULL };
391 title_headers = prepend_title(headers);
392 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800393
Doug Zongkerf93d8162009-09-22 15:16:02 -0700394 char* items[] = { " No",
395 " No",
396 " No",
397 " No",
398 " No",
399 " No",
400 " No",
401 " Yes -- delete all user data", // [7]
402 " No",
403 " No",
404 " No",
405 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800406
Doug Zongkerf93d8162009-09-22 15:16:02 -0700407 int chosen_item = get_menu_selection(title_headers, items, 1);
408 if (chosen_item != 7) {
409 return;
410 }
411 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700412
Doug Zongkerf93d8162009-09-22 15:16:02 -0700413 ui_print("\n-- Wiping data...\n");
414 device_wipe_data();
415 erase_root("DATA:");
Koushik Dutta63e04762010-06-15 12:56:17 -0700416#ifdef HAS_DATADATA
417 erase_root("DATADATA:");
418#endif
Doug Zongkerf93d8162009-09-22 15:16:02 -0700419 erase_root("CACHE:");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700420 erase_root("SDEXT:");
Koushik Dutta82c2ca22010-07-03 13:54:21 -0700421 erase_root("SDCARD:/.android_secure");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700422 ui_print("Data wipe complete.\n");
423}
424
425static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800426prompt_and_wait() {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700427 char** headers = prepend_title(MENU_HEADERS);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800428
Doug Zongkerf93d8162009-09-22 15:16:02 -0700429 for (;;) {
430 finish_recovery(NULL);
431 ui_reset_progress();
432
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800433 allow_display_toggle = 1;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700434 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800435 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700436
437 // device-specific code may take some action here. It may
438 // return one of the core actions handled in the switch
439 // statement below.
440 chosen_item = device_perform_action(chosen_item);
441
442 switch (chosen_item) {
443 case ITEM_REBOOT:
444 return;
445
446 case ITEM_WIPE_DATA:
447 wipe_data(ui_text_visible());
448 if (!ui_text_visible()) return;
449 break;
450
451 case ITEM_WIPE_CACHE:
452 ui_print("\n-- Wiping cache...\n");
453 erase_root("CACHE:");
454 ui_print("Cache wipe complete.\n");
455 if (!ui_text_visible()) return;
456 break;
457
458 case ITEM_APPLY_SDCARD:
459 ui_print("\n-- Install from sdcard...\n");
460 set_sdcard_update_bootloader_message();
461 int status = install_package(SDCARD_PACKAGE_FILE);
462 if (status != INSTALL_SUCCESS) {
463 ui_set_background(BACKGROUND_ICON_ERROR);
464 ui_print("Installation aborted.\n");
465 } else if (!ui_text_visible()) {
466 return; // reboot if logs aren't visible
467 } else {
Doug Zongkere08991e2010-02-02 13:09:52 -0800468 ui_print("\nInstall from sdcard complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700469 }
470 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800471 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800472 show_install_update_menu();
473 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700474 case ITEM_NANDROID:
475 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800476 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700477 case ITEM_PARTITION:
478 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700479 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700480 case ITEM_ADVANCED:
481 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800482 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800483 }
484 }
485}
486
487static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800488print_property(const char *key, const char *name, void *cookie) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800489 fprintf(stderr, "%s=%s\n", key, name);
490}
491
492int
Oscar Montemayor05231562009-11-30 08:40:57 -0800493main(int argc, char **argv) {
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800494 if (strstr(argv[0], "recovery") == NULL)
495 {
496 if (strstr(argv[0], "flash_image") != NULL)
497 return flash_image_main(argc, argv);
498 if (strstr(argv[0], "dump_image") != NULL)
499 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700500 if (strstr(argv[0], "erase_image") != NULL)
501 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800502 if (strstr(argv[0], "mkyaffs2image") != NULL)
503 return mkyaffs2image_main(argc, argv);
504 if (strstr(argv[0], "unyaffs") != NULL)
505 return unyaffs_main(argc, argv);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800506 if (strstr(argv[0], "amend"))
507 return amend_main(argc, argv);
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700508 if (strstr(argv[0], "nandroid"))
509 return nandroid_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800510 return busybox_driver(argc, argv);
511 }
Koushik Duttaa6522b32010-06-20 13:16:06 -0700512 create_fstab();
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700513 __system("/sbin/postrecoveryboot.sh");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700514
Koushik K. Dutta03173782010-02-26 14:14:23 -0800515 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800516 time_t start = time(NULL);
517
518 // If these fail, there's not really anywhere to complain...
519 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
520 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
521 fprintf(stderr, "Starting recovery on %s", ctime(&start));
522
523 ui_init();
Koushik Duttafdda0d62010-07-01 12:52:34 -0700524 ui_print(EXPAND(RECOVERY_VERSION)"\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800525 get_args(&argc, &argv);
526
527 int previous_runs = 0;
528 const char *send_intent = NULL;
529 const char *update_package = NULL;
530 int wipe_data = 0, wipe_cache = 0;
531
532 int arg;
533 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
534 switch (arg) {
535 case 'p': previous_runs = atoi(optarg); break;
536 case 's': send_intent = optarg; break;
537 case 'u': update_package = optarg; break;
538 case 'w': wipe_data = wipe_cache = 1; break;
539 case 'c': wipe_cache = 1; break;
540 case '?':
541 LOGE("Invalid command argument\n");
542 continue;
543 }
544 }
545
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800546 device_recovery_start();
547
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800548 fprintf(stderr, "Command:");
549 for (arg = 0; arg < argc; arg++) {
550 fprintf(stderr, " \"%s\"", argv[arg]);
551 }
552 fprintf(stderr, "\n\n");
553
554 property_list(print_property, NULL);
555 fprintf(stderr, "\n");
556
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800557 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800558
559 RecoveryCommandContext ctx = { NULL };
560 if (register_update_commands(&ctx)) {
561 LOGE("Can't install update commands\n");
562 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800563
564 if (update_package != NULL) {
Chris Soyarsa1749d92010-02-26 02:45:55 -0500565 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800566 status = install_package(update_package);
567 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700568 } else if (wipe_data) {
569 if (device_wipe_data()) status = INSTALL_ERROR;
570 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800571 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
572 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700573 } else if (wipe_cache) {
574 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
575 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800576 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700577 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800578 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800579 // we are starting up in user initiated recovery here
580 // let's set up some default options
581 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800582 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800583 is_user_initiated_recovery = 1;
584 ui_set_show_text(1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800585
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800586 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700587 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700588 int ret;
589 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800590 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800591 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800592 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700593 else {
594 handle_failure(ret);
595 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700596 } else {
597 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800598 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800599 }
600
Koushik K. Dutta03173782010-02-26 14:14:23 -0800601 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800602 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
603
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800604 // Otherwise, get ready to boot the main system...
605 finish_recovery(send_intent);
606 ui_print("Rebooting...\n");
607 sync();
608 reboot(RB_AUTOBOOT);
609 return EXIT_SUCCESS;
610}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800611
612int get_allow_toggle_display() {
613 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500614}