| 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" | 
| Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 29 | #include "update_engine/update_metadata.pb.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/payload_consumer/payload_constants.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 | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 190 | template <typename PartitinoUpdateArray> | 
|  | 191 | bool InstallPlan::ParseManifestToInstallPlan( | 
|  | 192 | const PartitinoUpdateArray& partitions, | 
|  | 193 | BootControlInterface* boot_control, | 
|  | 194 | size_t block_size, | 
|  | 195 | InstallPlan* install_plan, | 
|  | 196 | ErrorCode* error) { | 
|  | 197 | // Fill in the InstallPlan::partitions based on the partitions from the | 
|  | 198 | // payload. | 
|  | 199 | for (const PartitionUpdate& partition : partitions) { | 
|  | 200 | InstallPlan::Partition install_part; | 
|  | 201 | install_part.name = partition.partition_name(); | 
|  | 202 | install_part.run_postinstall = | 
|  | 203 | partition.has_run_postinstall() && partition.run_postinstall(); | 
|  | 204 | if (install_part.run_postinstall) { | 
|  | 205 | install_part.postinstall_path = | 
|  | 206 | (partition.has_postinstall_path() ? partition.postinstall_path() | 
|  | 207 | : kPostinstallDefaultScript); | 
|  | 208 | install_part.filesystem_type = partition.filesystem_type(); | 
|  | 209 | install_part.postinstall_optional = partition.postinstall_optional(); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | if (partition.has_old_partition_info()) { | 
|  | 213 | const PartitionInfo& info = partition.old_partition_info(); | 
|  | 214 | install_part.source_size = info.size(); | 
|  | 215 | install_part.source_hash.assign(info.hash().begin(), info.hash().end()); | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | if (!partition.has_new_partition_info()) { | 
|  | 219 | LOG(ERROR) << "Unable to get new partition hash info on partition " | 
|  | 220 | << install_part.name << "."; | 
|  | 221 | *error = ErrorCode::kDownloadNewPartitionInfoError; | 
|  | 222 | return false; | 
|  | 223 | } | 
|  | 224 | const PartitionInfo& info = partition.new_partition_info(); | 
|  | 225 | install_part.target_size = info.size(); | 
|  | 226 | install_part.target_hash.assign(info.hash().begin(), info.hash().end()); | 
|  | 227 |  | 
|  | 228 | install_part.block_size = block_size; | 
|  | 229 | if (partition.has_hash_tree_extent()) { | 
|  | 230 | Extent extent = partition.hash_tree_data_extent(); | 
|  | 231 | install_part.hash_tree_data_offset = extent.start_block() * block_size; | 
|  | 232 | install_part.hash_tree_data_size = extent.num_blocks() * block_size; | 
|  | 233 | extent = partition.hash_tree_extent(); | 
|  | 234 | install_part.hash_tree_offset = extent.start_block() * block_size; | 
|  | 235 | install_part.hash_tree_size = extent.num_blocks() * block_size; | 
|  | 236 | uint64_t hash_tree_data_end = | 
|  | 237 | install_part.hash_tree_data_offset + install_part.hash_tree_data_size; | 
|  | 238 | if (install_part.hash_tree_offset < hash_tree_data_end) { | 
|  | 239 | LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at " | 
|  | 240 | << hash_tree_data_end << ", but hash tree starts at " | 
|  | 241 | << install_part.hash_tree_offset; | 
|  | 242 | *error = ErrorCode::kDownloadNewPartitionInfoError; | 
|  | 243 | return false; | 
|  | 244 | } | 
|  | 245 | install_part.hash_tree_algorithm = partition.hash_tree_algorithm(); | 
|  | 246 | install_part.hash_tree_salt.assign(partition.hash_tree_salt().begin(), | 
|  | 247 | partition.hash_tree_salt().end()); | 
|  | 248 | } | 
|  | 249 | if (partition.has_fec_extent()) { | 
|  | 250 | Extent extent = partition.fec_data_extent(); | 
|  | 251 | install_part.fec_data_offset = extent.start_block() * block_size; | 
|  | 252 | install_part.fec_data_size = extent.num_blocks() * block_size; | 
|  | 253 | extent = partition.fec_extent(); | 
|  | 254 | install_part.fec_offset = extent.start_block() * block_size; | 
|  | 255 | install_part.fec_size = extent.num_blocks() * block_size; | 
|  | 256 | uint64_t fec_data_end = | 
|  | 257 | install_part.fec_data_offset + install_part.fec_data_size; | 
|  | 258 | if (install_part.fec_offset < fec_data_end) { | 
|  | 259 | LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end | 
|  | 260 | << ", but fec starts at " << install_part.fec_offset; | 
|  | 261 | *error = ErrorCode::kDownloadNewPartitionInfoError; | 
|  | 262 | return false; | 
|  | 263 | } | 
|  | 264 | install_part.fec_roots = partition.fec_roots(); | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | install_plan->partitions.push_back(install_part); | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | // TODO(xunchang) only need to load the partitions for those in payload. | 
|  | 271 | // Because we have already loaded the other once when generating SOURCE_COPY | 
|  | 272 | // operations. | 
|  | 273 | if (!install_plan->LoadPartitionsFromSlots(boot_control)) { | 
|  | 274 | LOG(ERROR) << "Unable to determine all the partition devices."; | 
|  | 275 | *error = ErrorCode::kInstallDeviceOpenError; | 
|  | 276 | return false; | 
|  | 277 | } | 
|  | 278 | return true; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | bool InstallPlan::ParsePartitions( | 
|  | 282 | const std::vector<PartitionUpdate>& partitions, | 
|  | 283 | BootControlInterface* boot_control, | 
|  | 284 | size_t block_size, | 
|  | 285 | ErrorCode* error) { | 
|  | 286 | return ParseManifestToInstallPlan( | 
|  | 287 | partitions, boot_control, block_size, this, error); | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | bool InstallPlan::ParsePartitions( | 
|  | 291 | const google::protobuf::RepeatedPtrField<PartitionUpdate>& partitions, | 
|  | 292 | BootControlInterface* boot_control, | 
|  | 293 | size_t block_size, | 
|  | 294 | ErrorCode* error) { | 
|  | 295 | return ParseManifestToInstallPlan( | 
|  | 296 | partitions, boot_control, block_size, this, error); | 
|  | 297 | } | 
|  | 298 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 299 | }  // namespace chromeos_update_engine |