blob: 6f9f8e75015049e60173dbf2d8e2a7fd735ec81f [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
Doug Zongker9b125b02010-09-22 12:01:37 -070091 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092 * 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 Zongkerc18eeb82010-09-21 16:49:26 -0700459 ensure_path_mounted(SDCARD_ROOT);
460
Doug Zongker8674a722010-09-15 11:08:23 -0700461 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700462 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700463 "",
464 NULL };
465 DIR* d;
466 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700467 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700468 if (d == NULL) {
469 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700470 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700471 return 0;
472 }
473
474 char** headers = prepend_title(MENU_HEADERS);
475
476 int d_size = 0;
477 int d_alloc = 10;
478 char** dirs = malloc(d_alloc * sizeof(char*));
479 int z_size = 1;
480 int z_alloc = 10;
481 char** zips = malloc(z_alloc * sizeof(char*));
482 zips[0] = strdup("../");
483
484 while ((de = readdir(d)) != NULL) {
485 int name_len = strlen(de->d_name);
486
487 if (de->d_type == DT_DIR) {
488 // skip "." and ".." entries
489 if (name_len == 1 && de->d_name[0] == '.') continue;
490 if (name_len == 2 && de->d_name[0] == '.' &&
491 de->d_name[1] == '.') continue;
492
493 if (d_size >= d_alloc) {
494 d_alloc *= 2;
495 dirs = realloc(dirs, d_alloc * sizeof(char*));
496 }
497 dirs[d_size] = malloc(name_len + 2);
498 strcpy(dirs[d_size], de->d_name);
499 dirs[d_size][name_len] = '/';
500 dirs[d_size][name_len+1] = '\0';
501 ++d_size;
502 } else if (de->d_type == DT_REG &&
503 name_len >= 4 &&
504 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
505 if (z_size >= z_alloc) {
506 z_alloc *= 2;
507 zips = realloc(zips, z_alloc * sizeof(char*));
508 }
509 zips[z_size++] = strdup(de->d_name);
510 }
511 }
512 closedir(d);
513
514 qsort(dirs, d_size, sizeof(char*), compare_string);
515 qsort(zips, z_size, sizeof(char*), compare_string);
516
517 // append dirs to the zips list
518 if (d_size + z_size + 1 > z_alloc) {
519 z_alloc = d_size + z_size + 1;
520 zips = realloc(zips, z_alloc * sizeof(char*));
521 }
522 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
523 free(dirs);
524 z_size += d_size;
525 zips[z_size] = NULL;
526
527 int result;
528 int chosen_item = 0;
529 do {
530 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
531
532 char* item = zips[chosen_item];
533 int item_len = strlen(item);
534 if (chosen_item == 0) { // item 0 is always "../"
535 // go up but continue browsing (if the caller is sdcard_directory)
536 result = -1;
537 break;
538 } else if (item[item_len-1] == '/') {
539 // recurse down into a subdirectory
540 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700541 strlcpy(new_path, path, PATH_MAX);
542 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700543 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700544 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700545 result = sdcard_directory(new_path);
546 if (result >= 0) break;
547 } else {
548 // selected a zip file: attempt to install it, and return
549 // the status to the caller.
550 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700551 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700552 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700553 strlcat(new_path, item, PATH_MAX);
554
Doug Zongkerd4208f92010-09-20 12:16:13 -0700555 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700556 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700557 char* copy = copy_sideloaded_package(new_path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700558 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700559 if (copy) {
560 result = install_package(copy);
561 free(copy);
562 } else {
563 result = INSTALL_ERROR;
564 }
Doug Zongker8674a722010-09-15 11:08:23 -0700565 break;
566 }
567 } while (true);
568
569 int i;
570 for (i = 0; i < z_size; ++i) free(zips[i]);
571 free(zips);
572 free(headers);
573
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700574 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700575 return result;
576}
577
Doug Zongkerf93d8162009-09-22 15:16:02 -0700578static void
579wipe_data(int confirm) {
580 if (confirm) {
581 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700582
Doug Zongkerf93d8162009-09-22 15:16:02 -0700583 if (title_headers == NULL) {
584 char* headers[] = { "Confirm wipe of all user data?",
585 " THIS CAN NOT BE UNDONE.",
586 "",
587 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700588 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700589 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800590
Doug Zongkerf93d8162009-09-22 15:16:02 -0700591 char* items[] = { " No",
592 " No",
593 " No",
594 " No",
595 " No",
596 " No",
597 " No",
598 " Yes -- delete all user data", // [7]
599 " No",
600 " No",
601 " No",
602 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800603
Doug Zongker8674a722010-09-15 11:08:23 -0700604 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700605 if (chosen_item != 7) {
606 return;
607 }
608 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700609
Doug Zongkerf93d8162009-09-22 15:16:02 -0700610 ui_print("\n-- Wiping data...\n");
611 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700612 erase_volume("/data");
613 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700614 ui_print("Data wipe complete.\n");
615}
616
617static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800618prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700619 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700620
621 for (;;) {
622 finish_recovery(NULL);
623 ui_reset_progress();
624
Doug Zongker8674a722010-09-15 11:08:23 -0700625 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700626
627 // device-specific code may take some action here. It may
628 // return one of the core actions handled in the switch
629 // statement below.
630 chosen_item = device_perform_action(chosen_item);
631
632 switch (chosen_item) {
633 case ITEM_REBOOT:
634 return;
635
636 case ITEM_WIPE_DATA:
637 wipe_data(ui_text_visible());
638 if (!ui_text_visible()) return;
639 break;
640
641 case ITEM_WIPE_CACHE:
642 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700643 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700644 ui_print("Cache wipe complete.\n");
645 if (!ui_text_visible()) return;
646 break;
647
648 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700649 ;
650 int status = sdcard_directory(SDCARD_ROOT);
651 if (status >= 0) {
652 if (status != INSTALL_SUCCESS) {
653 ui_set_background(BACKGROUND_ICON_ERROR);
654 ui_print("Installation aborted.\n");
655 } else if (!ui_text_visible()) {
656 return; // reboot if logs aren't visible
657 } else {
658 ui_print("\nInstall from sdcard complete.\n");
659 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700660 }
661 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800662 }
663 }
664}
665
666static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800667print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700668 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800669}
670
671int
Oscar Montemayor05231562009-11-30 08:40:57 -0800672main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800673 time_t start = time(NULL);
674
675 // If these fail, there's not really anywhere to complain...
676 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
677 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700678 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800679
680 ui_init();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700681 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800682 get_args(&argc, &argv);
683
684 int previous_runs = 0;
685 const char *send_intent = NULL;
686 const char *update_package = NULL;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800687 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800688 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800689 int toggle_secure_fs = 0;
690 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800691
692 int arg;
693 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
694 switch (arg) {
695 case 'p': previous_runs = atoi(optarg); break;
696 case 's': send_intent = optarg; break;
697 case 'u': update_package = optarg; break;
698 case 'w': wipe_data = wipe_cache = 1; break;
699 case 'c': wipe_cache = 1; break;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800700 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700701 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800702 case '?':
703 LOGE("Invalid command argument\n");
704 continue;
705 }
706 }
707
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800708 device_recovery_start();
709
Doug Zongker56c51052010-07-01 09:18:44 -0700710 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800711 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700712 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800713 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700714 printf("\n");
715
716 if (update_package) {
717 // For backwards compatibility on the cache partition only, if
718 // we're given an old 'root' path "CACHE:foo", change it to
719 // "/cache/foo".
720 if (strncmp(update_package, "CACHE:", 6) == 0) {
721 int len = strlen(update_package) + 10;
722 char* modified_path = malloc(len);
723 strlcpy(modified_path, "/cache/", len);
724 strlcat(modified_path, update_package+6, len);
725 printf("(replacing path \"%s\" with \"%s\")\n",
726 update_package, modified_path);
727 update_package = modified_path;
728 }
729 }
730 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800731
732 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700733 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800734
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800735 int status = INSTALL_SUCCESS;
736
Oscar Montemayor52219a62010-02-25 16:47:02 -0800737 if (toggle_secure_fs) {
738 if (strcmp(encrypted_fs_mode,"on") == 0) {
739 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800740 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800741 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
742 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800743 ui_print("Disabling Encrypted FS.\n");
744 } else {
745 ui_print("Error: invalid Encrypted FS setting.\n");
746 status = INSTALL_ERROR;
747 }
748
749 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
750 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor52219a62010-02-25 16:47:02 -0800751 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
752 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800753 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800754 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800755 }
756
757 if (status != INSTALL_ERROR) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700758 if (erase_volume("/data")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800759 ui_print("Data wipe failed.\n");
760 status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700761 } else if (erase_volume("/cache")) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800762 ui_print("Cache wipe failed.\n");
763 status = INSTALL_ERROR;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800764 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
765 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800766 ui_print("Encrypted FS change aborted.\n");
767 status = INSTALL_ERROR;
768 } else {
769 ui_print("Successfully updated Encrypted FS.\n");
770 status = INSTALL_SUCCESS;
771 }
772 }
773 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800774 status = install_package(update_package);
775 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700776 } else if (wipe_data) {
777 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700778 if (erase_volume("/data")) status = INSTALL_ERROR;
779 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800780 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700781 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700782 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700783 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800784 } else {
785 status = INSTALL_ERROR; // No command specified
786 }
787
788 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700789 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700790 prompt_and_wait();
791 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800792
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800793 // Otherwise, get ready to boot the main system...
794 finish_recovery(send_intent);
795 ui_print("Rebooting...\n");
796 sync();
797 reboot(RB_AUTOBOOT);
798 return EXIT_SUCCESS;
799}