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