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