blob: 6f02a38ee7b6637df57f52dc5d524c57f5c7da28 [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>
Doug Zongkera1bc1482014-02-13 15:18:19 -080027#include <stdbool.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028
29#include "mincrypt/sha.h"
30#include "applypatch.h"
31#include "mtdutils/mtdutils.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080032#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080033
Doug Zongkerf291d852010-07-07 13:55:25 -070034static int LoadPartitionContents(const char* filename, FileContents* file);
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070035static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
Doug Zongker1c43c972012-02-28 11:07:09 -080036static int GenerateTarget(FileContents* source_file,
37 const Value* source_patch_value,
38 FileContents* copy_file,
39 const Value* copy_patch_value,
40 const char* source_filename,
41 const char* target_filename,
42 const uint8_t target_sha1[SHA_DIGEST_SIZE],
Doug Zongkera3ccba62012-08-20 15:28:02 -070043 size_t target_size,
44 const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -080045
46static int mtd_partitions_scanned = 0;
47
Doug Zongkera1bc1482014-02-13 15:18:19 -080048// Read a file into memory; store the file contents and associated
Hristo Bojinovdb314d62010-08-02 10:29:49 -070049// metadata in *file.
50//
51// Return 0 on success.
Doug Zongkera1bc1482014-02-13 15:18:19 -080052int LoadFileContents(const char* filename, FileContents* file) {
Doug Zongker512536a2010-02-17 16:11:44 -080053 file->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -080054
Doug Zongkerf291d852010-07-07 13:55:25 -070055 // A special 'filename' beginning with "MTD:" or "EMMC:" means to
56 // load the contents of a partition.
57 if (strncmp(filename, "MTD:", 4) == 0 ||
58 strncmp(filename, "EMMC:", 5) == 0) {
59 return LoadPartitionContents(filename, file);
Doug Zongkerc4351c72010-02-22 14:46:32 -080060 }
Doug Zongker512536a2010-02-17 16:11:44 -080061
Doug Zongkerc4351c72010-02-22 14:46:32 -080062 if (stat(filename, &file->st) != 0) {
63 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
64 return -1;
65 }
66
67 file->size = file->st.st_size;
68 file->data = malloc(file->size);
69
70 FILE* f = fopen(filename, "rb");
71 if (f == NULL) {
72 printf("failed to open \"%s\": %s\n", filename, strerror(errno));
73 free(file->data);
74 file->data = NULL;
75 return -1;
76 }
77
78 ssize_t bytes_read = fread(file->data, 1, file->size, f);
79 if (bytes_read != file->size) {
80 printf("short read of \"%s\" (%ld bytes of %ld)\n",
81 filename, (long)bytes_read, (long)file->size);
82 free(file->data);
83 file->data = NULL;
84 return -1;
85 }
86 fclose(f);
87
Doug Zongkerbac7fba2013-04-10 11:32:17 -070088 SHA_hash(file->data, file->size, file->sha1);
Doug Zongkerc4351c72010-02-22 14:46:32 -080089 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -080090}
91
92static size_t* size_array;
93// comparison function for qsort()ing an int array of indexes into
94// size_array[].
95static int compare_size_indices(const void* a, const void* b) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080096 int aa = *(int*)a;
97 int bb = *(int*)b;
98 if (size_array[aa] < size_array[bb]) {
99 return -1;
100 } else if (size_array[aa] > size_array[bb]) {
101 return 1;
102 } else {
103 return 0;
104 }
Doug Zongker512536a2010-02-17 16:11:44 -0800105}
106
Doug Zongkerf291d852010-07-07 13:55:25 -0700107// Load the contents of an MTD or EMMC partition into the provided
Doug Zongker512536a2010-02-17 16:11:44 -0800108// FileContents. filename should be a string of the form
Doug Zongkerf291d852010-07-07 13:55:25 -0700109// "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or
110// "EMMC:<partition_device>:..."). The smallest size_n bytes for
111// which that prefix of the partition contents has the corresponding
112// sha1 hash will be loaded. It is acceptable for a size value to be
113// repeated with different sha1s. Will return 0 on success.
Doug Zongker512536a2010-02-17 16:11:44 -0800114//
115// This complexity is needed because if an OTA installation is
116// interrupted, the partition might contain either the source or the
117// target data, which might be of different lengths. We need to know
Doug Zongkerf291d852010-07-07 13:55:25 -0700118// the length in order to read from a partition (there is no
119// "end-of-file" marker), so the caller must specify the possible
120// lengths and the hash of the data, and we'll do the load expecting
121// to find one of those hashes.
122enum PartitionType { MTD, EMMC };
123
124static int LoadPartitionContents(const char* filename, FileContents* file) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800125 char* copy = strdup(filename);
126 const char* magic = strtok(copy, ":");
Doug Zongkerf291d852010-07-07 13:55:25 -0700127
128 enum PartitionType type;
129
130 if (strcmp(magic, "MTD") == 0) {
131 type = MTD;
132 } else if (strcmp(magic, "EMMC") == 0) {
133 type = EMMC;
134 } else {
135 printf("LoadPartitionContents called with bad filename (%s)\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -0800136 filename);
137 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800138 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800139 const char* partition = strtok(NULL, ":");
Doug Zongker512536a2010-02-17 16:11:44 -0800140
Doug Zongkerc4351c72010-02-22 14:46:32 -0800141 int i;
142 int colons = 0;
143 for (i = 0; filename[i] != '\0'; ++i) {
144 if (filename[i] == ':') {
145 ++colons;
146 }
Doug Zongker512536a2010-02-17 16:11:44 -0800147 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800148 if (colons < 3 || colons%2 == 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700149 printf("LoadPartitionContents called with bad filename (%s)\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -0800150 filename);
151 }
Doug Zongker512536a2010-02-17 16:11:44 -0800152
Doug Zongkerc4351c72010-02-22 14:46:32 -0800153 int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename
154 int* index = malloc(pairs * sizeof(int));
155 size_t* size = malloc(pairs * sizeof(size_t));
156 char** sha1sum = malloc(pairs * sizeof(char*));
Doug Zongker512536a2010-02-17 16:11:44 -0800157
Doug Zongkerc4351c72010-02-22 14:46:32 -0800158 for (i = 0; i < pairs; ++i) {
159 const char* size_str = strtok(NULL, ":");
160 size[i] = strtol(size_str, NULL, 10);
161 if (size[i] == 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700162 printf("LoadPartitionContents called with bad size (%s)\n", filename);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800163 return -1;
164 }
165 sha1sum[i] = strtok(NULL, ":");
166 index[i] = i;
167 }
Doug Zongker512536a2010-02-17 16:11:44 -0800168
Doug Zongkerc4351c72010-02-22 14:46:32 -0800169 // sort the index[] array so it indexes the pairs in order of
170 // increasing size.
171 size_array = size;
172 qsort(index, pairs, sizeof(int), compare_size_indices);
Doug Zongker512536a2010-02-17 16:11:44 -0800173
Doug Zongkerf291d852010-07-07 13:55:25 -0700174 MtdReadContext* ctx = NULL;
175 FILE* dev = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800176
Doug Zongkerf291d852010-07-07 13:55:25 -0700177 switch (type) {
178 case MTD:
179 if (!mtd_partitions_scanned) {
180 mtd_scan_partitions();
181 mtd_partitions_scanned = 1;
182 }
Doug Zongker512536a2010-02-17 16:11:44 -0800183
Doug Zongkerf291d852010-07-07 13:55:25 -0700184 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
185 if (mtd == NULL) {
186 printf("mtd partition \"%s\" not found (loading %s)\n",
187 partition, filename);
188 return -1;
189 }
190
191 ctx = mtd_read_partition(mtd);
192 if (ctx == NULL) {
193 printf("failed to initialize read of mtd partition \"%s\"\n",
194 partition);
195 return -1;
196 }
197 break;
198
199 case EMMC:
200 dev = fopen(partition, "rb");
201 if (dev == NULL) {
202 printf("failed to open emmc partition \"%s\": %s\n",
203 partition, strerror(errno));
204 return -1;
205 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800206 }
Doug Zongker512536a2010-02-17 16:11:44 -0800207
Doug Zongkerc4351c72010-02-22 14:46:32 -0800208 SHA_CTX sha_ctx;
209 SHA_init(&sha_ctx);
210 uint8_t parsed_sha[SHA_DIGEST_SIZE];
211
212 // allocate enough memory to hold the largest size.
213 file->data = malloc(size[index[pairs-1]]);
214 char* p = (char*)file->data;
215 file->size = 0; // # bytes read so far
216
217 for (i = 0; i < pairs; ++i) {
218 // Read enough additional bytes to get us up to the next size
219 // (again, we're trying the possibilities in order of increasing
220 // size).
221 size_t next = size[index[i]] - file->size;
222 size_t read = 0;
223 if (next > 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700224 switch (type) {
225 case MTD:
226 read = mtd_read_data(ctx, p, next);
227 break;
228
229 case EMMC:
230 read = fread(p, 1, next, dev);
231 break;
232 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800233 if (next != read) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700234 printf("short read (%zu bytes of %zu) for partition \"%s\"\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -0800235 read, next, partition);
236 free(file->data);
237 file->data = NULL;
238 return -1;
239 }
240 SHA_update(&sha_ctx, p, read);
241 file->size += read;
242 }
243
244 // Duplicate the SHA context and finalize the duplicate so we can
245 // check it against this pair's expected hash.
246 SHA_CTX temp_ctx;
247 memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
248 const uint8_t* sha_so_far = SHA_final(&temp_ctx);
249
250 if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) {
251 printf("failed to parse sha1 %s in %s\n",
252 sha1sum[index[i]], filename);
253 free(file->data);
254 file->data = NULL;
255 return -1;
256 }
257
258 if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) {
259 // we have a match. stop reading the partition; we'll return
260 // the data we've read so far.
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700261 printf("partition read matched size %zu sha %s\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -0800262 size[index[i]], sha1sum[index[i]]);
263 break;
264 }
265
266 p += read;
267 }
268
Doug Zongkerf291d852010-07-07 13:55:25 -0700269 switch (type) {
270 case MTD:
271 mtd_read_close(ctx);
272 break;
273
274 case EMMC:
275 fclose(dev);
276 break;
277 }
278
Doug Zongkerc4351c72010-02-22 14:46:32 -0800279
280 if (i == pairs) {
281 // Ran off the end of the list of (size,sha1) pairs without
282 // finding a match.
Doug Zongkerf291d852010-07-07 13:55:25 -0700283 printf("contents of partition \"%s\" didn't match %s\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -0800284 partition, filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800285 free(file->data);
286 file->data = NULL;
287 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800288 }
289
Doug Zongkerc4351c72010-02-22 14:46:32 -0800290 const uint8_t* sha_final = SHA_final(&sha_ctx);
291 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
292 file->sha1[i] = sha_final[i];
Doug Zongker512536a2010-02-17 16:11:44 -0800293 }
294
Doug Zongkerc4351c72010-02-22 14:46:32 -0800295 // Fake some stat() info.
296 file->st.st_mode = 0644;
297 file->st.st_uid = 0;
298 file->st.st_gid = 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800299
Doug Zongkerc4351c72010-02-22 14:46:32 -0800300 free(copy);
301 free(index);
302 free(size);
303 free(sha1sum);
Doug Zongker512536a2010-02-17 16:11:44 -0800304
Doug Zongkerc4351c72010-02-22 14:46:32 -0800305 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800306}
307
308
309// Save the contents of the given FileContents object under the given
310// filename. Return 0 on success.
Doug Zongker1c43c972012-02-28 11:07:09 -0800311int SaveFileContents(const char* filename, const FileContents* file) {
Michael Rungebe81e512014-10-29 12:42:15 -0700312 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800313 if (fd < 0) {
314 printf("failed to open \"%s\" for write: %s\n",
315 filename, strerror(errno));
316 return -1;
317 }
Doug Zongker512536a2010-02-17 16:11:44 -0800318
Doug Zongker1c43c972012-02-28 11:07:09 -0800319 ssize_t bytes_written = FileSink(file->data, file->size, &fd);
320 if (bytes_written != file->size) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800321 printf("short write of \"%s\" (%ld bytes of %ld) (%s)\n",
Doug Zongker1c43c972012-02-28 11:07:09 -0800322 filename, (long)bytes_written, (long)file->size,
Doug Zongkerc4351c72010-02-22 14:46:32 -0800323 strerror(errno));
324 close(fd);
325 return -1;
326 }
Michael Rungebe81e512014-10-29 12:42:15 -0700327 if (fsync(fd) != 0) {
328 printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
329 return -1;
330 }
331 if (close(fd) != 0) {
332 printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
333 return -1;
334 }
Doug Zongker512536a2010-02-17 16:11:44 -0800335
Doug Zongker1c43c972012-02-28 11:07:09 -0800336 if (chmod(filename, file->st.st_mode) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800337 printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
338 return -1;
339 }
Doug Zongker1c43c972012-02-28 11:07:09 -0800340 if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800341 printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
342 return -1;
343 }
Doug Zongker512536a2010-02-17 16:11:44 -0800344
Doug Zongkerc4351c72010-02-22 14:46:32 -0800345 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800346}
347
Doug Zongkerf291d852010-07-07 13:55:25 -0700348// Write a memory buffer to 'target' partition, a string of the form
349// "MTD:<partition>[:...]" or "EMMC:<partition_device>:". Return 0 on
350// success.
351int WriteToPartition(unsigned char* data, size_t len,
352 const char* target) {
353 char* copy = strdup(target);
354 const char* magic = strtok(copy, ":");
355
356 enum PartitionType type;
357 if (strcmp(magic, "MTD") == 0) {
358 type = MTD;
359 } else if (strcmp(magic, "EMMC") == 0) {
360 type = EMMC;
361 } else {
362 printf("WriteToPartition called with bad target (%s)\n", target);
363 return -1;
364 }
365 const char* partition = strtok(NULL, ":");
366
Doug Zongkerc4351c72010-02-22 14:46:32 -0800367 if (partition == NULL) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700368 printf("bad partition target name \"%s\"\n", target);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800369 return -1;
370 }
Doug Zongker512536a2010-02-17 16:11:44 -0800371
Doug Zongkerf291d852010-07-07 13:55:25 -0700372 switch (type) {
373 case MTD:
374 if (!mtd_partitions_scanned) {
375 mtd_scan_partitions();
376 mtd_partitions_scanned = 1;
377 }
378
379 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
380 if (mtd == NULL) {
381 printf("mtd partition \"%s\" not found for writing\n",
382 partition);
383 return -1;
384 }
385
386 MtdWriteContext* ctx = mtd_write_partition(mtd);
387 if (ctx == NULL) {
388 printf("failed to init mtd partition \"%s\" for writing\n",
389 partition);
390 return -1;
391 }
392
393 size_t written = mtd_write_data(ctx, (char*)data, len);
394 if (written != len) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700395 printf("only wrote %zu of %zu bytes to MTD %s\n",
Doug Zongkerf291d852010-07-07 13:55:25 -0700396 written, len, partition);
397 mtd_write_close(ctx);
398 return -1;
399 }
400
401 if (mtd_erase_blocks(ctx, -1) < 0) {
402 printf("error finishing mtd write of %s\n", partition);
403 mtd_write_close(ctx);
404 return -1;
405 }
406
407 if (mtd_write_close(ctx)) {
408 printf("error closing mtd write of %s\n", partition);
409 return -1;
410 }
411 break;
412
413 case EMMC:
Doug Zongker044a0b42013-07-08 09:42:54 -0700414 {
415 size_t start = 0;
416 int success = 0;
Michael Rungebe81e512014-10-29 12:42:15 -0700417 int fd = open(partition, O_RDWR | O_SYNC);
Doug Zongkerc870a992013-07-09 10:34:46 -0700418 if (fd < 0) {
Doug Zongker044a0b42013-07-08 09:42:54 -0700419 printf("failed to open %s: %s\n", partition, strerror(errno));
Doug Zongkerf291d852010-07-07 13:55:25 -0700420 return -1;
421 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700422 int attempt;
423
Doug Zongker168724c2013-12-19 15:16:57 -0800424 for (attempt = 0; attempt < 2; ++attempt) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700425 if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
426 printf("failed seek on %s: %s\n",
427 partition, strerror(errno));
428 return -1;
429 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700430 while (start < len) {
431 size_t to_write = len - start;
Doug Zongker168724c2013-12-19 15:16:57 -0800432 if (to_write > 1<<20) to_write = 1<<20;
Doug Zongker044a0b42013-07-08 09:42:54 -0700433
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700434 ssize_t written = TEMP_FAILURE_RETRY(write(fd, data+start, to_write));
435 if (written == -1) {
436 printf("failed write writing to %s: %s\n", partition, strerror(errno));
437 return -1;
Doug Zongker044a0b42013-07-08 09:42:54 -0700438 }
Doug Zongkerc870a992013-07-09 10:34:46 -0700439 start += written;
Doug Zongker044a0b42013-07-08 09:42:54 -0700440 }
Michael Rungebe81e512014-10-29 12:42:15 -0700441 if (fsync(fd) != 0) {
442 printf("failed to sync to %s (%s)\n",
443 partition, strerror(errno));
444 return -1;
445 }
446 if (close(fd) != 0) {
447 printf("failed to close %s (%s)\n",
448 partition, strerror(errno));
449 return -1;
450 }
451 fd = open(partition, O_RDONLY);
452 if (fd < 0) {
453 printf("failed to reopen %s for verify (%s)\n",
454 partition, strerror(errno));
455 return -1;
456 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700457
458 // drop caches so our subsequent verification read
459 // won't just be reading the cache.
460 sync();
Doug Zongkerc870a992013-07-09 10:34:46 -0700461 int dc = open("/proc/sys/vm/drop_caches", O_WRONLY);
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700462 if (TEMP_FAILURE_RETRY(write(dc, "3\n", 2)) == -1) {
463 printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
464 } else {
465 printf(" caches dropped\n");
466 }
Doug Zongkerc870a992013-07-09 10:34:46 -0700467 close(dc);
Doug Zongker044a0b42013-07-08 09:42:54 -0700468 sleep(1);
Doug Zongker044a0b42013-07-08 09:42:54 -0700469
470 // verify
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700471 if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
472 printf("failed to seek back to beginning of %s: %s\n",
473 partition, strerror(errno));
474 return -1;
475 }
Doug Zongker044a0b42013-07-08 09:42:54 -0700476 unsigned char buffer[4096];
477 start = len;
478 size_t p;
479 for (p = 0; p < len; p += sizeof(buffer)) {
480 size_t to_read = len - p;
481 if (to_read > sizeof(buffer)) to_read = sizeof(buffer);
482
Doug Zongkerc870a992013-07-09 10:34:46 -0700483 size_t so_far = 0;
484 while (so_far < to_read) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700485 ssize_t read_count =
486 TEMP_FAILURE_RETRY(read(fd, buffer+so_far, to_read-so_far));
487 if (read_count == -1) {
488 printf("verify read error %s at %zu: %s\n",
489 partition, p, strerror(errno));
490 return -1;
Doug Zongkerc870a992013-07-09 10:34:46 -0700491 }
Doug Zongkerbf4a69a2013-07-10 13:39:50 -0700492 if ((size_t)read_count < to_read) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700493 printf("short verify read %s at %zu: %zd %zu %s\n",
Doug Zongkerc870a992013-07-09 10:34:46 -0700494 partition, p, read_count, to_read, strerror(errno));
495 }
496 so_far += read_count;
Doug Zongker044a0b42013-07-08 09:42:54 -0700497 }
498
499 if (memcmp(buffer, data+p, to_read)) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700500 printf("verification failed starting at %zu\n", p);
Doug Zongker044a0b42013-07-08 09:42:54 -0700501 start = p;
502 break;
503 }
504 }
505
506 if (start == len) {
507 printf("verification read succeeded (attempt %d)\n", attempt+1);
508 success = true;
509 break;
510 }
511 }
512
513 if (!success) {
514 printf("failed to verify after all attempts\n");
515 return -1;
516 }
517
Doug Zongkerc870a992013-07-09 10:34:46 -0700518 if (close(fd) != 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -0700519 printf("error closing %s (%s)\n", partition, strerror(errno));
520 return -1;
521 }
Doug Zongkerbf4a69a2013-07-10 13:39:50 -0700522 sync();
Doug Zongkerf291d852010-07-07 13:55:25 -0700523 break;
Doug Zongker044a0b42013-07-08 09:42:54 -0700524 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800525 }
Doug Zongker512536a2010-02-17 16:11:44 -0800526
Doug Zongkerf291d852010-07-07 13:55:25 -0700527 free(copy);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800528 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800529}
530
531
532// Take a string 'str' of 40 hex digits and parse it into the 20
533// byte array 'digest'. 'str' may contain only the digest or be of
534// the form "<digest>:<anything>". Return 0 on success, -1 on any
535// error.
536int ParseSha1(const char* str, uint8_t* digest) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800537 int i;
538 const char* ps = str;
539 uint8_t* pd = digest;
540 for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) {
541 int digit;
542 if (*ps >= '0' && *ps <= '9') {
543 digit = *ps - '0';
544 } else if (*ps >= 'a' && *ps <= 'f') {
545 digit = *ps - 'a' + 10;
546 } else if (*ps >= 'A' && *ps <= 'F') {
547 digit = *ps - 'A' + 10;
548 } else {
549 return -1;
550 }
551 if (i % 2 == 0) {
552 *pd = digit << 4;
553 } else {
554 *pd |= digit;
555 ++pd;
556 }
Doug Zongker512536a2010-02-17 16:11:44 -0800557 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800558 if (*ps != '\0') return -1;
559 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800560}
561
Doug Zongkerc4351c72010-02-22 14:46:32 -0800562// Search an array of sha1 strings for one matching the given sha1.
563// Return the index of the match on success, or -1 if no match is
564// found.
Doug Zongker044a0b42013-07-08 09:42:54 -0700565int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
Doug Zongkerc4351c72010-02-22 14:46:32 -0800566 int num_patches) {
567 int i;
568 uint8_t patch_sha1[SHA_DIGEST_SIZE];
569 for (i = 0; i < num_patches; ++i) {
570 if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 &&
571 memcmp(patch_sha1, sha1, SHA_DIGEST_SIZE) == 0) {
572 return i;
573 }
Doug Zongker512536a2010-02-17 16:11:44 -0800574 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800575 return -1;
Doug Zongker512536a2010-02-17 16:11:44 -0800576}
577
578// Returns 0 if the contents of the file (argv[2]) or the cached file
579// match any of the sha1's on the command line (argv[3:]). Returns
580// nonzero otherwise.
Doug Zongkerc4351c72010-02-22 14:46:32 -0800581int applypatch_check(const char* filename,
582 int num_patches, char** const patch_sha1_str) {
583 FileContents file;
584 file.data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800585
Doug Zongkerc4351c72010-02-22 14:46:32 -0800586 // It's okay to specify no sha1s; the check will pass if the
Doug Zongkerf291d852010-07-07 13:55:25 -0700587 // LoadFileContents is successful. (Useful for reading
Doug Zongkerc4351c72010-02-22 14:46:32 -0800588 // partitions, where the filename encodes the sha1s; no need to
589 // check them twice.)
Doug Zongkera1bc1482014-02-13 15:18:19 -0800590 if (LoadFileContents(filename, &file) != 0 ||
Doug Zongkerc4351c72010-02-22 14:46:32 -0800591 (num_patches > 0 &&
592 FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) {
593 printf("file \"%s\" doesn't have any of expected "
594 "sha1 sums; checking cache\n", filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800595
Doug Zongkerc4351c72010-02-22 14:46:32 -0800596 free(file.data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800597 file.data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800598
Doug Zongkerc4351c72010-02-22 14:46:32 -0800599 // If the source file is missing or corrupted, it might be because
600 // we were killed in the middle of patching it. A copy of it
601 // should have been made in CACHE_TEMP_SOURCE. If that file
602 // exists and matches the sha1 we're looking for, the check still
603 // passes.
604
Doug Zongkera1bc1482014-02-13 15:18:19 -0800605 if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800606 printf("failed to load cache file\n");
607 return 1;
608 }
609
610 if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) {
611 printf("cache bits don't match any sha1 for \"%s\"\n", filename);
612 free(file.data);
613 return 1;
614 }
615 }
Doug Zongker512536a2010-02-17 16:11:44 -0800616
617 free(file.data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800618 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800619}
620
621int ShowLicenses() {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800622 ShowBSDiffLicense();
623 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800624}
625
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700626ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800627 int fd = *(int *)token;
628 ssize_t done = 0;
629 ssize_t wrote;
630 while (done < (ssize_t) len) {
Elliott Hughes2f5feed2015-04-28 17:24:24 -0700631 wrote = TEMP_FAILURE_RETRY(write(fd, data+done, len-done));
632 if (wrote == -1) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800633 printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno));
634 return done;
635 }
636 done += wrote;
Doug Zongker512536a2010-02-17 16:11:44 -0800637 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800638 return done;
Doug Zongker512536a2010-02-17 16:11:44 -0800639}
640
641typedef struct {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800642 unsigned char* buffer;
643 ssize_t size;
644 ssize_t pos;
Doug Zongker512536a2010-02-17 16:11:44 -0800645} MemorySinkInfo;
646
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700647ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800648 MemorySinkInfo* msi = (MemorySinkInfo*)token;
649 if (msi->size - msi->pos < len) {
650 return -1;
651 }
652 memcpy(msi->buffer + msi->pos, data, len);
653 msi->pos += len;
654 return len;
Doug Zongker512536a2010-02-17 16:11:44 -0800655}
656
657// Return the amount of free space (in bytes) on the filesystem
658// containing filename. filename must exist. Return -1 on error.
659size_t FreeSpaceForFile(const char* filename) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800660 struct statfs sf;
661 if (statfs(filename, &sf) != 0) {
662 printf("failed to statfs %s: %s\n", filename, strerror(errno));
663 return -1;
664 }
665 return sf.f_bsize * sf.f_bfree;
Doug Zongker512536a2010-02-17 16:11:44 -0800666}
667
Doug Zongkerc4351c72010-02-22 14:46:32 -0800668int CacheSizeCheck(size_t bytes) {
669 if (MakeFreeSpaceOnCache(bytes) < 0) {
670 printf("unable to make %ld bytes available on /cache\n", (long)bytes);
671 return 1;
672 } else {
673 return 0;
674 }
675}
676
Doug Zongkerbf80f492012-10-19 12:24:26 -0700677static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) {
678 int i;
679 const char* hex = "0123456789abcdef";
680 for (i = 0; i < 4; ++i) {
681 putchar(hex[(sha1[i]>>4) & 0xf]);
682 putchar(hex[sha1[i] & 0xf]);
683 }
684}
Doug Zongkerc4351c72010-02-22 14:46:32 -0800685
686// This function applies binary patches to files in a way that is safe
Doug Zongker512536a2010-02-17 16:11:44 -0800687// (the original file is not touched until we have the desired
688// replacement for it) and idempotent (it's okay to run this program
689// multiple times).
690//
Doug Zongkerc4351c72010-02-22 14:46:32 -0800691// - if the sha1 hash of <target_filename> is <target_sha1_string>,
692// does nothing and exits successfully.
Doug Zongker512536a2010-02-17 16:11:44 -0800693//
Doug Zongkerc4351c72010-02-22 14:46:32 -0800694// - otherwise, if the sha1 hash of <source_filename> is one of the
695// entries in <patch_sha1_str>, the corresponding patch from
696// <patch_data> (which must be a VAL_BLOB) is applied to produce a
697// new file (the type of patch is automatically detected from the
698// blob daat). If that new file has sha1 hash <target_sha1_str>,
699// moves it to replace <target_filename>, and exits successfully.
700// Note that if <source_filename> and <target_filename> are not the
701// same, <source_filename> is NOT deleted on success.
702// <target_filename> may be the string "-" to mean "the same as
703// source_filename".
Doug Zongker512536a2010-02-17 16:11:44 -0800704//
705// - otherwise, or if any error is encountered, exits with non-zero
706// status.
707//
Doug Zongkerf291d852010-07-07 13:55:25 -0700708// <source_filename> may refer to a partition to read the source data.
709// See the comments for the LoadPartition Contents() function above
Doug Zongkerc4351c72010-02-22 14:46:32 -0800710// for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800711
Doug Zongkerc4351c72010-02-22 14:46:32 -0800712int applypatch(const char* source_filename,
713 const char* target_filename,
714 const char* target_sha1_str,
715 size_t target_size,
716 int num_patches,
717 char** const patch_sha1_str,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700718 Value** patch_data,
719 Value* bonus_data) {
Doug Zongkerbf80f492012-10-19 12:24:26 -0700720 printf("patch %s: ", source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800721
Doug Zongkerc4351c72010-02-22 14:46:32 -0800722 if (target_filename[0] == '-' &&
723 target_filename[1] == '\0') {
724 target_filename = source_filename;
Doug Zongker512536a2010-02-17 16:11:44 -0800725 }
726
Doug Zongkerc4351c72010-02-22 14:46:32 -0800727 uint8_t target_sha1[SHA_DIGEST_SIZE];
728 if (ParseSha1(target_sha1_str, target_sha1) != 0) {
729 printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800730 return 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800731 }
Doug Zongker512536a2010-02-17 16:11:44 -0800732
Doug Zongkerc4351c72010-02-22 14:46:32 -0800733 FileContents copy_file;
734 FileContents source_file;
Doug Zongker1c43c972012-02-28 11:07:09 -0800735 copy_file.data = NULL;
736 source_file.data = NULL;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800737 const Value* source_patch_value = NULL;
738 const Value* copy_patch_value = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800739
Doug Zongkerc4351c72010-02-22 14:46:32 -0800740 // We try to load the target file into the source_file object.
Doug Zongkera1bc1482014-02-13 15:18:19 -0800741 if (LoadFileContents(target_filename, &source_file) == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800742 if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
743 // The early-exit case: the patch was already applied, this file
744 // has the desired hash, nothing for us to do.
Doug Zongkerbf80f492012-10-19 12:24:26 -0700745 printf("already ");
746 print_short_sha1(target_sha1);
747 putchar('\n');
Doug Zongker1c43c972012-02-28 11:07:09 -0800748 free(source_file.data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800749 return 0;
750 }
751 }
Doug Zongker512536a2010-02-17 16:11:44 -0800752
Doug Zongkerc4351c72010-02-22 14:46:32 -0800753 if (source_file.data == NULL ||
754 (target_filename != source_filename &&
755 strcmp(target_filename, source_filename) != 0)) {
756 // Need to load the source file: either we failed to load the
757 // target file, or we did but it's different from the source file.
758 free(source_file.data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800759 source_file.data = NULL;
Doug Zongkera1bc1482014-02-13 15:18:19 -0800760 LoadFileContents(source_filename, &source_file);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800761 }
762
763 if (source_file.data != NULL) {
764 int to_use = FindMatchingPatch(source_file.sha1,
765 patch_sha1_str, num_patches);
766 if (to_use >= 0) {
767 source_patch_value = patch_data[to_use];
768 }
769 }
770
771 if (source_patch_value == NULL) {
772 free(source_file.data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800773 source_file.data = NULL;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800774 printf("source file is bad; trying copy\n");
775
Doug Zongkera1bc1482014-02-13 15:18:19 -0800776 if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800777 // fail.
778 printf("failed to read copy file\n");
779 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800780 }
781
Doug Zongkerc4351c72010-02-22 14:46:32 -0800782 int to_use = FindMatchingPatch(copy_file.sha1,
783 patch_sha1_str, num_patches);
Doug Zongker8cd9e4f2010-08-12 17:38:09 -0700784 if (to_use >= 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800785 copy_patch_value = patch_data[to_use];
Doug Zongker512536a2010-02-17 16:11:44 -0800786 }
787
Doug Zongkerc4351c72010-02-22 14:46:32 -0800788 if (copy_patch_value == NULL) {
789 // fail.
790 printf("copy file doesn't match source SHA-1s either\n");
Doug Zongker1c43c972012-02-28 11:07:09 -0800791 free(copy_file.data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800792 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800793 }
Doug Zongker512536a2010-02-17 16:11:44 -0800794 }
795
Doug Zongker1c43c972012-02-28 11:07:09 -0800796 int result = GenerateTarget(&source_file, source_patch_value,
797 &copy_file, copy_patch_value,
798 source_filename, target_filename,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700799 target_sha1, target_size, bonus_data);
Doug Zongker1c43c972012-02-28 11:07:09 -0800800 free(source_file.data);
801 free(copy_file.data);
802
803 return result;
804}
805
806static int GenerateTarget(FileContents* source_file,
807 const Value* source_patch_value,
808 FileContents* copy_file,
809 const Value* copy_patch_value,
810 const char* source_filename,
811 const char* target_filename,
812 const uint8_t target_sha1[SHA_DIGEST_SIZE],
Doug Zongkera3ccba62012-08-20 15:28:02 -0700813 size_t target_size,
814 const Value* bonus_data) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800815 int retry = 1;
816 SHA_CTX ctx;
817 int output;
818 MemorySinkInfo msi;
819 FileContents* source_to_use;
820 char* outname;
Doug Zongker1c43c972012-02-28 11:07:09 -0800821 int made_copy = 0;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800822
823 // assume that target_filename (eg "/system/app/Foo.apk") is located
824 // on the same filesystem as its top-level directory ("/system").
825 // We need something that exists for calling statfs().
826 char target_fs[strlen(target_filename)+1];
827 char* slash = strchr(target_filename+1, '/');
828 if (slash != NULL) {
829 int count = slash - target_filename;
830 strncpy(target_fs, target_filename, count);
831 target_fs[count] = '\0';
Doug Zongker512536a2010-02-17 16:11:44 -0800832 } else {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800833 strcpy(target_fs, target_filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800834 }
835
Doug Zongkerc4351c72010-02-22 14:46:32 -0800836 do {
837 // Is there enough room in the target filesystem to hold the patched
838 // file?
839
Doug Zongkerf291d852010-07-07 13:55:25 -0700840 if (strncmp(target_filename, "MTD:", 4) == 0 ||
841 strncmp(target_filename, "EMMC:", 5) == 0) {
842 // If the target is a partition, we're actually going to
843 // write the output to /tmp and then copy it to the
844 // partition. statfs() always returns 0 blocks free for
845 // /tmp, so instead we'll just assume that /tmp has enough
846 // space to hold the file.
Doug Zongkerc4351c72010-02-22 14:46:32 -0800847
Doug Zongkerf291d852010-07-07 13:55:25 -0700848 // We still write the original source to cache, in case
849 // the partition write is interrupted.
Doug Zongker1c43c972012-02-28 11:07:09 -0800850 if (MakeFreeSpaceOnCache(source_file->size) < 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800851 printf("not enough free space on /cache\n");
852 return 1;
853 }
854 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
855 printf("failed to back up source file\n");
856 return 1;
857 }
858 made_copy = 1;
859 retry = 0;
860 } else {
861 int enough_space = 0;
862 if (retry > 0) {
863 size_t free_space = FreeSpaceForFile(target_fs);
Doug Zongker201cd462010-08-13 09:41:21 -0700864 enough_space =
865 (free_space > (256 << 10)) && // 256k (two-block) minimum
Doug Zongkerc4351c72010-02-22 14:46:32 -0800866 (free_space > (target_size * 3 / 2)); // 50% margin of error
Doug Zongkerbf80f492012-10-19 12:24:26 -0700867 if (!enough_space) {
868 printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n",
869 (long)target_size, (long)free_space, retry, enough_space);
870 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800871 }
872
873 if (!enough_space) {
874 retry = 0;
875 }
876
877 if (!enough_space && source_patch_value != NULL) {
878 // Using the original source, but not enough free space. First
879 // copy the source file to cache, then delete it from the original
880 // location.
881
Doug Zongkerf291d852010-07-07 13:55:25 -0700882 if (strncmp(source_filename, "MTD:", 4) == 0 ||
883 strncmp(source_filename, "EMMC:", 5) == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800884 // It's impossible to free space on the target filesystem by
Doug Zongkerf291d852010-07-07 13:55:25 -0700885 // deleting the source if the source is a partition. If
Doug Zongkerc4351c72010-02-22 14:46:32 -0800886 // we're ever in a state where we need to do this, fail.
Doug Zongkerf291d852010-07-07 13:55:25 -0700887 printf("not enough free space for target but source "
888 "is partition\n");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800889 return 1;
890 }
891
Doug Zongker1c43c972012-02-28 11:07:09 -0800892 if (MakeFreeSpaceOnCache(source_file->size) < 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800893 printf("not enough free space on /cache\n");
894 return 1;
895 }
896
897 if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
898 printf("failed to back up source file\n");
899 return 1;
900 }
901 made_copy = 1;
902 unlink(source_filename);
903
904 size_t free_space = FreeSpaceForFile(target_fs);
Doug Zongkerbf80f492012-10-19 12:24:26 -0700905 printf("(now %ld bytes free for target) ", (long)free_space);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800906 }
907 }
908
909 const Value* patch;
910 if (source_patch_value != NULL) {
Doug Zongker1c43c972012-02-28 11:07:09 -0800911 source_to_use = source_file;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800912 patch = source_patch_value;
913 } else {
Doug Zongker1c43c972012-02-28 11:07:09 -0800914 source_to_use = copy_file;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800915 patch = copy_patch_value;
916 }
917
918 if (patch->type != VAL_BLOB) {
919 printf("patch is not a blob\n");
920 return 1;
921 }
922
923 SinkFn sink = NULL;
924 void* token = NULL;
925 output = -1;
926 outname = NULL;
Doug Zongkerf291d852010-07-07 13:55:25 -0700927 if (strncmp(target_filename, "MTD:", 4) == 0 ||
928 strncmp(target_filename, "EMMC:", 5) == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800929 // We store the decoded output in memory.
930 msi.buffer = malloc(target_size);
931 if (msi.buffer == NULL) {
932 printf("failed to alloc %ld bytes for output\n",
933 (long)target_size);
934 return 1;
935 }
936 msi.pos = 0;
937 msi.size = target_size;
938 sink = MemorySink;
939 token = &msi;
940 } else {
941 // We write the decoded output to "<tgt-file>.patch".
942 outname = (char*)malloc(strlen(target_filename) + 10);
943 strcpy(outname, target_filename);
944 strcat(outname, ".patch");
945
Michael Rungebe81e512014-10-29 12:42:15 -0700946 output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
947 S_IRUSR | S_IWUSR);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800948 if (output < 0) {
949 printf("failed to open output file %s: %s\n",
950 outname, strerror(errno));
951 return 1;
952 }
953 sink = FileSink;
954 token = &output;
955 }
956
957 char* header = patch->data;
958 ssize_t header_bytes_read = patch->size;
959
960 SHA_init(&ctx);
961
962 int result;
963
964 if (header_bytes_read >= 8 &&
965 memcmp(header, "BSDIFF40", 8) == 0) {
966 result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
967 patch, 0, sink, token, &ctx);
968 } else if (header_bytes_read >= 8 &&
969 memcmp(header, "IMGDIFF2", 8) == 0) {
970 result = ApplyImagePatch(source_to_use->data, source_to_use->size,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700971 patch, sink, token, &ctx, bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800972 } else {
973 printf("Unknown patch file format\n");
974 return 1;
975 }
976
977 if (output >= 0) {
Michael Rungebe81e512014-10-29 12:42:15 -0700978 if (fsync(output) != 0) {
979 printf("failed to fsync file \"%s\" (%s)\n", outname, strerror(errno));
980 result = 1;
981 }
982 if (close(output) != 0) {
983 printf("failed to close file \"%s\" (%s)\n", outname, strerror(errno));
984 result = 1;
985 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800986 }
987
988 if (result != 0) {
989 if (retry == 0) {
990 printf("applying patch failed\n");
991 return result != 0;
992 } else {
993 printf("applying patch failed; retrying\n");
994 }
995 if (outname != NULL) {
996 unlink(outname);
997 }
998 } else {
999 // succeeded; no need to retry
1000 break;
1001 }
1002 } while (retry-- > 0);
1003
1004 const uint8_t* current_target_sha1 = SHA_final(&ctx);
1005 if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) {
1006 printf("patch did not produce expected sha1\n");
Doug Zongker512536a2010-02-17 16:11:44 -08001007 return 1;
Doug Zongkerbf80f492012-10-19 12:24:26 -07001008 } else {
1009 printf("now ");
1010 print_short_sha1(target_sha1);
1011 putchar('\n');
Doug Zongkerc4351c72010-02-22 14:46:32 -08001012 }
1013
1014 if (output < 0) {
Doug Zongkerf291d852010-07-07 13:55:25 -07001015 // Copy the temp file to the partition.
1016 if (WriteToPartition(msi.buffer, msi.pos, target_filename) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001017 printf("write of patched data to %s failed\n", target_filename);
1018 return 1;
1019 }
1020 free(msi.buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001021 } else {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001022 // Give the .patch file the same owner, group, and mode of the
1023 // original source file.
1024 if (chmod(outname, source_to_use->st.st_mode) != 0) {
1025 printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno));
1026 return 1;
1027 }
1028 if (chown(outname, source_to_use->st.st_uid,
1029 source_to_use->st.st_gid) != 0) {
1030 printf("chown of \"%s\" failed: %s\n", outname, strerror(errno));
1031 return 1;
1032 }
Doug Zongker512536a2010-02-17 16:11:44 -08001033
Doug Zongkerc4351c72010-02-22 14:46:32 -08001034 // Finally, rename the .patch file to replace the target file.
1035 if (rename(outname, target_filename) != 0) {
1036 printf("rename of .patch to \"%s\" failed: %s\n",
1037 target_filename, strerror(errno));
1038 return 1;
1039 }
Doug Zongker512536a2010-02-17 16:11:44 -08001040 }
1041
Doug Zongkerc4351c72010-02-22 14:46:32 -08001042 // If this run of applypatch created the copy, and we're here, we
1043 // can delete it.
1044 if (made_copy) unlink(CACHE_TEMP_SOURCE);
Doug Zongker512536a2010-02-17 16:11:44 -08001045
Doug Zongkerc4351c72010-02-22 14:46:32 -08001046 // Success!
1047 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -08001048}