blob: 671cfbebd7831e3dfd183fcdebd95b351996a2c7 [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>
Doug Zongker23ceeea2010-07-08 17:27:55 -070027#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
Doug Zongker8674a722010-09-15 11:08:23 -070031#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
33#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "common.h"
35#include "cutils/properties.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#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' },
Doug Zongker4bc98062010-09-03 11:00:13 -070047 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070048 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049};
50
Doug Zongkerd4208f92010-09-20 12:16:13 -070051static const char *COMMAND_FILE = "/cache/recovery/command";
52static const char *INTENT_FILE = "/cache/recovery/intent";
53static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070054static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070055static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070057static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058
59/*
60 * The recovery tool communicates with the main system through /cache files.
61 * /cache/recovery/command - INPUT - command line for tool, one arg per line
62 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
63 * /cache/recovery/intent - OUTPUT - intent that was passed in
64 *
65 * The arguments which may be supplied in the recovery.command file:
66 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070067 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068 * --wipe_data - erase user data (and cache), then reboot
69 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080070 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071 *
72 * After completing, we remove /cache/recovery/command and reboot.
73 * Arguments may also be supplied in the bootloader control block (BCB).
74 * These important scenarios must be safely restartable at any point:
75 *
76 * FACTORY RESET
77 * 1. user selects "factory reset"
78 * 2. main system writes "--wipe_data" to /cache/recovery/command
79 * 3. main system reboots into recovery
80 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
81 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070082 * 5. erase_volume() reformats /data
83 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084 * 7. finish_recovery() erases BCB
85 * -- after this, rebooting will restart the main system --
86 * 8. main() calls reboot() to boot main system
87 *
88 * OTA INSTALL
89 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -070090 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091 * 3. main system reboots into recovery
92 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
93 * -- after this, rebooting will attempt to reinstall the update --
94 * 5. install_package() attempts to install the update
95 * NOTE: the package install must itself be restartable from any point
96 * 6. finish_recovery() erases BCB
97 * -- after this, rebooting will (try to) restart the main system --
98 * 7. ** if install failed **
99 * 7a. prompt_and_wait() shows an error icon and waits for the user
100 * 7b; the user reboots (pulling the battery, etc) into the main system
101 * 8. main() calls maybe_install_firmware_update()
102 * ** if the update contained radio/hboot firmware **:
103 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
104 * -- after this, rebooting will reformat cache & restart main system --
105 * 8b. m_i_f_u() writes firmware image into raw cache partition
106 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
107 * -- after this, rebooting will attempt to reinstall firmware --
108 * 8d. bootloader tries to flash firmware
109 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
110 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700111 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 * 8g. finish_recovery() erases BCB
113 * -- after this, rebooting will (try to) restart the main system --
114 * 9. main() calls reboot() to boot main system
115 */
116
117static const int MAX_ARG_LENGTH = 4096;
118static const int MAX_ARGS = 100;
119
Doug Zongkerd4208f92010-09-20 12:16:13 -0700120// open a given path, mounting partitions as necessary
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121static FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700122fopen_path(const char *path, const char *mode) {
123 if (ensure_path_mounted(path) != 0) {
124 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 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);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 return fp;
134}
135
136// close a file, log an error if the error indicator is set
137static void
138check_and_fclose(FILE *fp, const char *name) {
139 fflush(fp);
140 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
141 fclose(fp);
142}
143
144// command line args come from, in decreasing precedence:
145// - the actual command line
146// - the bootloader control block (one per line, after "recovery")
147// - the contents of COMMAND_FILE (one per line)
148static void
149get_args(int *argc, char ***argv) {
150 struct bootloader_message boot;
151 memset(&boot, 0, sizeof(boot));
152 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
153
154 if (boot.command[0] != 0 && boot.command[0] != 255) {
155 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
156 }
157
158 if (boot.status[0] != 0 && boot.status[0] != 255) {
159 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
160 }
161
162 // --- if arguments weren't supplied, look in the bootloader control block
163 if (*argc <= 1) {
164 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
165 const char *arg = strtok(boot.recovery, "\n");
166 if (arg != NULL && !strcmp(arg, "recovery")) {
167 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
168 (*argv)[0] = strdup(arg);
169 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
170 if ((arg = strtok(NULL, "\n")) == NULL) break;
171 (*argv)[*argc] = strdup(arg);
172 }
173 LOGI("Got arguments from boot message\n");
174 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
175 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
176 }
177 }
178
179 // --- if that doesn't work, try the command file
180 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 if (fp != NULL) {
183 char *argv0 = (*argv)[0];
184 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
185 (*argv)[0] = argv0; // use the same program name
186
187 char buf[MAX_ARG_LENGTH];
188 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
189 if (!fgets(buf, sizeof(buf), fp)) break;
190 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
191 }
192
193 check_and_fclose(fp, COMMAND_FILE);
194 LOGI("Got arguments from %s\n", COMMAND_FILE);
195 }
196 }
197
198 // --> write the arguments we have back into the bootloader control block
199 // always boot into recovery after this (until finish_recovery() is called)
200 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
201 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
202 int i;
203 for (i = 1; i < *argc; ++i) {
204 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
205 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
206 }
207 set_bootloader_message(&boot);
208}
209
Doug Zongker34c98df2009-08-18 12:05:45 -0700210static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800211set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700212 struct bootloader_message boot;
213 memset(&boot, 0, sizeof(boot));
214 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
215 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
216 set_bootloader_message(&boot);
217}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218
Doug Zongker2c3539e2010-09-29 13:21:30 -0700219// How much of the temp log we have copied to the copy in cache.
220static long tmplog_offset = 0;
221
222static void
223copy_log_file(const char* destination, int append) {
224 FILE *log = fopen_path(destination, append ? "a" : "w");
225 if (log == NULL) {
226 LOGE("Can't open %s\n", destination);
227 } else {
228 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
229 if (tmplog == NULL) {
230 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
231 } else {
232 if (append) {
233 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
234 }
235 char buf[4096];
236 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
237 if (append) {
238 tmplog_offset = ftell(tmplog);
239 }
240 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
241 }
242 check_and_fclose(log, destination);
243 }
244}
245
246
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800247// clear the recovery command and prepare to boot a (hopefully working) system,
248// copy our log file to cache as well (for the system to read), and
249// record any intent we were asked to communicate back to the system.
250// this function is idempotent: call it as many times as you like.
251static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800252finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253 // By this point, we're ready to return to the main system...
254 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700255 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000256 if (fp == NULL) {
257 LOGE("Can't open %s\n", INTENT_FILE);
258 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800259 fputs(send_intent, fp);
260 check_and_fclose(fp, INTENT_FILE);
261 }
262 }
263
264 // Copy logs to cache so the system can find out what happened.
Doug Zongker2c3539e2010-09-29 13:21:30 -0700265 copy_log_file(LOG_FILE, true);
266 copy_log_file(LAST_LOG_FILE, false);
267 chmod(LAST_LOG_FILE, 0640);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268
Oscar Montemayor05231562009-11-30 08:40:57 -0800269 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 struct bootloader_message boot;
271 memset(&boot, 0, sizeof(boot));
272 set_bootloader_message(&boot);
273
274 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700275 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
276 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 LOGW("Can't unlink %s\n", COMMAND_FILE);
278 }
279
280 sync(); // For good measure.
281}
282
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700284erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 ui_set_background(BACKGROUND_ICON_INSTALLING);
286 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700287 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700288
289 if (strcmp(volume, "/cache") == 0) {
290 // Any part of the log we'd copied to cache is now gone.
291 // Reset the pointer so we copy from the beginning of the temp
292 // log.
293 tmplog_offset = 0;
294 }
295
Doug Zongkerd4208f92010-09-20 12:16:13 -0700296 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297}
298
Doug Zongker23ceeea2010-07-08 17:27:55 -0700299static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700300copy_sideloaded_package(const char* original_path) {
301 if (ensure_path_mounted(original_path) != 0) {
302 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700303 return NULL;
304 }
305
Doug Zongkerd4208f92010-09-20 12:16:13 -0700306 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700307 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
308 return NULL;
309 }
310
Doug Zongkerd4208f92010-09-20 12:16:13 -0700311 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700312 if (errno != EEXIST) {
313 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
314 return NULL;
315 }
316 }
317
Doug Zongkerd4208f92010-09-20 12:16:13 -0700318 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
319 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700320 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700321 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
322 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700323 return NULL;
324 }
325 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700326 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700327 return NULL;
328 }
329 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700330 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700331 return NULL;
332 }
333 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700334 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700335 return NULL;
336 }
337
Doug Zongkerd4208f92010-09-20 12:16:13 -0700338 char copy_path[PATH_MAX];
339 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700340 strcat(copy_path, "/package.zip");
341
342 char* buffer = malloc(BUFSIZ);
343 if (buffer == NULL) {
344 LOGE("Failed to allocate buffer\n");
345 return NULL;
346 }
347
348 size_t read;
349 FILE* fin = fopen(original_path, "rb");
350 if (fin == NULL) {
351 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
352 return NULL;
353 }
354 FILE* fout = fopen(copy_path, "wb");
355 if (fout == NULL) {
356 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
357 return NULL;
358 }
359
360 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
361 if (fwrite(buffer, 1, read, fout) != read) {
362 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
363 return NULL;
364 }
365 }
366
367 free(buffer);
368
369 if (fclose(fout) != 0) {
370 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
371 return NULL;
372 }
373
374 if (fclose(fin) != 0) {
375 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
376 return NULL;
377 }
378
379 // "adb push" is happy to overwrite read-only files when it's
380 // running as root, but we'll try anyway.
381 if (chmod(copy_path, 0400) != 0) {
382 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
383 return NULL;
384 }
385
Doug Zongkerd4208f92010-09-20 12:16:13 -0700386 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700387}
388
Doug Zongkerf93d8162009-09-22 15:16:02 -0700389static char**
Doug Zongker8674a722010-09-15 11:08:23 -0700390prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700391 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700392 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700393 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800394 NULL };
395
Doug Zongkerd6837852009-06-17 22:07:13 -0700396 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700397 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700398 int count = 0;
399 char** p;
400 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700401 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700402
Doug Zongkerf93d8162009-09-22 15:16:02 -0700403 char** new_headers = malloc((count+1) * sizeof(char*));
404 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700405 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700406 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700407 *h = NULL;
408
Doug Zongkerf93d8162009-09-22 15:16:02 -0700409 return new_headers;
410}
411
412static int
Doug Zongker8674a722010-09-15 11:08:23 -0700413get_menu_selection(char** headers, char** items, int menu_only,
414 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700415 // throw away keys pressed previously, so user doesn't
416 // accidentally trigger menu items.
417 ui_clear_key_queue();
418
Doug Zongker8674a722010-09-15 11:08:23 -0700419 ui_start_menu(headers, items, initial_selection);
420 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800421 int chosen_item = -1;
422
Doug Zongkerf93d8162009-09-22 15:16:02 -0700423 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800424 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800425 int visible = ui_text_visible();
426
Doug Zongker5cae4452011-01-25 13:15:30 -0800427 if (key == -1) { // ui_wait_key() timed out
428 if (ui_text_ever_visible()) {
429 continue;
430 } else {
431 LOGI("timed out waiting for key input; rebooting.\n");
432 ui_end_menu();
433 return ITEM_REBOOT;
434 }
435 }
436
Doug Zongkerddd6a282009-06-09 12:22:33 -0700437 int action = device_handle_key(key, visible);
438
439 if (action < 0) {
440 switch (action) {
441 case HIGHLIGHT_UP:
442 --selected;
443 selected = ui_menu_select(selected);
444 break;
445 case HIGHLIGHT_DOWN:
446 ++selected;
447 selected = ui_menu_select(selected);
448 break;
449 case SELECT_ITEM:
450 chosen_item = selected;
451 break;
452 case NO_ACTION:
453 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800454 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700455 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700456 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800457 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700458 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800459
Doug Zongkerf93d8162009-09-22 15:16:02 -0700460 ui_end_menu();
461 return chosen_item;
462}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800463
Doug Zongker8674a722010-09-15 11:08:23 -0700464static int compare_string(const void* a, const void* b) {
465 return strcmp(*(const char**)a, *(const char**)b);
466}
467
468static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700469sdcard_directory(const char* path) {
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700470 ensure_path_mounted(SDCARD_ROOT);
471
Doug Zongker8674a722010-09-15 11:08:23 -0700472 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700473 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700474 "",
475 NULL };
476 DIR* d;
477 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700478 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700479 if (d == NULL) {
480 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700481 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700482 return 0;
483 }
484
485 char** headers = prepend_title(MENU_HEADERS);
486
487 int d_size = 0;
488 int d_alloc = 10;
489 char** dirs = malloc(d_alloc * sizeof(char*));
490 int z_size = 1;
491 int z_alloc = 10;
492 char** zips = malloc(z_alloc * sizeof(char*));
493 zips[0] = strdup("../");
494
495 while ((de = readdir(d)) != NULL) {
496 int name_len = strlen(de->d_name);
497
498 if (de->d_type == DT_DIR) {
499 // skip "." and ".." entries
500 if (name_len == 1 && de->d_name[0] == '.') continue;
501 if (name_len == 2 && de->d_name[0] == '.' &&
502 de->d_name[1] == '.') continue;
503
504 if (d_size >= d_alloc) {
505 d_alloc *= 2;
506 dirs = realloc(dirs, d_alloc * sizeof(char*));
507 }
508 dirs[d_size] = malloc(name_len + 2);
509 strcpy(dirs[d_size], de->d_name);
510 dirs[d_size][name_len] = '/';
511 dirs[d_size][name_len+1] = '\0';
512 ++d_size;
513 } else if (de->d_type == DT_REG &&
514 name_len >= 4 &&
515 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
516 if (z_size >= z_alloc) {
517 z_alloc *= 2;
518 zips = realloc(zips, z_alloc * sizeof(char*));
519 }
520 zips[z_size++] = strdup(de->d_name);
521 }
522 }
523 closedir(d);
524
525 qsort(dirs, d_size, sizeof(char*), compare_string);
526 qsort(zips, z_size, sizeof(char*), compare_string);
527
528 // append dirs to the zips list
529 if (d_size + z_size + 1 > z_alloc) {
530 z_alloc = d_size + z_size + 1;
531 zips = realloc(zips, z_alloc * sizeof(char*));
532 }
533 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
534 free(dirs);
535 z_size += d_size;
536 zips[z_size] = NULL;
537
538 int result;
539 int chosen_item = 0;
540 do {
541 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
542
543 char* item = zips[chosen_item];
544 int item_len = strlen(item);
545 if (chosen_item == 0) { // item 0 is always "../"
546 // go up but continue browsing (if the caller is sdcard_directory)
547 result = -1;
548 break;
549 } else if (item[item_len-1] == '/') {
550 // recurse down into a subdirectory
551 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700552 strlcpy(new_path, path, PATH_MAX);
553 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700554 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700555 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700556 result = sdcard_directory(new_path);
557 if (result >= 0) break;
558 } else {
559 // selected a zip file: attempt to install it, and return
560 // the status to the caller.
561 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700562 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700563 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700564 strlcat(new_path, item, PATH_MAX);
565
Doug Zongkerd4208f92010-09-20 12:16:13 -0700566 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700567 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700568 char* copy = copy_sideloaded_package(new_path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700569 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700570 if (copy) {
571 result = install_package(copy);
572 free(copy);
573 } else {
574 result = INSTALL_ERROR;
575 }
Doug Zongker8674a722010-09-15 11:08:23 -0700576 break;
577 }
578 } while (true);
579
580 int i;
581 for (i = 0; i < z_size; ++i) free(zips[i]);
582 free(zips);
583 free(headers);
584
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700585 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700586 return result;
587}
588
Doug Zongkerf93d8162009-09-22 15:16:02 -0700589static void
590wipe_data(int confirm) {
591 if (confirm) {
592 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700593
Doug Zongkerf93d8162009-09-22 15:16:02 -0700594 if (title_headers == NULL) {
595 char* headers[] = { "Confirm wipe of all user data?",
596 " THIS CAN NOT BE UNDONE.",
597 "",
598 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700599 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700600 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800601
Doug Zongkerf93d8162009-09-22 15:16:02 -0700602 char* items[] = { " No",
603 " No",
604 " No",
605 " No",
606 " No",
607 " No",
608 " No",
609 " Yes -- delete all user data", // [7]
610 " No",
611 " No",
612 " No",
613 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800614
Doug Zongker8674a722010-09-15 11:08:23 -0700615 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700616 if (chosen_item != 7) {
617 return;
618 }
619 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700620
Doug Zongkerf93d8162009-09-22 15:16:02 -0700621 ui_print("\n-- Wiping data...\n");
622 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700623 erase_volume("/data");
624 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700625 ui_print("Data wipe complete.\n");
626}
627
628static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800629prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700630 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700631
632 for (;;) {
633 finish_recovery(NULL);
634 ui_reset_progress();
635
Doug Zongker8674a722010-09-15 11:08:23 -0700636 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700637
638 // device-specific code may take some action here. It may
639 // return one of the core actions handled in the switch
640 // statement below.
641 chosen_item = device_perform_action(chosen_item);
642
643 switch (chosen_item) {
644 case ITEM_REBOOT:
645 return;
646
647 case ITEM_WIPE_DATA:
648 wipe_data(ui_text_visible());
649 if (!ui_text_visible()) return;
650 break;
651
652 case ITEM_WIPE_CACHE:
653 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700654 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700655 ui_print("Cache wipe complete.\n");
656 if (!ui_text_visible()) return;
657 break;
658
659 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700660 ;
661 int status = sdcard_directory(SDCARD_ROOT);
662 if (status >= 0) {
663 if (status != INSTALL_SUCCESS) {
664 ui_set_background(BACKGROUND_ICON_ERROR);
665 ui_print("Installation aborted.\n");
666 } else if (!ui_text_visible()) {
667 return; // reboot if logs aren't visible
668 } else {
669 ui_print("\nInstall from sdcard complete.\n");
670 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700671 }
672 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800673 }
674 }
675}
676
677static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800678print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700679 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800680}
681
682int
Oscar Montemayor05231562009-11-30 08:40:57 -0800683main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800684 time_t start = time(NULL);
685
686 // If these fail, there's not really anywhere to complain...
687 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
688 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700689 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800690
691 ui_init();
Doug Zongker51266d12010-11-01 10:19:12 -0700692 ui_set_background(BACKGROUND_ICON_INSTALLING);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700693 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800694 get_args(&argc, &argv);
695
696 int previous_runs = 0;
697 const char *send_intent = NULL;
698 const char *update_package = NULL;
699 int wipe_data = 0, wipe_cache = 0;
700
701 int arg;
702 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
703 switch (arg) {
704 case 'p': previous_runs = atoi(optarg); break;
705 case 's': send_intent = optarg; break;
706 case 'u': update_package = optarg; break;
707 case 'w': wipe_data = wipe_cache = 1; break;
708 case 'c': wipe_cache = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700709 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800710 case '?':
711 LOGE("Invalid command argument\n");
712 continue;
713 }
714 }
715
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800716 device_recovery_start();
717
Doug Zongker56c51052010-07-01 09:18:44 -0700718 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800719 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700720 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800721 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700722 printf("\n");
723
724 if (update_package) {
725 // For backwards compatibility on the cache partition only, if
726 // we're given an old 'root' path "CACHE:foo", change it to
727 // "/cache/foo".
728 if (strncmp(update_package, "CACHE:", 6) == 0) {
729 int len = strlen(update_package) + 10;
730 char* modified_path = malloc(len);
731 strlcpy(modified_path, "/cache/", len);
732 strlcat(modified_path, update_package+6, len);
733 printf("(replacing path \"%s\" with \"%s\")\n",
734 update_package, modified_path);
735 update_package = modified_path;
736 }
737 }
738 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800739
740 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700741 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800742
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800743 int status = INSTALL_SUCCESS;
744
Doug Zongker540d57f2011-01-18 13:36:58 -0800745 if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800746 status = install_package(update_package);
747 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700748 } else if (wipe_data) {
749 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700750 if (erase_volume("/data")) status = INSTALL_ERROR;
751 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800752 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700753 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700754 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700755 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800756 } else {
757 status = INSTALL_ERROR; // No command specified
758 }
759
760 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700761 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700762 prompt_and_wait();
763 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800764
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800765 // Otherwise, get ready to boot the main system...
766 finish_recovery(send_intent);
767 ui_print("Rebooting...\n");
768 sync();
769 reboot(RB_AUTOBOOT);
770 return EXIT_SUCCESS;
771}