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