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