| 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 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 19 | #include <base/format_macros.h> | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 20 | #include <base/logging.h> | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 21 | #include <base/strings/string_number_conversions.h> | 
| Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 22 | #include <base/strings/string_util.h> | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 23 | #include <base/strings/stringprintf.h> | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 24 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/common/utils.h" | 
|  | 26 | #include "update_engine/payload_consumer/payload_constants.h" | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | using std::string; | 
|  | 29 |  | 
|  | 30 | namespace chromeos_update_engine { | 
|  | 31 |  | 
| Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 32 | string PayloadUrlsToString( | 
|  | 33 | const decltype(InstallPlan::Payload::payload_urls)& payload_urls) { | 
|  | 34 | return "(" + base::JoinString(payload_urls, ",") + ")"; | 
|  | 35 | } | 
|  | 36 |  | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 37 | string InstallPayloadTypeToString(InstallPayloadType type) { | 
|  | 38 | switch (type) { | 
|  | 39 | case InstallPayloadType::kUnknown: | 
|  | 40 | return "unknown"; | 
|  | 41 | case InstallPayloadType::kFull: | 
|  | 42 | return "full"; | 
|  | 43 | case InstallPayloadType::kDelta: | 
|  | 44 | return "delta"; | 
|  | 45 | } | 
|  | 46 | return "invalid type"; | 
|  | 47 | } | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | bool InstallPlan::operator==(const InstallPlan& that) const { | 
|  | 50 | return ((is_resume == that.is_resume) && | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 51 | (download_url == that.download_url) && (payloads == that.payloads) && | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 52 | (source_slot == that.source_slot) && | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 53 | (target_slot == that.target_slot) && (partitions == that.partitions)); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | bool InstallPlan::operator!=(const InstallPlan& that) const { | 
|  | 57 | return !((*this) == that); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | void InstallPlan::Dump() const { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 61 | string partitions_str; | 
|  | 62 | for (const auto& partition : partitions) { | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 63 | partitions_str += | 
|  | 64 | base::StringPrintf(", part: %s (source_size: %" PRIu64 | 
|  | 65 | ", target_size %" PRIu64 ", postinst:%s)", | 
|  | 66 | partition.name.c_str(), | 
|  | 67 | partition.source_size, | 
|  | 68 | partition.target_size, | 
|  | 69 | utils::ToString(partition.run_postinstall).c_str()); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 70 | } | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 71 | string payloads_str; | 
|  | 72 | for (const auto& payload : payloads) { | 
|  | 73 | payloads_str += base::StringPrintf( | 
| Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 74 | ", payload: (urls: %s, size: %" PRIu64 ", metadata_size: %" PRIu64 | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 75 | ", metadata signature: %s, hash: %s, payload type: %s)", | 
| Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 76 | PayloadUrlsToString(payload.payload_urls).c_str(), | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 77 | payload.size, | 
|  | 78 | payload.metadata_size, | 
|  | 79 | payload.metadata_signature.c_str(), | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 80 | base::HexEncode(payload.hash.data(), payload.hash.size()).c_str(), | 
|  | 81 | InstallPayloadTypeToString(payload.type).c_str()); | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 82 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 83 |  | 
| Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 84 | string version_str = base::StringPrintf(", version: %s", version.c_str()); | 
|  | 85 | if (!system_version.empty()) { | 
|  | 86 | version_str += | 
|  | 87 | base::StringPrintf(", system_version: %s", system_version.c_str()); | 
|  | 88 | } | 
|  | 89 |  | 
| Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 90 | string url_str = download_url; | 
|  | 91 | if (base::StartsWith( | 
|  | 92 | url_str, "fd://", base::CompareCase::INSENSITIVE_ASCII)) { | 
|  | 93 | int fd = std::stoi(url_str.substr(strlen("fd://"))); | 
|  | 94 | url_str = utils::GetFilePath(fd); | 
|  | 95 | } | 
|  | 96 |  | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 97 | LOG(INFO) << "InstallPlan: " << (is_resume ? "resume" : "new_update") | 
| Aaron Wood | 7dcdedf | 2017-09-06 17:17:41 -0700 | [diff] [blame] | 98 | << version_str | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 99 | << ", source_slot: " << BootControlInterface::SlotName(source_slot) | 
|  | 100 | << ", target_slot: " << BootControlInterface::SlotName(target_slot) | 
| Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 101 | << ", initial url: " << url_str << payloads_str | 
| Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 102 | << partitions_str << ", hash_checks_mandatory: " | 
| Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 103 | << utils::ToString(hash_checks_mandatory) | 
| Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 104 | << ", powerwash_required: " << utils::ToString(powerwash_required) | 
|  | 105 | << ", switch_slot_on_reboot: " | 
|  | 106 | << utils::ToString(switch_slot_on_reboot) | 
| Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 107 | << ", run_post_install: " << utils::ToString(run_post_install) | 
|  | 108 | << ", is_rollback: " << utils::ToString(is_rollback) | 
|  | 109 | << ", write_verity: " << utils::ToString(write_verity); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 112 | bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) { | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 113 | bool result = true; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 114 | for (Partition& partition : partitions) { | 
| Hridya Valsaraju | e69ca5f | 2019-02-25 22:33:56 -0800 | [diff] [blame] | 115 | if (source_slot != BootControlInterface::kInvalidSlot && | 
|  | 116 | partition.source_size > 0) { | 
| Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 117 | result = boot_control->GetPartitionDevice( | 
| Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 118 | partition.name, source_slot, &partition.source_path) && | 
|  | 119 | result; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 120 | } else { | 
|  | 121 | partition.source_path.clear(); | 
|  | 122 | } | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 123 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 124 | if (target_slot != BootControlInterface::kInvalidSlot && | 
|  | 125 | partition.target_size > 0) { | 
| Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 126 | result = boot_control->GetPartitionDevice( | 
| Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 127 | partition.name, target_slot, &partition.target_path) && | 
|  | 128 | result; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 129 | } else { | 
|  | 130 | partition.target_path.clear(); | 
|  | 131 | } | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 132 | } | 
|  | 133 | return result; | 
|  | 134 | } | 
|  | 135 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 136 | bool InstallPlan::Partition::operator==( | 
|  | 137 | const InstallPlan::Partition& that) const { | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 138 | return (name == that.name && source_path == that.source_path && | 
|  | 139 | source_size == that.source_size && source_hash == that.source_hash && | 
|  | 140 | target_path == that.target_path && target_size == that.target_size && | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 141 | target_hash == that.target_hash && | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 142 | run_postinstall == that.run_postinstall && | 
|  | 143 | postinstall_path == that.postinstall_path && | 
| Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 144 | filesystem_type == that.filesystem_type && | 
|  | 145 | postinstall_optional == that.postinstall_optional); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 148 | }  // namespace chromeos_update_engine |