blob: 0ff5d94d031ddf7fb5a11d95f3f7b5b1ba4dfbba [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"
39
40static const struct option OPTIONS[] = {
41 { "send_intent", required_argument, NULL, 's' },
42 { "update_package", required_argument, NULL, 'u' },
43 { "wipe_data", no_argument, NULL, 'w' },
44 { "wipe_cache", no_argument, NULL, 'c' },
45};
46
47static const char *COMMAND_FILE = "CACHE:recovery/command";
48static const char *INTENT_FILE = "CACHE:recovery/intent";
49static const char *LOG_FILE = "CACHE:recovery/log";
50static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
51static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
52
53/*
54 * The recovery tool communicates with the main system through /cache files.
55 * /cache/recovery/command - INPUT - command line for tool, one arg per line
56 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
57 * /cache/recovery/intent - OUTPUT - intent that was passed in
58 *
59 * The arguments which may be supplied in the recovery.command file:
60 * --send_intent=anystring - write the text out to recovery.intent
61 * --update_package=root:path - verify install an OTA package file
62 * --wipe_data - erase user data (and cache), then reboot
63 * --wipe_cache - wipe cache (but not user data), then reboot
64 *
65 * After completing, we remove /cache/recovery/command and reboot.
66 * Arguments may also be supplied in the bootloader control block (BCB).
67 * These important scenarios must be safely restartable at any point:
68 *
69 * FACTORY RESET
70 * 1. user selects "factory reset"
71 * 2. main system writes "--wipe_data" to /cache/recovery/command
72 * 3. main system reboots into recovery
73 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
74 * -- after this, rebooting will restart the erase --
75 * 5. erase_root() reformats /data
76 * 6. erase_root() reformats /cache
77 * 7. finish_recovery() erases BCB
78 * -- after this, rebooting will restart the main system --
79 * 8. main() calls reboot() to boot main system
80 *
81 * OTA INSTALL
82 * 1. main system downloads OTA package to /cache/some-filename.zip
83 * 2. main system writes "--update_package=CACHE:some-filename.zip"
84 * 3. main system reboots into recovery
85 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
86 * -- after this, rebooting will attempt to reinstall the update --
87 * 5. install_package() attempts to install the update
88 * NOTE: the package install must itself be restartable from any point
89 * 6. finish_recovery() erases BCB
90 * -- after this, rebooting will (try to) restart the main system --
91 * 7. ** if install failed **
92 * 7a. prompt_and_wait() shows an error icon and waits for the user
93 * 7b; the user reboots (pulling the battery, etc) into the main system
94 * 8. main() calls maybe_install_firmware_update()
95 * ** if the update contained radio/hboot firmware **:
96 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
97 * -- after this, rebooting will reformat cache & restart main system --
98 * 8b. m_i_f_u() writes firmware image into raw cache partition
99 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
100 * -- after this, rebooting will attempt to reinstall firmware --
101 * 8d. bootloader tries to flash firmware
102 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
103 * -- after this, rebooting will reformat cache & restart main system --
104 * 8f. erase_root() reformats /cache
105 * 8g. finish_recovery() erases BCB
106 * -- after this, rebooting will (try to) restart the main system --
107 * 9. main() calls reboot() to boot main system
108 */
109
110static const int MAX_ARG_LENGTH = 4096;
111static const int MAX_ARGS = 100;
112
113// open a file given in root:path format, mounting partitions as necessary
114static FILE*
115fopen_root_path(const char *root_path, const char *mode) {
116 if (ensure_root_path_mounted(root_path) != 0) {
117 LOGE("Can't mount %s\n", root_path);
118 return NULL;
119 }
120
121 char path[PATH_MAX] = "";
122 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
123 LOGE("Bad path %s\n", root_path);
124 return NULL;
125 }
126
127 // When writing, try to create the containing directory, if necessary.
128 // Use generous permissions, the system (init.rc) will reset them.
129 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
130
131 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800132 return fp;
133}
134
135// close a file, log an error if the error indicator is set
136static void
137check_and_fclose(FILE *fp, const char *name) {
138 fflush(fp);
139 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
140 fclose(fp);
141}
142
143// command line args come from, in decreasing precedence:
144// - the actual command line
145// - the bootloader control block (one per line, after "recovery")
146// - the contents of COMMAND_FILE (one per line)
147static void
148get_args(int *argc, char ***argv) {
149 struct bootloader_message boot;
150 memset(&boot, 0, sizeof(boot));
151 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
152
153 if (boot.command[0] != 0 && boot.command[0] != 255) {
154 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
155 }
156
157 if (boot.status[0] != 0 && boot.status[0] != 255) {
158 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
159 }
160
161 // --- if arguments weren't supplied, look in the bootloader control block
162 if (*argc <= 1) {
163 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
164 const char *arg = strtok(boot.recovery, "\n");
165 if (arg != NULL && !strcmp(arg, "recovery")) {
166 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
167 (*argv)[0] = strdup(arg);
168 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
169 if ((arg = strtok(NULL, "\n")) == NULL) break;
170 (*argv)[*argc] = strdup(arg);
171 }
172 LOGI("Got arguments from boot message\n");
173 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
174 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
175 }
176 }
177
178 // --- if that doesn't work, try the command file
179 if (*argc <= 1) {
180 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
181 if (fp != NULL) {
182 char *argv0 = (*argv)[0];
183 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
184 (*argv)[0] = argv0; // use the same program name
185
186 char buf[MAX_ARG_LENGTH];
187 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
188 if (!fgets(buf, sizeof(buf), fp)) break;
189 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
190 }
191
192 check_and_fclose(fp, COMMAND_FILE);
193 LOGI("Got arguments from %s\n", COMMAND_FILE);
194 }
195 }
196
197 // --> write the arguments we have back into the bootloader control block
198 // always boot into recovery after this (until finish_recovery() is called)
199 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
200 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
201 int i;
202 for (i = 1; i < *argc; ++i) {
203 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
204 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
205 }
206 set_bootloader_message(&boot);
207}
208
209
210// clear the recovery command and prepare to boot a (hopefully working) system,
211// copy our log file to cache as well (for the system to read), and
212// record any intent we were asked to communicate back to the system.
213// this function is idempotent: call it as many times as you like.
214static void
215finish_recovery(const char *send_intent)
216{
217 // By this point, we're ready to return to the main system...
218 if (send_intent != NULL) {
219 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000220 if (fp == NULL) {
221 LOGE("Can't open %s\n", INTENT_FILE);
222 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223 fputs(send_intent, fp);
224 check_and_fclose(fp, INTENT_FILE);
225 }
226 }
227
228 // Copy logs to cache so the system can find out what happened.
229 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000230 if (log == NULL) {
231 LOGE("Can't open %s\n", LOG_FILE);
232 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800233 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
234 if (tmplog == NULL) {
235 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
236 } else {
237 static long tmplog_offset = 0;
238 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
239 char buf[4096];
240 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
241 tmplog_offset = ftell(tmplog);
242 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
243 }
244 check_and_fclose(log, LOG_FILE);
245 }
246
247 // Reset the bootloader message to revert to a normal main system boot.
248 struct bootloader_message boot;
249 memset(&boot, 0, sizeof(boot));
250 set_bootloader_message(&boot);
251
252 // Remove the command file, so recovery won't repeat indefinitely.
253 char path[PATH_MAX] = "";
254 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
255 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
256 (unlink(path) && errno != ENOENT)) {
257 LOGW("Can't unlink %s\n", COMMAND_FILE);
258 }
259
260 sync(); // For good measure.
261}
262
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263static int
264erase_root(const char *root)
265{
266 ui_set_background(BACKGROUND_ICON_INSTALLING);
267 ui_show_indeterminate_progress();
268 ui_print("Formatting %s...\n", root);
269 return format_root_device(root);
270}
271
272static void
273prompt_and_wait()
274{
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700275 char* headers[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700276 EXPAND(RECOVERY_API_VERSION) "e>",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 "",
278 "Use trackball to highlight;",
279 "click to select.",
280 "",
281 NULL };
282
283 // these constants correspond to elements of the items[] list.
284#define ITEM_REBOOT 0
285#define ITEM_APPLY_SDCARD 1
286#define ITEM_WIPE_DATA 2
Doug Zongker1066d2c2009-04-01 13:57:40 -0700287#define ITEM_WIPE_CACHE 3
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288 char* items[] = { "reboot system now [Home+Back]",
289 "apply sdcard:update.zip [Alt+S]",
290 "wipe data/factory reset [Alt+W]",
Doug Zongker1066d2c2009-04-01 13:57:40 -0700291 "wipe cache partition",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 NULL };
293
294 ui_start_menu(headers, items);
295 int selected = 0;
296 int chosen_item = -1;
297
298 finish_recovery(NULL);
299 ui_reset_progress();
300 for (;;) {
301 int key = ui_wait_key();
302 int alt = ui_key_pressed(KEY_LEFTALT) || ui_key_pressed(KEY_RIGHTALT);
303 int visible = ui_text_visible();
304
305 if (key == KEY_DREAM_BACK && ui_key_pressed(KEY_DREAM_HOME)) {
306 // Wait for the keys to be released, to avoid triggering
307 // special boot modes (like coming back into recovery!).
308 while (ui_key_pressed(KEY_DREAM_BACK) ||
309 ui_key_pressed(KEY_DREAM_HOME)) {
310 usleep(1000);
311 }
312 chosen_item = ITEM_REBOOT;
313 } else if (alt && key == KEY_W) {
314 chosen_item = ITEM_WIPE_DATA;
315 } else if (alt && key == KEY_S) {
316 chosen_item = ITEM_APPLY_SDCARD;
317 } else if ((key == KEY_DOWN || key == KEY_VOLUMEDOWN) && visible) {
318 ++selected;
319 selected = ui_menu_select(selected);
320 } else if ((key == KEY_UP || key == KEY_VOLUMEUP) && visible) {
321 --selected;
322 selected = ui_menu_select(selected);
323 } else if (key == BTN_MOUSE && visible) {
324 chosen_item = selected;
325 }
326
327 if (chosen_item >= 0) {
328 // turn off the menu, letting ui_print() to scroll output
329 // on the screen.
330 ui_end_menu();
331
332 switch (chosen_item) {
333 case ITEM_REBOOT:
334 return;
335
336 case ITEM_WIPE_DATA:
337 ui_print("\n-- Wiping data...\n");
338 erase_root("DATA:");
339 erase_root("CACHE:");
340 ui_print("Data wipe complete.\n");
341 if (!ui_text_visible()) return;
342 break;
343
Doug Zongker1066d2c2009-04-01 13:57:40 -0700344 case ITEM_WIPE_CACHE:
345 ui_print("\n-- Wiping cache...\n");
346 erase_root("CACHE:");
347 ui_print("Cache wipe complete.\n");
348 if (!ui_text_visible()) return;
349 break;
350
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800351 case ITEM_APPLY_SDCARD:
352 ui_print("\n-- Install from sdcard...\n");
353 int status = install_package(SDCARD_PACKAGE_FILE);
354 if (status != INSTALL_SUCCESS) {
355 ui_set_background(BACKGROUND_ICON_ERROR);
356 ui_print("Installation aborted.\n");
357 } else if (!ui_text_visible()) {
358 return; // reboot if logs aren't visible
359 } else {
Doug Zongker07e1dca2009-05-28 19:02:45 -0700360 if (firmware_update_pending()) {
361 ui_print("\nReboot via home+back or menu\n"
362 "to complete installation.\n");
363 } else {
364 ui_print("\nInstall from sdcard complete.\n");
365 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800366 }
367 break;
368 }
369
370 // if we didn't return from this function to reboot, show
371 // the menu again.
372 ui_start_menu(headers, items);
373 selected = 0;
374 chosen_item = -1;
375
376 finish_recovery(NULL);
377 ui_reset_progress();
378
379 // throw away keys pressed while the command was running,
380 // so user doesn't accidentally trigger menu items.
381 ui_clear_key_queue();
382 }
383 }
384}
385
386static void
387print_property(const char *key, const char *name, void *cookie)
388{
389 fprintf(stderr, "%s=%s\n", key, name);
390}
391
392int
393main(int argc, char **argv)
394{
395 time_t start = time(NULL);
396
397 // If these fail, there's not really anywhere to complain...
398 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
399 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
400 fprintf(stderr, "Starting recovery on %s", ctime(&start));
401
402 ui_init();
403 get_args(&argc, &argv);
404
405 int previous_runs = 0;
406 const char *send_intent = NULL;
407 const char *update_package = NULL;
408 int wipe_data = 0, wipe_cache = 0;
409
410 int arg;
411 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
412 switch (arg) {
413 case 'p': previous_runs = atoi(optarg); break;
414 case 's': send_intent = optarg; break;
415 case 'u': update_package = optarg; break;
416 case 'w': wipe_data = wipe_cache = 1; break;
417 case 'c': wipe_cache = 1; break;
418 case '?':
419 LOGE("Invalid command argument\n");
420 continue;
421 }
422 }
423
424 fprintf(stderr, "Command:");
425 for (arg = 0; arg < argc; arg++) {
426 fprintf(stderr, " \"%s\"", argv[arg]);
427 }
428 fprintf(stderr, "\n\n");
429
430 property_list(print_property, NULL);
431 fprintf(stderr, "\n");
432
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800433 int status = INSTALL_SUCCESS;
434
435 if (update_package != NULL) {
436 status = install_package(update_package);
437 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
438 } else if (wipe_data || wipe_cache) {
439 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
440 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
441 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
442 } else {
443 status = INSTALL_ERROR; // No command specified
444 }
445
446 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
447 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
448
449 // If there is a radio image pending, reboot now to install it.
450 maybe_install_firmware_update(send_intent);
451
452 // Otherwise, get ready to boot the main system...
453 finish_recovery(send_intent);
454 ui_print("Rebooting...\n");
455 sync();
456 reboot(RB_AUTOBOOT);
457 return EXIT_SUCCESS;
458}