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