blob: f812e741b834f519c1c6aea42677ba09c0289e7a [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);
133 if (fp == NULL) LOGE("Can't open %s\n", path);
134 return fp;
135}
136
137// close a file, log an error if the error indicator is set
138static void
139check_and_fclose(FILE *fp, const char *name) {
140 fflush(fp);
141 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
142 fclose(fp);
143}
144
145// command line args come from, in decreasing precedence:
146// - the actual command line
147// - the bootloader control block (one per line, after "recovery")
148// - the contents of COMMAND_FILE (one per line)
149static void
150get_args(int *argc, char ***argv) {
151 struct bootloader_message boot;
152 memset(&boot, 0, sizeof(boot));
153 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
154
155 if (boot.command[0] != 0 && boot.command[0] != 255) {
156 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
157 }
158
159 if (boot.status[0] != 0 && boot.status[0] != 255) {
160 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
161 }
162
163 // --- if arguments weren't supplied, look in the bootloader control block
164 if (*argc <= 1) {
165 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
166 const char *arg = strtok(boot.recovery, "\n");
167 if (arg != NULL && !strcmp(arg, "recovery")) {
168 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
169 (*argv)[0] = strdup(arg);
170 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
171 if ((arg = strtok(NULL, "\n")) == NULL) break;
172 (*argv)[*argc] = strdup(arg);
173 }
174 LOGI("Got arguments from boot message\n");
175 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
176 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
177 }
178 }
179
180 // --- if that doesn't work, try the command file
181 if (*argc <= 1) {
182 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
183 if (fp != NULL) {
184 char *argv0 = (*argv)[0];
185 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
186 (*argv)[0] = argv0; // use the same program name
187
188 char buf[MAX_ARG_LENGTH];
189 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
190 if (!fgets(buf, sizeof(buf), fp)) break;
191 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
192 }
193
194 check_and_fclose(fp, COMMAND_FILE);
195 LOGI("Got arguments from %s\n", COMMAND_FILE);
196 }
197 }
198
199 // --> write the arguments we have back into the bootloader control block
200 // always boot into recovery after this (until finish_recovery() is called)
201 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
202 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
203 int i;
204 for (i = 1; i < *argc; ++i) {
205 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
206 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
207 }
208 set_bootloader_message(&boot);
209}
210
Doug Zongker34c98df2009-08-18 12:05:45 -0700211static void
212set_sdcard_update_bootloader_message()
213{
214 struct bootloader_message boot;
215 memset(&boot, 0, sizeof(boot));
216 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
217 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
218 set_bootloader_message(&boot);
219}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220
221// clear the recovery command and prepare to boot a (hopefully working) system,
222// copy our log file to cache as well (for the system to read), and
223// record any intent we were asked to communicate back to the system.
224// this function is idempotent: call it as many times as you like.
225static void
226finish_recovery(const char *send_intent)
227{
228 // By this point, we're ready to return to the main system...
229 if (send_intent != NULL) {
230 FILE *fp = fopen_root_path(INTENT_FILE, "w");
231 if (fp != NULL) {
232 fputs(send_intent, fp);
233 check_and_fclose(fp, INTENT_FILE);
234 }
235 }
236
237 // Copy logs to cache so the system can find out what happened.
238 FILE *log = fopen_root_path(LOG_FILE, "a");
239 if (log != NULL) {
240 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
241 if (tmplog == NULL) {
242 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
243 } else {
244 static long tmplog_offset = 0;
245 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
246 char buf[4096];
247 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
248 tmplog_offset = ftell(tmplog);
249 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
250 }
251 check_and_fclose(log, LOG_FILE);
252 }
253
254 // Reset the bootloader message to revert to a normal main system boot.
255 struct bootloader_message boot;
256 memset(&boot, 0, sizeof(boot));
257 set_bootloader_message(&boot);
258
259 // Remove the command file, so recovery won't repeat indefinitely.
260 char path[PATH_MAX] = "";
261 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
262 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
263 (unlink(path) && errno != ENOENT)) {
264 LOGW("Can't unlink %s\n", COMMAND_FILE);
265 }
266
267 sync(); // For good measure.
268}
269
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270static int
271erase_root(const char *root)
272{
273 ui_set_background(BACKGROUND_ICON_INSTALLING);
274 ui_show_indeterminate_progress();
275 ui_print("Formatting %s...\n", root);
276 return format_root_device(root);
277}
278
279static void
280prompt_and_wait()
281{
Doug Zongkerd6837852009-06-17 22:07:13 -0700282 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700283 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700284 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 NULL };
286
Doug Zongkerd6837852009-06-17 22:07:13 -0700287 // count the number of lines in our title, plus the
288 // product-provided headers.
289 int count = 0;
290 char** p;
291 for (p = title; *p; ++p, ++count);
292 for (p = MENU_HEADERS; *p; ++p, ++count);
293
294 char** headers = malloc((count+1) * sizeof(char*));
295 char** h = headers;
296 for (p = title; *p; ++p, ++h) *h = *p;
297 for (p = MENU_HEADERS; *p; ++p, ++h) *h = *p;
298 *h = NULL;
299
300 ui_start_menu(headers, MENU_ITEMS);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301 int selected = 0;
302 int chosen_item = -1;
303
304 finish_recovery(NULL);
305 ui_reset_progress();
306 for (;;) {
307 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 int visible = ui_text_visible();
309
Doug Zongkerddd6a282009-06-09 12:22:33 -0700310 int action = device_handle_key(key, visible);
311
312 if (action < 0) {
313 switch (action) {
314 case HIGHLIGHT_UP:
315 --selected;
316 selected = ui_menu_select(selected);
317 break;
318 case HIGHLIGHT_DOWN:
319 ++selected;
320 selected = ui_menu_select(selected);
321 break;
322 case SELECT_ITEM:
323 chosen_item = selected;
324 break;
325 case NO_ACTION:
326 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800327 }
Doug Zongkerddd6a282009-06-09 12:22:33 -0700328 } else {
329 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800330 }
331
332 if (chosen_item >= 0) {
333 // turn off the menu, letting ui_print() to scroll output
334 // on the screen.
335 ui_end_menu();
336
Doug Zongkerddd6a282009-06-09 12:22:33 -0700337 // device-specific code may take some action here. It may
338 // return one of the core actions handled in the switch
339 // statement below.
340 chosen_item = device_perform_action(chosen_item);
341
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800342 switch (chosen_item) {
343 case ITEM_REBOOT:
344 return;
345
346 case ITEM_WIPE_DATA:
347 ui_print("\n-- Wiping data...\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700348 device_wipe_data();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800349 erase_root("DATA:");
350 erase_root("CACHE:");
351 ui_print("Data wipe complete.\n");
352 if (!ui_text_visible()) return;
353 break;
354
Doug Zongker1066d2c2009-04-01 13:57:40 -0700355 case ITEM_WIPE_CACHE:
356 ui_print("\n-- Wiping cache...\n");
357 erase_root("CACHE:");
358 ui_print("Cache wipe complete.\n");
359 if (!ui_text_visible()) return;
360 break;
361
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800362 case ITEM_APPLY_SDCARD:
363 ui_print("\n-- Install from sdcard...\n");
Doug Zongker34c98df2009-08-18 12:05:45 -0700364 set_sdcard_update_bootloader_message();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800365 int status = install_package(SDCARD_PACKAGE_FILE);
366 if (status != INSTALL_SUCCESS) {
367 ui_set_background(BACKGROUND_ICON_ERROR);
368 ui_print("Installation aborted.\n");
369 } else if (!ui_text_visible()) {
370 return; // reboot if logs aren't visible
371 } else {
Doug Zongker07e1dca2009-05-28 19:02:45 -0700372 if (firmware_update_pending()) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700373 ui_print("\nReboot via menu to complete\n"
374 "installation.\n");
Doug Zongker07e1dca2009-05-28 19:02:45 -0700375 } else {
376 ui_print("\nInstall from sdcard complete.\n");
377 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800378 }
379 break;
380 }
381
382 // if we didn't return from this function to reboot, show
383 // the menu again.
Doug Zongkerd6837852009-06-17 22:07:13 -0700384 ui_start_menu(headers, MENU_ITEMS);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800385 selected = 0;
386 chosen_item = -1;
387
388 finish_recovery(NULL);
389 ui_reset_progress();
390
391 // throw away keys pressed while the command was running,
392 // so user doesn't accidentally trigger menu items.
393 ui_clear_key_queue();
394 }
395 }
396}
397
398static void
399print_property(const char *key, const char *name, void *cookie)
400{
401 fprintf(stderr, "%s=%s\n", key, name);
402}
403
404int
405main(int argc, char **argv)
406{
407 time_t start = time(NULL);
408
409 // If these fail, there's not really anywhere to complain...
410 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
411 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
412 fprintf(stderr, "Starting recovery on %s", ctime(&start));
413
414 ui_init();
415 get_args(&argc, &argv);
416
417 int previous_runs = 0;
418 const char *send_intent = NULL;
419 const char *update_package = NULL;
420 int wipe_data = 0, wipe_cache = 0;
421
422 int arg;
423 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
424 switch (arg) {
425 case 'p': previous_runs = atoi(optarg); break;
426 case 's': send_intent = optarg; break;
427 case 'u': update_package = optarg; break;
428 case 'w': wipe_data = wipe_cache = 1; break;
429 case 'c': wipe_cache = 1; break;
430 case '?':
431 LOGE("Invalid command argument\n");
432 continue;
433 }
434 }
435
436 fprintf(stderr, "Command:");
437 for (arg = 0; arg < argc; arg++) {
438 fprintf(stderr, " \"%s\"", argv[arg]);
439 }
440 fprintf(stderr, "\n\n");
441
442 property_list(print_property, NULL);
443 fprintf(stderr, "\n");
444
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800445 int status = INSTALL_SUCCESS;
446
447 if (update_package != NULL) {
448 status = install_package(update_package);
449 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700450 } else if (wipe_data) {
451 if (device_wipe_data()) status = INSTALL_ERROR;
452 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800453 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
454 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700455 } else if (wipe_cache) {
456 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
457 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800458 } else {
459 status = INSTALL_ERROR; // No command specified
460 }
461
462 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
463 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
464
465 // If there is a radio image pending, reboot now to install it.
466 maybe_install_firmware_update(send_intent);
467
468 // Otherwise, get ready to boot the main system...
469 finish_recovery(send_intent);
470 ui_print("Rebooting...\n");
471 sync();
472 reboot(RB_AUTOBOOT);
473 return EXIT_SUCCESS;
474}