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