blob: 06d649809321e338aef7f3b5e55ae69090c68ad3 [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>
Doug Zongker23ceeea2010-07-08 17:27:55 -070026#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
Doug Zongker8674a722010-09-15 11:08:23 -070030#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
32#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "common.h"
34#include "cutils/properties.h"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080035#include "cutils/android_reboot.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 Zongkerd0181b82011-10-19 10:51:12 -070055static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070056static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070057static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070059static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070060static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061
Doug Zongker6809c512011-03-01 14:04:34 -080062extern UIParameters ui_parameters; // from ui.c
63
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064/*
65 * The recovery tool communicates with the main system through /cache files.
66 * /cache/recovery/command - INPUT - command line for tool, one arg per line
67 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
68 * /cache/recovery/intent - OUTPUT - intent that was passed in
69 *
70 * The arguments which may be supplied in the recovery.command file:
71 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070072 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 * --wipe_data - erase user data (and cache), then reboot
74 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080075 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 *
77 * After completing, we remove /cache/recovery/command and reboot.
78 * Arguments may also be supplied in the bootloader control block (BCB).
79 * These important scenarios must be safely restartable at any point:
80 *
81 * FACTORY RESET
82 * 1. user selects "factory reset"
83 * 2. main system writes "--wipe_data" to /cache/recovery/command
84 * 3. main system reboots into recovery
85 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
86 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070087 * 5. erase_volume() reformats /data
88 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089 * 7. finish_recovery() erases BCB
90 * -- after this, rebooting will restart the main system --
91 * 8. main() calls reboot() to boot main system
92 *
93 * OTA INSTALL
94 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -070095 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096 * 3. main system reboots into recovery
97 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
98 * -- after this, rebooting will attempt to reinstall the update --
99 * 5. install_package() attempts to install the update
100 * NOTE: the package install must itself be restartable from any point
101 * 6. finish_recovery() erases BCB
102 * -- after this, rebooting will (try to) restart the main system --
103 * 7. ** if install failed **
104 * 7a. prompt_and_wait() shows an error icon and waits for the user
105 * 7b; the user reboots (pulling the battery, etc) into the main system
106 * 8. main() calls maybe_install_firmware_update()
107 * ** if the update contained radio/hboot firmware **:
108 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
109 * -- after this, rebooting will reformat cache & restart main system --
110 * 8b. m_i_f_u() writes firmware image into raw cache partition
111 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
112 * -- after this, rebooting will attempt to reinstall firmware --
113 * 8d. bootloader tries to flash firmware
114 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
115 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700116 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 * 8g. finish_recovery() erases BCB
118 * -- after this, rebooting will (try to) restart the main system --
119 * 9. main() calls reboot() to boot main system
120 */
121
122static const int MAX_ARG_LENGTH = 4096;
123static const int MAX_ARGS = 100;
124
Doug Zongkerd4208f92010-09-20 12:16:13 -0700125// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700126FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700127fopen_path(const char *path, const char *mode) {
128 if (ensure_path_mounted(path) != 0) {
129 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 return NULL;
131 }
132
133 // When writing, try to create the containing directory, if necessary.
134 // Use generous permissions, the system (init.rc) will reset them.
135 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
136
137 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138 return fp;
139}
140
141// close a file, log an error if the error indicator is set
142static void
143check_and_fclose(FILE *fp, const char *name) {
144 fflush(fp);
145 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
146 fclose(fp);
147}
148
149// command line args come from, in decreasing precedence:
150// - the actual command line
151// - the bootloader control block (one per line, after "recovery")
152// - the contents of COMMAND_FILE (one per line)
153static void
154get_args(int *argc, char ***argv) {
155 struct bootloader_message boot;
156 memset(&boot, 0, sizeof(boot));
157 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
158
159 if (boot.command[0] != 0 && boot.command[0] != 255) {
160 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
161 }
162
163 if (boot.status[0] != 0 && boot.status[0] != 255) {
164 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
165 }
166
167 // --- if arguments weren't supplied, look in the bootloader control block
168 if (*argc <= 1) {
169 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
170 const char *arg = strtok(boot.recovery, "\n");
171 if (arg != NULL && !strcmp(arg, "recovery")) {
172 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
173 (*argv)[0] = strdup(arg);
174 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
175 if ((arg = strtok(NULL, "\n")) == NULL) break;
176 (*argv)[*argc] = strdup(arg);
177 }
178 LOGI("Got arguments from boot message\n");
179 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
180 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
181 }
182 }
183
184 // --- if that doesn't work, try the command file
185 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700186 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187 if (fp != NULL) {
188 char *argv0 = (*argv)[0];
189 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
190 (*argv)[0] = argv0; // use the same program name
191
192 char buf[MAX_ARG_LENGTH];
193 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
194 if (!fgets(buf, sizeof(buf), fp)) break;
195 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
196 }
197
198 check_and_fclose(fp, COMMAND_FILE);
199 LOGI("Got arguments from %s\n", COMMAND_FILE);
200 }
201 }
202
203 // --> write the arguments we have back into the bootloader control block
204 // always boot into recovery after this (until finish_recovery() is called)
205 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
206 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
207 int i;
208 for (i = 1; i < *argc; ++i) {
209 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
210 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
211 }
212 set_bootloader_message(&boot);
213}
214
Doug Zongker34c98df2009-08-18 12:05:45 -0700215static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800216set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700217 struct bootloader_message boot;
218 memset(&boot, 0, sizeof(boot));
219 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
220 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
221 set_bootloader_message(&boot);
222}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223
Doug Zongker2c3539e2010-09-29 13:21:30 -0700224// How much of the temp log we have copied to the copy in cache.
225static long tmplog_offset = 0;
226
227static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700228copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700229 FILE *log = fopen_path(destination, append ? "a" : "w");
230 if (log == NULL) {
231 LOGE("Can't open %s\n", destination);
232 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700233 FILE *tmplog = fopen(source, "r");
234 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700235 if (append) {
236 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
237 }
238 char buf[4096];
239 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
240 if (append) {
241 tmplog_offset = ftell(tmplog);
242 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700243 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700244 }
245 check_and_fclose(log, destination);
246 }
247}
248
249
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250// clear the recovery command and prepare to boot a (hopefully working) system,
251// copy our log file to cache as well (for the system to read), and
252// record any intent we were asked to communicate back to the system.
253// this function is idempotent: call it as many times as you like.
254static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800255finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256 // By this point, we're ready to return to the main system...
257 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700258 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000259 if (fp == NULL) {
260 LOGE("Can't open %s\n", INTENT_FILE);
261 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800262 fputs(send_intent, fp);
263 check_and_fclose(fp, INTENT_FILE);
264 }
265 }
266
267 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700268 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
269 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
270 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
271 chmod(LOG_FILE, 0600);
272 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700273 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700274 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275
Oscar Montemayor05231562009-11-30 08:40:57 -0800276 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 struct bootloader_message boot;
278 memset(&boot, 0, sizeof(boot));
279 set_bootloader_message(&boot);
280
281 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700282 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
283 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800284 LOGW("Can't unlink %s\n", COMMAND_FILE);
285 }
286
Doug Zongkerd0181b82011-10-19 10:51:12 -0700287 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288 sync(); // For good measure.
289}
290
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800291static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700292erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293 ui_set_background(BACKGROUND_ICON_INSTALLING);
294 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700295 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700296
Doug Zongkerd0181b82011-10-19 10:51:12 -0700297 ensure_path_unmounted(volume);
298
Doug Zongker2c3539e2010-09-29 13:21:30 -0700299 if (strcmp(volume, "/cache") == 0) {
300 // Any part of the log we'd copied to cache is now gone.
301 // Reset the pointer so we copy from the beginning of the temp
302 // log.
303 tmplog_offset = 0;
304 }
305
Doug Zongkerd4208f92010-09-20 12:16:13 -0700306 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307}
308
Doug Zongker23ceeea2010-07-08 17:27:55 -0700309static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700310copy_sideloaded_package(const char* original_path) {
311 if (ensure_path_mounted(original_path) != 0) {
312 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700313 return NULL;
314 }
315
Doug Zongkerd4208f92010-09-20 12:16:13 -0700316 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700317 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
318 return NULL;
319 }
320
Doug Zongkerd4208f92010-09-20 12:16:13 -0700321 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700322 if (errno != EEXIST) {
323 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
324 return NULL;
325 }
326 }
327
Doug Zongkerd4208f92010-09-20 12:16:13 -0700328 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
329 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700330 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700331 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
332 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700333 return NULL;
334 }
335 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700336 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700337 return NULL;
338 }
339 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700340 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700341 return NULL;
342 }
343 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700344 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700345 return NULL;
346 }
347
Doug Zongkerd4208f92010-09-20 12:16:13 -0700348 char copy_path[PATH_MAX];
349 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700350 strcat(copy_path, "/package.zip");
351
352 char* buffer = malloc(BUFSIZ);
353 if (buffer == NULL) {
354 LOGE("Failed to allocate buffer\n");
355 return NULL;
356 }
357
358 size_t read;
359 FILE* fin = fopen(original_path, "rb");
360 if (fin == NULL) {
361 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
362 return NULL;
363 }
364 FILE* fout = fopen(copy_path, "wb");
365 if (fout == NULL) {
366 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
367 return NULL;
368 }
369
370 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
371 if (fwrite(buffer, 1, read, fout) != read) {
372 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
373 return NULL;
374 }
375 }
376
377 free(buffer);
378
379 if (fclose(fout) != 0) {
380 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
381 return NULL;
382 }
383
384 if (fclose(fin) != 0) {
385 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
386 return NULL;
387 }
388
389 // "adb push" is happy to overwrite read-only files when it's
390 // running as root, but we'll try anyway.
391 if (chmod(copy_path, 0400) != 0) {
392 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
393 return NULL;
394 }
395
Doug Zongkerd4208f92010-09-20 12:16:13 -0700396 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700397}
398
Doug Zongkerf93d8162009-09-22 15:16:02 -0700399static char**
Doug Zongker8674a722010-09-15 11:08:23 -0700400prepend_title(const char** headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700401 char* title[] = { "Android system recovery <"
Doug Zongker64893cc2009-07-14 16:31:56 -0700402 EXPAND(RECOVERY_API_VERSION) "e>",
Doug Zongkerd6837852009-06-17 22:07:13 -0700403 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800404 NULL };
405
Doug Zongkerd6837852009-06-17 22:07:13 -0700406 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700407 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700408 int count = 0;
409 char** p;
410 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700411 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700412
Doug Zongkerf93d8162009-09-22 15:16:02 -0700413 char** new_headers = malloc((count+1) * sizeof(char*));
414 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700415 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700416 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700417 *h = NULL;
418
Doug Zongkerf93d8162009-09-22 15:16:02 -0700419 return new_headers;
420}
421
422static int
Doug Zongker8674a722010-09-15 11:08:23 -0700423get_menu_selection(char** headers, char** items, int menu_only,
424 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700425 // throw away keys pressed previously, so user doesn't
426 // accidentally trigger menu items.
427 ui_clear_key_queue();
428
Doug Zongker8674a722010-09-15 11:08:23 -0700429 ui_start_menu(headers, items, initial_selection);
430 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800431 int chosen_item = -1;
432
Doug Zongkerf93d8162009-09-22 15:16:02 -0700433 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800434 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800435 int visible = ui_text_visible();
436
Doug Zongker5cae4452011-01-25 13:15:30 -0800437 if (key == -1) { // ui_wait_key() timed out
438 if (ui_text_ever_visible()) {
439 continue;
440 } else {
441 LOGI("timed out waiting for key input; rebooting.\n");
442 ui_end_menu();
443 return ITEM_REBOOT;
444 }
445 }
446
Doug Zongkerddd6a282009-06-09 12:22:33 -0700447 int action = device_handle_key(key, visible);
448
449 if (action < 0) {
450 switch (action) {
451 case HIGHLIGHT_UP:
452 --selected;
453 selected = ui_menu_select(selected);
454 break;
455 case HIGHLIGHT_DOWN:
456 ++selected;
457 selected = ui_menu_select(selected);
458 break;
459 case SELECT_ITEM:
460 chosen_item = selected;
461 break;
462 case NO_ACTION:
463 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700465 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700466 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800467 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700468 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800469
Doug Zongkerf93d8162009-09-22 15:16:02 -0700470 ui_end_menu();
471 return chosen_item;
472}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800473
Doug Zongker8674a722010-09-15 11:08:23 -0700474static int compare_string(const void* a, const void* b) {
475 return strcmp(*(const char**)a, *(const char**)b);
476}
477
478static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700479update_directory(const char* path, const char* unmount_when_done,
480 int* wipe_cache) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700481 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700482
Doug Zongker8674a722010-09-15 11:08:23 -0700483 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700484 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700485 "",
486 NULL };
487 DIR* d;
488 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700489 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700490 if (d == NULL) {
491 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700492 if (unmount_when_done != NULL) {
493 ensure_path_unmounted(unmount_when_done);
494 }
Doug Zongker8674a722010-09-15 11:08:23 -0700495 return 0;
496 }
497
498 char** headers = prepend_title(MENU_HEADERS);
499
500 int d_size = 0;
501 int d_alloc = 10;
502 char** dirs = malloc(d_alloc * sizeof(char*));
503 int z_size = 1;
504 int z_alloc = 10;
505 char** zips = malloc(z_alloc * sizeof(char*));
506 zips[0] = strdup("../");
507
508 while ((de = readdir(d)) != NULL) {
509 int name_len = strlen(de->d_name);
510
511 if (de->d_type == DT_DIR) {
512 // skip "." and ".." entries
513 if (name_len == 1 && de->d_name[0] == '.') continue;
514 if (name_len == 2 && de->d_name[0] == '.' &&
515 de->d_name[1] == '.') continue;
516
517 if (d_size >= d_alloc) {
518 d_alloc *= 2;
519 dirs = realloc(dirs, d_alloc * sizeof(char*));
520 }
521 dirs[d_size] = malloc(name_len + 2);
522 strcpy(dirs[d_size], de->d_name);
523 dirs[d_size][name_len] = '/';
524 dirs[d_size][name_len+1] = '\0';
525 ++d_size;
526 } else if (de->d_type == DT_REG &&
527 name_len >= 4 &&
528 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
529 if (z_size >= z_alloc) {
530 z_alloc *= 2;
531 zips = realloc(zips, z_alloc * sizeof(char*));
532 }
533 zips[z_size++] = strdup(de->d_name);
534 }
535 }
536 closedir(d);
537
538 qsort(dirs, d_size, sizeof(char*), compare_string);
539 qsort(zips, z_size, sizeof(char*), compare_string);
540
541 // append dirs to the zips list
542 if (d_size + z_size + 1 > z_alloc) {
543 z_alloc = d_size + z_size + 1;
544 zips = realloc(zips, z_alloc * sizeof(char*));
545 }
546 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
547 free(dirs);
548 z_size += d_size;
549 zips[z_size] = NULL;
550
551 int result;
552 int chosen_item = 0;
553 do {
554 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
555
556 char* item = zips[chosen_item];
557 int item_len = strlen(item);
558 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700559 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700560 result = -1;
561 break;
562 } else if (item[item_len-1] == '/') {
563 // recurse down into a subdirectory
564 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700565 strlcpy(new_path, path, PATH_MAX);
566 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700567 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700568 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerd0181b82011-10-19 10:51:12 -0700569 result = update_directory(new_path, unmount_when_done, wipe_cache);
Doug Zongker8674a722010-09-15 11:08:23 -0700570 if (result >= 0) break;
571 } else {
572 // selected a zip file: attempt to install it, and return
573 // the status to the caller.
574 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700575 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700576 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700577 strlcat(new_path, item, PATH_MAX);
578
Doug Zongkerd4208f92010-09-20 12:16:13 -0700579 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700580 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700581 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700582 if (unmount_when_done != NULL) {
583 ensure_path_unmounted(unmount_when_done);
584 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700585 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700586 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700587 free(copy);
588 } else {
589 result = INSTALL_ERROR;
590 }
Doug Zongker8674a722010-09-15 11:08:23 -0700591 break;
592 }
593 } while (true);
594
595 int i;
596 for (i = 0; i < z_size; ++i) free(zips[i]);
597 free(zips);
598 free(headers);
599
Michael Ward9d2629c2011-06-23 22:14:24 -0700600 if (unmount_when_done != NULL) {
601 ensure_path_unmounted(unmount_when_done);
602 }
Doug Zongker8674a722010-09-15 11:08:23 -0700603 return result;
604}
605
Doug Zongkerf93d8162009-09-22 15:16:02 -0700606static void
607wipe_data(int confirm) {
608 if (confirm) {
609 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700610
Doug Zongkerf93d8162009-09-22 15:16:02 -0700611 if (title_headers == NULL) {
612 char* headers[] = { "Confirm wipe of all user data?",
613 " THIS CAN NOT BE UNDONE.",
614 "",
615 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700616 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700617 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800618
Doug Zongkerf93d8162009-09-22 15:16:02 -0700619 char* items[] = { " No",
620 " No",
621 " No",
622 " No",
623 " No",
624 " No",
625 " No",
626 " Yes -- delete all user data", // [7]
627 " No",
628 " No",
629 " No",
630 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800631
Doug Zongker8674a722010-09-15 11:08:23 -0700632 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700633 if (chosen_item != 7) {
634 return;
635 }
636 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700637
Doug Zongkerf93d8162009-09-22 15:16:02 -0700638 ui_print("\n-- Wiping data...\n");
639 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700640 erase_volume("/data");
641 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700642 ui_print("Data wipe complete.\n");
643}
644
645static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800646prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700647 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700648
649 for (;;) {
650 finish_recovery(NULL);
651 ui_reset_progress();
652
Doug Zongker8674a722010-09-15 11:08:23 -0700653 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700654
655 // device-specific code may take some action here. It may
656 // return one of the core actions handled in the switch
657 // statement below.
658 chosen_item = device_perform_action(chosen_item);
659
Michael Ward9d2629c2011-06-23 22:14:24 -0700660 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700661 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700662 switch (chosen_item) {
663 case ITEM_REBOOT:
664 return;
665
666 case ITEM_WIPE_DATA:
667 wipe_data(ui_text_visible());
668 if (!ui_text_visible()) return;
669 break;
670
671 case ITEM_WIPE_CACHE:
672 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700673 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700674 ui_print("Cache wipe complete.\n");
675 if (!ui_text_visible()) return;
676 break;
677
678 case ITEM_APPLY_SDCARD:
Doug Zongkerd0181b82011-10-19 10:51:12 -0700679 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache);
680 if (status == INSTALL_SUCCESS && wipe_cache) {
681 ui_print("\n-- Wiping cache (at package request)...\n");
682 if (erase_volume("/cache")) {
683 ui_print("Cache wipe failed.\n");
684 } else {
685 ui_print("Cache wipe complete.\n");
686 }
687 }
Doug Zongker8674a722010-09-15 11:08:23 -0700688 if (status >= 0) {
689 if (status != INSTALL_SUCCESS) {
690 ui_set_background(BACKGROUND_ICON_ERROR);
691 ui_print("Installation aborted.\n");
692 } else if (!ui_text_visible()) {
693 return; // reboot if logs aren't visible
694 } else {
695 ui_print("\nInstall from sdcard complete.\n");
696 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700697 }
698 break;
Michael Ward9d2629c2011-06-23 22:14:24 -0700699 case ITEM_APPLY_CACHE:
700 // Don't unmount cache at the end of this.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700701 status = update_directory(CACHE_ROOT, NULL, &wipe_cache);
702 if (status == INSTALL_SUCCESS && wipe_cache) {
703 ui_print("\n-- Wiping cache (at package request)...\n");
704 if (erase_volume("/cache")) {
705 ui_print("Cache wipe failed.\n");
706 } else {
707 ui_print("Cache wipe complete.\n");
708 }
709 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700710 if (status >= 0) {
711 if (status != INSTALL_SUCCESS) {
712 ui_set_background(BACKGROUND_ICON_ERROR);
713 ui_print("Installation aborted.\n");
714 } else if (!ui_text_visible()) {
715 return; // reboot if logs aren't visible
716 } else {
717 ui_print("\nInstall from cache complete.\n");
718 }
719 }
720 break;
721
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800722 }
723 }
724}
725
726static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800727print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700728 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800729}
730
731int
Oscar Montemayor05231562009-11-30 08:40:57 -0800732main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800733 time_t start = time(NULL);
734
735 // If these fail, there's not really anywhere to complain...
736 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
737 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700738 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800739
Doug Zongker6809c512011-03-01 14:04:34 -0800740 device_ui_init(&ui_parameters);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800741 ui_init();
Doug Zongker51266d12010-11-01 10:19:12 -0700742 ui_set_background(BACKGROUND_ICON_INSTALLING);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700743 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800744 get_args(&argc, &argv);
745
746 int previous_runs = 0;
747 const char *send_intent = NULL;
748 const char *update_package = NULL;
749 int wipe_data = 0, wipe_cache = 0;
750
751 int arg;
752 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
753 switch (arg) {
754 case 'p': previous_runs = atoi(optarg); break;
755 case 's': send_intent = optarg; break;
756 case 'u': update_package = optarg; break;
757 case 'w': wipe_data = wipe_cache = 1; break;
758 case 'c': wipe_cache = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700759 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800760 case '?':
761 LOGE("Invalid command argument\n");
762 continue;
763 }
764 }
765
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800766 device_recovery_start();
767
Doug Zongker56c51052010-07-01 09:18:44 -0700768 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800769 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700770 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800771 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700772 printf("\n");
773
774 if (update_package) {
775 // For backwards compatibility on the cache partition only, if
776 // we're given an old 'root' path "CACHE:foo", change it to
777 // "/cache/foo".
778 if (strncmp(update_package, "CACHE:", 6) == 0) {
779 int len = strlen(update_package) + 10;
780 char* modified_path = malloc(len);
781 strlcpy(modified_path, "/cache/", len);
782 strlcat(modified_path, update_package+6, len);
783 printf("(replacing path \"%s\" with \"%s\")\n",
784 update_package, modified_path);
785 update_package = modified_path;
786 }
787 }
788 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800789
790 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700791 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800792
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800793 int status = INSTALL_SUCCESS;
794
Doug Zongker540d57f2011-01-18 13:36:58 -0800795 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700796 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
797 if (status == INSTALL_SUCCESS && wipe_cache) {
798 if (erase_volume("/cache")) {
799 LOGE("Cache wipe (requested by package) failed.");
800 }
801 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800802 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700803 } else if (wipe_data) {
804 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700805 if (erase_volume("/data")) status = INSTALL_ERROR;
806 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800807 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700808 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700809 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700810 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800811 } else {
812 status = INSTALL_ERROR; // No command specified
813 }
814
815 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700816 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700817 prompt_and_wait();
818 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800819
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800820 // Otherwise, get ready to boot the main system...
821 finish_recovery(send_intent);
822 ui_print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800823 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800824 return EXIT_SUCCESS;
825}