blob: 499fda574ccd34d944010d1533683f4951f28c90 [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"
32#include "commands.h"
33#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
42static const struct option OPTIONS[] = {
43 { "send_intent", required_argument, NULL, 's' },
44 { "update_package", required_argument, NULL, 'u' },
45 { "wipe_data", no_argument, NULL, 'w' },
46 { "wipe_cache", no_argument, NULL, 'c' },
47};
48
49static const char *COMMAND_FILE = "CACHE:recovery/command";
50static const char *INTENT_FILE = "CACHE:recovery/intent";
51static const char *LOG_FILE = "CACHE:recovery/log";
52static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
53static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
54
55/*
56 * The recovery tool communicates with the main system through /cache files.
57 * /cache/recovery/command - INPUT - command line for tool, one arg per line
58 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
59 * /cache/recovery/intent - OUTPUT - intent that was passed in
60 *
61 * The arguments which may be supplied in the recovery.command file:
62 * --send_intent=anystring - write the text out to recovery.intent
63 * --update_package=root:path - verify install an OTA package file
64 * --wipe_data - erase user data (and cache), then reboot
65 * --wipe_cache - wipe cache (but not user data), then reboot
66 *
67 * After completing, we remove /cache/recovery/command and reboot.
68 * Arguments may also be supplied in the bootloader control block (BCB).
69 * These important scenarios must be safely restartable at any point:
70 *
71 * FACTORY RESET
72 * 1. user selects "factory reset"
73 * 2. main system writes "--wipe_data" to /cache/recovery/command
74 * 3. main system reboots into recovery
75 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
76 * -- after this, rebooting will restart the erase --
77 * 5. erase_root() reformats /data
78 * 6. erase_root() reformats /cache
79 * 7. finish_recovery() erases BCB
80 * -- after this, rebooting will restart the main system --
81 * 8. main() calls reboot() to boot main system
82 *
83 * OTA INSTALL
84 * 1. main system downloads OTA package to /cache/some-filename.zip
85 * 2. main system writes "--update_package=CACHE:some-filename.zip"
86 * 3. main system reboots into recovery
87 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
88 * -- after this, rebooting will attempt to reinstall the update --
89 * 5. install_package() attempts to install the update
90 * NOTE: the package install must itself be restartable from any point
91 * 6. finish_recovery() erases BCB
92 * -- after this, rebooting will (try to) restart the main system --
93 * 7. ** if install failed **
94 * 7a. prompt_and_wait() shows an error icon and waits for the user
95 * 7b; the user reboots (pulling the battery, etc) into the main system
96 * 8. main() calls maybe_install_firmware_update()
97 * ** if the update contained radio/hboot firmware **:
98 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
99 * -- after this, rebooting will reformat cache & restart main system --
100 * 8b. m_i_f_u() writes firmware image into raw cache partition
101 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
102 * -- after this, rebooting will attempt to reinstall firmware --
103 * 8d. bootloader tries to flash firmware
104 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
105 * -- after this, rebooting will reformat cache & restart main system --
106 * 8f. erase_root() reformats /cache
107 * 8g. finish_recovery() erases BCB
108 * -- after this, rebooting will (try to) restart the main system --
109 * 9. main() calls reboot() to boot main system
110 */
111
112static const int MAX_ARG_LENGTH = 4096;
113static const int MAX_ARGS = 100;
114
115// open a file given in root:path format, mounting partitions as necessary
116static FILE*
117fopen_root_path(const char *root_path, const char *mode) {
118 if (ensure_root_path_mounted(root_path) != 0) {
119 LOGE("Can't mount %s\n", root_path);
120 return NULL;
121 }
122
123 char path[PATH_MAX] = "";
124 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
125 LOGE("Bad path %s\n", root_path);
126 return NULL;
127 }
128
129 // When writing, try to create the containing directory, if necessary.
130 // Use generous permissions, the system (init.rc) will reset them.
131 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
132
133 FILE *fp = fopen(path, mode);
134 if (fp == NULL) LOGE("Can't open %s\n", path);
135 return fp;
136}
137
138// close a file, log an error if the error indicator is set
139static void
140check_and_fclose(FILE *fp, const char *name) {
141 fflush(fp);
142 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
143 fclose(fp);
144}
145
146// command line args come from, in decreasing precedence:
147// - the actual command line
148// - the bootloader control block (one per line, after "recovery")
149// - the contents of COMMAND_FILE (one per line)
150static void
151get_args(int *argc, char ***argv) {
152 struct bootloader_message boot;
153 memset(&boot, 0, sizeof(boot));
154 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
155
156 if (boot.command[0] != 0 && boot.command[0] != 255) {
157 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
158 }
159
160 if (boot.status[0] != 0 && boot.status[0] != 255) {
161 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
162 }
163
164 // --- if arguments weren't supplied, look in the bootloader control block
165 if (*argc <= 1) {
166 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
167 const char *arg = strtok(boot.recovery, "\n");
168 if (arg != NULL && !strcmp(arg, "recovery")) {
169 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
170 (*argv)[0] = strdup(arg);
171 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
172 if ((arg = strtok(NULL, "\n")) == NULL) break;
173 (*argv)[*argc] = strdup(arg);
174 }
175 LOGI("Got arguments from boot message\n");
176 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
177 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
178 }
179 }
180
181 // --- if that doesn't work, try the command file
182 if (*argc <= 1) {
183 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
184 if (fp != NULL) {
185 char *argv0 = (*argv)[0];
186 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
187 (*argv)[0] = argv0; // use the same program name
188
189 char buf[MAX_ARG_LENGTH];
190 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
191 if (!fgets(buf, sizeof(buf), fp)) break;
192 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
193 }
194
195 check_and_fclose(fp, COMMAND_FILE);
196 LOGI("Got arguments from %s\n", COMMAND_FILE);
197 }
198 }
199
200 // --> write the arguments we have back into the bootloader control block
201 // always boot into recovery after this (until finish_recovery() is called)
202 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
203 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
204 int i;
205 for (i = 1; i < *argc; ++i) {
206 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
207 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
208 }
209 set_bootloader_message(&boot);
210}
211
212
213// clear the recovery command and prepare to boot a (hopefully working) system,
214// copy our log file to cache as well (for the system to read), and
215// record any intent we were asked to communicate back to the system.
216// this function is idempotent: call it as many times as you like.
217static void
218finish_recovery(const char *send_intent)
219{
220 // By this point, we're ready to return to the main system...
221 if (send_intent != NULL) {
222 FILE *fp = fopen_root_path(INTENT_FILE, "w");
223 if (fp != NULL) {
224 fputs(send_intent, fp);
225 check_and_fclose(fp, INTENT_FILE);
226 }
227 }
228
229 // Copy logs to cache so the system can find out what happened.
230 FILE *log = fopen_root_path(LOG_FILE, "a");
231 if (log != NULL) {
232 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
233 if (tmplog == NULL) {
234 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
235 } else {
236 static long tmplog_offset = 0;
237 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
238 char buf[4096];
239 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
240 tmplog_offset = ftell(tmplog);
241 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
242 }
243 check_and_fclose(log, LOG_FILE);
244 }
245
246 // Reset the bootloader message to revert to a normal main system boot.
247 struct bootloader_message boot;
248 memset(&boot, 0, sizeof(boot));
249 set_bootloader_message(&boot);
250
251 // Remove the command file, so recovery won't repeat indefinitely.
252 char path[PATH_MAX] = "";
253 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
254 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
255 (unlink(path) && errno != ENOENT)) {
256 LOGW("Can't unlink %s\n", COMMAND_FILE);
257 }
258
259 sync(); // For good measure.
260}
261
262#define TEST_AMEND 0
263#if TEST_AMEND
264static void
265test_amend()
266{
267 extern int test_symtab(void);
268 extern int test_cmd_fn(void);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269 int ret;
270 LOGD("Testing symtab...\n");
271 ret = test_symtab();
272 LOGD(" returned %d\n", ret);
273 LOGD("Testing cmd_fn...\n");
274 ret = test_cmd_fn();
275 LOGD(" returned %d\n", ret);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800276}
277#endif // TEST_AMEND
278
279static 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
288static void
289prompt_and_wait()
290{
Doug Zongkerd6837852009-06-17 22:07:13 -0700291 char* title[] = { "Android system recovery <"
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700292 EXPAND(RECOVERY_API_VERSION) ">",
Doug Zongkerd6837852009-06-17 22:07:13 -0700293 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 NULL };
295
Doug Zongkerd6837852009-06-17 22:07:13 -0700296 // count the number of lines in our title, plus the
297 // product-provided headers.
298 int count = 0;
299 char** p;
300 for (p = title; *p; ++p, ++count);
301 for (p = MENU_HEADERS; *p; ++p, ++count);
302
303 char** headers = malloc((count+1) * sizeof(char*));
304 char** h = headers;
305 for (p = title; *p; ++p, ++h) *h = *p;
306 for (p = MENU_HEADERS; *p; ++p, ++h) *h = *p;
307 *h = NULL;
308
309 ui_start_menu(headers, MENU_ITEMS);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310 int selected = 0;
311 int chosen_item = -1;
312
313 finish_recovery(NULL);
314 ui_reset_progress();
315 for (;;) {
316 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 Zongkerddd6a282009-06-09 12:22:33 -0700337 } else {
338 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800339 }
340
341 if (chosen_item >= 0) {
342 // turn off the menu, letting ui_print() to scroll output
343 // on the screen.
344 ui_end_menu();
345
Doug Zongkerddd6a282009-06-09 12:22:33 -0700346 // device-specific code may take some action here. It may
347 // return one of the core actions handled in the switch
348 // statement below.
349 chosen_item = device_perform_action(chosen_item);
350
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800351 switch (chosen_item) {
352 case ITEM_REBOOT:
353 return;
354
355 case ITEM_WIPE_DATA:
356 ui_print("\n-- Wiping data...\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700357 device_wipe_data();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358 erase_root("DATA:");
359 erase_root("CACHE:");
360 ui_print("Data wipe complete.\n");
361 if (!ui_text_visible()) return;
362 break;
363
Doug Zongker1066d2c2009-04-01 13:57:40 -0700364 case ITEM_WIPE_CACHE:
365 ui_print("\n-- Wiping cache...\n");
366 erase_root("CACHE:");
367 ui_print("Cache wipe complete.\n");
368 if (!ui_text_visible()) return;
369 break;
370
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800371 case ITEM_APPLY_SDCARD:
372 ui_print("\n-- Install from sdcard...\n");
373 int status = install_package(SDCARD_PACKAGE_FILE);
374 if (status != INSTALL_SUCCESS) {
375 ui_set_background(BACKGROUND_ICON_ERROR);
376 ui_print("Installation aborted.\n");
377 } else if (!ui_text_visible()) {
378 return; // reboot if logs aren't visible
379 } else {
Doug Zongker07e1dca2009-05-28 19:02:45 -0700380 if (firmware_update_pending()) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700381 ui_print("\nReboot via menu to complete\n"
382 "installation.\n");
Doug Zongker07e1dca2009-05-28 19:02:45 -0700383 } else {
384 ui_print("\nInstall from sdcard complete.\n");
385 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800386 }
387 break;
388 }
389
390 // if we didn't return from this function to reboot, show
391 // the menu again.
Doug Zongkerd6837852009-06-17 22:07:13 -0700392 ui_start_menu(headers, MENU_ITEMS);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800393 selected = 0;
394 chosen_item = -1;
395
396 finish_recovery(NULL);
397 ui_reset_progress();
398
399 // throw away keys pressed while the command was running,
400 // so user doesn't accidentally trigger menu items.
401 ui_clear_key_queue();
402 }
403 }
404}
405
406static void
407print_property(const char *key, const char *name, void *cookie)
408{
409 fprintf(stderr, "%s=%s\n", key, name);
410}
411
412int
413main(int argc, char **argv)
414{
415 time_t start = time(NULL);
416
417 // If these fail, there's not really anywhere to complain...
418 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
419 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
420 fprintf(stderr, "Starting recovery on %s", ctime(&start));
421
422 ui_init();
423 get_args(&argc, &argv);
424
425 int previous_runs = 0;
426 const char *send_intent = NULL;
427 const char *update_package = NULL;
428 int wipe_data = 0, wipe_cache = 0;
429
430 int arg;
431 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
432 switch (arg) {
433 case 'p': previous_runs = atoi(optarg); break;
434 case 's': send_intent = optarg; break;
435 case 'u': update_package = optarg; break;
436 case 'w': wipe_data = wipe_cache = 1; break;
437 case 'c': wipe_cache = 1; break;
438 case '?':
439 LOGE("Invalid command argument\n");
440 continue;
441 }
442 }
443
444 fprintf(stderr, "Command:");
445 for (arg = 0; arg < argc; arg++) {
446 fprintf(stderr, " \"%s\"", argv[arg]);
447 }
448 fprintf(stderr, "\n\n");
449
450 property_list(print_property, NULL);
451 fprintf(stderr, "\n");
452
453#if TEST_AMEND
454 test_amend();
455#endif
456
457 RecoveryCommandContext ctx = { NULL };
458 if (register_update_commands(&ctx)) {
459 LOGE("Can't install update commands\n");
460 }
461
462 int status = INSTALL_SUCCESS;
463
464 if (update_package != NULL) {
465 status = install_package(update_package);
466 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700467 } else if (wipe_data) {
468 if (device_wipe_data()) status = INSTALL_ERROR;
469 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800470 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
471 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700472 } else if (wipe_cache) {
473 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
474 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800475 } else {
476 status = INSTALL_ERROR; // No command specified
477 }
478
479 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
480 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
481
482 // If there is a radio image pending, reboot now to install it.
483 maybe_install_firmware_update(send_intent);
484
485 // Otherwise, get ready to boot the main system...
486 finish_recovery(send_intent);
487 ui_print("Rebooting...\n");
488 sync();
489 reboot(RB_AUTOBOOT);
490 return EXIT_SUCCESS;
491}