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