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 | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 47 | using namespace std::string_literals; |
| 48 | |
| 49 | static bool GenerateTarget(const Partition& target, const FileContents& source_file, |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 50 | const Value& patch, const Value* bonus_data, bool backup_source); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 51 | |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 52 | bool LoadFileContents(const std::string& filename, FileContents* file) { |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 53 | // No longer allow loading contents from eMMC partitions. |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 54 | if (android::base::StartsWith(filename, "EMMC:")) { |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 55 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 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 | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 61 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 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); |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 66 | return true; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 69 | // Reads the contents of a Partition to the given FileContents buffer. |
| 70 | static bool ReadPartitionToBuffer(const Partition& partition, FileContents* out, |
| 71 | bool check_backup) { |
| 72 | uint8_t expected_sha1[SHA_DIGEST_LENGTH]; |
| 73 | if (ParseSha1(partition.hash, expected_sha1) != 0) { |
| 74 | LOG(ERROR) << "Failed to parse target hash \"" << partition.hash << "\""; |
| 75 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 78 | android::base::unique_fd dev(open(partition.name.c_str(), O_RDONLY)); |
Bernie Innocenti | 8bd6f45 | 2019-03-28 15:48:08 +0900 | [diff] [blame] | 79 | if (dev == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 80 | PLOG(ERROR) << "Failed to open eMMC partition \"" << partition << "\""; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 81 | } else { |
| 82 | std::vector<unsigned char> buffer(partition.size); |
| 83 | if (!android::base::ReadFully(dev, buffer.data(), buffer.size())) { |
| 84 | PLOG(ERROR) << "Failed to read " << buffer.size() << " bytes of data for partition " |
| 85 | << partition; |
| 86 | } else { |
| 87 | SHA1(buffer.data(), buffer.size(), out->sha1); |
| 88 | if (memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) { |
| 89 | out->data = std::move(buffer); |
| 90 | return true; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 91 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 92 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 93 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 94 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 95 | if (!check_backup) { |
| 96 | LOG(ERROR) << "Partition contents don't have the expected checksum"; |
| 97 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 98 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 99 | |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 100 | if (LoadFileContents(Paths::Get().cache_temp_source(), out) && |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 101 | memcmp(out->sha1, expected_sha1, SHA_DIGEST_LENGTH) == 0) { |
| 102 | return true; |
| 103 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 104 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 105 | LOG(ERROR) << "Both of partition contents and backup don't have the expected checksum"; |
| 106 | return false; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 109 | bool SaveFileContents(const std::string& filename, const FileContents* file) { |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 110 | android::base::unique_fd fd( |
| 111 | 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] | 112 | if (fd == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 113 | PLOG(ERROR) << "Failed to open \"" << filename << "\" for write"; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 114 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 115 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 116 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 117 | if (!android::base::WriteFully(fd, file->data.data(), file->data.size())) { |
| 118 | PLOG(ERROR) << "Failed to write " << file->data.size() << " bytes of data to " << filename; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 119 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 120 | } |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 121 | |
| 122 | if (fsync(fd) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 123 | PLOG(ERROR) << "Failed to fsync \"" << filename << "\""; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 124 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 125 | } |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 126 | |
| 127 | if (close(fd.release()) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 128 | PLOG(ERROR) << "Failed to close \"" << filename << "\""; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 129 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 130 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 131 | |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 132 | return true; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 135 | // Writes a memory buffer to 'target' Partition. |
| 136 | static bool WriteBufferToPartition(const FileContents& file_contents, const Partition& partition) { |
| 137 | const unsigned char* data = file_contents.data.data(); |
| 138 | size_t len = file_contents.data.size(); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 139 | size_t start = 0; |
| 140 | bool success = false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 141 | for (size_t attempt = 0; attempt < 2; ++attempt) { |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 142 | android::base::unique_fd fd(open(partition.name.c_str(), O_RDWR)); |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 143 | if (fd == -1) { |
| 144 | PLOG(ERROR) << "Failed to open \"" << partition << "\""; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 145 | return false; |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 148 | if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 149 | PLOG(ERROR) << "Failed to seek to " << start << " on \"" << partition << "\""; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 150 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 151 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 152 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 153 | if (!android::base::WriteFully(fd, data + start, len - start)) { |
| 154 | PLOG(ERROR) << "Failed to write " << len - start << " bytes to \"" << partition << "\""; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 155 | return false; |
Tao Bao | aca8e89 | 2015-07-17 11:47:44 -0700 | [diff] [blame] | 156 | } |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 157 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 158 | if (fsync(fd) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 159 | PLOG(ERROR) << "Failed to sync \"" << partition << "\""; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 160 | return false; |
Doug Zongker | f291d85 | 2010-07-07 13:55:25 -0700 | [diff] [blame] | 161 | } |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 162 | if (close(fd.release()) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 163 | PLOG(ERROR) << "Failed to close \"" << partition << "\""; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 164 | return false; |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 165 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 166 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 167 | fd.reset(open(partition.name.c_str(), O_RDONLY)); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 168 | if (fd == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 169 | PLOG(ERROR) << "Failed to reopen \"" << partition << "\" for verification"; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 170 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // 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] | 174 | sync(); |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 175 | std::string drop_cache = "/proc/sys/vm/drop_caches"; |
| 176 | if (!android::base::WriteStringToFile("3\n", drop_cache)) { |
| 177 | PLOG(ERROR) << "Failed to write to " << drop_cache; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 178 | } else { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 179 | LOG(INFO) << " caches dropped"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 180 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 181 | sleep(1); |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 182 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 183 | // Verify. |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 184 | if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 185 | PLOG(ERROR) << "Failed to seek to 0 on " << partition; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 186 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | unsigned char buffer[4096]; |
| 190 | start = len; |
| 191 | for (size_t p = 0; p < len; p += sizeof(buffer)) { |
| 192 | size_t to_read = len - p; |
| 193 | if (to_read > sizeof(buffer)) { |
| 194 | to_read = sizeof(buffer); |
| 195 | } |
| 196 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 197 | if (!android::base::ReadFully(fd, buffer, to_read)) { |
| 198 | PLOG(ERROR) << "Failed to verify-read " << partition << " at " << p; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 199 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (memcmp(buffer, data + p, to_read) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 203 | LOG(ERROR) << "Verification failed starting at " << p; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 204 | start = p; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (start == len) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 210 | LOG(INFO) << "Verification read succeeded (attempt " << attempt + 1 << ")"; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 211 | success = true; |
| 212 | break; |
| 213 | } |
katao | 9a6f520 | 2016-12-19 11:22:30 +0800 | [diff] [blame] | 214 | |
Tianjie Xu | 22f1120 | 2018-08-27 10:50:31 -0700 | [diff] [blame] | 215 | if (close(fd.release()) != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 216 | PLOG(ERROR) << "Failed to close " << partition; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 217 | return false; |
katao | 9a6f520 | 2016-12-19 11:22:30 +0800 | [diff] [blame] | 218 | } |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (!success) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 222 | LOG(ERROR) << "Failed to verify after all attempts"; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 223 | return false; |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Tao Bao | 8fce75a | 2016-11-10 12:33:41 -0800 | [diff] [blame] | 226 | sync(); |
| 227 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 228 | return true; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Tao Bao | 8dc7049 | 2018-06-20 10:14:40 -0700 | [diff] [blame] | 231 | int ParseSha1(const std::string& str, uint8_t* digest) { |
| 232 | const char* ps = str.c_str(); |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 233 | uint8_t* pd = digest; |
| 234 | for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) { |
| 235 | int digit; |
| 236 | if (*ps >= '0' && *ps <= '9') { |
| 237 | digit = *ps - '0'; |
| 238 | } else if (*ps >= 'a' && *ps <= 'f') { |
| 239 | digit = *ps - 'a' + 10; |
| 240 | } else if (*ps >= 'A' && *ps <= 'F') { |
| 241 | digit = *ps - 'A' + 10; |
| 242 | } else { |
| 243 | return -1; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 244 | } |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 245 | if (i % 2 == 0) { |
| 246 | *pd = digit << 4; |
| 247 | } else { |
| 248 | *pd |= digit; |
| 249 | ++pd; |
| 250 | } |
| 251 | } |
| 252 | if (*ps != '\0') return -1; |
| 253 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 256 | bool PatchPartitionCheck(const Partition& target, const Partition& source) { |
| 257 | FileContents target_file; |
| 258 | FileContents source_file; |
| 259 | return (ReadPartitionToBuffer(target, &target_file, false) || |
| 260 | ReadPartitionToBuffer(source, &source_file, true)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | int ShowLicenses() { |
Tao Bao | 155771b | 2018-06-05 11:26:01 -0700 | [diff] [blame] | 264 | ShowBSDiffLicense(); |
| 265 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 268 | bool PatchPartition(const Partition& target, const Partition& source, const Value& patch, |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 269 | const Value* bonus, bool backup_source) { |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 270 | LOG(INFO) << "Patching " << target.name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 271 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 272 | // We try to load and check against the target hash first. |
| 273 | FileContents target_file; |
| 274 | if (ReadPartitionToBuffer(target, &target_file, false)) { |
| 275 | // The early-exit case: the patch was already applied, this file has the desired hash, nothing |
| 276 | // for us to do. |
| 277 | LOG(INFO) << " already " << target.hash.substr(0, 8); |
| 278 | return true; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 281 | FileContents source_file; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 282 | if (ReadPartitionToBuffer(source, &source_file, true)) { |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 283 | return GenerateTarget(target, source_file, patch, bonus, backup_source); |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 284 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 285 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 286 | LOG(ERROR) << "Failed to find any match"; |
| 287 | return false; |
Doug Zongker | 1c43c97 | 2012-02-28 11:07:09 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 290 | bool FlashPartition(const Partition& partition, const std::string& source_filename) { |
| 291 | LOG(INFO) << "Flashing " << partition; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 292 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 293 | // We try to load and check against the target hash first. |
| 294 | FileContents target_file; |
| 295 | if (ReadPartitionToBuffer(partition, &target_file, false)) { |
| 296 | // The early-exit case: the patch was already applied, this file has the desired hash, nothing |
| 297 | // for us to do. |
| 298 | LOG(INFO) << " already " << partition.hash.substr(0, 8); |
| 299 | return true; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 300 | } |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 301 | |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 302 | FileContents source_file; |
Tao Bao | 09e8493 | 2018-08-31 11:25:05 -0700 | [diff] [blame] | 303 | if (!LoadFileContents(source_filename, &source_file)) { |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 304 | LOG(ERROR) << "Failed to load source file"; |
| 305 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 308 | uint8_t expected_sha1[SHA_DIGEST_LENGTH]; |
| 309 | if (ParseSha1(partition.hash, expected_sha1) != 0) { |
| 310 | LOG(ERROR) << "Failed to parse source hash \"" << partition.hash << "\""; |
| 311 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 314 | if (memcmp(source_file.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) { |
| 315 | // The source doesn't have desired checksum. |
| 316 | LOG(ERROR) << "source \"" << source_filename << "\" doesn't have expected SHA-1 sum"; |
| 317 | LOG(ERROR) << "expected: " << partition.hash.substr(0, 8) |
| 318 | << ", found: " << short_sha1(source_file.sha1); |
| 319 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 320 | } |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 321 | if (!WriteBufferToPartition(source_file, partition)) { |
| 322 | LOG(ERROR) << "Failed to write to " << partition; |
| 323 | return false; |
| 324 | } |
| 325 | return true; |
Tao Bao | abba55b | 2015-07-17 18:11:12 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 328 | static bool GenerateTarget(const Partition& target, const FileContents& source_file, |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 329 | const Value& patch, const Value* bonus_data, bool backup_source) { |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 330 | uint8_t expected_sha1[SHA_DIGEST_LENGTH]; |
| 331 | if (ParseSha1(target.hash, expected_sha1) != 0) { |
| 332 | LOG(ERROR) << "Failed to parse target hash \"" << target.hash << "\""; |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | if (patch.type != Value::Type::BLOB) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 337 | LOG(ERROR) << "patch is not a blob"; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 338 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 339 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 340 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 341 | const char* header = patch.data.data(); |
| 342 | size_t header_bytes_read = patch.data.size(); |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 343 | bool use_bsdiff = false; |
| 344 | if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) { |
| 345 | use_bsdiff = true; |
| 346 | } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) { |
| 347 | use_bsdiff = false; |
| 348 | } else { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 349 | LOG(ERROR) << "Unknown patch file format"; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 350 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 351 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 352 | |
Tao Bao | 5ee2566 | 2018-07-11 15:55:32 -0700 | [diff] [blame] | 353 | // We write the original source to cache, in case the partition write is interrupted. |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 354 | if (backup_source && !CheckAndFreeSpaceOnCache(source_file.data.size())) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 355 | LOG(ERROR) << "Not enough free space on /cache"; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 356 | return false; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 357 | } |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 358 | if (backup_source && !SaveFileContents(Paths::Get().cache_temp_source(), &source_file)) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 359 | LOG(ERROR) << "Failed to back up source file"; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 360 | return false; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // We store the decoded output in memory. |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 364 | FileContents patched; |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 365 | SHA_CTX ctx; |
| 366 | SHA1_Init(&ctx); |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 367 | SinkFn sink = [&patched, &ctx](const unsigned char* data, size_t len) { |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 368 | SHA1_Update(&ctx, data, len); |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 369 | patched.data.insert(patched.data.end(), data, data + len); |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 370 | return len; |
| 371 | }; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 372 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 373 | int result; |
| 374 | if (use_bsdiff) { |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 375 | 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] | 376 | } else { |
Tao Bao | 8b0b0f1 | 2018-04-19 21:02:13 -0700 | [diff] [blame] | 377 | result = |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 378 | 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] | 379 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 380 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 381 | if (result != 0) { |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 382 | LOG(ERROR) << "Failed to apply the patch: " << result; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 383 | return false; |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 384 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 385 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 386 | SHA1_Final(patched.sha1, &ctx); |
| 387 | if (memcmp(patched.sha1, expected_sha1, SHA_DIGEST_LENGTH) != 0) { |
| 388 | LOG(ERROR) << "Patching did not produce the expected SHA-1 of " << short_sha1(expected_sha1); |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 389 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 390 | LOG(ERROR) << "target size " << patched.data.size() << " SHA-1 " << short_sha1(patched.sha1); |
Tao Bao | 859bfc5 | 2018-04-25 23:00:27 -0700 | [diff] [blame] | 391 | LOG(ERROR) << "source size " << source_file.data.size() << " SHA-1 " |
| 392 | << short_sha1(source_file.sha1); |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 393 | |
| 394 | uint8_t patch_digest[SHA_DIGEST_LENGTH]; |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 395 | SHA1(reinterpret_cast<const uint8_t*>(patch.data.data()), patch.data.size(), patch_digest); |
| 396 | 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] | 397 | |
Tao Bao | 7ea515e | 2018-07-09 15:16:13 -0700 | [diff] [blame] | 398 | if (bonus_data != nullptr) { |
| 399 | uint8_t bonus_digest[SHA_DIGEST_LENGTH]; |
| 400 | SHA1(reinterpret_cast<const uint8_t*>(bonus_data->data.data()), bonus_data->data.size(), |
| 401 | bonus_digest); |
| 402 | LOG(ERROR) << "bonus size " << bonus_data->data.size() << " SHA-1 " |
| 403 | << short_sha1(bonus_digest); |
| 404 | } |
Tao Bao | 4f83430 | 2018-04-19 12:35:14 -0700 | [diff] [blame] | 405 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 406 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 409 | LOG(INFO) << " now " << short_sha1(expected_sha1); |
| 410 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 411 | // Write back the temp file to the partition. |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 412 | if (!WriteBufferToPartition(patched, target)) { |
| 413 | LOG(ERROR) << "Failed to write patched data to " << target.name; |
| 414 | return false; |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Tao Bao | 40e144d | 2017-03-15 01:10:58 -0700 | [diff] [blame] | 417 | // Delete the backup copy of the source. |
Tao Bao | 5234ad4 | 2019-09-23 10:28:54 -0700 | [diff] [blame] | 418 | if (backup_source) { |
| 419 | unlink(Paths::Get().cache_temp_source().c_str()); |
| 420 | } |
Tao Bao | 6e02ea9 | 2016-11-17 11:24:07 -0800 | [diff] [blame] | 421 | |
| 422 | // Success! |
Tao Bao | 5609bc8 | 2018-06-20 00:30:48 -0700 | [diff] [blame] | 423 | return true; |
| 424 | } |
| 425 | |
| 426 | bool CheckPartition(const Partition& partition) { |
| 427 | FileContents target_file; |
| 428 | return ReadPartitionToBuffer(partition, &target_file, false); |
| 429 | } |
| 430 | |
| 431 | Partition Partition::Parse(const std::string& input_str, std::string* err) { |
| 432 | std::vector<std::string> pieces = android::base::Split(input_str, ":"); |
| 433 | if (pieces.size() != 4 || pieces[0] != "EMMC") { |
| 434 | *err = "Invalid number of tokens or non-eMMC target"; |
| 435 | return {}; |
| 436 | } |
| 437 | |
| 438 | size_t size; |
| 439 | if (!android::base::ParseUint(pieces[2], &size) || size == 0) { |
| 440 | *err = "Failed to parse \"" + pieces[2] + "\" as byte count"; |
| 441 | return {}; |
| 442 | } |
| 443 | |
| 444 | return Partition(pieces[1], size, pieces[3]); |
| 445 | } |
| 446 | |
| 447 | std::string Partition::ToString() const { |
| 448 | if (*this) { |
| 449 | return "EMMC:"s + name + ":" + std::to_string(size) + ":" + hash; |
| 450 | } |
| 451 | return "<invalid-partition>"; |
| 452 | } |
| 453 | |
| 454 | std::ostream& operator<<(std::ostream& os, const Partition& partition) { |
| 455 | os << partition.ToString(); |
| 456 | return os; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 457 | } |