blob: 40e79d73ae7bd633560254f77555ff5152c66e46 [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 Zongkerbe598882010-04-08 14:36:55 -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 Montemayor31f6ee82010-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 Montemayor31f6ee82010-02-25 16:47:02 -080048 { "set_encrypted_filesystems", required_argument, NULL, 'e' },
Doug Zongker988500b2009-10-06 14:41:38 -070049 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050};
51
52static const char *COMMAND_FILE = "CACHE:recovery/command";
53static const char *INTENT_FILE = "CACHE:recovery/intent";
54static const char *LOG_FILE = "CACHE:recovery/log";
Doug Zongkerdc9e87c2010-07-29 17:08:50 -070055static const char *EXT_ROOT = "EXT:";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongker23ceeea2010-07-08 17:27:55 -070057static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058
59/*
60 * The recovery tool communicates with the main system through /cache files.
61 * /cache/recovery/command - INPUT - command line for tool, one arg per line
62 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
63 * /cache/recovery/intent - OUTPUT - intent that was passed in
64 *
65 * The arguments which may be supplied in the recovery.command file:
66 * --send_intent=anystring - write the text out to recovery.intent
67 * --update_package=root:path - verify install an OTA package file
68 * --wipe_data - erase user data (and cache), then reboot
69 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080070 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071 *
72 * After completing, we remove /cache/recovery/command and reboot.
73 * Arguments may also be supplied in the bootloader control block (BCB).
74 * These important scenarios must be safely restartable at any point:
75 *
76 * FACTORY RESET
77 * 1. user selects "factory reset"
78 * 2. main system writes "--wipe_data" to /cache/recovery/command
79 * 3. main system reboots into recovery
80 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
81 * -- after this, rebooting will restart the erase --
82 * 5. erase_root() reformats /data
83 * 6. erase_root() reformats /cache
84 * 7. finish_recovery() erases BCB
85 * -- after this, rebooting will restart the main system --
86 * 8. main() calls reboot() to boot main system
87 *
88 * OTA INSTALL
89 * 1. main system downloads OTA package to /cache/some-filename.zip
90 * 2. main system writes "--update_package=CACHE:some-filename.zip"
91 * 3. main system reboots into recovery
92 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
93 * -- after this, rebooting will attempt to reinstall the update --
94 * 5. install_package() attempts to install the update
95 * NOTE: the package install must itself be restartable from any point
96 * 6. finish_recovery() erases BCB
97 * -- after this, rebooting will (try to) restart the main system --
98 * 7. ** if install failed **
99 * 7a. prompt_and_wait() shows an error icon and waits for the user
100 * 7b; the user reboots (pulling the battery, etc) into the main system
101 * 8. main() calls maybe_install_firmware_update()
102 * ** if the update contained radio/hboot firmware **:
103 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
104 * -- after this, rebooting will reformat cache & restart main system --
105 * 8b. m_i_f_u() writes firmware image into raw cache partition
106 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
107 * -- after this, rebooting will attempt to reinstall firmware --
108 * 8d. bootloader tries to flash firmware
109 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
110 * -- after this, rebooting will reformat cache & restart main system --
111 * 8f. erase_root() reformats /cache
112 * 8g. finish_recovery() erases BCB
113 * -- after this, rebooting will (try to) restart the main system --
114 * 9. main() calls reboot() to boot main system
Oscar Montemayor05231562009-11-30 08:40:57 -0800115 *
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800116 * SECURE FILE SYSTEMS ENABLE/DISABLE
Oscar Montemayor05231562009-11-30 08:40:57 -0800117 * 1. user selects "enable encrypted file systems"
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800118 * 2. main system writes "--set_encrypted_filesystems=on|off" to
Oscar Montemayor05231562009-11-30 08:40:57 -0800119 * /cache/recovery/command
120 * 3. main system reboots into recovery
121 * 4. get_args() writes BCB with "boot-recovery" and
122 * "--set_encrypted_filesystems=on|off"
123 * -- after this, rebooting will restart the transition --
124 * 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
125 * Settings include: property to specify the Encrypted FS istatus and
126 * FS encryption key if enabled (not yet implemented)
127 * 6. erase_root() reformats /data
128 * 7. erase_root() reformats /cache
129 * 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
130 * Settings include: property to specify the Encrypted FS status and
131 * FS encryption key if enabled (not yet implemented)
132 * 9. finish_recovery() erases BCB
133 * -- after this, rebooting will restart the main system --
134 * 10. main() calls reboot() to boot main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 */
136
137static const int MAX_ARG_LENGTH = 4096;
138static const int MAX_ARGS = 100;
139
140// open a file given in root:path format, mounting partitions as necessary
141static FILE*
142fopen_root_path(const char *root_path, const char *mode) {
143 if (ensure_root_path_mounted(root_path) != 0) {
144 LOGE("Can't mount %s\n", root_path);
145 return NULL;
146 }
147
148 char path[PATH_MAX] = "";
149 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
150 LOGE("Bad path %s\n", root_path);
151 return NULL;
152 }
153
154 // When writing, try to create the containing directory, if necessary.
155 // Use generous permissions, the system (init.rc) will reset them.
156 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
157
158 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159 return fp;
160}
161
162// close a file, log an error if the error indicator is set
163static void
164check_and_fclose(FILE *fp, const char *name) {
165 fflush(fp);
166 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
167 fclose(fp);
168}
169
170// command line args come from, in decreasing precedence:
171// - the actual command line
172// - the bootloader control block (one per line, after "recovery")
173// - the contents of COMMAND_FILE (one per line)
174static void
175get_args(int *argc, char ***argv) {
176 struct bootloader_message boot;
177 memset(&boot, 0, sizeof(boot));
178 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
179
180 if (boot.command[0] != 0 && boot.command[0] != 255) {
181 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
182 }
183
184 if (boot.status[0] != 0 && boot.status[0] != 255) {
185 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
186 }
187
188 // --- if arguments weren't supplied, look in the bootloader control block
189 if (*argc <= 1) {
190 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
191 const char *arg = strtok(boot.recovery, "\n");
192 if (arg != NULL && !strcmp(arg, "recovery")) {
193 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
194 (*argv)[0] = strdup(arg);
195 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
196 if ((arg = strtok(NULL, "\n")) == NULL) break;
197 (*argv)[*argc] = strdup(arg);
198 }
199 LOGI("Got arguments from boot message\n");
200 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
201 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
202 }
203 }
204
205 // --- if that doesn't work, try the command file
206 if (*argc <= 1) {
207 FILE *fp = fopen_root_path(COMMAND_FILE, "r");
208 if (fp != NULL) {
209 char *argv0 = (*argv)[0];
210 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
211 (*argv)[0] = argv0; // use the same program name
212
213 char buf[MAX_ARG_LENGTH];
214 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
215 if (!fgets(buf, sizeof(buf), fp)) break;
216 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
217 }
218
219 check_and_fclose(fp, COMMAND_FILE);
220 LOGI("Got arguments from %s\n", COMMAND_FILE);
221 }
222 }
223
224 // --> write the arguments we have back into the bootloader control block
225 // always boot into recovery after this (until finish_recovery() is called)
226 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
227 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
228 int i;
229 for (i = 1; i < *argc; ++i) {
230 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
231 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
232 }
233 set_bootloader_message(&boot);
234}
235
Doug Zongker34c98df2009-08-18 12:05:45 -0700236static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800237set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700238 struct bootloader_message boot;
239 memset(&boot, 0, sizeof(boot));
240 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
241 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
242 set_bootloader_message(&boot);
243}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244
245// clear the recovery command and prepare to boot a (hopefully working) system,
246// copy our log file to cache as well (for the system to read), and
247// record any intent we were asked to communicate back to the system.
248// this function is idempotent: call it as many times as you like.
249static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800250finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251 // By this point, we're ready to return to the main system...
252 if (send_intent != NULL) {
253 FILE *fp = fopen_root_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000254 if (fp == NULL) {
255 LOGE("Can't open %s\n", INTENT_FILE);
256 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800257 fputs(send_intent, fp);
258 check_and_fclose(fp, INTENT_FILE);
259 }
260 }
261
262 // Copy logs to cache so the system can find out what happened.
263 FILE *log = fopen_root_path(LOG_FILE, "a");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000264 if (log == NULL) {
265 LOGE("Can't open %s\n", LOG_FILE);
266 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800267 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
268 if (tmplog == NULL) {
269 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
270 } else {
271 static long tmplog_offset = 0;
272 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
273 char buf[4096];
274 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
275 tmplog_offset = ftell(tmplog);
276 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
277 }
278 check_and_fclose(log, LOG_FILE);
279 }
280
Oscar Montemayor05231562009-11-30 08:40:57 -0800281 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 struct bootloader_message boot;
283 memset(&boot, 0, sizeof(boot));
284 set_bootloader_message(&boot);
285
286 // Remove the command file, so recovery won't repeat indefinitely.
287 char path[PATH_MAX] = "";
288 if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
289 translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
290 (unlink(path) && errno != ENOENT)) {
291 LOGW("Can't unlink %s\n", COMMAND_FILE);
292 }
293
294 sync(); // For good measure.
295}
296
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297static int
Oscar Montemayor05231562009-11-30 08:40:57 -0800298erase_root(const char *root) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299 ui_set_background(BACKGROUND_ICON_INSTALLING);
300 ui_show_indeterminate_progress();
301 ui_print("Formatting %s...\n", root);
302 return format_root_device(root);
303}
304
Doug Zongker23ceeea2010-07-08 17:27:55 -0700305static char*
306copy_sideloaded_package(const char* original_root_path) {
307 if (ensure_root_path_mounted(original_root_path) != 0) {
308 LOGE("Can't mount %s\n", original_root_path);
309 return NULL;
310 }
311
312 char original_path[PATH_MAX] = "";
313 if (translate_root_path(original_root_path, original_path,
314 sizeof(original_path)) == NULL) {
315 LOGE("Bad path %s\n", original_root_path);
316 return NULL;
317 }
318
319 if (ensure_root_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
320 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
321 return NULL;
322 }
323
324 char copy_path[PATH_MAX] = "";
325 if (translate_root_path(SIDELOAD_TEMP_DIR, copy_path,
326 sizeof(copy_path)) == NULL) {
327 LOGE("Bad path %s\n", SIDELOAD_TEMP_DIR);
328 return NULL;
329 }
330
331 if (mkdir(copy_path, 0700) != 0) {
332 if (errno != EEXIST) {
333 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
334 return NULL;
335 }
336 }
337
338 struct stat st;
339 if (stat(copy_path, &st) != 0) {
340 LOGE("failed to stat %s (%s)\n", copy_path, strerror(errno));
341 return NULL;
342 }
343 if (!S_ISDIR(st.st_mode)) {
344 LOGE("%s isn't a directory\n", copy_path);
345 return NULL;
346 }
347 if ((st.st_mode & 0777) != 0700) {
348 LOGE("%s has perms %o\n", copy_path, st.st_mode);
349 return NULL;
350 }
351 if (st.st_uid != 0) {
352 LOGE("%s owned by %lu; not root\n", copy_path, st.st_uid);
353 return NULL;
354 }
355
356 strcat(copy_path, "/package.zip");
357
358 char* buffer = malloc(BUFSIZ);
359 if (buffer == NULL) {
360 LOGE("Failed to allocate buffer\n");
361 return NULL;
362 }
363
364 size_t read;
365 FILE* fin = fopen(original_path, "rb");
366 if (fin == NULL) {
367 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
368 return NULL;
369 }
370 FILE* fout = fopen(copy_path, "wb");
371 if (fout == NULL) {
372 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
373 return NULL;
374 }
375
376 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
377 if (fwrite(buffer, 1, read, fout) != read) {
378 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
379 return NULL;
380 }
381 }
382
383 free(buffer);
384
385 if (fclose(fout) != 0) {
386 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
387 return NULL;
388 }
389
390 if (fclose(fin) != 0) {
391 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
392 return NULL;
393 }
394
395 // "adb push" is happy to overwrite read-only files when it's
396 // running as root, but we'll try anyway.
397 if (chmod(copy_path, 0400) != 0) {
398 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
399 return NULL;
400 }
401
402 char* copy_root_path = malloc(strlen(SIDELOAD_TEMP_DIR) + 20);
403 strcpy(copy_root_path, SIDELOAD_TEMP_DIR);
404 strcat(copy_root_path, "/package.zip");
405 return copy_root_path;
406}
407
Doug Zongkerf93d8162009-09-22 15:16:02 -0700408static char**
Doug Zongkerbe598882010-04-08 14:36:55 -0700409prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700410 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700411 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700412 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800413 NULL };
414
Doug Zongkerd6837852009-06-17 22:07:13 -0700415 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700416 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700417 int count = 0;
418 char** p;
419 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700420 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700421
Doug Zongkerf93d8162009-09-22 15:16:02 -0700422 char** new_headers = malloc((count+1) * sizeof(char*));
423 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700424 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700425 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700426 *h = NULL;
427
Doug Zongkerf93d8162009-09-22 15:16:02 -0700428 return new_headers;
429}
430
431static int
Doug Zongkerbe598882010-04-08 14:36:55 -0700432get_menu_selection(char** headers, char** items, int menu_only,
433 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700434 // throw away keys pressed previously, so user doesn't
435 // accidentally trigger menu items.
436 ui_clear_key_queue();
437
Doug Zongkerbe598882010-04-08 14:36:55 -0700438 ui_start_menu(headers, items, initial_selection);
439 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800440 int chosen_item = -1;
441
Doug Zongkerf93d8162009-09-22 15:16:02 -0700442 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800443 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800444 int visible = ui_text_visible();
445
Doug Zongkerddd6a282009-06-09 12:22:33 -0700446 int action = device_handle_key(key, visible);
447
448 if (action < 0) {
449 switch (action) {
450 case HIGHLIGHT_UP:
451 --selected;
452 selected = ui_menu_select(selected);
453 break;
454 case HIGHLIGHT_DOWN:
455 ++selected;
456 selected = ui_menu_select(selected);
457 break;
458 case SELECT_ITEM:
459 chosen_item = selected;
460 break;
461 case NO_ACTION:
462 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800463 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700464 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700465 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800466 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700467 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468
Doug Zongkerf93d8162009-09-22 15:16:02 -0700469 ui_end_menu();
470 return chosen_item;
471}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800472
Doug Zongkerbe598882010-04-08 14:36:55 -0700473static int compare_string(const void* a, const void* b) {
474 return strcmp(*(const char**)a, *(const char**)b);
475}
476
477static int
478sdcard_directory(const char* root_path) {
Doug Zongker050d0f72010-07-09 09:13:51 -0700479 // Mount the sdcard when the package selection menu is enabled so
480 // you can "adb push" packages to the sdcard and immediately
481 // install them.
Doug Zongkerf635d2e2010-08-02 11:34:46 -0700482 if (ensure_root_path_mounted(EXT_ROOT) < 0) {
483 ui_print("Failed to mount external storage.\n");
484 return INSTALL_ERROR;
485 }
Doug Zongker050d0f72010-07-09 09:13:51 -0700486
Doug Zongkerbe598882010-04-08 14:36:55 -0700487 const char* MENU_HEADERS[] = { "Choose a package to install:",
488 root_path,
489 "",
490 NULL };
491 DIR* d;
492 struct dirent* de;
493 char path[PATH_MAX];
494 d = opendir(translate_root_path(root_path, path, sizeof(path)));
495 if (d == NULL) {
496 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700497 ensure_root_path_unmounted(EXT_ROOT);
Doug Zongkerbe598882010-04-08 14:36:55 -0700498 return 0;
499 }
500
501 char** headers = prepend_title(MENU_HEADERS);
502
503 int d_size = 0;
504 int d_alloc = 10;
505 char** dirs = malloc(d_alloc * sizeof(char*));
506 int z_size = 1;
507 int z_alloc = 10;
508 char** zips = malloc(z_alloc * sizeof(char*));
509 zips[0] = strdup("../");
510
511 while ((de = readdir(d)) != NULL) {
512 int name_len = strlen(de->d_name);
513
514 if (de->d_type == DT_DIR) {
515 // skip "." and ".." entries
516 if (name_len == 1 && de->d_name[0] == '.') continue;
517 if (name_len == 2 && de->d_name[0] == '.' &&
518 de->d_name[1] == '.') continue;
519
520 if (d_size >= d_alloc) {
521 d_alloc *= 2;
522 dirs = realloc(dirs, d_alloc * sizeof(char*));
523 }
524 dirs[d_size] = malloc(name_len + 2);
525 strcpy(dirs[d_size], de->d_name);
526 dirs[d_size][name_len] = '/';
527 dirs[d_size][name_len+1] = '\0';
528 ++d_size;
529 } else if (de->d_type == DT_REG &&
530 name_len >= 4 &&
531 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
532 if (z_size >= z_alloc) {
533 z_alloc *= 2;
534 zips = realloc(zips, z_alloc * sizeof(char*));
535 }
536 zips[z_size++] = strdup(de->d_name);
537 }
538 }
539 closedir(d);
540
541 qsort(dirs, d_size, sizeof(char*), compare_string);
542 qsort(zips, z_size, sizeof(char*), compare_string);
543
544 // append dirs to the zips list
545 if (d_size + z_size + 1 > z_alloc) {
546 z_alloc = d_size + z_size + 1;
547 zips = realloc(zips, z_alloc * sizeof(char*));
548 }
549 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
550 free(dirs);
551 z_size += d_size;
552 zips[z_size] = NULL;
553
Doug Zongker050d0f72010-07-09 09:13:51 -0700554 int result = INSTALL_CORRUPT;
Doug Zongkerbe598882010-04-08 14:36:55 -0700555 int chosen_item = 0;
556 do {
557 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
558
559 char* item = zips[chosen_item];
560 int item_len = strlen(item);
561 if (chosen_item == 0) { // item 0 is always "../"
562 // go up but continue browsing (if the caller is sdcard_directory)
563 result = -1;
564 break;
565 } else if (item[item_len-1] == '/') {
566 // recurse down into a subdirectory
567 char new_path[PATH_MAX];
568 strlcpy(new_path, root_path, PATH_MAX);
569 strlcat(new_path, item, PATH_MAX);
570 result = sdcard_directory(new_path);
571 if (result >= 0) break;
572 } else {
573 // selected a zip file: attempt to install it, and return
574 // the status to the caller.
575 char new_path[PATH_MAX];
576 strlcpy(new_path, root_path, PATH_MAX);
577 strlcat(new_path, item, PATH_MAX);
578
579 ui_print("\n-- Install %s ...\n", new_path);
Doug Zongker050d0f72010-07-09 09:13:51 -0700580 char* copy = copy_sideloaded_package(new_path);
581 if (copy != NULL) {
582 set_sdcard_update_bootloader_message();
583 result = install_package(copy);
584 free(copy);
585 }
Doug Zongkerbe598882010-04-08 14:36:55 -0700586 break;
587 }
588 } while (true);
589
590 int i;
591 for (i = 0; i < z_size; ++i) free(zips[i]);
592 free(zips);
593 free(headers);
594
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700595 ensure_root_path_unmounted(EXT_ROOT);
Doug Zongkerbe598882010-04-08 14:36:55 -0700596 return result;
597}
598
Doug Zongkerf93d8162009-09-22 15:16:02 -0700599static void
600wipe_data(int confirm) {
601 if (confirm) {
602 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700603
Doug Zongkerf93d8162009-09-22 15:16:02 -0700604 if (title_headers == NULL) {
605 char* headers[] = { "Confirm wipe of all user data?",
606 " THIS CAN NOT BE UNDONE.",
607 "",
608 NULL };
Doug Zongkerbe598882010-04-08 14:36:55 -0700609 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700610 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800611
Doug Zongkerf93d8162009-09-22 15:16:02 -0700612 char* items[] = { " No",
613 " No",
614 " No",
615 " No",
616 " No",
617 " No",
618 " No",
619 " Yes -- delete all user data", // [7]
620 " No",
621 " No",
622 " No",
623 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800624
Doug Zongkerbe598882010-04-08 14:36:55 -0700625 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700626 if (chosen_item != 7) {
627 return;
628 }
629 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700630
Doug Zongkerf93d8162009-09-22 15:16:02 -0700631 ui_print("\n-- Wiping data...\n");
632 device_wipe_data();
633 erase_root("DATA:");
634 erase_root("CACHE:");
635 ui_print("Data wipe complete.\n");
636}
637
638static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800639prompt_and_wait() {
Doug Zongkerbe598882010-04-08 14:36:55 -0700640 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700641
642 for (;;) {
643 finish_recovery(NULL);
644 ui_reset_progress();
645
Doug Zongkerbe598882010-04-08 14:36:55 -0700646 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700647
648 // device-specific code may take some action here. It may
649 // return one of the core actions handled in the switch
650 // statement below.
651 chosen_item = device_perform_action(chosen_item);
652
653 switch (chosen_item) {
654 case ITEM_REBOOT:
655 return;
656
657 case ITEM_WIPE_DATA:
658 wipe_data(ui_text_visible());
659 if (!ui_text_visible()) return;
660 break;
661
662 case ITEM_WIPE_CACHE:
663 ui_print("\n-- Wiping cache...\n");
664 erase_root("CACHE:");
665 ui_print("Cache wipe complete.\n");
666 if (!ui_text_visible()) return;
667 break;
668
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700669 case ITEM_APPLY_EXT:
Doug Zongkerbe598882010-04-08 14:36:55 -0700670 ;
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700671 int status = sdcard_directory(EXT_ROOT);
Doug Zongkerbe598882010-04-08 14:36:55 -0700672 if (status >= 0) {
673 if (status != INSTALL_SUCCESS) {
674 ui_set_background(BACKGROUND_ICON_ERROR);
675 ui_print("Installation aborted.\n");
676 } else if (!ui_text_visible()) {
677 return; // reboot if logs aren't visible
678 } else {
679 ui_print("\nInstall from sdcard complete.\n");
680 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700681 }
682 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800683 }
684 }
685}
686
687static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800688print_property(const char *key, const char *name, void *cookie) {
Doug Zongker3d177d02010-07-01 09:18:44 -0700689 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800690}
691
692int
Oscar Montemayor05231562009-11-30 08:40:57 -0800693main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800694 time_t start = time(NULL);
695
696 // If these fail, there's not really anywhere to complain...
697 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
698 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker3d177d02010-07-01 09:18:44 -0700699 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800700
701 ui_init();
702 get_args(&argc, &argv);
703
704 int previous_runs = 0;
705 const char *send_intent = NULL;
706 const char *update_package = NULL;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800707 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800708 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800709 int toggle_secure_fs = 0;
710 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800711
712 int arg;
713 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
714 switch (arg) {
715 case 'p': previous_runs = atoi(optarg); break;
716 case 's': send_intent = optarg; break;
717 case 'u': update_package = optarg; break;
718 case 'w': wipe_data = wipe_cache = 1; break;
719 case 'c': wipe_cache = 1; break;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800720 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800721 case '?':
722 LOGE("Invalid command argument\n");
723 continue;
724 }
725 }
726
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800727 device_recovery_start();
728
Doug Zongker3d177d02010-07-01 09:18:44 -0700729 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800730 for (arg = 0; arg < argc; arg++) {
Doug Zongker3d177d02010-07-01 09:18:44 -0700731 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800732 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700733 printf("\n\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800734
735 property_list(print_property, NULL);
Doug Zongker3d177d02010-07-01 09:18:44 -0700736 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800737
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800738 int status = INSTALL_SUCCESS;
739
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800740 if (toggle_secure_fs) {
741 if (strcmp(encrypted_fs_mode,"on") == 0) {
742 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800743 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800744 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
745 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800746 ui_print("Disabling Encrypted FS.\n");
747 } else {
748 ui_print("Error: invalid Encrypted FS setting.\n");
749 status = INSTALL_ERROR;
750 }
751
752 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
753 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800754 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
755 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800756 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800757 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800758 }
759
760 if (status != INSTALL_ERROR) {
761 if (erase_root("DATA:")) {
762 ui_print("Data wipe failed.\n");
763 status = INSTALL_ERROR;
764 } else if (erase_root("CACHE:")) {
765 ui_print("Cache wipe failed.\n");
766 status = INSTALL_ERROR;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800767 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
768 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800769 ui_print("Encrypted FS change aborted.\n");
770 status = INSTALL_ERROR;
771 } else {
772 ui_print("Successfully updated Encrypted FS.\n");
773 status = INSTALL_SUCCESS;
774 }
775 }
776 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800777 status = install_package(update_package);
778 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700779 } else if (wipe_data) {
780 if (device_wipe_data()) status = INSTALL_ERROR;
781 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800782 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
783 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700784 } else if (wipe_cache) {
785 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
786 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800787 } else {
788 status = INSTALL_ERROR; // No command specified
789 }
790
791 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongkerbe598882010-04-08 14:36:55 -0700792 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongkerbe598882010-04-08 14:36:55 -0700793 prompt_and_wait();
794 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800795
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800796 // Otherwise, get ready to boot the main system...
797 finish_recovery(send_intent);
798 ui_print("Rebooting...\n");
799 sync();
800 reboot(RB_AUTOBOOT);
801 return EXIT_SUCCESS;
802}