blob: 8916af55ed7a06f8b5611a1ca42f5cae86ebae94 [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 {"write_verity", utils::ToString(write_verity)},
102 },
103 "\n"));
104
105 for (const auto& partition : partitions) {
106 result_str.emplace_back(VectorToString(
107 {
108 {"Partition", partition.name},
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900109 {"source_size", std::format("{}", partition.source_size)},
Amin Hassani23795032020-11-24 14:38:55 -0800110 {"source_path", partition.source_path},
111 {"source_hash",
112 base::HexEncode(partition.source_hash.data(),
113 partition.source_hash.size())},
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900114 {"target_size", std::format("{}", partition.target_size)},
Amin Hassani23795032020-11-24 14:38:55 -0800115 {"target_path", partition.target_path},
116 {"target_hash",
117 base::HexEncode(partition.target_hash.data(),
118 partition.target_hash.size())},
119 {"run_postinstall", utils::ToString(partition.run_postinstall)},
120 {"postinstall_path", partition.postinstall_path},
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400121 {"readonly_target_path", partition.readonly_target_path},
Amin Hassani23795032020-11-24 14:38:55 -0800122 {"filesystem_type", partition.filesystem_type},
123 },
124 "\n "));
125 }
126
127 for (unsigned int i = 0; i < payloads.size(); ++i) {
128 const auto& payload = payloads[i];
129 result_str.emplace_back(VectorToString(
130 {
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900131 {"Payload", std::format("{}", i)},
Amin Hassani23795032020-11-24 14:38:55 -0800132 {"urls", PayloadUrlsToString(payload.payload_urls)},
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +0900133 {"size", std::format("{}", payload.size)},
134 {"metadata_size", std::format("{}", payload.metadata_size)},
Amin Hassani23795032020-11-24 14:38:55 -0800135 {"metadata_signature", payload.metadata_signature},
136 {"hash", base::HexEncode(payload.hash.data(), payload.hash.size())},
137 {"type", InstallPayloadTypeToString(payload.type)},
138 {"fingerprint", payload.fp},
139 {"app_id", payload.app_id},
140 {"already_applied", utils::ToString(payload.already_applied)},
141 },
142 "\n "));
143 }
144
Kelvin Zhang0c184242024-10-25 11:19:27 -0700145 return android::base::Join(result_str, "\n");
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700146}
147
Alex Deymo706a5ab2015-11-23 17:48:30 -0300148bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700149 bool result = true;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700150 for (Partition& partition : partitions) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -0800151 if (source_slot != BootControlInterface::kInvalidSlot &&
152 partition.source_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500153 TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice(
154 partition.name, source_slot, &partition.source_path));
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700155 } else {
156 partition.source_path.clear();
157 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700158
Yifan Hong537802d2018-08-15 13:15:42 -0700159 if (target_slot != BootControlInterface::kInvalidSlot &&
160 partition.target_size > 0) {
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500161 auto device = boot_control->GetPartitionDevice(
162 partition.name, target_slot, source_slot);
163 TEST_AND_RETURN_FALSE(device.has_value());
164 partition.target_path = device->rw_device_path;
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400165 partition.readonly_target_path = device->readonly_device_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700166 } else {
167 partition.target_path.clear();
168 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700169 }
170 return result;
171}
172
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700173bool InstallPlan::Partition::operator==(
174 const InstallPlan::Partition& that) const {
Amin Hassani008c4582019-01-13 16:22:47 -0800175 return (name == that.name && source_path == that.source_path &&
176 source_size == that.source_size && source_hash == that.source_hash &&
177 target_path == that.target_path && target_size == that.target_size &&
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700178 target_hash == that.target_hash &&
Alex Deymo390efed2016-02-18 11:00:40 -0800179 run_postinstall == that.run_postinstall &&
180 postinstall_path == that.postinstall_path &&
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700181 filesystem_type == that.filesystem_type &&
182 postinstall_optional == that.postinstall_optional);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700183}
184
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700185bool InstallPlan::Partition::ParseVerityConfig(
186 const PartitionUpdate& partition) {
187 if (partition.has_hash_tree_extent()) {
188 Extent extent = partition.hash_tree_data_extent();
189 hash_tree_data_offset = extent.start_block() * block_size;
190 hash_tree_data_size = extent.num_blocks() * block_size;
191 extent = partition.hash_tree_extent();
192 hash_tree_offset = extent.start_block() * block_size;
193 hash_tree_size = extent.num_blocks() * block_size;
194 uint64_t hash_tree_data_end = hash_tree_data_offset + hash_tree_data_size;
195 if (hash_tree_offset < hash_tree_data_end) {
196 LOG(ERROR) << "Invalid hash tree extents, hash tree data ends at "
197 << hash_tree_data_end << ", but hash tree starts at "
198 << hash_tree_offset;
199 return false;
200 }
201 hash_tree_algorithm = partition.hash_tree_algorithm();
202 hash_tree_salt.assign(partition.hash_tree_salt().begin(),
203 partition.hash_tree_salt().end());
204 }
205 if (partition.has_fec_extent()) {
206 Extent extent = partition.fec_data_extent();
207 fec_data_offset = extent.start_block() * block_size;
208 fec_data_size = extent.num_blocks() * block_size;
209 extent = partition.fec_extent();
210 fec_offset = extent.start_block() * block_size;
211 fec_size = extent.num_blocks() * block_size;
212 uint64_t fec_data_end = fec_data_offset + fec_data_size;
213 if (fec_offset < fec_data_end) {
214 LOG(ERROR) << "Invalid fec extents, fec data ends at " << fec_data_end
215 << ", but fec starts at " << fec_offset;
216 return false;
217 }
218 fec_roots = partition.fec_roots();
219 }
220 return true;
221}
222
Kelvin Zhang20982a52021-08-13 12:31:16 -0700223template <typename PartitinoUpdateArray>
224bool InstallPlan::ParseManifestToInstallPlan(
225 const PartitinoUpdateArray& partitions,
226 BootControlInterface* boot_control,
227 size_t block_size,
228 InstallPlan* install_plan,
229 ErrorCode* error) {
230 // Fill in the InstallPlan::partitions based on the partitions from the
231 // payload.
232 for (const PartitionUpdate& partition : partitions) {
233 InstallPlan::Partition install_part;
234 install_part.name = partition.partition_name();
235 install_part.run_postinstall =
236 partition.has_run_postinstall() && partition.run_postinstall();
237 if (install_part.run_postinstall) {
238 install_part.postinstall_path =
239 (partition.has_postinstall_path() ? partition.postinstall_path()
240 : kPostinstallDefaultScript);
241 install_part.filesystem_type = partition.filesystem_type();
242 install_part.postinstall_optional = partition.postinstall_optional();
243 }
244
245 if (partition.has_old_partition_info()) {
246 const PartitionInfo& info = partition.old_partition_info();
247 install_part.source_size = info.size();
248 install_part.source_hash.assign(info.hash().begin(), info.hash().end());
249 }
250
251 if (!partition.has_new_partition_info()) {
252 LOG(ERROR) << "Unable to get new partition hash info on partition "
253 << install_part.name << ".";
254 *error = ErrorCode::kDownloadNewPartitionInfoError;
255 return false;
256 }
257 const PartitionInfo& info = partition.new_partition_info();
258 install_part.target_size = info.size();
259 install_part.target_hash.assign(info.hash().begin(), info.hash().end());
260
261 install_part.block_size = block_size;
Kelvin Zhang91e839c2022-04-05 14:25:00 -0700262 if (!install_part.ParseVerityConfig(partition)) {
263 *error = ErrorCode::kDownloadNewPartitionInfoError;
264 LOG(INFO) << "Failed to parse partition `" << partition.partition_name()
265 << "` verity configs";
266 return false;
Kelvin Zhang20982a52021-08-13 12:31:16 -0700267 }
268
269 install_plan->partitions.push_back(install_part);
270 }
271
272 // TODO(xunchang) only need to load the partitions for those in payload.
273 // Because we have already loaded the other once when generating SOURCE_COPY
274 // operations.
275 if (!install_plan->LoadPartitionsFromSlots(boot_control)) {
276 LOG(ERROR) << "Unable to determine all the partition devices.";
277 *error = ErrorCode::kInstallDeviceOpenError;
278 return false;
279 }
280 return true;
281}
282
283bool InstallPlan::ParsePartitions(
284 const std::vector<PartitionUpdate>& partitions,
285 BootControlInterface* boot_control,
286 size_t block_size,
287 ErrorCode* error) {
288 return ParseManifestToInstallPlan(
289 partitions, boot_control, block_size, this, error);
290}
291
292bool InstallPlan::ParsePartitions(
293 const google::protobuf::RepeatedPtrField<PartitionUpdate>& partitions,
294 BootControlInterface* boot_control,
295 size_t block_size,
296 ErrorCode* error) {
297 return ParseManifestToInstallPlan(
298 partitions, boot_control, block_size, this, error);
299}
300
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700301} // namespace chromeos_update_engine