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