Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | // This program takes a file on an ext4 filesystem and produces a list |
| 18 | // of the blocks that file occupies, which enables the file contents |
| 19 | // to be read directly from the block device without mounting the |
| 20 | // filesystem. |
| 21 | // |
| 22 | // If the filesystem is using an encrypted block device, it will also |
| 23 | // read the file and rewrite it to the same blocks of the underlying |
| 24 | // (unencrypted) block device, so the file contents can be read |
| 25 | // without the need for the decryption key. |
| 26 | // |
| 27 | // The output of this program is a "block map" which looks like this: |
| 28 | // |
| 29 | // /dev/block/platform/msm_sdcc.1/by-name/userdata # block device |
| 30 | // 49652 4096 # file size in bytes, block size |
| 31 | // 3 # count of block ranges |
| 32 | // 1000 1008 # block range 0 |
| 33 | // 2100 2102 # ... block range 1 |
| 34 | // 30 33 # ... block range 2 |
| 35 | // |
| 36 | // Each block range represents a half-open interval; the line "30 33" |
| 37 | // reprents the blocks [30, 31, 32]. |
| 38 | // |
| 39 | // Recovery can take this block map file and retrieve the underlying |
| 40 | // file data to use as an update package. |
| 41 | |
Elliott Hughes | d4d4c24 | 2014-12-29 12:46:43 -0800 | [diff] [blame] | 42 | #include <errno.h> |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 45 | #include <string.h> |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 46 | #include <stdarg.h> |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <linux/fs.h> |
| 51 | #include <sys/mman.h> |
| 52 | |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 53 | #define LOG_TAG "uncrypt" |
| 54 | #include <log/log.h> |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 55 | #include <cutils/properties.h> |
| 56 | #include <fs_mgr.h> |
| 57 | |
| 58 | #define WINDOW_SIZE 5 |
| 59 | #define RECOVERY_COMMAND_FILE "/cache/recovery/command" |
| 60 | #define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp" |
| 61 | #define CACHE_BLOCK_MAP "/cache/recovery/block.map" |
| 62 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 63 | static struct fstab* fstab = NULL; |
| 64 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 65 | static int write_at_offset(unsigned char* buffer, size_t size, |
| 66 | int wfd, off64_t offset) |
| 67 | { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 68 | if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { |
| 69 | ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno)); |
| 70 | return -1; |
| 71 | } |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 72 | size_t written = 0; |
| 73 | while (written < size) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 74 | ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written)); |
| 75 | if (wrote == -1) { |
| 76 | ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 77 | return -1; |
| 78 | } |
| 79 | written += wrote; |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) |
| 85 | { |
| 86 | // If the current block start is < 0, set the start to the new |
| 87 | // block. (This only happens for the very first block of the very |
| 88 | // first range.) |
| 89 | if ((*ranges)[*range_used*2-2] < 0) { |
| 90 | (*ranges)[*range_used*2-2] = new_block; |
| 91 | (*ranges)[*range_used*2-1] = new_block; |
| 92 | } |
| 93 | |
| 94 | if (new_block == (*ranges)[*range_used*2-1]) { |
| 95 | // If the new block comes immediately after the current range, |
| 96 | // all we have to do is extend the current range. |
| 97 | ++(*ranges)[*range_used*2-1]; |
| 98 | } else { |
| 99 | // We need to start a new range. |
| 100 | |
| 101 | // If there isn't enough room in the array, we need to expand it. |
| 102 | if (*range_used >= *range_alloc) { |
| 103 | *range_alloc *= 2; |
| 104 | *ranges = realloc(*ranges, *range_alloc * 2 * sizeof(int)); |
| 105 | } |
| 106 | |
| 107 | ++*range_used; |
| 108 | (*ranges)[*range_used*2-2] = new_block; |
| 109 | (*ranges)[*range_used*2-1] = new_block+1; |
| 110 | } |
| 111 | } |
| 112 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 113 | static struct fstab* read_fstab() |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 114 | { |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 115 | fstab = NULL; |
| 116 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 117 | // The fstab path is always "/fstab.${ro.hardware}". |
| 118 | char fstab_path[PATH_MAX+1] = "/fstab."; |
| 119 | if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 120 | ALOGE("failed to get ro.hardware\n"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 124 | fstab = fs_mgr_read_fstab(fstab_path); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 125 | if (!fstab) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 126 | ALOGE("failed to read %s\n", fstab_path); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 127 | return NULL; |
| 128 | } |
| 129 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 130 | return fstab; |
| 131 | } |
| 132 | |
| 133 | const char* find_block_device(const char* path, int* encryptable, int* encrypted) |
| 134 | { |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 135 | // Look for a volume whose mount point is the prefix of path and |
| 136 | // return its block device. Set encrypted if it's currently |
| 137 | // encrypted. |
| 138 | int i; |
| 139 | for (i = 0; i < fstab->num_entries; ++i) { |
| 140 | struct fstab_rec* v = &fstab->recs[i]; |
| 141 | if (!v->mount_point) continue; |
| 142 | int len = strlen(v->mount_point); |
| 143 | if (strncmp(path, v->mount_point, len) == 0 && |
| 144 | (path[len] == '/' || path[len] == 0)) { |
| 145 | *encrypted = 0; |
| 146 | *encryptable = 0; |
| 147 | if (fs_mgr_is_encryptable(v)) { |
| 148 | *encryptable = 1; |
| 149 | char buffer[PROPERTY_VALUE_MAX+1]; |
| 150 | if (property_get("ro.crypto.state", buffer, "") && |
| 151 | strcmp(buffer, "encrypted") == 0) { |
| 152 | *encrypted = 1; |
| 153 | } |
| 154 | } |
| 155 | return v->blk_device; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | char* parse_recovery_command_file() |
| 163 | { |
| 164 | char* fn = NULL; |
| 165 | int count = 0; |
| 166 | char temp[1024]; |
| 167 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 168 | FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); |
Maxim Siniavine | e7b2888 | 2014-02-13 15:48:53 -0800 | [diff] [blame] | 169 | if (f == NULL) { |
| 170 | return NULL; |
| 171 | } |
Sungmin Choi | a72512c | 2014-12-10 21:57:09 +0900 | [diff] [blame] | 172 | int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); |
| 173 | if (fd < 0) { |
| 174 | ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP); |
| 175 | return NULL; |
| 176 | } |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 177 | FILE* fo = fdopen(fd, "w"); |
Maxim Siniavine | e7b2888 | 2014-02-13 15:48:53 -0800 | [diff] [blame] | 178 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 179 | while (fgets(temp, sizeof(temp), f)) { |
| 180 | printf("read: %s", temp); |
Doug Zongker | eaf3365 | 2014-07-31 14:59:01 -0700 | [diff] [blame] | 181 | if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) { |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 182 | fn = strdup(temp + strlen("--update_package=")); |
| 183 | strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n"); |
| 184 | } |
| 185 | fputs(temp, fo); |
| 186 | } |
| 187 | fclose(f); |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 188 | fsync(fd); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 189 | fclose(fo); |
| 190 | |
| 191 | if (fn) { |
| 192 | char* newline = strchr(fn, '\n'); |
| 193 | if (newline) *newline = 0; |
| 194 | } |
| 195 | return fn; |
| 196 | } |
| 197 | |
| 198 | int produce_block_map(const char* path, const char* map_file, const char* blk_dev, |
| 199 | int encrypted) |
| 200 | { |
| 201 | struct stat sb; |
| 202 | int ret; |
| 203 | |
Sungmin Choi | a72512c | 2014-12-10 21:57:09 +0900 | [diff] [blame] | 204 | int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); |
| 205 | if (mapfd < 0) { |
| 206 | ALOGE("failed to open %s\n", map_file); |
| 207 | return -1; |
| 208 | } |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 209 | FILE* mapf = fdopen(mapfd, "w"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 210 | |
| 211 | ret = stat(path, &sb); |
| 212 | if (ret != 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 213 | ALOGE("failed to stat %s\n", path); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 214 | return -1; |
| 215 | } |
| 216 | |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 217 | ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 218 | |
| 219 | int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 220 | ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 221 | |
| 222 | int* ranges; |
| 223 | int range_alloc = 1; |
| 224 | int range_used = 1; |
| 225 | ranges = malloc(range_alloc * 2 * sizeof(int)); |
| 226 | ranges[0] = -1; |
| 227 | ranges[1] = -1; |
| 228 | |
Mark Salyzyn | 2605dec | 2014-03-19 15:30:25 -0700 | [diff] [blame] | 229 | fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 230 | |
| 231 | unsigned char* buffers[WINDOW_SIZE]; |
| 232 | int i; |
| 233 | if (encrypted) { |
| 234 | for (i = 0; i < WINDOW_SIZE; ++i) { |
| 235 | buffers[i] = malloc(sb.st_blksize); |
| 236 | } |
| 237 | } |
| 238 | int head_block = 0; |
| 239 | int head = 0, tail = 0; |
| 240 | size_t pos = 0; |
| 241 | |
| 242 | int fd = open(path, O_RDONLY); |
| 243 | if (fd < 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 244 | ALOGE("failed to open fd for reading: %s\n", strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 245 | return -1; |
| 246 | } |
| 247 | fsync(fd); |
| 248 | |
| 249 | int wfd = -1; |
| 250 | if (encrypted) { |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 251 | wfd = open(blk_dev, O_WRONLY | O_SYNC); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 252 | if (wfd < 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 253 | ALOGE("failed to open fd for writing: %s\n", strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 254 | return -1; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | while (pos < sb.st_size) { |
| 259 | if ((tail+1) % WINDOW_SIZE == head) { |
| 260 | // write out head buffer |
| 261 | int block = head_block; |
| 262 | ret = ioctl(fd, FIBMAP, &block); |
| 263 | if (ret != 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 264 | ALOGE("failed to find block %d\n", head_block); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 265 | return -1; |
| 266 | } |
| 267 | add_block_to_ranges(&ranges, &range_alloc, &range_used, block); |
| 268 | if (encrypted) { |
| 269 | if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { |
| 270 | return -1; |
| 271 | } |
| 272 | } |
| 273 | head = (head + 1) % WINDOW_SIZE; |
| 274 | ++head_block; |
| 275 | } |
| 276 | |
| 277 | // read next block to tail |
| 278 | if (encrypted) { |
| 279 | size_t so_far = 0; |
| 280 | while (so_far < sb.st_blksize && pos < sb.st_size) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 281 | ssize_t this_read = |
| 282 | TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far)); |
| 283 | if (this_read == -1) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 284 | ALOGE("failed to read: %s\n", strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | so_far += this_read; |
| 288 | pos += this_read; |
| 289 | } |
| 290 | } else { |
| 291 | // If we're not encrypting; we don't need to actually read |
| 292 | // anything, just skip pos forward as if we'd read a |
| 293 | // block. |
| 294 | pos += sb.st_blksize; |
| 295 | } |
| 296 | tail = (tail+1) % WINDOW_SIZE; |
| 297 | } |
| 298 | |
| 299 | while (head != tail) { |
| 300 | // write out head buffer |
| 301 | int block = head_block; |
| 302 | ret = ioctl(fd, FIBMAP, &block); |
| 303 | if (ret != 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 304 | ALOGE("failed to find block %d\n", head_block); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 305 | return -1; |
| 306 | } |
| 307 | add_block_to_ranges(&ranges, &range_alloc, &range_used, block); |
| 308 | if (encrypted) { |
| 309 | if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { |
| 310 | return -1; |
| 311 | } |
| 312 | } |
| 313 | head = (head + 1) % WINDOW_SIZE; |
| 314 | ++head_block; |
| 315 | } |
| 316 | |
| 317 | fprintf(mapf, "%d\n", range_used); |
| 318 | for (i = 0; i < range_used; ++i) { |
| 319 | fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); |
| 320 | } |
| 321 | |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 322 | fsync(mapfd); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 323 | fclose(mapf); |
| 324 | close(fd); |
| 325 | if (encrypted) { |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 326 | fsync(wfd); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 327 | close(wfd); |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 333 | void wipe_misc() { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 334 | ALOGI("removing old commands from misc"); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 335 | int i; |
| 336 | for (i = 0; i < fstab->num_entries; ++i) { |
| 337 | struct fstab_rec* v = &fstab->recs[i]; |
| 338 | if (!v->mount_point) continue; |
| 339 | if (strcmp(v->mount_point, "/misc") == 0) { |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 340 | int fd = open(v->blk_device, O_WRONLY | O_SYNC); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 341 | uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery |
| 342 | memset(zeroes, 0, sizeof(zeroes)); |
| 343 | |
| 344 | size_t written = 0; |
| 345 | size_t size = sizeof(zeroes); |
| 346 | while (written < size) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 347 | ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written)); |
| 348 | if (w == -1) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 349 | ALOGE("zero write failed: %s\n", strerror(errno)); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 350 | return; |
| 351 | } else { |
| 352 | written += w; |
| 353 | } |
| 354 | } |
Michael Runge | 4b54239 | 2014-11-21 16:00:45 -0800 | [diff] [blame] | 355 | fsync(fd); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 356 | close(fd); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 361 | void reboot_to_recovery() { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 362 | ALOGI("rebooting to recovery"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 363 | property_set("sys.powerctl", "reboot,recovery"); |
| 364 | sleep(10); |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 365 | ALOGE("reboot didn't succeed?"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | int main(int argc, char** argv) |
| 369 | { |
| 370 | const char* input_path; |
| 371 | const char* map_file; |
| 372 | int do_reboot = 1; |
| 373 | |
| 374 | if (argc != 1 && argc != 3) { |
| 375 | fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]); |
| 376 | return 2; |
| 377 | } |
| 378 | |
| 379 | if (argc == 3) { |
| 380 | // when command-line args are given this binary is being used |
| 381 | // for debugging; don't reboot to recovery at the end. |
| 382 | input_path = argv[1]; |
| 383 | map_file = argv[2]; |
| 384 | do_reboot = 0; |
| 385 | } else { |
| 386 | input_path = parse_recovery_command_file(); |
| 387 | if (input_path == NULL) { |
| 388 | // if we're rebooting to recovery without a package (say, |
| 389 | // to wipe data), then we don't need to do anything before |
| 390 | // going to recovery. |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 391 | ALOGI("no recovery command file or no update package arg"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 392 | reboot_to_recovery(); |
| 393 | return 1; |
| 394 | } |
| 395 | map_file = CACHE_BLOCK_MAP; |
| 396 | } |
| 397 | |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 398 | ALOGI("update package is %s", input_path); |
| 399 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 400 | // Turn the name of the file we're supposed to convert into an |
| 401 | // absolute path, so we can find what filesystem it's on. |
| 402 | char path[PATH_MAX+1]; |
| 403 | if (realpath(input_path, path) == NULL) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 404 | ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | int encryptable; |
| 409 | int encrypted; |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 410 | if (read_fstab() == NULL) { |
| 411 | return 1; |
| 412 | } |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 413 | const char* blk_dev = find_block_device(path, &encryptable, &encrypted); |
| 414 | if (blk_dev == NULL) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 415 | ALOGE("failed to find block device for %s", path); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | // If the filesystem it's on isn't encrypted, we only produce the |
| 420 | // block map, we don't rewrite the file contents (it would be |
| 421 | // pointless to do so). |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 422 | ALOGI("encryptable: %s\n", encryptable ? "yes" : "no"); |
| 423 | ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 424 | |
Doug Zongker | 574443d | 2014-09-05 08:22:12 -0700 | [diff] [blame] | 425 | // Recovery supports installing packages from 3 paths: /cache, |
| 426 | // /data, and /sdcard. (On a particular device, other locations |
| 427 | // may work, but those are three we actually expect.) |
| 428 | // |
| 429 | // On /data we want to convert the file to a block map so that we |
| 430 | // can read the package without mounting the partition. On /cache |
| 431 | // and /sdcard we leave the file alone. |
| 432 | if (strncmp(path, "/data/", 6) != 0) { |
| 433 | // path does not start with "/data/"; leave it alone. |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 434 | unlink(RECOVERY_COMMAND_FILE_TMP); |
| 435 | } else { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 436 | ALOGI("writing block map %s", map_file); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 437 | if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { |
| 438 | return 1; |
| 439 | } |
| 440 | } |
| 441 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 442 | wipe_misc(); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 443 | rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 444 | if (do_reboot) reboot_to_recovery(); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 445 | return 0; |
| 446 | } |