blob: f5636d846315f0ff6e23e3e3bf0343ba8048e086 [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";
56static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070058static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059
60/*
61 * The recovery tool communicates with the main system through /cache files.
62 * /cache/recovery/command - INPUT - command line for tool, one arg per line
63 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
64 * /cache/recovery/intent - OUTPUT - intent that was passed in
65 *
66 * The arguments which may be supplied in the recovery.command file:
67 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070068 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 * --wipe_data - erase user data (and cache), then reboot
70 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080071 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072 *
73 * After completing, we remove /cache/recovery/command and reboot.
74 * Arguments may also be supplied in the bootloader control block (BCB).
75 * These important scenarios must be safely restartable at any point:
76 *
77 * FACTORY RESET
78 * 1. user selects "factory reset"
79 * 2. main system writes "--wipe_data" to /cache/recovery/command
80 * 3. main system reboots into recovery
81 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
82 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070083 * 5. erase_volume() reformats /data
84 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085 * 7. finish_recovery() erases BCB
86 * -- after this, rebooting will restart the main system --
87 * 8. main() calls reboot() to boot main system
88 *
89 * OTA INSTALL
90 * 1. main system downloads OTA package to /cache/some-filename.zip
91 * 2. main system writes "--update_package=CACHE:some-filename.zip"
92 * 3. main system reboots into recovery
93 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
94 * -- after this, rebooting will attempt to reinstall the update --
95 * 5. install_package() attempts to install the update
96 * NOTE: the package install must itself be restartable from any point
97 * 6. finish_recovery() erases BCB
98 * -- after this, rebooting will (try to) restart the main system --
99 * 7. ** if install failed **
100 * 7a. prompt_and_wait() shows an error icon and waits for the user
101 * 7b; the user reboots (pulling the battery, etc) into the main system
102 * 8. main() calls maybe_install_firmware_update()
103 * ** if the update contained radio/hboot firmware **:
104 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
105 * -- after this, rebooting will reformat cache & restart main system --
106 * 8b. m_i_f_u() writes firmware image into raw cache partition
107 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
108 * -- after this, rebooting will attempt to reinstall firmware --
109 * 8d. bootloader tries to flash firmware
110 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
111 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700112 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113 * 8g. finish_recovery() erases BCB
114 * -- after this, rebooting will (try to) restart the main system --
115 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800116 *
Oscar Montemayor52219a62010-02-25 16:47:02 -0800117 * SECURE FILE SYSTEMS ENABLE/DISABLE
Oscar Montemayor05231562009-11-30 08:40:57 -0800118 * 1. user selects "enable encrypted file systems"
Oscar Montemayor52219a62010-02-25 16:47:02 -0800119 * 2. main system writes "--set_encrypted_filesystems=on|off" to
Oscar Montemayor05231562009-11-30 08:40:57 -0800120 * /cache/recovery/command
121 * 3. main system reboots into recovery
122 * 4. get_args() writes BCB with "boot-recovery" and
123 * "--set_encrypted_filesystems=on|off"
124 * -- after this, rebooting will restart the transition --
125 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
126 * Settings include: property to specify the Encrypted FS istatus and
127 * FS encryption key if enabled (not yet implemented)
Doug Zongkerd4208f92010-09-20 12:16:13 -0700128 * 6. erase_volume() reformats /data
129 * 7. erase_volume() reformats /cache
Oscar Montemayor05231562009-11-30 08:40:57 -0800130 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
131 * Settings include: property to specify the Encrypted FS status and
132 * FS encryption key if enabled (not yet implemented)
133 * 9. finish_recovery() erases BCB
134 * -- after this, rebooting will restart the main system --
135 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 */
137
138static const int MAX_ARG_LENGTH = 4096;
139static const int MAX_ARGS = 100;
140
Doug Zongkerd4208f92010-09-20 12:16:13 -0700141// open a given path, mounting partitions as necessary
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142static FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700143fopen_path(const char *path, const char *mode) {
144 if (ensure_path_mounted(path) != 0) {
145 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146 return NULL;
147 }
148
149 // When writing, try to create the containing directory, if necessary.
150 // Use generous permissions, the system (init.rc) will reset them.
151 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
152
153 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 return fp;
155}
156
157// close a file, log an error if the error indicator is set
158static void
159check_and_fclose(FILE *fp, const char *name) {
160 fflush(fp);
161 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
162 fclose(fp);
163}
164
165// command line args come from, in decreasing precedence:
166// - the actual command line
167// - the bootloader control block (one per line, after "recovery")
168// - the contents of COMMAND_FILE (one per line)
169static void
170get_args(int *argc, char ***argv) {
171 struct bootloader_message boot;
172 memset(&boot, 0, sizeof(boot));
173 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
174
175 if (boot.command[0] != 0 && boot.command[0] != 255) {
176 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
177 }
178
179 if (boot.status[0] != 0 && boot.status[0] != 255) {
180 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
181 }
182
183 // --- if arguments weren't supplied, look in the bootloader control block
184 if (*argc <= 1) {
185 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
186 const char *arg = strtok(boot.recovery, "\n");
187 if (arg != NULL && !strcmp(arg, "recovery")) {
188 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
189 (*argv)[0] = strdup(arg);
190 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
191 if ((arg = strtok(NULL, "\n")) == NULL) break;
192 (*argv)[*argc] = strdup(arg);
193 }
194 LOGI("Got arguments from boot message\n");
195 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
196 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
197 }
198 }
199
200 // --- if that doesn't work, try the command file
201 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700202 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 if (fp != NULL) {
204 char *argv0 = (*argv)[0];
205 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
206 (*argv)[0] = argv0; // use the same program name
207
208 char buf[MAX_ARG_LENGTH];
209 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
210 if (!fgets(buf, sizeof(buf), fp)) break;
211 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
212 }
213
214 check_and_fclose(fp, COMMAND_FILE);
215 LOGI("Got arguments from %s\n", COMMAND_FILE);
216 }
217 }
218
219 // --> write the arguments we have back into the bootloader control block
220 // always boot into recovery after this (until finish_recovery() is called)
221 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
222 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
223 int i;
224 for (i = 1; i < *argc; ++i) {
225 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
226 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
227 }
228 set_bootloader_message(&boot);
229}
230
Doug Zongker34c98df2009-08-18 12:05:45 -0700231static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800232set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700233 struct bootloader_message boot;
234 memset(&boot, 0, sizeof(boot));
235 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
236 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
237 set_bootloader_message(&boot);
238}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239
240// clear the recovery command and prepare to boot a (hopefully working) system,
241// copy our log file to cache as well (for the system to read), and
242// record any intent we were asked to communicate back to the system.
243// this function is idempotent: call it as many times as you like.
244static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800245finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800246 // By this point, we're ready to return to the main system...
247 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700248 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000249 if (fp == NULL) {
250 LOGE("Can't open %s\n", INTENT_FILE);
251 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252 fputs(send_intent, fp);
253 check_and_fclose(fp, INTENT_FILE);
254 }
255 }
256
257 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700258 FILE *log = fopen_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000259 if (log == NULL) {
260 LOGE("Can't open %s\n", LOG_FILE);
261 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800262 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
263 if (tmplog == NULL) {
264 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
265 } else {
266 static long tmplog_offset = 0;
267 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
268 char buf[4096];
269 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
270 tmplog_offset = ftell(tmplog);
271 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
272 }
273 check_and_fclose(log, LOG_FILE);
274 }
275
Oscar Montemayor05231562009-11-30 08:40:57 -0800276 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 struct bootloader_message boot;
278 memset(&boot, 0, sizeof(boot));
279 set_bootloader_message(&boot);
280
281 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700282 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
283 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800284 LOGW("Can't unlink %s\n", COMMAND_FILE);
285 }
286
287 sync(); // For good measure.
288}
289
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700291erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 ui_set_background(BACKGROUND_ICON_INSTALLING);
293 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700294 ui_print("Formatting %s...\n", volume);
295 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296}
297
Doug Zongker23ceeea2010-07-08 17:27:55 -0700298static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700299copy_sideloaded_package(const char* original_path) {
300 if (ensure_path_mounted(original_path) != 0) {
301 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700302 return NULL;
303 }
304
Doug Zongkerd4208f92010-09-20 12:16:13 -0700305 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700306 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
307 return NULL;
308 }
309
Doug Zongkerd4208f92010-09-20 12:16:13 -0700310 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700311 if (errno != EEXIST) {
312 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
313 return NULL;
314 }
315 }
316
Doug Zongkerd4208f92010-09-20 12:16:13 -0700317 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
318 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700319 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700320 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
321 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700322 return NULL;
323 }
324 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700325 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700326 return NULL;
327 }
328 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700329 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700330 return NULL;
331 }
332 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700333 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700334 return NULL;
335 }
336
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 char copy_path[PATH_MAX];
338 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700339 strcat(copy_path, "/package.zip");
340
341 char* buffer = malloc(BUFSIZ);
342 if (buffer == NULL) {
343 LOGE("Failed to allocate buffer\n");
344 return NULL;
345 }
346
347 size_t read;
348 FILE* fin = fopen(original_path, "rb");
349 if (fin == NULL) {
350 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
351 return NULL;
352 }
353 FILE* fout = fopen(copy_path, "wb");
354 if (fout == NULL) {
355 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
356 return NULL;
357 }
358
359 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
360 if (fwrite(buffer, 1, read, fout) != read) {
361 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
362 return NULL;
363 }
364 }
365
366 free(buffer);
367
368 if (fclose(fout) != 0) {
369 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
370 return NULL;
371 }
372
373 if (fclose(fin) != 0) {
374 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
375 return NULL;
376 }
377
378 // "adb push" is happy to overwrite read-only files when it's
379 // running as root, but we'll try anyway.
380 if (chmod(copy_path, 0400) != 0) {
381 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
382 return NULL;
383 }
384
Doug Zongkerd4208f92010-09-20 12:16:13 -0700385 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700386}
387
Doug Zongkerf93d8162009-09-22 15:16:02 -0700388static char**
Doug Zongker8674a722010-09-15 11:08:23 -0700389prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700390 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700391 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700392 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800393 NULL };
394
Doug Zongkerd6837852009-06-17 22:07:13 -0700395 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700396 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700397 int count = 0;
398 char** p;
399 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700400 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700401
Doug Zongkerf93d8162009-09-22 15:16:02 -0700402 char** new_headers = malloc((count+1) * sizeof(char*));
403 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700404 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700405 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700406 *h = NULL;
407
Doug Zongkerf93d8162009-09-22 15:16:02 -0700408 return new_headers;
409}
410
411static int
Doug Zongker8674a722010-09-15 11:08:23 -0700412get_menu_selection(char** headers, char** items, int menu_only,
413 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700414 // throw away keys pressed previously, so user doesn't
415 // accidentally trigger menu items.
416 ui_clear_key_queue();
417
Doug Zongker8674a722010-09-15 11:08:23 -0700418 ui_start_menu(headers, items, initial_selection);
419 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800420 int chosen_item = -1;
421
Doug Zongkerf93d8162009-09-22 15:16:02 -0700422 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800423 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800424 int visible = ui_text_visible();
425
Doug Zongkerddd6a282009-06-09 12:22:33 -0700426 int action = device_handle_key(key, visible);
427
428 if (action < 0) {
429 switch (action) {
430 case HIGHLIGHT_UP:
431 --selected;
432 selected = ui_menu_select(selected);
433 break;
434 case HIGHLIGHT_DOWN:
435 ++selected;
436 selected = ui_menu_select(selected);
437 break;
438 case SELECT_ITEM:
439 chosen_item = selected;
440 break;
441 case NO_ACTION:
442 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800443 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700444 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700445 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800446 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700447 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800448
Doug Zongkerf93d8162009-09-22 15:16:02 -0700449 ui_end_menu();
450 return chosen_item;
451}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800452
Doug Zongker8674a722010-09-15 11:08:23 -0700453static int compare_string(const void* a, const void* b) {
454 return strcmp(*(const char**)a, *(const char**)b);
455}
456
457static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700458sdcard_directory(const char* path) {
Doug Zongker8674a722010-09-15 11:08:23 -0700459 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700460 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700461 "",
462 NULL };
463 DIR* d;
464 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700465 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700466 if (d == NULL) {
467 LOGE("error opening %s: %s\n", path, strerror(errno));
468 return 0;
469 }
470
471 char** headers = prepend_title(MENU_HEADERS);
472
473 int d_size = 0;
474 int d_alloc = 10;
475 char** dirs = malloc(d_alloc * sizeof(char*));
476 int z_size = 1;
477 int z_alloc = 10;
478 char** zips = malloc(z_alloc * sizeof(char*));
479 zips[0] = strdup("../");
480
481 while ((de = readdir(d)) != NULL) {
482 int name_len = strlen(de->d_name);
483
484 if (de->d_type == DT_DIR) {
485 // skip "." and ".." entries
486 if (name_len == 1 && de->d_name[0] == '.') continue;
487 if (name_len == 2 && de->d_name[0] == '.' &&
488 de->d_name[1] == '.') continue;
489
490 if (d_size >= d_alloc) {
491 d_alloc *= 2;
492 dirs = realloc(dirs, d_alloc * sizeof(char*));
493 }
494 dirs[d_size] = malloc(name_len + 2);
495 strcpy(dirs[d_size], de->d_name);
496 dirs[d_size][name_len] = '/';
497 dirs[d_size][name_len+1] = '\0';
498 ++d_size;
499 } else if (de->d_type == DT_REG &&
500 name_len >= 4 &&
501 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
502 if (z_size >= z_alloc) {
503 z_alloc *= 2;
504 zips = realloc(zips, z_alloc * sizeof(char*));
505 }
506 zips[z_size++] = strdup(de->d_name);
507 }
508 }
509 closedir(d);
510
511 qsort(dirs, d_size, sizeof(char*), compare_string);
512 qsort(zips, z_size, sizeof(char*), compare_string);
513
514 // append dirs to the zips list
515 if (d_size + z_size + 1 > z_alloc) {
516 z_alloc = d_size + z_size + 1;
517 zips = realloc(zips, z_alloc * sizeof(char*));
518 }
519 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
520 free(dirs);
521 z_size += d_size;
522 zips[z_size] = NULL;
523
524 int result;
525 int chosen_item = 0;
526 do {
527 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
528
529 char* item = zips[chosen_item];
530 int item_len = strlen(item);
531 if (chosen_item == 0) { // item 0 is always "../"
532 // go up but continue browsing (if the caller is sdcard_directory)
533 result = -1;
534 break;
535 } else if (item[item_len-1] == '/') {
536 // recurse down into a subdirectory
537 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700538 strlcpy(new_path, path, PATH_MAX);
539 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700540 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700541 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700542 result = sdcard_directory(new_path);
543 if (result >= 0) break;
544 } else {
545 // selected a zip file: attempt to install it, and return
546 // the status to the caller.
547 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700548 strlcpy(new_path, path, PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700549 strlcat(new_path, item, PATH_MAX);
550
Doug Zongkerd4208f92010-09-20 12:16:13 -0700551 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700552 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700553 char* copy = copy_sideloaded_package(new_path);
554 if (copy) {
555 result = install_package(copy);
556 free(copy);
557 } else {
558 result = INSTALL_ERROR;
559 }
Doug Zongker8674a722010-09-15 11:08:23 -0700560 break;
561 }
562 } while (true);
563
564 int i;
565 for (i = 0; i < z_size; ++i) free(zips[i]);
566 free(zips);
567 free(headers);
568
569 return result;
570}
571
Doug Zongkerf93d8162009-09-22 15:16:02 -0700572static void
573wipe_data(int confirm) {
574 if (confirm) {
575 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700576
Doug Zongkerf93d8162009-09-22 15:16:02 -0700577 if (title_headers == NULL) {
578 char* headers[] = { "Confirm wipe of all user data?",
579 " THIS CAN NOT BE UNDONE.",
580 "",
581 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700582 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700583 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800584
Doug Zongkerf93d8162009-09-22 15:16:02 -0700585 char* items[] = { " No",
586 " No",
587 " No",
588 " No",
589 " No",
590 " No",
591 " No",
592 " Yes -- delete all user data", // [7]
593 " No",
594 " No",
595 " No",
596 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800597
Doug Zongker8674a722010-09-15 11:08:23 -0700598 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700599 if (chosen_item != 7) {
600 return;
601 }
602 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700603
Doug Zongkerf93d8162009-09-22 15:16:02 -0700604 ui_print("\n-- Wiping data...\n");
605 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700606 erase_volume("/data");
607 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700608 ui_print("Data wipe complete.\n");
609}
610
611static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800612prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700613 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700614
615 for (;;) {
616 finish_recovery(NULL);
617 ui_reset_progress();
618
Doug Zongker8674a722010-09-15 11:08:23 -0700619 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700620
621 // device-specific code may take some action here. It may
622 // return one of the core actions handled in the switch
623 // statement below.
624 chosen_item = device_perform_action(chosen_item);
625
626 switch (chosen_item) {
627 case ITEM_REBOOT:
628 return;
629
630 case ITEM_WIPE_DATA:
631 wipe_data(ui_text_visible());
632 if (!ui_text_visible()) return;
633 break;
634
635 case ITEM_WIPE_CACHE:
636 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700637 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700638 ui_print("Cache wipe complete.\n");
639 if (!ui_text_visible()) return;
640 break;
641
642 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700643 ;
644 int status = sdcard_directory(SDCARD_ROOT);
645 if (status >= 0) {
646 if (status != INSTALL_SUCCESS) {
647 ui_set_background(BACKGROUND_ICON_ERROR);
648 ui_print("Installation aborted.\n");
649 } else if (!ui_text_visible()) {
650 return; // reboot if logs aren't visible
651 } else {
652 ui_print("\nInstall from sdcard complete.\n");
653 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700654 }
655 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800656 }
657 }
658}
659
660static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800661print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700662 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800663}
664
665int
Oscar Montemayor05231562009-11-30 08:40:57 -0800666main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800667 time_t start = time(NULL);
668
669 // If these fail, there's not really anywhere to complain...
670 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
671 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700672 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800673
674 ui_init();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700675 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800676 get_args(&argc, &argv);
677
678 int previous_runs = 0;
679 const char *send_intent = NULL;
680 const char *update_package = NULL;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800681 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800682 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800683 int toggle_secure_fs = 0;
684 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800685
686 int arg;
687 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
688 switch (arg) {
689 case 'p': previous_runs = atoi(optarg); break;
690 case 's': send_intent = optarg; break;
691 case 'u': update_package = optarg; break;
692 case 'w': wipe_data = wipe_cache = 1; break;
693 case 'c': wipe_cache = 1; break;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800694 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700695 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800696 case '?':
697 LOGE("Invalid command argument\n");
698 continue;
699 }
700 }
701
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800702 device_recovery_start();
703
Doug Zongker56c51052010-07-01 09:18:44 -0700704 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800705 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700706 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800707 }
Doug Zongker56c51052010-07-01 09:18:44 -0700708 printf("\n\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800709
710 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700711 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800712
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800713 int status = INSTALL_SUCCESS;
714
Oscar Montemayor52219a62010-02-25 16:47:02 -0800715 if (toggle_secure_fs) {
716 if (strcmp(encrypted_fs_mode,"on") == 0) {
717 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800718 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800719 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
720 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800721 ui_print("Disabling Encrypted FS.\n");
722 } else {
723 ui_print("Error: invalid Encrypted FS setting.\n");
724 status = INSTALL_ERROR;
725 }
726
727 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
728 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor52219a62010-02-25 16:47:02 -0800729 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
730 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800731 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800732 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800733 }
734
735 if (status != INSTALL_ERROR) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700736 if (erase_volume("/data")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800737 ui_print("Data wipe failed.\n");
738 status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700739 } else if (erase_volume("/cache")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800740 ui_print("Cache wipe failed.\n");
741 status = INSTALL_ERROR;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800742 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
743 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800744 ui_print("Encrypted FS change aborted.\n");
745 status = INSTALL_ERROR;
746 } else {
747 ui_print("Successfully updated Encrypted FS.\n");
748 status = INSTALL_SUCCESS;
749 }
750 }
751 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800752 status = install_package(update_package);
753 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700754 } else if (wipe_data) {
755 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700756 if (erase_volume("/data")) status = INSTALL_ERROR;
757 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800758 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700759 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700760 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700761 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800762 } else {
763 status = INSTALL_ERROR; // No command specified
764 }
765
766 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700767 if (status != INSTALL_SUCCESS || ui_text_visible()) {
768 // Mount the sdcard when the menu is enabled so you can "adb
769 // push" packages to the sdcard and immediately install them.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700770 ensure_path_mounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700771 prompt_and_wait();
772 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800773
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800774 // Otherwise, get ready to boot the main system...
775 finish_recovery(send_intent);
776 ui_print("Rebooting...\n");
777 sync();
778 reboot(RB_AUTOBOOT);
779 return EXIT_SUCCESS;
780}