blob: 14fc905c23bf671f14ffbb7adf9a92cf726a17bd [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"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
42static const struct option OPTIONS[] = {
43 { "send_intent", required_argument, NULL, 's' },
44 { "update_package", required_argument, NULL, 'u' },
45 { "wipe_data", no_argument, NULL, 'w' },
46 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070047 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070048 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049};
50
Doug Zongkerd4208f92010-09-20 12:16:13 -070051static const char *COMMAND_FILE = "/cache/recovery/command";
52static const char *INTENT_FILE = "/cache/recovery/intent";
53static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070054static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070055static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd4208f92010-09-20 12:16:13 -070057static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058
Doug Zongker6809c512011-03-01 14:04:34 -080059extern UIParameters ui_parameters; // from ui.c
60
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061/*
62 * The recovery tool communicates with the main system through /cache files.
63 * /cache/recovery/command - INPUT - command line for tool, one arg per line
64 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
65 * /cache/recovery/intent - OUTPUT - intent that was passed in
66 *
67 * The arguments which may be supplied in the recovery.command file:
68 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070069 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070 * --wipe_data - erase user data (and cache), then reboot
71 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080072 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 *
74 * After completing, we remove /cache/recovery/command and reboot.
75 * Arguments may also be supplied in the bootloader control block (BCB).
76 * These important scenarios must be safely restartable at any point:
77 *
78 * FACTORY RESET
79 * 1. user selects "factory reset"
80 * 2. main system writes "--wipe_data" to /cache/recovery/command
81 * 3. main system reboots into recovery
82 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
83 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070084 * 5. erase_volume() reformats /data
85 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086 * 7. finish_recovery() erases BCB
87 * -- after this, rebooting will restart the main system --
88 * 8. main() calls reboot() to boot main system
89 *
90 * OTA INSTALL
91 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -070092 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093 * 3. main system reboots into recovery
94 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
95 * -- after this, rebooting will attempt to reinstall the update --
96 * 5. install_package() attempts to install the update
97 * NOTE: the package install must itself be restartable from any point
98 * 6. finish_recovery() erases BCB
99 * -- after this, rebooting will (try to) restart the main system --
100 * 7. ** if install failed **
101 * 7a. prompt_and_wait() shows an error icon and waits for the user
102 * 7b; the user reboots (pulling the battery, etc) into the main system
103 * 8. main() calls maybe_install_firmware_update()
104 * ** if the update contained radio/hboot firmware **:
105 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
106 * -- after this, rebooting will reformat cache & restart main system --
107 * 8b. m_i_f_u() writes firmware image into raw cache partition
108 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
109 * -- after this, rebooting will attempt to reinstall firmware --
110 * 8d. bootloader tries to flash firmware
111 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
112 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114 * 8g. finish_recovery() erases BCB
115 * -- after this, rebooting will (try to) restart the main system --
116 * 9. main() calls reboot() to boot main system
117 */
118
119static const int MAX_ARG_LENGTH = 4096;
120static const int MAX_ARGS = 100;
121
Doug Zongkerd4208f92010-09-20 12:16:13 -0700122// open a given path, mounting partitions as necessary
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123static FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700124fopen_path(const char *path, const char *mode) {
125 if (ensure_path_mounted(path) != 0) {
126 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800127 return NULL;
128 }
129
130 // When writing, try to create the containing directory, if necessary.
131 // Use generous permissions, the system (init.rc) will reset them.
132 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
133
134 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 return fp;
136}
137
138// close a file, log an error if the error indicator is set
139static void
140check_and_fclose(FILE *fp, const char *name) {
141 fflush(fp);
142 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
143 fclose(fp);
144}
145
146// command line args come from, in decreasing precedence:
147// - the actual command line
148// - the bootloader control block (one per line, after "recovery")
149// - the contents of COMMAND_FILE (one per line)
150static void
151get_args(int *argc, char ***argv) {
152 struct bootloader_message boot;
153 memset(&boot, 0, sizeof(boot));
154 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
155
156 if (boot.command[0] != 0 && boot.command[0] != 255) {
157 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
158 }
159
160 if (boot.status[0] != 0 && boot.status[0] != 255) {
161 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
162 }
163
164 // --- if arguments weren't supplied, look in the bootloader control block
165 if (*argc <= 1) {
166 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
167 const char *arg = strtok(boot.recovery, "\n");
168 if (arg != NULL && !strcmp(arg, "recovery")) {
169 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
170 (*argv)[0] = strdup(arg);
171 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
172 if ((arg = strtok(NULL, "\n")) == NULL) break;
173 (*argv)[*argc] = strdup(arg);
174 }
175 LOGI("Got arguments from boot message\n");
176 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
177 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
178 }
179 }
180
181 // --- if that doesn't work, try the command file
182 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700183 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800184 if (fp != NULL) {
185 char *argv0 = (*argv)[0];
186 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
187 (*argv)[0] = argv0; // use the same program name
188
189 char buf[MAX_ARG_LENGTH];
190 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
191 if (!fgets(buf, sizeof(buf), fp)) break;
192 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
193 }
194
195 check_and_fclose(fp, COMMAND_FILE);
196 LOGI("Got arguments from %s\n", COMMAND_FILE);
197 }
198 }
199
200 // --> write the arguments we have back into the bootloader control block
201 // always boot into recovery after this (until finish_recovery() is called)
202 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
203 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
204 int i;
205 for (i = 1; i < *argc; ++i) {
206 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
207 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
208 }
209 set_bootloader_message(&boot);
210}
211
Doug Zongker34c98df2009-08-18 12:05:45 -0700212static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800213set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700214 struct bootloader_message boot;
215 memset(&boot, 0, sizeof(boot));
216 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
217 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
218 set_bootloader_message(&boot);
219}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220
Doug Zongker2c3539e2010-09-29 13:21:30 -0700221// How much of the temp log we have copied to the copy in cache.
222static long tmplog_offset = 0;
223
224static void
225copy_log_file(const char* destination, int append) {
226 FILE *log = fopen_path(destination, append ? "a" : "w");
227 if (log == NULL) {
228 LOGE("Can't open %s\n", destination);
229 } else {
230 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
231 if (tmplog == NULL) {
232 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
233 } else {
234 if (append) {
235 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
236 }
237 char buf[4096];
238 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
239 if (append) {
240 tmplog_offset = ftell(tmplog);
241 }
242 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
243 }
244 check_and_fclose(log, destination);
245 }
246}
247
248
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800249// clear the recovery command and prepare to boot a (hopefully working) system,
250// copy our log file to cache as well (for the system to read), and
251// record any intent we were asked to communicate back to the system.
252// this function is idempotent: call it as many times as you like.
253static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800254finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 // By this point, we're ready to return to the main system...
256 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700257 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000258 if (fp == NULL) {
259 LOGE("Can't open %s\n", INTENT_FILE);
260 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 fputs(send_intent, fp);
262 check_and_fclose(fp, INTENT_FILE);
263 }
264 }
265
266 // Copy logs to cache so the system can find out what happened.
Doug Zongker2c3539e2010-09-29 13:21:30 -0700267 copy_log_file(LOG_FILE, true);
268 copy_log_file(LAST_LOG_FILE, false);
269 chmod(LAST_LOG_FILE, 0640);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270
Oscar Montemayor05231562009-11-30 08:40:57 -0800271 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800272 struct bootloader_message boot;
273 memset(&boot, 0, sizeof(boot));
274 set_bootloader_message(&boot);
275
276 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700277 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
278 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279 LOGW("Can't unlink %s\n", COMMAND_FILE);
280 }
281
282 sync(); // For good measure.
283}
284
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700286erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287 ui_set_background(BACKGROUND_ICON_INSTALLING);
288 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700289 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700290
291 if (strcmp(volume, "/cache") == 0) {
292 // Any part of the log we'd copied to cache is now gone.
293 // Reset the pointer so we copy from the beginning of the temp
294 // log.
295 tmplog_offset = 0;
296 }
297
Doug Zongkerd4208f92010-09-20 12:16:13 -0700298 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299}
300
Doug Zongker23ceeea2010-07-08 17:27:55 -0700301static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700302copy_sideloaded_package(const char* original_path) {
303 if (ensure_path_mounted(original_path) != 0) {
304 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700305 return NULL;
306 }
307
Doug Zongkerd4208f92010-09-20 12:16:13 -0700308 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700309 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
310 return NULL;
311 }
312
Doug Zongkerd4208f92010-09-20 12:16:13 -0700313 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700314 if (errno != EEXIST) {
315 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
316 return NULL;
317 }
318 }
319
Doug Zongkerd4208f92010-09-20 12:16:13 -0700320 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
321 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700322 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700323 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
324 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700325 return NULL;
326 }
327 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700328 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700329 return NULL;
330 }
331 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700332 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700333 return NULL;
334 }
335 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700336 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700337 return NULL;
338 }
339
Doug Zongkerd4208f92010-09-20 12:16:13 -0700340 char copy_path[PATH_MAX];
341 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700342 strcat(copy_path, "/package.zip");
343
344 char* buffer = malloc(BUFSIZ);
345 if (buffer == NULL) {
346 LOGE("Failed to allocate buffer\n");
347 return NULL;
348 }
349
350 size_t read;
351 FILE* fin = fopen(original_path, "rb");
352 if (fin == NULL) {
353 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
354 return NULL;
355 }
356 FILE* fout = fopen(copy_path, "wb");
357 if (fout == NULL) {
358 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
359 return NULL;
360 }
361
362 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
363 if (fwrite(buffer, 1, read, fout) != read) {
364 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
365 return NULL;
366 }
367 }
368
369 free(buffer);
370
371 if (fclose(fout) != 0) {
372 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
373 return NULL;
374 }
375
376 if (fclose(fin) != 0) {
377 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
378 return NULL;
379 }
380
381 // "adb push" is happy to overwrite read-only files when it's
382 // running as root, but we'll try anyway.
383 if (chmod(copy_path, 0400) != 0) {
384 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
385 return NULL;
386 }
387
Doug Zongkerd4208f92010-09-20 12:16:13 -0700388 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700389}
390
Doug Zongkerf93d8162009-09-22 15:16:02 -0700391static char**
Doug Zongker8674a722010-09-15 11:08:23 -0700392prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700393 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700394 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700395 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800396 NULL };
397
Doug Zongkerd6837852009-06-17 22:07:13 -0700398 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700399 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700400 int count = 0;
401 char** p;
402 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700403 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700404
Doug Zongkerf93d8162009-09-22 15:16:02 -0700405 char** new_headers = malloc((count+1) * sizeof(char*));
406 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700407 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700408 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700409 *h = NULL;
410
Doug Zongkerf93d8162009-09-22 15:16:02 -0700411 return new_headers;
412}
413
414static int
Doug Zongker8674a722010-09-15 11:08:23 -0700415get_menu_selection(char** headers, char** items, int menu_only,
416 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700417 // throw away keys pressed previously, so user doesn't
418 // accidentally trigger menu items.
419 ui_clear_key_queue();
420
Doug Zongker8674a722010-09-15 11:08:23 -0700421 ui_start_menu(headers, items, initial_selection);
422 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800423 int chosen_item = -1;
424
Doug Zongkerf93d8162009-09-22 15:16:02 -0700425 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800426 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800427 int visible = ui_text_visible();
428
Doug Zongker5cae4452011-01-25 13:15:30 -0800429 if (key == -1) { // ui_wait_key() timed out
430 if (ui_text_ever_visible()) {
431 continue;
432 } else {
433 LOGI("timed out waiting for key input; rebooting.\n");
434 ui_end_menu();
435 return ITEM_REBOOT;
436 }
437 }
438
Doug Zongkerddd6a282009-06-09 12:22:33 -0700439 int action = device_handle_key(key, visible);
440
441 if (action < 0) {
442 switch (action) {
443 case HIGHLIGHT_UP:
444 --selected;
445 selected = ui_menu_select(selected);
446 break;
447 case HIGHLIGHT_DOWN:
448 ++selected;
449 selected = ui_menu_select(selected);
450 break;
451 case SELECT_ITEM:
452 chosen_item = selected;
453 break;
454 case NO_ACTION:
455 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800456 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700457 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700458 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800459 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700460 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800461
Doug Zongkerf93d8162009-09-22 15:16:02 -0700462 ui_end_menu();
463 return chosen_item;
464}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800465
Doug Zongker8674a722010-09-15 11:08:23 -0700466static int compare_string(const void* a, const void* b) {
467 return strcmp(*(const char**)a, *(const char**)b);
468}
469
470static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700471sdcard_directory(const char* path) {
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700472 ensure_path_mounted(SDCARD_ROOT);
473
Doug Zongker8674a722010-09-15 11:08:23 -0700474 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700475 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700476 "",
477 NULL };
478 DIR* d;
479 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700480 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700481 if (d == NULL) {
482 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700483 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700484 return 0;
485 }
486
487 char** headers = prepend_title(MENU_HEADERS);
488
489 int d_size = 0;
490 int d_alloc = 10;
491 char** dirs = malloc(d_alloc * sizeof(char*));
492 int z_size = 1;
493 int z_alloc = 10;
494 char** zips = malloc(z_alloc * sizeof(char*));
495 zips[0] = strdup("../");
496
497 while ((de = readdir(d)) != NULL) {
498 int name_len = strlen(de->d_name);
499
500 if (de->d_type == DT_DIR) {
501 // skip "." and ".." entries
502 if (name_len == 1 && de->d_name[0] == '.') continue;
503 if (name_len == 2 && de->d_name[0] == '.' &&
504 de->d_name[1] == '.') continue;
505
506 if (d_size >= d_alloc) {
507 d_alloc *= 2;
508 dirs = realloc(dirs, d_alloc * sizeof(char*));
509 }
510 dirs[d_size] = malloc(name_len + 2);
511 strcpy(dirs[d_size], de->d_name);
512 dirs[d_size][name_len] = '/';
513 dirs[d_size][name_len+1] = '\0';
514 ++d_size;
515 } else if (de->d_type == DT_REG &&
516 name_len >= 4 &&
517 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
518 if (z_size >= z_alloc) {
519 z_alloc *= 2;
520 zips = realloc(zips, z_alloc * sizeof(char*));
521 }
522 zips[z_size++] = strdup(de->d_name);
523 }
524 }
525 closedir(d);
526
527 qsort(dirs, d_size, sizeof(char*), compare_string);
528 qsort(zips, z_size, sizeof(char*), compare_string);
529
530 // append dirs to the zips list
531 if (d_size + z_size + 1 > z_alloc) {
532 z_alloc = d_size + z_size + 1;
533 zips = realloc(zips, z_alloc * sizeof(char*));
534 }
535 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
536 free(dirs);
537 z_size += d_size;
538 zips[z_size] = NULL;
539
540 int result;
541 int chosen_item = 0;
542 do {
543 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
544
545 char* item = zips[chosen_item];
546 int item_len = strlen(item);
547 if (chosen_item == 0) { // item 0 is always "../"
548 // go up but continue browsing (if the caller is sdcard_directory)
549 result = -1;
550 break;
551 } else if (item[item_len-1] == '/') {
552 // recurse down into a subdirectory
553 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700554 strlcpy(new_path, path, PATH_MAX);
555 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700556 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700557 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700558 result = sdcard_directory(new_path);
559 if (result >= 0) break;
560 } else {
561 // selected a zip file: attempt to install it, and return
562 // the status to the caller.
563 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700564 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700565 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700566 strlcat(new_path, item, PATH_MAX);
567
Doug Zongkerd4208f92010-09-20 12:16:13 -0700568 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700569 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700570 char* copy = copy_sideloaded_package(new_path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700571 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700572 if (copy) {
573 result = install_package(copy);
574 free(copy);
575 } else {
576 result = INSTALL_ERROR;
577 }
Doug Zongker8674a722010-09-15 11:08:23 -0700578 break;
579 }
580 } while (true);
581
582 int i;
583 for (i = 0; i < z_size; ++i) free(zips[i]);
584 free(zips);
585 free(headers);
586
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700587 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700588 return result;
589}
590
Doug Zongkerf93d8162009-09-22 15:16:02 -0700591static void
592wipe_data(int confirm) {
593 if (confirm) {
594 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700595
Doug Zongkerf93d8162009-09-22 15:16:02 -0700596 if (title_headers == NULL) {
597 char* headers[] = { "Confirm wipe of all user data?",
598 " THIS CAN NOT BE UNDONE.",
599 "",
600 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700601 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700602 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800603
Doug Zongkerf93d8162009-09-22 15:16:02 -0700604 char* items[] = { " No",
605 " No",
606 " No",
607 " No",
608 " No",
609 " No",
610 " No",
611 " Yes -- delete all user data", // [7]
612 " No",
613 " No",
614 " No",
615 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800616
Doug Zongker8674a722010-09-15 11:08:23 -0700617 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700618 if (chosen_item != 7) {
619 return;
620 }
621 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700622
Doug Zongkerf93d8162009-09-22 15:16:02 -0700623 ui_print("\n-- Wiping data...\n");
624 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700625 erase_volume("/data");
626 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700627 ui_print("Data wipe complete.\n");
628}
629
630static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800631prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700632 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700633
634 for (;;) {
635 finish_recovery(NULL);
636 ui_reset_progress();
637
Doug Zongker8674a722010-09-15 11:08:23 -0700638 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700639
640 // device-specific code may take some action here. It may
641 // return one of the core actions handled in the switch
642 // statement below.
643 chosen_item = device_perform_action(chosen_item);
644
645 switch (chosen_item) {
646 case ITEM_REBOOT:
647 return;
648
649 case ITEM_WIPE_DATA:
650 wipe_data(ui_text_visible());
651 if (!ui_text_visible()) return;
652 break;
653
654 case ITEM_WIPE_CACHE:
655 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700656 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700657 ui_print("Cache wipe complete.\n");
658 if (!ui_text_visible()) return;
659 break;
660
661 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700662 ;
663 int status = sdcard_directory(SDCARD_ROOT);
664 if (status >= 0) {
665 if (status != INSTALL_SUCCESS) {
666 ui_set_background(BACKGROUND_ICON_ERROR);
667 ui_print("Installation aborted.\n");
668 } else if (!ui_text_visible()) {
669 return; // reboot if logs aren't visible
670 } else {
671 ui_print("\nInstall from sdcard complete.\n");
672 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700673 }
674 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800675 }
676 }
677}
678
679static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800680print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700681 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800682}
683
684int
Oscar Montemayor05231562009-11-30 08:40:57 -0800685main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800686 time_t start = time(NULL);
687
688 // If these fail, there's not really anywhere to complain...
689 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
690 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700691 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800692
Doug Zongker6809c512011-03-01 14:04:34 -0800693 device_ui_init(&ui_parameters);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800694 ui_init();
Doug Zongker51266d12010-11-01 10:19:12 -0700695 ui_set_background(BACKGROUND_ICON_INSTALLING);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700696 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800697 get_args(&argc, &argv);
698
699 int previous_runs = 0;
700 const char *send_intent = NULL;
701 const char *update_package = NULL;
702 int wipe_data = 0, wipe_cache = 0;
703
704 int arg;
705 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
706 switch (arg) {
707 case 'p': previous_runs = atoi(optarg); break;
708 case 's': send_intent = optarg; break;
709 case 'u': update_package = optarg; break;
710 case 'w': wipe_data = wipe_cache = 1; break;
711 case 'c': wipe_cache = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700712 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800713 case '?':
714 LOGE("Invalid command argument\n");
715 continue;
716 }
717 }
718
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800719 device_recovery_start();
720
Doug Zongker56c51052010-07-01 09:18:44 -0700721 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800722 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700723 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800724 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700725 printf("\n");
726
727 if (update_package) {
728 // For backwards compatibility on the cache partition only, if
729 // we're given an old 'root' path "CACHE:foo", change it to
730 // "/cache/foo".
731 if (strncmp(update_package, "CACHE:", 6) == 0) {
732 int len = strlen(update_package) + 10;
733 char* modified_path = malloc(len);
734 strlcpy(modified_path, "/cache/", len);
735 strlcat(modified_path, update_package+6, len);
736 printf("(replacing path \"%s\" with \"%s\")\n",
737 update_package, modified_path);
738 update_package = modified_path;
739 }
740 }
741 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800742
743 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700744 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800745
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800746 int status = INSTALL_SUCCESS;
747
Doug Zongker540d57f2011-01-18 13:36:58 -0800748 if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800749 status = install_package(update_package);
750 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700751 } else if (wipe_data) {
752 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700753 if (erase_volume("/data")) status = INSTALL_ERROR;
754 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800755 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700756 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700757 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700758 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800759 } else {
760 status = INSTALL_ERROR; // No command specified
761 }
762
763 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700764 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700765 prompt_and_wait();
766 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800767
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800768 // Otherwise, get ready to boot the main system...
769 finish_recovery(send_intent);
770 ui_print("Rebooting...\n");
771 sync();
772 reboot(RB_AUTOBOOT);
773 return EXIT_SUCCESS;
774}