blob: aebfba4f84482d66b636fe9e22b22a3ad2f45ef9 [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
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
Doug Zongkerd4208f92010-09-20 12:16:13 -070067 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068 * --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 --
Doug Zongkerd4208f92010-09-20 12:16:13 -070082 * 5. erase_volume() reformats /data
83 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084 * 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
Doug Zongker9b125b02010-09-22 12:01:37 -070090 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091 * 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 --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700111 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 * 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
115 */
116
117static const int MAX_ARG_LENGTH = 4096;
118static const int MAX_ARGS = 100;
119
Doug Zongkerd4208f92010-09-20 12:16:13 -0700120// open a given path, mounting partitions as necessary
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121static FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700122fopen_path(const char *path, const char *mode) {
123 if (ensure_path_mounted(path) != 0) {
124 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 return NULL;
126 }
127
128 // When writing, try to create the containing directory, if necessary.
129 // Use generous permissions, the system (init.rc) will reset them.
130 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
131
132 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 return fp;
134}
135
136// close a file, log an error if the error indicator is set
137static void
138check_and_fclose(FILE *fp, const char *name) {
139 fflush(fp);
140 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
141 fclose(fp);
142}
143
144// command line args come from, in decreasing precedence:
145// - the actual command line
146// - the bootloader control block (one per line, after "recovery")
147// - the contents of COMMAND_FILE (one per line)
148static void
149get_args(int *argc, char ***argv) {
150 struct bootloader_message boot;
151 memset(&boot, 0, sizeof(boot));
152 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
153
154 if (boot.command[0] != 0 && boot.command[0] != 255) {
155 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
156 }
157
158 if (boot.status[0] != 0 && boot.status[0] != 255) {
159 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
160 }
161
162 // --- if arguments weren't supplied, look in the bootloader control block
163 if (*argc <= 1) {
164 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
165 const char *arg = strtok(boot.recovery, "\n");
166 if (arg != NULL && !strcmp(arg, "recovery")) {
167 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
168 (*argv)[0] = strdup(arg);
169 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
170 if ((arg = strtok(NULL, "\n")) == NULL) break;
171 (*argv)[*argc] = strdup(arg);
172 }
173 LOGI("Got arguments from boot message\n");
174 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
175 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
176 }
177 }
178
179 // --- if that doesn't work, try the command file
180 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 if (fp != NULL) {
183 char *argv0 = (*argv)[0];
184 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
185 (*argv)[0] = argv0; // use the same program name
186
187 char buf[MAX_ARG_LENGTH];
188 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
189 if (!fgets(buf, sizeof(buf), fp)) break;
190 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
191 }
192
193 check_and_fclose(fp, COMMAND_FILE);
194 LOGI("Got arguments from %s\n", COMMAND_FILE);
195 }
196 }
197
198 // --> write the arguments we have back into the bootloader control block
199 // always boot into recovery after this (until finish_recovery() is called)
200 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
201 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
202 int i;
203 for (i = 1; i < *argc; ++i) {
204 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
205 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
206 }
207 set_bootloader_message(&boot);
208}
209
Doug Zongker34c98df2009-08-18 12:05:45 -0700210static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800211set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700212 struct bootloader_message boot;
213 memset(&boot, 0, sizeof(boot));
214 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
215 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
216 set_bootloader_message(&boot);
217}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218
Doug Zongker2c3539e2010-09-29 13:21:30 -0700219// How much of the temp log we have copied to the copy in cache.
220static long tmplog_offset = 0;
221
222static void
223copy_log_file(const char* destination, int append) {
224 FILE *log = fopen_path(destination, append ? "a" : "w");
225 if (log == NULL) {
226 LOGE("Can't open %s\n", destination);
227 } else {
228 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
229 if (tmplog == NULL) {
230 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
231 } else {
232 if (append) {
233 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
234 }
235 char buf[4096];
236 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
237 if (append) {
238 tmplog_offset = ftell(tmplog);
239 }
240 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
241 }
242 check_and_fclose(log, destination);
243 }
244}
245
246
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800247// clear the recovery command and prepare to boot a (hopefully working) system,
248// copy our log file to cache as well (for the system to read), and
249// record any intent we were asked to communicate back to the system.
250// this function is idempotent: call it as many times as you like.
251static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800252finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253 // By this point, we're ready to return to the main system...
254 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700255 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000256 if (fp == NULL) {
257 LOGE("Can't open %s\n", INTENT_FILE);
258 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800259 fputs(send_intent, fp);
260 check_and_fclose(fp, INTENT_FILE);
261 }
262 }
263
264 // Copy logs to cache so the system can find out what happened.
Doug Zongker2c3539e2010-09-29 13:21:30 -0700265 copy_log_file(LOG_FILE, true);
266 copy_log_file(LAST_LOG_FILE, false);
267 chmod(LAST_LOG_FILE, 0640);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268
Oscar Montemayor05231562009-11-30 08:40:57 -0800269 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 struct bootloader_message boot;
271 memset(&boot, 0, sizeof(boot));
272 set_bootloader_message(&boot);
273
274 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700275 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
276 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 LOGW("Can't unlink %s\n", COMMAND_FILE);
278 }
279
280 sync(); // For good measure.
281}
282
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700284erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 ui_set_background(BACKGROUND_ICON_INSTALLING);
286 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700287 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700288
289 if (strcmp(volume, "/cache") == 0) {
290 // Any part of the log we'd copied to cache is now gone.
291 // Reset the pointer so we copy from the beginning of the temp
292 // log.
293 tmplog_offset = 0;
294 }
295
Doug Zongkerd4208f92010-09-20 12:16:13 -0700296 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297}
298
Doug Zongker23ceeea2010-07-08 17:27:55 -0700299static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700300copy_sideloaded_package(const char* original_path) {
301 if (ensure_path_mounted(original_path) != 0) {
302 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700303 return NULL;
304 }
305
Doug Zongkerd4208f92010-09-20 12:16:13 -0700306 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700307 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
308 return NULL;
309 }
310
Doug Zongkerd4208f92010-09-20 12:16:13 -0700311 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700312 if (errno != EEXIST) {
313 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
314 return NULL;
315 }
316 }
317
Doug Zongkerd4208f92010-09-20 12:16:13 -0700318 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
319 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700320 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700321 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
322 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700323 return NULL;
324 }
325 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700326 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700327 return NULL;
328 }
329 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700330 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700331 return NULL;
332 }
333 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700334 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700335 return NULL;
336 }
337
Doug Zongkerd4208f92010-09-20 12:16:13 -0700338 char copy_path[PATH_MAX];
339 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700340 strcat(copy_path, "/package.zip");
341
342 char* buffer = malloc(BUFSIZ);
343 if (buffer == NULL) {
344 LOGE("Failed to allocate buffer\n");
345 return NULL;
346 }
347
348 size_t read;
349 FILE* fin = fopen(original_path, "rb");
350 if (fin == NULL) {
351 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
352 return NULL;
353 }
354 FILE* fout = fopen(copy_path, "wb");
355 if (fout == NULL) {
356 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
357 return NULL;
358 }
359
360 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
361 if (fwrite(buffer, 1, read, fout) != read) {
362 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
363 return NULL;
364 }
365 }
366
367 free(buffer);
368
369 if (fclose(fout) != 0) {
370 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
371 return NULL;
372 }
373
374 if (fclose(fin) != 0) {
375 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
376 return NULL;
377 }
378
379 // "adb push" is happy to overwrite read-only files when it's
380 // running as root, but we'll try anyway.
381 if (chmod(copy_path, 0400) != 0) {
382 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
383 return NULL;
384 }
385
Doug Zongkerd4208f92010-09-20 12:16:13 -0700386 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700387}
388
Doug Zongkerf93d8162009-09-22 15:16:02 -0700389static char**
Doug Zongker8674a722010-09-15 11:08:23 -0700390prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700391 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700392 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700393 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800394 NULL };
395
Doug Zongkerd6837852009-06-17 22:07:13 -0700396 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700397 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700398 int count = 0;
399 char** p;
400 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700401 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700402
Doug Zongkerf93d8162009-09-22 15:16:02 -0700403 char** new_headers = malloc((count+1) * sizeof(char*));
404 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700405 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700406 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700407 *h = NULL;
408
Doug Zongkerf93d8162009-09-22 15:16:02 -0700409 return new_headers;
410}
411
412static int
Doug Zongker8674a722010-09-15 11:08:23 -0700413get_menu_selection(char** headers, char** items, int menu_only,
414 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700415 // throw away keys pressed previously, so user doesn't
416 // accidentally trigger menu items.
417 ui_clear_key_queue();
418
Doug Zongker8674a722010-09-15 11:08:23 -0700419 ui_start_menu(headers, items, initial_selection);
420 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800421 int chosen_item = -1;
422
Doug Zongkerf93d8162009-09-22 15:16:02 -0700423 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800424 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800425 int visible = ui_text_visible();
426
Doug Zongkerddd6a282009-06-09 12:22:33 -0700427 int action = device_handle_key(key, visible);
428
429 if (action < 0) {
430 switch (action) {
431 case HIGHLIGHT_UP:
432 --selected;
433 selected = ui_menu_select(selected);
434 break;
435 case HIGHLIGHT_DOWN:
436 ++selected;
437 selected = ui_menu_select(selected);
438 break;
439 case SELECT_ITEM:
440 chosen_item = selected;
441 break;
442 case NO_ACTION:
443 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800444 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700445 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700446 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800447 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700448 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800449
Doug Zongkerf93d8162009-09-22 15:16:02 -0700450 ui_end_menu();
451 return chosen_item;
452}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800453
Doug Zongker8674a722010-09-15 11:08:23 -0700454static int compare_string(const void* a, const void* b) {
455 return strcmp(*(const char**)a, *(const char**)b);
456}
457
458static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700459sdcard_directory(const char* path) {
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700460 ensure_path_mounted(SDCARD_ROOT);
461
Doug Zongker8674a722010-09-15 11:08:23 -0700462 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700463 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700464 "",
465 NULL };
466 DIR* d;
467 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700468 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700469 if (d == NULL) {
470 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700471 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700472 return 0;
473 }
474
475 char** headers = prepend_title(MENU_HEADERS);
476
477 int d_size = 0;
478 int d_alloc = 10;
479 char** dirs = malloc(d_alloc * sizeof(char*));
480 int z_size = 1;
481 int z_alloc = 10;
482 char** zips = malloc(z_alloc * sizeof(char*));
483 zips[0] = strdup("../");
484
485 while ((de = readdir(d)) != NULL) {
486 int name_len = strlen(de->d_name);
487
488 if (de->d_type == DT_DIR) {
489 // skip "." and ".." entries
490 if (name_len == 1 && de->d_name[0] == '.') continue;
491 if (name_len == 2 && de->d_name[0] == '.' &&
492 de->d_name[1] == '.') continue;
493
494 if (d_size >= d_alloc) {
495 d_alloc *= 2;
496 dirs = realloc(dirs, d_alloc * sizeof(char*));
497 }
498 dirs[d_size] = malloc(name_len + 2);
499 strcpy(dirs[d_size], de->d_name);
500 dirs[d_size][name_len] = '/';
501 dirs[d_size][name_len+1] = '\0';
502 ++d_size;
503 } else if (de->d_type == DT_REG &&
504 name_len >= 4 &&
505 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
506 if (z_size >= z_alloc) {
507 z_alloc *= 2;
508 zips = realloc(zips, z_alloc * sizeof(char*));
509 }
510 zips[z_size++] = strdup(de->d_name);
511 }
512 }
513 closedir(d);
514
515 qsort(dirs, d_size, sizeof(char*), compare_string);
516 qsort(zips, z_size, sizeof(char*), compare_string);
517
518 // append dirs to the zips list
519 if (d_size + z_size + 1 > z_alloc) {
520 z_alloc = d_size + z_size + 1;
521 zips = realloc(zips, z_alloc * sizeof(char*));
522 }
523 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
524 free(dirs);
525 z_size += d_size;
526 zips[z_size] = NULL;
527
528 int result;
529 int chosen_item = 0;
530 do {
531 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
532
533 char* item = zips[chosen_item];
534 int item_len = strlen(item);
535 if (chosen_item == 0) { // item 0 is always "../"
536 // go up but continue browsing (if the caller is sdcard_directory)
537 result = -1;
538 break;
539 } else if (item[item_len-1] == '/') {
540 // recurse down into a subdirectory
541 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700542 strlcpy(new_path, path, PATH_MAX);
543 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700544 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700545 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongker8674a722010-09-15 11:08:23 -0700546 result = sdcard_directory(new_path);
547 if (result >= 0) break;
548 } else {
549 // selected a zip file: attempt to install it, and return
550 // the status to the caller.
551 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700552 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700553 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700554 strlcat(new_path, item, PATH_MAX);
555
Doug Zongkerd4208f92010-09-20 12:16:13 -0700556 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700557 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700558 char* copy = copy_sideloaded_package(new_path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700559 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700560 if (copy) {
561 result = install_package(copy);
562 free(copy);
563 } else {
564 result = INSTALL_ERROR;
565 }
Doug Zongker8674a722010-09-15 11:08:23 -0700566 break;
567 }
568 } while (true);
569
570 int i;
571 for (i = 0; i < z_size; ++i) free(zips[i]);
572 free(zips);
573 free(headers);
574
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700575 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker8674a722010-09-15 11:08:23 -0700576 return result;
577}
578
Doug Zongkerf93d8162009-09-22 15:16:02 -0700579static void
580wipe_data(int confirm) {
581 if (confirm) {
582 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700583
Doug Zongkerf93d8162009-09-22 15:16:02 -0700584 if (title_headers == NULL) {
585 char* headers[] = { "Confirm wipe of all user data?",
586 " THIS CAN NOT BE UNDONE.",
587 "",
588 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700589 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700590 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800591
Doug Zongkerf93d8162009-09-22 15:16:02 -0700592 char* items[] = { " No",
593 " No",
594 " No",
595 " No",
596 " No",
597 " No",
598 " No",
599 " Yes -- delete all user data", // [7]
600 " No",
601 " No",
602 " No",
603 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800604
Doug Zongker8674a722010-09-15 11:08:23 -0700605 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700606 if (chosen_item != 7) {
607 return;
608 }
609 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700610
Doug Zongkerf93d8162009-09-22 15:16:02 -0700611 ui_print("\n-- Wiping data...\n");
612 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700613 erase_volume("/data");
614 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700615 ui_print("Data wipe complete.\n");
616}
617
618static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800619prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700620 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700621
622 for (;;) {
623 finish_recovery(NULL);
624 ui_reset_progress();
625
Doug Zongker8674a722010-09-15 11:08:23 -0700626 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700627
628 // device-specific code may take some action here. It may
629 // return one of the core actions handled in the switch
630 // statement below.
631 chosen_item = device_perform_action(chosen_item);
632
633 switch (chosen_item) {
634 case ITEM_REBOOT:
635 return;
636
637 case ITEM_WIPE_DATA:
638 wipe_data(ui_text_visible());
639 if (!ui_text_visible()) return;
640 break;
641
642 case ITEM_WIPE_CACHE:
643 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700644 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700645 ui_print("Cache wipe complete.\n");
646 if (!ui_text_visible()) return;
647 break;
648
649 case ITEM_APPLY_SDCARD:
Doug Zongker8674a722010-09-15 11:08:23 -0700650 ;
651 int status = sdcard_directory(SDCARD_ROOT);
652 if (status >= 0) {
653 if (status != INSTALL_SUCCESS) {
654 ui_set_background(BACKGROUND_ICON_ERROR);
655 ui_print("Installation aborted.\n");
656 } else if (!ui_text_visible()) {
657 return; // reboot if logs aren't visible
658 } else {
659 ui_print("\nInstall from sdcard complete.\n");
660 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700661 }
662 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800663 }
664 }
665}
666
667static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800668print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700669 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800670}
671
672int
Oscar Montemayor05231562009-11-30 08:40:57 -0800673main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800674 time_t start = time(NULL);
675
676 // If these fail, there's not really anywhere to complain...
677 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
678 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700679 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800680
681 ui_init();
Doug Zongker51266d12010-11-01 10:19:12 -0700682 ui_set_background(BACKGROUND_ICON_INSTALLING);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700683 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800684 get_args(&argc, &argv);
685
686 int previous_runs = 0;
687 const char *send_intent = NULL;
688 const char *update_package = NULL;
689 int wipe_data = 0, wipe_cache = 0;
690
691 int arg;
692 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
693 switch (arg) {
694 case 'p': previous_runs = atoi(optarg); break;
695 case 's': send_intent = optarg; break;
696 case 'u': update_package = optarg; break;
697 case 'w': wipe_data = wipe_cache = 1; break;
698 case 'c': wipe_cache = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700699 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800700 case '?':
701 LOGE("Invalid command argument\n");
702 continue;
703 }
704 }
705
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800706 device_recovery_start();
707
Doug Zongker56c51052010-07-01 09:18:44 -0700708 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800709 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700710 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800711 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700712 printf("\n");
713
714 if (update_package) {
715 // For backwards compatibility on the cache partition only, if
716 // we're given an old 'root' path "CACHE:foo", change it to
717 // "/cache/foo".
718 if (strncmp(update_package, "CACHE:", 6) == 0) {
719 int len = strlen(update_package) + 10;
720 char* modified_path = malloc(len);
721 strlcpy(modified_path, "/cache/", len);
722 strlcat(modified_path, update_package+6, len);
723 printf("(replacing path \"%s\" with \"%s\")\n",
724 update_package, modified_path);
725 update_package = modified_path;
726 }
727 }
728 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800729
730 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700731 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800732
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800733 int status = INSTALL_SUCCESS;
734
Doug Zongker540d57f2011-01-18 13:36:58 -0800735 if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800736 status = install_package(update_package);
737 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700738 } else if (wipe_data) {
739 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700740 if (erase_volume("/data")) status = INSTALL_ERROR;
741 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800742 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700743 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700744 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700745 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800746 } else {
747 status = INSTALL_ERROR; // No command specified
748 }
749
750 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700751 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700752 prompt_and_wait();
753 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800754
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800755 // Otherwise, get ready to boot the main system...
756 finish_recovery(send_intent);
757 ui_print("Rebooting...\n");
758 sync();
759 reboot(RB_AUTOBOOT);
760 return EXIT_SUCCESS;
761}