blob: 9c3934d549611746e25192bf2a5314677283b7c6 [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"
Colin Cross26b82b12021-12-22 10:09:19 -080029#include "update_engine/update_metadata.pb.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)},
Amin Hassani23795032020-11-24 14:38:55 -0800102 {"rollback_data_save_requested",
103 utils::ToString(rollback_data_save_requested)},
104 {"write_verity", utils::ToString(write_verity)},
105 },
106 "\n"));
107
108 for (const auto& partition : partitions) {
109 result_str.emplace_back(VectorToString(
110 {
111 {"Partition", partition.name},
112 {"source_size", base::NumberToString(partition.source_size)},
113 {"source_path", partition.source_path},
114 {"source_hash",
115 base::HexEncode(partition.source_hash.data(),
116 partition.source_hash.size())},
117 {"target_size", base::NumberToString(partition.target_size)},
118 {"target_path", partition.target_path},
119 {"target_hash",
120 base::HexEncode(partition.target_hash.data(),
121 partition.target_hash.size())},
122 {"run_postinstall", utils::ToString(partition.run_postinstall)},
123 {"postinstall_path", partition.postinstall_path},
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400124 {"readonly_target_path", partition.readonly_target_path},
Amin Hassani23795032020-11-24 14:38:55 -0800125 {"filesystem_type", partition.filesystem_type},
126 },
127 "\n "));
128 }
129
130 for (unsigned int i = 0; i < payloads.size(); ++i) {
131 const auto& payload = payloads[i];
132 result_str.emplace_back(VectorToString(
133 {
134 {"Payload", base::NumberToString(i)},
135 {"urls", PayloadUrlsToString(payload.payload_urls)},
136 {"size", base::NumberToString(payload.size)},
137 {"metadata_size", base::NumberToString(payload.metadata_size)},
138 {"metadata_signature", payload.metadata_signature},
139 {"hash", base::HexEncode(payload.hash.data(), payload.hash.size())},
140 {"type", InstallPayloadTypeToString(payload.type)},
141 {"fingerprint", payload.fp},
142 {"app_id", payload.app_id},
143 {"already_applied", utils::ToString(payload.already_applied)},
144 },
145 "\n "));
146 }
147
148 return base::JoinString(result_str, "\n");
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700149}
150
Alex Deymo706a5ab2015-11-23 17:48:30 -0300151bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700152 bool result = true;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700153 for (Partition& partition : partitions) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -0800154 if (source_slot != BootControlInterface::kInvalidSlot &&
155 partition.source_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500156 TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice(
157 partition.name, source_slot, &partition.source_path));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700158 } else {
159 partition.source_path.clear();
160 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700161
Yifan Hong537802d2018-08-15 13:15:42 -0700162 if (target_slot != BootControlInterface::kInvalidSlot &&
163 partition.target_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500164 auto device = boot_control->GetPartitionDevice(
165 partition.name, target_slot, source_slot);
166 TEST_AND_RETURN_FALSE(device.has_value());
167 partition.target_path = device->rw_device_path;
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400168 partition.readonly_target_path = device->readonly_device_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700169 } else {
170 partition.target_path.clear();
171 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700172 }
173 return result;
174}
175
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700176bool InstallPlan::Partition::operator==(
177 const InstallPlan::Partition& that) const {
Amin Hassani008c4582019-01-13 16:22:47 -0800178 return (name == that.name && source_path == that.source_path &&
179 source_size == that.source_size && source_hash == that.source_hash &&
180 target_path == that.target_path && target_size == that.target_size &&
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700181 target_hash == that.target_hash &&
Alex Deymo390efed2016-02-18 11:00:40 -0800182 run_postinstall == that.run_postinstall &&
183 postinstall_path == that.postinstall_path &&
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700184 filesystem_type == that.filesystem_type &&
185 postinstall_optional == that.postinstall_optional);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700186}
187
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700188bool InstallPlan::Partition::ParseVerityConfig(
189 const PartitionUpdate& partition) {
190 if (partition.has_hash_tree_extent()) {
191 Extent extent = partition.hash_tree_data_extent();
192 hash_tree_data_offset = extent.start_block() * block_size;
193 hash_tree_data_size = extent.num_blocks() * block_size;
194 extent = partition.hash_tree_extent();
195 hash_tree_offset = extent.start_block() * block_size;
196 hash_tree_size = extent.num_blocks() * block_size;
197 uint64_t hash_tree_data_end = hash_tree_data_offset + hash_tree_data_size;
198 if (hash_tree_offset < hash_tree_data_end) {
199 LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at "
200 << hash_tree_data_end << ", but hash tree starts at "
201 << hash_tree_offset;
202 return false;
203 }
204 hash_tree_algorithm = partition.hash_tree_algorithm();
205 hash_tree_salt.assign(partition.hash_tree_salt().begin(),
206 partition.hash_tree_salt().end());
207 }
208 if (partition.has_fec_extent()) {
209 Extent extent = partition.fec_data_extent();
210 fec_data_offset = extent.start_block() * block_size;
211 fec_data_size = extent.num_blocks() * block_size;
212 extent = partition.fec_extent();
213 fec_offset = extent.start_block() * block_size;
214 fec_size = extent.num_blocks() * block_size;
215 uint64_t fec_data_end = fec_data_offset + fec_data_size;
216 if (fec_offset < fec_data_end) {
217 LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end
218 << ", but fec starts at " << fec_offset;
219 return false;
220 }
221 fec_roots = partition.fec_roots();
222 }
223 return true;
224}
225
Kelvin Zhang20982a52021-08-13 12:31:16 -0700226template <typename PartitinoUpdateArray>
227bool InstallPlan::ParseManifestToInstallPlan(
228 const PartitinoUpdateArray& partitions,
229 BootControlInterface* boot_control,
230 size_t block_size,
231 InstallPlan* install_plan,
232 ErrorCode* error) {
233 // Fill in the InstallPlan::partitions based on the partitions from the
234 // payload.
235 for (const PartitionUpdate& partition : partitions) {
236 InstallPlan::Partition install_part;
237 install_part.name = partition.partition_name();
238 install_part.run_postinstall =
239 partition.has_run_postinstall() && partition.run_postinstall();
240 if (install_part.run_postinstall) {
241 install_part.postinstall_path =
242 (partition.has_postinstall_path() ? partition.postinstall_path()
243 : kPostinstallDefaultScript);
244 install_part.filesystem_type = partition.filesystem_type();
245 install_part.postinstall_optional = partition.postinstall_optional();
246 }
247
248 if (partition.has_old_partition_info()) {
249 const PartitionInfo& info = partition.old_partition_info();
250 install_part.source_size = info.size();
251 install_part.source_hash.assign(info.hash().begin(), info.hash().end());
252 }
253
254 if (!partition.has_new_partition_info()) {
255 LOG(ERROR) << "Unable to get new partition hash info on partition "
256 << install_part.name << ".";
257 *error = ErrorCode::kDownloadNewPartitionInfoError;
258 return false;
259 }
260 const PartitionInfo& info = partition.new_partition_info();
261 install_part.target_size = info.size();
262 install_part.target_hash.assign(info.hash().begin(), info.hash().end());
263
264 install_part.block_size = block_size;
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700265 if (!install_part.ParseVerityConfig(partition)) {
266 *error = ErrorCode::kDownloadNewPartitionInfoError;
267 LOG(INFO) << "Failed to parse partition `" << partition.partition_name()
268 << "` verity configs";
269 return false;
Kelvin Zhang20982a52021-08-13 12:31:16 -0700270 }
271
272 install_plan->partitions.push_back(install_part);
273 }
274
275 // TODO(xunchang) only need to load the partitions for those in payload.
276 // Because we have already loaded the other once when generating SOURCE_COPY
277 // operations.
278 if (!install_plan->LoadPartitionsFromSlots(boot_control)) {
279 LOG(ERROR) << "Unable to determine all the partition devices.";
280 *error = ErrorCode::kInstallDeviceOpenError;
281 return false;
282 }
283 return true;
284}
285
286bool InstallPlan::ParsePartitions(
287 const std::vector<PartitionUpdate>& partitions,
288 BootControlInterface* boot_control,
289 size_t block_size,
290 ErrorCode* error) {
291 return ParseManifestToInstallPlan(
292 partitions, boot_control, block_size, this, error);
293}
294
295bool InstallPlan::ParsePartitions(
296 const google::protobuf::RepeatedPtrField<PartitionUpdate>& partitions,
297 BootControlInterface* boot_control,
298 size_t block_size,
299 ErrorCode* error) {
300 return ParseManifestToInstallPlan(
301 partitions, boot_control, block_size, this, error);
302}
303
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700304} // namespace chromeos_update_engine