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