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