blob: 779e77d764a65ec6f3b469a242b0bb535210c98f [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 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 Zongkerdc9e87c2010-07-29 17:08:50 -070056static const char *EXT_ROOT = "EXT:";
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 Montemayor31f6ee82010-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 Montemayor31f6ee82010-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 Zongkerbe598882010-04-08 14:36:55 -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 Zongkerbe598882010-04-08 14:36:55 -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 Zongkerbe598882010-04-08 14:36:55 -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 Zongkerbe598882010-04-08 14:36:55 -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) {
Doug Zongker050d0f72010-07-09 09:13:51 -0700480 // Mount the sdcard when the package selection menu is enabled so
481 // you can "adb push" packages to the sdcard and immediately
482 // install them.
Doug Zongkerf635d2e2010-08-02 11:34:46 -0700483 if (ensure_root_path_mounted(EXT_ROOT) < 0) {
484 ui_print("Failed to mount external storage.\n");
485 return INSTALL_ERROR;
486 }
Doug Zongker050d0f72010-07-09 09:13:51 -0700487
Doug Zongkerbe598882010-04-08 14:36:55 -0700488 const char* MENU_HEADERS[] = { "Choose a package to install:",
489 root_path,
490 "",
491 NULL };
492 DIR* d;
493 struct dirent* de;
494 char path[PATH_MAX];
495 d = opendir(translate_root_path(root_path, path, sizeof(path)));
496 if (d == NULL) {
497 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700498 ensure_root_path_unmounted(EXT_ROOT);
Doug Zongkerbe598882010-04-08 14:36:55 -0700499 return 0;
500 }
501
502 char** headers = prepend_title(MENU_HEADERS);
503
504 int d_size = 0;
505 int d_alloc = 10;
506 char** dirs = malloc(d_alloc * sizeof(char*));
507 int z_size = 1;
508 int z_alloc = 10;
509 char** zips = malloc(z_alloc * sizeof(char*));
510 zips[0] = strdup("../");
511
512 while ((de = readdir(d)) != NULL) {
513 int name_len = strlen(de->d_name);
514
515 if (de->d_type == DT_DIR) {
516 // skip "." and ".." entries
517 if (name_len == 1 && de->d_name[0] == '.') continue;
518 if (name_len == 2 && de->d_name[0] == '.' &&
519 de->d_name[1] == '.') continue;
520
521 if (d_size >= d_alloc) {
522 d_alloc *= 2;
523 dirs = realloc(dirs, d_alloc * sizeof(char*));
524 }
525 dirs[d_size] = malloc(name_len + 2);
526 strcpy(dirs[d_size], de->d_name);
527 dirs[d_size][name_len] = '/';
528 dirs[d_size][name_len+1] = '\0';
529 ++d_size;
530 } else if (de->d_type == DT_REG &&
531 name_len >= 4 &&
532 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
533 if (z_size >= z_alloc) {
534 z_alloc *= 2;
535 zips = realloc(zips, z_alloc * sizeof(char*));
536 }
537 zips[z_size++] = strdup(de->d_name);
538 }
539 }
540 closedir(d);
541
542 qsort(dirs, d_size, sizeof(char*), compare_string);
543 qsort(zips, z_size, sizeof(char*), compare_string);
544
545 // append dirs to the zips list
546 if (d_size + z_size + 1 > z_alloc) {
547 z_alloc = d_size + z_size + 1;
548 zips = realloc(zips, z_alloc * sizeof(char*));
549 }
550 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
551 free(dirs);
552 z_size += d_size;
553 zips[z_size] = NULL;
554
Doug Zongker050d0f72010-07-09 09:13:51 -0700555 int result = INSTALL_CORRUPT;
Doug Zongkerbe598882010-04-08 14:36:55 -0700556 int chosen_item = 0;
557 do {
558 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
559
560 char* item = zips[chosen_item];
561 int item_len = strlen(item);
562 if (chosen_item == 0) { // item 0 is always "../"
563 // go up but continue browsing (if the caller is sdcard_directory)
564 result = -1;
565 break;
566 } else if (item[item_len-1] == '/') {
567 // recurse down into a subdirectory
568 char new_path[PATH_MAX];
569 strlcpy(new_path, root_path, PATH_MAX);
570 strlcat(new_path, item, PATH_MAX);
571 result = sdcard_directory(new_path);
572 if (result >= 0) break;
573 } else {
574 // selected a zip file: attempt to install it, and return
575 // the status to the caller.
576 char new_path[PATH_MAX];
577 strlcpy(new_path, root_path, PATH_MAX);
578 strlcat(new_path, item, PATH_MAX);
579
580 ui_print("\n-- Install %s ...\n", new_path);
Doug Zongker050d0f72010-07-09 09:13:51 -0700581 char* copy = copy_sideloaded_package(new_path);
582 if (copy != NULL) {
583 set_sdcard_update_bootloader_message();
584 result = install_package(copy);
585 free(copy);
586 }
Doug Zongkerbe598882010-04-08 14:36:55 -0700587 break;
588 }
589 } while (true);
590
591 int i;
592 for (i = 0; i < z_size; ++i) free(zips[i]);
593 free(zips);
594 free(headers);
595
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700596 ensure_root_path_unmounted(EXT_ROOT);
Doug Zongkerbe598882010-04-08 14:36:55 -0700597 return result;
598}
599
Doug Zongkerf93d8162009-09-22 15:16:02 -0700600static void
601wipe_data(int confirm) {
602 if (confirm) {
603 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700604
Doug Zongkerf93d8162009-09-22 15:16:02 -0700605 if (title_headers == NULL) {
606 char* headers[] = { "Confirm wipe of all user data?",
607 " THIS CAN NOT BE UNDONE.",
608 "",
609 NULL };
Doug Zongkerbe598882010-04-08 14:36:55 -0700610 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700611 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800612
Doug Zongkerf93d8162009-09-22 15:16:02 -0700613 char* items[] = { " No",
614 " No",
615 " No",
616 " No",
617 " No",
618 " No",
619 " No",
620 " Yes -- delete all user data", // [7]
621 " No",
622 " No",
623 " No",
624 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800625
Doug Zongkerbe598882010-04-08 14:36:55 -0700626 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700627 if (chosen_item != 7) {
628 return;
629 }
630 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700631
Doug Zongkerf93d8162009-09-22 15:16:02 -0700632 ui_print("\n-- Wiping data...\n");
633 device_wipe_data();
634 erase_root("DATA:");
635 erase_root("CACHE:");
636 ui_print("Data wipe complete.\n");
637}
638
639static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800640prompt_and_wait() {
Doug Zongkerbe598882010-04-08 14:36:55 -0700641 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700642
643 for (;;) {
644 finish_recovery(NULL);
645 ui_reset_progress();
646
Doug Zongkerbe598882010-04-08 14:36:55 -0700647 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700648
649 // device-specific code may take some action here. It may
650 // return one of the core actions handled in the switch
651 // statement below.
652 chosen_item = device_perform_action(chosen_item);
653
654 switch (chosen_item) {
655 case ITEM_REBOOT:
656 return;
657
658 case ITEM_WIPE_DATA:
659 wipe_data(ui_text_visible());
660 if (!ui_text_visible()) return;
661 break;
662
663 case ITEM_WIPE_CACHE:
664 ui_print("\n-- Wiping cache...\n");
665 erase_root("CACHE:");
666 ui_print("Cache wipe complete.\n");
667 if (!ui_text_visible()) return;
668 break;
669
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700670 case ITEM_APPLY_EXT:
Doug Zongkerbe598882010-04-08 14:36:55 -0700671 ;
Doug Zongkerdc9e87c2010-07-29 17:08:50 -0700672 int status = sdcard_directory(EXT_ROOT);
Doug Zongkerbe598882010-04-08 14:36:55 -0700673 if (status >= 0) {
674 if (status != INSTALL_SUCCESS) {
675 ui_set_background(BACKGROUND_ICON_ERROR);
676 ui_print("Installation aborted.\n");
677 } else if (!ui_text_visible()) {
678 return; // reboot if logs aren't visible
679 } else {
680 ui_print("\nInstall from sdcard complete.\n");
681 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700682 }
683 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800684 }
685 }
686}
687
688static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800689print_property(const char *key, const char *name, void *cookie) {
Doug Zongker3d177d02010-07-01 09:18:44 -0700690 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800691}
692
693int
Oscar Montemayor05231562009-11-30 08:40:57 -0800694main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800695 time_t start = time(NULL);
696
697 // If these fail, there's not really anywhere to complain...
698 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
699 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker3d177d02010-07-01 09:18:44 -0700700 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800701
702 ui_init();
703 get_args(&argc, &argv);
704
705 int previous_runs = 0;
706 const char *send_intent = NULL;
707 const char *update_package = NULL;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800708 const char *encrypted_fs_mode = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800709 int wipe_data = 0, wipe_cache = 0;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800710 int toggle_secure_fs = 0;
711 encrypted_fs_info encrypted_fs_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800712
713 int arg;
714 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
715 switch (arg) {
716 case 'p': previous_runs = atoi(optarg); break;
717 case 's': send_intent = optarg; break;
718 case 'u': update_package = optarg; break;
719 case 'w': wipe_data = wipe_cache = 1; break;
720 case 'c': wipe_cache = 1; break;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800721 case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700722 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800723 case '?':
724 LOGE("Invalid command argument\n");
725 continue;
726 }
727 }
728
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800729 device_recovery_start();
730
Doug Zongker3d177d02010-07-01 09:18:44 -0700731 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800732 for (arg = 0; arg < argc; arg++) {
Doug Zongker3d177d02010-07-01 09:18:44 -0700733 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800734 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700735 printf("\n\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800736
737 property_list(print_property, NULL);
Doug Zongker3d177d02010-07-01 09:18:44 -0700738 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800739
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800740 int status = INSTALL_SUCCESS;
741
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800742 if (toggle_secure_fs) {
743 if (strcmp(encrypted_fs_mode,"on") == 0) {
744 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800745 ui_print("Enabling Encrypted FS.\n");
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800746 } else if (strcmp(encrypted_fs_mode,"off") == 0) {
747 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800748 ui_print("Disabling Encrypted FS.\n");
749 } else {
750 ui_print("Error: invalid Encrypted FS setting.\n");
751 status = INSTALL_ERROR;
752 }
753
754 // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
755 // This preventsthe device recycling endlessly in recovery mode.
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800756 if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
757 (read_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800758 ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800759 encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
Oscar Montemayor05231562009-11-30 08:40:57 -0800760 }
761
762 if (status != INSTALL_ERROR) {
763 if (erase_root("DATA:")) {
764 ui_print("Data wipe failed.\n");
765 status = INSTALL_ERROR;
766 } else if (erase_root("CACHE:")) {
767 ui_print("Cache wipe failed.\n");
768 status = INSTALL_ERROR;
Oscar Montemayor31f6ee82010-02-25 16:47:02 -0800769 } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
770 (restore_encrypted_fs_info(&encrypted_fs_data))) {
Oscar Montemayor05231562009-11-30 08:40:57 -0800771 ui_print("Encrypted FS change aborted.\n");
772 status = INSTALL_ERROR;
773 } else {
774 ui_print("Successfully updated Encrypted FS.\n");
775 status = INSTALL_SUCCESS;
776 }
777 }
778 } else if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800779 status = install_package(update_package);
780 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700781 } else if (wipe_data) {
782 if (device_wipe_data()) status = INSTALL_ERROR;
783 if (erase_root("DATA:")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800784 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
785 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700786 } else if (wipe_cache) {
787 if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
788 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800789 } else {
790 status = INSTALL_ERROR; // No command specified
791 }
792
793 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongkerbe598882010-04-08 14:36:55 -0700794 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongkerbe598882010-04-08 14:36:55 -0700795 prompt_and_wait();
796 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800797
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800798 // Otherwise, get ready to boot the main system...
799 finish_recovery(send_intent);
800 ui_print("Rebooting...\n");
801 sync();
802 reboot(RB_AUTOBOOT);
803 return EXIT_SUCCESS;
804}