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> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 29 | #include <functional> |
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 | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 37 | #include <android-base/strings.h> |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 38 | #include <openssl/sha.h> |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 39 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 40 | #include "edify/expr.h" |
Tao Bao | d33b2f8 | 2017-09-28 21:29:11 -0700 | [diff] [blame] | 41 | #include "otafault/ota_io.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 42 | #include "otautil/paths.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 43 | #include "otautil/print_sha1.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 44 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 45 | static int LoadPartitionContents(const std::string& filename, FileContents* file); |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 46 | static size_t FileSink(const unsigned char* data, size_t len, int fd); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 47 | static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch, |
| 48 | const std::string& target_filename, |
| 49 | const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 50 | |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 51 | int LoadFileContents(const std::string& filename, FileContents* file) { |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 52 | // A special 'filename' beginning with "EMMC:" means to load the contents of a partition. |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 53 | if (android::base::StartsWith(filename, "EMMC:")) { |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 54 | return LoadPartitionContents(filename, file); |
| 55 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 56 | |
Tao Bao | 47e5a8d | 2017-12-01 10:53:34 -0800 | [diff] [blame] | 57 | struct stat sb; |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 58 | if (stat(filename.c_str(), &sb) == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 59 | PLOG(ERROR) << "Failed to stat \"" << filename << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 60 | return -1; |
| 61 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 62 | |
Tao Bao | 47e5a8d | 2017-12-01 10:53:34 -0800 | [diff] [blame] | 63 | std::vector<unsigned char> data(sb.st_size); |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 64 | unique_file f(ota_fopen(filename.c_str(), "rb")); |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 65 | if (!f) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 66 | PLOG(ERROR) << "Failed to open \"" << filename << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 67 | return -1; |
| 68 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 69 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 70 | size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get()); |
| 71 | if (bytes_read != data.size()) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 72 | LOG(ERROR) << "Short read of \"" << filename << "\" (" << bytes_read << " bytes of " |
| 73 | << data.size() << ")"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | file->data = std::move(data); |
| 77 | SHA1(file->data.data(), file->data.size(), file->sha1); |
| 78 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 81 | // Loads the contents of an EMMC partition into the provided FileContents. filename should be a |
| 82 | // string of the form "EMMC:<partition_device>:...". The smallest size_n bytes for which that prefix |
| 83 | // of the partition contents has the corresponding sha1 hash will be loaded. It is acceptable for a |
| 84 | // size value to be repeated with different sha1s. Returns 0 on success. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 85 | // |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 86 | // This complexity is needed because if an OTA installation is interrupted, the partition might |
| 87 | // contain either the source or the target data, which might be of different lengths. We need to |
| 88 | // know the length in order to read from a partition (there is no "end-of-file" marker), so the |
| 89 | // caller must specify the possible lengths and the hash of the data, and we'll do the load |
| 90 | // expecting to find one of those hashes. |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 91 | static int LoadPartitionContents(const std::string& filename, FileContents* file) { |
| 92 | std::vector<std::string> pieces = android::base::Split(filename, ":"); |
| 93 | if (pieces.size() < 4 || pieces.size() % 2 != 0 || pieces[0] != "EMMC") { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 94 | LOG(ERROR) << "LoadPartitionContents called with bad filename \"" << filename << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename |
| 99 | std::vector<std::pair<size_t, std::string>> pairs; |
| 100 | for (size_t i = 0; i < pair_count; ++i) { |
| 101 | size_t size; |
| 102 | if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 103 | LOG(ERROR) << "LoadPartitionContents called with bad size \"" << pieces[i * 2 + 2] << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | pairs.push_back({ size, pieces[i * 2 + 3] }); |
| 107 | } |
| 108 | |
| 109 | // Sort the pairs array so that they are in order of increasing size. |
| 110 | std::sort(pairs.begin(), pairs.end()); |
| 111 | |
| 112 | const char* partition = pieces[1].c_str(); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 113 | unique_file dev(ota_fopen(partition, "rb")); |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 114 | if (!dev) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 115 | PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | SHA_CTX sha_ctx; |
| 120 | SHA1_Init(&sha_ctx); |
| 121 | |
| 122 | // Allocate enough memory to hold the largest size. |
| 123 | std::vector<unsigned char> buffer(pairs[pair_count - 1].first); |
| 124 | unsigned char* buffer_ptr = buffer.data(); |
| 125 | size_t buffer_size = 0; // # bytes read so far |
| 126 | bool found = false; |
| 127 | |
| 128 | for (const auto& pair : pairs) { |
| 129 | size_t current_size = pair.first; |
| 130 | const std::string& current_sha1 = pair.second; |
| 131 | |
| 132 | // Read enough additional bytes to get us up to the next size. (Again, |
| 133 | // we're trying the possibilities in order of increasing size). |
| 134 | size_t next = current_size - buffer_size; |
| 135 | if (next > 0) { |
| 136 | size_t read = ota_fread(buffer_ptr, 1, next, dev.get()); |
| 137 | if (next != read) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 138 | LOG(ERROR) << "Short read (" << read << " bytes of " << next << ") for partition \"" |
| 139 | << partition << "\""; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 140 | return -1; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 141 | } |
| 142 | SHA1_Update(&sha_ctx, buffer_ptr, read); |
| 143 | buffer_size += read; |
| 144 | buffer_ptr += read; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 145 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 146 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 147 | // Duplicate the SHA context and finalize the duplicate so we can |
| 148 | // check it against this pair's expected hash. |
| 149 | SHA_CTX temp_ctx; |
| 150 | memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX)); |
| 151 | uint8_t sha_so_far[SHA_DIGEST_LENGTH]; |
| 152 | SHA1_Final(sha_so_far, &temp_ctx); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 153 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 154 | uint8_t parsed_sha[SHA_DIGEST_LENGTH]; |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 155 | if (ParseSha1(current_sha1, parsed_sha) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 156 | LOG(ERROR) << "Failed to parse SHA-1 \"" << current_sha1 << "\" in " << filename; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 157 | return -1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 160 | if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) { |
| 161 | // We have a match. Stop reading the partition; we'll return the data we've read so far. |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 162 | LOG(INFO) << "Partition read matched size " << current_size << " SHA-1 " << current_sha1; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 163 | found = true; |
| 164 | break; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 165 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 166 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 167 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 168 | if (!found) { |
| 169 | // Ran off the end of the list of (size, sha1) pairs without finding a match. |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 170 | LOG(ERROR) << "Contents of partition \"" << partition << "\" didn't match " << filename; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 171 | return -1; |
| 172 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 173 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 174 | SHA1_Final(file->sha1, &sha_ctx); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 175 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 176 | buffer.resize(buffer_size); |
| 177 | file->data = std::move(buffer); |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 178 | |
| 179 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 182 | int SaveFileContents(const std::string& filename, const FileContents* file) { |
| 183 | unique_fd fd( |
| 184 | ota_open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR)); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 185 | if (fd == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 186 | PLOG(ERROR) << "Failed to open \"" << filename << "\" for write"; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 187 | return -1; |
| 188 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 189 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 190 | size_t bytes_written = FileSink(file->data.data(), file->data.size(), fd); |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 191 | if (bytes_written != file->data.size()) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 192 | PLOG(ERROR) << "Short write of \"" << filename << "\" (" << bytes_written << " bytes of " |
| 193 | << file->data.size(); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | if (ota_fsync(fd) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 197 | PLOG(ERROR) << "Failed to fsync \"" << filename << "\""; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 198 | return -1; |
| 199 | } |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 200 | if (ota_close(fd) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 201 | PLOG(ERROR) << "Failed to close \"" << filename << "\""; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 202 | return -1; |
| 203 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 204 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 205 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 208 | // Writes a memory buffer to 'target' partition, a string of the form |
| 209 | // "EMMC:<partition_device>[:...]". The target name might contain multiple colons, but |
| 210 | // WriteToPartition() only uses the first two and ignores the rest. Returns 0 on success. |
| 211 | static int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 212 | std::vector<std::string> pieces = android::base::Split(target, ":"); |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 213 | if (pieces.size() < 2 || pieces[0] != "EMMC") { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 214 | LOG(ERROR) << "WriteToPartition called with bad target \"" << target << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | const char* partition = pieces[1].c_str(); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 219 | unique_fd fd(ota_open(partition, O_RDWR)); |
| 220 | if (fd == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 221 | PLOG(ERROR) << "Failed to open \"" << partition << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 225 | size_t start = 0; |
| 226 | bool success = false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 227 | for (size_t attempt = 0; attempt < 2; ++attempt) { |
| 228 | if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 229 | PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 230 | return -1; |
| 231 | } |
| 232 | while (start < len) { |
| 233 | size_t to_write = len - start; |
| 234 | if (to_write > 1 << 20) to_write = 1 << 20; |
| 235 | |
| 236 | ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data + start, to_write)); |
| 237 | if (written == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 238 | PLOG(ERROR) << "Failed to write to \"" << partition << "\""; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 239 | return -1; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 240 | } |
| 241 | start += written; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 242 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 243 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 244 | if (ota_fsync(fd) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 245 | PLOG(ERROR) << "Failed to sync \"" << partition << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 246 | return -1; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 247 | } |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 248 | if (ota_close(fd) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 249 | PLOG(ERROR) << "Failed to close \"" << partition << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 250 | return -1; |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 251 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 252 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 253 | fd.reset(ota_open(partition, O_RDONLY)); |
| 254 | if (fd == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 255 | PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | // 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] | 260 | sync(); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 261 | unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY)); |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 262 | if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 263 | PLOG(ERROR) << "Failed to write to /proc/sys/vm/drop_caches"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 264 | } else { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 265 | LOG(INFO) << " caches dropped"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 266 | } |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 267 | ota_close(dc); |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 268 | sleep(1); |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 269 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 270 | // Verify. |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 271 | if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 272 | PLOG(ERROR) << "Failed to seek to 0 on " << partition; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | unsigned char buffer[4096]; |
| 277 | start = len; |
| 278 | for (size_t p = 0; p < len; p += sizeof(buffer)) { |
| 279 | size_t to_read = len - p; |
| 280 | if (to_read > sizeof(buffer)) { |
| 281 | to_read = sizeof(buffer); |
| 282 | } |
| 283 | |
| 284 | size_t so_far = 0; |
| 285 | while (so_far < to_read) { |
| 286 | ssize_t read_count = TEMP_FAILURE_RETRY(ota_read(fd, buffer + so_far, to_read - so_far)); |
| 287 | if (read_count == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 288 | PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 289 | return -1; |
| 290 | } else if (read_count == 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 291 | LOG(ERROR) << "Verify-reading " << partition << " reached unexpected EOF at " << p; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | if (static_cast<size_t>(read_count) < to_read) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 295 | LOG(INFO) << "Short verify-read " << partition << " at " << p << ": expected " << to_read |
| 296 | << " actual " << read_count; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 297 | } |
| 298 | so_far += read_count; |
| 299 | } |
| 300 | |
| 301 | if (memcmp(buffer, data + p, to_read) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 302 | LOG(ERROR) << "Verification failed starting at " << p; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 303 | start = p; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (start == len) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 309 | LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 310 | success = true; |
| 311 | break; |
| 312 | } |
katao | 9a6f520 | 2016-12-19 11:22:30 +0800 | [diff] [blame] | 313 | |
| 314 | if (ota_close(fd) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 315 | PLOG(ERROR) << "Failed to close " << partition; |
katao | 9a6f520 | 2016-12-19 11:22:30 +0800 | [diff] [blame] | 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | fd.reset(ota_open(partition, O_RDWR)); |
| 320 | if (fd == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 321 | PLOG(ERROR) << "Failed to reopen " << partition << " for next attempt"; |
katao | 9a6f520 | 2016-12-19 11:22:30 +0800 | [diff] [blame] | 322 | return -1; |
| 323 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | if (!success) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 327 | LOG(ERROR) << "Failed to verify after all attempts"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 328 | return -1; |
| 329 | } |
| 330 | |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 331 | if (ota_close(fd) == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 332 | PLOG(ERROR) << "Failed to close " << partition; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 333 | return -1; |
| 334 | } |
| 335 | sync(); |
| 336 | |
| 337 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 340 | int ParseSha1(const std::string& str, uint8_t* digest) { |
| 341 | const char* ps = str.c_str(); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 342 | uint8_t* pd = digest; |
| 343 | for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) { |
| 344 | int digit; |
| 345 | if (*ps >= '0' && *ps <= '9') { |
| 346 | digit = *ps - '0'; |
| 347 | } else if (*ps >= 'a' && *ps <= 'f') { |
| 348 | digit = *ps - 'a' + 10; |
| 349 | } else if (*ps >= 'A' && *ps <= 'F') { |
| 350 | digit = *ps - 'A' + 10; |
| 351 | } else { |
| 352 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 353 | } |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 354 | if (i % 2 == 0) { |
| 355 | *pd = digit << 4; |
| 356 | } else { |
| 357 | *pd |= digit; |
| 358 | ++pd; |
| 359 | } |
| 360 | } |
| 361 | if (*ps != '\0') return -1; |
| 362 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 365 | // Searches a vector of SHA-1 strings for one matching the given SHA-1. Returns the index of the |
| 366 | // match on success, or -1 if no match is found. |
| 367 | static int FindMatchingPatch(const uint8_t* sha1, const std::vector<std::string>& patch_sha1s) { |
| 368 | for (size_t i = 0; i < patch_sha1s.size(); ++i) { |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 369 | uint8_t patch_sha1[SHA_DIGEST_LENGTH]; |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 370 | if (ParseSha1(patch_sha1s[i], patch_sha1) == 0 && |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 371 | memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) { |
| 372 | return i; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 373 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 374 | } |
| 375 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 378 | int applypatch_check(const std::string& filename, const std::vector<std::string>& sha1s) { |
| 379 | if (!android::base::StartsWith(filename, "EMMC:")) { |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | // The check will pass if LoadPartitionContents is successful, because the filename already |
| 384 | // encodes the desired SHA-1s. |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 385 | FileContents file; |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 386 | if (LoadPartitionContents(filename, &file) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 387 | LOG(INFO) << "\"" << filename << "\" doesn't have any of expected SHA-1 sums; checking cache"; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 388 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 389 | // If the partition is corrupted, it might be because we were killed in the middle of patching |
| 390 | // it. A copy should have been made in cache_temp_source. If that file exists and matches the |
| 391 | // SHA-1 we're looking for, the check still passes. |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 392 | if (LoadFileContents(Paths::Get().cache_temp_source(), &file) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 393 | LOG(ERROR) << "Failed to load cache file"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 394 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 395 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 396 | |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 397 | if (FindMatchingPatch(file.sha1, sha1s) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 398 | LOG(ERROR) << "The cache bits don't match any SHA-1 for \"" << filename << "\""; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 399 | return 1; |
| 400 | } |
| 401 | } |
| 402 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | int ShowLicenses() { |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 406 | ShowBSDiffLicense(); |
| 407 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 410 | static size_t FileSink(const unsigned char* data, size_t len, int fd) { |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 411 | size_t done = 0; |
| 412 | while (done < len) { |
| 413 | ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done)); |
| 414 | if (wrote == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 415 | PLOG(ERROR) << "Failed to write " << len - done << " bytes"; |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 416 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 417 | } |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 418 | done += wrote; |
| 419 | } |
| 420 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 423 | int applypatch(const char* source_filename, const char* target_filename, |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 424 | const char* target_sha1_str, size_t /* target_size */, |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 425 | const std::vector<std::string>& patch_sha1s, |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 426 | const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 427 | LOG(INFO) << "Patching " << source_filename; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 428 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 429 | if (target_filename[0] == '-' && target_filename[1] == '\0') { |
| 430 | target_filename = source_filename; |
| 431 | } |
| 432 | |
| 433 | if (strncmp(target_filename, "EMMC:", 5) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 434 | LOG(ERROR) << "Supporting patching EMMC targets only"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
| 439 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 440 | LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\""; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | // We try to load the target file into the source_file object. |
| 445 | FileContents source_file; |
| 446 | if (LoadFileContents(target_filename, &source_file) == 0) { |
| 447 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
| 448 | // The early-exit case: the patch was already applied, this file has the desired hash, nothing |
| 449 | // for us to do. |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 450 | LOG(INFO) << " already " << short_sha1(target_sha1); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 451 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 452 | } |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 453 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 454 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 455 | if (source_file.data.empty() || |
| 456 | (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) { |
| 457 | // Need to load the source file: either we failed to load the target file, or we did but it's |
| 458 | // different from the expected. |
| 459 | source_file.data.clear(); |
| 460 | LoadFileContents(source_filename, &source_file); |
| 461 | } |
| 462 | |
| 463 | if (!source_file.data.empty()) { |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 464 | int to_use = FindMatchingPatch(source_file.sha1, patch_sha1s); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 465 | if (to_use != -1) { |
| 466 | return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1, |
| 467 | bonus_data); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 468 | } |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 469 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 470 | |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 471 | LOG(INFO) << "Source file is bad; trying copy"; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 472 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 473 | FileContents copy_file; |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 474 | if (LoadFileContents(Paths::Get().cache_temp_source(), ©_file) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 475 | LOG(ERROR) << "Failed to read copy file"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 476 | return 1; |
| 477 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 478 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 479 | int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1s); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 480 | if (to_use == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 481 | LOG(ERROR) << "The copy on /cache doesn't match source SHA-1s either"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 482 | return 1; |
| 483 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 484 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 485 | return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data); |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 488 | int applypatch_flash(const char* source_filename, const char* target_filename, |
| 489 | const char* target_sha1_str, size_t target_size) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 490 | LOG(INFO) << "Flashing " << target_filename; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 491 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 492 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
| 493 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 494 | LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\""; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 495 | return 1; |
| 496 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 497 | |
Tao Bao | d34e6bc | 2018-07-13 13:11:09 -0700 | [diff] [blame] | 498 | std::vector<std::string> pieces = android::base::Split(target_filename, ":"); |
| 499 | if (pieces.size() != 4 || pieces[0] != "EMMC") { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 500 | LOG(ERROR) << "Invalid target name \"" << target_filename << "\""; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 501 | return 1; |
| 502 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 503 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 504 | // Load the target into the source_file object to see if already applied. |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 505 | FileContents source_file; |
Tao Bao | d34e6bc | 2018-07-13 13:11:09 -0700 | [diff] [blame] | 506 | if (LoadPartitionContents(target_filename, &source_file) == 0 && |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 507 | memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 508 | // The early-exit case: the image was already applied, this partition has the desired hash, |
| 509 | // nothing for us to do. |
| 510 | LOG(INFO) << " already " << short_sha1(target_sha1); |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 511 | return 0; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | if (LoadFileContents(source_filename, &source_file) == 0) { |
| 515 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
| 516 | // The source doesn't have desired checksum. |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 517 | LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum"; |
| 518 | LOG(ERROR) << "expected: " << short_sha1(target_sha1) |
| 519 | << ", found: " << short_sha1(source_file.sha1); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 520 | return 1; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 525 | LOG(ERROR) << "Failed to write copied data to " << target_filename; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 526 | return 1; |
| 527 | } |
| 528 | return 0; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 531 | static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch, |
| 532 | const std::string& target_filename, |
| 533 | const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) { |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 534 | if (patch->type != Value::Type::BLOB) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 535 | LOG(ERROR) << "patch is not a blob"; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 536 | return 1; |
| 537 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 538 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 539 | const char* header = &patch->data[0]; |
| 540 | size_t header_bytes_read = patch->data.size(); |
| 541 | bool use_bsdiff = false; |
| 542 | if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) { |
| 543 | use_bsdiff = true; |
| 544 | } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) { |
| 545 | use_bsdiff = false; |
| 546 | } else { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 547 | LOG(ERROR) << "Unknown patch file format"; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 548 | return 1; |
| 549 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 550 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 551 | CHECK(android::base::StartsWith(target_filename, "EMMC:")); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 552 | |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame] | 553 | // We write the original source to cache, in case the partition write is interrupted. |
| 554 | if (!CheckAndFreeSpaceOnCache(source_file.data.size())) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 555 | LOG(ERROR) << "Not enough free space on /cache"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 556 | return 1; |
| 557 | } |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 558 | if (SaveFileContents(Paths::Get().cache_temp_source(), &source_file) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 559 | LOG(ERROR) << "Failed to back up source file"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 560 | return 1; |
| 561 | } |
| 562 | |
| 563 | // We store the decoded output in memory. |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 564 | std::string memory_sink_str; // Don't need to reserve space. |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 565 | SHA_CTX ctx; |
| 566 | SHA1_Init(&ctx); |
| 567 | SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) { |
| 568 | SHA1_Update(&ctx, data, len); |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 569 | memory_sink_str.append(reinterpret_cast<const char*>(data), len); |
| 570 | return len; |
| 571 | }; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 572 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 573 | int result; |
| 574 | if (use_bsdiff) { |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 575 | result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 576 | } else { |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 577 | result = |
| 578 | ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 579 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 580 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 581 | if (result != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 582 | LOG(ERROR) << "Failed to apply the patch: " << result; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 583 | return 1; |
| 584 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 585 | |
| 586 | uint8_t current_target_sha1[SHA_DIGEST_LENGTH]; |
| 587 | SHA1_Final(current_target_sha1, &ctx); |
| 588 | if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 589 | LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(target_sha1); |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 590 | |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 591 | LOG(ERROR) << "target size " << memory_sink_str.size() << " SHA-1 " |
| 592 | << short_sha1(current_target_sha1); |
| 593 | LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 " |
| 594 | << short_sha1(source_file.sha1); |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 595 | |
| 596 | uint8_t patch_digest[SHA_DIGEST_LENGTH]; |
| 597 | SHA1(reinterpret_cast<const uint8_t*>(patch->data.data()), patch->data.size(), patch_digest); |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 598 | LOG(ERROR) << "patch size " << patch->data.size() << " SHA-1 " << short_sha1(patch_digest); |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 599 | |
Tao Bao | 7ea515e | 2018-07-09 15:16:13 -0700 | [diff] [blame] | 600 | if (bonus_data != nullptr) { |
| 601 | uint8_t bonus_digest[SHA_DIGEST_LENGTH]; |
| 602 | SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(), |
| 603 | bonus_digest); |
| 604 | LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 " |
| 605 | << short_sha1(bonus_digest); |
| 606 | } |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 607 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 608 | return 1; |
| 609 | } else { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 610 | LOG(INFO) << " now " << short_sha1(target_sha1); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 613 | // Write back the temp file to the partition. |
| 614 | if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()), |
| 615 | memory_sink_str.size(), target_filename) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 616 | LOG(ERROR) << "Failed to write patched data to " << target_filename; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 617 | return 1; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 620 | // Delete the backup copy of the source. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 621 | unlink(Paths::Get().cache_temp_source().c_str()); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 622 | |
| 623 | // Success! |
| 624 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 625 | } |