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