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