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