blob: 91eb53b31a5c8847877fbb1d61f12c06db7623b7 [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"
Colin Cross26b82b12021-12-22 10:09:19 -080030#include "update_engine/update_metadata.pb.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070031
32using std::string;
Amin Hassani23795032020-11-24 14:38:55 -080033using std::vector;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070034
35namespace chromeos_update_engine {
36
Amin Hassani23795032020-11-24 14:38:55 -080037namespace {
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080038string PayloadUrlsToString(
39 const decltype(InstallPlan::Payload::payload_urls)& payload_urls) {
40 return "(" + base::JoinString(payload_urls, ",") + ")";
41}
42
Amin Hassani23795032020-11-24 14:38:55 -080043string VectorToString(const vector<std::pair<string, string>>& input,
44 const string& separator) {
45 vector<string> vec;
46 std::transform(input.begin(),
47 input.end(),
48 std::back_inserter(vec),
49 [](const auto& pair) {
50 return base::JoinString({pair.first, pair.second}, ": ");
51 });
52 return base::JoinString(vec, separator);
53}
54} // namespace
55
Alex Deymo64d98782016-02-05 18:03:48 -080056string InstallPayloadTypeToString(InstallPayloadType type) {
57 switch (type) {
58 case InstallPayloadType::kUnknown:
59 return "unknown";
60 case InstallPayloadType::kFull:
61 return "full";
62 case InstallPayloadType::kDelta:
63 return "delta";
64 }
65 return "invalid type";
66}
Jay Srinivasanae4697c2013-03-18 17:08:08 -070067
68bool InstallPlan::operator==(const InstallPlan& that) const {
69 return ((is_resume == that.is_resume) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080070 (download_url == that.download_url) && (payloads == that.payloads) &&
Alex Deymo763e7db2015-08-27 21:08:08 -070071 (source_slot == that.source_slot) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080072 (target_slot == that.target_slot) && (partitions == that.partitions));
Jay Srinivasanae4697c2013-03-18 17:08:08 -070073}
74
75bool InstallPlan::operator!=(const InstallPlan& that) const {
76 return !((*this) == that);
77}
78
79void InstallPlan::Dump() const {
Amin Hassani23795032020-11-24 14:38:55 -080080 LOG(INFO) << "InstallPlan: \n" << ToString();
81}
Alex Deymoe5e5fe92015-10-05 09:28:19 -070082
Amin Hassani23795032020-11-24 14:38:55 -080083string InstallPlan::ToString() const {
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090084 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
Amin Hassani23795032020-11-24 14:38:55 -080091 vector<string> result_str;
92 result_str.emplace_back(VectorToString(
93 {
94 {"type", (is_resume ? "resume" : "new_update")},
95 {"version", version},
96 {"source_slot", BootControlInterface::SlotName(source_slot)},
97 {"target_slot", BootControlInterface::SlotName(target_slot)},
98 {"initial url", url_str},
99 {"hash_checks_mandatory", utils::ToString(hash_checks_mandatory)},
100 {"powerwash_required", utils::ToString(powerwash_required)},
101 {"switch_slot_on_reboot", utils::ToString(switch_slot_on_reboot)},
102 {"run_post_install", utils::ToString(run_post_install)},
103 {"is_rollback", utils::ToString(is_rollback)},
104 {"rollback_data_save_requested",
105 utils::ToString(rollback_data_save_requested)},
106 {"write_verity", utils::ToString(write_verity)},
107 },
108 "\n"));
109
110 for (const auto& partition : partitions) {
111 result_str.emplace_back(VectorToString(
112 {
113 {"Partition", partition.name},
114 {"source_size", base::NumberToString(partition.source_size)},
115 {"source_path", partition.source_path},
116 {"source_hash",
117 base::HexEncode(partition.source_hash.data(),
118 partition.source_hash.size())},
119 {"target_size", base::NumberToString(partition.target_size)},
120 {"target_path", partition.target_path},
121 {"target_hash",
122 base::HexEncode(partition.target_hash.data(),
123 partition.target_hash.size())},
124 {"run_postinstall", utils::ToString(partition.run_postinstall)},
125 {"postinstall_path", partition.postinstall_path},
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400126 {"readonly_target_path", partition.readonly_target_path},
Amin Hassani23795032020-11-24 14:38:55 -0800127 {"filesystem_type", partition.filesystem_type},
128 },
129 "\n "));
130 }
131
132 for (unsigned int i = 0; i < payloads.size(); ++i) {
133 const auto& payload = payloads[i];
134 result_str.emplace_back(VectorToString(
135 {
136 {"Payload", base::NumberToString(i)},
137 {"urls", PayloadUrlsToString(payload.payload_urls)},
138 {"size", base::NumberToString(payload.size)},
139 {"metadata_size", base::NumberToString(payload.metadata_size)},
140 {"metadata_signature", payload.metadata_signature},
141 {"hash", base::HexEncode(payload.hash.data(), payload.hash.size())},
142 {"type", InstallPayloadTypeToString(payload.type)},
143 {"fingerprint", payload.fp},
144 {"app_id", payload.app_id},
145 {"already_applied", utils::ToString(payload.already_applied)},
146 },
147 "\n "));
148 }
149
150 return base::JoinString(result_str, "\n");
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700151}
152
Alex Deymo706a5ab2015-11-23 17:48:30 -0300153bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700154 bool result = true;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700155 for (Partition& partition : partitions) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -0800156 if (source_slot != BootControlInterface::kInvalidSlot &&
157 partition.source_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500158 TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice(
159 partition.name, source_slot, &partition.source_path));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700160 } else {
161 partition.source_path.clear();
162 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700163
Yifan Hong537802d2018-08-15 13:15:42 -0700164 if (target_slot != BootControlInterface::kInvalidSlot &&
165 partition.target_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500166 auto device = boot_control->GetPartitionDevice(
167 partition.name, target_slot, source_slot);
168 TEST_AND_RETURN_FALSE(device.has_value());
169 partition.target_path = device->rw_device_path;
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400170 partition.readonly_target_path = device->readonly_device_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700171 } else {
172 partition.target_path.clear();
173 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700174 }
175 return result;
176}
177
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700178bool InstallPlan::Partition::operator==(
179 const InstallPlan::Partition& that) const {
Amin Hassani008c4582019-01-13 16:22:47 -0800180 return (name == that.name && source_path == that.source_path &&
181 source_size == that.source_size && source_hash == that.source_hash &&
182 target_path == that.target_path && target_size == that.target_size &&
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700183 target_hash == that.target_hash &&
Alex Deymo390efed2016-02-18 11:00:40 -0800184 run_postinstall == that.run_postinstall &&
185 postinstall_path == that.postinstall_path &&
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700186 filesystem_type == that.filesystem_type &&
187 postinstall_optional == that.postinstall_optional);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700188}
189
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700190bool InstallPlan::Partition::ParseVerityConfig(
191 const PartitionUpdate& partition) {
192 if (partition.has_hash_tree_extent()) {
193 Extent extent = partition.hash_tree_data_extent();
194 hash_tree_data_offset = extent.start_block() * block_size;
195 hash_tree_data_size = extent.num_blocks() * block_size;
196 extent = partition.hash_tree_extent();
197 hash_tree_offset = extent.start_block() * block_size;
198 hash_tree_size = extent.num_blocks() * block_size;
199 uint64_t hash_tree_data_end = hash_tree_data_offset + hash_tree_data_size;
200 if (hash_tree_offset < hash_tree_data_end) {
201 LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at "
202 << hash_tree_data_end << ", but hash tree starts at "
203 << hash_tree_offset;
204 return false;
205 }
206 hash_tree_algorithm = partition.hash_tree_algorithm();
207 hash_tree_salt.assign(partition.hash_tree_salt().begin(),
208 partition.hash_tree_salt().end());
209 }
210 if (partition.has_fec_extent()) {
211 Extent extent = partition.fec_data_extent();
212 fec_data_offset = extent.start_block() * block_size;
213 fec_data_size = extent.num_blocks() * block_size;
214 extent = partition.fec_extent();
215 fec_offset = extent.start_block() * block_size;
216 fec_size = extent.num_blocks() * block_size;
217 uint64_t fec_data_end = fec_data_offset + fec_data_size;
218 if (fec_offset < fec_data_end) {
219 LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end
220 << ", but fec starts at " << fec_offset;
221 return false;
222 }
223 fec_roots = partition.fec_roots();
224 }
225 return true;
226}
227
Kelvin Zhang20982a52021-08-13 12:31:16 -0700228template <typename PartitinoUpdateArray>
229bool InstallPlan::ParseManifestToInstallPlan(
230 const PartitinoUpdateArray& partitions,
231 BootControlInterface* boot_control,
232 size_t block_size,
233 InstallPlan* install_plan,
234 ErrorCode* error) {
235 // Fill in the InstallPlan::partitions based on the partitions from the
236 // payload.
237 for (const PartitionUpdate& partition : partitions) {
238 InstallPlan::Partition install_part;
239 install_part.name = partition.partition_name();
240 install_part.run_postinstall =
241 partition.has_run_postinstall() && partition.run_postinstall();
242 if (install_part.run_postinstall) {
243 install_part.postinstall_path =
244 (partition.has_postinstall_path() ? partition.postinstall_path()
245 : kPostinstallDefaultScript);
246 install_part.filesystem_type = partition.filesystem_type();
247 install_part.postinstall_optional = partition.postinstall_optional();
248 }
249
250 if (partition.has_old_partition_info()) {
251 const PartitionInfo& info = partition.old_partition_info();
252 install_part.source_size = info.size();
253 install_part.source_hash.assign(info.hash().begin(), info.hash().end());
254 }
255
256 if (!partition.has_new_partition_info()) {
257 LOG(ERROR) << "Unable to get new partition hash info on partition "
258 << install_part.name << ".";
259 *error = ErrorCode::kDownloadNewPartitionInfoError;
260 return false;
261 }
262 const PartitionInfo& info = partition.new_partition_info();
263 install_part.target_size = info.size();
264 install_part.target_hash.assign(info.hash().begin(), info.hash().end());
265
266 install_part.block_size = block_size;
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700267 if (!install_part.ParseVerityConfig(partition)) {
268 *error = ErrorCode::kDownloadNewPartitionInfoError;
269 LOG(INFO) << "Failed to parse partition `" << partition.partition_name()
270 << "` verity configs";
271 return false;
Kelvin Zhang20982a52021-08-13 12:31:16 -0700272 }
273
274 install_plan->partitions.push_back(install_part);
275 }
276
277 // TODO(xunchang) only need to load the partitions for those in payload.
278 // Because we have already loaded the other once when generating SOURCE_COPY
279 // operations.
280 if (!install_plan->LoadPartitionsFromSlots(boot_control)) {
281 LOG(ERROR) << "Unable to determine all the partition devices.";
282 *error = ErrorCode::kInstallDeviceOpenError;
283 return false;
284 }
285 return true;
286}
287
288bool InstallPlan::ParsePartitions(
289 const std::vector<PartitionUpdate>& partitions,
290 BootControlInterface* boot_control,
291 size_t block_size,
292 ErrorCode* error) {
293 return ParseManifestToInstallPlan(
294 partitions, boot_control, block_size, this, error);
295}
296
297bool InstallPlan::ParsePartitions(
298 const google::protobuf::RepeatedPtrField<PartitionUpdate>& partitions,
299 BootControlInterface* boot_control,
300 size_t block_size,
301 ErrorCode* error) {
302 return ParseManifestToInstallPlan(
303 partitions, boot_control, block_size, this, error);
304}
305
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700306} // namespace chromeos_update_engine