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 | } |
| 168 | FILE* fo = fopen(RECOVERY_COMMAND_FILE_TMP, "w"); |
| 169 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 170 | while (fgets(temp, sizeof(temp), f)) { |
| 171 | printf("read: %s", temp); |
Doug Zongker | eaf3365 | 2014-07-31 14:59:01 -0700 | [diff] [blame] | 172 | if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) { |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 173 | fn = strdup(temp + strlen("--update_package=")); |
| 174 | strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n"); |
| 175 | } |
| 176 | fputs(temp, fo); |
| 177 | } |
| 178 | fclose(f); |
| 179 | fclose(fo); |
| 180 | |
| 181 | if (fn) { |
| 182 | char* newline = strchr(fn, '\n'); |
| 183 | if (newline) *newline = 0; |
| 184 | } |
| 185 | return fn; |
| 186 | } |
| 187 | |
| 188 | int produce_block_map(const char* path, const char* map_file, const char* blk_dev, |
| 189 | int encrypted) |
| 190 | { |
| 191 | struct stat sb; |
| 192 | int ret; |
| 193 | |
| 194 | FILE* mapf = fopen(map_file, "w"); |
| 195 | |
| 196 | ret = stat(path, &sb); |
| 197 | if (ret != 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 198 | ALOGE("failed to stat %s\n", path); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 199 | return -1; |
| 200 | } |
| 201 | |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 202 | ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 203 | |
| 204 | int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 205 | 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] | 206 | |
| 207 | int* ranges; |
| 208 | int range_alloc = 1; |
| 209 | int range_used = 1; |
| 210 | ranges = malloc(range_alloc * 2 * sizeof(int)); |
| 211 | ranges[0] = -1; |
| 212 | ranges[1] = -1; |
| 213 | |
Mark Salyzyn | 2605dec | 2014-03-19 15:30:25 -0700 | [diff] [blame] | 214 | 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] | 215 | |
| 216 | unsigned char* buffers[WINDOW_SIZE]; |
| 217 | int i; |
| 218 | if (encrypted) { |
| 219 | for (i = 0; i < WINDOW_SIZE; ++i) { |
| 220 | buffers[i] = malloc(sb.st_blksize); |
| 221 | } |
| 222 | } |
| 223 | int head_block = 0; |
| 224 | int head = 0, tail = 0; |
| 225 | size_t pos = 0; |
| 226 | |
| 227 | int fd = open(path, O_RDONLY); |
| 228 | if (fd < 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 229 | ALOGE("failed to open fd for reading: %s\n", strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 230 | return -1; |
| 231 | } |
| 232 | fsync(fd); |
| 233 | |
| 234 | int wfd = -1; |
| 235 | if (encrypted) { |
| 236 | wfd = open(blk_dev, O_WRONLY); |
| 237 | if (wfd < 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 238 | ALOGE("failed to open fd for writing: %s\n", strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 239 | return -1; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | while (pos < sb.st_size) { |
| 244 | if ((tail+1) % WINDOW_SIZE == head) { |
| 245 | // write out head buffer |
| 246 | int block = head_block; |
| 247 | ret = ioctl(fd, FIBMAP, &block); |
| 248 | if (ret != 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 249 | ALOGE("failed to find block %d\n", head_block); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | add_block_to_ranges(&ranges, &range_alloc, &range_used, block); |
| 253 | if (encrypted) { |
| 254 | if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { |
| 255 | return -1; |
| 256 | } |
| 257 | } |
| 258 | head = (head + 1) % WINDOW_SIZE; |
| 259 | ++head_block; |
| 260 | } |
| 261 | |
| 262 | // read next block to tail |
| 263 | if (encrypted) { |
| 264 | size_t so_far = 0; |
| 265 | while (so_far < sb.st_blksize && pos < sb.st_size) { |
| 266 | ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far); |
| 267 | if (this_read < 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 268 | ALOGE("failed to read: %s\n", strerror(errno)); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | so_far += this_read; |
| 272 | pos += this_read; |
| 273 | } |
| 274 | } else { |
| 275 | // If we're not encrypting; we don't need to actually read |
| 276 | // anything, just skip pos forward as if we'd read a |
| 277 | // block. |
| 278 | pos += sb.st_blksize; |
| 279 | } |
| 280 | tail = (tail+1) % WINDOW_SIZE; |
| 281 | } |
| 282 | |
| 283 | while (head != tail) { |
| 284 | // write out head buffer |
| 285 | int block = head_block; |
| 286 | ret = ioctl(fd, FIBMAP, &block); |
| 287 | if (ret != 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 288 | ALOGE("failed to find block %d\n", head_block); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 289 | return -1; |
| 290 | } |
| 291 | add_block_to_ranges(&ranges, &range_alloc, &range_used, block); |
| 292 | if (encrypted) { |
| 293 | if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { |
| 294 | return -1; |
| 295 | } |
| 296 | } |
| 297 | head = (head + 1) % WINDOW_SIZE; |
| 298 | ++head_block; |
| 299 | } |
| 300 | |
| 301 | fprintf(mapf, "%d\n", range_used); |
| 302 | for (i = 0; i < range_used; ++i) { |
| 303 | fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); |
| 304 | } |
| 305 | |
| 306 | fclose(mapf); |
| 307 | close(fd); |
| 308 | if (encrypted) { |
| 309 | close(wfd); |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 315 | void wipe_misc() { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 316 | ALOGI("removing old commands from misc"); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 317 | int i; |
| 318 | for (i = 0; i < fstab->num_entries; ++i) { |
| 319 | struct fstab_rec* v = &fstab->recs[i]; |
| 320 | if (!v->mount_point) continue; |
| 321 | if (strcmp(v->mount_point, "/misc") == 0) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 322 | int fd = open(v->blk_device, O_WRONLY); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 323 | uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery |
| 324 | memset(zeroes, 0, sizeof(zeroes)); |
| 325 | |
| 326 | size_t written = 0; |
| 327 | size_t size = sizeof(zeroes); |
| 328 | while (written < size) { |
| 329 | ssize_t w = write(fd, zeroes, size-written); |
| 330 | if (w < 0 && errno != EINTR) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 331 | ALOGE("zero write failed: %s\n", strerror(errno)); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 332 | return; |
| 333 | } else { |
| 334 | written += w; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | close(fd); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 343 | void reboot_to_recovery() { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 344 | ALOGI("rebooting to recovery"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 345 | property_set("sys.powerctl", "reboot,recovery"); |
| 346 | sleep(10); |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 347 | ALOGE("reboot didn't succeed?"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int main(int argc, char** argv) |
| 351 | { |
| 352 | const char* input_path; |
| 353 | const char* map_file; |
| 354 | int do_reboot = 1; |
| 355 | |
| 356 | if (argc != 1 && argc != 3) { |
| 357 | fprintf(stderr, "usage: %s [<transform_path> <map_file>]\n", argv[0]); |
| 358 | return 2; |
| 359 | } |
| 360 | |
| 361 | if (argc == 3) { |
| 362 | // when command-line args are given this binary is being used |
| 363 | // for debugging; don't reboot to recovery at the end. |
| 364 | input_path = argv[1]; |
| 365 | map_file = argv[2]; |
| 366 | do_reboot = 0; |
| 367 | } else { |
| 368 | input_path = parse_recovery_command_file(); |
| 369 | if (input_path == NULL) { |
| 370 | // if we're rebooting to recovery without a package (say, |
| 371 | // to wipe data), then we don't need to do anything before |
| 372 | // going to recovery. |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 373 | ALOGI("no recovery command file or no update package arg"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 374 | reboot_to_recovery(); |
| 375 | return 1; |
| 376 | } |
| 377 | map_file = CACHE_BLOCK_MAP; |
| 378 | } |
| 379 | |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 380 | ALOGI("update package is %s", input_path); |
| 381 | |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 382 | // Turn the name of the file we're supposed to convert into an |
| 383 | // absolute path, so we can find what filesystem it's on. |
| 384 | char path[PATH_MAX+1]; |
| 385 | if (realpath(input_path, path) == NULL) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 386 | 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] | 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | int encryptable; |
| 391 | int encrypted; |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 392 | if (read_fstab() == NULL) { |
| 393 | return 1; |
| 394 | } |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 395 | const char* blk_dev = find_block_device(path, &encryptable, &encrypted); |
| 396 | if (blk_dev == NULL) { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 397 | ALOGE("failed to find block device for %s", path); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | // If the filesystem it's on isn't encrypted, we only produce the |
| 402 | // block map, we don't rewrite the file contents (it would be |
| 403 | // pointless to do so). |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 404 | ALOGI("encryptable: %s\n", encryptable ? "yes" : "no"); |
| 405 | ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no"); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 406 | |
Doug Zongker | 574443d | 2014-09-05 08:22:12 -0700 | [diff] [blame] | 407 | // Recovery supports installing packages from 3 paths: /cache, |
| 408 | // /data, and /sdcard. (On a particular device, other locations |
| 409 | // may work, but those are three we actually expect.) |
| 410 | // |
| 411 | // On /data we want to convert the file to a block map so that we |
| 412 | // can read the package without mounting the partition. On /cache |
| 413 | // and /sdcard we leave the file alone. |
| 414 | if (strncmp(path, "/data/", 6) != 0) { |
| 415 | // path does not start with "/data/"; leave it alone. |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 416 | unlink(RECOVERY_COMMAND_FILE_TMP); |
| 417 | } else { |
Doug Zongker | f449db2 | 2014-08-26 09:15:08 -0700 | [diff] [blame] | 418 | ALOGI("writing block map %s", map_file); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 419 | if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { |
| 420 | return 1; |
| 421 | } |
| 422 | } |
| 423 | |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 424 | wipe_misc(); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 425 | rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); |
Doug Zongker | 2efc9d9 | 2014-08-18 15:55:28 -0700 | [diff] [blame] | 426 | if (do_reboot) reboot_to_recovery(); |
Doug Zongker | 76adfc5 | 2014-01-13 10:04:25 -0800 | [diff] [blame] | 427 | return 0; |
| 428 | } |