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