blob: 30bbe38a131b160f14f61eda3b4a0e31aca6015e [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
53static const char *COMMAND_FILE = "CACHE:recovery/command";
54static const char *INTENT_FILE = "CACHE:recovery/intent";
55static const char *LOG_FILE = "CACHE:recovery/log";
Doug Zongker8674a722010-09-15 11:08:23 -070056static 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 Zongker23ceeea2010-07-08 17:27:55 -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
68 * --update_package=root:path - verify install an OTA package file
69 * --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 --
83 * 5. erase_root() reformats /data
84 * 6. erase_root() reformats /cache
85 * 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 --
112 * 8f. erase_root() reformats /cache
113 * 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)
128 * 6. erase_root() reformats /data
129 * 7. erase_root() reformats /cache
130 * 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
141// open a file given in root:path format, mounting partitions as necessary
142static FILE*
143fopen_root_path(const char *root_path, const char *mode) {
144 if (ensure_root_path_mounted(root_path) != 0) {
145 LOGE("Can't mount %s\n", root_path);
146 return NULL;
147 }
148
149 char path[PATH_MAX] = "";
150 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
151 LOGE("Bad path %s\n", root_path);
152 return NULL;
153 }
154
155 // When writing, try to create the containing directory, if necessary.
156 // Use generous permissions, the system (init.rc) will reset them.
157 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
158
159 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160 return fp;
161}
162
163// close a file, log an error if the error indicator is set
164static void
165check_and_fclose(FILE *fp, const char *name) {
166 fflush(fp);
167 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
168 fclose(fp);
169}
170
171// command line args come from, in decreasing precedence:
172// - the actual command line
173// - the bootloader control block (one per line, after "recovery")
174// - the contents of COMMAND_FILE (one per line)
175static void
176get_args(int *argc, char ***argv) {
177 struct bootloader_message boot;
178 memset(&boot, 0, sizeof(boot));
179 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
180
181 if (boot.command[0] != 0 && boot.command[0] != 255) {
182 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
183 }
184
185 if (boot.status[0] != 0 && boot.status[0] != 255) {
186 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
187 }
188
189 // --- if arguments weren't supplied, look in the bootloader control block
190 if (*argc <= 1) {
191 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
192 const char *arg = strtok(boot.recovery, "\n");
193 if (arg != NULL && !strcmp(arg, "recovery")) {
194 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
195 (*argv)[0] = strdup(arg);
196 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
197 if ((arg = strtok(NULL, "\n")) == NULL) break;
198 (*argv)[*argc] = strdup(arg);
199 }
200 LOGI("Got arguments from boot message\n");
201 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
202 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
203 }
204 }
205
206 // --- if that doesn't work, try the command file
207 if (*argc <= 1) {
208 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
209 if (fp != NULL) {
210 char *argv0 = (*argv)[0];
211 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
212 (*argv)[0] = argv0; // use the same program name
213
214 char buf[MAX_ARG_LENGTH];
215 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
216 if (!fgets(buf, sizeof(buf), fp)) break;
217 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
218 }
219
220 check_and_fclose(fp, COMMAND_FILE);
221 LOGI("Got arguments from %s\n", COMMAND_FILE);
222 }
223 }
224
225 // --> write the arguments we have back into the bootloader control block
226 // always boot into recovery after this (until finish_recovery() is called)
227 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
228 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
229 int i;
230 for (i = 1; i < *argc; ++i) {
231 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
232 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
233 }
234 set_bootloader_message(&boot);
235}
236
Doug Zongker34c98df2009-08-18 12:05:45 -0700237static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800238set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700239 struct bootloader_message boot;
240 memset(&boot, 0, sizeof(boot));
241 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
242 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
243 set_bootloader_message(&boot);
244}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245
246// clear the recovery command and prepare to boot a (hopefully working) system,
247// copy our log file to cache as well (for the system to read), and
248// record any intent we were asked to communicate back to the system.
249// this function is idempotent: call it as many times as you like.
250static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800251finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252 // By this point, we're ready to return to the main system...
253 if (send_intent != NULL) {
254 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000255 if (fp == NULL) {
256 LOGE("Can't open %s\n", INTENT_FILE);
257 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800258 fputs(send_intent, fp);
259 check_and_fclose(fp, INTENT_FILE);
260 }
261 }
262
263 // Copy logs to cache so the system can find out what happened.
264 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000265 if (log == NULL) {
266 LOGE("Can't open %s\n", LOG_FILE);
267 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
269 if (tmplog == NULL) {
270 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
271 } else {
272 static long tmplog_offset = 0;
273 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
274 char buf[4096];
275 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
276 tmplog_offset = ftell(tmplog);
277 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
278 }
279 check_and_fclose(log, LOG_FILE);
280 }
281
Oscar Montemayor05231562009-11-30 08:40:57 -0800282 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283 struct bootloader_message boot;
284 memset(&boot, 0, sizeof(boot));
285 set_bootloader_message(&boot);
286
287 // Remove the command file, so recovery won't repeat indefinitely.
288 char path[PATH_MAX] = "";
289 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
290 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
291 (unlink(path) && errno != ENOENT)) {
292 LOGW("Can't unlink %s\n", COMMAND_FILE);
293 }
294
295 sync(); // For good measure.
296}
297
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800298static int
Oscar Montemayor05231562009-11-30 08:40:57 -0800299erase_root(const char *root) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300 ui_set_background(BACKGROUND_ICON_INSTALLING);
301 ui_show_indeterminate_progress();
302 ui_print("Formatting %s...\n", root);
303 return format_root_device(root);
304}
305
Doug Zongker23ceeea2010-07-08 17:27:55 -0700306static char*
307copy_sideloaded_package(const char* original_root_path) {
308 if (ensure_root_path_mounted(original_root_path) != 0) {
309 LOGE("Can't mount %s\n", original_root_path);
310 return NULL;
311 }
312
313 char original_path[PATH_MAX] = "";
314 if (translate_root_path(original_root_path, original_path,
315 sizeof(original_path)) == NULL) {
316 LOGE("Bad path %s\n", original_root_path);
317 return NULL;
318 }
319
320 if (ensure_root_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
321 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
322 return NULL;
323 }
324
325 char copy_path[PATH_MAX] = "";
326 if (translate_root_path(SIDELOAD_TEMP_DIR, copy_path,
327 sizeof(copy_path)) == NULL) {
328 LOGE("Bad path %s\n", SIDELOAD_TEMP_DIR);
329 return NULL;
330 }
331
332 if (mkdir(copy_path, 0700) != 0) {
333 if (errno != EEXIST) {
334 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
335 return NULL;
336 }
337 }
338
339 struct stat st;
340 if (stat(copy_path, &st) != 0) {
341 LOGE("failed to stat %s (%s)\n", copy_path, strerror(errno));
342 return NULL;
343 }
344 if (!S_ISDIR(st.st_mode)) {
345 LOGE("%s isn't a directory\n", copy_path);
346 return NULL;
347 }
348 if ((st.st_mode & 0777) != 0700) {
349 LOGE("%s has perms %o\n", copy_path, st.st_mode);
350 return NULL;
351 }
352 if (st.st_uid != 0) {
353 LOGE("%s owned by %lu; not root\n", copy_path, st.st_uid);
354 return NULL;
355 }
356
357 strcat(copy_path, "/package.zip");
358
359 char* buffer = malloc(BUFSIZ);
360 if (buffer == NULL) {
361 LOGE("Failed to allocate buffer\n");
362 return NULL;
363 }
364
365 size_t read;
366 FILE* fin = fopen(original_path, "rb");
367 if (fin == NULL) {
368 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
369 return NULL;
370 }
371 FILE* fout = fopen(copy_path, "wb");
372 if (fout == NULL) {
373 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
374 return NULL;
375 }
376
377 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
378 if (fwrite(buffer, 1, read, fout) != read) {
379 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
380 return NULL;
381 }
382 }
383
384 free(buffer);
385
386 if (fclose(fout) != 0) {
387 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
388 return NULL;
389 }
390
391 if (fclose(fin) != 0) {
392 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
393 return NULL;
394 }
395
396 // "adb push" is happy to overwrite read-only files when it's
397 // running as root, but we'll try anyway.
398 if (chmod(copy_path, 0400) != 0) {
399 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
400 return NULL;
401 }
402
403 char* copy_root_path = malloc(strlen(SIDELOAD_TEMP_DIR) + 20);
404 strcpy(copy_root_path, SIDELOAD_TEMP_DIR);
405 strcat(copy_root_path, "/package.zip");
406 return copy_root_path;
407}
408
Doug Zongkerf93d8162009-09-22 15:16:02 -0700409static char**
Doug Zongker8674a722010-09-15 11:08:23 -0700410prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700411 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700412 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700413 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800414 NULL };
415
Doug Zongkerd6837852009-06-17 22:07:13 -0700416 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700417 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700418 int count = 0;
419 char** p;
420 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700421 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700422
Doug Zongkerf93d8162009-09-22 15:16:02 -0700423 char** new_headers = malloc((count+1) * sizeof(char*));
424 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700425 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700426 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700427 *h = NULL;
428
Doug Zongkerf93d8162009-09-22 15:16:02 -0700429 return new_headers;
430}
431
432static int
Doug Zongker8674a722010-09-15 11:08:23 -0700433get_menu_selection(char** headers, char** items, int menu_only,
434 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700435 // throw away keys pressed previously, so user doesn't
436 // accidentally trigger menu items.
437 ui_clear_key_queue();
438
Doug Zongker8674a722010-09-15 11:08:23 -0700439 ui_start_menu(headers, items, initial_selection);
440 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800441 int chosen_item = -1;
442
Doug Zongkerf93d8162009-09-22 15:16:02 -0700443 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800444 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800445 int visible = ui_text_visible();
446
Doug Zongkerddd6a282009-06-09 12:22:33 -0700447 int action = device_handle_key(key, visible);
448
449 if (action < 0) {
450 switch (action) {
451 case HIGHLIGHT_UP:
452 --selected;
453 selected = ui_menu_select(selected);
454 break;
455 case HIGHLIGHT_DOWN:
456 ++selected;
457 selected = ui_menu_select(selected);
458 break;
459 case SELECT_ITEM:
460 chosen_item = selected;
461 break;
462 case NO_ACTION:
463 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700465 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700466 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800467 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700468 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800469
Doug Zongkerf93d8162009-09-22 15:16:02 -0700470 ui_end_menu();
471 return chosen_item;
472}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800473
Doug Zongker8674a722010-09-15 11:08:23 -0700474static int compare_string(const void* a, const void* b) {
475 return strcmp(*(const char**)a, *(const char**)b);
476}
477
478static int
479sdcard_directory(const char* root_path) {
480 const char* MENU_HEADERS[] = { "Choose a package to install:",
481 root_path,
482 "",
483 NULL };
484 DIR* d;
485 struct dirent* de;
486 char path[PATH_MAX];
487 d = opendir(translate_root_path(root_path, path, sizeof(path)));
488 if (d == NULL) {
489 LOGE("error opening %s: %s\n", path, strerror(errno));
490 return 0;
491 }
492
493 char** headers = prepend_title(MENU_HEADERS);
494
495 int d_size = 0;
496 int d_alloc = 10;
497 char** dirs = malloc(d_alloc * sizeof(char*));
498 int z_size = 1;
499 int z_alloc = 10;
500 char** zips = malloc(z_alloc * sizeof(char*));
501 zips[0] = strdup("../");
502
503 while ((de = readdir(d)) != NULL) {
504 int name_len = strlen(de->d_name);
505
506 if (de->d_type == DT_DIR) {
507 // skip "." and ".." entries
508 if (name_len == 1 && de->d_name[0] == '.') continue;
509 if (name_len == 2 && de->d_name[0] == '.' &&
510 de->d_name[1] == '.') continue;
511
512 if (d_size >= d_alloc) {
513 d_alloc *= 2;
514 dirs = realloc(dirs, d_alloc * sizeof(char*));
515 }
516 dirs[d_size] = malloc(name_len + 2);
517 strcpy(dirs[d_size], de->d_name);
518 dirs[d_size][name_len] = '/';
519 dirs[d_size][name_len+1] = '\0';
520 ++d_size;
521 } else if (de->d_type == DT_REG &&
522 name_len >= 4 &&
523 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
524 if (z_size >= z_alloc) {
525 z_alloc *= 2;
526 zips = realloc(zips, z_alloc * sizeof(char*));
527 }
528 zips[z_size++] = strdup(de->d_name);
529 }
530 }
531 closedir(d);
532
533 qsort(dirs, d_size, sizeof(char*), compare_string);
534 qsort(zips, z_size, sizeof(char*), compare_string);
535
536 // append dirs to the zips list
537 if (d_size + z_size + 1 > z_alloc) {
538 z_alloc = d_size + z_size + 1;
539 zips = realloc(zips, z_alloc * sizeof(char*));
540 }
541 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
542 free(dirs);
543 z_size += d_size;
544 zips[z_size] = NULL;
545
546 int result;
547 int chosen_item = 0;
548 do {
549 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
550
551 char* item = zips[chosen_item];
552 int item_len = strlen(item);
553 if (chosen_item == 0) { // item 0 is always "../"
554 // go up but continue browsing (if the caller is sdcard_directory)
555 result = -1;
556 break;
557 } else if (item[item_len-1] == '/') {
558 // recurse down into a subdirectory
559 char new_path[PATH_MAX];
560 strlcpy(new_path, root_path, PATH_MAX);
561 strlcat(new_path, item, PATH_MAX);
562 result = sdcard_directory(new_path);
563 if (result >= 0) break;
564 } else {
565 // selected a zip file: attempt to install it, and return
566 // the status to the caller.
567 char new_path[PATH_MAX];
568 strlcpy(new_path, root_path, PATH_MAX);
569 strlcat(new_path, item, PATH_MAX);
570
571 ui_print("\n-- Install %s ...\n", new_path);
572 set_sdcard_update_bootloader_message();
573 result = install_package(new_path);
574 break;
575 }
576 } while (true);
577
578 int i;
579 for (i = 0; i < z_size; ++i) free(zips[i]);
580 free(zips);
581 free(headers);
582
583 return result;
584}
585
Doug Zongkerf93d8162009-09-22 15:16:02 -0700586static void
587wipe_data(int confirm) {
588 if (confirm) {
589 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700590
Doug Zongkerf93d8162009-09-22 15:16:02 -0700591 if (title_headers == NULL) {
592 char* headers[] = { "Confirm wipe of all user data?",
593 " THIS CAN NOT BE UNDONE.",
594 "",
595 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700596 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700597 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800598
Doug Zongkerf93d8162009-09-22 15:16:02 -0700599 char* items[] = { " No",
600 " No",
601 " No",
602 " No",
603 " No",
604 " No",
605 " No",
606 " Yes -- delete all user data", // [7]
607 " No",
608 " No",
609 " No",
610 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800611
Doug Zongker8674a722010-09-15 11:08:23 -0700612 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700613 if (chosen_item != 7) {
614 return;
615 }
616 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700617
Doug Zongkerf93d8162009-09-22 15:16:02 -0700618 ui_print("\n-- Wiping data...\n");
619 device_wipe_data();
620 erase_root("DATA:");
621 erase_root("CACHE:");
622 ui_print("Data wipe complete.\n");
623}
624
625static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800626prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700627 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700628
629 for (;;) {
630 finish_recovery(NULL);
631 ui_reset_progress();
632
Doug Zongker8674a722010-09-15 11:08:23 -0700633 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700634
635 // device-specific code may take some action here. It may
636 // return one of the core actions handled in the switch
637 // statement below.
638 chosen_item = device_perform_action(chosen_item);
639
640 switch (chosen_item) {
641 case ITEM_REBOOT:
642 return;
643
644 case ITEM_WIPE_DATA:
645 wipe_data(ui_text_visible());
646 if (!ui_text_visible()) return;
647 break;
648
649 case ITEM_WIPE_CACHE:
650 ui_print("\n-- Wiping cache...\n");
651 erase_root("CACHE:");
652 ui_print("Cache wipe complete.\n");
653 if (!ui_text_visible()) return;
654 break;
655
656 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700657 ;
658 int status = sdcard_directory(SDCARD_ROOT);
659 if (status >= 0) {
660 if (status != INSTALL_SUCCESS) {
661 ui_set_background(BACKGROUND_ICON_ERROR);
662 ui_print("Installation aborted.\n");
663 } else if (!ui_text_visible()) {
664 return; // reboot if logs aren't visible
665 } else {
666 ui_print("\nInstall from sdcard complete.\n");
667 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700668 }
669 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800670 }
671 }
672}
673
674static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800675print_property(const char *key, const char *name, void *cookie) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800676 fprintf(stderr, "%s=%s\n", key, name);
677}
678
679int
Oscar Montemayor05231562009-11-30 08:40:57 -0800680main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800681 time_t start = time(NULL);
682
683 // If these fail, there's not really anywhere to complain...
684 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
685 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
686 fprintf(stderr, "Starting recovery on %s", ctime(&start));
687
688 ui_init();
689 get_args(&argc, &argv);
690
691 int previous_runs = 0;
692 const char *send_intent = NULL;
693 const char *update_package = NULL;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800694 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800695 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800696 int toggle_secure_fs = 0;
697 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800698
699 int arg;
700 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
701 switch (arg) {
702 case 'p': previous_runs = atoi(optarg); break;
703 case 's': send_intent = optarg; break;
704 case 'u': update_package = optarg; break;
705 case 'w': wipe_data = wipe_cache = 1; break;
706 case 'c': wipe_cache = 1; break;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800707 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700708 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800709 case '?':
710 LOGE("Invalid command argument\n");
711 continue;
712 }
713 }
714
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800715 device_recovery_start();
716
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800717 fprintf(stderr, "Command:");
718 for (arg = 0; arg < argc; arg++) {
719 fprintf(stderr, " \"%s\"", argv[arg]);
720 }
721 fprintf(stderr, "\n\n");
722
723 property_list(print_property, NULL);
724 fprintf(stderr, "\n");
725
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800726 int status = INSTALL_SUCCESS;
727
Oscar Montemayor52219a62010-02-25 16:47:02 -0800728 if (toggle_secure_fs) {
729 if (strcmp(encrypted_fs_mode,"on") == 0) {
730 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800731 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800732 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
733 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800734 ui_print("Disabling Encrypted FS.\n");
735 } else {
736 ui_print("Error: invalid Encrypted FS setting.\n");
737 status = INSTALL_ERROR;
738 }
739
740 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
741 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor52219a62010-02-25 16:47:02 -0800742 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
743 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800744 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor52219a62010-02-25 16:47:02 -0800745 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800746 }
747
748 if (status != INSTALL_ERROR) {
749 if (erase_root("DATA:")) {
750 ui_print("Data wipe failed.\n");
751 status = INSTALL_ERROR;
752 } else if (erase_root("CACHE:")) {
753 ui_print("Cache wipe failed.\n");
754 status = INSTALL_ERROR;
Oscar Montemayor52219a62010-02-25 16:47:02 -0800755 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
756 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800757 ui_print("Encrypted FS change aborted.\n");
758 status = INSTALL_ERROR;
759 } else {
760 ui_print("Successfully updated Encrypted FS.\n");
761 status = INSTALL_SUCCESS;
762 }
763 }
764 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800765 status = install_package(update_package);
766 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700767 } else if (wipe_data) {
768 if (device_wipe_data()) status = INSTALL_ERROR;
769 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800770 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
771 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700772 } else if (wipe_cache) {
773 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
774 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800775 } else {
776 status = INSTALL_ERROR; // No command specified
777 }
778
779 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700780 if (status != INSTALL_SUCCESS || ui_text_visible()) {
781 // Mount the sdcard when the menu is enabled so you can "adb
782 // push" packages to the sdcard and immediately install them.
783 ensure_root_path_mounted(SDCARD_ROOT);
784 prompt_and_wait();
785 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800786
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800787 // Otherwise, get ready to boot the main system...
788 finish_recovery(send_intent);
789 ui_print("Rebooting...\n");
790 sync();
791 reboot(RB_AUTOBOOT);
792 return EXIT_SUCCESS;
793}