blob: 7c1d7fb0f922420cd9bb0b5c74f1473bb312568f [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"
Doug Zongker28ce47c2011-10-28 10:33:05 -070041#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
43static const struct option OPTIONS[] = {
44 { "send_intent", required_argument, NULL, 's' },
45 { "update_package", required_argument, NULL, 'u' },
46 { "wipe_data", no_argument, NULL, 'w' },
47 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070048 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070049 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050};
51
Doug Zongkerd4208f92010-09-20 12:16:13 -070052static const char *COMMAND_FILE = "/cache/recovery/command";
53static const char *INTENT_FILE = "/cache/recovery/intent";
54static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070055static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070056static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070057static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070058static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070060static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070061static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062
Doug Zongker6809c512011-03-01 14:04:34 -080063extern UIParameters ui_parameters; // from ui.c
64
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065/*
66 * The recovery tool communicates with the main system through /cache files.
67 * /cache/recovery/command - INPUT - command line for tool, one arg per line
68 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
69 * /cache/recovery/intent - OUTPUT - intent that was passed in
70 *
71 * The arguments which may be supplied in the recovery.command file:
72 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070073 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074 * --wipe_data - erase user data (and cache), then reboot
75 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080076 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 *
78 * After completing, we remove /cache/recovery/command and reboot.
79 * Arguments may also be supplied in the bootloader control block (BCB).
80 * These important scenarios must be safely restartable at any point:
81 *
82 * FACTORY RESET
83 * 1. user selects "factory reset"
84 * 2. main system writes "--wipe_data" to /cache/recovery/command
85 * 3. main system reboots into recovery
86 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
87 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070088 * 5. erase_volume() reformats /data
89 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090 * 7. finish_recovery() erases BCB
91 * -- after this, rebooting will restart the main system --
92 * 8. main() calls reboot() to boot main system
93 *
94 * OTA INSTALL
95 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -070096 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 * 3. main system reboots into recovery
98 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
99 * -- after this, rebooting will attempt to reinstall the update --
100 * 5. install_package() attempts to install the update
101 * NOTE: the package install must itself be restartable from any point
102 * 6. finish_recovery() erases BCB
103 * -- after this, rebooting will (try to) restart the main system --
104 * 7. ** if install failed **
105 * 7a. prompt_and_wait() shows an error icon and waits for the user
106 * 7b; the user reboots (pulling the battery, etc) into the main system
107 * 8. main() calls maybe_install_firmware_update()
108 * ** if the update contained radio/hboot firmware **:
109 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
110 * -- after this, rebooting will reformat cache & restart main system --
111 * 8b. m_i_f_u() writes firmware image into raw cache partition
112 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
113 * -- after this, rebooting will attempt to reinstall firmware --
114 * 8d. bootloader tries to flash firmware
115 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
116 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700117 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 * 8g. finish_recovery() erases BCB
119 * -- after this, rebooting will (try to) restart the main system --
120 * 9. main() calls reboot() to boot main system
121 */
122
123static const int MAX_ARG_LENGTH = 4096;
124static const int MAX_ARGS = 100;
125
Doug Zongkerd4208f92010-09-20 12:16:13 -0700126// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700127FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700128fopen_path(const char *path, const char *mode) {
129 if (ensure_path_mounted(path) != 0) {
130 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800131 return NULL;
132 }
133
134 // When writing, try to create the containing directory, if necessary.
135 // Use generous permissions, the system (init.rc) will reset them.
136 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
137
138 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 return fp;
140}
141
142// close a file, log an error if the error indicator is set
143static void
144check_and_fclose(FILE *fp, const char *name) {
145 fflush(fp);
146 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
147 fclose(fp);
148}
149
150// command line args come from, in decreasing precedence:
151// - the actual command line
152// - the bootloader control block (one per line, after "recovery")
153// - the contents of COMMAND_FILE (one per line)
154static void
155get_args(int *argc, char ***argv) {
156 struct bootloader_message boot;
157 memset(&boot, 0, sizeof(boot));
158 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
159
160 if (boot.command[0] != 0 && boot.command[0] != 255) {
161 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
162 }
163
164 if (boot.status[0] != 0 && boot.status[0] != 255) {
165 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
166 }
167
168 // --- if arguments weren't supplied, look in the bootloader control block
169 if (*argc <= 1) {
170 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
171 const char *arg = strtok(boot.recovery, "\n");
172 if (arg != NULL && !strcmp(arg, "recovery")) {
173 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
174 (*argv)[0] = strdup(arg);
175 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
176 if ((arg = strtok(NULL, "\n")) == NULL) break;
177 (*argv)[*argc] = strdup(arg);
178 }
179 LOGI("Got arguments from boot message\n");
180 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
181 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
182 }
183 }
184
185 // --- if that doesn't work, try the command file
186 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700187 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188 if (fp != NULL) {
189 char *argv0 = (*argv)[0];
190 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
191 (*argv)[0] = argv0; // use the same program name
192
193 char buf[MAX_ARG_LENGTH];
194 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
195 if (!fgets(buf, sizeof(buf), fp)) break;
196 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
197 }
198
199 check_and_fclose(fp, COMMAND_FILE);
200 LOGI("Got arguments from %s\n", COMMAND_FILE);
201 }
202 }
203
204 // --> write the arguments we have back into the bootloader control block
205 // always boot into recovery after this (until finish_recovery() is called)
206 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
207 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
208 int i;
209 for (i = 1; i < *argc; ++i) {
210 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
211 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
212 }
213 set_bootloader_message(&boot);
214}
215
Doug Zongker34c98df2009-08-18 12:05:45 -0700216static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800217set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700218 struct bootloader_message boot;
219 memset(&boot, 0, sizeof(boot));
220 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
221 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
222 set_bootloader_message(&boot);
223}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224
Doug Zongker2c3539e2010-09-29 13:21:30 -0700225// How much of the temp log we have copied to the copy in cache.
226static long tmplog_offset = 0;
227
228static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700229copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700230 FILE *log = fopen_path(destination, append ? "a" : "w");
231 if (log == NULL) {
232 LOGE("Can't open %s\n", destination);
233 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700234 FILE *tmplog = fopen(source, "r");
235 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700236 if (append) {
237 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
238 }
239 char buf[4096];
240 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
241 if (append) {
242 tmplog_offset = ftell(tmplog);
243 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700244 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700245 }
246 check_and_fclose(log, destination);
247 }
248}
249
250
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251// clear the recovery command and prepare to boot a (hopefully working) system,
252// copy our log file to cache as well (for the system to read), and
253// record any intent we were asked to communicate back to the system.
254// this function is idempotent: call it as many times as you like.
255static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800256finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800257 // By this point, we're ready to return to the main system...
258 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700259 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000260 if (fp == NULL) {
261 LOGE("Can't open %s\n", INTENT_FILE);
262 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 fputs(send_intent, fp);
264 check_and_fclose(fp, INTENT_FILE);
265 }
266 }
267
268 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700269 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
270 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
271 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
272 chmod(LOG_FILE, 0600);
273 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700274 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700275 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800276
Oscar Montemayor05231562009-11-30 08:40:57 -0800277 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 struct bootloader_message boot;
279 memset(&boot, 0, sizeof(boot));
280 set_bootloader_message(&boot);
281
282 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700283 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
284 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 LOGW("Can't unlink %s\n", COMMAND_FILE);
286 }
287
Doug Zongkerd0181b82011-10-19 10:51:12 -0700288 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289 sync(); // For good measure.
290}
291
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700293erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 ui_set_background(BACKGROUND_ICON_INSTALLING);
295 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700296 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700297
Doug Zongkerd0181b82011-10-19 10:51:12 -0700298 ensure_path_unmounted(volume);
299
Doug Zongker2c3539e2010-09-29 13:21:30 -0700300 if (strcmp(volume, "/cache") == 0) {
301 // Any part of the log we'd copied to cache is now gone.
302 // Reset the pointer so we copy from the beginning of the temp
303 // log.
304 tmplog_offset = 0;
305 }
306
Doug Zongkerd4208f92010-09-20 12:16:13 -0700307 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308}
309
Doug Zongker23ceeea2010-07-08 17:27:55 -0700310static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700311copy_sideloaded_package(const char* original_path) {
312 if (ensure_path_mounted(original_path) != 0) {
313 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700314 return NULL;
315 }
316
Doug Zongkerd4208f92010-09-20 12:16:13 -0700317 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700318 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
319 return NULL;
320 }
321
Doug Zongkerd4208f92010-09-20 12:16:13 -0700322 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700323 if (errno != EEXIST) {
324 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
325 return NULL;
326 }
327 }
328
Doug Zongkerd4208f92010-09-20 12:16:13 -0700329 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
330 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700331 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700332 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
333 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700334 return NULL;
335 }
336 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700338 return NULL;
339 }
340 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700341 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700342 return NULL;
343 }
344 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700345 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700346 return NULL;
347 }
348
Doug Zongkerd4208f92010-09-20 12:16:13 -0700349 char copy_path[PATH_MAX];
350 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700351 strcat(copy_path, "/package.zip");
352
Doug Zongker28ce47c2011-10-28 10:33:05 -0700353 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700354 if (buffer == NULL) {
355 LOGE("Failed to allocate buffer\n");
356 return NULL;
357 }
358
359 size_t read;
360 FILE* fin = fopen(original_path, "rb");
361 if (fin == NULL) {
362 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
363 return NULL;
364 }
365 FILE* fout = fopen(copy_path, "wb");
366 if (fout == NULL) {
367 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
368 return NULL;
369 }
370
371 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
372 if (fwrite(buffer, 1, read, fout) != read) {
373 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
374 return NULL;
375 }
376 }
377
378 free(buffer);
379
380 if (fclose(fout) != 0) {
381 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
382 return NULL;
383 }
384
385 if (fclose(fin) != 0) {
386 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
387 return NULL;
388 }
389
390 // "adb push" is happy to overwrite read-only files when it's
391 // running as root, but we'll try anyway.
392 if (chmod(copy_path, 0400) != 0) {
393 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
394 return NULL;
395 }
396
Doug Zongkerd4208f92010-09-20 12:16:13 -0700397 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700398}
399
Doug Zongker28ce47c2011-10-28 10:33:05 -0700400static const char**
Doug Zongker8674a722010-09-15 11:08:23 -0700401prepend_title(const char** headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700402 const char* title[] = { "Android system recovery <"
403 EXPAND(RECOVERY_API_VERSION) "e>",
404 "",
405 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800406
Doug Zongkerd6837852009-06-17 22:07:13 -0700407 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700408 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700409 int count = 0;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700410 const char** p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700411 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700412 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700413
Doug Zongker28ce47c2011-10-28 10:33:05 -0700414 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
415 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700416 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700417 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700418 *h = NULL;
419
Doug Zongkerf93d8162009-09-22 15:16:02 -0700420 return new_headers;
421}
422
423static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700424get_menu_selection(const char* const * headers, const char* const * items,
425 int menu_only, int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700426 // throw away keys pressed previously, so user doesn't
427 // accidentally trigger menu items.
428 ui_clear_key_queue();
429
Doug Zongker8674a722010-09-15 11:08:23 -0700430 ui_start_menu(headers, items, initial_selection);
431 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800432 int chosen_item = -1;
433
Doug Zongkerf93d8162009-09-22 15:16:02 -0700434 while (chosen_item < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800435 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800436 int visible = ui_text_visible();
437
Doug Zongker5cae4452011-01-25 13:15:30 -0800438 if (key == -1) { // ui_wait_key() timed out
439 if (ui_text_ever_visible()) {
440 continue;
441 } else {
442 LOGI("timed out waiting for key input; rebooting.\n");
443 ui_end_menu();
444 return ITEM_REBOOT;
445 }
446 }
447
Doug Zongkerddd6a282009-06-09 12:22:33 -0700448 int action = device_handle_key(key, visible);
449
450 if (action < 0) {
451 switch (action) {
452 case HIGHLIGHT_UP:
453 --selected;
454 selected = ui_menu_select(selected);
455 break;
456 case HIGHLIGHT_DOWN:
457 ++selected;
458 selected = ui_menu_select(selected);
459 break;
460 case SELECT_ITEM:
461 chosen_item = selected;
462 break;
463 case NO_ACTION:
464 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800465 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700466 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700467 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700469 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800470
Doug Zongkerf93d8162009-09-22 15:16:02 -0700471 ui_end_menu();
472 return chosen_item;
473}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800474
Doug Zongker8674a722010-09-15 11:08:23 -0700475static int compare_string(const void* a, const void* b) {
476 return strcmp(*(const char**)a, *(const char**)b);
477}
478
479static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700480update_directory(const char* path, const char* unmount_when_done,
481 int* wipe_cache) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700482 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700483
Doug Zongker8674a722010-09-15 11:08:23 -0700484 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700485 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700486 "",
487 NULL };
488 DIR* d;
489 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700490 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700491 if (d == NULL) {
492 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700493 if (unmount_when_done != NULL) {
494 ensure_path_unmounted(unmount_when_done);
495 }
Doug Zongker8674a722010-09-15 11:08:23 -0700496 return 0;
497 }
498
Doug Zongker28ce47c2011-10-28 10:33:05 -0700499 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700500
501 int d_size = 0;
502 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700503 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700504 int z_size = 1;
505 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700506 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700507 zips[0] = strdup("../");
508
509 while ((de = readdir(d)) != NULL) {
510 int name_len = strlen(de->d_name);
511
512 if (de->d_type == DT_DIR) {
513 // skip "." and ".." entries
514 if (name_len == 1 && de->d_name[0] == '.') continue;
515 if (name_len == 2 && de->d_name[0] == '.' &&
516 de->d_name[1] == '.') continue;
517
518 if (d_size >= d_alloc) {
519 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700520 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700521 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700522 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700523 strcpy(dirs[d_size], de->d_name);
524 dirs[d_size][name_len] = '/';
525 dirs[d_size][name_len+1] = '\0';
526 ++d_size;
527 } else if (de->d_type == DT_REG &&
528 name_len >= 4 &&
529 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
530 if (z_size >= z_alloc) {
531 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700532 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700533 }
534 zips[z_size++] = strdup(de->d_name);
535 }
536 }
537 closedir(d);
538
539 qsort(dirs, d_size, sizeof(char*), compare_string);
540 qsort(zips, z_size, sizeof(char*), compare_string);
541
542 // append dirs to the zips list
543 if (d_size + z_size + 1 > z_alloc) {
544 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700545 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700546 }
547 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
548 free(dirs);
549 z_size += d_size;
550 zips[z_size] = NULL;
551
552 int result;
553 int chosen_item = 0;
554 do {
555 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
556
557 char* item = zips[chosen_item];
558 int item_len = strlen(item);
559 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700560 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700561 result = -1;
562 break;
563 } else if (item[item_len-1] == '/') {
564 // recurse down into a subdirectory
565 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700566 strlcpy(new_path, path, PATH_MAX);
567 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700568 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700569 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerd0181b82011-10-19 10:51:12 -0700570 result = update_directory(new_path, unmount_when_done, wipe_cache);
Doug Zongker8674a722010-09-15 11:08:23 -0700571 if (result >= 0) break;
572 } else {
573 // selected a zip file: attempt to install it, and return
574 // the status to the caller.
575 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700576 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700577 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700578 strlcat(new_path, item, PATH_MAX);
579
Doug Zongkerd4208f92010-09-20 12:16:13 -0700580 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700581 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700582 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700583 if (unmount_when_done != NULL) {
584 ensure_path_unmounted(unmount_when_done);
585 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700586 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700587 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700588 free(copy);
589 } else {
590 result = INSTALL_ERROR;
591 }
Doug Zongker8674a722010-09-15 11:08:23 -0700592 break;
593 }
594 } while (true);
595
596 int i;
597 for (i = 0; i < z_size; ++i) free(zips[i]);
598 free(zips);
599 free(headers);
600
Michael Ward9d2629c2011-06-23 22:14:24 -0700601 if (unmount_when_done != NULL) {
602 ensure_path_unmounted(unmount_when_done);
603 }
Doug Zongker8674a722010-09-15 11:08:23 -0700604 return result;
605}
606
Doug Zongkerf93d8162009-09-22 15:16:02 -0700607static void
608wipe_data(int confirm) {
609 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700610 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700611
Doug Zongkerf93d8162009-09-22 15:16:02 -0700612 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700613 const char* headers[] = { "Confirm wipe of all user data?",
614 " THIS CAN NOT BE UNDONE.",
615 "",
616 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700617 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700618 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800619
Doug Zongker28ce47c2011-10-28 10:33:05 -0700620 const char* items[] = { " No",
621 " No",
622 " No",
623 " No",
624 " No",
625 " No",
626 " No",
627 " Yes -- delete all user data", // [7]
628 " No",
629 " No",
630 " No",
631 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800632
Doug Zongker8674a722010-09-15 11:08:23 -0700633 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700634 if (chosen_item != 7) {
635 return;
636 }
637 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700638
Doug Zongkerf93d8162009-09-22 15:16:02 -0700639 ui_print("\n-- Wiping data...\n");
640 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700641 erase_volume("/data");
642 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700643 ui_print("Data wipe complete.\n");
644}
645
646static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800647prompt_and_wait() {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700648 const char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700649
650 for (;;) {
651 finish_recovery(NULL);
652 ui_reset_progress();
653
Doug Zongker8674a722010-09-15 11:08:23 -0700654 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700655
656 // device-specific code may take some action here. It may
657 // return one of the core actions handled in the switch
658 // statement below.
659 chosen_item = device_perform_action(chosen_item);
660
Michael Ward9d2629c2011-06-23 22:14:24 -0700661 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700662 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700663 switch (chosen_item) {
664 case ITEM_REBOOT:
665 return;
666
667 case ITEM_WIPE_DATA:
668 wipe_data(ui_text_visible());
669 if (!ui_text_visible()) return;
670 break;
671
672 case ITEM_WIPE_CACHE:
673 ui_print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700674 erase_volume("/cache");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700675 ui_print("Cache wipe complete.\n");
676 if (!ui_text_visible()) return;
677 break;
678
679 case ITEM_APPLY_SDCARD:
Doug Zongkerd0181b82011-10-19 10:51:12 -0700680 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache);
681 if (status == INSTALL_SUCCESS && wipe_cache) {
682 ui_print("\n-- Wiping cache (at package request)...\n");
683 if (erase_volume("/cache")) {
684 ui_print("Cache wipe failed.\n");
685 } else {
686 ui_print("Cache wipe complete.\n");
687 }
688 }
Doug Zongker8674a722010-09-15 11:08:23 -0700689 if (status >= 0) {
690 if (status != INSTALL_SUCCESS) {
691 ui_set_background(BACKGROUND_ICON_ERROR);
692 ui_print("Installation aborted.\n");
693 } else if (!ui_text_visible()) {
694 return; // reboot if logs aren't visible
695 } else {
696 ui_print("\nInstall from sdcard complete.\n");
697 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700698 }
699 break;
Michael Ward9d2629c2011-06-23 22:14:24 -0700700 case ITEM_APPLY_CACHE:
701 // Don't unmount cache at the end of this.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700702 status = update_directory(CACHE_ROOT, NULL, &wipe_cache);
703 if (status == INSTALL_SUCCESS && wipe_cache) {
704 ui_print("\n-- Wiping cache (at package request)...\n");
705 if (erase_volume("/cache")) {
706 ui_print("Cache wipe failed.\n");
707 } else {
708 ui_print("Cache wipe complete.\n");
709 }
710 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700711 if (status >= 0) {
712 if (status != INSTALL_SUCCESS) {
713 ui_set_background(BACKGROUND_ICON_ERROR);
714 ui_print("Installation aborted.\n");
715 } else if (!ui_text_visible()) {
716 return; // reboot if logs aren't visible
717 } else {
718 ui_print("\nInstall from cache complete.\n");
719 }
720 }
721 break;
722
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800723 }
724 }
725}
726
727static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800728print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700729 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800730}
731
732int
Oscar Montemayor05231562009-11-30 08:40:57 -0800733main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800734 time_t start = time(NULL);
735
736 // If these fail, there's not really anywhere to complain...
737 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
738 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700739 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800740
Doug Zongker6809c512011-03-01 14:04:34 -0800741 device_ui_init(&ui_parameters);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800742 ui_init();
Doug Zongker51266d12010-11-01 10:19:12 -0700743 ui_set_background(BACKGROUND_ICON_INSTALLING);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700744 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800745 get_args(&argc, &argv);
746
747 int previous_runs = 0;
748 const char *send_intent = NULL;
749 const char *update_package = NULL;
750 int wipe_data = 0, wipe_cache = 0;
751
752 int arg;
753 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
754 switch (arg) {
755 case 'p': previous_runs = atoi(optarg); break;
756 case 's': send_intent = optarg; break;
757 case 'u': update_package = optarg; break;
758 case 'w': wipe_data = wipe_cache = 1; break;
759 case 'c': wipe_cache = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700760 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800761 case '?':
762 LOGE("Invalid command argument\n");
763 continue;
764 }
765 }
766
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800767 device_recovery_start();
768
Doug Zongker56c51052010-07-01 09:18:44 -0700769 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800770 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700771 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800772 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700773 printf("\n");
774
775 if (update_package) {
776 // For backwards compatibility on the cache partition only, if
777 // we're given an old 'root' path "CACHE:foo", change it to
778 // "/cache/foo".
779 if (strncmp(update_package, "CACHE:", 6) == 0) {
780 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700781 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700782 strlcpy(modified_path, "/cache/", len);
783 strlcat(modified_path, update_package+6, len);
784 printf("(replacing path \"%s\" with \"%s\")\n",
785 update_package, modified_path);
786 update_package = modified_path;
787 }
788 }
789 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800790
791 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700792 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800793
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800794 int status = INSTALL_SUCCESS;
795
Doug Zongker540d57f2011-01-18 13:36:58 -0800796 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700797 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
798 if (status == INSTALL_SUCCESS && wipe_cache) {
799 if (erase_volume("/cache")) {
800 LOGE("Cache wipe (requested by package) failed.");
801 }
802 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800803 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700804 } else if (wipe_data) {
805 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700806 if (erase_volume("/data")) status = INSTALL_ERROR;
807 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800808 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700809 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700810 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700811 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800812 } else {
813 status = INSTALL_ERROR; // No command specified
814 }
815
816 if (status != INSTALL_SUCCESS) ui_set_background(BACKGROUND_ICON_ERROR);
Doug Zongker8674a722010-09-15 11:08:23 -0700817 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700818 prompt_and_wait();
819 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800820
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800821 // Otherwise, get ready to boot the main system...
822 finish_recovery(send_intent);
823 ui_print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800824 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800825 return EXIT_SUCCESS;
826}