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" 31 #include "edify/expr.h" 32 33 static int LoadPartitionContents(const char* filename, FileContents* file); 34 static ssize_t FileSink(unsigned char* data, ssize_t len, void* token); 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], 42 size_t target_size, 43 const Value* bonus_data); 44 45 static int mtd_partitions_scanned = 0; 46 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) { 55 file->data = NULL; 56 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); 62 } 63 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 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 104 SHA_hash(file->data, file->size, file->sha1); 105 return 0; 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) { 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 } 121 } 122 123 // Load the contents of an MTD or EMMC partition into the provided 124 // FileContents. filename should be a string of the form 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. 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 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) { 141 char* copy = strdup(filename); 142 const char* magic = strtok(copy, ":"); 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", 152 filename); 153 return -1; 154 } 155 const char* partition = strtok(NULL, ":"); 156 157 int i; 158 int colons = 0; 159 for (i = 0; filename[i] != '\0'; ++i) { 160 if (filename[i] == ':') { 161 ++colons; 162 } 163 } 164 if (colons < 3 || colons%2 == 0) { 165 printf("LoadPartitionContents called with bad filename (%s)\n", 166 filename); 167 } 168 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*)); 173 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) { 178 printf("LoadPartitionContents called with bad size (%s)\n", filename); 179 return -1; 180 } 181 sha1sum[i] = strtok(NULL, ":"); 182 index[i] = i; 183 } 184 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); 189 190 MtdReadContext* ctx = NULL; 191 FILE* dev = NULL; 192 193 switch (type) { 194 case MTD: 195 if (!mtd_partitions_scanned) { 196 mtd_scan_partitions(); 197 mtd_partitions_scanned = 1; 198 } 199 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 } 222 } 223 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) { 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 } 249 if (next != read) { 250 printf("short read (%d bytes of %d) for partition \"%s\"\n", 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. 277 printf("partition read matched size %d sha %s\n", 278 size[index[i]], sha1sum[index[i]]); 279 break; 280 } 281 282 p += read; 283 } 284 285 switch (type) { 286 case MTD: 287 mtd_read_close(ctx); 288 break; 289 290 case EMMC: 291 fclose(dev); 292 break; 293 } 294 295 296 if (i == pairs) { 297 // Ran off the end of the list of (size,sha1) pairs without 298 // finding a match. 299 printf("contents of partition \"%s\" didn't match %s\n", 300 partition, filename); 301 free(file->data); 302 file->data = NULL; 303 return -1; 304 } 305 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]; 309 } 310 311 // Fake some stat() info. 312 file->st.st_mode = 0644; 313 file->st.st_uid = 0; 314 file->st.st_gid = 0; 315 316 free(copy); 317 free(index); 318 free(size); 319 free(sha1sum); 320 321 return 0; 322 } 323 324 325 // Save the contents of the given FileContents object under the given 326 // filename. Return 0 on success. 327 int SaveFileContents(const char* filename, const FileContents* file) { 328 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); 329 if (fd < 0) { 330 printf("failed to open \"%s\" for write: %s\n", 331 filename, strerror(errno)); 332 return -1; 333 } 334 335 ssize_t bytes_written = FileSink(file->data, file->size, &fd); 336 if (bytes_written != file->size) { 337 printf("short write of \"%s\" (%ld bytes of %ld) (%s)\n", 338 filename, (long)bytes_written, (long)file->size, 339 strerror(errno)); 340 close(fd); 341 return -1; 342 } 343 fsync(fd); 344 close(fd); 345 346 if (chmod(filename, file->st.st_mode) != 0) { 347 printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno)); 348 return -1; 349 } 350 if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) { 351 printf("chown of \"%s\" failed: %s\n", filename, strerror(errno)); 352 return -1; 353 } 354 355 return 0; 356 } 357 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 377 if (partition == NULL) { 378 printf("bad partition target name \"%s\"\n", target); 379 return -1; 380 } 381 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) { 405 printf("only wrote %d of %d bytes to MTD %s\n", 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: 424 { 425 size_t start = 0; 426 int success = 0; 427 int fd = open(partition, O_RDWR); 428 if (fd < 0) { 429 printf("failed to open %s: %s\n", partition, strerror(errno)); 430 return -1; 431 } 432 int attempt; 433 434 for (attempt = 0; attempt < 2; ++attempt) { 435 lseek(fd, start, SEEK_SET); 436 while (start < len) { 437 size_t to_write = len - start; 438 if (to_write > 1<<20) to_write = 1<<20; 439 440 ssize_t written = write(fd, data+start, to_write); 441 if (written < 0) { 442 if (errno == EINTR) { 443 written = 0; 444 } else { 445 printf("failed write writing to %s (%s)\n", 446 partition, strerror(errno)); 447 return -1; 448 } 449 } 450 start += written; 451 } 452 fsync(fd); 453 454 // drop caches so our subsequent verification read 455 // won't just be reading the cache. 456 sync(); 457 int dc = open("/proc/sys/vm/drop_caches", O_WRONLY); 458 write(dc, "3\n", 2); 459 close(dc); 460 sleep(1); 461 printf(" caches dropped\n"); 462 463 // verify 464 lseek(fd, 0, SEEK_SET); 465 unsigned char buffer[4096]; 466 start = len; 467 size_t p; 468 for (p = 0; p < len; p += sizeof(buffer)) { 469 size_t to_read = len - p; 470 if (to_read > sizeof(buffer)) to_read = sizeof(buffer); 471 472 size_t so_far = 0; 473 while (so_far < to_read) { 474 ssize_t read_count = read(fd, buffer+so_far, to_read-so_far); 475 if (read_count < 0) { 476 if (errno == EINTR) { 477 read_count = 0; 478 } else { 479 printf("verify read error %s at %d: %s\n", 480 partition, p, strerror(errno)); 481 return -1; 482 } 483 } 484 if ((size_t)read_count < to_read) { 485 printf("short verify read %s at %d: %d %d %s\n", 486 partition, p, read_count, to_read, strerror(errno)); 487 } 488 so_far += read_count; 489 } 490 491 if (memcmp(buffer, data+p, to_read)) { 492 printf("verification failed starting at %d\n", p); 493 start = p; 494 break; 495 } 496 } 497 498 if (start == len) { 499 printf("verification read succeeded (attempt %d)\n", attempt+1); 500 success = true; 501 break; 502 } 503 } 504 505 if (!success) { 506 printf("failed to verify after all attempts\n"); 507 return -1; 508 } 509 510 if (close(fd) != 0) { 511 printf("error closing %s (%s)\n", partition, strerror(errno)); 512 return -1; 513 } 514 sync(); 515 break; 516 } 517 } 518 519 free(copy); 520 return 0; 521 } 522 523 524 // Take a string 'str' of 40 hex digits and parse it into the 20 525 // byte array 'digest'. 'str' may contain only the digest or be of 526 // the form "<digest>:<anything>". Return 0 on success, -1 on any 527 // error. 528 int ParseSha1(const char* str, uint8_t* digest) { 529 int i; 530 const char* ps = str; 531 uint8_t* pd = digest; 532 for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) { 533 int digit; 534 if (*ps >= '0' && *ps <= '9') { 535 digit = *ps - '0'; 536 } else if (*ps >= 'a' && *ps <= 'f') { 537 digit = *ps - 'a' + 10; 538 } else if (*ps >= 'A' && *ps <= 'F') { 539 digit = *ps - 'A' + 10; 540 } else { 541 return -1; 542 } 543 if (i % 2 == 0) { 544 *pd = digit << 4; 545 } else { 546 *pd |= digit; 547 ++pd; 548 } 549 } 550 if (*ps != '\0') return -1; 551 return 0; 552 } 553 554 // Search an array of sha1 strings for one matching the given sha1. 555 // Return the index of the match on success, or -1 if no match is 556 // found. 557 int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str, 558 int num_patches) { 559 int i; 560 uint8_t patch_sha1[SHA_DIGEST_SIZE]; 561 for (i = 0; i < num_patches; ++i) { 562 if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 && 563 memcmp(patch_sha1, sha1, SHA_DIGEST_SIZE) == 0) { 564 return i; 565 } 566 } 567 return -1; 568 } 569 570 // Returns 0 if the contents of the file (argv[2]) or the cached file 571 // match any of the sha1's on the command line (argv[3:]). Returns 572 // nonzero otherwise. 573 int applypatch_check(const char* filename, 574 int num_patches, char** const patch_sha1_str) { 575 FileContents file; 576 file.data = NULL; 577 578 // It's okay to specify no sha1s; the check will pass if the 579 // LoadFileContents is successful. (Useful for reading 580 // partitions, where the filename encodes the sha1s; no need to 581 // check them twice.) 582 if (LoadFileContents(filename, &file, RETOUCH_DO_MASK) != 0 || 583 (num_patches > 0 && 584 FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) { 585 printf("file \"%s\" doesn't have any of expected " 586 "sha1 sums; checking cache\n", filename); 587 588 free(file.data); 589 file.data = NULL; 590 591 // If the source file is missing or corrupted, it might be because 592 // we were killed in the middle of patching it. A copy of it 593 // should have been made in CACHE_TEMP_SOURCE. If that file 594 // exists and matches the sha1 we're looking for, the check still 595 // passes. 596 597 if (LoadFileContents(CACHE_TEMP_SOURCE, &file, RETOUCH_DO_MASK) != 0) { 598 printf("failed to load cache file\n"); 599 return 1; 600 } 601 602 if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) { 603 printf("cache bits don't match any sha1 for \"%s\"\n", filename); 604 free(file.data); 605 return 1; 606 } 607 } 608 609 free(file.data); 610 return 0; 611 } 612 613 int ShowLicenses() { 614 ShowBSDiffLicense(); 615 return 0; 616 } 617 618 ssize_t FileSink(unsigned char* data, ssize_t len, void* token) { 619 int fd = *(int *)token; 620 ssize_t done = 0; 621 ssize_t wrote; 622 while (done < (ssize_t) len) { 623 wrote = write(fd, data+done, len-done); 624 if (wrote <= 0) { 625 printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno)); 626 return done; 627 } 628 done += wrote; 629 } 630 return done; 631 } 632 633 typedef struct { 634 unsigned char* buffer; 635 ssize_t size; 636 ssize_t pos; 637 } MemorySinkInfo; 638 639 ssize_t MemorySink(unsigned char* data, ssize_t len, void* token) { 640 MemorySinkInfo* msi = (MemorySinkInfo*)token; 641 if (msi->size - msi->pos < len) { 642 return -1; 643 } 644 memcpy(msi->buffer + msi->pos, data, len); 645 msi->pos += len; 646 return len; 647 } 648 649 // Return the amount of free space (in bytes) on the filesystem 650 // containing filename. filename must exist. Return -1 on error. 651 size_t FreeSpaceForFile(const char* filename) { 652 struct statfs sf; 653 if (statfs(filename, &sf) != 0) { 654 printf("failed to statfs %s: %s\n", filename, strerror(errno)); 655 return -1; 656 } 657 return sf.f_bsize * sf.f_bfree; 658 } 659 660 int CacheSizeCheck(size_t bytes) { 661 if (MakeFreeSpaceOnCache(bytes) < 0) { 662 printf("unable to make %ld bytes available on /cache\n", (long)bytes); 663 return 1; 664 } else { 665 return 0; 666 } 667 } 668 669 static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { 670 int i; 671 const char* hex = "0123456789abcdef"; 672 for (i = 0; i < 4; ++i) { 673 putchar(hex[(sha1[i]>>4) & 0xf]); 674 putchar(hex[sha1[i] & 0xf]); 675 } 676 } 677 678 // This function applies binary patches to files in a way that is safe 679 // (the original file is not touched until we have the desired 680 // replacement for it) and idempotent (it's okay to run this program 681 // multiple times). 682 // 683 // - if the sha1 hash of <target_filename> is <target_sha1_string>, 684 // does nothing and exits successfully. 685 // 686 // - otherwise, if the sha1 hash of <source_filename> is one of the 687 // entries in <patch_sha1_str>, the corresponding patch from 688 // <patch_data> (which must be a VAL_BLOB) is applied to produce a 689 // new file (the type of patch is automatically detected from the 690 // blob daat). If that new file has sha1 hash <target_sha1_str>, 691 // moves it to replace <target_filename>, and exits successfully. 692 // Note that if <source_filename> and <target_filename> are not the 693 // same, <source_filename> is NOT deleted on success. 694 // <target_filename> may be the string "-" to mean "the same as 695 // source_filename". 696 // 697 // - otherwise, or if any error is encountered, exits with non-zero 698 // status. 699 // 700 // <source_filename> may refer to a partition to read the source data. 701 // See the comments for the LoadPartition Contents() function above 702 // for the format of such a filename. 703 704 int applypatch(const char* source_filename, 705 const char* target_filename, 706 const char* target_sha1_str, 707 size_t target_size, 708 int num_patches, 709 char** const patch_sha1_str, 710 Value** patch_data, 711 Value* bonus_data) { 712 printf("patch %s: ", source_filename); 713 714 if (target_filename[0] == '-' && 715 target_filename[1] == '\0') { 716 target_filename = source_filename; 717 } 718 719 uint8_t target_sha1[SHA_DIGEST_SIZE]; 720 if (ParseSha1(target_sha1_str, target_sha1) != 0) { 721 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); 722 return 1; 723 } 724 725 FileContents copy_file; 726 FileContents source_file; 727 copy_file.data = NULL; 728 source_file.data = NULL; 729 const Value* source_patch_value = NULL; 730 const Value* copy_patch_value = NULL; 731 732 // We try to load the target file into the source_file object. 733 if (LoadFileContents(target_filename, &source_file, 734 RETOUCH_DO_MASK) == 0) { 735 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { 736 // The early-exit case: the patch was already applied, this file 737 // has the desired hash, nothing for us to do. 738 printf("already "); 739 print_short_sha1(target_sha1); 740 putchar('\n'); 741 free(source_file.data); 742 return 0; 743 } 744 } 745 746 if (source_file.data == NULL || 747 (target_filename != source_filename && 748 strcmp(target_filename, source_filename) != 0)) { 749 // Need to load the source file: either we failed to load the 750 // target file, or we did but it's different from the source file. 751 free(source_file.data); 752 source_file.data = NULL; 753 LoadFileContents(source_filename, &source_file, 754 RETOUCH_DO_MASK); 755 } 756 757 if (source_file.data != NULL) { 758 int to_use = FindMatchingPatch(source_file.sha1, 759 patch_sha1_str, num_patches); 760 if (to_use >= 0) { 761 source_patch_value = patch_data[to_use]; 762 } 763 } 764 765 if (source_patch_value == NULL) { 766 free(source_file.data); 767 source_file.data = NULL; 768 printf("source file is bad; trying copy\n"); 769 770 if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file, 771 RETOUCH_DO_MASK) < 0) { 772 // fail. 773 printf("failed to read copy file\n"); 774 return 1; 775 } 776 777 int to_use = FindMatchingPatch(copy_file.sha1, 778 patch_sha1_str, num_patches); 779 if (to_use >= 0) { 780 copy_patch_value = patch_data[to_use]; 781 } 782 783 if (copy_patch_value == NULL) { 784 // fail. 785 printf("copy file doesn't match source SHA-1s either\n"); 786 free(copy_file.data); 787 return 1; 788 } 789 } 790 791 int result = GenerateTarget(&source_file, source_patch_value, 792 ©_file, copy_patch_value, 793 source_filename, target_filename, 794 target_sha1, target_size, bonus_data); 795 free(source_file.data); 796 free(copy_file.data); 797 798 return result; 799 } 800 801 static int GenerateTarget(FileContents* source_file, 802 const Value* source_patch_value, 803 FileContents* copy_file, 804 const Value* copy_patch_value, 805 const char* source_filename, 806 const char* target_filename, 807 const uint8_t target_sha1[SHA_DIGEST_SIZE], 808 size_t target_size, 809 const Value* bonus_data) { 810 int retry = 1; 811 SHA_CTX ctx; 812 int output; 813 MemorySinkInfo msi; 814 FileContents* source_to_use; 815 char* outname; 816 int made_copy = 0; 817 818 // assume that target_filename (eg "/system/app/Foo.apk") is located 819 // on the same filesystem as its top-level directory ("/system"). 820 // We need something that exists for calling statfs(). 821 char target_fs[strlen(target_filename)+1]; 822 char* slash = strchr(target_filename+1, '/'); 823 if (slash != NULL) { 824 int count = slash - target_filename; 825 strncpy(target_fs, target_filename, count); 826 target_fs[count] = '\0'; 827 } else { 828 strcpy(target_fs, target_filename); 829 } 830 831 do { 832 // Is there enough room in the target filesystem to hold the patched 833 // file? 834 835 if (strncmp(target_filename, "MTD:", 4) == 0 || 836 strncmp(target_filename, "EMMC:", 5) == 0) { 837 // If the target is a partition, we're actually going to 838 // write the output to /tmp and then copy it to the 839 // partition. statfs() always returns 0 blocks free for 840 // /tmp, so instead we'll just assume that /tmp has enough 841 // space to hold the file. 842 843 // We still write the original source to cache, in case 844 // the partition write is interrupted. 845 if (MakeFreeSpaceOnCache(source_file->size) < 0) { 846 printf("not enough free space on /cache\n"); 847 return 1; 848 } 849 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { 850 printf("failed to back up source file\n"); 851 return 1; 852 } 853 made_copy = 1; 854 retry = 0; 855 } else { 856 int enough_space = 0; 857 if (retry > 0) { 858 size_t free_space = FreeSpaceForFile(target_fs); 859 enough_space = 860 (free_space > (256 << 10)) && // 256k (two-block) minimum 861 (free_space > (target_size * 3 / 2)); // 50% margin of error 862 if (!enough_space) { 863 printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n", 864 (long)target_size, (long)free_space, retry, enough_space); 865 } 866 } 867 868 if (!enough_space) { 869 retry = 0; 870 } 871 872 if (!enough_space && source_patch_value != NULL) { 873 // Using the original source, but not enough free space. First 874 // copy the source file to cache, then delete it from the original 875 // location. 876 877 if (strncmp(source_filename, "MTD:", 4) == 0 || 878 strncmp(source_filename, "EMMC:", 5) == 0) { 879 // It's impossible to free space on the target filesystem by 880 // deleting the source if the source is a partition. If 881 // we're ever in a state where we need to do this, fail. 882 printf("not enough free space for target but source " 883 "is partition\n"); 884 return 1; 885 } 886 887 if (MakeFreeSpaceOnCache(source_file->size) < 0) { 888 printf("not enough free space on /cache\n"); 889 return 1; 890 } 891 892 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { 893 printf("failed to back up source file\n"); 894 return 1; 895 } 896 made_copy = 1; 897 unlink(source_filename); 898 899 size_t free_space = FreeSpaceForFile(target_fs); 900 printf("(now %ld bytes free for target) ", (long)free_space); 901 } 902 } 903 904 const Value* patch; 905 if (source_patch_value != NULL) { 906 source_to_use = source_file; 907 patch = source_patch_value; 908 } else { 909 source_to_use = copy_file; 910 patch = copy_patch_value; 911 } 912 913 if (patch->type != VAL_BLOB) { 914 printf("patch is not a blob\n"); 915 return 1; 916 } 917 918 SinkFn sink = NULL; 919 void* token = NULL; 920 output = -1; 921 outname = NULL; 922 if (strncmp(target_filename, "MTD:", 4) == 0 || 923 strncmp(target_filename, "EMMC:", 5) == 0) { 924 // We store the decoded output in memory. 925 msi.buffer = malloc(target_size); 926 if (msi.buffer == NULL) { 927 printf("failed to alloc %ld bytes for output\n", 928 (long)target_size); 929 return 1; 930 } 931 msi.pos = 0; 932 msi.size = target_size; 933 sink = MemorySink; 934 token = &msi; 935 } else { 936 // We write the decoded output to "<tgt-file>.patch". 937 outname = (char*)malloc(strlen(target_filename) + 10); 938 strcpy(outname, target_filename); 939 strcat(outname, ".patch"); 940 941 output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); 942 if (output < 0) { 943 printf("failed to open output file %s: %s\n", 944 outname, strerror(errno)); 945 return 1; 946 } 947 sink = FileSink; 948 token = &output; 949 } 950 951 char* header = patch->data; 952 ssize_t header_bytes_read = patch->size; 953 954 SHA_init(&ctx); 955 956 int result; 957 958 if (header_bytes_read >= 8 && 959 memcmp(header, "BSDIFF40", 8) == 0) { 960 result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size, 961 patch, 0, sink, token, &ctx); 962 } else if (header_bytes_read >= 8 && 963 memcmp(header, "IMGDIFF2", 8) == 0) { 964 result = ApplyImagePatch(source_to_use->data, source_to_use->size, 965 patch, sink, token, &ctx, bonus_data); 966 } else { 967 printf("Unknown patch file format\n"); 968 return 1; 969 } 970 971 if (output >= 0) { 972 fsync(output); 973 close(output); 974 } 975 976 if (result != 0) { 977 if (retry == 0) { 978 printf("applying patch failed\n"); 979 return result != 0; 980 } else { 981 printf("applying patch failed; retrying\n"); 982 } 983 if (outname != NULL) { 984 unlink(outname); 985 } 986 } else { 987 // succeeded; no need to retry 988 break; 989 } 990 } while (retry-- > 0); 991 992 const uint8_t* current_target_sha1 = SHA_final(&ctx); 993 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { 994 printf("patch did not produce expected sha1\n"); 995 return 1; 996 } else { 997 printf("now "); 998 print_short_sha1(target_sha1); 999 putchar('\n'); 1000 } 1001 1002 if (output < 0) { 1003 // Copy the temp file to the partition. 1004 if (WriteToPartition(msi.buffer, msi.pos, target_filename) != 0) { 1005 printf("write of patched data to %s failed\n", target_filename); 1006 return 1; 1007 } 1008 free(msi.buffer); 1009 } else { 1010 // Give the .patch file the same owner, group, and mode of the 1011 // original source file. 1012 if (chmod(outname, source_to_use->st.st_mode) != 0) { 1013 printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno)); 1014 return 1; 1015 } 1016 if (chown(outname, source_to_use->st.st_uid, 1017 source_to_use->st.st_gid) != 0) { 1018 printf("chown of \"%s\" failed: %s\n", outname, strerror(errno)); 1019 return 1; 1020 } 1021 1022 // Finally, rename the .patch file to replace the target file. 1023 if (rename(outname, target_filename) != 0) { 1024 printf("rename of .patch to \"%s\" failed: %s\n", 1025 target_filename, strerror(errno)); 1026 return 1; 1027 } 1028 } 1029 1030 // If this run of applypatch created the copy, and we're here, we 1031 // can delete it. 1032 if (made_copy) unlink(CACHE_TEMP_SOURCE); 1033 1034 // Success! 1035 return 0; 1036 } 1037