1 //
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 //
16
17 #include "update_engine/payload_consumer/install_plan.h"
18
19 #include <algorithm>
20 #include <utility>
21
22 #include <base/format_macros.h>
23 #include <base/logging.h>
24 #include <base/strings/string_number_conversions.h>
25 #include <base/strings/string_util.h>
26 #include <base/strings/stringprintf.h>
27
28 #include "update_engine/common/utils.h"
29 #include "update_engine/payload_consumer/payload_constants.h"
30 #include "update_engine/update_metadata.pb.h"
31
32 using std::string;
33 using std::vector;
34
35 namespace chromeos_update_engine {
36
37 namespace {
PayloadUrlsToString(const decltype(InstallPlan::Payload::payload_urls) & payload_urls)38 string PayloadUrlsToString(
39 const decltype(InstallPlan::Payload::payload_urls)& payload_urls) {
40 return "(" + base::JoinString(payload_urls, ",") + ")";
41 }
42
VectorToString(const vector<std::pair<string,string>> & input,const string & separator)43 string 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
InstallPayloadTypeToString(InstallPayloadType type)56 string 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 }
67
operator ==(const InstallPlan & that) const68 bool InstallPlan::operator==(const InstallPlan& that) const {
69 return ((is_resume == that.is_resume) &&
70 (download_url == that.download_url) && (payloads == that.payloads) &&
71 (source_slot == that.source_slot) &&
72 (target_slot == that.target_slot) && (partitions == that.partitions));
73 }
74
operator !=(const InstallPlan & that) const75 bool InstallPlan::operator!=(const InstallPlan& that) const {
76 return !((*this) == that);
77 }
78
Dump() const79 void InstallPlan::Dump() const {
80 LOG(INFO) << "InstallPlan: \n" << ToString();
81 }
82
ToString() const83 string InstallPlan::ToString() const {
84 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
91 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},
126 {"readonly_target_path", partition.readonly_target_path},
127 {"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");
151 }
152
LoadPartitionsFromSlots(BootControlInterface * boot_control)153 bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
154 bool result = true;
155 for (Partition& partition : partitions) {
156 if (source_slot != BootControlInterface::kInvalidSlot &&
157 partition.source_size > 0) {
158 TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice(
159 partition.name, source_slot, &partition.source_path));
160 } else {
161 partition.source_path.clear();
162 }
163
164 if (target_slot != BootControlInterface::kInvalidSlot &&
165 partition.target_size > 0) {
166 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;
170 partition.readonly_target_path = device->readonly_device_path;
171 } else {
172 partition.target_path.clear();
173 }
174 }
175 return result;
176 }
177
operator ==(const InstallPlan::Partition & that) const178 bool InstallPlan::Partition::operator==(
179 const InstallPlan::Partition& that) const {
180 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 &&
183 target_hash == that.target_hash &&
184 run_postinstall == that.run_postinstall &&
185 postinstall_path == that.postinstall_path &&
186 filesystem_type == that.filesystem_type &&
187 postinstall_optional == that.postinstall_optional);
188 }
189
ParseVerityConfig(const PartitionUpdate & partition)190 bool 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
228 template <typename PartitinoUpdateArray>
ParseManifestToInstallPlan(const PartitinoUpdateArray & partitions,BootControlInterface * boot_control,size_t block_size,InstallPlan * install_plan,ErrorCode * error)229 bool 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;
267 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;
272 }
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
ParsePartitions(const std::vector<PartitionUpdate> & partitions,BootControlInterface * boot_control,size_t block_size,ErrorCode * error)288 bool 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
ParsePartitions(const google::protobuf::RepeatedPtrField<PartitionUpdate> & partitions,BootControlInterface * boot_control,size_t block_size,ErrorCode * error)297 bool 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
306 } // namespace chromeos_update_engine
307