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 | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 379 | int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1s) { |
| 380 | // It's okay to specify no SHA-1s; the check will pass if the LoadFileContents is successful. |
| 381 | // (Useful for reading partitions, where the filename encodes the SHA-1s; no need to check them |
| 382 | // twice.) |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 383 | FileContents file; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 384 | if (LoadFileContents(filename, &file) != 0 || |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 385 | (!patch_sha1s.empty() && FindMatchingPatch(file.sha1, patch_sha1s) < 0)) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 386 | 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] | 387 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 388 | // If the source file is missing or corrupted, it might be because we were killed in the middle |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 389 | // of patching it. A copy should have been made in cache_temp_source. If that file exists and |
| 390 | // matches the SHA-1 we're looking for, the check still passes. |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 391 | if (LoadFileContents(Paths::Get().cache_temp_source(), &file) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 392 | LOG(ERROR) << "Failed to load cache file"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 393 | return 1; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 394 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 395 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 396 | if (FindMatchingPatch(file.sha1, patch_sha1s) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 397 | 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] | 398 | return 1; |
| 399 | } |
| 400 | } |
| 401 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | int ShowLicenses() { |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 405 | ShowBSDiffLicense(); |
| 406 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 409 | 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] | 410 | size_t done = 0; |
| 411 | while (done < len) { |
| 412 | ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done)); |
| 413 | if (wrote == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 414 | PLOG(ERROR) << "Failed to write " << len - done << " bytes"; |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 415 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 416 | } |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 417 | done += wrote; |
| 418 | } |
| 419 | return done; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 422 | size_t FreeSpaceForFile(const std::string& filename) { |
| 423 | struct statfs sf; |
| 424 | if (statfs(filename.c_str(), &sf) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 425 | PLOG(ERROR) << "Failed to statfs " << filename; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | return sf.f_bsize * sf.f_bavail; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 431 | int CacheSizeCheck(size_t bytes) { |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 432 | if (MakeFreeSpaceOnCache(bytes) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 433 | LOG(ERROR) << "Failed to make " << bytes << " bytes available on /cache"; |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 434 | return 1; |
| 435 | } |
| 436 | return 0; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 439 | int applypatch(const char* source_filename, const char* target_filename, |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 440 | const char* target_sha1_str, size_t /* target_size */, |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 441 | const std::vector<std::string>& patch_sha1s, |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 442 | 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] | 443 | LOG(INFO) << "Patching " << source_filename; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 444 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 445 | if (target_filename[0] == '-' && target_filename[1] == '\0') { |
| 446 | target_filename = source_filename; |
| 447 | } |
| 448 | |
| 449 | if (strncmp(target_filename, "EMMC:", 5) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 450 | LOG(ERROR) << "Supporting patching EMMC targets only"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 451 | return 1; |
| 452 | } |
| 453 | |
| 454 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
| 455 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 456 | LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\""; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | // We try to load the target file into the source_file object. |
| 461 | FileContents source_file; |
| 462 | if (LoadFileContents(target_filename, &source_file) == 0) { |
| 463 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
| 464 | // The early-exit case: the patch was already applied, this file has the desired hash, nothing |
| 465 | // for us to do. |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 466 | LOG(INFO) << " already " << short_sha1(target_sha1); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 467 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -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 | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 471 | if (source_file.data.empty() || |
| 472 | (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) { |
| 473 | // Need to load the source file: either we failed to load the target file, or we did but it's |
| 474 | // different from the expected. |
| 475 | source_file.data.clear(); |
| 476 | LoadFileContents(source_filename, &source_file); |
| 477 | } |
| 478 | |
| 479 | if (!source_file.data.empty()) { |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 480 | int to_use = FindMatchingPatch(source_file.sha1, patch_sha1s); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 481 | if (to_use != -1) { |
| 482 | return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1, |
| 483 | bonus_data); |
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 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 486 | |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 487 | LOG(INFO) << "Source file is bad; trying copy"; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 488 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 489 | FileContents copy_file; |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 490 | if (LoadFileContents(Paths::Get().cache_temp_source(), ©_file) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 491 | LOG(ERROR) << "Failed to read copy file"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 492 | return 1; |
| 493 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 494 | |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 495 | int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1s); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 496 | if (to_use == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 497 | 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] | 498 | return 1; |
| 499 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 500 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 501 | 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] | 502 | } |
| 503 | |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 504 | int applypatch_flash(const char* source_filename, const char* target_filename, |
| 505 | const char* target_sha1_str, size_t target_size) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 506 | LOG(INFO) << "Flashing " << target_filename; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 507 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 508 | uint8_t target_sha1[SHA_DIGEST_LENGTH]; |
| 509 | if (ParseSha1(target_sha1_str, target_sha1) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 510 | LOG(ERROR) << "Failed to parse target SHA-1 \"" << target_sha1_str << "\""; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 511 | return 1; |
| 512 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 513 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 514 | std::string target_str(target_filename); |
| 515 | std::vector<std::string> pieces = android::base::Split(target_str, ":"); |
| 516 | if (pieces.size() != 2 || pieces[0] != "EMMC") { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 517 | LOG(ERROR) << "Invalid target name \"" << target_filename << "\""; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 518 | return 1; |
| 519 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 520 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 521 | // Load the target into the source_file object to see if already applied. |
| 522 | pieces.push_back(std::to_string(target_size)); |
| 523 | pieces.push_back(target_sha1_str); |
| 524 | std::string fullname = android::base::Join(pieces, ':'); |
| 525 | FileContents source_file; |
| 526 | if (LoadPartitionContents(fullname, &source_file) == 0 && |
| 527 | memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 528 | // The early-exit case: the image was already applied, this partition has the desired hash, |
| 529 | // nothing for us to do. |
| 530 | LOG(INFO) << " already " << short_sha1(target_sha1); |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 531 | return 0; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (LoadFileContents(source_filename, &source_file) == 0) { |
| 535 | if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
| 536 | // The source doesn't have desired checksum. |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 537 | LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum"; |
| 538 | LOG(ERROR) << "expected: " << short_sha1(target_sha1) |
| 539 | << ", found: " << short_sha1(source_file.sha1); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 540 | return 1; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 545 | LOG(ERROR) << "Failed to write copied data to " << target_filename; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 546 | return 1; |
| 547 | } |
| 548 | return 0; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 551 | static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch, |
| 552 | const std::string& target_filename, |
| 553 | const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) { |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 554 | if (patch->type != VAL_BLOB) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 555 | LOG(ERROR) << "patch is not a blob"; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 556 | return 1; |
| 557 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 558 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 559 | const char* header = &patch->data[0]; |
| 560 | size_t header_bytes_read = patch->data.size(); |
| 561 | bool use_bsdiff = false; |
| 562 | if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) { |
| 563 | use_bsdiff = true; |
| 564 | } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) { |
| 565 | use_bsdiff = false; |
| 566 | } else { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 567 | LOG(ERROR) << "Unknown patch file format"; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 568 | return 1; |
| 569 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 570 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 571 | CHECK(android::base::StartsWith(target_filename, "EMMC:")); |
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 | // We still write the original source to cache, in case the partition write is interrupted. |
| 574 | if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 575 | LOG(ERROR) << "Not enough free space on /cache"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 576 | return 1; |
| 577 | } |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 578 | if (SaveFileContents(Paths::Get().cache_temp_source(), &source_file) < 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 579 | LOG(ERROR) << "Failed to back up source file"; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 580 | return 1; |
| 581 | } |
| 582 | |
| 583 | // We store the decoded output in memory. |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 584 | std::string memory_sink_str; // Don't need to reserve space. |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 585 | SHA_CTX ctx; |
| 586 | SHA1_Init(&ctx); |
| 587 | 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] | 588 | if (len != 0) { |
| 589 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 590 | SHA1(data, len, digest); |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 591 | LOG(DEBUG) << "Appending " << len << " bytes data, SHA-1: " << short_sha1(digest); |
Tianjie Xu | ffed57a | 2018-04-23 17:51:45 -0700 | [diff] [blame] | 592 | } |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 593 | SHA1_Update(&ctx, data, len); |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 594 | memory_sink_str.append(reinterpret_cast<const char*>(data), len); |
| 595 | return len; |
| 596 | }; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 597 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 598 | int result; |
| 599 | if (use_bsdiff) { |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 600 | 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] | 601 | } else { |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 602 | result = |
| 603 | 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] | 604 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 605 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 606 | if (result != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 607 | LOG(ERROR) << "Failed to apply the patch: " << result; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 608 | return 1; |
| 609 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 610 | |
| 611 | uint8_t current_target_sha1[SHA_DIGEST_LENGTH]; |
| 612 | SHA1_Final(current_target_sha1, &ctx); |
| 613 | if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 614 | 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] | 615 | |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 616 | LOG(ERROR) << "target size " << memory_sink_str.size() << " SHA-1 " |
| 617 | << short_sha1(current_target_sha1); |
| 618 | LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 " |
| 619 | << short_sha1(source_file.sha1); |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 620 | |
| 621 | uint8_t patch_digest[SHA_DIGEST_LENGTH]; |
| 622 | 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] | 623 | 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] | 624 | |
| 625 | uint8_t bonus_digest[SHA_DIGEST_LENGTH]; |
| 626 | SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(), |
| 627 | bonus_digest); |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 628 | LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 " << short_sha1(bonus_digest); |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 629 | |
Tianjie Xu | 3f638ee | 2018-04-26 17:23:23 -0700 | [diff] [blame] | 630 | // TODO(b/67849209) Remove after debugging the unit test flakiness. |
| 631 | if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG) { |
| 632 | if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()), |
| 633 | memory_sink_str.size(), target_filename) != 0) { |
| 634 | LOG(DEBUG) << "Failed to write patched data " << target_filename; |
| 635 | } |
| 636 | } |
| 637 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 638 | return 1; |
| 639 | } else { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 640 | LOG(INFO) << " now " << short_sha1(target_sha1); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 643 | // Write back the temp file to the partition. |
| 644 | if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()), |
| 645 | memory_sink_str.size(), target_filename) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 646 | LOG(ERROR) << "Failed to write patched data to " << target_filename; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 647 | return 1; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 650 | // Delete the backup copy of the source. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 651 | unlink(Paths::Get().cache_temp_source().c_str()); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 652 | |
| 653 | // Success! |
| 654 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 655 | } |