blob: 726442b84f4fd7547c582bf5af6c49778a217adc [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 Zongker28ce47c2011-10-28 10:33:05 -070040#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070041#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070042#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080043#include "adb_install.h"
44extern "C" {
45#include "minadbd/adb.h"
46}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
48static const struct option OPTIONS[] = {
49 { "send_intent", required_argument, NULL, 's' },
50 { "update_package", required_argument, NULL, 'u' },
51 { "wipe_data", no_argument, NULL, 'w' },
52 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070053 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070054 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055};
56
Doug Zongkerd4208f92010-09-20 12:16:13 -070057static const char *COMMAND_FILE = "/cache/recovery/command";
58static const char *INTENT_FILE = "/cache/recovery/intent";
59static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070060static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070061static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070062static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070063static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070065static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070066static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067
Doug Zongker211aebc2011-10-28 15:13:10 -070068RecoveryUI* ui = NULL;
69
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070/*
71 * The recovery tool communicates with the main system through /cache files.
72 * /cache/recovery/command - INPUT - command line for tool, one arg per line
73 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
74 * /cache/recovery/intent - OUTPUT - intent that was passed in
75 *
76 * The arguments which may be supplied in the recovery.command file:
77 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070078 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079 * --wipe_data - erase user data (and cache), then reboot
80 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080081 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082 *
83 * After completing, we remove /cache/recovery/command and reboot.
84 * Arguments may also be supplied in the bootloader control block (BCB).
85 * These important scenarios must be safely restartable at any point:
86 *
87 * FACTORY RESET
88 * 1. user selects "factory reset"
89 * 2. main system writes "--wipe_data" to /cache/recovery/command
90 * 3. main system reboots into recovery
91 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
92 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -070093 * 5. erase_volume() reformats /data
94 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095 * 7. finish_recovery() erases BCB
96 * -- after this, rebooting will restart the main system --
97 * 8. main() calls reboot() to boot main system
98 *
99 * OTA INSTALL
100 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700101 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102 * 3. main system reboots into recovery
103 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
104 * -- after this, rebooting will attempt to reinstall the update --
105 * 5. install_package() attempts to install the update
106 * NOTE: the package install must itself be restartable from any point
107 * 6. finish_recovery() erases BCB
108 * -- after this, rebooting will (try to) restart the main system --
109 * 7. ** if install failed **
110 * 7a. prompt_and_wait() shows an error icon and waits for the user
111 * 7b; the user reboots (pulling the battery, etc) into the main system
112 * 8. main() calls maybe_install_firmware_update()
113 * ** if the update contained radio/hboot firmware **:
114 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
115 * -- after this, rebooting will reformat cache & restart main system --
116 * 8b. m_i_f_u() writes firmware image into raw cache partition
117 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
118 * -- after this, rebooting will attempt to reinstall firmware --
119 * 8d. bootloader tries to flash firmware
120 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
121 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700122 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123 * 8g. finish_recovery() erases BCB
124 * -- after this, rebooting will (try to) restart the main system --
125 * 9. main() calls reboot() to boot main system
126 */
127
128static const int MAX_ARG_LENGTH = 4096;
129static const int MAX_ARGS = 100;
130
Doug Zongkerd4208f92010-09-20 12:16:13 -0700131// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700132FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700133fopen_path(const char *path, const char *mode) {
134 if (ensure_path_mounted(path) != 0) {
135 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 return NULL;
137 }
138
139 // When writing, try to create the containing directory, if necessary.
140 // Use generous permissions, the system (init.rc) will reset them.
141 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
142
143 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144 return fp;
145}
146
147// close a file, log an error if the error indicator is set
148static void
149check_and_fclose(FILE *fp, const char *name) {
150 fflush(fp);
151 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
152 fclose(fp);
153}
154
155// command line args come from, in decreasing precedence:
156// - the actual command line
157// - the bootloader control block (one per line, after "recovery")
158// - the contents of COMMAND_FILE (one per line)
159static void
160get_args(int *argc, char ***argv) {
161 struct bootloader_message boot;
162 memset(&boot, 0, sizeof(boot));
163 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
164
165 if (boot.command[0] != 0 && boot.command[0] != 255) {
166 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
167 }
168
169 if (boot.status[0] != 0 && boot.status[0] != 255) {
170 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
171 }
172
173 // --- if arguments weren't supplied, look in the bootloader control block
174 if (*argc <= 1) {
175 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
176 const char *arg = strtok(boot.recovery, "\n");
177 if (arg != NULL && !strcmp(arg, "recovery")) {
178 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
179 (*argv)[0] = strdup(arg);
180 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
181 if ((arg = strtok(NULL, "\n")) == NULL) break;
182 (*argv)[*argc] = strdup(arg);
183 }
184 LOGI("Got arguments from boot message\n");
185 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
186 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
187 }
188 }
189
190 // --- if that doesn't work, try the command file
191 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700192 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800193 if (fp != NULL) {
194 char *argv0 = (*argv)[0];
195 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
196 (*argv)[0] = argv0; // use the same program name
197
198 char buf[MAX_ARG_LENGTH];
199 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
200 if (!fgets(buf, sizeof(buf), fp)) break;
201 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
202 }
203
204 check_and_fclose(fp, COMMAND_FILE);
205 LOGI("Got arguments from %s\n", COMMAND_FILE);
206 }
207 }
208
209 // --> write the arguments we have back into the bootloader control block
210 // always boot into recovery after this (until finish_recovery() is called)
211 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
212 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
213 int i;
214 for (i = 1; i < *argc; ++i) {
215 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
216 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
217 }
218 set_bootloader_message(&boot);
219}
220
Doug Zongker34c98df2009-08-18 12:05:45 -0700221static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800222set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700223 struct bootloader_message boot;
224 memset(&boot, 0, sizeof(boot));
225 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
226 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
227 set_bootloader_message(&boot);
228}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229
Doug Zongker2c3539e2010-09-29 13:21:30 -0700230// How much of the temp log we have copied to the copy in cache.
231static long tmplog_offset = 0;
232
233static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700234copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700235 FILE *log = fopen_path(destination, append ? "a" : "w");
236 if (log == NULL) {
237 LOGE("Can't open %s\n", destination);
238 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700239 FILE *tmplog = fopen(source, "r");
240 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700241 if (append) {
242 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
243 }
244 char buf[4096];
245 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
246 if (append) {
247 tmplog_offset = ftell(tmplog);
248 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700249 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700250 }
251 check_and_fclose(log, destination);
252 }
253}
254
255
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256// clear the recovery command and prepare to boot a (hopefully working) system,
257// copy our log file to cache as well (for the system to read), and
258// record any intent we were asked to communicate back to the system.
259// this function is idempotent: call it as many times as you like.
260static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800261finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800262 // By this point, we're ready to return to the main system...
263 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700264 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000265 if (fp == NULL) {
266 LOGE("Can't open %s\n", INTENT_FILE);
267 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268 fputs(send_intent, fp);
269 check_and_fclose(fp, INTENT_FILE);
270 }
271 }
272
273 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700274 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
275 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
276 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
277 chmod(LOG_FILE, 0600);
278 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700279 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700280 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281
Oscar Montemayor05231562009-11-30 08:40:57 -0800282 // Reset to mormal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283 struct bootloader_message boot;
284 memset(&boot, 0, sizeof(boot));
285 set_bootloader_message(&boot);
286
287 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700288 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
289 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290 LOGW("Can't unlink %s\n", COMMAND_FILE);
291 }
292
Doug Zongkerd0181b82011-10-19 10:51:12 -0700293 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 sync(); // For good measure.
295}
296
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700298erase_volume(const char *volume) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700299 ui->SetBackground(RecoveryUI::INSTALLING);
300 ui->SetProgressType(RecoveryUI::INDETERMINATE);
301 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700302
Doug Zongkerd0181b82011-10-19 10:51:12 -0700303 ensure_path_unmounted(volume);
304
Doug Zongker2c3539e2010-09-29 13:21:30 -0700305 if (strcmp(volume, "/cache") == 0) {
306 // Any part of the log we'd copied to cache is now gone.
307 // Reset the pointer so we copy from the beginning of the temp
308 // log.
309 tmplog_offset = 0;
310 }
311
Doug Zongkerd4208f92010-09-20 12:16:13 -0700312 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313}
314
Doug Zongker23ceeea2010-07-08 17:27:55 -0700315static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700316copy_sideloaded_package(const char* original_path) {
317 if (ensure_path_mounted(original_path) != 0) {
318 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700319 return NULL;
320 }
321
Doug Zongkerd4208f92010-09-20 12:16:13 -0700322 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700323 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
324 return NULL;
325 }
326
Doug Zongkerd4208f92010-09-20 12:16:13 -0700327 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700328 if (errno != EEXIST) {
329 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
330 return NULL;
331 }
332 }
333
Doug Zongkerd4208f92010-09-20 12:16:13 -0700334 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
335 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700336 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
338 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700339 return NULL;
340 }
341 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700342 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700343 return NULL;
344 }
345 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700346 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700347 return NULL;
348 }
349 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700350 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700351 return NULL;
352 }
353
Doug Zongkerd4208f92010-09-20 12:16:13 -0700354 char copy_path[PATH_MAX];
355 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700356 strcat(copy_path, "/package.zip");
357
Doug Zongker28ce47c2011-10-28 10:33:05 -0700358 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700359 if (buffer == NULL) {
360 LOGE("Failed to allocate buffer\n");
361 return NULL;
362 }
363
364 size_t read;
365 FILE* fin = fopen(original_path, "rb");
366 if (fin == NULL) {
367 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
368 return NULL;
369 }
370 FILE* fout = fopen(copy_path, "wb");
371 if (fout == NULL) {
372 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
373 return NULL;
374 }
375
376 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
377 if (fwrite(buffer, 1, read, fout) != read) {
378 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
379 return NULL;
380 }
381 }
382
383 free(buffer);
384
385 if (fclose(fout) != 0) {
386 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
387 return NULL;
388 }
389
390 if (fclose(fin) != 0) {
391 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
392 return NULL;
393 }
394
395 // "adb push" is happy to overwrite read-only files when it's
396 // running as root, but we'll try anyway.
397 if (chmod(copy_path, 0400) != 0) {
398 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
399 return NULL;
400 }
401
Doug Zongkerd4208f92010-09-20 12:16:13 -0700402 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700403}
404
Doug Zongker28ce47c2011-10-28 10:33:05 -0700405static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700406prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700407 const char* title[] = { "Android system recovery <"
408 EXPAND(RECOVERY_API_VERSION) "e>",
409 "",
410 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800411
Doug Zongkerd6837852009-06-17 22:07:13 -0700412 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700413 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700414 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700415 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700416 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700417 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700418
Doug Zongker28ce47c2011-10-28 10:33:05 -0700419 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
420 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700421 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700422 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700423 *h = NULL;
424
Doug Zongkerf93d8162009-09-22 15:16:02 -0700425 return new_headers;
426}
427
428static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700429get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700430 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700431 // throw away keys pressed previously, so user doesn't
432 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700433 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700434
Doug Zongker211aebc2011-10-28 15:13:10 -0700435 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700436 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800437 int chosen_item = -1;
438
Doug Zongkerf93d8162009-09-22 15:16:02 -0700439 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700440 int key = ui->WaitKey();
441 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800442
Doug Zongker5cae4452011-01-25 13:15:30 -0800443 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700444 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800445 continue;
446 } else {
447 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700448 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700449 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800450 }
451 }
452
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700453 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700454
455 if (action < 0) {
456 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700457 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700458 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700459 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700460 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700461 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700462 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700463 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700464 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700465 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700466 chosen_item = selected;
467 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700468 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700469 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800470 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700471 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700472 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800473 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700474 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800475
Doug Zongker211aebc2011-10-28 15:13:10 -0700476 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700477 return chosen_item;
478}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800479
Doug Zongker8674a722010-09-15 11:08:23 -0700480static int compare_string(const void* a, const void* b) {
481 return strcmp(*(const char**)a, *(const char**)b);
482}
483
484static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700485update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700486 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700487 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700488
Doug Zongker8674a722010-09-15 11:08:23 -0700489 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700490 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700491 "",
492 NULL };
493 DIR* d;
494 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700495 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700496 if (d == NULL) {
497 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700498 if (unmount_when_done != NULL) {
499 ensure_path_unmounted(unmount_when_done);
500 }
Doug Zongker8674a722010-09-15 11:08:23 -0700501 return 0;
502 }
503
Doug Zongker28ce47c2011-10-28 10:33:05 -0700504 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700505
506 int d_size = 0;
507 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700508 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700509 int z_size = 1;
510 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700511 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700512 zips[0] = strdup("../");
513
514 while ((de = readdir(d)) != NULL) {
515 int name_len = strlen(de->d_name);
516
517 if (de->d_type == DT_DIR) {
518 // skip "." and ".." entries
519 if (name_len == 1 && de->d_name[0] == '.') continue;
520 if (name_len == 2 && de->d_name[0] == '.' &&
521 de->d_name[1] == '.') continue;
522
523 if (d_size >= d_alloc) {
524 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700525 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700526 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700527 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700528 strcpy(dirs[d_size], de->d_name);
529 dirs[d_size][name_len] = '/';
530 dirs[d_size][name_len+1] = '\0';
531 ++d_size;
532 } else if (de->d_type == DT_REG &&
533 name_len >= 4 &&
534 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
535 if (z_size >= z_alloc) {
536 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700537 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700538 }
539 zips[z_size++] = strdup(de->d_name);
540 }
541 }
542 closedir(d);
543
544 qsort(dirs, d_size, sizeof(char*), compare_string);
545 qsort(zips, z_size, sizeof(char*), compare_string);
546
547 // append dirs to the zips list
548 if (d_size + z_size + 1 > z_alloc) {
549 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700550 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700551 }
552 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
553 free(dirs);
554 z_size += d_size;
555 zips[z_size] = NULL;
556
557 int result;
558 int chosen_item = 0;
559 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700560 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700561
562 char* item = zips[chosen_item];
563 int item_len = strlen(item);
564 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700565 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700566 result = -1;
567 break;
568 } else if (item[item_len-1] == '/') {
569 // recurse down into a subdirectory
570 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700571 strlcpy(new_path, path, PATH_MAX);
572 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700573 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700574 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700575 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700576 if (result >= 0) break;
577 } else {
578 // selected a zip file: attempt to install it, and return
579 // the status to the caller.
580 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700581 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700582 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700583 strlcat(new_path, item, PATH_MAX);
584
Doug Zongker211aebc2011-10-28 15:13:10 -0700585 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700586 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700587 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700588 if (unmount_when_done != NULL) {
589 ensure_path_unmounted(unmount_when_done);
590 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700591 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700592 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700593 free(copy);
594 } else {
595 result = INSTALL_ERROR;
596 }
Doug Zongker8674a722010-09-15 11:08:23 -0700597 break;
598 }
599 } while (true);
600
601 int i;
602 for (i = 0; i < z_size; ++i) free(zips[i]);
603 free(zips);
604 free(headers);
605
Michael Ward9d2629c2011-06-23 22:14:24 -0700606 if (unmount_when_done != NULL) {
607 ensure_path_unmounted(unmount_when_done);
608 }
Doug Zongker8674a722010-09-15 11:08:23 -0700609 return result;
610}
611
Doug Zongkerf93d8162009-09-22 15:16:02 -0700612static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700613wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700614 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700615 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700616
Doug Zongkerf93d8162009-09-22 15:16:02 -0700617 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700618 const char* headers[] = { "Confirm wipe of all user data?",
619 " THIS CAN NOT BE UNDONE.",
620 "",
621 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700622 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700623 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800624
Doug Zongker28ce47c2011-10-28 10:33:05 -0700625 const char* items[] = { " No",
626 " No",
627 " No",
628 " No",
629 " No",
630 " No",
631 " No",
632 " Yes -- delete all user data", // [7]
633 " No",
634 " No",
635 " No",
636 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800637
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700638 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700639 if (chosen_item != 7) {
640 return;
641 }
642 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700643
Doug Zongker211aebc2011-10-28 15:13:10 -0700644 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700645 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700646 erase_volume("/data");
647 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700648 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700649}
650
651static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700652prompt_and_wait(Device* device) {
653 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700654
655 for (;;) {
656 finish_recovery(NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700657 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700658
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700659 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700660
661 // device-specific code may take some action here. It may
662 // return one of the core actions handled in the switch
663 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700664 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700665
Michael Ward9d2629c2011-06-23 22:14:24 -0700666 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700667 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700668 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700669 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700670 return;
671
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700672 case Device::WIPE_DATA:
673 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700674 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700675 break;
676
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700677 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700678 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700679 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700680 ui->Print("Cache wipe complete.\n");
681 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700682 break;
683
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700684 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800685 // Some packages expect /cache to be mounted (eg,
686 // standard incremental packages expect to use /cache
687 // as scratch space).
688 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700689 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700690 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700691 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700692 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700693 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700694 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700695 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700696 }
697 }
Doug Zongker8674a722010-09-15 11:08:23 -0700698 if (status >= 0) {
699 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700700 ui->SetBackground(RecoveryUI::ERROR);
701 ui->Print("Installation aborted.\n");
702 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700703 return; // reboot if logs aren't visible
704 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700705 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700706 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700707 }
708 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700709
710 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700711 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700712 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700713 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700714 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700715 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700716 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700717 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700718 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700719 }
720 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700721 if (status >= 0) {
722 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700723 ui->SetBackground(RecoveryUI::ERROR);
724 ui->Print("Installation aborted.\n");
725 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700726 return; // reboot if logs aren't visible
727 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700728 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700729 }
730 }
731 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800732
733 case Device::APPLY_ADB_SIDELOAD:
734 ensure_path_mounted(CACHE_ROOT);
735 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
736 if (status >= 0) {
737 if (status != INSTALL_SUCCESS) {
738 ui->SetBackground(RecoveryUI::ERROR);
739 ui->Print("Installation aborted.\n");
740 } else if (!ui->IsTextVisible()) {
741 return; // reboot if logs aren't visible
742 } else {
743 ui->Print("\nInstall from ADB complete.\n");
744 }
745 }
746 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800747 }
748 }
749}
750
751static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800752print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700753 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800754}
755
756int
Oscar Montemayor05231562009-11-30 08:40:57 -0800757main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800758 time_t start = time(NULL);
759
760 // If these fail, there's not really anywhere to complain...
761 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
762 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800763
764 // If this binary is started with the single argument "--adbd",
765 // instead of being the normal recovery binary, it turns into kind
766 // of a stripped-down version of adbd that only supports the
767 // 'sideload' command. Note this must be a real argument, not
768 // anything in the command file or bootloader control block; the
769 // only way recovery should be run with this argument is when it
770 // starts a copy of itself from the apply_from_adb() function.
771 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
772 adb_main();
773 return 0;
774 }
775
Doug Zongker56c51052010-07-01 09:18:44 -0700776 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800777
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700778 Device* device = make_device();
779 ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700780
781 ui->Init();
Doug Zongker32a0a472011-11-01 11:00:20 -0700782 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700783 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800784 get_args(&argc, &argv);
785
786 int previous_runs = 0;
787 const char *send_intent = NULL;
788 const char *update_package = NULL;
789 int wipe_data = 0, wipe_cache = 0;
790
791 int arg;
792 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
793 switch (arg) {
794 case 'p': previous_runs = atoi(optarg); break;
795 case 's': send_intent = optarg; break;
796 case 'u': update_package = optarg; break;
797 case 'w': wipe_data = wipe_cache = 1; break;
798 case 'c': wipe_cache = 1; break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700799 case 't': ui->ShowText(true); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800800 case '?':
801 LOGE("Invalid command argument\n");
802 continue;
803 }
804 }
805
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700806 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800807
Doug Zongker56c51052010-07-01 09:18:44 -0700808 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800809 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700810 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800811 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700812 printf("\n");
813
814 if (update_package) {
815 // For backwards compatibility on the cache partition only, if
816 // we're given an old 'root' path "CACHE:foo", change it to
817 // "/cache/foo".
818 if (strncmp(update_package, "CACHE:", 6) == 0) {
819 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700820 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700821 strlcpy(modified_path, "/cache/", len);
822 strlcat(modified_path, update_package+6, len);
823 printf("(replacing path \"%s\" with \"%s\")\n",
824 update_package, modified_path);
825 update_package = modified_path;
826 }
827 }
828 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800829
830 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700831 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800832
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800833 int status = INSTALL_SUCCESS;
834
Doug Zongker540d57f2011-01-18 13:36:58 -0800835 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700836 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
837 if (status == INSTALL_SUCCESS && wipe_cache) {
838 if (erase_volume("/cache")) {
839 LOGE("Cache wipe (requested by package) failed.");
840 }
841 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700842 if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700843 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700844 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700845 if (erase_volume("/data")) status = INSTALL_ERROR;
846 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700847 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700848 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700849 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700850 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800851 } else {
852 status = INSTALL_ERROR; // No command specified
853 }
854
Doug Zongker211aebc2011-10-28 15:13:10 -0700855 if (status != INSTALL_SUCCESS) ui->SetBackground(RecoveryUI::ERROR);
856 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700857 prompt_and_wait(device);
Doug Zongker8674a722010-09-15 11:08:23 -0700858 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800859
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800860 // Otherwise, get ready to boot the main system...
861 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -0700862 ui->Print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800863 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800864 return EXIT_SUCCESS;
865}