Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 19 | #include <libgen.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/statfs.h> |
| 25 | #include <sys/types.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "mincrypt/sha.h" |
| 29 | #include "applypatch.h" |
| 30 | #include "mtdutils/mtdutils.h" |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 31 | #include "edify/expr.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 32 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 33 | static int LoadPartitionContents(const char* filename, FileContents* file); |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 34 | static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 35 | static int GenerateTarget(FileContents* source_file, |
| 36 | const Value* source_patch_value, |
| 37 | FileContents* copy_file, |
| 38 | const Value* copy_patch_value, |
| 39 | const char* source_filename, |
| 40 | const char* target_filename, |
| 41 | const uint8_t target_sha1[SHA_DIGEST_SIZE], |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 42 | size_t target_size, |
| 43 | const Value* bonus_data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 44 | |
| 45 | static int mtd_partitions_scanned = 0; |
| 46 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 47 | // Read a file into memory; store the file contents and associated |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 48 | // metadata in *file. |
| 49 | // |
| 50 | // Return 0 on success. |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 51 | int LoadFileContents(const char* filename, FileContents* file) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 52 | file->data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 53 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 54 | // A special 'filename' beginning with "MTD:" or "EMMC:" means to |
| 55 | // load the contents of a partition. |
| 56 | if (strncmp(filename, "MTD:", 4) == 0 || |
| 57 | strncmp(filename, "EMMC:", 5) == 0) { |
| 58 | return LoadPartitionContents(filename, file); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 59 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 60 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 61 | if (stat(filename, &file->st) != 0) { |
| 62 | printf("failed to stat \"%s\": %s\n", filename, strerror(errno)); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | file->size = file->st.st_size; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 67 | file->data = reinterpret_cast<unsigned char*>(malloc(file->size)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 68 | |
| 69 | FILE* f = fopen(filename, "rb"); |
| 70 | if (f == NULL) { |
| 71 | printf("failed to open \"%s\": %s\n", filename, strerror(errno)); |
| 72 | free(file->data); |
| 73 | file->data = NULL; |
| 74 | return -1; |
| 75 | } |
| 76 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 77 | size_t bytes_read = fread(file->data, 1, file->size, f); |
| 78 | if (bytes_read != static_cast<size_t>(file->size)) { |
| 79 | printf("short read of \"%s\" (%zu bytes of %zd)\n", filename, bytes_read, file->size); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 80 | free(file->data); |
| 81 | file->data = NULL; |
| 82 | return -1; |
| 83 | } |
| 84 | fclose(f); |
| 85 | |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 86 | SHA_hash(file->data, file->size, file->sha1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 87 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static size_t* size_array; |
| 91 | // comparison function for qsort()ing an int array of indexes into |
| 92 | // size_array[]. |
| 93 | static int compare_size_indices(const void* a, const void* b) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 94 | const int aa = *reinterpret_cast<const int*>(a); |
| 95 | const int bb = *reinterpret_cast<const int*>(b); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 96 | if (size_array[aa] < size_array[bb]) { |
| 97 | return -1; |
| 98 | } else if (size_array[aa] > size_array[bb]) { |
| 99 | return 1; |
| 100 | } else { |
| 101 | return 0; |
| 102 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 105 | // Load the contents of an MTD or EMMC partition into the provided |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 106 | // FileContents. filename should be a string of the form |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 107 | // "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or |
| 108 | // "EMMC:<partition_device>:..."). The smallest size_n bytes for |
| 109 | // which that prefix of the partition contents has the corresponding |
| 110 | // sha1 hash will be loaded. It is acceptable for a size value to be |
| 111 | // repeated with different sha1s. Will return 0 on success. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 112 | // |
| 113 | // This complexity is needed because if an OTA installation is |
| 114 | // interrupted, the partition might contain either the source or the |
| 115 | // target data, which might be of different lengths. We need to know |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 116 | // the length in order to read from a partition (there is no |
| 117 | // "end-of-file" marker), so the caller must specify the possible |
| 118 | // lengths and the hash of the data, and we'll do the load expecting |
| 119 | // to find one of those hashes. |
| 120 | enum PartitionType { MTD, EMMC }; |
| 121 | |
| 122 | static int LoadPartitionContents(const char* filename, FileContents* file) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 123 | char* copy = strdup(filename); |
| 124 | const char* magic = strtok(copy, ":"); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 125 | |
| 126 | enum PartitionType type; |
| 127 | |
| 128 | if (strcmp(magic, "MTD") == 0) { |
| 129 | type = MTD; |
| 130 | } else if (strcmp(magic, "EMMC") == 0) { |
| 131 | type = EMMC; |
| 132 | } else { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 133 | printf("LoadPartitionContents called with bad filename (%s)\n", filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 134 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 135 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 136 | const char* partition = strtok(NULL, ":"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 137 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 138 | int i; |
| 139 | int colons = 0; |
| 140 | for (i = 0; filename[i] != '\0'; ++i) { |
| 141 | if (filename[i] == ':') { |
| 142 | ++colons; |
| 143 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 144 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 145 | if (colons < 3 || colons%2 == 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 146 | printf("LoadPartitionContents called with bad filename (%s)\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 147 | filename); |
| 148 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 149 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 150 | int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 151 | int* index = reinterpret_cast<int*>(malloc(pairs * sizeof(int))); |
| 152 | size_t* size = reinterpret_cast<size_t*>(malloc(pairs * sizeof(size_t))); |
| 153 | char** sha1sum = reinterpret_cast<char**>(malloc(pairs * sizeof(char*))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 154 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 155 | for (i = 0; i < pairs; ++i) { |
| 156 | const char* size_str = strtok(NULL, ":"); |
| 157 | size[i] = strtol(size_str, NULL, 10); |
| 158 | if (size[i] == 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 159 | printf("LoadPartitionContents called with bad size (%s)\n", filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 160 | return -1; |
| 161 | } |
| 162 | sha1sum[i] = strtok(NULL, ":"); |
| 163 | index[i] = i; |
| 164 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 165 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 166 | // sort the index[] array so it indexes the pairs in order of |
| 167 | // increasing size. |
| 168 | size_array = size; |
| 169 | qsort(index, pairs, sizeof(int), compare_size_indices); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 170 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 171 | MtdReadContext* ctx = NULL; |
| 172 | FILE* dev = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 173 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 174 | switch (type) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 175 | case MTD: { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 176 | if (!mtd_partitions_scanned) { |
| 177 | mtd_scan_partitions(); |
| 178 | mtd_partitions_scanned = 1; |
| 179 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 180 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 181 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 182 | if (mtd == NULL) { |
| 183 | printf("mtd partition \"%s\" not found (loading %s)\n", |
| 184 | partition, filename); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | ctx = mtd_read_partition(mtd); |
| 189 | if (ctx == NULL) { |
| 190 | printf("failed to initialize read of mtd partition \"%s\"\n", |
| 191 | partition); |
| 192 | return -1; |
| 193 | } |
| 194 | break; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 195 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 196 | |
| 197 | case EMMC: |
| 198 | dev = fopen(partition, "rb"); |
| 199 | if (dev == NULL) { |
| 200 | printf("failed to open emmc partition \"%s\": %s\n", |
| 201 | partition, strerror(errno)); |
| 202 | return -1; |
| 203 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 204 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 205 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 206 | SHA_CTX sha_ctx; |
| 207 | SHA_init(&sha_ctx); |
| 208 | uint8_t parsed_sha[SHA_DIGEST_SIZE]; |
| 209 | |
| 210 | // allocate enough memory to hold the largest size. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 211 | file->data = reinterpret_cast<unsigned char*>(malloc(size[index[pairs-1]])); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 212 | char* p = (char*)file->data; |
| 213 | file->size = 0; // # bytes read so far |
| 214 | |
| 215 | for (i = 0; i < pairs; ++i) { |
| 216 | // Read enough additional bytes to get us up to the next size |
| 217 | // (again, we're trying the possibilities in order of increasing |
| 218 | // size). |
| 219 | size_t next = size[index[i]] - file->size; |
| 220 | size_t read = 0; |
| 221 | if (next > 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 222 | switch (type) { |
| 223 | case MTD: |
| 224 | read = mtd_read_data(ctx, p, next); |
| 225 | break; |
| 226 | |
| 227 | case EMMC: |
| 228 | read = fread(p, 1, next, dev); |
| 229 | break; |
| 230 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 231 | if (next != read) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 232 | printf("short read (%zu bytes of %zu) for partition \"%s\"\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 233 | read, next, partition); |
| 234 | free(file->data); |
| 235 | file->data = NULL; |
| 236 | return -1; |
| 237 | } |
| 238 | SHA_update(&sha_ctx, p, read); |
| 239 | file->size += read; |
| 240 | } |
| 241 | |
| 242 | // Duplicate the SHA context and finalize the duplicate so we can |
| 243 | // check it against this pair's expected hash. |
| 244 | SHA_CTX temp_ctx; |
| 245 | memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX)); |
| 246 | const uint8_t* sha_so_far = SHA_final(&temp_ctx); |
| 247 | |
| 248 | if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 249 | printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]], filename); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 250 | free(file->data); |
| 251 | file->data = NULL; |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) { |
| 256 | // we have a match. stop reading the partition; we'll return |
| 257 | // the data we've read so far. |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 258 | printf("partition read matched size %zu sha %s\n", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 259 | size[index[i]], sha1sum[index[i]]); |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | p += read; |
| 264 | } |
| 265 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 266 | switch (type) { |
| 267 | case MTD: |
| 268 | mtd_read_close(ctx); |
| 269 | break; |
| 270 | |
| 271 | case EMMC: |
| 272 | fclose(dev); |
| 273 | break; |
| 274 | } |
| 275 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 276 | |
| 277 | if (i == pairs) { |
| 278 | // Ran off the end of the list of (size,sha1) pairs without |
| 279 | // finding a match. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 280 | printf("contents of partition \"%s\" didn't match %s\n", partition, filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 281 | free(file->data); |
| 282 | file->data = NULL; |
| 283 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 286 | const uint8_t* sha_final = SHA_final(&sha_ctx); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 287 | for (size_t i = 0; i < SHA_DIGEST_SIZE; ++i) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 288 | file->sha1[i] = sha_final[i]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 291 | // Fake some stat() info. |
| 292 | file->st.st_mode = 0644; |
| 293 | file->st.st_uid = 0; |
| 294 | file->st.st_gid = 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 295 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 296 | free(copy); |
| 297 | free(index); |
| 298 | free(size); |
| 299 | free(sha1sum); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 300 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 301 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | // Save the contents of the given FileContents object under the given |
| 306 | // filename. Return 0 on success. |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 307 | int SaveFileContents(const char* filename, const FileContents* file) { |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 308 | int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 309 | if (fd < 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 310 | printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 311 | return -1; |
| 312 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 313 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 314 | ssize_t bytes_written = FileSink(file->data, file->size, &fd); |
| 315 | if (bytes_written != file->size) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 316 | printf("short write of \"%s\" (%zd bytes of %zd) (%s)\n", |
| 317 | filename, bytes_written, file->size, strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 318 | close(fd); |
| 319 | return -1; |
| 320 | } |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 321 | if (fsync(fd) != 0) { |
| 322 | printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 323 | return -1; |
| 324 | } |
| 325 | if (close(fd) != 0) { |
| 326 | printf("close of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 327 | return -1; |
| 328 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 329 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 330 | if (chmod(filename, file->st.st_mode) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 331 | printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 332 | return -1; |
| 333 | } |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 334 | if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 335 | printf("chown of \"%s\" failed: %s\n", filename, strerror(errno)); |
| 336 | return -1; |
| 337 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 338 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 339 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 342 | // Write a memory buffer to 'target' partition, a string of the form |
| 343 | // "MTD:<partition>[:...]" or "EMMC:<partition_device>:". Return 0 on |
| 344 | // success. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 345 | int WriteToPartition(unsigned char* data, size_t len, const char* target) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 346 | char* copy = strdup(target); |
| 347 | const char* magic = strtok(copy, ":"); |
| 348 | |
| 349 | enum PartitionType type; |
| 350 | if (strcmp(magic, "MTD") == 0) { |
| 351 | type = MTD; |
| 352 | } else if (strcmp(magic, "EMMC") == 0) { |
| 353 | type = EMMC; |
| 354 | } else { |
| 355 | printf("WriteToPartition called with bad target (%s)\n", target); |
| 356 | return -1; |
| 357 | } |
| 358 | const char* partition = strtok(NULL, ":"); |
| 359 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 360 | if (partition == NULL) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 361 | printf("bad partition target name \"%s\"\n", target); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 362 | return -1; |
| 363 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 364 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 365 | switch (type) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 366 | case MTD: { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 367 | if (!mtd_partitions_scanned) { |
| 368 | mtd_scan_partitions(); |
| 369 | mtd_partitions_scanned = 1; |
| 370 | } |
| 371 | |
| 372 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 373 | if (mtd == NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 374 | printf("mtd partition \"%s\" not found for writing\n", partition); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 379 | if (ctx == NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 380 | printf("failed to init mtd partition \"%s\" for writing\n", partition); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 381 | return -1; |
| 382 | } |
| 383 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 384 | size_t written = mtd_write_data(ctx, reinterpret_cast<char*>(data), len); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 385 | if (written != len) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 386 | printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 387 | mtd_write_close(ctx); |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | if (mtd_erase_blocks(ctx, -1) < 0) { |
| 392 | printf("error finishing mtd write of %s\n", partition); |
| 393 | mtd_write_close(ctx); |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | if (mtd_write_close(ctx)) { |
| 398 | printf("error closing mtd write of %s\n", partition); |
| 399 | return -1; |
| 400 | } |
| 401 | break; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 402 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 403 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 404 | case EMMC: { |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 405 | size_t start = 0; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 406 | bool success = false; |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 407 | int fd = open(partition, O_RDWR | O_SYNC); |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 408 | if (fd < 0) { |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 409 | printf("failed to open %s: %s\n", partition, strerror(errno)); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 410 | return -1; |
| 411 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 412 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 413 | for (int attempt = 0; attempt < 2; ++attempt) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 414 | if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 415 | printf("failed seek on %s: %s\n", partition, strerror(errno)); |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 416 | return -1; |
| 417 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 418 | while (start < len) { |
| 419 | size_t to_write = len - start; |
Doug Zongker | 168724c | 2013-12-19 15:16:57 -0800 | [diff] [blame] | 420 | if (to_write > 1<<20) to_write = 1<<20; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 421 | |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 422 | ssize_t written = TEMP_FAILURE_RETRY(write(fd, data+start, to_write)); |
| 423 | if (written == -1) { |
| 424 | printf("failed write writing to %s: %s\n", partition, strerror(errno)); |
| 425 | return -1; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 426 | } |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 427 | start += written; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 428 | } |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 429 | if (fsync(fd) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 430 | printf("failed to sync to %s (%s)\n", partition, strerror(errno)); |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 431 | return -1; |
| 432 | } |
| 433 | if (close(fd) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 434 | printf("failed to close %s (%s)\n", partition, strerror(errno)); |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 435 | return -1; |
| 436 | } |
| 437 | fd = open(partition, O_RDONLY); |
| 438 | if (fd < 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 439 | printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno)); |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 440 | return -1; |
| 441 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 442 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 443 | // Drop caches so our subsequent verification read |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 444 | // won't just be reading the cache. |
| 445 | sync(); |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 446 | int dc = open("/proc/sys/vm/drop_caches", O_WRONLY); |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 447 | if (TEMP_FAILURE_RETRY(write(dc, "3\n", 2)) == -1) { |
| 448 | printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno)); |
| 449 | } else { |
| 450 | printf(" caches dropped\n"); |
| 451 | } |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 452 | close(dc); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 453 | sleep(1); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 454 | |
| 455 | // verify |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 456 | if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) { |
| 457 | printf("failed to seek back to beginning of %s: %s\n", |
| 458 | partition, strerror(errno)); |
| 459 | return -1; |
| 460 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 461 | unsigned char buffer[4096]; |
| 462 | start = len; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 463 | for (size_t p = 0; p < len; p += sizeof(buffer)) { |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 464 | size_t to_read = len - p; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 465 | if (to_read > sizeof(buffer)) { |
| 466 | to_read = sizeof(buffer); |
| 467 | } |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 468 | |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 469 | size_t so_far = 0; |
| 470 | while (so_far < to_read) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 471 | ssize_t read_count = |
| 472 | TEMP_FAILURE_RETRY(read(fd, buffer+so_far, to_read-so_far)); |
| 473 | if (read_count == -1) { |
| 474 | printf("verify read error %s at %zu: %s\n", |
| 475 | partition, p, strerror(errno)); |
| 476 | return -1; |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 477 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 478 | if (static_cast<size_t>(read_count) < to_read) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 479 | printf("short verify read %s at %zu: %zd %zu %s\n", |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 480 | partition, p, read_count, to_read, strerror(errno)); |
| 481 | } |
| 482 | so_far += read_count; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 485 | if (memcmp(buffer, data+p, to_read) != 0) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 486 | printf("verification failed starting at %zu\n", p); |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 487 | start = p; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (start == len) { |
| 493 | printf("verification read succeeded (attempt %d)\n", attempt+1); |
| 494 | success = true; |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (!success) { |
| 500 | printf("failed to verify after all attempts\n"); |
| 501 | return -1; |
| 502 | } |
| 503 | |
Doug Zongker | c870a99 | 2013-07-09 10:34:46 -0700 | [diff] [blame] | 504 | if (close(fd) != 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 505 | printf("error closing %s (%s)\n", partition, strerror(errno)); |
| 506 | return -1; |
| 507 | } |
Doug Zongker | bf4a69a | 2013-07-10 13:39:50 -0700 | [diff] [blame] | 508 | sync(); |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 509 | break; |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 510 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 511 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 512 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 513 | free(copy); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 514 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
| 518 | // Take a string 'str' of 40 hex digits and parse it into the 20 |
| 519 | // byte array 'digest'. 'str' may contain only the digest or be of |
| 520 | // the form "<digest>:<anything>". Return 0 on success, -1 on any |
| 521 | // error. |
| 522 | int ParseSha1(const char* str, uint8_t* digest) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 523 | const char* ps = str; |
| 524 | uint8_t* pd = digest; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 525 | for (int i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 526 | int digit; |
| 527 | if (*ps >= '0' && *ps <= '9') { |
| 528 | digit = *ps - '0'; |
| 529 | } else if (*ps >= 'a' && *ps <= 'f') { |
| 530 | digit = *ps - 'a' + 10; |
| 531 | } else if (*ps >= 'A' && *ps <= 'F') { |
| 532 | digit = *ps - 'A' + 10; |
| 533 | } else { |
| 534 | return -1; |
| 535 | } |
| 536 | if (i % 2 == 0) { |
| 537 | *pd = digit << 4; |
| 538 | } else { |
| 539 | *pd |= digit; |
| 540 | ++pd; |
| 541 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 542 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 543 | if (*ps != '\0') return -1; |
| 544 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 547 | // Search an array of sha1 strings for one matching the given sha1. |
| 548 | // Return the index of the match on success, or -1 if no match is |
| 549 | // found. |
Doug Zongker | 044a0b4 | 2013-07-08 09:42:54 -0700 | [diff] [blame] | 550 | int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 551 | int num_patches) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 552 | uint8_t patch_sha1[SHA_DIGEST_SIZE]; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 553 | for (int i = 0; i < num_patches; ++i) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 554 | if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 && |
| 555 | memcmp(patch_sha1, sha1, SHA_DIGEST_SIZE) == 0) { |
| 556 | return i; |
| 557 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 558 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 559 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | // Returns 0 if the contents of the file (argv[2]) or the cached file |
| 563 | // match any of the sha1's on the command line (argv[3:]). Returns |
| 564 | // nonzero otherwise. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 565 | int applypatch_check(const char* filename, int num_patches, |
| 566 | char** const patch_sha1_str) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 567 | FileContents file; |
| 568 | file.data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 569 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 570 | // It's okay to specify no sha1s; the check will pass if the |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 571 | // LoadFileContents is successful. (Useful for reading |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 572 | // partitions, where the filename encodes the sha1s; no need to |
| 573 | // check them twice.) |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 574 | if (LoadFileContents(filename, &file) != 0 || |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 575 | (num_patches > 0 && |
| 576 | FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) { |
| 577 | printf("file \"%s\" doesn't have any of expected " |
| 578 | "sha1 sums; checking cache\n", filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 579 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 580 | free(file.data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 581 | file.data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 582 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 583 | // If the source file is missing or corrupted, it might be because |
| 584 | // we were killed in the middle of patching it. A copy of it |
| 585 | // should have been made in CACHE_TEMP_SOURCE. If that file |
| 586 | // exists and matches the sha1 we're looking for, the check still |
| 587 | // passes. |
| 588 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 589 | if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 590 | printf("failed to load cache file\n"); |
| 591 | return 1; |
| 592 | } |
| 593 | |
| 594 | if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) { |
| 595 | printf("cache bits don't match any sha1 for \"%s\"\n", filename); |
| 596 | free(file.data); |
| 597 | return 1; |
| 598 | } |
| 599 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 600 | |
| 601 | free(file.data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 602 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | int ShowLicenses() { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 606 | ShowBSDiffLicense(); |
| 607 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 610 | ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 611 | int fd = *reinterpret_cast<int *>(token); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 612 | ssize_t done = 0; |
| 613 | ssize_t wrote; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 614 | while (done < len) { |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 615 | wrote = TEMP_FAILURE_RETRY(write(fd, data+done, len-done)); |
| 616 | if (wrote == -1) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 617 | printf("error writing %zd bytes: %s\n", (len-done), strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 618 | return done; |
| 619 | } |
| 620 | done += wrote; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 621 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 622 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | typedef struct { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 626 | unsigned char* buffer; |
| 627 | ssize_t size; |
| 628 | ssize_t pos; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 629 | } MemorySinkInfo; |
| 630 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 631 | ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 632 | MemorySinkInfo* msi = reinterpret_cast<MemorySinkInfo*>(token); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 633 | if (msi->size - msi->pos < len) { |
| 634 | return -1; |
| 635 | } |
| 636 | memcpy(msi->buffer + msi->pos, data, len); |
| 637 | msi->pos += len; |
| 638 | return len; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Return the amount of free space (in bytes) on the filesystem |
| 642 | // containing filename. filename must exist. Return -1 on error. |
| 643 | size_t FreeSpaceForFile(const char* filename) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 644 | struct statfs sf; |
| 645 | if (statfs(filename, &sf) != 0) { |
| 646 | printf("failed to statfs %s: %s\n", filename, strerror(errno)); |
| 647 | return -1; |
| 648 | } |
caozhiyuan | 3b49776 | 2015-05-19 17:21:00 +0800 | [diff] [blame] | 649 | return sf.f_bsize * sf.f_bavail; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 652 | int CacheSizeCheck(size_t bytes) { |
| 653 | if (MakeFreeSpaceOnCache(bytes) < 0) { |
| 654 | printf("unable to make %ld bytes available on /cache\n", (long)bytes); |
| 655 | return 1; |
| 656 | } else { |
| 657 | return 0; |
| 658 | } |
| 659 | } |
| 660 | |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 661 | static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 662 | const char* hex = "0123456789abcdef"; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 663 | for (size_t i = 0; i < 4; ++i) { |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 664 | putchar(hex[(sha1[i]>>4) & 0xf]); |
| 665 | putchar(hex[sha1[i] & 0xf]); |
| 666 | } |
| 667 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 668 | |
| 669 | // This function applies binary patches to files in a way that is safe |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 670 | // (the original file is not touched until we have the desired |
| 671 | // replacement for it) and idempotent (it's okay to run this program |
| 672 | // multiple times). |
| 673 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 674 | // - if the sha1 hash of <target_filename> is <target_sha1_string>, |
| 675 | // does nothing and exits successfully. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 676 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 677 | // - otherwise, if the sha1 hash of <source_filename> is one of the |
| 678 | // entries in <patch_sha1_str>, the corresponding patch from |
| 679 | // <patch_data> (which must be a VAL_BLOB) is applied to produce a |
| 680 | // new file (the type of patch is automatically detected from the |
| 681 | // blob daat). If that new file has sha1 hash <target_sha1_str>, |
| 682 | // moves it to replace <target_filename>, and exits successfully. |
| 683 | // Note that if <source_filename> and <target_filename> are not the |
| 684 | // same, <source_filename> is NOT deleted on success. |
| 685 | // <target_filename> may be the string "-" to mean "the same as |
| 686 | // source_filename". |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 687 | // |
| 688 | // - otherwise, or if any error is encountered, exits with non-zero |
| 689 | // status. |
| 690 | // |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 691 | // <source_filename> may refer to a partition to read the source data. |
| 692 | // See the comments for the LoadPartition Contents() function above |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 693 | // for the format of such a filename. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 694 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 695 | int applypatch(const char* source_filename, |
| 696 | const char* target_filename, |
| 697 | const char* target_sha1_str, |
| 698 | size_t target_size, |
| 699 | int num_patches, |
| 700 | char** const patch_sha1_str, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 701 | Value** patch_data, |
| 702 | Value* bonus_data) { |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 703 | printf("patch %s: ", source_filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 704 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 705 | if (target_filename[0] == '-' && target_filename[1] == '\0') { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 706 | target_filename = source_filename; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 709 | uint8_t target_sha1[SHA_DIGEST_SIZE]; |
| 710 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
| 711 | printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 712 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 713 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 714 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 715 | FileContents copy_file; |
| 716 | FileContents source_file; |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 717 | copy_file.data = NULL; |
| 718 | source_file.data = NULL; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 719 | const Value* source_patch_value = NULL; |
| 720 | const Value* copy_patch_value = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 721 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 722 | // We try to load the target file into the source_file object. |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 723 | if (LoadFileContents(target_filename, &source_file) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 724 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { |
| 725 | // The early-exit case: the patch was already applied, this file |
| 726 | // has the desired hash, nothing for us to do. |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 727 | printf("already "); |
| 728 | print_short_sha1(target_sha1); |
| 729 | putchar('\n'); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 730 | free(source_file.data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 731 | return 0; |
| 732 | } |
| 733 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 734 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 735 | if (source_file.data == NULL || |
| 736 | (target_filename != source_filename && |
| 737 | strcmp(target_filename, source_filename) != 0)) { |
| 738 | // Need to load the source file: either we failed to load the |
| 739 | // target file, or we did but it's different from the source file. |
| 740 | free(source_file.data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 741 | source_file.data = NULL; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 742 | LoadFileContents(source_filename, &source_file); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | if (source_file.data != NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 746 | int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 747 | if (to_use >= 0) { |
| 748 | source_patch_value = patch_data[to_use]; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (source_patch_value == NULL) { |
| 753 | free(source_file.data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 754 | source_file.data = NULL; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 755 | printf("source file is bad; trying copy\n"); |
| 756 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 757 | if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 758 | // fail. |
| 759 | printf("failed to read copy file\n"); |
| 760 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 763 | int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches); |
Doug Zongker | 8cd9e4f | 2010-08-12 17:38:09 -0700 | [diff] [blame] | 764 | if (to_use >= 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 765 | copy_patch_value = patch_data[to_use]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 768 | if (copy_patch_value == NULL) { |
| 769 | // fail. |
| 770 | printf("copy file doesn't match source SHA-1s either\n"); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 771 | free(copy_file.data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 772 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 773 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 776 | int result = GenerateTarget(&source_file, source_patch_value, |
| 777 | ©_file, copy_patch_value, |
| 778 | source_filename, target_filename, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 779 | target_sha1, target_size, bonus_data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 780 | free(source_file.data); |
| 781 | free(copy_file.data); |
| 782 | |
| 783 | return result; |
| 784 | } |
| 785 | |
| 786 | static int GenerateTarget(FileContents* source_file, |
| 787 | const Value* source_patch_value, |
| 788 | FileContents* copy_file, |
| 789 | const Value* copy_patch_value, |
| 790 | const char* source_filename, |
| 791 | const char* target_filename, |
| 792 | const uint8_t target_sha1[SHA_DIGEST_SIZE], |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 793 | size_t target_size, |
| 794 | const Value* bonus_data) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 795 | int retry = 1; |
| 796 | SHA_CTX ctx; |
| 797 | int output; |
| 798 | MemorySinkInfo msi; |
| 799 | FileContents* source_to_use; |
| 800 | char* outname; |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 801 | int made_copy = 0; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 802 | |
| 803 | // assume that target_filename (eg "/system/app/Foo.apk") is located |
| 804 | // on the same filesystem as its top-level directory ("/system"). |
| 805 | // We need something that exists for calling statfs(). |
| 806 | char target_fs[strlen(target_filename)+1]; |
| 807 | char* slash = strchr(target_filename+1, '/'); |
| 808 | if (slash != NULL) { |
| 809 | int count = slash - target_filename; |
| 810 | strncpy(target_fs, target_filename, count); |
| 811 | target_fs[count] = '\0'; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 812 | } else { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 813 | strcpy(target_fs, target_filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 816 | do { |
| 817 | // Is there enough room in the target filesystem to hold the patched |
| 818 | // file? |
| 819 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 820 | if (strncmp(target_filename, "MTD:", 4) == 0 || |
| 821 | strncmp(target_filename, "EMMC:", 5) == 0) { |
| 822 | // If the target is a partition, we're actually going to |
| 823 | // write the output to /tmp and then copy it to the |
| 824 | // partition. statfs() always returns 0 blocks free for |
| 825 | // /tmp, so instead we'll just assume that /tmp has enough |
| 826 | // space to hold the file. |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 827 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 828 | // We still write the original source to cache, in case |
| 829 | // the partition write is interrupted. |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 830 | if (MakeFreeSpaceOnCache(source_file->size) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 831 | printf("not enough free space on /cache\n"); |
| 832 | return 1; |
| 833 | } |
| 834 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 835 | printf("failed to back up source file\n"); |
| 836 | return 1; |
| 837 | } |
| 838 | made_copy = 1; |
| 839 | retry = 0; |
| 840 | } else { |
| 841 | int enough_space = 0; |
| 842 | if (retry > 0) { |
| 843 | size_t free_space = FreeSpaceForFile(target_fs); |
Doug Zongker | 201cd46 | 2010-08-13 09:41:21 -0700 | [diff] [blame] | 844 | enough_space = |
| 845 | (free_space > (256 << 10)) && // 256k (two-block) minimum |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 846 | (free_space > (target_size * 3 / 2)); // 50% margin of error |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 847 | if (!enough_space) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 848 | printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n", |
| 849 | target_size, free_space, retry, enough_space); |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 850 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if (!enough_space) { |
| 854 | retry = 0; |
| 855 | } |
| 856 | |
| 857 | if (!enough_space && source_patch_value != NULL) { |
| 858 | // Using the original source, but not enough free space. First |
| 859 | // copy the source file to cache, then delete it from the original |
| 860 | // location. |
| 861 | |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 862 | if (strncmp(source_filename, "MTD:", 4) == 0 || |
| 863 | strncmp(source_filename, "EMMC:", 5) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 864 | // It's impossible to free space on the target filesystem by |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 865 | // deleting the source if the source is a partition. If |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 866 | // we're ever in a state where we need to do this, fail. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 867 | printf("not enough free space for target but source is partition\n"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 868 | return 1; |
| 869 | } |
| 870 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 871 | if (MakeFreeSpaceOnCache(source_file->size) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 872 | printf("not enough free space on /cache\n"); |
| 873 | return 1; |
| 874 | } |
| 875 | |
| 876 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 877 | printf("failed to back up source file\n"); |
| 878 | return 1; |
| 879 | } |
| 880 | made_copy = 1; |
| 881 | unlink(source_filename); |
| 882 | |
| 883 | size_t free_space = FreeSpaceForFile(target_fs); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 884 | printf("(now %zu bytes free for target) ", free_space); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
| 888 | const Value* patch; |
| 889 | if (source_patch_value != NULL) { |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 890 | source_to_use = source_file; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 891 | patch = source_patch_value; |
| 892 | } else { |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 893 | source_to_use = copy_file; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 894 | patch = copy_patch_value; |
| 895 | } |
| 896 | |
| 897 | if (patch->type != VAL_BLOB) { |
| 898 | printf("patch is not a blob\n"); |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | SinkFn sink = NULL; |
| 903 | void* token = NULL; |
| 904 | output = -1; |
| 905 | outname = NULL; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 906 | if (strncmp(target_filename, "MTD:", 4) == 0 || |
| 907 | strncmp(target_filename, "EMMC:", 5) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 908 | // We store the decoded output in memory. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 909 | msi.buffer = reinterpret_cast<unsigned char*>(malloc(target_size)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 910 | if (msi.buffer == NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 911 | printf("failed to alloc %zu bytes for output\n", target_size); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 912 | return 1; |
| 913 | } |
| 914 | msi.pos = 0; |
| 915 | msi.size = target_size; |
| 916 | sink = MemorySink; |
| 917 | token = &msi; |
| 918 | } else { |
| 919 | // We write the decoded output to "<tgt-file>.patch". |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 920 | outname = reinterpret_cast<char*>(malloc(strlen(target_filename) + 10)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 921 | strcpy(outname, target_filename); |
| 922 | strcat(outname, ".patch"); |
| 923 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 924 | output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 925 | if (output < 0) { |
| 926 | printf("failed to open output file %s: %s\n", |
| 927 | outname, strerror(errno)); |
| 928 | return 1; |
| 929 | } |
| 930 | sink = FileSink; |
| 931 | token = &output; |
| 932 | } |
| 933 | |
| 934 | char* header = patch->data; |
| 935 | ssize_t header_bytes_read = patch->size; |
| 936 | |
| 937 | SHA_init(&ctx); |
| 938 | |
| 939 | int result; |
| 940 | |
| 941 | if (header_bytes_read >= 8 && |
| 942 | memcmp(header, "BSDIFF40", 8) == 0) { |
| 943 | result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size, |
| 944 | patch, 0, sink, token, &ctx); |
| 945 | } else if (header_bytes_read >= 8 && |
| 946 | memcmp(header, "IMGDIFF2", 8) == 0) { |
| 947 | result = ApplyImagePatch(source_to_use->data, source_to_use->size, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 948 | patch, sink, token, &ctx, bonus_data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 949 | } else { |
| 950 | printf("Unknown patch file format\n"); |
| 951 | return 1; |
| 952 | } |
| 953 | |
| 954 | if (output >= 0) { |
Michael Runge | be81e51 | 2014-10-29 12:42:15 -0700 | [diff] [blame] | 955 | if (fsync(output) != 0) { |
| 956 | printf("failed to fsync file \"%s\" (%s)\n", outname, strerror(errno)); |
| 957 | result = 1; |
| 958 | } |
| 959 | if (close(output) != 0) { |
| 960 | printf("failed to close file \"%s\" (%s)\n", outname, strerror(errno)); |
| 961 | result = 1; |
| 962 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | if (result != 0) { |
| 966 | if (retry == 0) { |
| 967 | printf("applying patch failed\n"); |
| 968 | return result != 0; |
| 969 | } else { |
| 970 | printf("applying patch failed; retrying\n"); |
| 971 | } |
| 972 | if (outname != NULL) { |
| 973 | unlink(outname); |
| 974 | } |
| 975 | } else { |
| 976 | // succeeded; no need to retry |
| 977 | break; |
| 978 | } |
| 979 | } while (retry-- > 0); |
| 980 | |
| 981 | const uint8_t* current_target_sha1 = SHA_final(&ctx); |
| 982 | if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { |
| 983 | printf("patch did not produce expected sha1\n"); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 984 | return 1; |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 985 | } else { |
| 986 | printf("now "); |
| 987 | print_short_sha1(target_sha1); |
| 988 | putchar('\n'); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | if (output < 0) { |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 992 | // Copy the temp file to the partition. |
| 993 | if (WriteToPartition(msi.buffer, msi.pos, target_filename) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 994 | printf("write of patched data to %s failed\n", target_filename); |
| 995 | return 1; |
| 996 | } |
| 997 | free(msi.buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 998 | } else { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 999 | // Give the .patch file the same owner, group, and mode of the |
| 1000 | // original source file. |
| 1001 | if (chmod(outname, source_to_use->st.st_mode) != 0) { |
| 1002 | printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno)); |
| 1003 | return 1; |
| 1004 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1005 | if (chown(outname, source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1006 | printf("chown of \"%s\" failed: %s\n", outname, strerror(errno)); |
| 1007 | return 1; |
| 1008 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1009 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1010 | // Finally, rename the .patch file to replace the target file. |
| 1011 | if (rename(outname, target_filename) != 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1012 | printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1013 | return 1; |
| 1014 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1017 | // If this run of applypatch created the copy, and we're here, we |
| 1018 | // can delete it. |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1019 | if (made_copy) { |
| 1020 | unlink(CACHE_TEMP_SOURCE); |
| 1021 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1022 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1023 | // Success! |
| 1024 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1025 | } |