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