Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2013 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 | // |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/install_plan.h" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 18 | |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <utility> |
| 21 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 22 | #include <base/format_macros.h> |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 23 | #include <base/logging.h> |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 24 | #include <base/strings/string_number_conversions.h> |
Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 25 | #include <base/strings/string_util.h> |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 26 | #include <base/strings/stringprintf.h> |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/utils.h" |
| 29 | #include "update_engine/payload_consumer/payload_constants.h" |
Colin Cross | 26b82b1 | 2021-12-22 10:09:19 -0800 | [diff] [blame] | 30 | #include "update_engine/update_metadata.pb.h" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 31 | |
| 32 | using std::string; |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 33 | using std::vector; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 37 | namespace { |
Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 38 | string PayloadUrlsToString( |
| 39 | const decltype(InstallPlan::Payload::payload_urls)& payload_urls) { |
| 40 | return "(" + base::JoinString(payload_urls, ",") + ")"; |
| 41 | } |
| 42 | |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 43 | string VectorToString(const vector<std::pair<string, string>>& input, |
| 44 | const string& separator) { |
| 45 | vector<string> vec; |
| 46 | std::transform(input.begin(), |
| 47 | input.end(), |
| 48 | std::back_inserter(vec), |
| 49 | [](const auto& pair) { |
| 50 | return base::JoinString({pair.first, pair.second}, ": "); |
| 51 | }); |
| 52 | return base::JoinString(vec, separator); |
| 53 | } |
| 54 | } // namespace |
| 55 | |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 56 | string InstallPayloadTypeToString(InstallPayloadType type) { |
| 57 | switch (type) { |
| 58 | case InstallPayloadType::kUnknown: |
| 59 | return "unknown"; |
| 60 | case InstallPayloadType::kFull: |
| 61 | return "full"; |
| 62 | case InstallPayloadType::kDelta: |
| 63 | return "delta"; |
| 64 | } |
| 65 | return "invalid type"; |
| 66 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 67 | |
| 68 | bool InstallPlan::operator==(const InstallPlan& that) const { |
| 69 | return ((is_resume == that.is_resume) && |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 70 | (download_url == that.download_url) && (payloads == that.payloads) && |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 71 | (source_slot == that.source_slot) && |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 72 | (target_slot == that.target_slot) && (partitions == that.partitions)); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool InstallPlan::operator!=(const InstallPlan& that) const { |
| 76 | return !((*this) == that); |
| 77 | } |
| 78 | |
| 79 | void InstallPlan::Dump() const { |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 80 | LOG(INFO) << "InstallPlan: \n" << ToString(); |
| 81 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 82 | |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 83 | string InstallPlan::ToString() const { |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 84 | string url_str = download_url; |
| 85 | if (base::StartsWith( |
| 86 | url_str, "fd://", base::CompareCase::INSENSITIVE_ASCII)) { |
| 87 | int fd = std::stoi(url_str.substr(strlen("fd://"))); |
| 88 | url_str = utils::GetFilePath(fd); |
| 89 | } |
| 90 | |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 91 | vector<string> result_str; |
| 92 | result_str.emplace_back(VectorToString( |
| 93 | { |
| 94 | {"type", (is_resume ? "resume" : "new_update")}, |
| 95 | {"version", version}, |
| 96 | {"source_slot", BootControlInterface::SlotName(source_slot)}, |
| 97 | {"target_slot", BootControlInterface::SlotName(target_slot)}, |
| 98 | {"initial url", url_str}, |
| 99 | {"hash_checks_mandatory", utils::ToString(hash_checks_mandatory)}, |
| 100 | {"powerwash_required", utils::ToString(powerwash_required)}, |
| 101 | {"switch_slot_on_reboot", utils::ToString(switch_slot_on_reboot)}, |
| 102 | {"run_post_install", utils::ToString(run_post_install)}, |
| 103 | {"is_rollback", utils::ToString(is_rollback)}, |
| 104 | {"rollback_data_save_requested", |
| 105 | utils::ToString(rollback_data_save_requested)}, |
| 106 | {"write_verity", utils::ToString(write_verity)}, |
| 107 | }, |
| 108 | "\n")); |
| 109 | |
| 110 | for (const auto& partition : partitions) { |
| 111 | result_str.emplace_back(VectorToString( |
| 112 | { |
| 113 | {"Partition", partition.name}, |
| 114 | {"source_size", base::NumberToString(partition.source_size)}, |
| 115 | {"source_path", partition.source_path}, |
| 116 | {"source_hash", |
| 117 | base::HexEncode(partition.source_hash.data(), |
| 118 | partition.source_hash.size())}, |
| 119 | {"target_size", base::NumberToString(partition.target_size)}, |
| 120 | {"target_path", partition.target_path}, |
| 121 | {"target_hash", |
| 122 | base::HexEncode(partition.target_hash.data(), |
| 123 | partition.target_hash.size())}, |
| 124 | {"run_postinstall", utils::ToString(partition.run_postinstall)}, |
| 125 | {"postinstall_path", partition.postinstall_path}, |
Kelvin Zhang | a9b5d8c | 2021-05-05 09:17:46 -0400 | [diff] [blame] | 126 | {"readonly_target_path", partition.readonly_target_path}, |
Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 127 | {"filesystem_type", partition.filesystem_type}, |
| 128 | }, |
| 129 | "\n ")); |
| 130 | } |
| 131 | |
| 132 | for (unsigned int i = 0; i < payloads.size(); ++i) { |
| 133 | const auto& payload = payloads[i]; |
| 134 | result_str.emplace_back(VectorToString( |
| 135 | { |
| 136 | {"Payload", base::NumberToString(i)}, |
| 137 | {"urls", PayloadUrlsToString(payload.payload_urls)}, |
| 138 | {"size", base::NumberToString(payload.size)}, |
| 139 | {"metadata_size", base::NumberToString(payload.metadata_size)}, |
| 140 | {"metadata_signature", payload.metadata_signature}, |
| 141 | {"hash", base::HexEncode(payload.hash.data(), payload.hash.size())}, |
| 142 | {"type", InstallPayloadTypeToString(payload.type)}, |
| 143 | {"fingerprint", payload.fp}, |
| 144 | {"app_id", payload.app_id}, |
| 145 | {"already_applied", utils::ToString(payload.already_applied)}, |
| 146 | }, |
| 147 | "\n ")); |
| 148 | } |
| 149 | |
| 150 | return base::JoinString(result_str, "\n"); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 153 | bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) { |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 154 | bool result = true; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 155 | for (Partition& partition : partitions) { |
Hridya Valsaraju | e69ca5f | 2019-02-25 22:33:56 -0800 | [diff] [blame] | 156 | if (source_slot != BootControlInterface::kInvalidSlot && |
| 157 | partition.source_size > 0) { |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 158 | TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice( |
| 159 | partition.name, source_slot, &partition.source_path)); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 160 | } else { |
| 161 | partition.source_path.clear(); |
| 162 | } |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 163 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 164 | if (target_slot != BootControlInterface::kInvalidSlot && |
| 165 | partition.target_size > 0) { |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 166 | auto device = boot_control->GetPartitionDevice( |
| 167 | partition.name, target_slot, source_slot); |
| 168 | TEST_AND_RETURN_FALSE(device.has_value()); |
| 169 | partition.target_path = device->rw_device_path; |
Kelvin Zhang | a9b5d8c | 2021-05-05 09:17:46 -0400 | [diff] [blame] | 170 | partition.readonly_target_path = device->readonly_device_path; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 171 | } else { |
| 172 | partition.target_path.clear(); |
| 173 | } |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 174 | } |
| 175 | return result; |
| 176 | } |
| 177 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 178 | bool InstallPlan::Partition::operator==( |
| 179 | const InstallPlan::Partition& that) const { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 180 | return (name == that.name && source_path == that.source_path && |
| 181 | source_size == that.source_size && source_hash == that.source_hash && |
| 182 | target_path == that.target_path && target_size == that.target_size && |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 183 | target_hash == that.target_hash && |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 184 | run_postinstall == that.run_postinstall && |
| 185 | postinstall_path == that.postinstall_path && |
Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 186 | filesystem_type == that.filesystem_type && |
| 187 | postinstall_optional == that.postinstall_optional); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Kelvin Zhang | 91e839c | 2022-04-05 14:25:00 -0700 | [diff] [blame^] | 190 | bool InstallPlan::Partition::ParseVerityConfig( |
| 191 | const PartitionUpdate& partition) { |
| 192 | if (partition.has_hash_tree_extent()) { |
| 193 | Extent extent = partition.hash_tree_data_extent(); |
| 194 | hash_tree_data_offset = extent.start_block() * block_size; |
| 195 | hash_tree_data_size = extent.num_blocks() * block_size; |
| 196 | extent = partition.hash_tree_extent(); |
| 197 | hash_tree_offset = extent.start_block() * block_size; |
| 198 | hash_tree_size = extent.num_blocks() * block_size; |
| 199 | uint64_t hash_tree_data_end = hash_tree_data_offset + hash_tree_data_size; |
| 200 | if (hash_tree_offset < hash_tree_data_end) { |
| 201 | LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at " |
| 202 | << hash_tree_data_end << ", but hash tree starts at " |
| 203 | << hash_tree_offset; |
| 204 | return false; |
| 205 | } |
| 206 | hash_tree_algorithm = partition.hash_tree_algorithm(); |
| 207 | hash_tree_salt.assign(partition.hash_tree_salt().begin(), |
| 208 | partition.hash_tree_salt().end()); |
| 209 | } |
| 210 | if (partition.has_fec_extent()) { |
| 211 | Extent extent = partition.fec_data_extent(); |
| 212 | fec_data_offset = extent.start_block() * block_size; |
| 213 | fec_data_size = extent.num_blocks() * block_size; |
| 214 | extent = partition.fec_extent(); |
| 215 | fec_offset = extent.start_block() * block_size; |
| 216 | fec_size = extent.num_blocks() * block_size; |
| 217 | uint64_t fec_data_end = fec_data_offset + fec_data_size; |
| 218 | if (fec_offset < fec_data_end) { |
| 219 | LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end |
| 220 | << ", but fec starts at " << fec_offset; |
| 221 | return false; |
| 222 | } |
| 223 | fec_roots = partition.fec_roots(); |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 228 | template <typename PartitinoUpdateArray> |
| 229 | bool InstallPlan::ParseManifestToInstallPlan( |
| 230 | const PartitinoUpdateArray& partitions, |
| 231 | BootControlInterface* boot_control, |
| 232 | size_t block_size, |
| 233 | InstallPlan* install_plan, |
| 234 | ErrorCode* error) { |
| 235 | // Fill in the InstallPlan::partitions based on the partitions from the |
| 236 | // payload. |
| 237 | for (const PartitionUpdate& partition : partitions) { |
| 238 | InstallPlan::Partition install_part; |
| 239 | install_part.name = partition.partition_name(); |
| 240 | install_part.run_postinstall = |
| 241 | partition.has_run_postinstall() && partition.run_postinstall(); |
| 242 | if (install_part.run_postinstall) { |
| 243 | install_part.postinstall_path = |
| 244 | (partition.has_postinstall_path() ? partition.postinstall_path() |
| 245 | : kPostinstallDefaultScript); |
| 246 | install_part.filesystem_type = partition.filesystem_type(); |
| 247 | install_part.postinstall_optional = partition.postinstall_optional(); |
| 248 | } |
| 249 | |
| 250 | if (partition.has_old_partition_info()) { |
| 251 | const PartitionInfo& info = partition.old_partition_info(); |
| 252 | install_part.source_size = info.size(); |
| 253 | install_part.source_hash.assign(info.hash().begin(), info.hash().end()); |
| 254 | } |
| 255 | |
| 256 | if (!partition.has_new_partition_info()) { |
| 257 | LOG(ERROR) << "Unable to get new partition hash info on partition " |
| 258 | << install_part.name << "."; |
| 259 | *error = ErrorCode::kDownloadNewPartitionInfoError; |
| 260 | return false; |
| 261 | } |
| 262 | const PartitionInfo& info = partition.new_partition_info(); |
| 263 | install_part.target_size = info.size(); |
| 264 | install_part.target_hash.assign(info.hash().begin(), info.hash().end()); |
| 265 | |
| 266 | install_part.block_size = block_size; |
Kelvin Zhang | 91e839c | 2022-04-05 14:25:00 -0700 | [diff] [blame^] | 267 | if (!install_part.ParseVerityConfig(partition)) { |
| 268 | *error = ErrorCode::kDownloadNewPartitionInfoError; |
| 269 | LOG(INFO) << "Failed to parse partition `" << partition.partition_name() |
| 270 | << "` verity configs"; |
| 271 | return false; |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | install_plan->partitions.push_back(install_part); |
| 275 | } |
| 276 | |
| 277 | // TODO(xunchang) only need to load the partitions for those in payload. |
| 278 | // Because we have already loaded the other once when generating SOURCE_COPY |
| 279 | // operations. |
| 280 | if (!install_plan->LoadPartitionsFromSlots(boot_control)) { |
| 281 | LOG(ERROR) << "Unable to determine all the partition devices."; |
| 282 | *error = ErrorCode::kInstallDeviceOpenError; |
| 283 | return false; |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | bool InstallPlan::ParsePartitions( |
| 289 | const std::vector<PartitionUpdate>& partitions, |
| 290 | BootControlInterface* boot_control, |
| 291 | size_t block_size, |
| 292 | ErrorCode* error) { |
| 293 | return ParseManifestToInstallPlan( |
| 294 | partitions, boot_control, block_size, this, error); |
| 295 | } |
| 296 | |
| 297 | bool InstallPlan::ParsePartitions( |
| 298 | const google::protobuf::RepeatedPtrField<PartitionUpdate>& partitions, |
| 299 | BootControlInterface* boot_control, |
| 300 | size_t block_size, |
| 301 | ErrorCode* error) { |
| 302 | return ParseManifestToInstallPlan( |
| 303 | partitions, boot_control, block_size, this, error); |
| 304 | } |
| 305 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 306 | } // namespace chromeos_update_engine |