blob: 9ad075d7449a76136d69978821d78e40aa203346 [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"
Oscar Montemayor52219a62010-02-25 16:47:02 -080041#include "encryptedfs_provisioning.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
43static const struct option OPTIONS[] = {
44 { "send_intent", required_argument, NULL, 's' },
45 { "update_package", required_argument, NULL, 'u' },
46 { "wipe_data", no_argument, NULL, 'w' },
47 { "wipe_cache", no_argument, NULL, 'c' },
Oscar Montemayor52219a62010-02-25 16:47:02 -080048 { "set_encrypted_filesystems", required_argument, NULL, 'e' },
Doug Zongker4bc98062010-09-03 11:00:13 -070049 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070050 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051};
52
Doug Zongkerd4208f92010-09-20 12:16:13 -070053static const char *COMMAND_FILE = "/cache/recovery/command";
54static const char *INTENT_FILE = "/cache/recovery/intent";
55static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070056static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070057static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070059static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060
61/*
62 * The recovery tool communicates with the main system through /cache files.
63 * /cache/recovery/command - INPUT - command line for tool, one arg per line
64 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
65 * /cache/recovery/intent - OUTPUT - intent that was passed in
66 *
67 * The arguments which may be supplied in the recovery.command file:
68 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070069 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070 * --wipe_data - erase user data (and cache), then reboot
71 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080072 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 *
74 * After completing, we remove /cache/recovery/command and reboot.
75 * Arguments may also be supplied in the bootloader control block (BCB).
76 * These important scenarios must be safely restartable at any point:
77 *
78 * FACTORY RESET
79 * 1. user selects "factory reset"
80 * 2. main system writes "--wipe_data" to /cache/recovery/command
81 * 3. main system reboots into recovery
82 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
83 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070084 * 5. erase_volume() reformats /data
85 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086 * 7. finish_recovery() erases BCB
87 * -- after this, rebooting will restart the main system --
88 * 8. main() calls reboot() to boot main system
89 *
90 * OTA INSTALL
91 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -070092 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093 * 3. main system reboots into recovery
94 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
95 * -- after this, rebooting will attempt to reinstall the update --
96 * 5. install_package() attempts to install the update
97 * NOTE: the package install must itself be restartable from any point
98 * 6. finish_recovery() erases BCB
99 * -- after this, rebooting will (try to) restart the main system --
100 * 7. ** if install failed **
101 * 7a. prompt_and_wait() shows an error icon and waits for the user
102 * 7b; the user reboots (pulling the battery, etc) into the main system
103 * 8. main() calls maybe_install_firmware_update()
104 * ** if the update contained radio/hboot firmware **:
105 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
106 * -- after this, rebooting will reformat cache & restart main system --
107 * 8b. m_i_f_u() writes firmware image into raw cache partition
108 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
109 * -- after this, rebooting will attempt to reinstall firmware --
110 * 8d. bootloader tries to flash firmware
111 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
112 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114 * 8g. finish_recovery() erases BCB
115 * -- after this, rebooting will (try to) restart the main system --
116 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800117 *
Oscar Montemayor52219a62010-02-25 16:47:02 -0800118 * SECURE FILE SYSTEMS ENABLE/DISABLE
Oscar Montemayor05231562009-11-30 08:40:57 -0800119 * 1. user selects "enable encrypted file systems"
Oscar Montemayor52219a62010-02-25 16:47:02 -0800120 * 2. main system writes "--set_encrypted_filesystems=on|off" to
Oscar Montemayor05231562009-11-30 08:40:57 -0800121 * /cache/recovery/command
122 * 3. main system reboots into recovery
123 * 4. get_args() writes BCB with "boot-recovery" and
124 * "--set_encrypted_filesystems=on|off"
125 * -- after this, rebooting will restart the transition --
126 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
127 * Settings include: property to specify the Encrypted FS istatus and
128 * FS encryption key if enabled (not yet implemented)
Doug Zongkerd4208f92010-09-20 12:16:13 -0700129 * 6. erase_volume() reformats /data
130 * 7. erase_volume() reformats /cache
Oscar Montemayor05231562009-11-30 08:40:57 -0800131 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
132 * Settings include: property to specify the Encrypted FS status and
133 * FS encryption key if enabled (not yet implemented)
134 * 9. finish_recovery() erases BCB
135 * -- after this, rebooting will restart the main system --
136 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 */
138
139static const int MAX_ARG_LENGTH = 4096;
140static const int MAX_ARGS = 100;
141
Doug Zongkerd4208f92010-09-20 12:16:13 -0700142// open a given path, mounting partitions as necessary
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143static FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700144fopen_path(const char *path, const char *mode) {
145 if (ensure_path_mounted(path) != 0) {
146 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147 return NULL;
148 }
149
150 // When writing, try to create the containing directory, if necessary.
151 // Use generous permissions, the system (init.rc) will reset them.
152 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
153
154 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 return fp;
156}
157
158// close a file, log an error if the error indicator is set
159static void
160check_and_fclose(FILE *fp, const char *name) {
161 fflush(fp);
162 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
163 fclose(fp);
164}
165
166// command line args come from, in decreasing precedence:
167// - the actual command line
168// - the bootloader control block (one per line, after "recovery")
169// - the contents of COMMAND_FILE (one per line)
170static void
171get_args(int *argc, char ***argv) {
172 struct bootloader_message boot;
173 memset(&boot, 0, sizeof(boot));
174 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
175
176 if (boot.command[0] != 0 && boot.command[0] != 255) {
177 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
178 }
179
180 if (boot.status[0] != 0 && boot.status[0] != 255) {
181 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
182 }
183
184 // --- if arguments weren't supplied, look in the bootloader control block
185 if (*argc <= 1) {
186 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
187 const char *arg = strtok(boot.recovery, "\n");
188 if (arg != NULL && !strcmp(arg, "recovery")) {
189 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
190 (*argv)[0] = strdup(arg);
191 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
192 if ((arg = strtok(NULL, "\n")) == NULL) break;
193 (*argv)[*argc] = strdup(arg);
194 }
195 LOGI("Got arguments from boot message\n");
196 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
197 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
198 }
199 }
200
201 // --- if that doesn't work, try the command file
202 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700203 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800204 if (fp != NULL) {
205 char *argv0 = (*argv)[0];
206 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
207 (*argv)[0] = argv0; // use the same program name
208
209 char buf[MAX_ARG_LENGTH];
210 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
211 if (!fgets(buf, sizeof(buf), fp)) break;
212 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
213 }
214
215 check_and_fclose(fp, COMMAND_FILE);
216 LOGI("Got arguments from %s\n", COMMAND_FILE);
217 }
218 }
219
220 // --> write the arguments we have back into the bootloader control block
221 // always boot into recovery after this (until finish_recovery() is called)
222 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
223 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
224 int i;
225 for (i = 1; i < *argc; ++i) {
226 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
227 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
228 }
229 set_bootloader_message(&boot);
230}
231
Doug Zongker34c98df2009-08-18 12:05:45 -0700232static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800233set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700234 struct bootloader_message boot;
235 memset(&boot, 0, sizeof(boot));
236 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
237 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
238 set_bootloader_message(&boot);
239}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800240
Doug Zongker2c3539e2010-09-29 13:21:30 -0700241// How much of the temp log we have copied to the copy in cache.
242static long tmplog_offset = 0;
243
244static void
245copy_log_file(const char* destination, int append) {
246 FILE *log = fopen_path(destination, append ? "a" : "w");
247 if (log == NULL) {
248 LOGE("Can't open %s\n", destination);
249 } else {
250 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
251 if (tmplog == NULL) {
252 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
253 } else {
254 if (append) {
255 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
256 }
257 char buf[4096];
258 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
259 if (append) {
260 tmplog_offset = ftell(tmplog);
261 }
262 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
263 }
264 check_and_fclose(log, destination);
265 }
266}
267
268
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269// clear the recovery command and prepare to boot a (hopefully working) system,
270// copy our log file to cache as well (for the system to read), and
271// record any intent we were asked to communicate back to the system.
272// this function is idempotent: call it as many times as you like.
273static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800274finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275 // By this point, we're ready to return to the main system...
276 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700277 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000278 if (fp == NULL) {
279 LOGE("Can't open %s\n", INTENT_FILE);
280 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281 fputs(send_intent, fp);
282 check_and_fclose(fp, INTENT_FILE);
283 }
284 }
285
286 // Copy logs to cache so the system can find out what happened.
Doug Zongker2c3539e2010-09-29 13:21:30 -0700287 copy_log_file(LOG_FILE, true);
288 copy_log_file(LAST_LOG_FILE, false);
289 chmod(LAST_LOG_FILE, 0640);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290
Oscar Montemayor05231562009-11-30 08:40:57 -0800291 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 struct bootloader_message boot;
293 memset(&boot, 0, sizeof(boot));
294 set_bootloader_message(&boot);
295
296 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700297 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
298 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299 LOGW("Can't unlink %s\n", COMMAND_FILE);
300 }
301
302 sync(); // For good measure.
303}
304
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700306erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307 ui_set_background(BACKGROUND_ICON_INSTALLING);
308 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700309 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700310
311 if (strcmp(volume, "/cache") == 0) {
312 // Any part of the log we'd copied to cache is now gone.
313 // Reset the pointer so we copy from the beginning of the temp
314 // log.
315 tmplog_offset = 0;
316 }
317
Doug Zongkerd4208f92010-09-20 12:16:13 -0700318 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800319}
320
Doug Zongker23ceeea2010-07-08 17:27:55 -0700321static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700322copy_sideloaded_package(const char* original_path) {
323 if (ensure_path_mounted(original_path) != 0) {
324 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700325 return NULL;
326 }
327
Doug Zongkerd4208f92010-09-20 12:16:13 -0700328 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700329 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
330 return NULL;
331 }
332
Doug Zongkerd4208f92010-09-20 12:16:13 -0700333 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700334 if (errno != EEXIST) {
335 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
336 return NULL;
337 }
338 }
339
Doug Zongkerd4208f92010-09-20 12:16:13 -0700340 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
341 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700342 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700343 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
344 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700345 return NULL;
346 }
347 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700348 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700349 return NULL;
350 }
351 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700352 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700353 return NULL;
354 }
355 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700356 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700357 return NULL;
358 }
359
Doug Zongkerd4208f92010-09-20 12:16:13 -0700360 char copy_path[PATH_MAX];
361 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700362 strcat(copy_path, "/package.zip");
363
364 char* buffer = malloc(BUFSIZ);
365 if (buffer == NULL) {
366 LOGE("Failed to allocate buffer\n");
367 return NULL;
368 }
369
370 size_t read;
371 FILE* fin = fopen(original_path, "rb");
372 if (fin == NULL) {
373 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
374 return NULL;
375 }
376 FILE* fout = fopen(copy_path, "wb");
377 if (fout == NULL) {
378 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
379 return NULL;
380 }
381
382 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
383 if (fwrite(buffer, 1, read, fout) != read) {
384 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
385 return NULL;
386 }
387 }
388
389 free(buffer);
390
391 if (fclose(fout) != 0) {
392 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
393 return NULL;
394 }
395
396 if (fclose(fin) != 0) {
397 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
398 return NULL;
399 }
400
401 // "adb push" is happy to overwrite read-only files when it's
402 // running as root, but we'll try anyway.
403 if (chmod(copy_path, 0400) != 0) {
404 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
405 return NULL;
406 }
407
Doug Zongkerd4208f92010-09-20 12:16:13 -0700408 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700409}
410
Doug Zongkerf93d8162009-09-22 15:16:02 -0700411static char**
Doug Zongker8674a722010-09-15 11:08:23 -0700412prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700413 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700414 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700415 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800416 NULL };
417
Doug Zongkerd6837852009-06-17 22:07:13 -0700418 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700419 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700420 int count = 0;
421 char** p;
422 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700423 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700424
Doug Zongkerf93d8162009-09-22 15:16:02 -0700425 char** new_headers = malloc((count+1) * sizeof(char*));
426 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700427 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700428 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700429 *h = NULL;
430
Doug Zongkerf93d8162009-09-22 15:16:02 -0700431 return new_headers;
432}
433
434static int
Doug Zongker8674a722010-09-15 11:08:23 -0700435get_menu_selection(char** headers, char** items, int menu_only,
436 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700437 // throw away keys pressed previously, so user doesn't
438 // accidentally trigger menu items.
439 ui_clear_key_queue();
440
Doug Zongker8674a722010-09-15 11:08:23 -0700441 ui_start_menu(headers, items, initial_selection);
442 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800443 int chosen_item = -1;
444
Doug Zongkerf93d8162009-09-22 15:16:02 -0700445 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800446 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800447 int visible = ui_text_visible();
448
Doug Zongkerddd6a282009-06-09 12:22:33 -0700449 int action = device_handle_key(key, visible);
450
451 if (action < 0) {
452 switch (action) {
453 case HIGHLIGHT_UP:
454 --selected;
455 selected = ui_menu_select(selected);
456 break;
457 case HIGHLIGHT_DOWN:
458 ++selected;
459 selected = ui_menu_select(selected);
460 break;
461 case SELECT_ITEM:
462 chosen_item = selected;
463 break;
464 case NO_ACTION:
465 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800466 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700467 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700468 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800469 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700470 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800471
Doug Zongkerf93d8162009-09-22 15:16:02 -0700472 ui_end_menu();
473 return chosen_item;
474}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800475
Doug Zongker8674a722010-09-15 11:08:23 -0700476static int compare_string(const void* a, const void* b) {
477 return strcmp(*(const char**)a, *(const char**)b);
478}
479
480static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700481sdcard_directory(const char* path) {
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700482 ensure_path_mounted(SDCARD_ROOT);
483
Doug Zongker8674a722010-09-15 11:08:23 -0700484 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700485 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700486 "",
487 NULL };
488 DIR* d;
489 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700490 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700491 if (d == NULL) {
492 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700493 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700494 return 0;
495 }
496
497 char** headers = prepend_title(MENU_HEADERS);
498
499 int d_size = 0;
500 int d_alloc = 10;
501 char** dirs = malloc(d_alloc * sizeof(char*));
502 int z_size = 1;
503 int z_alloc = 10;
504 char** zips = malloc(z_alloc * sizeof(char*));
505 zips[0] = strdup("../");
506
507 while ((de = readdir(d)) != NULL) {
508 int name_len = strlen(de->d_name);
509
510 if (de->d_type == DT_DIR) {
511 // skip "." and ".." entries
512 if (name_len == 1 && de->d_name[0] == '.') continue;
513 if (name_len == 2 && de->d_name[0] == '.' &&
514 de->d_name[1] == '.') continue;
515
516 if (d_size >= d_alloc) {
517 d_alloc *= 2;
518 dirs = realloc(dirs, d_alloc * sizeof(char*));
519 }
520 dirs[d_size] = malloc(name_len + 2);
521 strcpy(dirs[d_size], de->d_name);
522 dirs[d_size][name_len] = '/';
523 dirs[d_size][name_len+1] = '\0';
524 ++d_size;
525 } else if (de->d_type == DT_REG &&
526 name_len >= 4 &&
527 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
528 if (z_size >= z_alloc) {
529 z_alloc *= 2;
530 zips = realloc(zips, z_alloc * sizeof(char*));
531 }
532 zips[z_size++] = strdup(de->d_name);
533 }
534 }
535 closedir(d);
536
537 qsort(dirs, d_size, sizeof(char*), compare_string);
538 qsort(zips, z_size, sizeof(char*), compare_string);
539
540 // append dirs to the zips list
541 if (d_size + z_size + 1 > z_alloc) {
542 z_alloc = d_size + z_size + 1;
543 zips = realloc(zips, z_alloc * sizeof(char*));
544 }
545 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
546 free(dirs);
547 z_size += d_size;
548 zips[z_size] = NULL;
549
550 int result;
551 int chosen_item = 0;
552 do {
553 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
554
555 char* item = zips[chosen_item];
556 int item_len = strlen(item);
557 if (chosen_item == 0) { // item 0 is always "../"
558 // go up but continue browsing (if the caller is sdcard_directory)
559 result = -1;
560 break;
561 } else if (item[item_len-1] == '/') {
562 // recurse down into a subdirectory
563 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700564 strlcpy(new_path, path, PATH_MAX);
565 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700566 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700567 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700568 result = sdcard_directory(new_path);
569 if (result >= 0) break;
570 } else {
571 // selected a zip file: attempt to install it, and return
572 // the status to the caller.
573 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700574 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700575 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700576 strlcat(new_path, item, PATH_MAX);
577
Doug Zongkerd4208f92010-09-20 12:16:13 -0700578 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700579 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700580 char* copy = copy_sideloaded_package(new_path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700581 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700582 if (copy) {
583 result = install_package(copy);
584 free(copy);
585 } else {
586 result = INSTALL_ERROR;
587 }
Doug Zongker8674a722010-09-15 11:08:23 -0700588 break;
589 }
590 } while (true);
591
592 int i;
593 for (i = 0; i < z_size; ++i) free(zips[i]);
594 free(zips);
595 free(headers);
596
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700597 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700598 return result;
599}
600
Doug Zongkerf93d8162009-09-22 15:16:02 -0700601static void
602wipe_data(int confirm) {
603 if (confirm) {
604 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700605
Doug Zongkerf93d8162009-09-22 15:16:02 -0700606 if (title_headers == NULL) {
607 char* headers[] = { "Confirm wipe of all user data?",
608 " THIS CAN NOT BE UNDONE.",
609 "",
610 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700611 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700612 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800613
Doug Zongkerf93d8162009-09-22 15:16:02 -0700614 char* items[] = { " No",
615 " No",
616 " No",
617 " No",
618 " No",
619 " No",
620 " No",
621 " Yes -- delete all user data", // [7]
622 " No",
623 " No",
624 " No",
625 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800626
Doug Zongker8674a722010-09-15 11:08:23 -0700627 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700628 if (chosen_item != 7) {
629 return;
630 }
631 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700632
Doug Zongkerf93d8162009-09-22 15:16:02 -0700633 ui_print("\n-- Wiping data...\n");
634 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700635 erase_volume("/data");
636 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700637 ui_print("Data wipe complete.\n");
638}
639
640static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800641prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700642 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700643
644 for (;;) {
645 finish_recovery(NULL);
646 ui_reset_progress();
647
Doug Zongker8674a722010-09-15 11:08:23 -0700648 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700649
650 // device-specific code may take some action here. It may
651 // return one of the core actions handled in the switch
652 // statement below.
653 chosen_item = device_perform_action(chosen_item);
654
655 switch (chosen_item) {
656 case ITEM_REBOOT:
657 return;
658
659 case ITEM_WIPE_DATA:
660 wipe_data(ui_text_visible());
661 if (!ui_text_visible()) return;
662 break;
663
664 case ITEM_WIPE_CACHE:
665 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700666 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700667 ui_print("Cache wipe complete.\n");
668 if (!ui_text_visible()) return;
669 break;
670
671 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700672 ;
673 int status = sdcard_directory(SDCARD_ROOT);
674 if (status >= 0) {
675 if (status != INSTALL_SUCCESS) {
676 ui_set_background(BACKGROUND_ICON_ERROR);
677 ui_print("Installation aborted.\n");
678 } else if (!ui_text_visible()) {
679 return; // reboot if logs aren't visible
680 } else {
681 ui_print("\nInstall from sdcard complete.\n");
682 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700683 }
684 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800685 }
686 }
687}
688
689static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800690print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700691 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800692}
693
694int
Oscar Montemayor05231562009-11-30 08:40:57 -0800695main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800696 time_t start = time(NULL);
697
698 // If these fail, there's not really anywhere to complain...
699 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
700 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700701 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800702
703 ui_init();
Doug Zongker51266d12010-11-01 10:19:12 -0700704 ui_set_background(BACKGROUND_ICON_INSTALLING);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700705 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800706 get_args(&argc, &argv);
707
708 int previous_runs = 0;
709 const char *send_intent = NULL;
710 const char *update_package = NULL;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800711 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800712 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800713 int toggle_secure_fs = 0;
714 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800715
716 int arg;
717 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
718 switch (arg) {
719 case 'p': previous_runs = atoi(optarg); break;
720 case 's': send_intent = optarg; break;
721 case 'u': update_package = optarg; break;
722 case 'w': wipe_data = wipe_cache = 1; break;
723 case 'c': wipe_cache = 1; break;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800724 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700725 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800726 case '?':
727 LOGE("Invalid command argument\n");
728 continue;
729 }
730 }
731
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800732 device_recovery_start();
733
Doug Zongker56c51052010-07-01 09:18:44 -0700734 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800735 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700736 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800737 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700738 printf("\n");
739
740 if (update_package) {
741 // For backwards compatibility on the cache partition only, if
742 // we're given an old 'root' path "CACHE:foo", change it to
743 // "/cache/foo".
744 if (strncmp(update_package, "CACHE:", 6) == 0) {
745 int len = strlen(update_package) + 10;
746 char* modified_path = malloc(len);
747 strlcpy(modified_path, "/cache/", len);
748 strlcat(modified_path, update_package+6, len);
749 printf("(replacing path \"%s\" with \"%s\")\n",
750 update_package, modified_path);
751 update_package = modified_path;
752 }
753 }
754 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800755
756 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700757 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800758
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800759 int status = INSTALL_SUCCESS;
760
Oscar Montemayor52219a62010-02-25 16:47:02 -0800761 if (toggle_secure_fs) {
762 if (strcmp(encrypted_fs_mode,"on") == 0) {
763 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800764 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800765 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
766 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800767 ui_print("Disabling Encrypted FS.\n");
768 } else {
769 ui_print("Error: invalid Encrypted FS setting.\n");
770 status = INSTALL_ERROR;
771 }
772
773 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
774 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor52219a62010-02-25 16:47:02 -0800775 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
776 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800777 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800778 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800779 }
780
781 if (status != INSTALL_ERROR) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700782 if (erase_volume("/data")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800783 ui_print("Data wipe failed.\n");
784 status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700785 } else if (erase_volume("/cache")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800786 ui_print("Cache wipe failed.\n");
787 status = INSTALL_ERROR;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800788 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
789 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800790 ui_print("Encrypted FS change aborted.\n");
791 status = INSTALL_ERROR;
792 } else {
793 ui_print("Successfully updated Encrypted FS.\n");
794 status = INSTALL_SUCCESS;
795 }
796 }
797 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800798 status = install_package(update_package);
799 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700800 } else if (wipe_data) {
801 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700802 if (erase_volume("/data")) status = INSTALL_ERROR;
803 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800804 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700805 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700806 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700807 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800808 } else {
809 status = INSTALL_ERROR; // No command specified
810 }
811
812 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700813 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700814 prompt_and_wait();
815 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800816
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800817 // Otherwise, get ready to boot the main system...
818 finish_recovery(send_intent);
819 ui_print("Rebooting...\n");
820 sync();
821 reboot(RB_AUTOBOOT);
822 return EXIT_SUCCESS;
823}