The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 30 | #include <dirent.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | |
| 32 | #include "bootloader.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | #include "common.h" |
| 34 | #include "cutils/properties.h" |
Ken Sumrall | 6e4472a | 2011-03-07 23:37:27 -0800 | [diff] [blame] | 35 | #include "cutils/android_reboot.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 36 | #include "install.h" |
| 37 | #include "minui/minui.h" |
| 38 | #include "minzip/DirUtil.h" |
| 39 | #include "roots.h" |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 40 | #include "ui.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 41 | #include "screen_ui.h" |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 42 | #include "device.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 43 | #include "adb_install.h" |
| 44 | extern "C" { |
| 45 | #include "minadbd/adb.h" |
| 46 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 47 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 48 | struct selabel_handle *sehandle; |
| 49 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 50 | static const struct option OPTIONS[] = { |
| 51 | { "send_intent", required_argument, NULL, 's' }, |
| 52 | { "update_package", required_argument, NULL, 'u' }, |
| 53 | { "wipe_data", no_argument, NULL, 'w' }, |
| 54 | { "wipe_cache", no_argument, NULL, 'c' }, |
Doug Zongker | 4bc9806 | 2010-09-03 11:00:13 -0700 | [diff] [blame] | 55 | { "show_text", no_argument, NULL, 't' }, |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 56 | { "just_exit", no_argument, NULL, 'x' }, |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 57 | { "locale", required_argument, NULL, 'l' }, |
Doug Zongker | 988500b | 2009-10-06 14:41:38 -0700 | [diff] [blame] | 58 | { NULL, 0, NULL, 0 }, |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 61 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 62 | static const char *INTENT_FILE = "/cache/recovery/intent"; |
| 63 | static const char *LOG_FILE = "/cache/recovery/log"; |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 64 | static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 65 | static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
Doug Zongker | 8b240cc | 2012-08-29 15:19:29 -0700 | [diff] [blame] | 66 | static const char *LOCALE_FILE = "/cache/recovery/last_locale"; |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 67 | static const char *CACHE_ROOT = "/cache"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 68 | static const char *SDCARD_ROOT = "/sdcard"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 70 | static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 71 | static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 72 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 73 | RecoveryUI* ui = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 74 | char* locale = NULL; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 75 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | /* |
| 77 | * The recovery tool communicates with the main system through /cache files. |
| 78 | * /cache/recovery/command - INPUT - command line for tool, one arg per line |
| 79 | * /cache/recovery/log - OUTPUT - combined log file from recovery run(s) |
| 80 | * /cache/recovery/intent - OUTPUT - intent that was passed in |
| 81 | * |
| 82 | * The arguments which may be supplied in the recovery.command file: |
| 83 | * --send_intent=anystring - write the text out to recovery.intent |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 84 | * --update_package=path - verify install an OTA package file |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 85 | * --wipe_data - erase user data (and cache), then reboot |
| 86 | * --wipe_cache - wipe cache (but not user data), then reboot |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 87 | * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 88 | * --just_exit - do nothing; exit and reboot |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 89 | * |
| 90 | * After completing, we remove /cache/recovery/command and reboot. |
| 91 | * Arguments may also be supplied in the bootloader control block (BCB). |
| 92 | * These important scenarios must be safely restartable at any point: |
| 93 | * |
| 94 | * FACTORY RESET |
| 95 | * 1. user selects "factory reset" |
| 96 | * 2. main system writes "--wipe_data" to /cache/recovery/command |
| 97 | * 3. main system reboots into recovery |
| 98 | * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data" |
| 99 | * -- after this, rebooting will restart the erase -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 100 | * 5. erase_volume() reformats /data |
| 101 | * 6. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 102 | * 7. finish_recovery() erases BCB |
| 103 | * -- after this, rebooting will restart the main system -- |
| 104 | * 8. main() calls reboot() to boot main system |
| 105 | * |
| 106 | * OTA INSTALL |
| 107 | * 1. main system downloads OTA package to /cache/some-filename.zip |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 108 | * 2. main system writes "--update_package=/cache/some-filename.zip" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 109 | * 3. main system reboots into recovery |
| 110 | * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..." |
| 111 | * -- after this, rebooting will attempt to reinstall the update -- |
| 112 | * 5. install_package() attempts to install the update |
| 113 | * NOTE: the package install must itself be restartable from any point |
| 114 | * 6. finish_recovery() erases BCB |
| 115 | * -- after this, rebooting will (try to) restart the main system -- |
| 116 | * 7. ** if install failed ** |
| 117 | * 7a. prompt_and_wait() shows an error icon and waits for the user |
| 118 | * 7b; the user reboots (pulling the battery, etc) into the main system |
| 119 | * 8. main() calls maybe_install_firmware_update() |
| 120 | * ** if the update contained radio/hboot firmware **: |
| 121 | * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache" |
| 122 | * -- after this, rebooting will reformat cache & restart main system -- |
| 123 | * 8b. m_i_f_u() writes firmware image into raw cache partition |
| 124 | * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache" |
| 125 | * -- after this, rebooting will attempt to reinstall firmware -- |
| 126 | * 8d. bootloader tries to flash firmware |
| 127 | * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache") |
| 128 | * -- after this, rebooting will reformat cache & restart main system -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 129 | * 8f. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | * 8g. finish_recovery() erases BCB |
| 131 | * -- after this, rebooting will (try to) restart the main system -- |
| 132 | * 9. main() calls reboot() to boot main system |
| 133 | */ |
| 134 | |
| 135 | static const int MAX_ARG_LENGTH = 4096; |
| 136 | static const int MAX_ARGS = 100; |
| 137 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 138 | // open a given path, mounting partitions as necessary |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 139 | FILE* |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 140 | fopen_path(const char *path, const char *mode) { |
| 141 | if (ensure_path_mounted(path) != 0) { |
| 142 | LOGE("Can't mount %s\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | // When writing, try to create the containing directory, if necessary. |
| 147 | // Use generous permissions, the system (init.rc) will reset them. |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 148 | if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 149 | |
| 150 | FILE *fp = fopen(path, mode); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 151 | return fp; |
| 152 | } |
| 153 | |
| 154 | // close a file, log an error if the error indicator is set |
| 155 | static void |
| 156 | check_and_fclose(FILE *fp, const char *name) { |
| 157 | fflush(fp); |
| 158 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); |
| 159 | fclose(fp); |
| 160 | } |
| 161 | |
| 162 | // command line args come from, in decreasing precedence: |
| 163 | // - the actual command line |
| 164 | // - the bootloader control block (one per line, after "recovery") |
| 165 | // - the contents of COMMAND_FILE (one per line) |
| 166 | static void |
| 167 | get_args(int *argc, char ***argv) { |
| 168 | struct bootloader_message boot; |
| 169 | memset(&boot, 0, sizeof(boot)); |
| 170 | get_bootloader_message(&boot); // this may fail, leaving a zeroed structure |
| 171 | |
| 172 | if (boot.command[0] != 0 && boot.command[0] != 255) { |
| 173 | LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command); |
| 174 | } |
| 175 | |
| 176 | if (boot.status[0] != 0 && boot.status[0] != 255) { |
| 177 | LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status); |
| 178 | } |
| 179 | |
| 180 | // --- if arguments weren't supplied, look in the bootloader control block |
| 181 | if (*argc <= 1) { |
| 182 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 183 | const char *arg = strtok(boot.recovery, "\n"); |
| 184 | if (arg != NULL && !strcmp(arg, "recovery")) { |
| 185 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 186 | (*argv)[0] = strdup(arg); |
| 187 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 188 | if ((arg = strtok(NULL, "\n")) == NULL) break; |
| 189 | (*argv)[*argc] = strdup(arg); |
| 190 | } |
| 191 | LOGI("Got arguments from boot message\n"); |
| 192 | } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) { |
| 193 | LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // --- if that doesn't work, try the command file |
| 198 | if (*argc <= 1) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 199 | FILE *fp = fopen_path(COMMAND_FILE, "r"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 200 | if (fp != NULL) { |
| 201 | char *argv0 = (*argv)[0]; |
| 202 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 203 | (*argv)[0] = argv0; // use the same program name |
| 204 | |
| 205 | char buf[MAX_ARG_LENGTH]; |
| 206 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 207 | if (!fgets(buf, sizeof(buf), fp)) break; |
| 208 | (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline. |
| 209 | } |
| 210 | |
| 211 | check_and_fclose(fp, COMMAND_FILE); |
| 212 | LOGI("Got arguments from %s\n", COMMAND_FILE); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // --> write the arguments we have back into the bootloader control block |
| 217 | // always boot into recovery after this (until finish_recovery() is called) |
| 218 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 219 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 220 | int i; |
| 221 | for (i = 1; i < *argc; ++i) { |
| 222 | strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery)); |
| 223 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 224 | } |
| 225 | set_bootloader_message(&boot); |
| 226 | } |
| 227 | |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 228 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 229 | set_sdcard_update_bootloader_message() { |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 230 | 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 236 | |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 237 | // How much of the temp log we have copied to the copy in cache. |
| 238 | static long tmplog_offset = 0; |
| 239 | |
| 240 | static void |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 241 | copy_log_file(const char* source, const char* destination, int append) { |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 242 | FILE *log = fopen_path(destination, append ? "a" : "w"); |
| 243 | if (log == NULL) { |
| 244 | LOGE("Can't open %s\n", destination); |
| 245 | } else { |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 246 | FILE *tmplog = fopen(source, "r"); |
| 247 | if (tmplog != NULL) { |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 248 | if (append) { |
| 249 | fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write |
| 250 | } |
| 251 | char buf[4096]; |
| 252 | while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); |
| 253 | if (append) { |
| 254 | tmplog_offset = ftell(tmplog); |
| 255 | } |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 256 | check_and_fclose(tmplog, source); |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 257 | } |
| 258 | check_and_fclose(log, destination); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 263 | // clear the recovery command and prepare to boot a (hopefully working) system, |
| 264 | // copy our log file to cache as well (for the system to read), and |
| 265 | // record any intent we were asked to communicate back to the system. |
| 266 | // this function is idempotent: call it as many times as you like. |
| 267 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 268 | finish_recovery(const char *send_intent) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 269 | // By this point, we're ready to return to the main system... |
| 270 | if (send_intent != NULL) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 271 | FILE *fp = fopen_path(INTENT_FILE, "w"); |
Jay Freeman (saurik) | 619ec2f | 2008-11-17 01:56:05 +0000 | [diff] [blame] | 272 | if (fp == NULL) { |
| 273 | LOGE("Can't open %s\n", INTENT_FILE); |
| 274 | } else { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 275 | fputs(send_intent, fp); |
| 276 | check_and_fclose(fp, INTENT_FILE); |
| 277 | } |
| 278 | } |
| 279 | |
Doug Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 280 | // Save the locale to cache, so if recovery is next started up |
| 281 | // without a --locale argument (eg, directly from the bootloader) |
| 282 | // it will use the last-known locale. |
| 283 | if (locale != NULL) { |
| 284 | LOGI("Saving locale \"%s\"\n", locale); |
| 285 | FILE* fp = fopen_path(LOCALE_FILE, "w"); |
| 286 | fwrite(locale, 1, strlen(locale), fp); |
| 287 | fflush(fp); |
| 288 | fsync(fileno(fp)); |
| 289 | check_and_fclose(fp, LOCALE_FILE); |
| 290 | } |
| 291 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 292 | // Copy logs to cache so the system can find out what happened. |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 293 | copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); |
| 294 | copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); |
| 295 | copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); |
| 296 | chmod(LOG_FILE, 0600); |
| 297 | chown(LOG_FILE, 1000, 1000); // system user |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 298 | chmod(LAST_LOG_FILE, 0640); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 299 | chmod(LAST_INSTALL_FILE, 0644); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 300 | |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 301 | // Reset to normal system boot so recovery won't cycle indefinitely. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 302 | struct bootloader_message boot; |
| 303 | memset(&boot, 0, sizeof(boot)); |
| 304 | set_bootloader_message(&boot); |
| 305 | |
| 306 | // Remove the command file, so recovery won't repeat indefinitely. |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 307 | if (ensure_path_mounted(COMMAND_FILE) != 0 || |
| 308 | (unlink(COMMAND_FILE) && errno != ENOENT)) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 309 | LOGW("Can't unlink %s\n", COMMAND_FILE); |
| 310 | } |
| 311 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 312 | ensure_path_unmounted(CACHE_ROOT); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 313 | sync(); // For good measure. |
| 314 | } |
| 315 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 316 | static int |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 317 | erase_volume(const char *volume) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 318 | ui->SetBackground(RecoveryUI::ERASING); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 319 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
| 320 | ui->Print("Formatting %s...\n", volume); |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 321 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 322 | ensure_path_unmounted(volume); |
| 323 | |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 324 | if (strcmp(volume, "/cache") == 0) { |
| 325 | // Any part of the log we'd copied to cache is now gone. |
| 326 | // Reset the pointer so we copy from the beginning of the temp |
| 327 | // log. |
| 328 | tmplog_offset = 0; |
| 329 | } |
| 330 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 331 | return format_volume(volume); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 334 | static char* |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 335 | copy_sideloaded_package(const char* original_path) { |
| 336 | if (ensure_path_mounted(original_path) != 0) { |
| 337 | LOGE("Can't mount %s\n", original_path); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 338 | return NULL; |
| 339 | } |
| 340 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 341 | if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) { |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 342 | LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 346 | if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) { |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 347 | if (errno != EEXIST) { |
| 348 | LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno)); |
| 349 | return NULL; |
| 350 | } |
| 351 | } |
| 352 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 353 | // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a |
| 354 | // directory, owned by root, readable and writable only by root. |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 355 | struct stat st; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 356 | if (stat(SIDELOAD_TEMP_DIR, &st) != 0) { |
| 357 | LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno)); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 358 | return NULL; |
| 359 | } |
| 360 | if (!S_ISDIR(st.st_mode)) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 361 | LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 362 | return NULL; |
| 363 | } |
| 364 | if ((st.st_mode & 0777) != 0700) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 365 | LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | if (st.st_uid != 0) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 369 | LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 370 | return NULL; |
| 371 | } |
| 372 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 373 | char copy_path[PATH_MAX]; |
| 374 | strcpy(copy_path, SIDELOAD_TEMP_DIR); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 375 | strcat(copy_path, "/package.zip"); |
| 376 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 377 | char* buffer = (char*)malloc(BUFSIZ); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 378 | if (buffer == NULL) { |
| 379 | LOGE("Failed to allocate buffer\n"); |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | size_t read; |
| 384 | FILE* fin = fopen(original_path, "rb"); |
| 385 | if (fin == NULL) { |
| 386 | LOGE("Failed to open %s (%s)\n", original_path, strerror(errno)); |
| 387 | return NULL; |
| 388 | } |
| 389 | FILE* fout = fopen(copy_path, "wb"); |
| 390 | if (fout == NULL) { |
| 391 | LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno)); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) { |
| 396 | if (fwrite(buffer, 1, read, fout) != read) { |
| 397 | LOGE("Short write of %s (%s)\n", copy_path, strerror(errno)); |
| 398 | return NULL; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | free(buffer); |
| 403 | |
| 404 | if (fclose(fout) != 0) { |
| 405 | LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno)); |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | if (fclose(fin) != 0) { |
| 410 | LOGE("Failed to close %s (%s)\n", original_path, strerror(errno)); |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | // "adb push" is happy to overwrite read-only files when it's |
| 415 | // running as root, but we'll try anyway. |
| 416 | if (chmod(copy_path, 0400) != 0) { |
| 417 | LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno)); |
| 418 | return NULL; |
| 419 | } |
| 420 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 421 | return strdup(copy_path); |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 424 | static const char** |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 425 | prepend_title(const char* const* headers) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 426 | const char* title[] = { "Android system recovery <" |
| 427 | EXPAND(RECOVERY_API_VERSION) "e>", |
| 428 | "", |
| 429 | NULL }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 430 | |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 431 | // count the number of lines in our title, plus the |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 432 | // caller-provided headers. |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 433 | int count = 0; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 434 | const char* const* p; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 435 | for (p = title; *p; ++p, ++count); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 436 | for (p = headers; *p; ++p, ++count); |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 437 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 438 | const char** new_headers = (const char**)malloc((count+1) * sizeof(char*)); |
| 439 | const char** h = new_headers; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 440 | for (p = title; *p; ++p, ++h) *h = *p; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 441 | for (p = headers; *p; ++p, ++h) *h = *p; |
Doug Zongker | d683785 | 2009-06-17 22:07:13 -0700 | [diff] [blame] | 442 | *h = NULL; |
| 443 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 444 | return new_headers; |
| 445 | } |
| 446 | |
| 447 | static int |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 448 | get_menu_selection(const char* const * headers, const char* const * items, |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 449 | int menu_only, int initial_selection, Device* device) { |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 450 | // throw away keys pressed previously, so user doesn't |
| 451 | // accidentally trigger menu items. |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 452 | ui->FlushKeys(); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 453 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 454 | ui->StartMenu(headers, items, initial_selection); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 455 | int selected = initial_selection; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 456 | int chosen_item = -1; |
| 457 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 458 | while (chosen_item < 0) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 459 | int key = ui->WaitKey(); |
| 460 | int visible = ui->IsTextVisible(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 461 | |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 462 | if (key == -1) { // ui_wait_key() timed out |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 463 | if (ui->WasTextEverVisible()) { |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 464 | continue; |
| 465 | } else { |
| 466 | LOGI("timed out waiting for key input; rebooting.\n"); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 467 | ui->EndMenu(); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 468 | return 0; // XXX fixme |
Doug Zongker | 5cae445 | 2011-01-25 13:15:30 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 472 | int action = device->HandleMenuKey(key, visible); |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 473 | |
| 474 | if (action < 0) { |
| 475 | switch (action) { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 476 | case Device::kHighlightUp: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 477 | --selected; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 478 | selected = ui->SelectMenu(selected); |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 479 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 480 | case Device::kHighlightDown: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 481 | ++selected; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 482 | selected = ui->SelectMenu(selected); |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 483 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 484 | case Device::kInvokeItem: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 485 | chosen_item = selected; |
| 486 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 487 | case Device::kNoAction: |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 488 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 489 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 490 | } else if (!menu_only) { |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 491 | chosen_item = action; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 492 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 493 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 494 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 495 | ui->EndMenu(); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 496 | return chosen_item; |
| 497 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 498 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 499 | static int compare_string(const void* a, const void* b) { |
| 500 | return strcmp(*(const char**)a, *(const char**)b); |
| 501 | } |
| 502 | |
| 503 | static int |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 504 | update_directory(const char* path, const char* unmount_when_done, |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 505 | int* wipe_cache, Device* device) { |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 506 | ensure_path_mounted(path); |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 507 | |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 508 | const char* MENU_HEADERS[] = { "Choose a package to install:", |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 509 | path, |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 510 | "", |
| 511 | NULL }; |
| 512 | DIR* d; |
| 513 | struct dirent* de; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 514 | d = opendir(path); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 515 | if (d == NULL) { |
| 516 | LOGE("error opening %s: %s\n", path, strerror(errno)); |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 517 | if (unmount_when_done != NULL) { |
| 518 | ensure_path_unmounted(unmount_when_done); |
| 519 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 523 | const char** headers = prepend_title(MENU_HEADERS); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 524 | |
| 525 | int d_size = 0; |
| 526 | int d_alloc = 10; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 527 | char** dirs = (char**)malloc(d_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 528 | int z_size = 1; |
| 529 | int z_alloc = 10; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 530 | char** zips = (char**)malloc(z_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 531 | zips[0] = strdup("../"); |
| 532 | |
| 533 | while ((de = readdir(d)) != NULL) { |
| 534 | int name_len = strlen(de->d_name); |
| 535 | |
| 536 | if (de->d_type == DT_DIR) { |
| 537 | // skip "." and ".." entries |
| 538 | if (name_len == 1 && de->d_name[0] == '.') continue; |
| 539 | if (name_len == 2 && de->d_name[0] == '.' && |
| 540 | de->d_name[1] == '.') continue; |
| 541 | |
| 542 | if (d_size >= d_alloc) { |
| 543 | d_alloc *= 2; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 544 | dirs = (char**)realloc(dirs, d_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 545 | } |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 546 | dirs[d_size] = (char*)malloc(name_len + 2); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 547 | strcpy(dirs[d_size], de->d_name); |
| 548 | dirs[d_size][name_len] = '/'; |
| 549 | dirs[d_size][name_len+1] = '\0'; |
| 550 | ++d_size; |
| 551 | } else if (de->d_type == DT_REG && |
| 552 | name_len >= 4 && |
| 553 | strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) { |
| 554 | if (z_size >= z_alloc) { |
| 555 | z_alloc *= 2; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 556 | zips = (char**)realloc(zips, z_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 557 | } |
| 558 | zips[z_size++] = strdup(de->d_name); |
| 559 | } |
| 560 | } |
| 561 | closedir(d); |
| 562 | |
| 563 | qsort(dirs, d_size, sizeof(char*), compare_string); |
| 564 | qsort(zips, z_size, sizeof(char*), compare_string); |
| 565 | |
| 566 | // append dirs to the zips list |
| 567 | if (d_size + z_size + 1 > z_alloc) { |
| 568 | z_alloc = d_size + z_size + 1; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 569 | zips = (char**)realloc(zips, z_alloc * sizeof(char*)); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 570 | } |
| 571 | memcpy(zips + z_size, dirs, d_size * sizeof(char*)); |
| 572 | free(dirs); |
| 573 | z_size += d_size; |
| 574 | zips[z_size] = NULL; |
| 575 | |
| 576 | int result; |
| 577 | int chosen_item = 0; |
| 578 | do { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 579 | chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 580 | |
| 581 | char* item = zips[chosen_item]; |
| 582 | int item_len = strlen(item); |
| 583 | if (chosen_item == 0) { // item 0 is always "../" |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 584 | // go up but continue browsing (if the caller is update_directory) |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 585 | result = -1; |
| 586 | break; |
| 587 | } else if (item[item_len-1] == '/') { |
| 588 | // recurse down into a subdirectory |
| 589 | char new_path[PATH_MAX]; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 590 | strlcpy(new_path, path, PATH_MAX); |
| 591 | strlcat(new_path, "/", PATH_MAX); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 592 | strlcat(new_path, item, PATH_MAX); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 593 | new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/' |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 594 | result = update_directory(new_path, unmount_when_done, wipe_cache, device); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 595 | if (result >= 0) break; |
| 596 | } else { |
| 597 | // selected a zip file: attempt to install it, and return |
| 598 | // the status to the caller. |
| 599 | char new_path[PATH_MAX]; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 600 | strlcpy(new_path, path, PATH_MAX); |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 601 | strlcat(new_path, "/", PATH_MAX); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 602 | strlcat(new_path, item, PATH_MAX); |
| 603 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 604 | ui->Print("\n-- Install %s ...\n", path); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 605 | set_sdcard_update_bootloader_message(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 606 | char* copy = copy_sideloaded_package(new_path); |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 607 | if (unmount_when_done != NULL) { |
| 608 | ensure_path_unmounted(unmount_when_done); |
| 609 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 610 | if (copy) { |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 611 | result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 612 | free(copy); |
| 613 | } else { |
| 614 | result = INSTALL_ERROR; |
| 615 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | } while (true); |
| 619 | |
| 620 | int i; |
| 621 | for (i = 0; i < z_size; ++i) free(zips[i]); |
| 622 | free(zips); |
| 623 | free(headers); |
| 624 | |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 625 | if (unmount_when_done != NULL) { |
| 626 | ensure_path_unmounted(unmount_when_done); |
| 627 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 628 | return result; |
| 629 | } |
| 630 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 631 | static void |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 632 | wipe_data(int confirm, Device* device) { |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 633 | if (confirm) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 634 | static const char** title_headers = NULL; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 635 | |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 636 | if (title_headers == NULL) { |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 637 | const char* headers[] = { "Confirm wipe of all user data?", |
| 638 | " THIS CAN NOT BE UNDONE.", |
| 639 | "", |
| 640 | NULL }; |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 641 | title_headers = prepend_title((const char**)headers); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 642 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 643 | |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 644 | const char* items[] = { " No", |
| 645 | " No", |
| 646 | " No", |
| 647 | " No", |
| 648 | " No", |
| 649 | " No", |
| 650 | " No", |
| 651 | " Yes -- delete all user data", // [7] |
| 652 | " No", |
| 653 | " No", |
| 654 | " No", |
| 655 | NULL }; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 656 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 657 | int chosen_item = get_menu_selection(title_headers, items, 1, 0, device); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 658 | if (chosen_item != 7) { |
| 659 | return; |
| 660 | } |
| 661 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 662 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 663 | ui->Print("\n-- Wiping data...\n"); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 664 | device->WipeData(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 665 | erase_volume("/data"); |
| 666 | erase_volume("/cache"); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 667 | ui->Print("Data wipe complete.\n"); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static void |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 671 | prompt_and_wait(Device* device) { |
| 672 | const char* const* headers = prepend_title(device->GetMenuHeaders()); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 673 | |
| 674 | for (;;) { |
| 675 | finish_recovery(NULL); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 676 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 677 | ui->SetProgressType(RecoveryUI::EMPTY); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 678 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 679 | int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 680 | |
| 681 | // device-specific code may take some action here. It may |
| 682 | // return one of the core actions handled in the switch |
| 683 | // statement below. |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 684 | chosen_item = device->InvokeMenuItem(chosen_item); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 685 | |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 686 | int status; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 687 | int wipe_cache; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 688 | switch (chosen_item) { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 689 | case Device::REBOOT: |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 690 | return; |
| 691 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 692 | case Device::WIPE_DATA: |
| 693 | wipe_data(ui->IsTextVisible(), device); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 694 | if (!ui->IsTextVisible()) return; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 695 | break; |
| 696 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 697 | case Device::WIPE_CACHE: |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 698 | ui->ShowText(false); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 699 | ui->Print("\n-- Wiping cache...\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 700 | erase_volume("/cache"); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 701 | ui->Print("Cache wipe complete.\n"); |
| 702 | if (!ui->IsTextVisible()) return; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 703 | break; |
| 704 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 705 | case Device::APPLY_EXT: |
Doug Zongker | d9428e3 | 2011-12-13 16:03:28 -0800 | [diff] [blame] | 706 | // Some packages expect /cache to be mounted (eg, |
| 707 | // standard incremental packages expect to use /cache |
| 708 | // as scratch space). |
| 709 | ensure_path_mounted(CACHE_ROOT); |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 710 | status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 711 | if (status == INSTALL_SUCCESS && wipe_cache) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 712 | ui->Print("\n-- Wiping cache (at package request)...\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 713 | if (erase_volume("/cache")) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 714 | ui->Print("Cache wipe failed.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 715 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 716 | ui->Print("Cache wipe complete.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 717 | } |
| 718 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 719 | if (status >= 0) { |
| 720 | if (status != INSTALL_SUCCESS) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 721 | ui->SetBackground(RecoveryUI::ERROR); |
| 722 | ui->Print("Installation aborted.\n"); |
| 723 | } else if (!ui->IsTextVisible()) { |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 724 | return; // reboot if logs aren't visible |
| 725 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 726 | ui->Print("\nInstall from sdcard complete.\n"); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 727 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 728 | } |
| 729 | break; |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 730 | |
| 731 | case Device::APPLY_CACHE: |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 732 | // Don't unmount cache at the end of this. |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 733 | status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 734 | if (status == INSTALL_SUCCESS && wipe_cache) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 735 | ui->Print("\n-- Wiping cache (at package request)...\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 736 | if (erase_volume("/cache")) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 737 | ui->Print("Cache wipe failed.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 738 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 739 | ui->Print("Cache wipe complete.\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 742 | if (status >= 0) { |
| 743 | if (status != INSTALL_SUCCESS) { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 744 | ui->SetBackground(RecoveryUI::ERROR); |
| 745 | ui->Print("Installation aborted.\n"); |
| 746 | } else if (!ui->IsTextVisible()) { |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 747 | return; // reboot if logs aren't visible |
| 748 | } else { |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 749 | ui->Print("\nInstall from cache complete.\n"); |
Michael Ward | 9d2629c | 2011-06-23 22:14:24 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | break; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 753 | |
| 754 | case Device::APPLY_ADB_SIDELOAD: |
| 755 | ensure_path_mounted(CACHE_ROOT); |
| 756 | status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE); |
| 757 | if (status >= 0) { |
| 758 | if (status != INSTALL_SUCCESS) { |
| 759 | ui->SetBackground(RecoveryUI::ERROR); |
| 760 | ui->Print("Installation aborted.\n"); |
| 761 | } else if (!ui->IsTextVisible()) { |
| 762 | return; // reboot if logs aren't visible |
| 763 | } else { |
| 764 | ui->Print("\nInstall from ADB complete.\n"); |
| 765 | } |
| 766 | } |
| 767 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | static void |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 773 | print_property(const char *key, const char *name, void *cookie) { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 774 | printf("%s=%s\n", key, name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 777 | static void |
| 778 | load_locale_from_cache() { |
| 779 | FILE* fp = fopen_path(LOCALE_FILE, "r"); |
| 780 | char buffer[80]; |
| 781 | if (fp != NULL) { |
| 782 | fgets(buffer, sizeof(buffer), fp); |
| 783 | int j = 0; |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame^] | 784 | unsigned int i; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 785 | for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) { |
| 786 | if (!isspace(buffer[i])) { |
| 787 | buffer[j++] = buffer[i]; |
| 788 | } |
| 789 | } |
| 790 | buffer[j] = 0; |
| 791 | locale = strdup(buffer); |
| 792 | } |
| 793 | } |
| 794 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 795 | int |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 796 | main(int argc, char **argv) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 797 | time_t start = time(NULL); |
| 798 | |
| 799 | // If these fail, there's not really anywhere to complain... |
| 800 | freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL); |
| 801 | freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 802 | |
| 803 | // If this binary is started with the single argument "--adbd", |
| 804 | // instead of being the normal recovery binary, it turns into kind |
| 805 | // of a stripped-down version of adbd that only supports the |
| 806 | // 'sideload' command. Note this must be a real argument, not |
| 807 | // anything in the command file or bootloader control block; the |
| 808 | // only way recovery should be run with this argument is when it |
| 809 | // starts a copy of itself from the apply_from_adb() function. |
| 810 | if (argc == 2 && strcmp(argv[1], "--adbd") == 0) { |
| 811 | adb_main(); |
| 812 | return 0; |
| 813 | } |
| 814 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 815 | printf("Starting recovery on %s", ctime(&start)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 816 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 817 | load_volume_table(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 818 | get_args(&argc, &argv); |
| 819 | |
| 820 | int previous_runs = 0; |
| 821 | const char *send_intent = NULL; |
| 822 | const char *update_package = NULL; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 823 | int wipe_data = 0, wipe_cache = 0, show_text = 0; |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 824 | bool just_exit = false; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 825 | |
| 826 | int arg; |
| 827 | while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) { |
| 828 | switch (arg) { |
| 829 | case 'p': previous_runs = atoi(optarg); break; |
| 830 | case 's': send_intent = optarg; break; |
| 831 | case 'u': update_package = optarg; break; |
| 832 | case 'w': wipe_data = wipe_cache = 1; break; |
| 833 | case 'c': wipe_cache = 1; break; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 834 | case 't': show_text = 1; break; |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 835 | case 'x': just_exit = true; break; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 836 | case 'l': locale = optarg; break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 837 | case '?': |
| 838 | LOGE("Invalid command argument\n"); |
| 839 | continue; |
| 840 | } |
| 841 | } |
| 842 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 843 | if (locale == NULL) { |
| 844 | load_locale_from_cache(); |
| 845 | } |
| 846 | printf("locale is [%s]\n", locale); |
| 847 | |
| 848 | Device* device = make_device(); |
| 849 | ui = device->GetUI(); |
| 850 | |
| 851 | ui->Init(); |
Doug Zongker | 5fa8c23 | 2012-09-18 12:37:02 -0700 | [diff] [blame^] | 852 | ui->SetLocale(locale); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 853 | ui->SetBackground(RecoveryUI::NONE); |
| 854 | if (show_text) ui->ShowText(true); |
| 855 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 856 | #ifdef HAVE_SELINUX |
| 857 | struct selinux_opt seopts[] = { |
| 858 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 859 | }; |
| 860 | |
| 861 | sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); |
| 862 | |
| 863 | if (!sehandle) { |
| 864 | fprintf(stderr, "Warning: No file_contexts\n"); |
Kenny Root | 038818c | 2012-04-08 11:03:01 -0700 | [diff] [blame] | 865 | ui->Print("Warning: No file_contexts\n"); |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 866 | } |
| 867 | #endif |
| 868 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 869 | device->StartRecovery(); |
Doug Zongker | efa1bab | 2010-02-01 15:59:12 -0800 | [diff] [blame] | 870 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 871 | printf("Command:"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 872 | for (arg = 0; arg < argc; arg++) { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 873 | printf(" \"%s\"", argv[arg]); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 874 | } |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 875 | printf("\n"); |
| 876 | |
| 877 | if (update_package) { |
| 878 | // For backwards compatibility on the cache partition only, if |
| 879 | // we're given an old 'root' path "CACHE:foo", change it to |
| 880 | // "/cache/foo". |
| 881 | if (strncmp(update_package, "CACHE:", 6) == 0) { |
| 882 | int len = strlen(update_package) + 10; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 883 | char* modified_path = (char*)malloc(len); |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 884 | strlcpy(modified_path, "/cache/", len); |
| 885 | strlcat(modified_path, update_package+6, len); |
| 886 | printf("(replacing path \"%s\" with \"%s\")\n", |
| 887 | update_package, modified_path); |
| 888 | update_package = modified_path; |
| 889 | } |
| 890 | } |
| 891 | printf("\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 892 | |
| 893 | property_list(print_property, NULL); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 894 | printf("\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 895 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 896 | int status = INSTALL_SUCCESS; |
| 897 | |
Doug Zongker | 540d57f | 2011-01-18 13:36:58 -0800 | [diff] [blame] | 898 | if (update_package != NULL) { |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 899 | status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE); |
| 900 | if (status == INSTALL_SUCCESS && wipe_cache) { |
| 901 | if (erase_volume("/cache")) { |
| 902 | LOGE("Cache wipe (requested by package) failed."); |
| 903 | } |
| 904 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 905 | if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 906 | } else if (wipe_data) { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 907 | if (device->WipeData()) status = INSTALL_ERROR; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 908 | if (erase_volume("/data")) status = INSTALL_ERROR; |
| 909 | if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 910 | if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n"); |
Doug Zongker | b128f54 | 2009-06-18 15:07:14 -0700 | [diff] [blame] | 911 | } else if (wipe_cache) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 912 | if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 913 | if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n"); |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 914 | } else if (!just_exit) { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 915 | status = INSTALL_NONE; // No command specified |
| 916 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 919 | if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) { |
| 920 | ui->SetBackground(RecoveryUI::ERROR); |
| 921 | } |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 922 | if (status != INSTALL_SUCCESS || ui->IsTextVisible()) { |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 923 | prompt_and_wait(device); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 924 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 925 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 926 | // Otherwise, get ready to boot the main system... |
| 927 | finish_recovery(send_intent); |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 928 | ui->Print("Rebooting...\n"); |
Ken Sumrall | 6e4472a | 2011-03-07 23:37:27 -0800 | [diff] [blame] | 929 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 930 | return EXIT_SUCCESS; |
| 931 | } |