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