blob: b55dea1fb78211f645a4cc6a6c404b0c17303414 [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>
Kelvin Zhangb9a9aa22024-10-15 10:38:35 -070025#include <android-base/stringprintf.h>
Jay Srinivasanae4697c2013-03-18 17:08:08 -070026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/utils.h"
Colin Cross26b82b12021-12-22 10:09:19 -080028#include "update_engine/update_metadata.pb.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070029
30using std::string;
Amin Hassani23795032020-11-24 14:38:55 -080031using std::vector;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070032
33namespace chromeos_update_engine {
34
Amin Hassani23795032020-11-24 14:38:55 -080035namespace {
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080036string PayloadUrlsToString(
37 const decltype(InstallPlan::Payload::payload_urls)& payload_urls) {
Kelvin Zhang0c184242024-10-25 11:19:27 -070038 return "(" + android::base::Join(payload_urls, ",") + ")";
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080039}
40
Amin Hassani23795032020-11-24 14:38:55 -080041string VectorToString(const vector<std::pair<string, string>>& input,
42 const string& separator) {
43 vector<string> vec;
44 std::transform(input.begin(),
45 input.end(),
46 std::back_inserter(vec),
47 [](const auto& pair) {
Kelvin Zhang0c184242024-10-25 11:19:27 -070048 return android::base::Join(vector{pair.first, pair.second},
49 ": ");
Amin Hassani23795032020-11-24 14:38:55 -080050 });
Kelvin Zhang0c184242024-10-25 11:19:27 -070051 return android::base::Join(vec, separator);
Amin Hassani23795032020-11-24 14:38:55 -080052}
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;
Kelvin Zhang0c184242024-10-25 11:19:27 -070084 if (android::base::StartsWith(ToLower(url_str), "fd://")) {
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090085 int fd = std::stoi(url_str.substr(strlen("fd://")));
86 url_str = utils::GetFilePath(fd);
87 }
88
Amin Hassani23795032020-11-24 14:38:55 -080089 vector<string> result_str;
90 result_str.emplace_back(VectorToString(
91 {
92 {"type", (is_resume ? "resume" : "new_update")},
93 {"version", version},
94 {"source_slot", BootControlInterface::SlotName(source_slot)},
95 {"target_slot", BootControlInterface::SlotName(target_slot)},
96 {"initial url", url_str},
97 {"hash_checks_mandatory", utils::ToString(hash_checks_mandatory)},
98 {"powerwash_required", utils::ToString(powerwash_required)},
99 {"switch_slot_on_reboot", utils::ToString(switch_slot_on_reboot)},
100 {"run_post_install", utils::ToString(run_post_install)},
Amin Hassani23795032020-11-24 14:38:55 -0800101 {"rollback_data_save_requested",
102 utils::ToString(rollback_data_save_requested)},
103 {"write_verity", utils::ToString(write_verity)},
104 },
105 "\n"));
106
107 for (const auto& partition : partitions) {
108 result_str.emplace_back(VectorToString(
109 {
110 {"Partition", partition.name},
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900111 {"source_size", std::format("{}", partition.source_size)},
Amin Hassani23795032020-11-24 14:38:55 -0800112 {"source_path", partition.source_path},
113 {"source_hash",
114 base::HexEncode(partition.source_hash.data(),
115 partition.source_hash.size())},
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900116 {"target_size", std::format("{}", partition.target_size)},
Amin Hassani23795032020-11-24 14:38:55 -0800117 {"target_path", partition.target_path},
118 {"target_hash",
119 base::HexEncode(partition.target_hash.data(),
120 partition.target_hash.size())},
121 {"run_postinstall", utils::ToString(partition.run_postinstall)},
122 {"postinstall_path", partition.postinstall_path},
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400123 {"readonly_target_path", partition.readonly_target_path},
Amin Hassani23795032020-11-24 14:38:55 -0800124 {"filesystem_type", partition.filesystem_type},
125 },
126 "\n "));
127 }
128
129 for (unsigned int i = 0; i < payloads.size(); ++i) {
130 const auto& payload = payloads[i];
131 result_str.emplace_back(VectorToString(
132 {
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900133 {"Payload", std::format("{}", i)},
Amin Hassani23795032020-11-24 14:38:55 -0800134 {"urls", PayloadUrlsToString(payload.payload_urls)},
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900135 {"size", std::format("{}", payload.size)},
136 {"metadata_size", std::format("{}", payload.metadata_size)},
Amin Hassani23795032020-11-24 14:38:55 -0800137 {"metadata_signature", payload.metadata_signature},
138 {"hash", base::HexEncode(payload.hash.data(), payload.hash.size())},
139 {"type", InstallPayloadTypeToString(payload.type)},
140 {"fingerprint", payload.fp},
141 {"app_id", payload.app_id},
142 {"already_applied", utils::ToString(payload.already_applied)},
143 },
144 "\n "));
145 }
146
Kelvin Zhang0c184242024-10-25 11:19:27 -0700147 return android::base::Join(result_str, "\n");
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700148}
149
Alex Deymo706a5ab2015-11-23 17:48:30 -0300150bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700151 bool result = true;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700152 for (Partition& partition : partitions) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -0800153 if (source_slot != BootControlInterface::kInvalidSlot &&
154 partition.source_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500155 TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice(
156 partition.name, source_slot, &partition.source_path));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700157 } else {
158 partition.source_path.clear();
159 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700160
Yifan Hong537802d2018-08-15 13:15:42 -0700161 if (target_slot != BootControlInterface::kInvalidSlot &&
162 partition.target_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500163 auto device = boot_control->GetPartitionDevice(
164 partition.name, target_slot, source_slot);
165 TEST_AND_RETURN_FALSE(device.has_value());
166 partition.target_path = device->rw_device_path;
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400167 partition.readonly_target_path = device->readonly_device_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700168 } else {
169 partition.target_path.clear();
170 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700171 }
172 return result;
173}
174
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700175bool InstallPlan::Partition::operator==(
176 const InstallPlan::Partition& that) const {
Amin Hassani008c4582019-01-13 16:22:47 -0800177 return (name == that.name && source_path == that.source_path &&
178 source_size == that.source_size && source_hash == that.source_hash &&
179 target_path == that.target_path && target_size == that.target_size &&
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700180 target_hash == that.target_hash &&
Alex Deymo390efed2016-02-18 11:00:40 -0800181 run_postinstall == that.run_postinstall &&
182 postinstall_path == that.postinstall_path &&
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700183 filesystem_type == that.filesystem_type &&
184 postinstall_optional == that.postinstall_optional);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700185}
186
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700187bool InstallPlan::Partition::ParseVerityConfig(
188 const PartitionUpdate& partition) {
189 if (partition.has_hash_tree_extent()) {
190 Extent extent = partition.hash_tree_data_extent();
191 hash_tree_data_offset = extent.start_block() * block_size;
192 hash_tree_data_size = extent.num_blocks() * block_size;
193 extent = partition.hash_tree_extent();
194 hash_tree_offset = extent.start_block() * block_size;
195 hash_tree_size = extent.num_blocks() * block_size;
196 uint64_t hash_tree_data_end = hash_tree_data_offset + hash_tree_data_size;
197 if (hash_tree_offset < hash_tree_data_end) {
198 LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at "
199 << hash_tree_data_end << ", but hash tree starts at "
200 << hash_tree_offset;
201 return false;
202 }
203 hash_tree_algorithm = partition.hash_tree_algorithm();
204 hash_tree_salt.assign(partition.hash_tree_salt().begin(),
205 partition.hash_tree_salt().end());
206 }
207 if (partition.has_fec_extent()) {
208 Extent extent = partition.fec_data_extent();
209 fec_data_offset = extent.start_block() * block_size;
210 fec_data_size = extent.num_blocks() * block_size;
211 extent = partition.fec_extent();
212 fec_offset = extent.start_block() * block_size;
213 fec_size = extent.num_blocks() * block_size;
214 uint64_t fec_data_end = fec_data_offset + fec_data_size;
215 if (fec_offset < fec_data_end) {
216 LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end
217 << ", but fec starts at " << fec_offset;
218 return false;
219 }
220 fec_roots = partition.fec_roots();
221 }
222 return true;
223}
224
Kelvin Zhang20982a52021-08-13 12:31:16 -0700225template <typename PartitinoUpdateArray>
226bool InstallPlan::ParseManifestToInstallPlan(
227 const PartitinoUpdateArray& partitions,
228 BootControlInterface* boot_control,
229 size_t block_size,
230 InstallPlan* install_plan,
231 ErrorCode* error) {
232 // Fill in the InstallPlan::partitions based on the partitions from the
233 // payload.
234 for (const PartitionUpdate& partition : partitions) {
235 InstallPlan::Partition install_part;
236 install_part.name = partition.partition_name();
237 install_part.run_postinstall =
238 partition.has_run_postinstall() && partition.run_postinstall();
239 if (install_part.run_postinstall) {
240 install_part.postinstall_path =
241 (partition.has_postinstall_path() ? partition.postinstall_path()
242 : kPostinstallDefaultScript);
243 install_part.filesystem_type = partition.filesystem_type();
244 install_part.postinstall_optional = partition.postinstall_optional();
245 }
246
247 if (partition.has_old_partition_info()) {
248 const PartitionInfo& info = partition.old_partition_info();
249 install_part.source_size = info.size();
250 install_part.source_hash.assign(info.hash().begin(), info.hash().end());
251 }
252
253 if (!partition.has_new_partition_info()) {
254 LOG(ERROR) << "Unable to get new partition hash info on partition "
255 << install_part.name << ".";
256 *error = ErrorCode::kDownloadNewPartitionInfoError;
257 return false;
258 }
259 const PartitionInfo& info = partition.new_partition_info();
260 install_part.target_size = info.size();
261 install_part.target_hash.assign(info.hash().begin(), info.hash().end());
262
263 install_part.block_size = block_size;
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700264 if (!install_part.ParseVerityConfig(partition)) {
265 *error = ErrorCode::kDownloadNewPartitionInfoError;
266 LOG(INFO) << "Failed to parse partition `" << partition.partition_name()
267 << "` verity configs";
268 return false;
Kelvin Zhang20982a52021-08-13 12:31:16 -0700269 }
270
271 install_plan->partitions.push_back(install_part);
272 }
273
274 // TODO(xunchang) only need to load the partitions for those in payload.
275 // Because we have already loaded the other once when generating SOURCE_COPY
276 // operations.
277 if (!install_plan->LoadPartitionsFromSlots(boot_control)) {
278 LOG(ERROR) << "Unable to determine all the partition devices.";
279 *error = ErrorCode::kInstallDeviceOpenError;
280 return false;
281 }
282 return true;
283}
284
285bool InstallPlan::ParsePartitions(
286 const std::vector<PartitionUpdate>& partitions,
287 BootControlInterface* boot_control,
288 size_t block_size,
289 ErrorCode* error) {
290 return ParseManifestToInstallPlan(
291 partitions, boot_control, block_size, this, error);
292}
293
294bool InstallPlan::ParsePartitions(
295 const google::protobuf::RepeatedPtrField<PartitionUpdate>& partitions,
296 BootControlInterface* boot_control,
297 size_t block_size,
298 ErrorCode* error) {
299 return ParseManifestToInstallPlan(
300 partitions, boot_control, block_size, this, error);
301}
302
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700303} // namespace chromeos_update_engine