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