Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 17 | #include <bootloader_message/bootloader_message.h> |
| 18 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <string.h> |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/types.h> |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 26 | #include <sys/system_properties.h> |
| 27 | |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 31 | /* |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 32 | #include <android-base/file.h> |
| 33 | #include <android-base/stringprintf.h> |
| 34 | #include <android-base/unique_fd.h> |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 35 | */ |
| 36 | #ifndef EXCLUDE_FS_MGR |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 37 | #include <fs_mgr.h> |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 38 | #endif |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 39 | |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 40 | static std::string misc_blkdev; |
| 41 | |
Matt Mower | 3626bdc | 2017-01-06 13:45:54 -0600 | [diff] [blame] | 42 | void set_misc_device(std::string name) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 43 | misc_blkdev = name; |
| 44 | } |
| 45 | |
| 46 | #ifndef EXCLUDE_FS_MGR |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 47 | static struct fstab* read_fstab(std::string* err) { |
| 48 | // The fstab path is always "/fstab.${ro.hardware}". |
| 49 | std::string fstab_path = "/fstab."; |
| 50 | char value[PROP_VALUE_MAX]; |
| 51 | if (__system_property_get("ro.hardware", value) == 0) { |
| 52 | *err = "failed to get ro.hardware"; |
| 53 | return nullptr; |
| 54 | } |
| 55 | fstab_path += value; |
| 56 | struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str()); |
| 57 | if (fstab == nullptr) { |
| 58 | *err = "failed to read " + fstab_path; |
| 59 | } |
| 60 | return fstab; |
| 61 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 62 | #endif |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 63 | |
| 64 | static std::string get_misc_blk_device(std::string* err) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 65 | #ifdef EXCLUDE_FS_MGR |
| 66 | return misc_blkdev; |
| 67 | #else |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 68 | struct fstab* fstab = read_fstab(err); |
| 69 | if (fstab == nullptr) { |
| 70 | return ""; |
| 71 | } |
| 72 | fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab, "/misc"); |
| 73 | if (record == nullptr) { |
| 74 | *err = "failed to find /misc partition"; |
| 75 | return ""; |
| 76 | } |
| 77 | return record->blk_device; |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 78 | #endif |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 81 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 82 | // In recovery mode, recovery can get started and try to access the misc |
| 83 | // device before the kernel has actually created it. |
| 84 | static bool wait_for_device(const std::string& blk_device, std::string* err) { |
| 85 | int tries = 0; |
| 86 | int ret; |
| 87 | err->clear(); |
| 88 | do { |
| 89 | ++tries; |
| 90 | struct stat buf; |
| 91 | ret = stat(blk_device.c_str(), &buf); |
| 92 | if (ret == -1) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 93 | char buffer[2048]; |
| 94 | sprintf(buffer, "failed to stat %s try %d: %s\n", |
| 95 | blk_device.c_str(), tries, strerror(errno)); |
| 96 | *err += buffer; |
| 97 | /* |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 98 | *err += android::base::StringPrintf("failed to stat %s try %d: %s\n", |
| 99 | blk_device.c_str(), tries, strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 100 | */ |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 101 | sleep(1); |
| 102 | } |
| 103 | } while (ret && tries < 10); |
| 104 | |
| 105 | if (ret) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 106 | *err += "failed to stat " + blk_device + "\n"; |
| 107 | /* |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 108 | *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str()); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 109 | */ |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 110 | } |
| 111 | return ret == 0; |
| 112 | } |
| 113 | |
| 114 | static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) { |
| 115 | std::string misc_blk_device = get_misc_blk_device(err); |
| 116 | if (misc_blk_device.empty()) { |
| 117 | return false; |
| 118 | } |
| 119 | if (!wait_for_device(misc_blk_device, err)) { |
| 120 | return false; |
| 121 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 122 | int fd(open(misc_blk_device.c_str(), O_RDONLY)); |
| 123 | if (fd < 0) { |
| 124 | *err = "failed to open " + misc_blk_device + ": "; |
| 125 | *err += strerror(errno); |
| 126 | /* |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 127 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 128 | strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 129 | */ |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 132 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
| 133 | *err = "failed to lseek " + misc_blk_device + ": "; |
| 134 | *err += strerror(errno); |
| 135 | close(fd); |
| 136 | /* |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 137 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 138 | strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 139 | */ |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 142 | if (read(fd, p, size) != size) { |
| 143 | *err = "failed to read " + misc_blk_device + ": "; |
| 144 | *err += strerror(errno); |
| 145 | close(fd); |
| 146 | /* |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 147 | *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), |
| 148 | strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 149 | */ |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 150 | return false; |
| 151 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 152 | close(fd); |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
| 156 | static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 157 | std::string misc_blk_device = get_misc_blk_device(err); |
| 158 | if (misc_blk_device.empty()) { |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 159 | *err = "no misc device set"; |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
Matt Mower | 6244806 | 2017-02-12 15:13:00 -0600 | [diff] [blame] | 162 | int fd = (open(misc_blk_device.c_str(), O_WRONLY | O_SYNC)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 163 | if (fd == -1) { |
| 164 | *err = "failed to open " + misc_blk_device + ": "; |
| 165 | *err += strerror(errno); |
| 166 | /* |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 167 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 168 | strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 169 | */ |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 170 | return false; |
| 171 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 172 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
| 173 | *err = "failed to lseek " + misc_blk_device + ": "; |
| 174 | *err += strerror(errno); |
| 175 | close(fd); |
| 176 | /* |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 177 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 178 | strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 179 | */ |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 182 | if (write(fd, p, size) != size) { |
| 183 | *err = "failed to write " + misc_blk_device + ": "; |
| 184 | *err += strerror(errno); |
| 185 | close(fd); |
| 186 | /* |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 187 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), |
| 188 | strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 189 | */ |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 190 | return false; |
| 191 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 192 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 193 | // TODO: O_SYNC and fsync duplicates each other? |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 194 | if (fsync(fd) == -1) { |
| 195 | *err = "failed to fsync " + misc_blk_device + ": "; |
| 196 | *err += strerror(errno); |
| 197 | close(fd); |
| 198 | /* |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 199 | *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), |
| 200 | strerror(errno)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 201 | */ |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 204 | close(fd); |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 208 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
| 209 | return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 210 | } |
| 211 | |
| 212 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 213 | return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 214 | } |
| 215 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 216 | bool clear_bootloader_message(std::string* err) { |
| 217 | bootloader_message boot = {}; |
| 218 | return write_bootloader_message(boot, err); |
| 219 | } |
| 220 | |
| 221 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 222 | bootloader_message boot = {}; |
| 223 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 224 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 225 | for (const auto& s : options) { |
| 226 | strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 227 | if (s.substr(s.size() - 1) != "\n") { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 228 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 229 | } |
| 230 | } |
| 231 | return write_bootloader_message(boot, err); |
| 232 | } |
| 233 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 234 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err) { |
| 235 | package_data->resize(size); |
| 236 | return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 237 | } |
| 238 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 239 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
| 240 | return write_misc_partition(package_data.data(), package_data.size(), |
| 241 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 242 | } |
| 243 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 244 | extern "C" bool write_bootloader_message(const char* options) { |
| 245 | std::string err; |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 246 | bootloader_message boot = {}; |
| 247 | memcpy(&boot, options, sizeof(boot)); |
| 248 | return write_bootloader_message(boot, &err); |
| 249 | } |
| 250 | |
| 251 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 252 | static const int MAX_ARG_LENGTH = 4096; |
| 253 | static const int MAX_ARGS = 100; |
| 254 | |
| 255 | // command line args come from, in decreasing precedence: |
| 256 | // - the actual command line |
| 257 | // - the bootloader control block (one per line, after "recovery") |
| 258 | // - the contents of COMMAND_FILE (one per line) |
| 259 | void |
| 260 | get_args(int *argc, char ***argv) { |
| 261 | bootloader_message boot = {}; |
| 262 | std::string err; |
| 263 | if (!read_bootloader_message(&boot, &err)) { |
| 264 | printf("%s\n", err.c_str()); |
| 265 | // If fails, leave a zeroed bootloader_message. |
| 266 | memset(&boot, 0, sizeof(boot)); |
| 267 | } |
| 268 | //stage = strndup(boot.stage, sizeof(boot.stage)); |
| 269 | |
| 270 | if (boot.command[0] != 0 && boot.command[0] != 255) { |
| 271 | printf("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command); |
| 272 | } |
| 273 | |
| 274 | if (boot.status[0] != 0 && boot.status[0] != 255) { |
| 275 | printf("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status); |
| 276 | } |
| 277 | |
| 278 | // --- if arguments weren't supplied, look in the bootloader control block |
| 279 | if (*argc <= 1) { |
| 280 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 281 | const char *arg = strtok(boot.recovery, "\n"); |
| 282 | if (arg != NULL && !strcmp(arg, "recovery")) { |
| 283 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 284 | (*argv)[0] = strdup(arg); |
| 285 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 286 | if ((arg = strtok(NULL, "\n")) == NULL) break; |
nailyk-fr | 1021edf | 2016-12-18 14:32:25 +0100 | [diff] [blame] | 287 | |
| 288 | // if the device does not have an own recovery key combo we just want to open TWRP after |
| 289 | // walking through the factory reset screen - without actually doing a factory reset |
| 290 | #ifdef IGNORE_MISC_WIPE_DATA |
| 291 | if (!strcmp(arg, "--wipe_data")) { |
| 292 | (*argv)[*argc] = ""; |
| 293 | *argc = *argc -1; |
| 294 | printf("Bootloader arg \"%s\" ignored because TWRP was compiled with TW_IGNORE_MISC_WIPE_DATA\n", arg); |
| 295 | continue; |
| 296 | } |
| 297 | #endif |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 298 | (*argv)[*argc] = strdup(arg); |
| 299 | } |
| 300 | printf("Got arguments from boot message\n"); |
| 301 | } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) { |
| 302 | printf("Bad boot message\n\"%.20s\"\n", boot.recovery); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // --- if that doesn't work, try the command file (if we have /cache). |
| 307 | if (*argc <= 1/* && has_cache*/) { |
| 308 | FILE *fp = fopen(COMMAND_FILE, "r"); |
| 309 | if (fp != NULL) { |
| 310 | char *token; |
| 311 | char *argv0 = (*argv)[0]; |
| 312 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 313 | (*argv)[0] = argv0; // use the same program name |
| 314 | |
| 315 | char buf[MAX_ARG_LENGTH]; |
| 316 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 317 | if (!fgets(buf, sizeof(buf), fp)) break; |
| 318 | token = strtok(buf, "\r\n"); |
| 319 | if (token != NULL) { |
| 320 | (*argv)[*argc] = strdup(token); // Strip newline. |
| 321 | } else { |
| 322 | --*argc; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | fclose(fp); |
| 327 | printf("Got arguments from %s\n", COMMAND_FILE); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // --> write the arguments we have back into the bootloader control block |
| 332 | // always boot into recovery after this (until finish_recovery() is called) |
| 333 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 334 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 335 | int i; |
| 336 | for (i = 1; i < *argc; ++i) { |
| 337 | strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery)); |
| 338 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 339 | } |
| 340 | if (!write_bootloader_message(boot, &err)) { |
| 341 | printf("%s\n", err.c_str()); |
| 342 | } |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 343 | } |