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