blob: 3d0f9d05a3f629991088bf0dd547c10b181d7587 [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>
30
31#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032#include "common.h"
33#include "cutils/properties.h"
34#include "firmware.h"
35#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
41static const struct option OPTIONS[] = {
42 { "send_intent", required_argument, NULL, 's' },
43 { "update_package", required_argument, NULL, 'u' },
44 { "wipe_data", no_argument, NULL, 'w' },
45 { "wipe_cache", no_argument, NULL, 'c' },
46};
47
48static const char *COMMAND_FILE = "CACHE:recovery/command";
49static const char *INTENT_FILE = "CACHE:recovery/intent";
50static const char *LOG_FILE = "CACHE:recovery/log";
51static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
52static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
53
54/*
55 * The recovery tool communicates with the main system through /cache files.
56 * /cache/recovery/command - INPUT - command line for tool, one arg per line
57 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
58 * /cache/recovery/intent - OUTPUT - intent that was passed in
59 *
60 * The arguments which may be supplied in the recovery.command file:
61 * --send_intent=anystring - write the text out to recovery.intent
62 * --update_package=root:path - verify install an OTA package file
63 * --wipe_data - erase user data (and cache), then reboot
64 * --wipe_cache - wipe cache (but not user data), then reboot
65 *
66 * After completing, we remove /cache/recovery/command and reboot.
67 * Arguments may also be supplied in the bootloader control block (BCB).
68 * These important scenarios must be safely restartable at any point:
69 *
70 * FACTORY RESET
71 * 1. user selects "factory reset"
72 * 2. main system writes "--wipe_data" to /cache/recovery/command
73 * 3. main system reboots into recovery
74 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
75 * -- after this, rebooting will restart the erase --
76 * 5. erase_root() reformats /data
77 * 6. erase_root() reformats /cache
78 * 7. finish_recovery() erases BCB
79 * -- after this, rebooting will restart the main system --
80 * 8. main() calls reboot() to boot main system
81 *
82 * OTA INSTALL
83 * 1. main system downloads OTA package to /cache/some-filename.zip
84 * 2. main system writes "--update_package=CACHE:some-filename.zip"
85 * 3. main system reboots into recovery
86 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
87 * -- after this, rebooting will attempt to reinstall the update --
88 * 5. install_package() attempts to install the update
89 * NOTE: the package install must itself be restartable from any point
90 * 6. finish_recovery() erases BCB
91 * -- after this, rebooting will (try to) restart the main system --
92 * 7. ** if install failed **
93 * 7a. prompt_and_wait() shows an error icon and waits for the user
94 * 7b; the user reboots (pulling the battery, etc) into the main system
95 * 8. main() calls maybe_install_firmware_update()
96 * ** if the update contained radio/hboot firmware **:
97 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
98 * -- after this, rebooting will reformat cache & restart main system --
99 * 8b. m_i_f_u() writes firmware image into raw cache partition
100 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
101 * -- after this, rebooting will attempt to reinstall firmware --
102 * 8d. bootloader tries to flash firmware
103 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
104 * -- after this, rebooting will reformat cache & restart main system --
105 * 8f. erase_root() reformats /cache
106 * 8g. finish_recovery() erases BCB
107 * -- after this, rebooting will (try to) restart the main system --
108 * 9. main() calls reboot() to boot main system
109 */
110
111static const int MAX_ARG_LENGTH = 4096;
112static const int MAX_ARGS = 100;
113
114// open a file given in root:path format, mounting partitions as necessary
115static FILE*
116fopen_root_path(const char *root_path, const char *mode) {
117 if (ensure_root_path_mounted(root_path) != 0) {
118 LOGE("Can't mount %s\n", root_path);
119 return NULL;
120 }
121
122 char path[PATH_MAX] = "";
123 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
124 LOGE("Bad path %s\n", root_path);
125 return NULL;
126 }
127
128 // When writing, try to create the containing directory, if necessary.
129 // Use generous permissions, the system (init.rc) will reset them.
130 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
131
132 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 return fp;
134}
135
136// close a file, log an error if the error indicator is set
137static void
138check_and_fclose(FILE *fp, const char *name) {
139 fflush(fp);
140 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
141 fclose(fp);
142}
143
144// command line args come from, in decreasing precedence:
145// - the actual command line
146// - the bootloader control block (one per line, after "recovery")
147// - the contents of COMMAND_FILE (one per line)
148static void
149get_args(int *argc, char ***argv) {
150 struct bootloader_message boot;
151 memset(&boot, 0, sizeof(boot));
152 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
153
154 if (boot.command[0] != 0 && boot.command[0] != 255) {
155 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
156 }
157
158 if (boot.status[0] != 0 && boot.status[0] != 255) {
159 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
160 }
161
162 // --- if arguments weren't supplied, look in the bootloader control block
163 if (*argc <= 1) {
164 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
165 const char *arg = strtok(boot.recovery, "\n");
166 if (arg != NULL && !strcmp(arg, "recovery")) {
167 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
168 (*argv)[0] = strdup(arg);
169 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
170 if ((arg = strtok(NULL, "\n")) == NULL) break;
171 (*argv)[*argc] = strdup(arg);
172 }
173 LOGI("Got arguments from boot message\n");
174 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
175 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
176 }
177 }
178
179 // --- if that doesn't work, try the command file
180 if (*argc <= 1) {
181 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
182 if (fp != NULL) {
183 char *argv0 = (*argv)[0];
184 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
185 (*argv)[0] = argv0; // use the same program name
186
187 char buf[MAX_ARG_LENGTH];
188 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
189 if (!fgets(buf, sizeof(buf), fp)) break;
190 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
191 }
192
193 check_and_fclose(fp, COMMAND_FILE);
194 LOGI("Got arguments from %s\n", COMMAND_FILE);
195 }
196 }
197
198 // --> write the arguments we have back into the bootloader control block
199 // always boot into recovery after this (until finish_recovery() is called)
200 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
201 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
202 int i;
203 for (i = 1; i < *argc; ++i) {
204 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
205 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
206 }
207 set_bootloader_message(&boot);
208}
209
Doug Zongker34c98df2009-08-18 12:05:45 -0700210static void
211set_sdcard_update_bootloader_message()
212{
213 struct bootloader_message boot;
214 memset(&boot, 0, sizeof(boot));
215 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
216 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
217 set_bootloader_message(&boot);
218}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219
220// clear the recovery command and prepare to boot a (hopefully working) system,
221// copy our log file to cache as well (for the system to read), and
222// record any intent we were asked to communicate back to the system.
223// this function is idempotent: call it as many times as you like.
224static void
225finish_recovery(const char *send_intent)
226{
227 // By this point, we're ready to return to the main system...
228 if (send_intent != NULL) {
229 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000230 if (fp == NULL) {
231 LOGE("Can't open %s\n", INTENT_FILE);
232 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800233 fputs(send_intent, fp);
234 check_and_fclose(fp, INTENT_FILE);
235 }
236 }
237
238 // Copy logs to cache so the system can find out what happened.
239 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000240 if (log == NULL) {
241 LOGE("Can't open %s\n", LOG_FILE);
242 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
244 if (tmplog == NULL) {
245 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
246 } else {
247 static long tmplog_offset = 0;
248 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
249 char buf[4096];
250 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
251 tmplog_offset = ftell(tmplog);
252 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
253 }
254 check_and_fclose(log, LOG_FILE);
255 }
256
257 // Reset the bootloader message to revert to a normal main system boot.
258 struct bootloader_message boot;
259 memset(&boot, 0, sizeof(boot));
260 set_bootloader_message(&boot);
261
262 // Remove the command file, so recovery won't repeat indefinitely.
263 char path[PATH_MAX] = "";
264 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
265 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
266 (unlink(path) && errno != ENOENT)) {
267 LOGW("Can't unlink %s\n", COMMAND_FILE);
268 }
269
270 sync(); // For good measure.
271}
272
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800273static int
274erase_root(const char *root)
275{
276 ui_set_background(BACKGROUND_ICON_INSTALLING);
277 ui_show_indeterminate_progress();
278 ui_print("Formatting %s...\n", root);
279 return format_root_device(root);
280}
281
Doug Zongkerf93d8162009-09-22 15:16:02 -0700282static char**
283prepend_title(char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700284 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700285 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700286 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287 NULL };
288
Doug Zongkerd6837852009-06-17 22:07:13 -0700289 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700290 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700291 int count = 0;
292 char** p;
293 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700294 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700295
Doug Zongkerf93d8162009-09-22 15:16:02 -0700296 char** new_headers = malloc((count+1) * sizeof(char*));
297 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700298 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700299 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700300 *h = NULL;
301
Doug Zongkerf93d8162009-09-22 15:16:02 -0700302 return new_headers;
303}
304
305static int
306get_menu_selection(char** headers, char** items, int menu_only) {
307 // throw away keys pressed previously, so user doesn't
308 // accidentally trigger menu items.
309 ui_clear_key_queue();
310
311 ui_start_menu(headers, items);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312 int selected = 0;
313 int chosen_item = -1;
314
Doug Zongkerf93d8162009-09-22 15:16:02 -0700315 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800316 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317 int visible = ui_text_visible();
318
Doug Zongkerddd6a282009-06-09 12:22:33 -0700319 int action = device_handle_key(key, visible);
320
321 if (action < 0) {
322 switch (action) {
323 case HIGHLIGHT_UP:
324 --selected;
325 selected = ui_menu_select(selected);
326 break;
327 case HIGHLIGHT_DOWN:
328 ++selected;
329 selected = ui_menu_select(selected);
330 break;
331 case SELECT_ITEM:
332 chosen_item = selected;
333 break;
334 case NO_ACTION:
335 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800336 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700337 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700338 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800339 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700340 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800341
Doug Zongkerf93d8162009-09-22 15:16:02 -0700342 ui_end_menu();
343 return chosen_item;
344}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345
Doug Zongkerf93d8162009-09-22 15:16:02 -0700346static void
347wipe_data(int confirm) {
348 if (confirm) {
349 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700350
Doug Zongkerf93d8162009-09-22 15:16:02 -0700351 if (title_headers == NULL) {
352 char* headers[] = { "Confirm wipe of all user data?",
353 " THIS CAN NOT BE UNDONE.",
354 "",
355 NULL };
356 title_headers = prepend_title(headers);
357 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358
Doug Zongkerf93d8162009-09-22 15:16:02 -0700359 char* items[] = { " No",
360 " No",
361 " No",
362 " No",
363 " No",
364 " No",
365 " No",
366 " Yes -- delete all user data", // [7]
367 " No",
368 " No",
369 " No",
370 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800371
Doug Zongkerf93d8162009-09-22 15:16:02 -0700372 int chosen_item = get_menu_selection(title_headers, items, 1);
373 if (chosen_item != 7) {
374 return;
375 }
376 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700377
Doug Zongkerf93d8162009-09-22 15:16:02 -0700378 ui_print("\n-- Wiping data...\n");
379 device_wipe_data();
380 erase_root("DATA:");
381 erase_root("CACHE:");
382 ui_print("Data wipe complete.\n");
383}
384
385static void
386prompt_and_wait()
387{
388 char** headers = prepend_title(MENU_HEADERS);
389
390 for (;;) {
391 finish_recovery(NULL);
392 ui_reset_progress();
393
394 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0);
395
396 // device-specific code may take some action here. It may
397 // return one of the core actions handled in the switch
398 // statement below.
399 chosen_item = device_perform_action(chosen_item);
400
401 switch (chosen_item) {
402 case ITEM_REBOOT:
403 return;
404
405 case ITEM_WIPE_DATA:
406 wipe_data(ui_text_visible());
407 if (!ui_text_visible()) return;
408 break;
409
410 case ITEM_WIPE_CACHE:
411 ui_print("\n-- Wiping cache...\n");
412 erase_root("CACHE:");
413 ui_print("Cache wipe complete.\n");
414 if (!ui_text_visible()) return;
415 break;
416
417 case ITEM_APPLY_SDCARD:
418 ui_print("\n-- Install from sdcard...\n");
419 set_sdcard_update_bootloader_message();
420 int status = install_package(SDCARD_PACKAGE_FILE);
421 if (status != INSTALL_SUCCESS) {
422 ui_set_background(BACKGROUND_ICON_ERROR);
423 ui_print("Installation aborted.\n");
424 } else if (!ui_text_visible()) {
425 return; // reboot if logs aren't visible
426 } else {
427 if (firmware_update_pending()) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700428 ui_print("\nReboot via menu to complete\n"
429 "installation.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700430 } else {
Doug Zongker07e1dca2009-05-28 19:02:45 -0700431 ui_print("\nInstall from sdcard complete.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800432 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700433 }
434 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800435 }
436 }
437}
438
439static void
440print_property(const char *key, const char *name, void *cookie)
441{
442 fprintf(stderr, "%s=%s\n", key, name);
443}
444
445int
446main(int argc, char **argv)
447{
448 time_t start = time(NULL);
449
450 // If these fail, there's not really anywhere to complain...
451 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
452 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
453 fprintf(stderr, "Starting recovery on %s", ctime(&start));
454
455 ui_init();
456 get_args(&argc, &argv);
457
458 int previous_runs = 0;
459 const char *send_intent = NULL;
460 const char *update_package = NULL;
461 int wipe_data = 0, wipe_cache = 0;
462
463 int arg;
464 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
465 switch (arg) {
466 case 'p': previous_runs = atoi(optarg); break;
467 case 's': send_intent = optarg; break;
468 case 'u': update_package = optarg; break;
469 case 'w': wipe_data = wipe_cache = 1; break;
470 case 'c': wipe_cache = 1; break;
471 case '?':
472 LOGE("Invalid command argument\n");
473 continue;
474 }
475 }
476
477 fprintf(stderr, "Command:");
478 for (arg = 0; arg < argc; arg++) {
479 fprintf(stderr, " \"%s\"", argv[arg]);
480 }
481 fprintf(stderr, "\n\n");
482
483 property_list(print_property, NULL);
484 fprintf(stderr, "\n");
485
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800486 int status = INSTALL_SUCCESS;
487
488 if (update_package != NULL) {
489 status = install_package(update_package);
490 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700491 } else if (wipe_data) {
492 if (device_wipe_data()) status = INSTALL_ERROR;
493 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
495 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700496 } else if (wipe_cache) {
497 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
498 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800499 } else {
500 status = INSTALL_ERROR; // No command specified
501 }
502
503 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
504 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
505
506 // If there is a radio image pending, reboot now to install it.
507 maybe_install_firmware_update(send_intent);
508
509 // Otherwise, get ready to boot the main system...
510 finish_recovery(send_intent);
511 ui_print("Rebooting...\n");
512 sync();
513 reboot(RB_AUTOBOOT);
514 return EXIT_SUCCESS;
515}