blob: 3fe7ae2341d3899f4e16737d3f4832ae0bb347a5 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
Shashank Mittal815ca5d2010-07-27 11:09:19 -07003 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <ctype.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <limits.h>
23#include <linux/input.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070027#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include <sys/types.h>
29#include <time.h>
30#include <unistd.h>
Doug Zongker8674a722010-09-15 11:08:23 -070031#include <dirent.h>
Koushik Dutta1741dcd2010-06-11 00:49:49 -070032#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
34#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "common.h"
36#include "cutils/properties.h"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080037#include "cutils/android_reboot.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#include "install.h"
39#include "minui/minui.h"
40#include "minzip/DirUtil.h"
41#include "roots.h"
Doug Zongkerddd6a282009-06-09 12:22:33 -070042#include "recovery_ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080044#include "extendedcommands.h"
Koushik Dutta9765a032010-12-18 21:31:02 -080045#include "flashutils/flashutils.h"
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080046
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047static const struct option OPTIONS[] = {
48 { "send_intent", required_argument, NULL, 's' },
49 { "update_package", required_argument, NULL, 'u' },
50 { "wipe_data", no_argument, NULL, 'w' },
51 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070052 { "show_text", no_argument, NULL, 't' },
Doug Zongker988500b2009-10-06 14:41:38 -070053 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054};
55
Doug Zongkerd4208f92010-09-20 12:16:13 -070056static const char *COMMAND_FILE = "/cache/recovery/command";
57static const char *INTENT_FILE = "/cache/recovery/intent";
58static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070059static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Michael Ward9d2629c2011-06-23 22:14:24 -070060static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070061static const char *SDCARD_ROOT = "/sdcard";
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -080062static int allow_display_toggle = 1;
KalimochoAzd646a6f2011-01-10 06:14:21 +010063static int poweroff = 0;
Koushik Duttadf1e4062010-12-18 17:42:31 -080064static const char *SDCARD_PACKAGE_FILE = "/sdcard/update.zip";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
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 Zongker6809c512011-03-01 14:04:34 -080068extern UIParameters ui_parameters; // from ui.c
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);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800144 if (fp == NULL && path != COMMAND_FILE) LOGE("Can't open %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145 return fp;
146}
147
148// close a file, log an error if the error indicator is set
149static void
150check_and_fclose(FILE *fp, const char *name) {
151 fflush(fp);
152 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
153 fclose(fp);
154}
155
156// command line args come from, in decreasing precedence:
157// - the actual command line
158// - the bootloader control block (one per line, after "recovery")
159// - the contents of COMMAND_FILE (one per line)
160static void
161get_args(int *argc, char ***argv) {
162 struct bootloader_message boot;
163 memset(&boot, 0, sizeof(boot));
Koushik Duttaddc12412011-11-23 14:06:12 -0800164 if (device_flash_type() == MTD || device_flash_type() == MMC) {
Koushik Dutta9765a032010-12-18 21:31:02 -0800165 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
166 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167
168 if (boot.command[0] != 0 && boot.command[0] != 255) {
169 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
170 }
171
172 if (boot.status[0] != 0 && boot.status[0] != 255) {
173 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
174 }
175
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700176 struct stat file_info;
177
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800178 // --- if arguments weren't supplied, look in the bootloader control block
Koushik Dutta1741dcd2010-06-11 00:49:49 -0700179 if (*argc <= 1 && 0 != stat("/tmp/.ignorebootmessage", &file_info)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800180 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
181 const char *arg = strtok(boot.recovery, "\n");
182 if (arg != NULL && !strcmp(arg, "recovery")) {
183 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
184 (*argv)[0] = strdup(arg);
185 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
186 if ((arg = strtok(NULL, "\n")) == NULL) break;
187 (*argv)[*argc] = strdup(arg);
188 }
189 LOGI("Got arguments from boot message\n");
190 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
191 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
192 }
193 }
194
195 // --- if that doesn't work, try the command file
196 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700197 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198 if (fp != NULL) {
199 char *argv0 = (*argv)[0];
200 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
201 (*argv)[0] = argv0; // use the same program name
202
203 char buf[MAX_ARG_LENGTH];
204 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
205 if (!fgets(buf, sizeof(buf), fp)) break;
206 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
207 }
208
209 check_and_fclose(fp, COMMAND_FILE);
210 LOGI("Got arguments from %s\n", COMMAND_FILE);
211 }
212 }
213
214 // --> write the arguments we have back into the bootloader control block
215 // always boot into recovery after this (until finish_recovery() is called)
216 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
217 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
218 int i;
219 for (i = 1; i < *argc; ++i) {
220 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
221 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
222 }
Koushik Dutta9765a032010-12-18 21:31:02 -0800223 if (device_flash_type() == MTD) {
224 set_bootloader_message(&boot);
225 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800226}
227
Koushik K. Duttae9234872010-02-12 00:43:24 -0800228void
Oscar Montemayor05231562009-11-30 08:40:57 -0800229set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700230 struct bootloader_message boot;
231 memset(&boot, 0, sizeof(boot));
232 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
233 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
234 set_bootloader_message(&boot);
235}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236
Doug Zongker2c3539e2010-09-29 13:21:30 -0700237// How much of the temp log we have copied to the copy in cache.
238static long tmplog_offset = 0;
239
240static void
241copy_log_file(const char* destination, int append) {
242 FILE *log = fopen_path(destination, append ? "a" : "w");
243 if (log == NULL) {
244 LOGE("Can't open %s\n", destination);
245 } else {
246 FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
247 if (tmplog == NULL) {
248 LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
249 } else {
250 if (append) {
251 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
252 }
253 char buf[4096];
254 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
255 if (append) {
256 tmplog_offset = ftell(tmplog);
257 }
258 check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
259 }
260 check_and_fclose(log, destination);
261 }
262}
263
264
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265// clear the recovery command and prepare to boot a (hopefully working) system,
266// copy our log file to cache as well (for the system to read), and
267// record any intent we were asked to communicate back to the system.
268// this function is idempotent: call it as many times as you like.
269static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800270finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 // By this point, we're ready to return to the main system...
272 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700273 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000274 if (fp == NULL) {
275 LOGE("Can't open %s\n", INTENT_FILE);
276 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 fputs(send_intent, fp);
278 check_and_fclose(fp, INTENT_FILE);
279 }
280 }
281
282 // Copy logs to cache so the system can find out what happened.
Doug Zongker2c3539e2010-09-29 13:21:30 -0700283 copy_log_file(LOG_FILE, true);
284 copy_log_file(LAST_LOG_FILE, false);
285 chmod(LAST_LOG_FILE, 0640);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800286
Koushik Dutta9765a032010-12-18 21:31:02 -0800287 if (device_flash_type() == MTD) {
288 // Reset to mormal system boot so recovery won't cycle indefinitely.
289 struct bootloader_message boot;
290 memset(&boot, 0, sizeof(boot));
291 set_bootloader_message(&boot);
292 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293
294 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700295 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
296 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297 LOGW("Can't unlink %s\n", COMMAND_FILE);
298 }
299
300 sync(); // For good measure.
301}
302
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700304erase_volume(const char *volume) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305 ui_set_background(BACKGROUND_ICON_INSTALLING);
306 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700307 ui_print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700308
309 if (strcmp(volume, "/cache") == 0) {
310 // Any part of the log we'd copied to cache is now gone.
311 // Reset the pointer so we copy from the beginning of the temp
312 // log.
313 tmplog_offset = 0;
314 }
315
Doug Zongkerd4208f92010-09-20 12:16:13 -0700316 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317}
318
Doug Zongker23ceeea2010-07-08 17:27:55 -0700319static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700320copy_sideloaded_package(const char* original_path) {
321 if (ensure_path_mounted(original_path) != 0) {
322 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700323 return NULL;
324 }
325
Doug Zongkerd4208f92010-09-20 12:16:13 -0700326 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700327 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
328 return NULL;
329 }
330
Doug Zongkerd4208f92010-09-20 12:16:13 -0700331 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700332 if (errno != EEXIST) {
333 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
334 return NULL;
335 }
336 }
337
Doug Zongkerd4208f92010-09-20 12:16:13 -0700338 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
339 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700340 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700341 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
342 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700343 return NULL;
344 }
345 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700346 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700347 return NULL;
348 }
349 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700350 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700351 return NULL;
352 }
353 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700354 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700355 return NULL;
356 }
357
Doug Zongkerd4208f92010-09-20 12:16:13 -0700358 char copy_path[PATH_MAX];
359 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700360 strcat(copy_path, "/package.zip");
361
362 char* buffer = malloc(BUFSIZ);
363 if (buffer == NULL) {
364 LOGE("Failed to allocate buffer\n");
365 return NULL;
366 }
367
368 size_t read;
369 FILE* fin = fopen(original_path, "rb");
370 if (fin == NULL) {
371 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
372 return NULL;
373 }
374 FILE* fout = fopen(copy_path, "wb");
375 if (fout == NULL) {
376 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
377 return NULL;
378 }
379
380 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
381 if (fwrite(buffer, 1, read, fout) != read) {
382 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
383 return NULL;
384 }
385 }
386
387 free(buffer);
388
389 if (fclose(fout) != 0) {
390 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
391 return NULL;
392 }
393
394 if (fclose(fin) != 0) {
395 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
396 return NULL;
397 }
398
399 // "adb push" is happy to overwrite read-only files when it's
400 // running as root, but we'll try anyway.
401 if (chmod(copy_path, 0400) != 0) {
402 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
403 return NULL;
404 }
405
Doug Zongkerd4208f92010-09-20 12:16:13 -0700406 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700407}
408
Doug Zongkerf93d8162009-09-22 15:16:02 -0700409static char**
410prepend_title(char** headers) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700411 char* title[] = { EXPAND(RECOVERY_VERSION),
Doug Zongkerd6837852009-06-17 22:07:13 -0700412 "",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800413 NULL };
414
Doug Zongkerd6837852009-06-17 22:07:13 -0700415 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700416 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700417 int count = 0;
418 char** p;
419 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700420 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700421
Doug Zongkerf93d8162009-09-22 15:16:02 -0700422 char** new_headers = malloc((count+1) * sizeof(char*));
423 char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700424 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700425 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700426 *h = NULL;
427
Doug Zongkerf93d8162009-09-22 15:16:02 -0700428 return new_headers;
429}
430
Koushik K. Duttae9234872010-02-12 00:43:24 -0800431int
Doug Zongker8674a722010-09-15 11:08:23 -0700432get_menu_selection(char** headers, char** items, int menu_only,
433 int initial_selection) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700434 // throw away keys pressed previously, so user doesn't
435 // accidentally trigger menu items.
436 ui_clear_key_queue();
CEnnis914f781762012-01-14 23:51:58 -0500437
438 ++ui_menu_level;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800439 int item_count = ui_start_menu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700440 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800441 int chosen_item = -1;
442
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700443 // Some users with dead enter keys need a way to turn on power to select.
444 // Jiggering across the wrapping menu is one "secret" way to enable it.
445 // We can't rely on /cache or /sdcard since they may not be available.
446 int wrap_count = 0;
447
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800448 while (chosen_item < 0 && chosen_item != GO_BACK) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800449 int key = ui_wait_key();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800450 int visible = ui_text_visible();
451
Doug Zongker5cae4452011-01-25 13:15:30 -0800452 if (key == -1) { // ui_wait_key() timed out
453 if (ui_text_ever_visible()) {
454 continue;
455 } else {
456 LOGI("timed out waiting for key input; rebooting.\n");
457 ui_end_menu();
458 return ITEM_REBOOT;
459 }
460 }
461
Doug Zongkerddd6a282009-06-09 12:22:33 -0700462 int action = device_handle_key(key, visible);
463
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700464 int old_selected = selected;
465
Doug Zongkerddd6a282009-06-09 12:22:33 -0700466 if (action < 0) {
467 switch (action) {
468 case HIGHLIGHT_UP:
469 --selected;
470 selected = ui_menu_select(selected);
471 break;
472 case HIGHLIGHT_DOWN:
473 ++selected;
474 selected = ui_menu_select(selected);
475 break;
476 case SELECT_ITEM:
477 chosen_item = selected;
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700478 if (ui_get_showing_back_button()) {
CEnnis914f781762012-01-14 23:51:58 -0500479 if (chosen_item == item_count-1) {
480 --ui_menu_level;
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700481 chosen_item = GO_BACK;
482 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700483 }
Doug Zongkerddd6a282009-06-09 12:22:33 -0700484 break;
485 case NO_ACTION:
486 break;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800487 case GO_BACK:
CEnnis914f781762012-01-14 23:51:58 -0500488 --ui_menu_level;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800489 chosen_item = GO_BACK;
490 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800491 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700492 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700493 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494 }
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700495
496 if (abs(selected - old_selected) > 1) {
497 wrap_count++;
498 if (wrap_count == 3) {
499 wrap_count = 0;
500 if (ui_get_showing_back_button()) {
Koushik Dutta95654242010-07-14 21:01:21 -0700501 ui_print("Back menu button disabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700502 ui_set_showing_back_button(0);
503 }
504 else {
Koushik Dutta95654242010-07-14 21:01:21 -0700505 ui_print("Back menu button enabled.\n");
Koushik Dutta4ca9b4c2010-07-14 18:37:33 -0700506 ui_set_showing_back_button(1);
507 }
508 }
509 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700510 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800511
Doug Zongkerf93d8162009-09-22 15:16:02 -0700512 ui_end_menu();
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800513 ui_clear_key_queue();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700514 return chosen_item;
515}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800516
Doug Zongker8674a722010-09-15 11:08:23 -0700517static int compare_string(const void* a, const void* b) {
518 return strcmp(*(const char**)a, *(const char**)b);
519}
520
521static int
Michael Ward9d2629c2011-06-23 22:14:24 -0700522update_directory(const char* path, const char* unmount_when_done) {
523 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700524
Doug Zongker8674a722010-09-15 11:08:23 -0700525 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700526 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700527 "",
528 NULL };
529 DIR* d;
530 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700531 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700532 if (d == NULL) {
533 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700534 if (unmount_when_done != NULL) {
535 ensure_path_unmounted(unmount_when_done);
536 }
Doug Zongker8674a722010-09-15 11:08:23 -0700537 return 0;
538 }
539
540 char** headers = prepend_title(MENU_HEADERS);
541
542 int d_size = 0;
543 int d_alloc = 10;
544 char** dirs = malloc(d_alloc * sizeof(char*));
545 int z_size = 1;
546 int z_alloc = 10;
547 char** zips = malloc(z_alloc * sizeof(char*));
548 zips[0] = strdup("../");
549
550 while ((de = readdir(d)) != NULL) {
551 int name_len = strlen(de->d_name);
552
553 if (de->d_type == DT_DIR) {
554 // skip "." and ".." entries
555 if (name_len == 1 && de->d_name[0] == '.') continue;
556 if (name_len == 2 && de->d_name[0] == '.' &&
557 de->d_name[1] == '.') continue;
558
559 if (d_size >= d_alloc) {
560 d_alloc *= 2;
561 dirs = realloc(dirs, d_alloc * sizeof(char*));
562 }
563 dirs[d_size] = malloc(name_len + 2);
564 strcpy(dirs[d_size], de->d_name);
565 dirs[d_size][name_len] = '/';
566 dirs[d_size][name_len+1] = '\0';
567 ++d_size;
568 } else if (de->d_type == DT_REG &&
569 name_len >= 4 &&
570 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
571 if (z_size >= z_alloc) {
572 z_alloc *= 2;
573 zips = realloc(zips, z_alloc * sizeof(char*));
574 }
575 zips[z_size++] = strdup(de->d_name);
576 }
577 }
578 closedir(d);
579
580 qsort(dirs, d_size, sizeof(char*), compare_string);
581 qsort(zips, z_size, sizeof(char*), compare_string);
582
583 // append dirs to the zips list
584 if (d_size + z_size + 1 > z_alloc) {
585 z_alloc = d_size + z_size + 1;
586 zips = realloc(zips, z_alloc * sizeof(char*));
587 }
588 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
589 free(dirs);
590 z_size += d_size;
591 zips[z_size] = NULL;
592
593 int result;
594 int chosen_item = 0;
595 do {
596 chosen_item = get_menu_selection(headers, zips, 1, chosen_item);
597
598 char* item = zips[chosen_item];
599 int item_len = strlen(item);
600 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700601 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700602 result = -1;
603 break;
604 } else if (item[item_len-1] == '/') {
605 // recurse down into a subdirectory
606 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700607 strlcpy(new_path, path, PATH_MAX);
608 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700609 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700610 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Michael Ward9d2629c2011-06-23 22:14:24 -0700611 result = update_directory(new_path, unmount_when_done);
Doug Zongker8674a722010-09-15 11:08:23 -0700612 if (result >= 0) break;
613 } else {
614 // selected a zip file: attempt to install it, and return
615 // the status to the caller.
616 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700617 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700618 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700619 strlcat(new_path, item, PATH_MAX);
620
Doug Zongkerd4208f92010-09-20 12:16:13 -0700621 ui_print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700622 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700623 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700624 if (unmount_when_done != NULL) {
625 ensure_path_unmounted(unmount_when_done);
626 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700627 if (copy) {
628 result = install_package(copy);
629 free(copy);
630 } else {
631 result = INSTALL_ERROR;
632 }
Doug Zongker8674a722010-09-15 11:08:23 -0700633 break;
634 }
635 } while (true);
636
637 int i;
638 for (i = 0; i < z_size; ++i) free(zips[i]);
639 free(zips);
640 free(headers);
641
Michael Ward9d2629c2011-06-23 22:14:24 -0700642 if (unmount_when_done != NULL) {
643 ensure_path_unmounted(unmount_when_done);
644 }
Doug Zongker8674a722010-09-15 11:08:23 -0700645 return result;
646}
647
Doug Zongkerf93d8162009-09-22 15:16:02 -0700648static void
649wipe_data(int confirm) {
650 if (confirm) {
651 static char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700652
Doug Zongkerf93d8162009-09-22 15:16:02 -0700653 if (title_headers == NULL) {
654 char* headers[] = { "Confirm wipe of all user data?",
655 " THIS CAN NOT BE UNDONE.",
656 "",
657 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700658 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700659 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800660
Doug Zongkerf93d8162009-09-22 15:16:02 -0700661 char* items[] = { " No",
662 " No",
663 " No",
664 " No",
665 " No",
666 " No",
667 " No",
668 " Yes -- delete all user data", // [7]
669 " No",
670 " No",
671 " No",
672 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800673
Doug Zongker8674a722010-09-15 11:08:23 -0700674 int chosen_item = get_menu_selection(title_headers, items, 1, 0);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700675 if (chosen_item != 7) {
676 return;
677 }
678 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700679
Doug Zongkerf93d8162009-09-22 15:16:02 -0700680 ui_print("\n-- Wiping data...\n");
681 device_wipe_data();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700682 erase_volume("/data");
683 erase_volume("/cache");
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800684 if (has_datadata()) {
685 erase_volume("/datadata");
686 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800687 erase_volume("/sd-ext");
688 erase_volume("/sdcard/.android_secure");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700689 ui_print("Data wipe complete.\n");
690}
691
692static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800693prompt_and_wait() {
Doug Zongker8674a722010-09-15 11:08:23 -0700694 char** headers = prepend_title((const char**)MENU_HEADERS);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700695
696 for (;;) {
697 finish_recovery(NULL);
698 ui_reset_progress();
CEnnis914f781762012-01-14 23:51:58 -0500699
700 ui_menu_level = -1;
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800701 allow_display_toggle = 1;
Doug Zongker8674a722010-09-15 11:08:23 -0700702 int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800703 allow_display_toggle = 0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700704
705 // device-specific code may take some action here. It may
706 // return one of the core actions handled in the switch
707 // statement below.
708 chosen_item = device_perform_action(chosen_item);
709
Michael Ward9d2629c2011-06-23 22:14:24 -0700710 int status;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700711 switch (chosen_item) {
712 case ITEM_REBOOT:
KalimochoAzd646a6f2011-01-10 06:14:21 +0100713 poweroff=0;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700714 return;
715
716 case ITEM_WIPE_DATA:
717 wipe_data(ui_text_visible());
718 if (!ui_text_visible()) return;
719 break;
720
721 case ITEM_WIPE_CACHE:
Koushik Dutta95654242010-07-14 21:01:21 -0700722 if (confirm_selection("Confirm wipe?", "Yes - Wipe Cache"))
723 {
724 ui_print("\n-- Wiping cache...\n");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800725 erase_volume("/cache");
Koushik Dutta95654242010-07-14 21:01:21 -0700726 ui_print("Cache wipe complete.\n");
727 if (!ui_text_visible()) return;
728 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700729 break;
730
731 case ITEM_APPLY_SDCARD:
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800732 show_install_update_menu();
733 break;
Michael Ward9d2629c2011-06-23 22:14:24 -0700734
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700735 case ITEM_NANDROID:
736 show_nandroid_menu();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800737 break;
Koushik Dutta0df56f42011-11-16 16:00:35 -0800738
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700739 case ITEM_PARTITION:
740 show_partition_menu();
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700741 break;
Koushik Dutta0df56f42011-11-16 16:00:35 -0800742
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700743 case ITEM_ADVANCED:
744 show_advanced_menu();
Koushik K. Dutta1fa52ec2010-02-21 21:29:10 -0800745 break;
Koushik Dutta0df56f42011-11-16 16:00:35 -0800746
KalimochoAzd646a6f2011-01-10 06:14:21 +0100747 case ITEM_POWEROFF:
Koushik Dutta0df56f42011-11-16 16:00:35 -0800748 poweroff = 1;
KalimochoAzd646a6f2011-01-10 06:14:21 +0100749 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800750 }
751 }
752}
753
754static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800755print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700756 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800757}
758
759int
Oscar Montemayor05231562009-11-30 08:40:57 -0800760main(int argc, char **argv) {
Koushik Dutta4fcd5232011-10-28 11:47:02 -0700761 if (strcmp(basename(argv[0]), "recovery") != 0)
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800762 {
763 if (strstr(argv[0], "flash_image") != NULL)
764 return flash_image_main(argc, argv);
Koushik Duttad8e21c32011-01-04 10:46:23 -0800765 if (strstr(argv[0], "volume") != NULL)
766 return volume_main(argc, argv);
Koushik Duttabec09952010-12-19 20:37:57 -0800767 if (strstr(argv[0], "edify") != NULL)
768 return edify_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800769 if (strstr(argv[0], "dump_image") != NULL)
770 return dump_image_main(argc, argv);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700771 if (strstr(argv[0], "erase_image") != NULL)
772 return erase_image_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800773 if (strstr(argv[0], "mkyaffs2image") != NULL)
774 return mkyaffs2image_main(argc, argv);
775 if (strstr(argv[0], "unyaffs") != NULL)
776 return unyaffs_main(argc, argv);
Koushik Duttad3cc60b2010-07-03 13:56:45 -0700777 if (strstr(argv[0], "nandroid"))
778 return nandroid_main(argc, argv);
Koushik Dutta852bb422010-07-24 11:18:00 -0700779 if (strstr(argv[0], "reboot"))
780 return reboot_main(argc, argv);
Kolja Dummann6afbcdc2011-07-24 21:40:16 +0200781#ifdef BOARD_RECOVERY_HANDLES_MOUNT
782 if (strstr(argv[0], "mount") && argc == 2 && !strstr(argv[0], "umount"))
783 {
784 load_volume_table();
785 return ensure_path_mounted(argv[1]);
786 }
787#endif
KalimochoAzd646a6f2011-01-10 06:14:21 +0100788 if (strstr(argv[0], "poweroff")){
789 return reboot_main(argc, argv);
790 }
Koushik Dutta852bb422010-07-24 11:18:00 -0700791 if (strstr(argv[0], "setprop"))
792 return setprop_main(argc, argv);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -0800793 return busybox_driver(argc, argv);
794 }
Koushik Duttaf8b21c22010-06-14 12:49:47 -0700795 __system("/sbin/postrecoveryboot.sh");
Koushik Dutta4d8c0332011-06-09 14:19:04 -0700796
Koushik K. Dutta03173782010-02-26 14:14:23 -0800797 int is_user_initiated_recovery = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800798 time_t start = time(NULL);
799
800 // If these fail, there's not really anywhere to complain...
801 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
802 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700803 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800804
Doug Zongker6809c512011-03-01 14:04:34 -0800805 device_ui_init(&ui_parameters);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800806 ui_init();
Koushik Duttafdda0d62010-07-01 12:52:34 -0700807 ui_print(EXPAND(RECOVERY_VERSION)"\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700808 load_volume_table();
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800809 process_volumes();
Koushik Dutta9765a032010-12-18 21:31:02 -0800810 LOGI("Processing arguments.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800811 get_args(&argc, &argv);
812
813 int previous_runs = 0;
814 const char *send_intent = NULL;
815 const char *update_package = NULL;
816 int wipe_data = 0, wipe_cache = 0;
817
Koushik Dutta9765a032010-12-18 21:31:02 -0800818 LOGI("Checking arguments.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800819 int arg;
820 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
821 switch (arg) {
822 case 'p': previous_runs = atoi(optarg); break;
823 case 's': send_intent = optarg; break;
824 case 'u': update_package = optarg; break;
Koushik Dutta94a4bab2011-07-13 10:34:23 -0700825 case 'w':
Ricardo Cerqueira0b06fc02011-07-07 01:09:52 +0100826#ifndef BOARD_RECOVERY_ALWAYS_WIPES
827 wipe_data = wipe_cache = 1;
828#endif
829 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800830 case 'c': wipe_cache = 1; break;
Doug Zongker4bc98062010-09-03 11:00:13 -0700831 case 't': ui_show_text(1); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800832 case '?':
833 LOGE("Invalid command argument\n");
834 continue;
835 }
836 }
837
Koushik Dutta9765a032010-12-18 21:31:02 -0800838 LOGI("device_recovery_start()\n");
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800839 device_recovery_start();
840
Doug Zongker56c51052010-07-01 09:18:44 -0700841 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800842 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700843 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800844 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700845 printf("\n");
846
847 if (update_package) {
848 // For backwards compatibility on the cache partition only, if
849 // we're given an old 'root' path "CACHE:foo", change it to
850 // "/cache/foo".
851 if (strncmp(update_package, "CACHE:", 6) == 0) {
852 int len = strlen(update_package) + 10;
853 char* modified_path = malloc(len);
854 strlcpy(modified_path, "/cache/", len);
855 strlcat(modified_path, update_package+6, len);
856 printf("(replacing path \"%s\" with \"%s\")\n",
857 update_package, modified_path);
858 update_package = modified_path;
859 }
860 }
861 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800862
863 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700864 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800865
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800866 int status = INSTALL_SUCCESS;
Oscar Montemayor05231562009-11-30 08:40:57 -0800867
Doug Zongker540d57f2011-01-18 13:36:58 -0800868 if (update_package != NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800869 status = install_package(update_package);
870 if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700871 } else if (wipe_data) {
872 if (device_wipe_data()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700873 if (erase_volume("/data")) status = INSTALL_ERROR;
Pawit Pornkitprasan16598142011-11-26 22:43:57 +0700874 if (has_datadata() && erase_volume("/datadata")) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700875 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800876 if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700877 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700878 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongkerb128f542009-06-18 15:07:14 -0700879 if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800880 } else {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700881 LOGI("Checking for extendedcommand...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800882 status = INSTALL_ERROR; // No command specified
Koushik K. Dutta03173782010-02-26 14:14:23 -0800883 // we are starting up in user initiated recovery here
884 // let's set up some default options
885 signature_check_enabled = 0;
Koushik K. Dutta2bda3e92010-03-06 16:40:52 -0800886 script_assert_enabled = 0;
Koushik K. Dutta03173782010-02-26 14:14:23 -0800887 is_user_initiated_recovery = 1;
888 ui_set_show_text(1);
Koushik Dutta5d808172010-12-18 22:29:27 -0800889 ui_set_background(BACKGROUND_ICON_CLOCKWORK);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800890
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800891 if (extendedcommand_file_exists()) {
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700892 LOGI("Running extendedcommand...\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700893 int ret;
894 if (0 == (ret = run_and_remove_extendedcommand())) {
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800895 status = INSTALL_SUCCESS;
Koushik K. Dutta13d8fcc2010-03-07 14:15:14 -0800896 ui_set_show_text(0);
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800897 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700898 else {
899 handle_failure(ret);
900 }
Koushik Duttaf4e3a672010-06-09 12:19:41 -0700901 } else {
902 LOGI("Skipping execution of extendedcommand, file not found...\n");
Koushik K. Dutta32e41112010-03-07 14:11:56 -0800903 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800904 }
905
Danesh Mondegarian56170562011-10-11 16:52:23 -0400906 if (status != INSTALL_SUCCESS && !is_user_initiated_recovery) {
907 ui_set_show_text(1);
908 ui_set_background(BACKGROUND_ICON_ERROR);
909 }
Doug Zongker8674a722010-09-15 11:08:23 -0700910 if (status != INSTALL_SUCCESS || ui_text_visible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700911 prompt_and_wait();
912 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800913
Koushik Dutta7f13e152011-09-08 16:55:35 -0700914 // If there is a radio image pending, reboot now to install it.
915 maybe_install_firmware_update(send_intent);
916
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800917 // Otherwise, get ready to boot the main system...
918 finish_recovery(send_intent);
Koushik Dutta0df56f42011-11-16 16:00:35 -0800919
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800920 sync();
Koushik Dutta0df56f42011-11-16 16:00:35 -0800921 if(!poweroff) {
922 ui_print("Rebooting...\n");
923 android_reboot(ANDROID_RB_RESTART, 0, 0);
924 }
925 else {
926 ui_print("Shutting down...\n");
927 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
928 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800929 return EXIT_SUCCESS;
930}
Koushik K. Duttaa3c2f732010-02-19 14:17:22 -0800931
932int get_allow_toggle_display() {
933 return allow_display_toggle;
Chris Soyarsa1749d92010-02-26 02:45:55 -0500934}