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