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>
26 #include "mincrypt/sha.h"
27 #include "applypatch.h"
28 #include "mtdutils/mtdutils.h"
30 int LoadMTDContents(const char* filename, FileContents* file);
31 int ParseSha1(const char* str, uint8_t* digest);
33 // Read a file into memory; store it and its associated metadata in
34 // *file. Return 0 on success.
35 int LoadFileContents(const char* filename, FileContents* file) {
38 // A special 'filename' beginning with "MTD:" means to load the
39 // contents of an MTD partition.
40 if (strncmp(filename, "MTD:", 4) == 0) {
41 return LoadMTDContents(filename, file);
44 if (stat(filename, &file->st) != 0) {
45 fprintf(stderr, "failed to stat \"%s\": %s\n", filename, strerror(errno));
49 file->size = file->st.st_size;
50 file->data = malloc(file->size);
52 FILE* f = fopen(filename, "rb");
54 fprintf(stderr, "failed to open \"%s\": %s\n", filename, strerror(errno));
60 size_t bytes_read = fread(file->data, 1, file->size, f);
61 if (bytes_read != file->size) {
62 fprintf(stderr, "short read of \"%s\" (%d bytes of %d)\n",
63 filename, bytes_read, file->size);
70 SHA(file->data, file->size, file->sha1);
74 static size_t* size_array;
75 // comparison function for qsort()ing an int array of indexes into
77 static int compare_size_indices(const void* a, const void* b) {
80 if (size_array[aa] < size_array[bb]) {
82 } else if (size_array[aa] > size_array[bb]) {
89 // Load the contents of an MTD partition into the provided
90 // FileContents. filename should be a string of the form
91 // "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:...".
92 // The smallest size_n bytes for which that prefix of the mtd contents
93 // has the corresponding sha1 hash will be loaded. It is acceptable
94 // for a size value to be repeated with different sha1s. Will return
97 // This complexity is needed because if an OTA installation is
98 // interrupted, the partition might contain either the source or the
99 // target data, which might be of different lengths. We need to know
100 // the length in order to read from MTD (there is no "end-of-file"
101 // marker), so the caller must specify the possible lengths and the
102 // hash of the data, and we'll do the load expecting to find one of
104 int LoadMTDContents(const char* filename, FileContents* file) {
105 char* copy = strdup(filename);
106 const char* magic = strtok(copy, ":");
107 if (strcmp(magic, "MTD") != 0) {
108 fprintf(stderr, "LoadMTDContents called with bad filename (%s)\n",
112 const char* partition = strtok(NULL, ":");
116 for (i = 0; filename[i] != '\0'; ++i) {
117 if (filename[i] == ':') {
121 if (colons < 3 || colons%2 == 0) {
122 fprintf(stderr, "LoadMTDContents called with bad filename (%s)\n",
126 int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename
127 int* index = malloc(pairs * sizeof(int));
128 size_t* size = malloc(pairs * sizeof(size_t));
129 char** sha1sum = malloc(pairs * sizeof(char*));
131 for (i = 0; i < pairs; ++i) {
132 const char* size_str = strtok(NULL, ":");
133 size[i] = strtol(size_str, NULL, 10);
135 fprintf(stderr, "LoadMTDContents called with bad size (%s)\n", filename);
138 sha1sum[i] = strtok(NULL, ":");
142 // sort the index[] array so it indexs the pairs in order of
145 qsort(index, pairs, sizeof(int), compare_size_indices);
147 static int partitions_scanned = 0;
148 if (!partitions_scanned) {
149 mtd_scan_partitions();
150 partitions_scanned = 1;
153 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
155 fprintf(stderr, "mtd partition \"%s\" not found (loading %s)\n",
156 partition, filename);
160 MtdReadContext* ctx = mtd_read_partition(mtd);
162 fprintf(stderr, "failed to initialize read of mtd partition \"%s\"\n",
169 uint8_t parsed_sha[SHA_DIGEST_SIZE];
171 // allocate enough memory to hold the largest size.
172 file->data = malloc(size[index[pairs-1]]);
173 char* p = (char*)file->data;
174 file->size = 0; // # bytes read so far
176 for (i = 0; i < pairs; ++i) {
177 // Read enough additional bytes to get us up to the next size
178 // (again, we're trying the possibilities in order of increasing
180 size_t next = size[index[i]] - file->size;
183 read = mtd_read_data(ctx, p, next);
185 fprintf(stderr, "short read (%d bytes of %d) for partition \"%s\"\n",
186 read, next, partition);
191 SHA_update(&sha_ctx, p, read);
195 // Duplicate the SHA context and finalize the duplicate so we can
196 // check it against this pair's expected hash.
198 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
199 const uint8_t* sha_so_far = SHA_final(&temp_ctx);
201 if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) {
202 fprintf(stderr, "failed to parse sha1 %s in %s\n",
203 sha1sum[index[i]], filename);
209 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) {
210 // we have a match. stop reading the partition; we'll return
211 // the data we've read so far.
212 printf("mtd read matched size %d sha %s\n",
213 size[index[i]], sha1sum[index[i]]);
223 // Ran off the end of the list of (size,sha1) pairs without
225 fprintf(stderr, "contents of MTD partition \"%s\" didn't match %s\n",
226 partition, filename);
232 const uint8_t* sha_final = SHA_final(&sha_ctx);
233 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
234 file->sha1[i] = sha_final[i];
246 // Save the contents of the given FileContents object under the given
247 // filename. Return 0 on success.
248 int SaveFileContents(const char* filename, FileContents file) {
249 FILE* f = fopen(filename, "wb");
251 fprintf(stderr, "failed to open \"%s\" for write: %s\n",
252 filename, strerror(errno));
256 size_t bytes_written = fwrite(file.data, 1, file.size, f);
257 if (bytes_written != file.size) {
258 fprintf(stderr, "short write of \"%s\" (%d bytes of %d)\n",
259 filename, bytes_written, file.size);
266 if (chmod(filename, file.st.st_mode) != 0) {
267 fprintf(stderr, "chmod of \"%s\" failed: %s\n", filename, strerror(errno));
270 if (chown(filename, file.st.st_uid, file.st.st_gid) != 0) {
271 fprintf(stderr, "chown of \"%s\" failed: %s\n", filename, strerror(errno));
279 // Take a string 'str' of 40 hex digits and parse it into the 20
280 // byte array 'digest'. 'str' may contain only the digest or be of
281 // the form "<digest>:<anything>". Return 0 on success, -1 on any
283 int ParseSha1(const char* str, uint8_t* digest) {
285 const char* ps = str;
286 uint8_t* pd = digest;
287 for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) {
289 if (*ps >= '0' && *ps <= '9') {
291 } else if (*ps >= 'a' && *ps <= 'f') {
292 digit = *ps - 'a' + 10;
293 } else if (*ps >= 'A' && *ps <= 'F') {
294 digit = *ps - 'A' + 10;
305 if (*ps != '\0' && *ps != ':') return -1;
309 // Parse arguments (which should be of the form "<sha1>" or
310 // "<sha1>:<filename>" into the array *patches, returning the number
311 // of Patch objects in *num_patches. Return 0 on success.
312 int ParseShaArgs(int argc, char** argv, Patch** patches, int* num_patches) {
314 *patches = malloc(*num_patches * sizeof(Patch));
317 for (i = 0; i < *num_patches; ++i) {
318 if (ParseSha1(argv[i], (*patches)[i].sha1) != 0) {
319 fprintf(stderr, "failed to parse sha1 \"%s\"\n", argv[i]);
322 if (argv[i][SHA_DIGEST_SIZE*2] == '\0') {
323 (*patches)[i].patch_filename = NULL;
324 } else if (argv[i][SHA_DIGEST_SIZE*2] == ':') {
325 (*patches)[i].patch_filename = argv[i] + (SHA_DIGEST_SIZE*2+1);
327 fprintf(stderr, "failed to parse filename \"%s\"\n", argv[i]);
335 // Search an array of Patch objects for one matching the given sha1.
336 // Return the Patch object on success, or NULL if no match is found.
337 const Patch* FindMatchingPatch(uint8_t* sha1, Patch* patches, int num_patches) {
339 for (i = 0; i < num_patches; ++i) {
340 if (memcmp(patches[i].sha1, sha1, SHA_DIGEST_SIZE) == 0) {
347 // Returns 0 if the contents of the file (argv[2]) or the cached file
348 // match any of the sha1's on the command line (argv[3:]). Returns
349 // nonzero otherwise.
350 int CheckMode(int argc, char** argv) {
352 fprintf(stderr, "no filename given\n");
358 if (ParseShaArgs(argc-3, argv+3, &patches, &num_patches) != 0) { return 1; }
363 // It's okay to specify no sha1s; the check will pass if the
364 // LoadFileContents is successful. (Useful for reading MTD
365 // partitions, where the filename encodes the sha1s; no need to
366 // check them twice.)
367 if (LoadFileContents(argv[2], &file) != 0 ||
369 FindMatchingPatch(file.sha1, patches, num_patches) == NULL)) {
370 fprintf(stderr, "file \"%s\" doesn't have any of expected "
371 "sha1 sums; checking cache\n", argv[2]);
375 // If the source file is missing or corrupted, it might be because
376 // we were killed in the middle of patching it. A copy of it
377 // should have been made in CACHE_TEMP_SOURCE. If that file
378 // exists and matches the sha1 we're looking for, the check still
381 if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
382 fprintf(stderr, "failed to load cache file\n");
386 if (FindMatchingPatch(file.sha1, patches, num_patches) == NULL) {
387 fprintf(stderr, "cache bits don't match any sha1 for \"%s\"\n",
402 // Return the amount of free space (in bytes) on the filesystem
403 // containing filename. filename must exist. Return -1 on error.
404 size_t FreeSpaceForFile(const char* filename) {
406 if (statfs(filename, &sf) != 0) {
407 fprintf(stderr, "failed to statfs %s: %s\n", filename, strerror(errno));
410 return sf.f_bsize * sf.f_bfree;
413 // This program applies binary patches to files in a way that is safe
414 // (the original file is not touched until we have the desired
415 // replacement for it) and idempotent (it's okay to run this program
418 // - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
421 // - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
422 // bsdiff <patch> to <src-file> to produce a new file (the type of patch
423 // is automatically detected from the file header). If that new
424 // file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
425 // exits successfully. Note that if <src-file> and <tgt-file> are
426 // not the same, <src-file> is NOT deleted on success. <tgt-file>
427 // may be the string "-" to mean "the same as src-file".
429 // - otherwise, or if any error is encountered, exits with non-zero
432 // <src-file> (or <file> in check mode) may refer to an MTD partition
433 // to read the source data. See the comments for the
434 // LoadMTDContents() function above for the format of such a filename.
436 int main(int argc, char** argv) {
440 "usage: %s <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
441 "[<src-sha1>:<patch> ...]\n"
442 " or %s -c <file> [<sha1> ...]\n"
443 " or %s -s <bytes>\n"
446 "<src-file> or <file> may be of the form\n"
447 " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
448 "to specify reading from an MTD partition.\n\n",
449 argv[0], argv[0], argv[0], argv[0]);
453 if (strncmp(argv[1], "-l", 3) == 0) {
454 return ShowLicenses();
457 if (strncmp(argv[1], "-c", 3) == 0) {
458 return CheckMode(argc, argv);
461 if (strncmp(argv[1], "-s", 3) == 0) {
465 size_t bytes = strtol(argv[2], NULL, 10);
466 if (MakeFreeSpaceOnCache(bytes) < 0) {
467 printf("unable to make %ld bytes available on /cache\n", (long)bytes);
474 uint8_t target_sha1[SHA_DIGEST_SIZE];
476 const char* source_filename = argv[1];
477 const char* target_filename = argv[2];
478 if (target_filename[0] == '-' &&
479 target_filename[1] == '\0') {
480 target_filename = source_filename;
483 // assume that target_filename (eg "/system/app/Foo.apk") is located
484 // on the same filesystem as its top-level directory ("/system").
485 // We need something that exists for calling statfs().
486 char* target_fs = strdup(target_filename);
487 char* slash = strchr(target_fs+1, '/');
492 if (ParseSha1(argv[3], target_sha1) != 0) {
493 fprintf(stderr, "failed to parse tgt-sha1 \"%s\"\n", argv[3]);
497 unsigned long target_size = strtoul(argv[4], NULL, 0);
501 if (ParseShaArgs(argc-5, argv+5, &patches, &num_patches) < 0) { return 1; }
503 FileContents copy_file;
504 FileContents source_file;
505 const char* source_patch_filename = NULL;
506 const char* copy_patch_filename = NULL;
509 // We try to load the target file into the source_file object.
510 if (LoadFileContents(target_filename, &source_file) == 0) {
511 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
512 // The early-exit case: the patch was already applied, this file
513 // has the desired hash, nothing for us to do.
514 fprintf(stderr, "\"%s\" is already target; no patch needed\n",
520 if (source_file.data == NULL ||
521 (target_filename != source_filename &&
522 strcmp(target_filename, source_filename) != 0)) {
523 // Need to load the source file: either we failed to load the
524 // target file, or we did but it's different from the source file.
525 free(source_file.data);
526 LoadFileContents(source_filename, &source_file);
529 if (source_file.data != NULL) {
530 const Patch* to_use =
531 FindMatchingPatch(source_file.sha1, patches, num_patches);
532 if (to_use != NULL) {
533 source_patch_filename = to_use->patch_filename;
537 if (source_patch_filename == NULL) {
538 free(source_file.data);
539 fprintf(stderr, "source file is bad; trying copy\n");
541 if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file) < 0) {
543 fprintf(stderr, "failed to read copy file\n");
547 const Patch* to_use =
548 FindMatchingPatch(copy_file.sha1, patches, num_patches);
549 if (to_use != NULL) {
550 copy_patch_filename = to_use->patch_filename;
553 if (copy_patch_filename == NULL) {
555 fprintf(stderr, "copy file doesn't match source SHA-1s either\n");
560 // Is there enough room in the target filesystem to hold the patched file?
561 size_t free_space = FreeSpaceForFile(target_fs);
562 int enough_space = free_space > (target_size * 3 / 2); // 50% margin of error
563 printf("target %ld bytes; free space %ld bytes; enough %d\n",
564 (long)target_size, (long)free_space, enough_space);
566 if (!enough_space && source_patch_filename != NULL) {
567 // Using the original source, but not enough free space. First
568 // copy the source file to cache, then delete it from the original
571 if (strncmp(source_filename, "MTD:", 4) == 0) {
572 // It's impossible to free space on the target filesystem by
573 // deleting the source if the source is an MTD partition. If
574 // we're ever in a state where we need to do this, fail.
575 fprintf(stderr, "not enough free space for target but source is MTD\n");
579 if (MakeFreeSpaceOnCache(source_file.size) < 0) {
580 fprintf(stderr, "not enough free space on /cache\n");
584 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
585 fprintf(stderr, "failed to back up source file\n");
589 unlink(source_filename);
591 size_t free_space = FreeSpaceForFile(target_fs);
592 printf("(now %ld bytes free for target)\n", (long)free_space);
595 FileContents* source_to_use;
596 const char* patch_filename;
597 if (source_patch_filename != NULL) {
598 source_to_use = &source_file;
599 patch_filename = source_patch_filename;
601 source_to_use = ©_file;
602 patch_filename = copy_patch_filename;
605 // We write the decoded output to "<tgt-file>.patch".
606 char* outname = (char*)malloc(strlen(target_filename) + 10);
607 strcpy(outname, target_filename);
608 strcat(outname, ".patch");
609 FILE* output = fopen(outname, "wb");
610 if (output == NULL) {
611 fprintf(stderr, "failed to patch file %s: %s\n",
612 target_filename, strerror(errno));
616 #define MAX_HEADER_LENGTH 8
617 unsigned char header[MAX_HEADER_LENGTH];
618 FILE* patchf = fopen(patch_filename, "rb");
619 if (patchf == NULL) {
620 fprintf(stderr, "failed to open patch file %s: %s\n",
621 patch_filename, strerror(errno));
624 int header_bytes_read = fread(header, 1, MAX_HEADER_LENGTH, patchf);
630 if (header_bytes_read >= 4 &&
631 header[0] == 0xd6 && header[1] == 0xc3 &&
632 header[2] == 0xc4 && header[3] == 0) {
633 // xdelta3 patches begin "VCD" (with the high bits set) followed
634 // by a zero byte (the version number).
635 fprintf(stderr, "error: xdelta3 patches no longer supported\n");
637 } else if (header_bytes_read >= 8 &&
638 memcmp(header, "BSDIFF40", 8) == 0) {
639 int result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
640 patch_filename, 0, output, &ctx);
642 fprintf(stderr, "ApplyBSDiffPatch failed\n");
645 } else if (header_bytes_read >= 8 &&
646 memcmp(header, "IMGDIFF1", 8) == 0) {
647 int result = ApplyImagePatch(source_to_use->data, source_to_use->size,
648 patch_filename, output, &ctx);
650 fprintf(stderr, "ApplyImagePatch failed\n");
654 fprintf(stderr, "Unknown patch file format");
659 fsync(fileno(output));
662 const uint8_t* current_target_sha1 = SHA_final(&ctx);
663 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) {
664 fprintf(stderr, "patch did not produce expected sha1\n");
668 // Give the .patch file the same owner, group, and mode of the
669 // original source file.
670 if (chmod(outname, source_to_use->st.st_mode) != 0) {
671 fprintf(stderr, "chmod of \"%s\" failed: %s\n", outname, strerror(errno));
674 if (chown(outname, source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) {
675 fprintf(stderr, "chown of \"%s\" failed: %s\n", outname, strerror(errno));
679 // Finally, rename the .patch file to replace the target file.
680 if (rename(outname, target_filename) != 0) {
681 fprintf(stderr, "rename of .patch to \"%s\" failed: %s\n",
682 target_filename, strerror(errno));
686 // If this run of applypatch created the copy, and we're here, we
688 if (made_copy) unlink(CACHE_TEMP_SOURCE);