blob: c81a13c376dbeb292d1e7ac740d9664c30ebe49f [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 Zongker5cae4452011-01-25 13:15:30 -0800449 if (key == -1) { // ui_wait_key() timed out
450 if (ui_text_ever_visible()) {
451 continue;
452 } else {
453 LOGI("timed out waiting for key input; rebooting.\n");
454 ui_end_menu();
455 return ITEM_REBOOT;
456 }
457 }
458
Doug Zongkerddd6a282009-06-09 12:22:33 -0700459 int action = device_handle_key(key, visible);
460
461 if (action < 0) {
462 switch (action) {
463 case HIGHLIGHT_UP:
464 --selected;
465 selected = ui_menu_select(selected);
466 break;
467 case HIGHLIGHT_DOWN:
468 ++selected;
469 selected = ui_menu_select(selected);
470 break;
471 case SELECT_ITEM:
472 chosen_item = selected;
473 break;
474 case NO_ACTION:
475 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800476 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700477 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700478 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800479 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700480 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800481
Doug Zongkerf93d8162009-09-22 15:16:02 -0700482 ui_end_menu();
483 return chosen_item;
484}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800485
Doug Zongker8674a722010-09-15 11:08:23 -0700486static int compare_string(const void* a, const void* b) {
487 return strcmp(*(const char**)a, *(const char**)b);
488}
489
490static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700491sdcard_directory(const char* path) {
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700492 ensure_path_mounted(SDCARD_ROOT);
493
Doug Zongker8674a722010-09-15 11:08:23 -0700494 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700495 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700496 "",
497 NULL };
498 DIR* d;
499 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700500 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700501 if (d == NULL) {
502 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700503 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700504 return 0;
505 }
506
507 char** headers = prepend_title(MENU_HEADERS);
508
509 int d_size = 0;
510 int d_alloc = 10;
511 char** dirs = malloc(d_alloc * sizeof(char*));
512 int z_size = 1;
513 int z_alloc = 10;
514 char** zips = malloc(z_alloc * sizeof(char*));
515 zips[0] = strdup("../");
516
517 while ((de = readdir(d)) != NULL) {
518 int name_len = strlen(de->d_name);
519
520 if (de->d_type == DT_DIR) {
521 // skip "." and ".." entries
522 if (name_len == 1 && de->d_name[0] == '.') continue;
523 if (name_len == 2 && de->d_name[0] == '.' &&
524 de->d_name[1] == '.') continue;
525
526 if (d_size >= d_alloc) {
527 d_alloc *= 2;
528 dirs = realloc(dirs, d_alloc * sizeof(char*));
529 }
530 dirs[d_size] = malloc(name_len + 2);
531 strcpy(dirs[d_size], de->d_name);
532 dirs[d_size][name_len] = '/';
533 dirs[d_size][name_len+1] = '\0';
534 ++d_size;
535 } else if (de->d_type == DT_REG &&
536 name_len >= 4 &&
537 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
538 if (z_size >= z_alloc) {
539 z_alloc *= 2;
540 zips = realloc(zips, z_alloc * sizeof(char*));
541 }
542 zips[z_size++] = strdup(de->d_name);
543 }
544 }
545 closedir(d);
546
547 qsort(dirs, d_size, sizeof(char*), compare_string);
548 qsort(zips, z_size, sizeof(char*), compare_string);
549
550 // append dirs to the zips list
551 if (d_size + z_size + 1 > z_alloc) {
552 z_alloc = d_size + z_size + 1;
553 zips = realloc(zips, z_alloc * sizeof(char*));
554 }
555 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
556 free(dirs);
557 z_size += d_size;
558 zips[z_size] = NULL;
559
560 int result;
561 int chosen_item = 0;
562 do {
563 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
564
565 char* item = zips[chosen_item];
566 int item_len = strlen(item);
567 if (chosen_item == 0) { // item 0 is always "../"
568 // go up but continue browsing (if the caller is sdcard_directory)
569 result = -1;
570 break;
571 } else if (item[item_len-1] == '/') {
572 // recurse down into a subdirectory
573 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700574 strlcpy(new_path, path, PATH_MAX);
575 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700576 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700577 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700578 result = sdcard_directory(new_path);
579 if (result >= 0) break;
580 } else {
581 // selected a zip file: attempt to install it, and return
582 // the status to the caller.
583 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700584 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700585 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700586 strlcat(new_path, item, PATH_MAX);
587
Doug Zongkerd4208f92010-09-20 12:16:13 -0700588 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700589 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700590 char* copy = copy_sideloaded_package(new_path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700591 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700592 if (copy) {
593 result = install_package(copy);
594 free(copy);
595 } else {
596 result = INSTALL_ERROR;
597 }
Doug Zongker8674a722010-09-15 11:08:23 -0700598 break;
599 }
600 } while (true);
601
602 int i;
603 for (i = 0; i < z_size; ++i) free(zips[i]);
604 free(zips);
605 free(headers);
606
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700607 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700608 return result;
609}
610
Doug Zongkerf93d8162009-09-22 15:16:02 -0700611static void
612wipe_data(int confirm) {
613 if (confirm) {
614 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700615
Doug Zongkerf93d8162009-09-22 15:16:02 -0700616 if (title_headers == NULL) {
617 char* headers[] = { "Confirm wipe of all user data?",
618 " THIS CAN NOT BE UNDONE.",
619 "",
620 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700621 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700622 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800623
Doug Zongkerf93d8162009-09-22 15:16:02 -0700624 char* items[] = { " No",
625 " No",
626 " No",
627 " No",
628 " No",
629 " No",
630 " No",
631 " Yes -- delete all user data", // [7]
632 " No",
633 " No",
634 " No",
635 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800636
Doug Zongker8674a722010-09-15 11:08:23 -0700637 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700638 if (chosen_item != 7) {
639 return;
640 }
641 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700642
Doug Zongkerf93d8162009-09-22 15:16:02 -0700643 ui_print("\n-- Wiping data...\n");
644 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700645 erase_volume("/data");
646 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700647 ui_print("Data wipe complete.\n");
648}
649
650static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800651prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700652 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700653
654 for (;;) {
655 finish_recovery(NULL);
656 ui_reset_progress();
657
Doug Zongker8674a722010-09-15 11:08:23 -0700658 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700659
660 // device-specific code may take some action here. It may
661 // return one of the core actions handled in the switch
662 // statement below.
663 chosen_item = device_perform_action(chosen_item);
664
665 switch (chosen_item) {
666 case ITEM_REBOOT:
667 return;
668
669 case ITEM_WIPE_DATA:
670 wipe_data(ui_text_visible());
671 if (!ui_text_visible()) return;
672 break;
673
674 case ITEM_WIPE_CACHE:
675 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700676 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700677 ui_print("Cache wipe complete.\n");
678 if (!ui_text_visible()) return;
679 break;
680
681 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700682 ;
683 int status = sdcard_directory(SDCARD_ROOT);
684 if (status >= 0) {
685 if (status != INSTALL_SUCCESS) {
686 ui_set_background(BACKGROUND_ICON_ERROR);
687 ui_print("Installation aborted.\n");
688 } else if (!ui_text_visible()) {
689 return; // reboot if logs aren't visible
690 } else {
691 ui_print("\nInstall from sdcard complete.\n");
692 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700693 }
694 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800695 }
696 }
697}
698
699static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800700print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700701 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800702}
703
704int
Oscar Montemayor05231562009-11-30 08:40:57 -0800705main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800706 time_t start = time(NULL);
707
708 // If these fail, there's not really anywhere to complain...
709 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
710 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700711 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800712
713 ui_init();
Doug Zongker51266d12010-11-01 10:19:12 -0700714 ui_set_background(BACKGROUND_ICON_INSTALLING);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700715 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800716 get_args(&argc, &argv);
717
718 int previous_runs = 0;
719 const char *send_intent = NULL;
720 const char *update_package = NULL;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800721 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800722 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800723 int toggle_secure_fs = 0;
724 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800725
726 int arg;
727 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
728 switch (arg) {
729 case 'p': previous_runs = atoi(optarg); break;
730 case 's': send_intent = optarg; break;
731 case 'u': update_package = optarg; break;
732 case 'w': wipe_data = wipe_cache = 1; break;
733 case 'c': wipe_cache = 1; break;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800734 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700735 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800736 case '?':
737 LOGE("Invalid command argument\n");
738 continue;
739 }
740 }
741
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800742 device_recovery_start();
743
Doug Zongker56c51052010-07-01 09:18:44 -0700744 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800745 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700746 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800747 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700748 printf("\n");
749
750 if (update_package) {
751 // For backwards compatibility on the cache partition only, if
752 // we're given an old 'root' path "CACHE:foo", change it to
753 // "/cache/foo".
754 if (strncmp(update_package, "CACHE:", 6) == 0) {
755 int len = strlen(update_package) + 10;
756 char* modified_path = malloc(len);
757 strlcpy(modified_path, "/cache/", len);
758 strlcat(modified_path, update_package+6, len);
759 printf("(replacing path \"%s\" with \"%s\")\n",
760 update_package, modified_path);
761 update_package = modified_path;
762 }
763 }
764 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800765
766 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700767 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800768
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800769 int status = INSTALL_SUCCESS;
770
Oscar Montemayor52219a62010-02-25 16:47:02 -0800771 if (toggle_secure_fs) {
772 if (strcmp(encrypted_fs_mode,"on") == 0) {
773 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800774 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800775 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
776 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800777 ui_print("Disabling Encrypted FS.\n");
778 } else {
779 ui_print("Error: invalid Encrypted FS setting.\n");
780 status = INSTALL_ERROR;
781 }
782
783 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
784 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor52219a62010-02-25 16:47:02 -0800785 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
786 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800787 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800788 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800789 }
790
791 if (status != INSTALL_ERROR) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700792 if (erase_volume("/data")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800793 ui_print("Data wipe failed.\n");
794 status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700795 } else if (erase_volume("/cache")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800796 ui_print("Cache wipe failed.\n");
797 status = INSTALL_ERROR;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800798 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
799 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800800 ui_print("Encrypted FS change aborted.\n");
801 status = INSTALL_ERROR;
802 } else {
803 ui_print("Successfully updated Encrypted FS.\n");
804 status = INSTALL_SUCCESS;
805 }
806 }
807 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800808 status = install_package(update_package);
809 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700810 } else if (wipe_data) {
811 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700812 if (erase_volume("/data")) status = INSTALL_ERROR;
813 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800814 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700815 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700816 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700817 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800818 } else {
819 status = INSTALL_ERROR; // No command specified
820 }
821
822 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700823 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700824 prompt_and_wait();
825 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800826
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800827 // Otherwise, get ready to boot the main system...
828 finish_recovery(send_intent);
829 ui_print("Rebooting...\n");
830 sync();
831 reboot(RB_AUTOBOOT);
832 return EXIT_SUCCESS;
833}