xref: /aosp_15_r20/system/update_engine/payload_consumer/install_plan.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
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 <android-base/stringprintf.h>
26 
27 #include "update_engine/common/utils.h"
28 #include "update_engine/update_metadata.pb.h"
29 
30 using std::string;
31 using std::vector;
32 
33 namespace chromeos_update_engine {
34 
35 namespace {
PayloadUrlsToString(const decltype(InstallPlan::Payload::payload_urls) & payload_urls)36 string PayloadUrlsToString(
37     const decltype(InstallPlan::Payload::payload_urls)& payload_urls) {
38   return "(" + android::base::Join(payload_urls, ",") + ")";
39 }
40 
VectorToString(const vector<std::pair<string,string>> & input,const string & separator)41 string 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) {
48                    return android::base::Join(vector{pair.first, pair.second},
49                                               ": ");
50                  });
51   return android::base::Join(vec, separator);
52 }
53 }  // namespace
54 
InstallPayloadTypeToString(InstallPayloadType type)55 string 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 }
66 
operator ==(const InstallPlan & that) const67 bool InstallPlan::operator==(const InstallPlan& that) const {
68   return ((is_resume == that.is_resume) &&
69           (download_url == that.download_url) && (payloads == that.payloads) &&
70           (source_slot == that.source_slot) &&
71           (target_slot == that.target_slot) && (partitions == that.partitions));
72 }
73 
operator !=(const InstallPlan & that) const74 bool InstallPlan::operator!=(const InstallPlan& that) const {
75   return !((*this) == that);
76 }
77 
Dump() const78 void InstallPlan::Dump() const {
79   LOG(INFO) << "InstallPlan: \n" << ToString();
80 }
81 
ToString() const82 string InstallPlan::ToString() const {
83   string url_str = download_url;
84   if (android::base::StartsWith(ToLower(url_str), "fd://")) {
85     int fd = std::stoi(url_str.substr(strlen("fd://")));
86     url_str = utils::GetFilePath(fd);
87   }
88 
89   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)},
101           {"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},
109             {"source_size", std::format("{}", partition.source_size)},
110             {"source_path", partition.source_path},
111             {"source_hash",
112              base::HexEncode(partition.source_hash.data(),
113                              partition.source_hash.size())},
114             {"target_size", std::format("{}", partition.target_size)},
115             {"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},
121             {"readonly_target_path", partition.readonly_target_path},
122             {"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         {
131             {"Payload", std::format("{}", i)},
132             {"urls", PayloadUrlsToString(payload.payload_urls)},
133             {"size", std::format("{}", payload.size)},
134             {"metadata_size", std::format("{}", payload.metadata_size)},
135             {"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 
145   return android::base::Join(result_str, "\n");
146 }
147 
LoadPartitionsFromSlots(BootControlInterface * boot_control)148 bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
149   bool result = true;
150   for (Partition& partition : partitions) {
151     if (source_slot != BootControlInterface::kInvalidSlot &&
152         partition.source_size > 0) {
153       TEST_AND_RETURN_FALSE(boot_control->GetPartitionDevice(
154           partition.name, source_slot, &partition.source_path));
155     } else {
156       partition.source_path.clear();
157     }
158 
159     if (target_slot != BootControlInterface::kInvalidSlot &&
160         partition.target_size > 0) {
161       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;
165       partition.readonly_target_path = device->readonly_device_path;
166     } else {
167       partition.target_path.clear();
168     }
169   }
170   return result;
171 }
172 
operator ==(const InstallPlan::Partition & that) const173 bool InstallPlan::Partition::operator==(
174     const InstallPlan::Partition& that) const {
175   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 &&
178           target_hash == that.target_hash &&
179           run_postinstall == that.run_postinstall &&
180           postinstall_path == that.postinstall_path &&
181           filesystem_type == that.filesystem_type &&
182           postinstall_optional == that.postinstall_optional);
183 }
184 
ParseVerityConfig(const PartitionUpdate & partition)185 bool 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 
223 template <typename PartitinoUpdateArray>
ParseManifestToInstallPlan(const PartitinoUpdateArray & partitions,BootControlInterface * boot_control,size_t block_size,InstallPlan * install_plan,ErrorCode * error)224 bool 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;
262     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;
267     }
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 
ParsePartitions(const std::vector<PartitionUpdate> & partitions,BootControlInterface * boot_control,size_t block_size,ErrorCode * error)283 bool 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 
ParsePartitions(const google::protobuf::RepeatedPtrField<PartitionUpdate> & partitions,BootControlInterface * boot_control,size_t block_size,ErrorCode * error)292 bool 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 
301 }  // namespace chromeos_update_engine
302