blob: 3d44df3b4947f26801fe60b94400b11c0f1a3401 [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001/*
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>
The Android Open Source Project928d4712009-01-09 17:50:54 -080025#include <string.h>
The Android Open Source Project23580ca2008-10-21 07:00:00 -070026#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"
40
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.
The Android Open Source Project928d4712009-01-09 17:50:54 -080067 * 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
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700109 */
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) {
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700151 struct bootloader_message boot;
The Android Open Source Project928d4712009-01-09 17:50:54 -0800152 memset(&boot, 0, sizeof(boot));
153 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700154
The Android Open Source Project928d4712009-01-09 17:50:54 -0800155 if (boot.command[0] != 0 && boot.command[0] != 255) {
156 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
157 }
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700158
The Android Open Source Project928d4712009-01-09 17:50:54 -0800159 if (boot.status[0] != 0 && boot.status[0] != 255) {
160 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
161 }
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700162
The Android Open Source Project928d4712009-01-09 17:50:54 -0800163 // --- if arguments weren't supplied, look in the bootloader control block
164 if (*argc <= 1) {
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700165 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);
The Android Open Source Project928d4712009-01-09 17:50:54 -0800169 (*argv)[0] = strdup(arg);
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700170 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");
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700175 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
176 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
177 }
178 }
179
The Android Open Source Project928d4712009-01-09 17:50:54 -0800180 // --- 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
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700187
The Android Open Source Project928d4712009-01-09 17:50:54 -0800188 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 }
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700193
The Android Open Source Project928d4712009-01-09 17:50:54 -0800194 check_and_fclose(fp, COMMAND_FILE);
195 LOGI("Got arguments from %s\n", COMMAND_FILE);
196 }
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700197 }
198
The Android Open Source Project928d4712009-01-09 17:50:54 -0800199 // --> 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);
The Android Open Source Project23580ca2008-10-21 07:00:00 -0700209}
210
211
212// clear the recovery command and prepare to boot a (hopefully working) system,
213// copy our log file to cache as well (for the system to read), and
214// record any intent we were asked to communicate back to the system.
215// this function is idempotent: call it as many times as you like.
216static void
217finish_recovery(const char *send_intent)
218{
219 // By this point, we're ready to return to the main system...
220 if (send_intent != NULL) {
221 FILE *fp = fopen_root_path(INTENT_FILE, "w");
222 if (fp != NULL) {
223 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");
230 if (log != NULL) {
231 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
232 if (tmplog == NULL) {
233 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
234 } else {
235 static long tmplog_offset = 0;
236 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
237 char buf[4096];
238 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
239 tmplog_offset = ftell(tmplog);
240 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
241 }
242 check_and_fclose(log, LOG_FILE);
243 }
244
245 // Reset the bootloader message to revert to a normal main system boot.
246 struct bootloader_message boot;
247 memset(&boot, 0, sizeof(boot));
248 set_bootloader_message(&boot);
249
250 // Remove the command file, so recovery won't repeat indefinitely.
251 char path[PATH_MAX] = "";
252 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
253 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
254 (unlink(path) && errno != ENOENT)) {
255 LOGW("Can't unlink %s\n", COMMAND_FILE);
256 }
257
258 sync(); // For good measure.
259}
260
261#define TEST_AMEND 0
262#if TEST_AMEND
263static void
264test_amend()
265{
266 extern int test_symtab(void);
267 extern int test_cmd_fn(void);
268 extern int test_permissions(void);
269 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);
276 LOGD("Testing permissions...\n");
277 ret = test_permissions();
278 LOGD(" returned %d\n", ret);
279}
280#endif // TEST_AMEND
281
282static int
283erase_root(const char *root)
284{
285 ui_set_background(BACKGROUND_ICON_INSTALLING);
286 ui_show_indeterminate_progress();
287 ui_print("Formatting %s...\n", root);
288 return format_root_device(root);
289}
290
291static void
292prompt_and_wait()
293{
294 ui_print("\n"
295 "Home+Back - reboot system now\n"
296 "Alt+L - toggle log text display\n"
297 "Alt+S - apply sdcard:update.zip\n"
298 "Alt+W - wipe data/factory reset\n");
299
300 for (;;) {
301 finish_recovery(NULL);
302 ui_reset_progress();
303 int key = ui_wait_key();
304 int alt = ui_key_pressed(KEY_LEFTALT) || ui_key_pressed(KEY_RIGHTALT);
305
306 if (key == KEY_DREAM_BACK && ui_key_pressed(KEY_DREAM_HOME)) {
307 // Wait for the keys to be released, to avoid triggering
308 // special boot modes (like coming back into recovery!).
309 while (ui_key_pressed(KEY_DREAM_BACK) ||
310 ui_key_pressed(KEY_DREAM_HOME)) {
311 usleep(1000);
312 }
313 break;
314 } else if (alt && key == KEY_W) {
315 ui_print("\n");
316 erase_root("DATA:");
317 erase_root("CACHE:");
318 ui_print("Data wipe complete.\n");
319 if (!ui_text_visible()) break;
320 } else if (alt && key == KEY_S) {
321 ui_print("\nInstalling from sdcard...\n");
322 int status = install_package(SDCARD_PACKAGE_FILE);
323 if (status != INSTALL_SUCCESS) {
324 ui_set_background(BACKGROUND_ICON_ERROR);
325 ui_print("Installation aborted.\n");
326 } else if (!ui_text_visible()) {
327 break; // reboot if logs aren't visible
328 }
329 ui_print("\nPress Home+Back to reboot\n");
330 }
331 }
332}
333
334static void
335print_property(const char *key, const char *name, void *cookie)
336{
337 fprintf(stderr, "%s=%s\n", key, name);
338}
339
340int
341main(int argc, char **argv)
342{
343 time_t start = time(NULL);
344
345 // If these fail, there's not really anywhere to complain...
346 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
347 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
348 fprintf(stderr, "Starting recovery on %s", ctime(&start));
349
350 ui_init();
351 ui_print("Android system recovery utility\n");
352 get_args(&argc, &argv);
353
354 int previous_runs = 0;
355 const char *send_intent = NULL;
356 const char *update_package = NULL;
357 int wipe_data = 0, wipe_cache = 0;
358
359 int arg;
360 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
361 switch (arg) {
362 case 'p': previous_runs = atoi(optarg); break;
363 case 's': send_intent = optarg; break;
364 case 'u': update_package = optarg; break;
365 case 'w': wipe_data = wipe_cache = 1; break;
366 case 'c': wipe_cache = 1; break;
367 case '?':
368 LOGE("Invalid command argument\n");
369 continue;
370 }
371 }
372
373 fprintf(stderr, "Command:");
374 for (arg = 0; arg < argc; arg++) {
375 fprintf(stderr, " \"%s\"", argv[arg]);
376 }
377 fprintf(stderr, "\n\n");
378
379 property_list(print_property, NULL);
380 fprintf(stderr, "\n");
381
382#if TEST_AMEND
383 test_amend();
384#endif
385
386 RecoveryCommandContext ctx = { NULL };
387 if (register_update_commands(&ctx)) {
388 LOGE("Can't install update commands\n");
389 }
390
391 int status = INSTALL_SUCCESS;
392
393 if (update_package != NULL) {
394 status = install_package(update_package);
395 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
396 } else if (wipe_data || wipe_cache) {
397 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
398 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
399 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
400 } else {
401 status = INSTALL_ERROR; // No command specified
402 }
403
404 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
405 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
406
407 // If there is a radio image pending, reboot now to install it.
408 maybe_install_firmware_update(send_intent);
409
410 // Otherwise, get ready to boot the main system...
411 finish_recovery(send_intent);
412 ui_print("Rebooting...\n");
413 sync();
414 reboot(RB_AUTOBOOT);
415 return EXIT_SUCCESS;
416}