blob: ce4405b916e3896d59a47d583e583cd1f4669e18 [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));
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700160#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700162#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163
164 if (boot.command[0] != 0 && boot.command[0] != 255) {
165 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
166 }
167
168 if (boot.status[0] != 0 && boot.status[0] != 255) {
169 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
170 }
171
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700172 struct stat file_info;
173
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700175 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800176 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
177 const char *arg = strtok(boot.recovery, "\n");
178 if (arg != NULL && !strcmp(arg, "recovery")) {
179 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
180 (*argv)[0] = strdup(arg);
181 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
182 if ((arg = strtok(NULL, "\n")) == NULL) break;
183 (*argv)[*argc] = strdup(arg);
184 }
185 LOGI("Got arguments from boot message\n");
186 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
187 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
188 }
189 }
190
191 // --- if that doesn't work, try the command file
192 if (*argc <= 1) {
193 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
194 if (fp != NULL) {
195 char *argv0 = (*argv)[0];
196 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
197 (*argv)[0] = argv0; // use the same program name
198
199 char buf[MAX_ARG_LENGTH];
200 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
201 if (!fgets(buf, sizeof(buf), fp)) break;
202 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
203 }
204
205 check_and_fclose(fp, COMMAND_FILE);
206 LOGI("Got arguments from %s\n", COMMAND_FILE);
207 }
208 }
209
210 // --> write the arguments we have back into the bootloader control block
211 // always boot into recovery after this (until finish_recovery() is called)
212 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
213 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
214 int i;
215 for (i = 1; i < *argc; ++i) {
216 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
217 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
218 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700219#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700221#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222}
223
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700224#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik K. Duttae9234872010-02-12 00:43:24 -0800225void
Doug Zongker34c98df2009-08-18 12:05:45 -0700226set_sdcard_update_bootloader_message()
227{
228 struct bootloader_message boot;
229 memset(&boot, 0, sizeof(boot));
230 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
231 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
232 set_bootloader_message(&boot);
233}
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700234#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800235
236// clear the recovery command and prepare to boot a (hopefully working) system,
237// copy our log file to cache as well (for the system to read), and
238// record any intent we were asked to communicate back to the system.
239// this function is idempotent: call it as many times as you like.
240static void
241finish_recovery(const char *send_intent)
242{
243 // By this point, we're ready to return to the main system...
244 if (send_intent != NULL) {
245 FILE *fp = fopen_root_path(INTENT_FILE, "w");
246 if (fp != NULL) {
247 fputs(send_intent, fp);
248 check_and_fclose(fp, INTENT_FILE);
249 }
250 }
251
252 // Copy logs to cache so the system can find out what happened.
253 FILE *log = fopen_root_path(LOG_FILE, "a");
254 if (log != NULL) {
255 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
256 if (tmplog == NULL) {
257 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
258 } else {
259 static long tmplog_offset = 0;
260 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
261 char buf[4096];
262 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
263 tmplog_offset = ftell(tmplog);
264 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
265 }
266 check_and_fclose(log, LOG_FILE);
267 }
268
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700269#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 // Reset the bootloader message to revert to a normal main system boot.
271 struct bootloader_message boot;
272 memset(&boot, 0, sizeof(boot));
273 set_bootloader_message(&boot);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700274#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275
276 // Remove the command file, so recovery won't repeat indefinitely.
277 char path[PATH_MAX] = "";
278 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
279 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
280 (unlink(path) && errno != ENOENT)) {
281 LOGW("Can't unlink %s\n", COMMAND_FILE);
282 }
283
284 sync(); // For good measure.
285}
286
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287static int
288erase_root(const char *root)
289{
290 ui_set_background(BACKGROUND_ICON_INSTALLING);
291 ui_show_indeterminate_progress();
292 ui_print("Formatting %s...\n", root);
293 return format_root_device(root);
294}
295
Doug Zongkerf93d8162009-09-22 15:16:02 -0700296static char**
297prepend_title(char** headers) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700298 char* title[] = { EXPAND(RECOVERY_VERSION),
Doug Zongkerd6837852009-06-17 22:07:13 -0700299 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300 NULL };
301
Doug Zongkerd6837852009-06-17 22:07:13 -0700302 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700303 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700304 int count = 0;
305 char** p;
306 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700307 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700308
Doug Zongkerf93d8162009-09-22 15:16:02 -0700309 char** new_headers = malloc((count+1) * sizeof(char*));
310 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700311 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700312 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700313 *h = NULL;
314
Doug Zongkerf93d8162009-09-22 15:16:02 -0700315 return new_headers;
316}
317
Koushik K. Duttae9234872010-02-12 00:43:24 -0800318int
Doug Zongkerf93d8162009-09-22 15:16:02 -0700319get_menu_selection(char** headers, char** items, int menu_only) {
320 // throw away keys pressed previously, so user doesn't
321 // accidentally trigger menu items.
322 ui_clear_key_queue();
323
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700324 int item_count = ui_start_menu(headers, items);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800325 int selected = 0;
326 int chosen_item = -1;
327
Koushik Dutta1bf4f692010-07-14 18:37:33 -0700328 // Some users with dead enter keys need a way to turn on power to select.
329 // Jiggering across the wrapping menu is one "secret" way to enable it.
330 // We can't rely on /cache or /sdcard since they may not be available.
331 int wrap_count = 0;
332
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800333 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800334 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335 int visible = ui_text_visible();
336
Doug Zongkerddd6a282009-06-09 12:22:33 -0700337 int action = device_handle_key(key, visible);
338
Koushik Dutta1bf4f692010-07-14 18:37:33 -0700339 int old_selected = selected;
340
Doug Zongkerddd6a282009-06-09 12:22:33 -0700341 if (action < 0) {
342 switch (action) {
343 case HIGHLIGHT_UP:
344 --selected;
345 selected = ui_menu_select(selected);
346 break;
347 case HIGHLIGHT_DOWN:
348 ++selected;
349 selected = ui_menu_select(selected);
350 break;
351 case SELECT_ITEM:
352 chosen_item = selected;
Koushik Dutta1bf4f692010-07-14 18:37:33 -0700353 if (ui_get_showing_back_button()) {
354 if (chosen_item == item_count) {
355 chosen_item = GO_BACK;
356 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700357 }
Doug Zongkerddd6a282009-06-09 12:22:33 -0700358 break;
359 case NO_ACTION:
360 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800361 case GO_BACK:
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800362 chosen_item = GO_BACK;
363 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800364 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700365 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700366 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800367 }
Koushik Dutta1bf4f692010-07-14 18:37:33 -0700368
369 if (abs(selected - old_selected) > 1) {
370 wrap_count++;
371 if (wrap_count == 3) {
372 wrap_count = 0;
373 if (ui_get_showing_back_button()) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700374 ui_print("Back menu button disabled.\n");
Koushik Dutta1bf4f692010-07-14 18:37:33 -0700375 ui_set_showing_back_button(0);
376 }
377 else {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700378 ui_print("Back menu button enabled.\n");
Koushik Dutta1bf4f692010-07-14 18:37:33 -0700379 ui_set_showing_back_button(1);
380 }
381 }
382 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700383 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800384
Doug Zongkerf93d8162009-09-22 15:16:02 -0700385 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800386 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700387 return chosen_item;
388}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800389
Doug Zongkerf93d8162009-09-22 15:16:02 -0700390static void
391wipe_data(int confirm) {
392 if (confirm) {
393 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700394
Doug Zongkerf93d8162009-09-22 15:16:02 -0700395 if (title_headers == NULL) {
396 char* headers[] = { "Confirm wipe of all user data?",
397 " THIS CAN NOT BE UNDONE.",
398 "",
399 NULL };
400 title_headers = prepend_title(headers);
401 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800402
Doug Zongkerf93d8162009-09-22 15:16:02 -0700403 char* items[] = { " No",
404 " No",
405 " No",
406 " No",
407 " No",
408 " No",
409 " No",
410 " Yes -- delete all user data", // [7]
411 " No",
412 " No",
413 " No",
414 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800415
Doug Zongkerf93d8162009-09-22 15:16:02 -0700416 int chosen_item = get_menu_selection(title_headers, items, 1);
417 if (chosen_item != 7) {
418 return;
419 }
420 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700421
Doug Zongkerf93d8162009-09-22 15:16:02 -0700422 ui_print("\n-- Wiping data...\n");
423 device_wipe_data();
424 erase_root("DATA:");
Koushik Dutta63e04762010-06-15 12:56:17 -0700425#ifdef HAS_DATADATA
426 erase_root("DATADATA:");
427#endif
Doug Zongkerf93d8162009-09-22 15:16:02 -0700428 erase_root("CACHE:");
Koushik Dutta2f73e582010-04-18 16:00:21 -0700429 erase_root("SDEXT:");
Koushik Dutta062d6b02010-07-03 13:54:21 -0700430 erase_root("SDCARD:/.android_secure");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700431 ui_print("Data wipe complete.\n");
432}
433
434static void
435prompt_and_wait()
436{
437 char** headers = prepend_title(MENU_HEADERS);
Koushik K. Dutta261dde92010-02-25 16:51:45 -0800438
Doug Zongkerf93d8162009-09-22 15:16:02 -0700439 for (;;) {
440 finish_recovery(NULL);
441 ui_reset_progress();
442
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800443 allow_display_toggle = 1;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700444 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800445 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700446
447 // device-specific code may take some action here. It may
448 // return one of the core actions handled in the switch
449 // statement below.
450 chosen_item = device_perform_action(chosen_item);
451
452 switch (chosen_item) {
453 case ITEM_REBOOT:
454 return;
455
456 case ITEM_WIPE_DATA:
457 wipe_data(ui_text_visible());
458 if (!ui_text_visible()) return;
459 break;
460
461 case ITEM_WIPE_CACHE:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700462 if (confirm_selection("Confirm wipe?", "Yes - Wipe Cache"))
463 {
464 ui_print("\n-- Wiping cache...\n");
465 erase_root("CACHE:");
466 ui_print("Cache wipe complete.\n");
467 if (!ui_text_visible()) return;
468 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700469 break;
470
471 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700472 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
473 {
474 ui_print("\n-- Install from sdcard...\n");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700475#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik Duttad63eaef2010-07-14 21:01:21 -0700476 set_sdcard_update_bootloader_message();
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700477#endif
Koushik Duttad63eaef2010-07-14 21:01:21 -0700478 int status = install_package(SDCARD_PACKAGE_FILE);
479 if (status != INSTALL_SUCCESS) {
480 ui_set_background(BACKGROUND_ICON_ERROR);
481 ui_print("Installation aborted.\n");
482 } else if (!ui_text_visible()) {
483 return; // reboot if logs aren't visible
Doug Zongkerf93d8162009-09-22 15:16:02 -0700484 } else {
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700485#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik Duttad63eaef2010-07-14 21:01:21 -0700486 if (firmware_update_pending()) {
487 ui_print("\nReboot via menu to complete\n"
488 "installation.\n");
489 } else {
490 ui_print("\nInstall from sdcard complete.\n");
491 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700492#else
493 ui_print("\nInstall from sdcard complete.\n");
494#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700496 }
497 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800498 case ITEM_INSTALL_ZIP:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800499 show_install_update_menu();
500 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700501 case ITEM_NANDROID:
502 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800503 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700504 case ITEM_PARTITION:
505 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700506 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700507 case ITEM_ADVANCED:
508 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800509 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800510 }
511 }
512}
513
514static void
515print_property(const char *key, const char *name, void *cookie)
516{
517 fprintf(stderr, "%s=%s\n", key, name);
518}
519
520int
521main(int argc, char **argv)
522{
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800523 if (strstr(argv[0], "recovery") == NULL)
524 {
525 if (strstr(argv[0], "flash_image") != NULL)
526 return flash_image_main(argc, argv);
527 if (strstr(argv[0], "dump_image") != NULL)
528 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700529 if (strstr(argv[0], "erase_image") != NULL)
530 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800531 if (strstr(argv[0], "mkyaffs2image") != NULL)
532 return mkyaffs2image_main(argc, argv);
533 if (strstr(argv[0], "unyaffs") != NULL)
534 return unyaffs_main(argc, argv);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800535 if (strstr(argv[0], "amend"))
536 return amend_main(argc, argv);
Koushik Dutta08370912010-06-26 12:25:02 -0700537 if (strstr(argv[0], "nandroid"))
538 return nandroid_main(argc, argv);
Koushik Dutta852bb422010-07-24 11:18:00 -0700539 if (strstr(argv[0], "reboot"))
540 return reboot_main(argc, argv);
541 if (strstr(argv[0], "setprop"))
542 return setprop_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800543 return busybox_driver(argc, argv);
544 }
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700545 __system("/sbin/postrecoveryboot.sh");
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700546 create_fstab();
Koushik Duttafd1579b2010-05-01 12:46:55 -0700547
Koushik K. Dutta03173782010-02-26 14:14:23 -0800548 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800549 time_t start = time(NULL);
550
551 // If these fail, there's not really anywhere to complain...
552 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
553 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
554 fprintf(stderr, "Starting recovery on %s", ctime(&start));
555
556 ui_init();
Koushik Dutta6440ed52010-07-01 12:52:34 -0700557 ui_print(EXPAND(RECOVERY_VERSION)"\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800558 get_args(&argc, &argv);
559
560 int previous_runs = 0;
561 const char *send_intent = NULL;
562 const char *update_package = NULL;
563 int wipe_data = 0, wipe_cache = 0;
564
565 int arg;
566 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
567 switch (arg) {
568 case 'p': previous_runs = atoi(optarg); break;
569 case 's': send_intent = optarg; break;
570 case 'u': update_package = optarg; break;
571 case 'w': wipe_data = wipe_cache = 1; break;
572 case 'c': wipe_cache = 1; break;
573 case '?':
574 LOGE("Invalid command argument\n");
575 continue;
576 }
577 }
578
579 fprintf(stderr, "Command:");
580 for (arg = 0; arg < argc; arg++) {
581 fprintf(stderr, " \"%s\"", argv[arg]);
582 }
583 fprintf(stderr, "\n\n");
584
585 property_list(print_property, NULL);
586 fprintf(stderr, "\n");
587
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800588 int status = INSTALL_SUCCESS;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800589
590 RecoveryCommandContext ctx = { NULL };
591 if (register_update_commands(&ctx)) {
592 LOGE("Can't install update commands\n");
593 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800594
595 if (update_package != NULL) {
Chris Soyarsa1749d92010-02-26 02:45:55 -0500596 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800597 status = install_package(update_package);
598 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700599 } else if (wipe_data) {
600 if (device_wipe_data()) status = INSTALL_ERROR;
601 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800602 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
603 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700604 } else if (wipe_cache) {
605 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
606 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800607 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700608 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800609 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800610 // we are starting up in user initiated recovery here
611 // let's set up some default options
612 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800613 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800614 is_user_initiated_recovery = 1;
615 ui_set_show_text(1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800616
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800617 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700618 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700619 int ret;
620 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800621 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800622 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800623 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700624 else {
625 handle_failure(ret);
626 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700627 } else {
628 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800629 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800630 }
631
Koushik K. Dutta03173782010-02-26 14:14:23 -0800632 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) ui_set_background(BACKGROUND_ICON_ERROR);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800633 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
634
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700635#ifndef BOARD_HAS_NO_MISC_PARTITION
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800636 // If there is a radio image pending, reboot now to install it.
637 maybe_install_firmware_update(send_intent);
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700638#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800639
640 // Otherwise, get ready to boot the main system...
641 finish_recovery(send_intent);
642 ui_print("Rebooting...\n");
643 sync();
644 reboot(RB_AUTOBOOT);
645 return EXIT_SUCCESS;
646}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800647
648int get_allow_toggle_display() {
649 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500650}