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