2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/statfs.h>
24 #include <sys/types.h>
28 #include "mincrypt/sha.h"
29 #include "applypatch.h"
30 #include "mtdutils/mtdutils.h"
32 int SaveFileContents(const char* filename, FileContents file);
33 int LoadMTDContents(const char* filename, FileContents* file);
34 int ParseSha1(const char* str, uint8_t* digest);
35 size_t FileSink(unsigned char* data, size_t len, void* token);
37 static int mtd_partitions_scanned = 0;
39 // Read a file into memory; store it and its associated metadata in
40 // *file. Return 0 on success.
41 int LoadFileContents(const char* filename, FileContents* file) {
44 // A special 'filename' beginning with "MTD:" means to load the
45 // contents of an MTD partition.
46 if (strncmp(filename, "MTD:", 4) == 0) {
47 return LoadMTDContents(filename, file);
50 if (stat(filename, &file->st) != 0) {
51 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
55 file->size = file->st.st_size;
56 file->data = malloc(file->size);
58 FILE* f = fopen(filename, "rb");
60 printf("failed to open \"%s\": %s\n", filename, strerror(errno));
66 size_t bytes_read = fread(file->data, 1, file->size, f);
67 if (bytes_read != file->size) {
68 printf("short read of \"%s\" (%d bytes of %d)\n",
69 filename, bytes_read, file->size);
76 SHA(file->data, file->size, file->sha1);
80 static size_t* size_array;
81 // comparison function for qsort()ing an int array of indexes into
83 static int compare_size_indices(const void* a, const void* b) {
86 if (size_array[aa] < size_array[bb]) {
88 } else if (size_array[aa] > size_array[bb]) {
95 // Load the contents of an MTD partition into the provided
96 // FileContents. filename should be a string of the form
97 // "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:...".
98 // The smallest size_n bytes for which that prefix of the mtd contents
99 // has the corresponding sha1 hash will be loaded. It is acceptable
100 // for a size value to be repeated with different sha1s. Will return
103 // This complexity is needed because if an OTA installation is
104 // interrupted, the partition might contain either the source or the
105 // target data, which might be of different lengths. We need to know
106 // the length in order to read from MTD (there is no "end-of-file"
107 // marker), so the caller must specify the possible lengths and the
108 // hash of the data, and we'll do the load expecting to find one of
110 int LoadMTDContents(const char* filename, FileContents* file) {
111 char* copy = strdup(filename);
112 const char* magic = strtok(copy, ":");
113 if (strcmp(magic, "MTD") != 0) {
114 printf("LoadMTDContents called with bad filename (%s)\n",
118 const char* partition = strtok(NULL, ":");
122 for (i = 0; filename[i] != '\0'; ++i) {
123 if (filename[i] == ':') {
127 if (colons < 3 || colons%2 == 0) {
128 printf("LoadMTDContents called with bad filename (%s)\n",
132 int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename
133 int* index = malloc(pairs * sizeof(int));
134 size_t* size = malloc(pairs * sizeof(size_t));
135 char** sha1sum = malloc(pairs * sizeof(char*));
137 for (i = 0; i < pairs; ++i) {
138 const char* size_str = strtok(NULL, ":");
139 size[i] = strtol(size_str, NULL, 10);
141 printf("LoadMTDContents called with bad size (%s)\n", filename);
144 sha1sum[i] = strtok(NULL, ":");
148 // sort the index[] array so it indexes the pairs in order of
151 qsort(index, pairs, sizeof(int), compare_size_indices);
153 if (!mtd_partitions_scanned) {
154 mtd_scan_partitions();
155 mtd_partitions_scanned = 1;
158 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
160 printf("mtd partition \"%s\" not found (loading %s)\n",
161 partition, filename);
165 MtdReadContext* ctx = mtd_read_partition(mtd);
167 printf("failed to initialize read of mtd partition \"%s\"\n",
174 uint8_t parsed_sha[SHA_DIGEST_SIZE];
176 // allocate enough memory to hold the largest size.
177 file->data = malloc(size[index[pairs-1]]);
178 char* p = (char*)file->data;
179 file->size = 0; // # bytes read so far
181 for (i = 0; i < pairs; ++i) {
182 // Read enough additional bytes to get us up to the next size
183 // (again, we're trying the possibilities in order of increasing
185 size_t next = size[index[i]] - file->size;
188 read = mtd_read_data(ctx, p, next);
190 printf("short read (%d bytes of %d) for partition \"%s\"\n",
191 read, next, partition);
196 SHA_update(&sha_ctx, p, read);
200 // Duplicate the SHA context and finalize the duplicate so we can
201 // check it against this pair's expected hash.
203 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
204 const uint8_t* sha_so_far = SHA_final(&temp_ctx);
206 if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) {
207 printf("failed to parse sha1 %s in %s\n",
208 sha1sum[index[i]], filename);
214 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) {
215 // we have a match. stop reading the partition; we'll return
216 // the data we've read so far.
217 printf("mtd read matched size %d sha %s\n",
218 size[index[i]], sha1sum[index[i]]);
228 // Ran off the end of the list of (size,sha1) pairs without
230 printf("contents of MTD partition \"%s\" didn't match %s\n",
231 partition, filename);
237 const uint8_t* sha_final = SHA_final(&sha_ctx);
238 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
239 file->sha1[i] = sha_final[i];
242 // Fake some stat() info.
243 file->st.st_mode = 0644;
256 // Save the contents of the given FileContents object under the given
257 // filename. Return 0 on success.
258 int SaveFileContents(const char* filename, FileContents file) {
259 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
261 printf("failed to open \"%s\" for write: %s\n",
262 filename, strerror(errno));
266 size_t bytes_written = FileSink(file.data, file.size, &fd);
267 if (bytes_written != file.size) {
268 printf("short write of \"%s\" (%d bytes of %d) (%s)\n",
269 filename, bytes_written, file.size, strerror(errno));
276 if (chmod(filename, file.st.st_mode) != 0) {
277 printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
280 if (chown(filename, file.st.st_uid, file.st.st_gid) != 0) {
281 printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
288 // Write a memory buffer to target_mtd partition, a string of the form
289 // "MTD:<partition>[:...]". Return 0 on success.
290 int WriteToMTDPartition(unsigned char* data, size_t len,
291 const char* target_mtd) {
292 char* partition = strchr(target_mtd, ':');
293 if (partition == NULL) {
294 printf("bad MTD target name \"%s\"\n", target_mtd);
298 // Trim off anything after a colon, eg "MTD:boot:blah:blah:blah...".
299 // We want just the partition name "boot".
300 partition = strdup(partition);
301 char* end = strchr(partition, ':');
305 if (!mtd_partitions_scanned) {
306 mtd_scan_partitions();
307 mtd_partitions_scanned = 1;
310 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
312 printf("mtd partition \"%s\" not found for writing\n", partition);
316 MtdWriteContext* ctx = mtd_write_partition(mtd);
318 printf("failed to init mtd partition \"%s\" for writing\n",
323 size_t written = mtd_write_data(ctx, (char*)data, len);
324 if (written != len) {
325 printf("only wrote %d of %d bytes to MTD %s\n",
326 written, len, partition);
327 mtd_write_close(ctx);
331 if (mtd_erase_blocks(ctx, -1) < 0) {
332 printf("error finishing mtd write of %s\n", partition);
333 mtd_write_close(ctx);
337 if (mtd_write_close(ctx)) {
338 printf("error closing mtd write of %s\n", partition);
347 // Take a string 'str' of 40 hex digits and parse it into the 20
348 // byte array 'digest'. 'str' may contain only the digest or be of
349 // the form "<digest>:<anything>". Return 0 on success, -1 on any
351 int ParseSha1(const char* str, uint8_t* digest) {
353 const char* ps = str;
354 uint8_t* pd = digest;
355 for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) {
357 if (*ps >= '0' && *ps <= '9') {
359 } else if (*ps >= 'a' && *ps <= 'f') {
360 digit = *ps - 'a' + 10;
361 } else if (*ps >= 'A' && *ps <= 'F') {
362 digit = *ps - 'A' + 10;
373 if (*ps != '\0' && *ps != ':') return -1;
377 // Parse arguments (which should be of the form "<sha1>" or
378 // "<sha1>:<filename>" into the array *patches, returning the number
379 // of Patch objects in *num_patches. Return 0 on success.
380 int ParseShaArgs(int argc, char** argv, Patch** patches, int* num_patches) {
382 *patches = malloc(*num_patches * sizeof(Patch));
385 for (i = 0; i < *num_patches; ++i) {
386 if (ParseSha1(argv[i], (*patches)[i].sha1) != 0) {
387 printf("failed to parse sha1 \"%s\"\n", argv[i]);
390 if (argv[i][SHA_DIGEST_SIZE*2] == '\0') {
391 (*patches)[i].patch_filename = NULL;
392 } else if (argv[i][SHA_DIGEST_SIZE*2] == ':') {
393 (*patches)[i].patch_filename = argv[i] + (SHA_DIGEST_SIZE*2+1);
395 printf("failed to parse filename \"%s\"\n", argv[i]);
403 // Search an array of Patch objects for one matching the given sha1.
404 // Return the Patch object on success, or NULL if no match is found.
405 const Patch* FindMatchingPatch(uint8_t* sha1, Patch* patches, int num_patches) {
407 for (i = 0; i < num_patches; ++i) {
408 if (memcmp(patches[i].sha1, sha1, SHA_DIGEST_SIZE) == 0) {
415 // Returns 0 if the contents of the file (argv[2]) or the cached file
416 // match any of the sha1's on the command line (argv[3:]). Returns
417 // nonzero otherwise.
418 int CheckMode(int argc, char** argv) {
420 printf("no filename given\n");
426 if (ParseShaArgs(argc-3, argv+3, &patches, &num_patches) != 0) { return 1; }
431 // It's okay to specify no sha1s; the check will pass if the
432 // LoadFileContents is successful. (Useful for reading MTD
433 // partitions, where the filename encodes the sha1s; no need to
434 // check them twice.)
435 if (LoadFileContents(argv[2], &file) != 0 ||
437 FindMatchingPatch(file.sha1, patches, num_patches) == NULL)) {
438 printf("file \"%s\" doesn't have any of expected "
439 "sha1 sums; checking cache\n", argv[2]);
443 // If the source file is missing or corrupted, it might be because
444 // we were killed in the middle of patching it. A copy of it
445 // should have been made in CACHE_TEMP_SOURCE. If that file
446 // exists and matches the sha1 we're looking for, the check still
449 if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
450 printf("failed to load cache file\n");
454 if (FindMatchingPatch(file.sha1, patches, num_patches) == NULL) {
455 printf("cache bits don't match any sha1 for \"%s\"\n",
470 size_t FileSink(unsigned char* data, size_t len, void* token) {
471 int fd = *(int *)token;
474 while (done < (ssize_t) len) {
475 wrote = write(fd, data+done, len-done);
477 printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno));
482 printf("wrote %d bytes to output\n", (int)done);
487 unsigned char* buffer;
492 size_t MemorySink(unsigned char* data, size_t len, void* token) {
493 MemorySinkInfo* msi = (MemorySinkInfo*)token;
494 if (msi->size - msi->pos < len) {
497 memcpy(msi->buffer + msi->pos, data, len);
502 // Return the amount of free space (in bytes) on the filesystem
503 // containing filename. filename must exist. Return -1 on error.
504 size_t FreeSpaceForFile(const char* filename) {
506 if (statfs(filename, &sf) != 0) {
507 printf("failed to statfs %s: %s\n", filename, strerror(errno));
510 return sf.f_bsize * sf.f_bfree;
513 // This program applies binary patches to files in a way that is safe
514 // (the original file is not touched until we have the desired
515 // replacement for it) and idempotent (it's okay to run this program
518 // - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
521 // - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
522 // bsdiff <patch> to <src-file> to produce a new file (the type of patch
523 // is automatically detected from the file header). If that new
524 // file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
525 // exits successfully. Note that if <src-file> and <tgt-file> are
526 // not the same, <src-file> is NOT deleted on success. <tgt-file>
527 // may be the string "-" to mean "the same as src-file".
529 // - otherwise, or if any error is encountered, exits with non-zero
532 // <src-file> (or <file> in check mode) may refer to an MTD partition
533 // to read the source data. See the comments for the
534 // LoadMTDContents() function above for the format of such a filename.
537 // As you might guess from the arguments, this function used to be
538 // main(); it was split out this way so applypatch could be built as a
539 // static library and linked into other executables as well. In the
540 // future only the library form will exist; we will not need to build
541 // this as a standalone executable.
543 // The arguments to this function are just the command-line of the
544 // standalone executable:
546 // <src-file> <tgt-file> <tgt-sha1> <tgt-size> [<src-sha1>:<patch> ...]
547 // to apply a patch. Returns 0 on success, 1 on failure.
549 // "-c" <file> [<sha1> ...]
550 // to check a file's contents against zero or more sha1s. Returns
551 // 0 if it matches any of them, 1 if it doesn't.
554 // returns 0 if enough free space is available on /cache; 1 if it
558 // shows open-source license information and returns 0.
560 // This function returns 2 if the arguments are not understood (in the
561 // standalone executable, this causes the usage message to be
564 // TODO: make the interface more sensible for use as a library.
566 int applypatch(int argc, char** argv) {
571 if (strncmp(argv[1], "-l", 3) == 0) {
572 return ShowLicenses();
575 if (strncmp(argv[1], "-c", 3) == 0) {
576 return CheckMode(argc, argv);
579 if (strncmp(argv[1], "-s", 3) == 0) {
583 size_t bytes = strtol(argv[2], NULL, 10);
584 if (MakeFreeSpaceOnCache(bytes) < 0) {
585 printf("unable to make %ld bytes available on /cache\n", (long)bytes);
592 uint8_t target_sha1[SHA_DIGEST_SIZE];
594 const char* source_filename = argv[1];
595 const char* target_filename = argv[2];
596 if (target_filename[0] == '-' &&
597 target_filename[1] == '\0') {
598 target_filename = source_filename;
601 printf("\napplying patch to %s\n", source_filename);
603 if (ParseSha1(argv[3], target_sha1) != 0) {
604 printf("failed to parse tgt-sha1 \"%s\"\n", argv[3]);
608 unsigned long target_size = strtoul(argv[4], NULL, 0);
612 if (ParseShaArgs(argc-5, argv+5, &patches, &num_patches) < 0) { return 1; }
614 FileContents copy_file;
615 FileContents source_file;
616 const char* source_patch_filename = NULL;
617 const char* copy_patch_filename = NULL;
620 // We try to load the target file into the source_file object.
621 if (LoadFileContents(target_filename, &source_file) == 0) {
622 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
623 // The early-exit case: the patch was already applied, this file
624 // has the desired hash, nothing for us to do.
625 printf("\"%s\" is already target; no patch needed\n",
631 if (source_file.data == NULL ||
632 (target_filename != source_filename &&
633 strcmp(target_filename, source_filename) != 0)) {
634 // Need to load the source file: either we failed to load the
635 // target file, or we did but it's different from the source file.
636 free(source_file.data);
637 LoadFileContents(source_filename, &source_file);
640 if (source_file.data != NULL) {
641 const Patch* to_use =
642 FindMatchingPatch(source_file.sha1, patches, num_patches);
643 if (to_use != NULL) {
644 source_patch_filename = to_use->patch_filename;
648 if (source_patch_filename == NULL) {
649 free(source_file.data);
650 printf("source file is bad; trying copy\n");
652 if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file) < 0) {
654 printf("failed to read copy file\n");
658 const Patch* to_use =
659 FindMatchingPatch(copy_file.sha1, patches, num_patches);
660 if (to_use != NULL) {
661 copy_patch_filename = to_use->patch_filename;
664 if (copy_patch_filename == NULL) {
666 printf("copy file doesn't match source SHA-1s either\n");
675 FileContents* source_to_use;
678 // assume that target_filename (eg "/system/app/Foo.apk") is located
679 // on the same filesystem as its top-level directory ("/system").
680 // We need something that exists for calling statfs().
681 char target_fs[strlen(target_filename)+1];
682 char* slash = strchr(target_filename+1, '/');
684 int count = slash - target_filename;
685 strncpy(target_fs, target_filename, count);
686 target_fs[count] = '\0';
688 strcpy(target_fs, target_filename);
692 // Is there enough room in the target filesystem to hold the patched
695 if (strncmp(target_filename, "MTD:", 4) == 0) {
696 // If the target is an MTD partition, we're actually going to
697 // write the output to /tmp and then copy it to the partition.
698 // statfs() always returns 0 blocks free for /tmp, so instead
699 // we'll just assume that /tmp has enough space to hold the file.
701 // We still write the original source to cache, in case the MTD
702 // write is interrupted.
703 if (MakeFreeSpaceOnCache(source_file.size) < 0) {
704 printf("not enough free space on /cache\n");
707 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
708 printf("failed to back up source file\n");
714 int enough_space = 0;
716 size_t free_space = FreeSpaceForFile(target_fs);
718 (free_space > (target_size * 3 / 2)); // 50% margin of error
719 printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n",
720 (long)target_size, (long)free_space, retry, enough_space);
727 if (!enough_space && source_patch_filename != NULL) {
728 // Using the original source, but not enough free space. First
729 // copy the source file to cache, then delete it from the original
732 if (strncmp(source_filename, "MTD:", 4) == 0) {
733 // It's impossible to free space on the target filesystem by
734 // deleting the source if the source is an MTD partition. If
735 // we're ever in a state where we need to do this, fail.
736 printf("not enough free space for target but source is MTD\n");
740 if (MakeFreeSpaceOnCache(source_file.size) < 0) {
741 printf("not enough free space on /cache\n");
745 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
746 printf("failed to back up source file\n");
750 unlink(source_filename);
752 size_t free_space = FreeSpaceForFile(target_fs);
753 printf("(now %ld bytes free for target)\n", (long)free_space);
757 const char* patch_filename;
758 if (source_patch_filename != NULL) {
759 source_to_use = &source_file;
760 patch_filename = source_patch_filename;
762 source_to_use = ©_file;
763 patch_filename = copy_patch_filename;
770 if (strncmp(target_filename, "MTD:", 4) == 0) {
771 // We store the decoded output in memory.
772 msi.buffer = malloc(target_size);
773 if (msi.buffer == NULL) {
774 printf("failed to alloc %ld bytes for output\n",
779 msi.size = target_size;
783 // We write the decoded output to "<tgt-file>.patch".
784 outname = (char*)malloc(strlen(target_filename) + 10);
785 strcpy(outname, target_filename);
786 strcat(outname, ".patch");
788 output = open(outname, O_WRONLY | O_CREAT | O_TRUNC);
790 printf("failed to open output file %s: %s\n",
791 outname, strerror(errno));
798 #define MAX_HEADER_LENGTH 8
799 unsigned char header[MAX_HEADER_LENGTH];
800 FILE* patchf = fopen(patch_filename, "rb");
801 if (patchf == NULL) {
802 printf("failed to open patch file %s: %s\n",
803 patch_filename, strerror(errno));
806 int header_bytes_read = fread(header, 1, MAX_HEADER_LENGTH, patchf);
813 if (header_bytes_read >= 4 &&
814 header[0] == 0xd6 && header[1] == 0xc3 &&
815 header[2] == 0xc4 && header[3] == 0) {
816 // xdelta3 patches begin "VCD" (with the high bits set) followed
817 // by a zero byte (the version number).
818 printf("error: xdelta3 patches no longer supported\n");
820 } else if (header_bytes_read >= 8 &&
821 memcmp(header, "BSDIFF40", 8) == 0) {
822 result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
823 patch_filename, 0, sink, token, &ctx);
824 } else if (header_bytes_read >= 8 &&
825 memcmp(header, "IMGDIFF", 7) == 0 &&
826 (header[7] == '1' || header[7] == '2')) {
827 result = ApplyImagePatch(source_to_use->data, source_to_use->size,
828 patch_filename, sink, token, &ctx);
830 printf("Unknown patch file format\n");
841 printf("applying patch failed\n");
844 printf("applying patch failed; retrying\n");
846 if (outname != NULL) {
850 // succeeded; no need to retry
853 } while (retry-- > 0);
855 const uint8_t* current_target_sha1 = SHA_final(&ctx);
856 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) {
857 printf("patch did not produce expected sha1\n");
862 // Copy the temp file to the MTD partition.
863 if (WriteToMTDPartition(msi.buffer, msi.pos, target_filename) != 0) {
864 printf("write of patched data to %s failed\n", target_filename);
869 // Give the .patch file the same owner, group, and mode of the
870 // original source file.
871 if (chmod(outname, source_to_use->st.st_mode) != 0) {
872 printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno));
875 if (chown(outname, source_to_use->st.st_uid,
876 source_to_use->st.st_gid) != 0) {
877 printf("chown of \"%s\" failed: %s\n", outname, strerror(errno));
881 // Finally, rename the .patch file to replace the target file.
882 if (rename(outname, target_filename) != 0) {
883 printf("rename of .patch to \"%s\" failed: %s\n",
884 target_filename, strerror(errno));
889 // If this run of applypatch created the copy, and we're here, we
891 if (made_copy) unlink(CACHE_TEMP_SOURCE);