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 
32 string InstallPayloadTypeToString(InstallPayloadType type) {
33   switch (type) {
34     case InstallPayloadType::kUnknown:
35       return "unknown";
36     case InstallPayloadType::kFull:
37       return "full";
38     case InstallPayloadType::kDelta:
39       return "delta";
40   }
41   return "invalid type";
42 }
43 
44 bool InstallPlan::operator==(const InstallPlan& that) const {
45   return ((is_resume == that.is_resume) &&
46           (download_url == that.download_url) && (payloads == that.payloads) &&
47           (source_slot == that.source_slot) &&
48           (target_slot == that.target_slot) && (partitions == that.partitions));
49 }
50 
51 bool InstallPlan::operator!=(const InstallPlan& that) const {
52   return !((*this) == that);
53 }
54 
55 void InstallPlan::Dump() const {
56   string partitions_str;
57   for (const auto& partition : partitions) {
58     partitions_str +=
59         base::StringPrintf(", part: %s (source_size: %" PRIu64
60                            ", target_size %" PRIu64 ", postinst:%s)",
61                            partition.name.c_str(),
62                            partition.source_size,
63                            partition.target_size,
64                            utils::ToString(partition.run_postinstall).c_str());
65   }
66   string payloads_str;
67   for (const auto& payload : payloads) {
68     payloads_str += base::StringPrintf(
69         ", payload: (size: %" PRIu64 ", metadata_size: %" PRIu64
70         ", metadata signature: %s, hash: %s, payload type: %s)",
71         payload.size,
72         payload.metadata_size,
73         payload.metadata_signature.c_str(),
74         base::HexEncode(payload.hash.data(), payload.hash.size()).c_str(),
75         InstallPayloadTypeToString(payload.type).c_str());
76   }
77 
78   string version_str = base::StringPrintf(", version: %s", version.c_str());
79   if (!system_version.empty()) {
80     version_str +=
81         base::StringPrintf(", system_version: %s", system_version.c_str());
82   }
83 
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 
91   LOG(INFO) << "InstallPlan: " << (is_resume ? "resume" : "new_update")
92             << version_str
93             << ", source_slot: " << BootControlInterface::SlotName(source_slot)
94             << ", target_slot: " << BootControlInterface::SlotName(target_slot)
95             << ", url: " << url_str << payloads_str << partitions_str
96             << ", hash_checks_mandatory: "
97             << utils::ToString(hash_checks_mandatory)
98             << ", powerwash_required: " << utils::ToString(powerwash_required)
99             << ", switch_slot_on_reboot: "
100             << utils::ToString(switch_slot_on_reboot)
101             << ", run_post_install: " << utils::ToString(run_post_install)
102             << ", is_rollback: " << utils::ToString(is_rollback)
103             << ", write_verity: " << utils::ToString(write_verity);
104 }
105 
106 bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
107   bool result = true;
108   for (Partition& partition : partitions) {
109     if (source_slot != BootControlInterface::kInvalidSlot &&
110         partition.source_size > 0) {
111       result = boot_control->GetPartitionDevice(
112                    partition.name, source_slot, &partition.source_path) &&
113                result;
114     } else {
115       partition.source_path.clear();
116     }
117 
118     if (target_slot != BootControlInterface::kInvalidSlot &&
119         partition.target_size > 0) {
120       result = boot_control->GetPartitionDevice(
121                    partition.name, target_slot, &partition.target_path) &&
122                result;
123     } else {
124       partition.target_path.clear();
125     }
126   }
127   return result;
128 }
129 
130 bool InstallPlan::Partition::operator==(
131     const InstallPlan::Partition& that) const {
132   return (name == that.name && source_path == that.source_path &&
133           source_size == that.source_size && source_hash == that.source_hash &&
134           target_path == that.target_path && target_size == that.target_size &&
135           target_hash == that.target_hash &&
136           run_postinstall == that.run_postinstall &&
137           postinstall_path == that.postinstall_path &&
138           filesystem_type == that.filesystem_type &&
139           postinstall_optional == that.postinstall_optional);
140 }
141 
142 }  // namespace chromeos_update_engine
143