blob: 06b7dd8c51554e412f2bea5b780613ed237f4afe [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Srinivasanae4697c2013-03-18 17:08:08 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/install_plan.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070018
Amin Hassani23795032020-11-24 14:38:55 -080019#include <algorithm>
20#include <utility>
21
Alex Deymoe5e5fe92015-10-05 09:28:19 -070022#include <base/format_macros.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080023#include <base/logging.h>
Sen Jiang2703ef42017-03-16 13:36:21 -070024#include <base/strings/string_number_conversions.h>
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080025#include <base/strings/string_util.h>
Alex Deymoe5e5fe92015-10-05 09:28:19 -070026#include <base/strings/stringprintf.h>
Jay Srinivasanae4697c2013-03-18 17:08:08 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/utils.h"
29#include "update_engine/payload_consumer/payload_constants.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070030
31using std::string;
Amin Hassani23795032020-11-24 14:38:55 -080032using std::vector;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070033
34namespace chromeos_update_engine {
35
Amin Hassani23795032020-11-24 14:38:55 -080036namespace {
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080037string PayloadUrlsToString(
38 const decltype(InstallPlan::Payload::payload_urls)& payload_urls) {
39 return "(" + base::JoinString(payload_urls, ",") + ")";
40}
41
Amin Hassani23795032020-11-24 14:38:55 -080042string 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 Deymo64d98782016-02-05 18:03:48 -080055string 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 Srinivasanae4697c2013-03-18 17:08:08 -070066
67bool InstallPlan::operator==(const InstallPlan& that) const {
68 return ((is_resume == that.is_resume) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080069 (download_url == that.download_url) && (payloads == that.payloads) &&
Alex Deymo763e7db2015-08-27 21:08:08 -070070 (source_slot == that.source_slot) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080071 (target_slot == that.target_slot) && (partitions == that.partitions));
Jay Srinivasanae4697c2013-03-18 17:08:08 -070072}
73
74bool InstallPlan::operator!=(const InstallPlan& that) const {
75 return !((*this) == that);
76}
77
78void InstallPlan::Dump() const {
Amin Hassani23795032020-11-24 14:38:55 -080079 LOG(INFO) << "InstallPlan: \n" << ToString();
80}
Alex Deymoe5e5fe92015-10-05 09:28:19 -070081
Amin Hassani23795032020-11-24 14:38:55 -080082string InstallPlan::ToString() const {
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090083 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 Hassani23795032020-11-24 14:38:55 -080090 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 Zhanga9b5d8c2021-05-05 09:17:46 -0400125 {"readonly_target_path", partition.readonly_target_path},
Amin Hassani23795032020-11-24 14:38:55 -0800126 {"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 Srinivasanae4697c2013-03-18 17:08:08 -0700150}
151
Alex Deymo706a5ab2015-11-23 17:48:30 -0300152bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700153 bool result = true;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700154 for (Partition& partition : partitions) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -0800155 if (source_slot != BootControlInterface::kInvalidSlot &&
156 partition.source_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500157 TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice(
158 partition.name, source_slot, &partition.source_path));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700159 } else {
160 partition.source_path.clear();
161 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700162
Yifan Hong537802d2018-08-15 13:15:42 -0700163 if (target_slot != BootControlInterface::kInvalidSlot &&
164 partition.target_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500165 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 Zhanga9b5d8c2021-05-05 09:17:46 -0400169 partition.readonly_target_path = device->readonly_device_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700170 } else {
171 partition.target_path.clear();
172 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700173 }
174 return result;
175}
176
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700177bool InstallPlan::Partition::operator==(
178 const InstallPlan::Partition& that) const {
Amin Hassani008c4582019-01-13 16:22:47 -0800179 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 Deymoe5e5fe92015-10-05 09:28:19 -0700182 target_hash == that.target_hash &&
Alex Deymo390efed2016-02-18 11:00:40 -0800183 run_postinstall == that.run_postinstall &&
184 postinstall_path == that.postinstall_path &&
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700185 filesystem_type == that.filesystem_type &&
186 postinstall_optional == that.postinstall_optional);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700187}
188
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700189} // namespace chromeos_update_engine