blob: 61f938d1a0e89962317fd8e60ca1607db24bf965 [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"
35#include "firmware.h"
36#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
72 *
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
116 */
117
118static const int MAX_ARG_LENGTH = 4096;
119static const int MAX_ARGS = 100;
120
121// open a file given in root:path format, mounting partitions as necessary
122static FILE*
123fopen_root_path(const char *root_path, const char *mode) {
124 if (ensure_root_path_mounted(root_path) != 0) {
125 LOGE("Can't mount %s\n", root_path);
126 return NULL;
127 }
128
129 char path[PATH_MAX] = "";
130 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
131 LOGE("Bad path %s\n", root_path);
132 return NULL;
133 }
134
135 // When writing, try to create the containing directory, if necessary.
136 // Use generous permissions, the system (init.rc) will reset them.
137 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
138
139 FILE *fp = fopen(path, mode);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800140 if (fp == NULL && root_path != COMMAND_FILE) LOGE("Can't open %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141 return fp;
142}
143
144// close a file, log an error if the error indicator is set
145static void
146check_and_fclose(FILE *fp, const char *name) {
147 fflush(fp);
148 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
149 fclose(fp);
150}
151
152// command line args come from, in decreasing precedence:
153// - the actual command line
154// - the bootloader control block (one per line, after "recovery")
155// - the contents of COMMAND_FILE (one per line)
156static void
157get_args(int *argc, char ***argv) {
158 struct bootloader_message boot;
159 memset(&boot, 0, sizeof(boot));
160 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
161
162 if (boot.command[0] != 0 && boot.command[0] != 255) {
163 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
164 }
165
166 if (boot.status[0] != 0 && boot.status[0] != 255) {
167 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
168 }
169
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700170 struct stat file_info;
171
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700173 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
175 const char *arg = strtok(boot.recovery, "\n");
176 if (arg != NULL && !strcmp(arg, "recovery")) {
177 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
178 (*argv)[0] = strdup(arg);
179 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
180 if ((arg = strtok(NULL, "\n")) == NULL) break;
181 (*argv)[*argc] = strdup(arg);
182 }
183 LOGI("Got arguments from boot message\n");
184 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
185 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
186 }
187 }
188
189 // --- if that doesn't work, try the command file
190 if (*argc <= 1) {
191 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
192 if (fp != NULL) {
193 char *argv0 = (*argv)[0];
194 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
195 (*argv)[0] = argv0; // use the same program name
196
197 char buf[MAX_ARG_LENGTH];
198 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
199 if (!fgets(buf, sizeof(buf), fp)) break;
200 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
201 }
202
203 check_and_fclose(fp, COMMAND_FILE);
204 LOGI("Got arguments from %s\n", COMMAND_FILE);
205 }
206 }
207
208 // --> write the arguments we have back into the bootloader control block
209 // always boot into recovery after this (until finish_recovery() is called)
210 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
211 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
212 int i;
213 for (i = 1; i < *argc; ++i) {
214 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
215 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
216 }
217 set_bootloader_message(&boot);
218}
219
Koushik K. Duttae9234872010-02-12 00:43:24 -0800220void
Doug Zongker34c98df2009-08-18 12:05:45 -0700221set_sdcard_update_bootloader_message()
222{
223 struct bootloader_message boot;
224 memset(&boot, 0, sizeof(boot));
225 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
226 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
227 set_bootloader_message(&boot);
228}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229
230// clear the recovery command and prepare to boot a (hopefully working) system,
231// copy our log file to cache as well (for the system to read), and
232// record any intent we were asked to communicate back to the system.
233// this function is idempotent: call it as many times as you like.
234static void
235finish_recovery(const char *send_intent)
236{
237 // By this point, we're ready to return to the main system...
238 if (send_intent != NULL) {
239 FILE *fp = fopen_root_path(INTENT_FILE, "w");
240 if (fp != NULL) {
241 fputs(send_intent, fp);
242 check_and_fclose(fp, INTENT_FILE);
243 }
244 }
245
246 // Copy logs to cache so the system can find out what happened.
247 FILE *log = fopen_root_path(LOG_FILE, "a");
248 if (log != NULL) {
249 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
250 if (tmplog == NULL) {
251 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
252 } else {
253 static long tmplog_offset = 0;
254 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
255 char buf[4096];
256 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
257 tmplog_offset = ftell(tmplog);
258 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
259 }
260 check_and_fclose(log, LOG_FILE);
261 }
262
263 // Reset the bootloader message to revert to a normal main system boot.
264 struct bootloader_message boot;
265 memset(&boot, 0, sizeof(boot));
266 set_bootloader_message(&boot);
267
268 // Remove the command file, so recovery won't repeat indefinitely.
269 char path[PATH_MAX] = "";
270 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
271 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
272 (unlink(path) && errno != ENOENT)) {
273 LOGW("Can't unlink %s\n", COMMAND_FILE);
274 }
275
276 sync(); // For good measure.
277}
278
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279static int
280erase_root(const char *root)
281{
282 ui_set_background(BACKGROUND_ICON_INSTALLING);
283 ui_show_indeterminate_progress();
284 ui_print("Formatting %s...\n", root);
285 return format_root_device(root);
286}
287
Doug Zongkerf93d8162009-09-22 15:16:02 -0700288static char**
289prepend_title(char** headers) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700290 char* title[] = { EXPAND(RECOVERY_VERSION),
Doug Zongkerd6837852009-06-17 22:07:13 -0700291 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 NULL };
293
Doug Zongkerd6837852009-06-17 22:07:13 -0700294 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700295 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700296 int count = 0;
297 char** p;
298 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700299 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700300
Doug Zongkerf93d8162009-09-22 15:16:02 -0700301 char** new_headers = malloc((count+1) * sizeof(char*));
302 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700303 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700304 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700305 *h = NULL;
306
Doug Zongkerf93d8162009-09-22 15:16:02 -0700307 return new_headers;
308}
309
Koushik K. Duttae9234872010-02-12 00:43:24 -0800310int
Doug Zongkerf93d8162009-09-22 15:16:02 -0700311get_menu_selection(char** headers, char** items, int menu_only) {
312 // throw away keys pressed previously, so user doesn't
313 // accidentally trigger menu items.
314 ui_clear_key_queue();
315
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700316 int item_count = ui_start_menu(headers, items);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317 int selected = 0;
318 int chosen_item = -1;
319
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800320 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322 int visible = ui_text_visible();
323
Doug Zongkerddd6a282009-06-09 12:22:33 -0700324 int action = device_handle_key(key, visible);
325
326 if (action < 0) {
327 switch (action) {
328 case HIGHLIGHT_UP:
329 --selected;
330 selected = ui_menu_select(selected);
331 break;
332 case HIGHLIGHT_DOWN:
333 ++selected;
334 selected = ui_menu_select(selected);
335 break;
336 case SELECT_ITEM:
337 chosen_item = selected;
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700338#ifdef KEY_POWER_IS_SELECT_ITEM
339 if (chosen_item == item_count) {
340 chosen_item = GO_BACK;
341 }
342#endif
Doug Zongkerddd6a282009-06-09 12:22:33 -0700343 break;
344 case NO_ACTION:
345 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800346 case GO_BACK:
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800347 chosen_item = GO_BACK;
348 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800349 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700350 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700351 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700353 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800354
Doug Zongkerf93d8162009-09-22 15:16:02 -0700355 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800356 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700357 return chosen_item;
358}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800359
Doug Zongkerf93d8162009-09-22 15:16:02 -0700360static void
361wipe_data(int confirm) {
362 if (confirm) {
363 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700364
Doug Zongkerf93d8162009-09-22 15:16:02 -0700365 if (title_headers == NULL) {
366 char* headers[] = { "Confirm wipe of all user data?",
367 " THIS CAN NOT BE UNDONE.",
368 "",
369 NULL };
370 title_headers = prepend_title(headers);
371 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800372
Doug Zongkerf93d8162009-09-22 15:16:02 -0700373 char* items[] = { " No",
374 " No",
375 " No",
376 " No",
377 " No",
378 " No",
379 " No",
380 " Yes -- delete all user data", // [7]
381 " No",
382 " No",
383 " No",
384 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800385
Doug Zongkerf93d8162009-09-22 15:16:02 -0700386 int chosen_item = get_menu_selection(title_headers, items, 1);
387 if (chosen_item != 7) {
388 return;
389 }
390 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700391
Doug Zongkerf93d8162009-09-22 15:16:02 -0700392 ui_print("\n-- Wiping data...\n");
393 device_wipe_data();
394 erase_root("DATA:");
Koushik Dutta63e04762010-06-15 12:56:17 -0700395#ifdef HAS_DATADATA
396 erase_root("DATADATA:");
397#endif
Doug Zongkerf93d8162009-09-22 15:16:02 -0700398 erase_root("CACHE:");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700399 erase_root("SDEXT:");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700400 ui_print("Data wipe complete.\n");
401}
402
403static void
404prompt_and_wait()
405{
406 char** headers = prepend_title(MENU_HEADERS);
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700407 ui_print(EXPAND(RECOVERY_VERSION)"\n");
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800408
Doug Zongkerf93d8162009-09-22 15:16:02 -0700409 for (;;) {
410 finish_recovery(NULL);
411 ui_reset_progress();
412
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800413 allow_display_toggle = 1;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700414 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800415 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700416
417 // device-specific code may take some action here. It may
418 // return one of the core actions handled in the switch
419 // statement below.
420 chosen_item = device_perform_action(chosen_item);
421
422 switch (chosen_item) {
423 case ITEM_REBOOT:
424 return;
425
426 case ITEM_WIPE_DATA:
427 wipe_data(ui_text_visible());
428 if (!ui_text_visible()) return;
429 break;
430
431 case ITEM_WIPE_CACHE:
432 ui_print("\n-- Wiping cache...\n");
433 erase_root("CACHE:");
434 ui_print("Cache wipe complete.\n");
435 if (!ui_text_visible()) return;
436 break;
437
438 case ITEM_APPLY_SDCARD:
439 ui_print("\n-- Install from sdcard...\n");
440 set_sdcard_update_bootloader_message();
441 int status = install_package(SDCARD_PACKAGE_FILE);
442 if (status != INSTALL_SUCCESS) {
443 ui_set_background(BACKGROUND_ICON_ERROR);
444 ui_print("Installation aborted.\n");
445 } else if (!ui_text_visible()) {
446 return; // reboot if logs aren't visible
447 } else {
448 if (firmware_update_pending()) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700449 ui_print("\nReboot via menu to complete\n"
450 "installation.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700451 } else {
Doug Zongker07e1dca2009-05-28 19:02:45 -0700452 ui_print("\nInstall from sdcard complete.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800453 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700454 }
455 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800456 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800457 show_install_update_menu();
458 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700459 case ITEM_NANDROID:
460 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800461 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700462 case ITEM_PARTITION:
463 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700464 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700465 case ITEM_ADVANCED:
466 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800467 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468 }
469 }
470}
471
472static void
473print_property(const char *key, const char *name, void *cookie)
474{
475 fprintf(stderr, "%s=%s\n", key, name);
476}
477
478int
479main(int argc, char **argv)
480{
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800481 if (strstr(argv[0], "recovery") == NULL)
482 {
483 if (strstr(argv[0], "flash_image") != NULL)
484 return flash_image_main(argc, argv);
485 if (strstr(argv[0], "dump_image") != NULL)
486 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700487 if (strstr(argv[0], "erase_image") != NULL)
488 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800489 if (strstr(argv[0], "mkyaffs2image") != NULL)
490 return mkyaffs2image_main(argc, argv);
491 if (strstr(argv[0], "unyaffs") != NULL)
492 return unyaffs_main(argc, argv);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800493 if (strstr(argv[0], "amend"))
494 return amend_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800495 return busybox_driver(argc, argv);
496 }
Koushik Dutta52d3f202010-06-21 12:11:13 -0700497 LOGI(EXPAND(RECOVERY_VERSION)"\n");
Koushik Duttaa6522b32010-06-20 13:16:06 -0700498 create_fstab();
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700499 __system("/sbin/postrecoveryboot.sh");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700500
Koushik K. Dutta03173782010-02-26 14:14:23 -0800501 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800502 time_t start = time(NULL);
503
504 // If these fail, there's not really anywhere to complain...
505 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
506 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
507 fprintf(stderr, "Starting recovery on %s", ctime(&start));
508
509 ui_init();
510 get_args(&argc, &argv);
511
512 int previous_runs = 0;
513 const char *send_intent = NULL;
514 const char *update_package = NULL;
515 int wipe_data = 0, wipe_cache = 0;
516
517 int arg;
518 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
519 switch (arg) {
520 case 'p': previous_runs = atoi(optarg); break;
521 case 's': send_intent = optarg; break;
522 case 'u': update_package = optarg; break;
523 case 'w': wipe_data = wipe_cache = 1; break;
524 case 'c': wipe_cache = 1; break;
525 case '?':
526 LOGE("Invalid command argument\n");
527 continue;
528 }
529 }
530
531 fprintf(stderr, "Command:");
532 for (arg = 0; arg < argc; arg++) {
533 fprintf(stderr, " \"%s\"", argv[arg]);
534 }
535 fprintf(stderr, "\n\n");
536
537 property_list(print_property, NULL);
538 fprintf(stderr, "\n");
539
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800540 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800541
542 RecoveryCommandContext ctx = { NULL };
543 if (register_update_commands(&ctx)) {
544 LOGE("Can't install update commands\n");
545 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800546
547 if (update_package != NULL) {
Chris Soyarsa1749d92010-02-26 02:45:55 -0500548 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800549 status = install_package(update_package);
550 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700551 } else if (wipe_data) {
552 if (device_wipe_data()) status = INSTALL_ERROR;
553 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800554 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
555 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700556 } else if (wipe_cache) {
557 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
558 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800559 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700560 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800561 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800562 // we are starting up in user initiated recovery here
563 // let's set up some default options
564 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800565 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800566 is_user_initiated_recovery = 1;
567 ui_set_show_text(1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800568
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800569 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700570 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700571 int ret;
572 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800573 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800574 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800575 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700576 else {
577 handle_failure(ret);
578 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700579 } else {
580 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800581 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800582 }
583
Koushik K. Dutta03173782010-02-26 14:14:23 -0800584 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800585 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
586
587 // If there is a radio image pending, reboot now to install it.
588 maybe_install_firmware_update(send_intent);
589
590 // Otherwise, get ready to boot the main system...
591 finish_recovery(send_intent);
592 ui_print("Rebooting...\n");
593 sync();
594 reboot(RB_AUTOBOOT);
595 return EXIT_SUCCESS;
596}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800597
598int get_allow_toggle_display() {
599 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500600}