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