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