Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include <errno.h> |
| 18 | #include <libgen.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/statfs.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 27 | #include <stdbool.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include "mincrypt/sha.h" |
| 30 | #include "applypatch.h" |
| 31 | #include "mtdutils/mtdutils.h" |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 32 | #include "edify/expr.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 33 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 34 | static int LoadPartitionContents(const char* filename, FileContents* file); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 35 | static ssize_t FileSink(unsigned char* data, ssize_t len, void* token); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 36 | static int GenerateTarget(FileContents* source_file, |
| 37 | const Value* source_patch_value, |
| 38 | FileContents* copy_file, |
| 39 | const Value* copy_patch_value, |
| 40 | const char* source_filename, |
| 41 | const char* target_filename, |
| 42 | const uint8_t target_sha1[SHA_DIGEST_SIZE], |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 43 | size_t target_size, |
| 44 | const Value* bonus_data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 45 | |
| 46 | static int mtd_partitions_scanned = 0; |
| 47 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 48 | // Read a file into memory; store the file contents and associated |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 49 | // metadata in *file. |
| 50 | // |
| 51 | // Return 0 on success. |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 52 | int LoadFileContents(const char* filename, FileContents* file) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 53 | file->data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 54 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 55 | // A special 'filename' beginning with "MTD:" or "EMMC:" means to |
| 56 | // load the contents of a partition. |
| 57 | if (strncmp(filename, "MTD:", 4) == 0 || |
| 58 | strncmp(filename, "EMMC:", 5) == 0) { |
| 59 | return LoadPartitionContents(filename, file); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 60 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 61 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 62 | if (stat(filename, &file->st) != 0) { |
| 63 | printf("failed to stat \"%s\": %s\n", filename, strerror(errno)); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | file->size = file->st.st_size; |
| 68 | file->data = malloc(file->size); |
| 69 | |
| 70 | FILE* f = fopen(filename, "rb"); |
| 71 | if (f == NULL) { |
| 72 | printf("failed to open \"%s\": %s\n", filename, strerror(errno)); |
| 73 | free(file->data); |
| 74 | file->data = NULL; |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | ssize_t bytes_read = fread(file->data, 1, file->size, f); |
| 79 | if (bytes_read != file->size) { |
| 80 | printf("short read of \"%s\" (%ld bytes of %ld)\n", |
| 81 | filename, (long)bytes_read, (long)file->size); |
| 82 | free(file->data); |
| 83 | file->data = NULL; |
| 84 | return -1; |
| 85 | } |
| 86 | fclose(f); |
| 87 | |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 88 | SHA_hash(file->data, file->size, file->sha1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 89 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static size_t* size_array; |
| 93 | // comparison function for qsort()ing an int array of indexes into |
| 94 | // size_array[]. |
| 95 | static int compare_size_indices(const void* a, const void* b) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 96 | int aa = *(int*)a; |
| 97 | int bb = *(int*)b; |
| 98 | if (size_array[aa] < size_array[bb]) { |
| 99 | return -1; |
| 100 | } else if (size_array[aa] > size_array[bb]) { |
| 101 | return 1; |
| 102 | } else { |
| 103 | return 0; |
| 104 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 107 | // Load the contents of an MTD or EMMC partition into the provided |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 108 | // FileContents. filename should be a string of the form |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 109 | // "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or |
| 110 | // "EMMC:<partition_device>:..."). The smallest size_n bytes for |
| 111 | // which that prefix of the partition contents has the corresponding |
| 112 | // sha1 hash will be loaded. It is acceptable for a size value to be |
| 113 | // repeated with different sha1s. Will return 0 on success. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 114 | // |
| 115 | // This complexity is needed because if an OTA installation is |
| 116 | // interrupted, the partition might contain either the source or the |
| 117 | // target data, which might be of different lengths. We need to know |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 118 | // the length in order to read from a partition (there is no |
| 119 | // "end-of-file" marker), so the caller must specify the possible |
| 120 | // lengths and the hash of the data, and we'll do the load expecting |
| 121 | // to find one of those hashes. |
| 122 | enum PartitionType { MTD, EMMC }; |
| 123 | |
| 124 | static int LoadPartitionContents(const char* filename, FileContents* file) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 125 | char* copy = strdup(filename); |
| 126 | const char* magic = strtok(copy, ":"); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 127 | |
| 128 | enum PartitionType type; |
| 129 | |
| 130 | if (strcmp(magic, "MTD") == 0) { |
| 131 | type = MTD; |
| 132 | } else if (strcmp(magic, "EMMC") == 0) { |
| 133 | type = EMMC; |
| 134 | } else { |
| 135 | printf("LoadPartitionContents called with bad filename (%s)\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 136 | filename); |
| 137 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 138 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 139 | const char* partition = strtok(NULL, ":"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 140 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 141 | int i; |
| 142 | int colons = 0; |
| 143 | for (i = 0; filename[i] != '\0'; ++i) { |
| 144 | if (filename[i] == ':') { |
| 145 | ++colons; |
| 146 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 147 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 148 | if (colons < 3 || colons%2 == 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 149 | printf("LoadPartitionContents called with bad filename (%s)\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 150 | filename); |
| 151 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 152 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 153 | int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename |
| 154 | int* index = malloc(pairs * sizeof(int)); |
| 155 | size_t* size = malloc(pairs * sizeof(size_t)); |
| 156 | char** sha1sum = malloc(pairs * sizeof(char*)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 157 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 158 | for (i = 0; i < pairs; ++i) { |
| 159 | const char* size_str = strtok(NULL, ":"); |
| 160 | size[i] = strtol(size_str, NULL, 10); |
| 161 | if (size[i] == 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 162 | printf("LoadPartitionContents called with bad size (%s)\n", filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | sha1sum[i] = strtok(NULL, ":"); |
| 166 | index[i] = i; |
| 167 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 168 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 169 | // sort the index[] array so it indexes the pairs in order of |
| 170 | // increasing size. |
| 171 | size_array = size; |
| 172 | qsort(index, pairs, sizeof(int), compare_size_indices); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 173 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 174 | MtdReadContext* ctx = NULL; |
| 175 | FILE* dev = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 176 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 177 | switch (type) { |
| 178 | case MTD: |
| 179 | if (!mtd_partitions_scanned) { |
| 180 | mtd_scan_partitions(); |
| 181 | mtd_partitions_scanned = 1; |
| 182 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 183 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 184 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 185 | if (mtd == NULL) { |
| 186 | printf("mtd partition \"%s\" not found (loading %s)\n", |
| 187 | partition, filename); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | ctx = mtd_read_partition(mtd); |
| 192 | if (ctx == NULL) { |
| 193 | printf("failed to initialize read of mtd partition \"%s\"\n", |
| 194 | partition); |
| 195 | return -1; |
| 196 | } |
| 197 | break; |
| 198 | |
| 199 | case EMMC: |
| 200 | dev = fopen(partition, "rb"); |
| 201 | if (dev == NULL) { |
| 202 | printf("failed to open emmc partition \"%s\": %s\n", |
| 203 | partition, strerror(errno)); |
| 204 | return -1; |
| 205 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 206 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 207 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 208 | SHA_CTX sha_ctx; |
| 209 | SHA_init(&sha_ctx); |
| 210 | uint8_t parsed_sha[SHA_DIGEST_SIZE]; |
| 211 | |
| 212 | // allocate enough memory to hold the largest size. |
| 213 | file->data = malloc(size[index[pairs-1]]); |
| 214 | char* p = (char*)file->data; |
| 215 | file->size = 0; // # bytes read so far |
| 216 | |
| 217 | for (i = 0; i < pairs; ++i) { |
| 218 | // Read enough additional bytes to get us up to the next size |
| 219 | // (again, we're trying the possibilities in order of increasing |
| 220 | // size). |
| 221 | size_t next = size[index[i]] - file->size; |
| 222 | size_t read = 0; |
| 223 | if (next > 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 224 | switch (type) { |
| 225 | case MTD: |
| 226 | read = mtd_read_data(ctx, p, next); |
| 227 | break; |
| 228 | |
| 229 | case EMMC: |
| 230 | read = fread(p, 1, next, dev); |
| 231 | break; |
| 232 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 233 | if (next != read) { |
| 234 | printf("short read (%d bytes of %d) for partition \"%s\"\n", |
| 235 | read, next, partition); |
| 236 | free(file->data); |
| 237 | file->data = NULL; |
| 238 | return -1; |
| 239 | } |
| 240 | SHA_update(&sha_ctx, p, read); |
| 241 | file->size += read; |
| 242 | } |
| 243 | |
| 244 | // Duplicate the SHA context and finalize the duplicate so we can |
| 245 | // check it against this pair's expected hash. |
| 246 | SHA_CTX temp_ctx; |
| 247 | memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX)); |
| 248 | const uint8_t* sha_so_far = SHA_final(&temp_ctx); |
| 249 | |
| 250 | if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) { |
| 251 | printf("failed to parse sha1 %s in %s\n", |
| 252 | sha1sum[index[i]], filename); |
| 253 | free(file->data); |
| 254 | file->data = NULL; |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) { |
| 259 | // we have a match. stop reading the partition; we'll return |
| 260 | // the data we've read so far. |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 261 | printf("partition read matched size %d sha %s\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 262 | size[index[i]], sha1sum[index[i]]); |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | p += read; |
| 267 | } |
| 268 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 269 | switch (type) { |
| 270 | case MTD: |
| 271 | mtd_read_close(ctx); |
| 272 | break; |
| 273 | |
| 274 | case EMMC: |
| 275 | fclose(dev); |
| 276 | break; |
| 277 | } |
| 278 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 279 | |
| 280 | if (i == pairs) { |
| 281 | // Ran off the end of the list of (size,sha1) pairs without |
| 282 | // finding a match. |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 283 | printf("contents of partition \"%s\" didn't match %s\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 284 | partition, filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 285 | free(file->data); |
| 286 | file->data = NULL; |
| 287 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 290 | const uint8_t* sha_final = SHA_final(&sha_ctx); |
| 291 | for (i = 0; i < SHA_DIGEST_SIZE; ++i) { |
| 292 | file->sha1[i] = sha_final[i]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 295 | // Fake some stat() info. |
| 296 | file->st.st_mode = 0644; |
| 297 | file->st.st_uid = 0; |
| 298 | file->st.st_gid = 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 299 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 300 | free(copy); |
| 301 | free(index); |
| 302 | free(size); |
| 303 | free(sha1sum); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 304 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 305 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | // Save the contents of the given FileContents object under the given |
| 310 | // filename. Return 0 on success. |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 311 | int SaveFileContents(const char* filename, const FileContents* file) { |
Nick Kralevich | 956cde8 | 2012-06-26 15:01:03 -0700 | [diff] [blame] | 312 | int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 313 | if (fd < 0) { |
| 314 | printf("failed to open \"%s\" for write: %s\n", |
| 315 | filename, strerror(errno)); |
| 316 | return -1; |
| 317 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 318 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 319 | ssize_t bytes_written = FileSink(file->data, file->size, &fd); |
| 320 | if (bytes_written != file->size) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 321 | printf("short write of \"%s\" (%ld bytes of %ld) (%s)\n", |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 322 | filename, (long)bytes_written, (long)file->size, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 323 | strerror(errno)); |
| 324 | close(fd); |
| 325 | return -1; |
| 326 | } |
| 327 | fsync(fd); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 328 | close(fd); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 329 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 330 | if (chmod(filename, file->st.st_mode) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 331 | printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 332 | return -1; |
| 333 | } |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 334 | if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 335 | printf("chown of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 336 | return -1; |
| 337 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 338 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 339 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 342 | // Write a memory buffer to 'target' partition, a string of the form |
| 343 | // "MTD:<partition>[:...]" or "EMMC:<partition_device>:". Return 0 on |
| 344 | // success. |
| 345 | int WriteToPartition(unsigned char* data, size_t len, |
| 346 | const char* target) { |
| 347 | char* copy = strdup(target); |
| 348 | const char* magic = strtok(copy, ":"); |
| 349 | |
| 350 | enum PartitionType type; |
| 351 | if (strcmp(magic, "MTD") == 0) { |
| 352 | type = MTD; |
| 353 | } else if (strcmp(magic, "EMMC") == 0) { |
| 354 | type = EMMC; |
| 355 | } else { |
| 356 | printf("WriteToPartition called with bad target (%s)\n", target); |
| 357 | return -1; |
| 358 | } |
| 359 | const char* partition = strtok(NULL, ":"); |
| 360 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 361 | if (partition == NULL) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 362 | printf("bad partition target name \"%s\"\n", target); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 363 | return -1; |
| 364 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 365 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 366 | switch (type) { |
| 367 | case MTD: |
| 368 | if (!mtd_partitions_scanned) { |
| 369 | mtd_scan_partitions(); |
| 370 | mtd_partitions_scanned = 1; |
| 371 | } |
| 372 | |
| 373 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 374 | if (mtd == NULL) { |
| 375 | printf("mtd partition \"%s\" not found for writing\n", |
| 376 | partition); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 381 | if (ctx == NULL) { |
| 382 | printf("failed to init mtd partition \"%s\" for writing\n", |
| 383 | partition); |
| 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | size_t written = mtd_write_data(ctx, (char*)data, len); |
| 388 | if (written != len) { |
| 389 | printf("only wrote %d of %d bytes to MTD %s\n", |
| 390 | written, len, partition); |
| 391 | mtd_write_close(ctx); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | if (mtd_erase_blocks(ctx, -1) < 0) { |
| 396 | printf("error finishing mtd write of %s\n", partition); |
| 397 | mtd_write_close(ctx); |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | if (mtd_write_close(ctx)) { |
| 402 | printf("error closing mtd write of %s\n", partition); |
| 403 | return -1; |
| 404 | } |
| 405 | break; |
| 406 | |
| 407 | case EMMC: |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 408 | { |
| 409 | size_t start = 0; |
| 410 | int success = 0; |
Doug Zongker | 168724c | 2013-12-19 15:16:57 -0800 | [diff] [blame] | 411 | int fd = open(partition, O_RDWR); |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 412 | if (fd < 0) { |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 413 | printf("failed to open %s: %s\n", partition, strerror(errno)); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 414 | return -1; |
| 415 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 416 | int attempt; |
| 417 | |
Doug Zongker | 168724c | 2013-12-19 15:16:57 -0800 | [diff] [blame] | 418 | for (attempt = 0; attempt < 2; ++attempt) { |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 419 | lseek(fd, start, SEEK_SET); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 420 | while (start < len) { |
| 421 | size_t to_write = len - start; |
Doug Zongker | 168724c | 2013-12-19 15:16:57 -0800 | [diff] [blame] | 422 | if (to_write > 1<<20) to_write = 1<<20; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 423 | |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 424 | ssize_t written = write(fd, data+start, to_write); |
| 425 | if (written < 0) { |
| 426 | if (errno == EINTR) { |
| 427 | written = 0; |
| 428 | } else { |
| 429 | printf("failed write writing to %s (%s)\n", |
| 430 | partition, strerror(errno)); |
| 431 | return -1; |
| 432 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 433 | } |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 434 | start += written; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 435 | } |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 436 | fsync(fd); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 437 | |
| 438 | // drop caches so our subsequent verification read |
| 439 | // won't just be reading the cache. |
| 440 | sync(); |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 441 | int dc = open("/proc/sys/vm/drop_caches", O_WRONLY); |
| 442 | write(dc, "3\n", 2); |
| 443 | close(dc); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 444 | sleep(1); |
| 445 | printf(" caches dropped\n"); |
| 446 | |
| 447 | // verify |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 448 | lseek(fd, 0, SEEK_SET); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 449 | unsigned char buffer[4096]; |
| 450 | start = len; |
| 451 | size_t p; |
| 452 | for (p = 0; p < len; p += sizeof(buffer)) { |
| 453 | size_t to_read = len - p; |
| 454 | if (to_read > sizeof(buffer)) to_read = sizeof(buffer); |
| 455 | |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 456 | size_t so_far = 0; |
| 457 | while (so_far < to_read) { |
| 458 | ssize_t read_count = read(fd, buffer+so_far, to_read-so_far); |
| 459 | if (read_count < 0) { |
| 460 | if (errno == EINTR) { |
| 461 | read_count = 0; |
| 462 | } else { |
| 463 | printf("verify read error %s at %d: %s\n", |
| 464 | partition, p, strerror(errno)); |
| 465 | return -1; |
| 466 | } |
| 467 | } |
Doug Zongker | bf4a69a | 2013-07-10 13:39:50 -0700 | [diff] [blame] | 468 | if ((size_t)read_count < to_read) { |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 469 | printf("short verify read %s at %d: %d %d %s\n", |
| 470 | partition, p, read_count, to_read, strerror(errno)); |
| 471 | } |
| 472 | so_far += read_count; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | if (memcmp(buffer, data+p, to_read)) { |
| 476 | printf("verification failed starting at %d\n", p); |
| 477 | start = p; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (start == len) { |
| 483 | printf("verification read succeeded (attempt %d)\n", attempt+1); |
| 484 | success = true; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (!success) { |
| 490 | printf("failed to verify after all attempts\n"); |
| 491 | return -1; |
| 492 | } |
| 493 | |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 494 | if (close(fd) != 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 495 | printf("error closing %s (%s)\n", partition, strerror(errno)); |
| 496 | return -1; |
| 497 | } |
Doug Zongker | bf4a69a | 2013-07-10 13:39:50 -0700 | [diff] [blame] | 498 | sync(); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 499 | break; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 500 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 501 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 502 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 503 | free(copy); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 504 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
| 508 | // Take a string 'str' of 40 hex digits and parse it into the 20 |
| 509 | // byte array 'digest'. 'str' may contain only the digest or be of |
| 510 | // the form "<digest>:<anything>". Return 0 on success, -1 on any |
| 511 | // error. |
| 512 | int ParseSha1(const char* str, uint8_t* digest) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 513 | int i; |
| 514 | const char* ps = str; |
| 515 | uint8_t* pd = digest; |
| 516 | for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) { |
| 517 | int digit; |
| 518 | if (*ps >= '0' && *ps <= '9') { |
| 519 | digit = *ps - '0'; |
| 520 | } else if (*ps >= 'a' && *ps <= 'f') { |
| 521 | digit = *ps - 'a' + 10; |
| 522 | } else if (*ps >= 'A' && *ps <= 'F') { |
| 523 | digit = *ps - 'A' + 10; |
| 524 | } else { |
| 525 | return -1; |
| 526 | } |
| 527 | if (i % 2 == 0) { |
| 528 | *pd = digit << 4; |
| 529 | } else { |
| 530 | *pd |= digit; |
| 531 | ++pd; |
| 532 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 533 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 534 | if (*ps != '\0') return -1; |
| 535 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 538 | // Search an array of sha1 strings for one matching the given sha1. |
| 539 | // Return the index of the match on success, or -1 if no match is |
| 540 | // found. |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 541 | int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 542 | int num_patches) { |
| 543 | int i; |
| 544 | uint8_t patch_sha1[SHA_DIGEST_SIZE]; |
| 545 | for (i = 0; i < num_patches; ++i) { |
| 546 | if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 && |
| 547 | memcmp(patch_sha1, sha1, SHA_DIGEST_SIZE) == 0) { |
| 548 | return i; |
| 549 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 550 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 551 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // Returns 0 if the contents of the file (argv[2]) or the cached file |
| 555 | // match any of the sha1's on the command line (argv[3:]). Returns |
| 556 | // nonzero otherwise. |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 557 | int applypatch_check(const char* filename, |
| 558 | int num_patches, char** const patch_sha1_str) { |
| 559 | FileContents file; |
| 560 | file.data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 561 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 562 | // It's okay to specify no sha1s; the check will pass if the |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 563 | // LoadFileContents is successful. (Useful for reading |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 564 | // partitions, where the filename encodes the sha1s; no need to |
| 565 | // check them twice.) |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 566 | if (LoadFileContents(filename, &file) != 0 || |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 567 | (num_patches > 0 && |
| 568 | FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) { |
| 569 | printf("file \"%s\" doesn't have any of expected " |
| 570 | "sha1 sums; checking cache\n", filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 571 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 572 | free(file.data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 573 | file.data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 574 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 575 | // If the source file is missing or corrupted, it might be because |
| 576 | // we were killed in the middle of patching it. A copy of it |
| 577 | // should have been made in CACHE_TEMP_SOURCE. If that file |
| 578 | // exists and matches the sha1 we're looking for, the check still |
| 579 | // passes. |
| 580 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 581 | if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 582 | printf("failed to load cache file\n"); |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) { |
| 587 | printf("cache bits don't match any sha1 for \"%s\"\n", filename); |
| 588 | free(file.data); |
| 589 | return 1; |
| 590 | } |
| 591 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 592 | |
| 593 | free(file.data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 594 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | int ShowLicenses() { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 598 | ShowBSDiffLicense(); |
| 599 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | ssize_t FileSink(unsigned char* data, ssize_t len, void* token) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 603 | int fd = *(int *)token; |
| 604 | ssize_t done = 0; |
| 605 | ssize_t wrote; |
| 606 | while (done < (ssize_t) len) { |
| 607 | wrote = write(fd, data+done, len-done); |
| 608 | if (wrote <= 0) { |
| 609 | printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno)); |
| 610 | return done; |
| 611 | } |
| 612 | done += wrote; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 613 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 614 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | typedef struct { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 618 | unsigned char* buffer; |
| 619 | ssize_t size; |
| 620 | ssize_t pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 621 | } MemorySinkInfo; |
| 622 | |
| 623 | ssize_t MemorySink(unsigned char* data, ssize_t len, void* token) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 624 | MemorySinkInfo* msi = (MemorySinkInfo*)token; |
| 625 | if (msi->size - msi->pos < len) { |
| 626 | return -1; |
| 627 | } |
| 628 | memcpy(msi->buffer + msi->pos, data, len); |
| 629 | msi->pos += len; |
| 630 | return len; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // Return the amount of free space (in bytes) on the filesystem |
| 634 | // containing filename. filename must exist. Return -1 on error. |
| 635 | size_t FreeSpaceForFile(const char* filename) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 636 | struct statfs sf; |
| 637 | if (statfs(filename, &sf) != 0) { |
| 638 | printf("failed to statfs %s: %s\n", filename, strerror(errno)); |
| 639 | return -1; |
| 640 | } |
| 641 | return sf.f_bsize * sf.f_bfree; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 644 | int CacheSizeCheck(size_t bytes) { |
| 645 | if (MakeFreeSpaceOnCache(bytes) < 0) { |
| 646 | printf("unable to make %ld bytes available on /cache\n", (long)bytes); |
| 647 | return 1; |
| 648 | } else { |
| 649 | return 0; |
| 650 | } |
| 651 | } |
| 652 | |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 653 | static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { |
| 654 | int i; |
| 655 | const char* hex = "0123456789abcdef"; |
| 656 | for (i = 0; i < 4; ++i) { |
| 657 | putchar(hex[(sha1[i]>>4) & 0xf]); |
| 658 | putchar(hex[sha1[i] & 0xf]); |
| 659 | } |
| 660 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 661 | |
| 662 | // This function applies binary patches to files in a way that is safe |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 663 | // (the original file is not touched until we have the desired |
| 664 | // replacement for it) and idempotent (it's okay to run this program |
| 665 | // multiple times). |
| 666 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 667 | // - if the sha1 hash of <target_filename> is <target_sha1_string>, |
| 668 | // does nothing and exits successfully. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 669 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 670 | // - otherwise, if the sha1 hash of <source_filename> is one of the |
| 671 | // entries in <patch_sha1_str>, the corresponding patch from |
| 672 | // <patch_data> (which must be a VAL_BLOB) is applied to produce a |
| 673 | // new file (the type of patch is automatically detected from the |
| 674 | // blob daat). If that new file has sha1 hash <target_sha1_str>, |
| 675 | // moves it to replace <target_filename>, and exits successfully. |
| 676 | // Note that if <source_filename> and <target_filename> are not the |
| 677 | // same, <source_filename> is NOT deleted on success. |
| 678 | // <target_filename> may be the string "-" to mean "the same as |
| 679 | // source_filename". |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 680 | // |
| 681 | // - otherwise, or if any error is encountered, exits with non-zero |
| 682 | // status. |
| 683 | // |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 684 | // <source_filename> may refer to a partition to read the source data. |
| 685 | // See the comments for the LoadPartition Contents() function above |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 686 | // for the format of such a filename. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 687 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 688 | int applypatch(const char* source_filename, |
| 689 | const char* target_filename, |
| 690 | const char* target_sha1_str, |
| 691 | size_t target_size, |
| 692 | int num_patches, |
| 693 | char** const patch_sha1_str, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 694 | Value** patch_data, |
| 695 | Value* bonus_data) { |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 696 | printf("patch %s: ", source_filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 697 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 698 | if (target_filename[0] == '-' && |
| 699 | target_filename[1] == '\0') { |
| 700 | target_filename = source_filename; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 703 | uint8_t target_sha1[SHA_DIGEST_SIZE]; |
| 704 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
| 705 | printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 706 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 707 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 708 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 709 | FileContents copy_file; |
| 710 | FileContents source_file; |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 711 | copy_file.data = NULL; |
| 712 | source_file.data = NULL; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 713 | const Value* source_patch_value = NULL; |
| 714 | const Value* copy_patch_value = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 715 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 716 | // We try to load the target file into the source_file object. |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 717 | if (LoadFileContents(target_filename, &source_file) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 718 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { |
| 719 | // The early-exit case: the patch was already applied, this file |
| 720 | // has the desired hash, nothing for us to do. |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 721 | printf("already "); |
| 722 | print_short_sha1(target_sha1); |
| 723 | putchar('\n'); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 724 | free(source_file.data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 725 | return 0; |
| 726 | } |
| 727 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 728 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 729 | if (source_file.data == NULL || |
| 730 | (target_filename != source_filename && |
| 731 | strcmp(target_filename, source_filename) != 0)) { |
| 732 | // Need to load the source file: either we failed to load the |
| 733 | // target file, or we did but it's different from the source file. |
| 734 | free(source_file.data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 735 | source_file.data = NULL; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 736 | LoadFileContents(source_filename, &source_file); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | if (source_file.data != NULL) { |
| 740 | int to_use = FindMatchingPatch(source_file.sha1, |
| 741 | patch_sha1_str, num_patches); |
| 742 | if (to_use >= 0) { |
| 743 | source_patch_value = patch_data[to_use]; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | if (source_patch_value == NULL) { |
| 748 | free(source_file.data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 749 | source_file.data = NULL; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 750 | printf("source file is bad; trying copy\n"); |
| 751 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 752 | if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 753 | // fail. |
| 754 | printf("failed to read copy file\n"); |
| 755 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 758 | int to_use = FindMatchingPatch(copy_file.sha1, |
| 759 | patch_sha1_str, num_patches); |
Doug Zongker | 8cd9e4f | 2010-08-12 17:38:09 -0700 | [diff] [blame] | 760 | if (to_use >= 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 761 | copy_patch_value = patch_data[to_use]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 762 | } |
| 763 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 764 | if (copy_patch_value == NULL) { |
| 765 | // fail. |
| 766 | printf("copy file doesn't match source SHA-1s either\n"); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 767 | free(copy_file.data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 768 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 769 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 772 | int result = GenerateTarget(&source_file, source_patch_value, |
| 773 | ©_file, copy_patch_value, |
| 774 | source_filename, target_filename, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 775 | target_sha1, target_size, bonus_data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 776 | free(source_file.data); |
| 777 | free(copy_file.data); |
| 778 | |
| 779 | return result; |
| 780 | } |
| 781 | |
| 782 | static int GenerateTarget(FileContents* source_file, |
| 783 | const Value* source_patch_value, |
| 784 | FileContents* copy_file, |
| 785 | const Value* copy_patch_value, |
| 786 | const char* source_filename, |
| 787 | const char* target_filename, |
| 788 | const uint8_t target_sha1[SHA_DIGEST_SIZE], |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 789 | size_t target_size, |
| 790 | const Value* bonus_data) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 791 | int retry = 1; |
| 792 | SHA_CTX ctx; |
| 793 | int output; |
| 794 | MemorySinkInfo msi; |
| 795 | FileContents* source_to_use; |
| 796 | char* outname; |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 797 | int made_copy = 0; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 798 | |
| 799 | // assume that target_filename (eg "/system/app/Foo.apk") is located |
| 800 | // on the same filesystem as its top-level directory ("/system"). |
| 801 | // We need something that exists for calling statfs(). |
| 802 | char target_fs[strlen(target_filename)+1]; |
| 803 | char* slash = strchr(target_filename+1, '/'); |
| 804 | if (slash != NULL) { |
| 805 | int count = slash - target_filename; |
| 806 | strncpy(target_fs, target_filename, count); |
| 807 | target_fs[count] = '\0'; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 808 | } else { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 809 | strcpy(target_fs, target_filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 812 | do { |
| 813 | // Is there enough room in the target filesystem to hold the patched |
| 814 | // file? |
| 815 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 816 | if (strncmp(target_filename, "MTD:", 4) == 0 || |
| 817 | strncmp(target_filename, "EMMC:", 5) == 0) { |
| 818 | // If the target is a partition, we're actually going to |
| 819 | // write the output to /tmp and then copy it to the |
| 820 | // partition. statfs() always returns 0 blocks free for |
| 821 | // /tmp, so instead we'll just assume that /tmp has enough |
| 822 | // space to hold the file. |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 823 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 824 | // We still write the original source to cache, in case |
| 825 | // the partition write is interrupted. |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 826 | if (MakeFreeSpaceOnCache(source_file->size) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 827 | printf("not enough free space on /cache\n"); |
| 828 | return 1; |
| 829 | } |
| 830 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 831 | printf("failed to back up source file\n"); |
| 832 | return 1; |
| 833 | } |
| 834 | made_copy = 1; |
| 835 | retry = 0; |
| 836 | } else { |
| 837 | int enough_space = 0; |
| 838 | if (retry > 0) { |
| 839 | size_t free_space = FreeSpaceForFile(target_fs); |
Doug Zongker | 201cd46 | 2010-08-13 09:41:21 -0700 | [diff] [blame] | 840 | enough_space = |
| 841 | (free_space > (256 << 10)) && // 256k (two-block) minimum |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 842 | (free_space > (target_size * 3 / 2)); // 50% margin of error |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 843 | if (!enough_space) { |
| 844 | printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n", |
| 845 | (long)target_size, (long)free_space, retry, enough_space); |
| 846 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | if (!enough_space) { |
| 850 | retry = 0; |
| 851 | } |
| 852 | |
| 853 | if (!enough_space && source_patch_value != NULL) { |
| 854 | // Using the original source, but not enough free space. First |
| 855 | // copy the source file to cache, then delete it from the original |
| 856 | // location. |
| 857 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 858 | if (strncmp(source_filename, "MTD:", 4) == 0 || |
| 859 | strncmp(source_filename, "EMMC:", 5) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 860 | // It's impossible to free space on the target filesystem by |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 861 | // deleting the source if the source is a partition. If |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 862 | // we're ever in a state where we need to do this, fail. |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 863 | printf("not enough free space for target but source " |
| 864 | "is partition\n"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 865 | return 1; |
| 866 | } |
| 867 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 868 | if (MakeFreeSpaceOnCache(source_file->size) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 869 | printf("not enough free space on /cache\n"); |
| 870 | return 1; |
| 871 | } |
| 872 | |
| 873 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 874 | printf("failed to back up source file\n"); |
| 875 | return 1; |
| 876 | } |
| 877 | made_copy = 1; |
| 878 | unlink(source_filename); |
| 879 | |
| 880 | size_t free_space = FreeSpaceForFile(target_fs); |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 881 | printf("(now %ld bytes free for target) ", (long)free_space); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
| 885 | const Value* patch; |
| 886 | if (source_patch_value != NULL) { |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 887 | source_to_use = source_file; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 888 | patch = source_patch_value; |
| 889 | } else { |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 890 | source_to_use = copy_file; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 891 | patch = copy_patch_value; |
| 892 | } |
| 893 | |
| 894 | if (patch->type != VAL_BLOB) { |
| 895 | printf("patch is not a blob\n"); |
| 896 | return 1; |
| 897 | } |
| 898 | |
| 899 | SinkFn sink = NULL; |
| 900 | void* token = NULL; |
| 901 | output = -1; |
| 902 | outname = NULL; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 903 | if (strncmp(target_filename, "MTD:", 4) == 0 || |
| 904 | strncmp(target_filename, "EMMC:", 5) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 905 | // We store the decoded output in memory. |
| 906 | msi.buffer = malloc(target_size); |
| 907 | if (msi.buffer == NULL) { |
| 908 | printf("failed to alloc %ld bytes for output\n", |
| 909 | (long)target_size); |
| 910 | return 1; |
| 911 | } |
| 912 | msi.pos = 0; |
| 913 | msi.size = target_size; |
| 914 | sink = MemorySink; |
| 915 | token = &msi; |
| 916 | } else { |
| 917 | // We write the decoded output to "<tgt-file>.patch". |
| 918 | outname = (char*)malloc(strlen(target_filename) + 10); |
| 919 | strcpy(outname, target_filename); |
| 920 | strcat(outname, ".patch"); |
| 921 | |
Nick Kralevich | 956cde8 | 2012-06-26 15:01:03 -0700 | [diff] [blame] | 922 | output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 923 | if (output < 0) { |
| 924 | printf("failed to open output file %s: %s\n", |
| 925 | outname, strerror(errno)); |
| 926 | return 1; |
| 927 | } |
| 928 | sink = FileSink; |
| 929 | token = &output; |
| 930 | } |
| 931 | |
| 932 | char* header = patch->data; |
| 933 | ssize_t header_bytes_read = patch->size; |
| 934 | |
| 935 | SHA_init(&ctx); |
| 936 | |
| 937 | int result; |
| 938 | |
| 939 | if (header_bytes_read >= 8 && |
| 940 | memcmp(header, "BSDIFF40", 8) == 0) { |
| 941 | result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size, |
| 942 | patch, 0, sink, token, &ctx); |
| 943 | } else if (header_bytes_read >= 8 && |
| 944 | memcmp(header, "IMGDIFF2", 8) == 0) { |
| 945 | result = ApplyImagePatch(source_to_use->data, source_to_use->size, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 946 | patch, sink, token, &ctx, bonus_data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 947 | } else { |
| 948 | printf("Unknown patch file format\n"); |
| 949 | return 1; |
| 950 | } |
| 951 | |
| 952 | if (output >= 0) { |
| 953 | fsync(output); |
| 954 | close(output); |
| 955 | } |
| 956 | |
| 957 | if (result != 0) { |
| 958 | if (retry == 0) { |
| 959 | printf("applying patch failed\n"); |
| 960 | return result != 0; |
| 961 | } else { |
| 962 | printf("applying patch failed; retrying\n"); |
| 963 | } |
| 964 | if (outname != NULL) { |
| 965 | unlink(outname); |
| 966 | } |
| 967 | } else { |
| 968 | // succeeded; no need to retry |
| 969 | break; |
| 970 | } |
| 971 | } while (retry-- > 0); |
| 972 | |
| 973 | const uint8_t* current_target_sha1 = SHA_final(&ctx); |
| 974 | if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { |
| 975 | printf("patch did not produce expected sha1\n"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 976 | return 1; |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 977 | } else { |
| 978 | printf("now "); |
| 979 | print_short_sha1(target_sha1); |
| 980 | putchar('\n'); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | if (output < 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 984 | // Copy the temp file to the partition. |
| 985 | if (WriteToPartition(msi.buffer, msi.pos, target_filename) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 986 | printf("write of patched data to %s failed\n", target_filename); |
| 987 | return 1; |
| 988 | } |
| 989 | free(msi.buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 990 | } else { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 991 | // Give the .patch file the same owner, group, and mode of the |
| 992 | // original source file. |
| 993 | if (chmod(outname, source_to_use->st.st_mode) != 0) { |
| 994 | printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno)); |
| 995 | return 1; |
| 996 | } |
| 997 | if (chown(outname, source_to_use->st.st_uid, |
| 998 | source_to_use->st.st_gid) != 0) { |
| 999 | printf("chown of \"%s\" failed: %s\n", outname, strerror(errno)); |
| 1000 | return 1; |
| 1001 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1002 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1003 | // Finally, rename the .patch file to replace the target file. |
| 1004 | if (rename(outname, target_filename) != 0) { |
| 1005 | printf("rename of .patch to \"%s\" failed: %s\n", |
| 1006 | target_filename, strerror(errno)); |
| 1007 | return 1; |
| 1008 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1011 | // If this run of applypatch created the copy, and we're here, we |
| 1012 | // can delete it. |
| 1013 | if (made_copy) unlink(CACHE_TEMP_SOURCE); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1014 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1015 | // Success! |
| 1016 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1017 | } |