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