blob: 4570cb9f02dd8bb03b6b43a2b5ec27e78f17c37d [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>
25#include <sys/reboot.h>
26#include <sys/types.h>
27#include <time.h>
28#include <unistd.h>
29
30#include "bootloader.h"
31#include "commands.h"
32#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 */
67
68static const int MAX_ARG_LENGTH = 4096;
69static const int MAX_ARGS = 100;
70
71// open a file given in root:path format, mounting partitions as necessary
72static FILE*
73fopen_root_path(const char *root_path, const char *mode) {
74 if (ensure_root_path_mounted(root_path) != 0) {
75 LOGE("Can't mount %s\n", root_path);
76 return NULL;
77 }
78
79 char path[PATH_MAX] = "";
80 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
81 LOGE("Bad path %s\n", root_path);
82 return NULL;
83 }
84
85 // When writing, try to create the containing directory, if necessary.
86 // Use generous permissions, the system (init.rc) will reset them.
87 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
88
89 FILE *fp = fopen(path, mode);
90 if (fp == NULL) LOGE("Can't open %s\n", path);
91 return fp;
92}
93
94// close a file, log an error if the error indicator is set
95static void
96check_and_fclose(FILE *fp, const char *name) {
97 fflush(fp);
98 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
99 fclose(fp);
100}
101
102// command line args come from, in decreasing precedence:
103// - the actual command line
104// - the bootloader control block (one per line, after "recovery")
105// - the contents of COMMAND_FILE (one per line)
106static void
107get_args(int *argc, char ***argv) {
108 if (*argc > 1) return; // actual command line arguments take priority
109 char *argv0 = (*argv)[0];
110
111 struct bootloader_message boot;
112 if (!get_bootloader_message(&boot)) {
113 if (boot.command[0] != 0 && boot.command[0] != 255) {
114 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
115 }
116
117 if (boot.status[0] != 0 && boot.status[0] != 255) {
118 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
119 }
120
121 // Ensure that from here on, a reboot goes back into recovery
122 strcpy(boot.command, "boot-recovery");
123 set_bootloader_message(&boot);
124
125 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
126 const char *arg = strtok(boot.recovery, "\n");
127 if (arg != NULL && !strcmp(arg, "recovery")) {
128 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
129 (*argv)[0] = argv0;
130 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
131 if ((arg = strtok(NULL, "\n")) == NULL) break;
132 (*argv)[*argc] = strdup(arg);
133 }
134 LOGI("Got arguments from boot message\n");
135 return;
136 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
137 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
138 }
139 }
140
141 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
142 if (fp == NULL) return;
143
144 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
145 (*argv)[0] = argv0; // use the same program name
146
147 char buf[MAX_ARG_LENGTH];
148 for (*argc = 1; *argc < MAX_ARGS && fgets(buf, sizeof(buf), fp); ++*argc) {
149 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
150 }
151
152 check_and_fclose(fp, COMMAND_FILE);
153 LOGI("Got arguments from %s\n", COMMAND_FILE);
154}
155
156
157// clear the recovery command and prepare to boot a (hopefully working) system,
158// copy our log file to cache as well (for the system to read), and
159// record any intent we were asked to communicate back to the system.
160// this function is idempotent: call it as many times as you like.
161static void
162finish_recovery(const char *send_intent)
163{
164 // By this point, we're ready to return to the main system...
165 if (send_intent != NULL) {
166 FILE *fp = fopen_root_path(INTENT_FILE, "w");
167 if (fp != NULL) {
168 fputs(send_intent, fp);
169 check_and_fclose(fp, INTENT_FILE);
170 }
171 }
172
173 // Copy logs to cache so the system can find out what happened.
174 FILE *log = fopen_root_path(LOG_FILE, "a");
175 if (log != NULL) {
176 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
177 if (tmplog == NULL) {
178 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
179 } else {
180 static long tmplog_offset = 0;
181 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
182 char buf[4096];
183 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
184 tmplog_offset = ftell(tmplog);
185 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
186 }
187 check_and_fclose(log, LOG_FILE);
188 }
189
190 // Reset the bootloader message to revert to a normal main system boot.
191 struct bootloader_message boot;
192 memset(&boot, 0, sizeof(boot));
193 set_bootloader_message(&boot);
194
195 // Remove the command file, so recovery won't repeat indefinitely.
196 char path[PATH_MAX] = "";
197 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
198 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
199 (unlink(path) && errno != ENOENT)) {
200 LOGW("Can't unlink %s\n", COMMAND_FILE);
201 }
202
203 sync(); // For good measure.
204}
205
206#define TEST_AMEND 0
207#if TEST_AMEND
208static void
209test_amend()
210{
211 extern int test_symtab(void);
212 extern int test_cmd_fn(void);
213 extern int test_permissions(void);
214 int ret;
215 LOGD("Testing symtab...\n");
216 ret = test_symtab();
217 LOGD(" returned %d\n", ret);
218 LOGD("Testing cmd_fn...\n");
219 ret = test_cmd_fn();
220 LOGD(" returned %d\n", ret);
221 LOGD("Testing permissions...\n");
222 ret = test_permissions();
223 LOGD(" returned %d\n", ret);
224}
225#endif // TEST_AMEND
226
227static int
228erase_root(const char *root)
229{
230 ui_set_background(BACKGROUND_ICON_INSTALLING);
231 ui_show_indeterminate_progress();
232 ui_print("Formatting %s...\n", root);
233 return format_root_device(root);
234}
235
236static void
237prompt_and_wait()
238{
239 ui_print("\n"
240 "Home+Back - reboot system now\n"
241 "Alt+L - toggle log text display\n"
242 "Alt+S - apply sdcard:update.zip\n"
243 "Alt+W - wipe data/factory reset\n");
244
245 for (;;) {
246 finish_recovery(NULL);
247 ui_reset_progress();
248 int key = ui_wait_key();
249 int alt = ui_key_pressed(KEY_LEFTALT) || ui_key_pressed(KEY_RIGHTALT);
250
251 if (key == KEY_DREAM_BACK && ui_key_pressed(KEY_DREAM_HOME)) {
252 // Wait for the keys to be released, to avoid triggering
253 // special boot modes (like coming back into recovery!).
254 while (ui_key_pressed(KEY_DREAM_BACK) ||
255 ui_key_pressed(KEY_DREAM_HOME)) {
256 usleep(1000);
257 }
258 break;
259 } else if (alt && key == KEY_W) {
260 ui_print("\n");
261 erase_root("DATA:");
262 erase_root("CACHE:");
263 ui_print("Data wipe complete.\n");
264 if (!ui_text_visible()) break;
265 } else if (alt && key == KEY_S) {
266 ui_print("\nInstalling from sdcard...\n");
267 int status = install_package(SDCARD_PACKAGE_FILE);
268 if (status != INSTALL_SUCCESS) {
269 ui_set_background(BACKGROUND_ICON_ERROR);
270 ui_print("Installation aborted.\n");
271 } else if (!ui_text_visible()) {
272 break; // reboot if logs aren't visible
273 }
274 ui_print("\nPress Home+Back to reboot\n");
275 }
276 }
277}
278
279static void
280print_property(const char *key, const char *name, void *cookie)
281{
282 fprintf(stderr, "%s=%s\n", key, name);
283}
284
285int
286main(int argc, char **argv)
287{
288 time_t start = time(NULL);
289
290 // If these fail, there's not really anywhere to complain...
291 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
292 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
293 fprintf(stderr, "Starting recovery on %s", ctime(&start));
294
295 ui_init();
296 ui_print("Android system recovery utility\n");
297 get_args(&argc, &argv);
298
299 int previous_runs = 0;
300 const char *send_intent = NULL;
301 const char *update_package = NULL;
302 int wipe_data = 0, wipe_cache = 0;
303
304 int arg;
305 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
306 switch (arg) {
307 case 'p': previous_runs = atoi(optarg); break;
308 case 's': send_intent = optarg; break;
309 case 'u': update_package = optarg; break;
310 case 'w': wipe_data = wipe_cache = 1; break;
311 case 'c': wipe_cache = 1; break;
312 case '?':
313 LOGE("Invalid command argument\n");
314 continue;
315 }
316 }
317
318 fprintf(stderr, "Command:");
319 for (arg = 0; arg < argc; arg++) {
320 fprintf(stderr, " \"%s\"", argv[arg]);
321 }
322 fprintf(stderr, "\n\n");
323
324 property_list(print_property, NULL);
325 fprintf(stderr, "\n");
326
327#if TEST_AMEND
328 test_amend();
329#endif
330
331 RecoveryCommandContext ctx = { NULL };
332 if (register_update_commands(&ctx)) {
333 LOGE("Can't install update commands\n");
334 }
335
336 int status = INSTALL_SUCCESS;
337
338 if (update_package != NULL) {
339 status = install_package(update_package);
340 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
341 } else if (wipe_data || wipe_cache) {
342 if (wipe_data && erase_root("DATA:")) status = INSTALL_ERROR;
343 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
344 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
345 } else {
346 status = INSTALL_ERROR; // No command specified
347 }
348
349 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
350 if (status != INSTALL_SUCCESS || ui_text_visible()) prompt_and_wait();
351
352 // If there is a radio image pending, reboot now to install it.
353 maybe_install_firmware_update(send_intent);
354
355 // Otherwise, get ready to boot the main system...
356 finish_recovery(send_intent);
357 ui_print("Rebooting...\n");
358 sync();
359 reboot(RB_AUTOBOOT);
360 return EXIT_SUCCESS;
361}