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/stringprintf.h>
22 
23 #include "update_engine/common/utils.h"
24 #include "update_engine/payload_consumer/payload_constants.h"
25 
26 using std::string;
27 
28 namespace chromeos_update_engine {
29 
InstallPayloadTypeToString(InstallPayloadType type)30 string InstallPayloadTypeToString(InstallPayloadType type) {
31   switch (type) {
32     case InstallPayloadType::kUnknown:
33       return "unknown";
34     case InstallPayloadType::kFull:
35       return "full";
36     case InstallPayloadType::kDelta:
37       return "delta";
38   }
39   return "invalid type";
40 }
41 
operator ==(const InstallPlan & that) const42 bool InstallPlan::operator==(const InstallPlan& that) const {
43   return ((is_resume == that.is_resume) &&
44           (payload_type == that.payload_type) &&
45           (download_url == that.download_url) &&
46           (payload_size == that.payload_size) &&
47           (payload_hash == that.payload_hash) &&
48           (metadata_size == that.metadata_size) &&
49           (metadata_signature == that.metadata_signature) &&
50           (source_slot == that.source_slot) &&
51           (target_slot == that.target_slot) &&
52           (partitions == that.partitions));
53 }
54 
operator !=(const InstallPlan & that) const55 bool InstallPlan::operator!=(const InstallPlan& that) const {
56   return !((*this) == that);
57 }
58 
Dump() const59 void InstallPlan::Dump() const {
60   string partitions_str;
61   for (const auto& partition : partitions) {
62     partitions_str +=
63         base::StringPrintf(", part: %s (source_size: %" PRIu64
64                            ", target_size %" PRIu64 ", postinst:%s)",
65                            partition.name.c_str(),
66                            partition.source_size,
67                            partition.target_size,
68                            utils::ToString(partition.run_postinstall).c_str());
69   }
70 
71   LOG(INFO) << "InstallPlan: "
72             << (is_resume ? "resume" : "new_update")
73             << ", payload type: " << InstallPayloadTypeToString(payload_type)
74             << ", source_slot: " << BootControlInterface::SlotName(source_slot)
75             << ", target_slot: " << BootControlInterface::SlotName(target_slot)
76             << ", url: " << download_url
77             << ", payload size: " << payload_size
78             << ", payload hash: " << payload_hash
79             << ", metadata size: " << metadata_size
80             << ", metadata signature: " << metadata_signature
81             << partitions_str
82             << ", hash_checks_mandatory: " << utils::ToString(
83                 hash_checks_mandatory)
84             << ", powerwash_required: " << utils::ToString(powerwash_required);
85 }
86 
LoadPartitionsFromSlots(BootControlInterface * boot_control)87 bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
88   bool result = true;
89   for (Partition& partition : partitions) {
90     if (source_slot != BootControlInterface::kInvalidSlot) {
91       result = boot_control->GetPartitionDevice(
92           partition.name, source_slot, &partition.source_path) && result;
93     } else {
94       partition.source_path.clear();
95     }
96 
97     if (target_slot != BootControlInterface::kInvalidSlot) {
98       result = boot_control->GetPartitionDevice(
99           partition.name, target_slot, &partition.target_path) && result;
100     } else {
101       partition.target_path.clear();
102     }
103   }
104   return result;
105 }
106 
operator ==(const InstallPlan::Partition & that) const107 bool InstallPlan::Partition::operator==(
108     const InstallPlan::Partition& that) const {
109   return (name == that.name &&
110           source_path == that.source_path &&
111           source_size == that.source_size &&
112           source_hash == that.source_hash &&
113           target_path == that.target_path &&
114           target_size == that.target_size &&
115           target_hash == that.target_hash &&
116           run_postinstall == that.run_postinstall &&
117           postinstall_path == that.postinstall_path &&
118           filesystem_type == that.filesystem_type);
119 }
120 
121 }  // namespace chromeos_update_engine
122