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