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