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 | } |
| 335 | } |
| 336 | |
| 337 | if (!success) { |
| 338 | printf("failed to verify after all attempts\n"); |
| 339 | return -1; |
| 340 | } |
| 341 | |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 342 | if (ota_close(fd) == -1) { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 343 | printf("error closing %s: %s\n", partition, strerror(errno)); |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 344 | return -1; |
| 345 | } |
| 346 | sync(); |
| 347 | |
| 348 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 351 | // Take a string 'str' of 40 hex digits and parse it into the 20 |
| 352 | // byte array 'digest'. 'str' may contain only the digest or be of |
| 353 | // the form "<digest>:<anything>". Return 0 on success, -1 on any |
| 354 | // error. |
| 355 | int ParseSha1(const char* str, uint8_t* digest) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 356 | const char* ps = str; |
| 357 | uint8_t* pd = digest; |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 358 | for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 359 | int digit; |
| 360 | if (*ps >= '0' && *ps <= '9') { |
| 361 | digit = *ps - '0'; |
| 362 | } else if (*ps >= 'a' && *ps <= 'f') { |
| 363 | digit = *ps - 'a' + 10; |
| 364 | } else if (*ps >= 'A' && *ps <= 'F') { |
| 365 | digit = *ps - 'A' + 10; |
| 366 | } else { |
| 367 | return -1; |
| 368 | } |
| 369 | if (i % 2 == 0) { |
| 370 | *pd = digit << 4; |
| 371 | } else { |
| 372 | *pd |= digit; |
| 373 | ++pd; |
| 374 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 375 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 376 | if (*ps != '\0') return -1; |
| 377 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 380 | // Search an array of sha1 strings for one matching the given sha1. |
| 381 | // Return the index of the match on success, or -1 if no match is |
| 382 | // found. |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 383 | 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] | 384 | for (size_t i = 0; i < patch_sha1_str.size(); ++i) { |
| 385 | uint8_t patch_sha1[SHA_DIGEST_LENGTH]; |
| 386 | if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 && |
| 387 | memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) { |
| 388 | return i; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 389 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 390 | } |
| 391 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // Returns 0 if the contents of the file (argv[2]) or the cached file |
| 395 | // match any of the sha1's on the command line (argv[3:]). Returns |
| 396 | // nonzero otherwise. |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 397 | 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] | 398 | FileContents file; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 399 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 400 | // It's okay to specify no sha1s; the check will pass if the |
| 401 | // LoadFileContents is successful. (Useful for reading |
| 402 | // partitions, where the filename encodes the sha1s; no need to |
| 403 | // check them twice.) |
| 404 | if (LoadFileContents(filename, &file) != 0 || |
| 405 | (!patch_sha1_str.empty() && FindMatchingPatch(file.sha1, patch_sha1_str) < 0)) { |
| 406 | 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] | 407 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 408 | // If the source file is missing or corrupted, it might be because |
| 409 | // we were killed in the middle of patching it. A copy of it |
| 410 | // should have been made in CACHE_TEMP_SOURCE. If that file |
| 411 | // exists and matches the sha1 we're looking for, the check still |
| 412 | // passes. |
| 413 | if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) { |
| 414 | printf("failed to load cache file\n"); |
| 415 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 416 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 417 | |
| 418 | if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) { |
| 419 | printf("cache bits don't match any sha1 for \"%s\"\n", filename); |
| 420 | return 1; |
| 421 | } |
| 422 | } |
| 423 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | int ShowLicenses() { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 427 | ShowBSDiffLicense(); |
| 428 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 431 | ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 432 | int fd = *static_cast<int*>(token); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 433 | ssize_t done = 0; |
| 434 | ssize_t wrote; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 435 | while (done < len) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 436 | wrote = TEMP_FAILURE_RETRY(ota_write(fd, data+done, len-done)); |
Elliott Hughes | 7bad7c4 | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 437 | if (wrote == -1) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 438 | printf("error writing %zd bytes: %s\n", (len-done), strerror(errno)); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 439 | return done; |
| 440 | } |
| 441 | done += wrote; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 442 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 443 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Doug Zongker | bc7ffed | 2014-08-15 14:31:52 -0700 | [diff] [blame] | 446 | ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 447 | std::string* s = static_cast<std::string*>(token); |
| 448 | s->append(reinterpret_cast<const char*>(data), len); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 449 | return len; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Return the amount of free space (in bytes) on the filesystem |
| 453 | // containing filename. filename must exist. Return -1 on error. |
| 454 | size_t FreeSpaceForFile(const char* filename) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 455 | struct statfs sf; |
| 456 | if (statfs(filename, &sf) != 0) { |
| 457 | printf("failed to statfs %s: %s\n", filename, strerror(errno)); |
| 458 | return -1; |
| 459 | } |
caozhiyuan | 3b49776 | 2015-05-19 17:21:00 +0800 | [diff] [blame] | 460 | return sf.f_bsize * sf.f_bavail; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 463 | int CacheSizeCheck(size_t bytes) { |
| 464 | if (MakeFreeSpaceOnCache(bytes) < 0) { |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 465 | printf("unable to make %zu bytes available on /cache\n", bytes); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 466 | return 1; |
| 467 | } else { |
| 468 | return 0; |
| 469 | } |
| 470 | } |
| 471 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 472 | // 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] | 473 | // (the original file is not touched until we have the desired |
| 474 | // replacement for it) and idempotent (it's okay to run this program |
| 475 | // multiple times). |
| 476 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 477 | // - if the sha1 hash of <target_filename> is <target_sha1_string>, |
| 478 | // does nothing and exits successfully. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 479 | // |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 480 | // - otherwise, if the sha1 hash of <source_filename> is one of the |
| 481 | // entries in <patch_sha1_str>, the corresponding patch from |
| 482 | // <patch_data> (which must be a VAL_BLOB) is applied to produce a |
| 483 | // new file (the type of patch is automatically detected from the |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 484 | // blob data). If that new file has sha1 hash <target_sha1_str>, |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 485 | // moves it to replace <target_filename>, and exits successfully. |
| 486 | // Note that if <source_filename> and <target_filename> are not the |
| 487 | // same, <source_filename> is NOT deleted on success. |
| 488 | // <target_filename> may be the string "-" to mean "the same as |
| 489 | // source_filename". |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 490 | // |
| 491 | // - otherwise, or if any error is encountered, exits with non-zero |
| 492 | // status. |
| 493 | // |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 494 | // <source_filename> may refer to a partition to read the source data. |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 495 | // See the comments for the LoadPartitionContents() function above |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 496 | // for the format of such a filename. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 497 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 498 | int applypatch(const char* source_filename, |
| 499 | const char* target_filename, |
| 500 | const char* target_sha1_str, |
| 501 | size_t target_size, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 502 | const std::vector<std::string>& patch_sha1_str, |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 503 | const std::vector<std::unique_ptr<Value>>& patch_data, |
| 504 | const Value* bonus_data) { |
Doug Zongker | bf80f49 | 2012-10-19 12:24:26 -0700 | [diff] [blame] | 505 | printf("patch %s: ", source_filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 506 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 507 | if (target_filename[0] == '-' && target_filename[1] == '\0') { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 508 | target_filename = source_filename; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 511 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 512 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
| 513 | printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 514 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 515 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 516 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 517 | FileContents source_file; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 518 | const Value* source_patch_value = nullptr; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 519 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 520 | // We try to load the target file into the source_file object. |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 521 | if (LoadFileContents(target_filename, &source_file) == 0) { |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 522 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 523 | // The early-exit case: the patch was already applied, this file |
| 524 | // has the desired hash, nothing for us to do. |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 525 | printf("already %s\n", short_sha1(target_sha1).c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 529 | |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 530 | if (source_file.data.empty() || |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 531 | (target_filename != source_filename && |
| 532 | strcmp(target_filename, source_filename) != 0)) { |
| 533 | // Need to load the source file: either we failed to load the |
| 534 | // 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] | 535 | source_file.data.clear(); |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 536 | LoadFileContents(source_filename, &source_file); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 539 | if (!source_file.data.empty()) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 540 | int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 541 | if (to_use >= 0) { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 542 | source_patch_value = patch_data[to_use].get(); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 546 | FileContents copy_file; |
| 547 | const Value* copy_patch_value = nullptr; |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 548 | if (source_patch_value == nullptr) { |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 549 | source_file.data.clear(); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 550 | printf("source file is bad; trying copy\n"); |
| 551 | |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 552 | if (LoadFileContents(CACHE_TEMP_SOURCE, ©_file) < 0) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 553 | // fail. |
| 554 | printf("failed to read copy file\n"); |
| 555 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 558 | int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str); |
Doug Zongker | 8cd9e4f | 2010-08-12 17:38:09 -0700 | [diff] [blame] | 559 | if (to_use >= 0) { |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 560 | copy_patch_value = patch_data[to_use].get(); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Tao Bao | fada91c | 2016-10-27 18:16:06 -0700 | [diff] [blame] | 563 | if (copy_patch_value == nullptr) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 564 | // fail. |
| 565 | printf("copy file doesn't match source SHA-1s either\n"); |
| 566 | return 1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 567 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Yabin Cui | d6c93af | 2016-02-10 16:41:10 -0800 | [diff] [blame] | 570 | return GenerateTarget(&source_file, source_patch_value, |
| 571 | ©_file, copy_patch_value, |
| 572 | source_filename, target_filename, |
| 573 | target_sha1, target_size, bonus_data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 576 | /* |
| 577 | * This function flashes a given image to the target partition. It verifies |
| 578 | * the target cheksum first, and will return if target has the desired hash. |
| 579 | * It checks the checksum of the given source image before flashing, and |
| 580 | * verifies the target partition afterwards. The function is idempotent. |
| 581 | * Returns zero on success. |
| 582 | */ |
| 583 | int applypatch_flash(const char* source_filename, const char* target_filename, |
| 584 | const char* target_sha1_str, size_t target_size) { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 585 | printf("flash %s: ", target_filename); |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 586 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 587 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
| 588 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
| 589 | printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); |
| 590 | return 1; |
| 591 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 592 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 593 | std::string target_str(target_filename); |
| 594 | std::vector<std::string> pieces = android::base::Split(target_str, ":"); |
| 595 | if (pieces.size() != 2 || pieces[0] != "EMMC") { |
| 596 | printf("invalid target name \"%s\"", target_filename); |
| 597 | return 1; |
| 598 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 599 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 600 | // Load the target into the source_file object to see if already applied. |
| 601 | pieces.push_back(std::to_string(target_size)); |
| 602 | pieces.push_back(target_sha1_str); |
| 603 | std::string fullname = android::base::Join(pieces, ':'); |
| 604 | FileContents source_file; |
| 605 | if (LoadPartitionContents(fullname, &source_file) == 0 && |
| 606 | memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
| 607 | // The early-exit case: the image was already applied, this partition |
| 608 | // has the desired hash, nothing for us to do. |
| 609 | printf("already %s\n", short_sha1(target_sha1).c_str()); |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 610 | return 0; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (LoadFileContents(source_filename, &source_file) == 0) { |
| 614 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
| 615 | // The source doesn't have desired checksum. |
| 616 | printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename); |
| 617 | printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(), |
| 618 | short_sha1(source_file.sha1).c_str()); |
| 619 | return 1; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) { |
| 624 | printf("write of copied data to %s failed\n", target_filename); |
| 625 | return 1; |
| 626 | } |
| 627 | return 0; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 630 | static int GenerateTarget(FileContents* source_file, |
| 631 | const Value* source_patch_value, |
| 632 | FileContents* copy_file, |
| 633 | const Value* copy_patch_value, |
| 634 | const char* source_filename, |
| 635 | const char* target_filename, |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 636 | const uint8_t target_sha1[SHA_DIGEST_LENGTH], |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 637 | size_t target_size, |
| 638 | const Value* bonus_data) { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 639 | // assume that target_filename (eg "/system/app/Foo.apk") is located |
| 640 | // on the same filesystem as its top-level directory ("/system"). |
| 641 | // We need something that exists for calling statfs(). |
| 642 | std::string target_fs = target_filename; |
| 643 | auto slash_pos = target_fs.find('/', 1); |
| 644 | if (slash_pos != std::string::npos) { |
| 645 | target_fs.resize(slash_pos); |
| 646 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 647 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 648 | FileContents* source_to_use; |
| 649 | const Value* patch; |
| 650 | if (source_patch_value != nullptr) { |
| 651 | source_to_use = source_file; |
| 652 | patch = source_patch_value; |
| 653 | } else { |
| 654 | source_to_use = copy_file; |
| 655 | patch = copy_patch_value; |
| 656 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 657 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 658 | if (patch->type != VAL_BLOB) { |
| 659 | printf("patch is not a blob\n"); |
| 660 | return 1; |
| 661 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 662 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 663 | const char* header = &patch->data[0]; |
| 664 | size_t header_bytes_read = patch->data.size(); |
| 665 | bool use_bsdiff = false; |
| 666 | if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) { |
| 667 | use_bsdiff = true; |
| 668 | } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) { |
| 669 | use_bsdiff = false; |
| 670 | } else { |
| 671 | printf("Unknown patch file format\n"); |
| 672 | return 1; |
| 673 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 674 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 675 | bool target_is_partition = (strncmp(target_filename, "EMMC:", 5) == 0); |
| 676 | const std::string tmp_target_filename = std::string(target_filename) + ".patch"; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 677 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 678 | int retry = 1; |
| 679 | bool made_copy = false; |
| 680 | SHA_CTX ctx; |
| 681 | std::string memory_sink_str; // Don't need to reserve space. |
| 682 | do { |
| 683 | // 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] | 684 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 685 | if (target_is_partition) { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 686 | // If the target is a partition, we're actually going to |
| 687 | // write the output to /tmp and then copy it to the |
| 688 | // partition. statfs() always returns 0 blocks free for |
| 689 | // /tmp, so instead we'll just assume that /tmp has enough |
| 690 | // space to hold the file. |
| 691 | |
| 692 | // We still write the original source to cache, in case |
| 693 | // the partition write is interrupted. |
| 694 | if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) { |
| 695 | printf("not enough free space on /cache\n"); |
| 696 | return 1; |
| 697 | } |
| 698 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 699 | printf("failed to back up source file\n"); |
| 700 | return 1; |
| 701 | } |
| 702 | made_copy = true; |
| 703 | retry = 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 704 | } else { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 705 | bool enough_space = false; |
| 706 | if (retry > 0) { |
| 707 | size_t free_space = FreeSpaceForFile(target_fs.c_str()); |
| 708 | enough_space = (free_space > (256 << 10)) && // 256k (two-block) minimum |
| 709 | (free_space > (target_size * 3 / 2)); // 50% margin of error |
| 710 | if (!enough_space) { |
| 711 | printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n", target_size, |
| 712 | free_space, retry, enough_space); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 713 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (!enough_space) { |
| 717 | retry = 0; |
| 718 | } |
| 719 | |
| 720 | if (!enough_space && source_patch_value != nullptr) { |
| 721 | // Using the original source, but not enough free space. First |
| 722 | // copy the source file to cache, then delete it from the original |
| 723 | // location. |
| 724 | |
| 725 | if (strncmp(source_filename, "EMMC:", 5) == 0) { |
| 726 | // It's impossible to free space on the target filesystem by |
| 727 | // deleting the source if the source is a partition. If |
| 728 | // we're ever in a state where we need to do this, fail. |
| 729 | printf("not enough free space for target but source is partition\n"); |
| 730 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 731 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 732 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 733 | if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) { |
| 734 | printf("not enough free space on /cache\n"); |
| 735 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 736 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 737 | |
| 738 | if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { |
| 739 | printf("failed to back up source file\n"); |
| 740 | return 1; |
| 741 | } |
| 742 | made_copy = true; |
| 743 | unlink(source_filename); |
| 744 | |
| 745 | size_t free_space = FreeSpaceForFile(target_fs.c_str()); |
| 746 | printf("(now %zu bytes free for target) ", free_space); |
| 747 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 750 | SinkFn sink = nullptr; |
| 751 | void* token = nullptr; |
| 752 | unique_fd output_fd; |
| 753 | if (target_is_partition) { |
| 754 | // We store the decoded output in memory. |
| 755 | sink = MemorySink; |
| 756 | token = &memory_sink_str; |
| 757 | } else { |
| 758 | // We write the decoded output to "<tgt-file>.patch". |
| 759 | output_fd.reset(ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, |
| 760 | S_IRUSR | S_IWUSR)); |
| 761 | if (output_fd == -1) { |
| 762 | printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(), strerror(errno)); |
| 763 | return 1; |
| 764 | } |
| 765 | sink = FileSink; |
| 766 | token = &output_fd; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 767 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 768 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 769 | SHA1_Init(&ctx); |
| 770 | |
| 771 | int result; |
| 772 | if (use_bsdiff) { |
| 773 | result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(), patch, 0, |
| 774 | sink, token, &ctx); |
| 775 | } else { |
| 776 | result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(), patch, sink, |
| 777 | token, &ctx, bonus_data); |
| 778 | } |
| 779 | |
| 780 | if (!target_is_partition) { |
| 781 | if (ota_fsync(output_fd) != 0) { |
| 782 | printf("failed to fsync file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno)); |
| 783 | result = 1; |
| 784 | } |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 785 | if (ota_close(output_fd) != 0) { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 786 | printf("failed to close file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno)); |
| 787 | result = 1; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | if (result != 0) { |
| 792 | if (retry == 0) { |
| 793 | printf("applying patch failed\n"); |
| 794 | return 1; |
| 795 | } else { |
| 796 | printf("applying patch failed; retrying\n"); |
| 797 | } |
| 798 | if (!target_is_partition) { |
| 799 | unlink(tmp_target_filename.c_str()); |
| 800 | } |
| 801 | } else { |
| 802 | // succeeded; no need to retry |
| 803 | break; |
| 804 | } |
| 805 | } while (retry-- > 0); |
| 806 | |
| 807 | uint8_t current_target_sha1[SHA_DIGEST_LENGTH]; |
| 808 | SHA1_Final(current_target_sha1, &ctx); |
| 809 | if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
| 810 | printf("patch did not produce expected sha1\n"); |
| 811 | return 1; |
| 812 | } else { |
| 813 | printf("now %s\n", short_sha1(target_sha1).c_str()); |
| 814 | } |
| 815 | |
| 816 | if (target_is_partition) { |
| 817 | // Copy the temp file to the partition. |
| 818 | if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()), |
| 819 | memory_sink_str.size(), target_filename) != 0) { |
| 820 | printf("write of patched data to %s failed\n", target_filename); |
| 821 | return 1; |
| 822 | } |
| 823 | } else { |
| 824 | // Give the .patch file the same owner, group, and mode of the original source file. |
| 825 | if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) { |
| 826 | printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno)); |
| 827 | return 1; |
| 828 | } |
| 829 | if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid, |
| 830 | source_to_use->st.st_gid) != 0) { |
| 831 | printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno)); |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | // Finally, rename the .patch file to replace the target file. |
| 836 | if (rename(tmp_target_filename.c_str(), target_filename) != 0) { |
| 837 | printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno)); |
| 838 | return 1; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | // If this run of applypatch created the copy, and we're here, we can delete it. |
| 843 | if (made_copy) { |
| 844 | unlink(CACHE_TEMP_SOURCE); |
| 845 | } |
| 846 | |
| 847 | // Success! |
| 848 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 849 | } |