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 //
16 
17 #include "update_engine/payload_consumer/install_plan.h"
18 
19 #include <base/format_macros.h>
20 #include <base/logging.h>
21 #include <base/strings/string_number_conversions.h>
22 #include <base/strings/string_util.h>
23 #include <base/strings/stringprintf.h>
24 
25 #include "update_engine/common/utils.h"
26 #include "update_engine/payload_consumer/payload_constants.h"
27 
28 using std::string;
29 
30 namespace chromeos_update_engine {
31 
PayloadUrlsToString(const decltype(InstallPlan::Payload::payload_urls) & payload_urls)32 string PayloadUrlsToString(
33     const decltype(InstallPlan::Payload::payload_urls)& payload_urls) {
34   return "(" + base::JoinString(payload_urls, ",") + ")";
35 }
36 
InstallPayloadTypeToString(InstallPayloadType type)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 }
48 
operator ==(const InstallPlan & that) const49 bool InstallPlan::operator==(const InstallPlan& that) const {
50   return ((is_resume == that.is_resume) &&
51           (download_url == that.download_url) && (payloads == that.payloads) &&
52           (source_slot == that.source_slot) &&
53           (target_slot == that.target_slot) && (partitions == that.partitions));
54 }
55 
operator !=(const InstallPlan & that) const56 bool InstallPlan::operator!=(const InstallPlan& that) const {
57   return !((*this) == that);
58 }
59 
Dump() const60 void InstallPlan::Dump() const {
61   string partitions_str;
62   for (const auto& partition : partitions) {
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());
70   }
71   string payloads_str;
72   for (const auto& payload : payloads) {
73     payloads_str += base::StringPrintf(
74         ", payload: (urls: %s, size: %" PRIu64 ", metadata_size: %" PRIu64
75         ", metadata signature: %s, hash: %s, payload type: %s)",
76         PayloadUrlsToString(payload.payload_urls).c_str(),
77         payload.size,
78         payload.metadata_size,
79         payload.metadata_signature.c_str(),
80         base::HexEncode(payload.hash.data(), payload.hash.size()).c_str(),
81         InstallPayloadTypeToString(payload.type).c_str());
82   }
83 
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 
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 
97   LOG(INFO) << "InstallPlan: " << (is_resume ? "resume" : "new_update")
98             << version_str
99             << ", source_slot: " << BootControlInterface::SlotName(source_slot)
100             << ", target_slot: " << BootControlInterface::SlotName(target_slot)
101             << ", initial url: " << url_str << payloads_str
102             << partitions_str << ", hash_checks_mandatory: "
103             << utils::ToString(hash_checks_mandatory)
104             << ", powerwash_required: " << utils::ToString(powerwash_required)
105             << ", switch_slot_on_reboot: "
106             << utils::ToString(switch_slot_on_reboot)
107             << ", run_post_install: " << utils::ToString(run_post_install)
108             << ", is_rollback: " << utils::ToString(is_rollback)
109             << ", write_verity: " << utils::ToString(write_verity);
110 }
111 
LoadPartitionsFromSlots(BootControlInterface * boot_control)112 bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
113   bool result = true;
114   for (Partition& partition : partitions) {
115     if (source_slot != BootControlInterface::kInvalidSlot &&
116         partition.source_size > 0) {
117       result = boot_control->GetPartitionDevice(
118                    partition.name, source_slot, &partition.source_path) &&
119                result;
120     } else {
121       partition.source_path.clear();
122     }
123 
124     if (target_slot != BootControlInterface::kInvalidSlot &&
125         partition.target_size > 0) {
126       result = boot_control->GetPartitionDevice(
127                    partition.name, target_slot, &partition.target_path) &&
128                result;
129     } else {
130       partition.target_path.clear();
131     }
132   }
133   return result;
134 }
135 
operator ==(const InstallPlan::Partition & that) const136 bool InstallPlan::Partition::operator==(
137     const InstallPlan::Partition& that) const {
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 &&
141           target_hash == that.target_hash &&
142           run_postinstall == that.run_postinstall &&
143           postinstall_path == that.postinstall_path &&
144           filesystem_type == that.filesystem_type &&
145           postinstall_optional == that.postinstall_optional);
146 }
147 
148 }  // namespace chromeos_update_engine
149