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