xref: /aosp_15_r20/system/update_engine/payload_generator/payload_generation_config.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1 //
2 // Copyright (C) 2015 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_generator/payload_generation_config.h"
18 
19 #include <algorithm>
20 #include <charconv>
21 #include <utility>
22 
23 #include <android-base/parseint.h>
24 #include <base/logging.h>
25 #include <brillo/strings/string_utils.h>
26 #include <libsnapshot/cow_format.h>
27 
28 #include "bsdiff/constants.h"
29 #include "payload_consumer/payload_constants.h"
30 #include "update_engine/common/utils.h"
31 #include "update_engine/payload_generator/boot_img_filesystem.h"
32 #include "update_engine/payload_generator/delta_diff_generator.h"
33 #include "update_engine/payload_generator/delta_diff_utils.h"
34 #include "update_engine/payload_generator/erofs_filesystem.h"
35 #include "update_engine/payload_generator/ext2_filesystem.h"
36 #include "update_engine/payload_generator/mapfile_filesystem.h"
37 #include "update_engine/payload_generator/raw_filesystem.h"
38 #include "update_engine/payload_generator/squashfs_filesystem.h"
39 #include "update_engine/update_metadata.pb.h"
40 
41 using std::string;
42 
43 namespace chromeos_update_engine {
44 
IsEmpty() const45 bool PostInstallConfig::IsEmpty() const {
46   return !run && path.empty() && filesystem_type.empty() && !optional;
47 }
48 
IsEmpty() const49 bool VerityConfig::IsEmpty() const {
50   return hash_tree_data_extent.num_blocks() == 0 &&
51          hash_tree_extent.num_blocks() == 0 && hash_tree_algorithm.empty() &&
52          hash_tree_salt.empty() && fec_data_extent.num_blocks() == 0 &&
53          fec_extent.num_blocks() == 0 && fec_roots == 0;
54 }
55 
Clear()56 void VerityConfig::Clear() {
57   hash_tree_data_extent.Clear();
58   hash_tree_extent.Clear();
59   hash_tree_algorithm.clear();
60   hash_tree_salt.clear();
61   fec_data_extent.Clear();
62   fec_extent.Clear();
63   fec_roots = 0;
64 }
65 
ValidateExists() const66 bool PartitionConfig::ValidateExists() const {
67   TEST_AND_RETURN_FALSE(!path.empty());
68   TEST_AND_RETURN_FALSE(utils::FileExists(path.c_str()));
69   TEST_AND_RETURN_FALSE(size > 0);
70   // The requested size is within the limits of the file.
71   TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
72                         utils::FileSize(path.c_str()));
73   return true;
74 }
75 
OpenFilesystem()76 bool PartitionConfig::OpenFilesystem() {
77   if (path.empty())
78     return true;
79   fs_interface.reset();
80   if (diff_utils::IsExtFilesystem(path)) {
81     fs_interface = Ext2Filesystem::CreateFromFile(path);
82     // TODO(deymo): The delta generator algorithm doesn't support a block size
83     // different than 4 KiB. Remove this check once that's fixed. b/26972455
84     if (fs_interface) {
85       TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
86       return true;
87     }
88   }
89   fs_interface = ErofsFilesystem::CreateFromFile(path, erofs_compression_param);
90   if (fs_interface) {
91     TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
92     return true;
93   }
94 
95   if (!mapfile_path.empty()) {
96     fs_interface = MapfileFilesystem::CreateFromFile(path, mapfile_path);
97     if (fs_interface) {
98       TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
99       return true;
100     }
101   }
102 
103   fs_interface = BootImgFilesystem::CreateFromFile(path);
104   if (fs_interface) {
105     TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
106     return true;
107   }
108 
109   fs_interface = SquashfsFilesystem::CreateFromFile(path,
110                                                     /*extract_deflates=*/true);
111   if (fs_interface) {
112     TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
113     return true;
114   }
115 
116   // Fall back to a RAW filesystem.
117   TEST_AND_RETURN_FALSE(size % kBlockSize == 0);
118   fs_interface = RawFilesystem::Create(
119       "<" + name + "-partition>", kBlockSize, size / kBlockSize);
120   return true;
121 }
122 
ValidateIsEmpty() const123 bool ImageConfig::ValidateIsEmpty() const {
124   return partitions.empty();
125 }
126 
LoadImageSize()127 bool ImageConfig::LoadImageSize() {
128   for (PartitionConfig& part : partitions) {
129     if (part.path.empty())
130       continue;
131     part.size = utils::FileSize(part.path);
132   }
133   return true;
134 }
135 
LoadPostInstallConfig(const brillo::KeyValueStore & store)136 bool ImageConfig::LoadPostInstallConfig(const brillo::KeyValueStore& store) {
137   bool found_postinstall = false;
138   for (PartitionConfig& part : partitions) {
139     bool run_postinstall{};
140     if (!store.GetBoolean("RUN_POSTINSTALL_" + part.name, &run_postinstall) ||
141         !run_postinstall)
142       continue;
143     found_postinstall = true;
144     part.postinstall.run = true;
145     store.GetString("POSTINSTALL_PATH_" + part.name, &part.postinstall.path);
146     store.GetString("FILESYSTEM_TYPE_" + part.name,
147                     &part.postinstall.filesystem_type);
148     store.GetBoolean("POSTINSTALL_OPTIONAL_" + part.name,
149                      &part.postinstall.optional);
150   }
151   if (!found_postinstall) {
152     LOG(ERROR) << "No valid postinstall config found.";
153     return false;
154   }
155   return true;
156 }
157 
LoadDynamicPartitionMetadata(const brillo::KeyValueStore & store)158 bool ImageConfig::LoadDynamicPartitionMetadata(
159     const brillo::KeyValueStore& store) {
160   auto metadata = std::make_unique<DynamicPartitionMetadata>();
161   string buf;
162   if (!store.GetString("super_partition_groups", &buf)) {
163     LOG(ERROR) << "Dynamic partition info missing super_partition_groups.";
164     return false;
165   }
166   auto group_names = brillo::string_utils::Split(buf, " ");
167   for (const auto& group_name : group_names) {
168     DynamicPartitionGroup* group = metadata->add_groups();
169     group->set_name(group_name);
170     if (!store.GetString("super_" + group_name + "_group_size", &buf) &&
171         !store.GetString(group_name + "_size", &buf)) {
172       LOG(ERROR) << "Missing super_" << group_name + "_group_size or "
173                  << group_name << "_size.";
174       return false;
175     }
176 
177     uint64_t max_size{};
178     if (!android::base::ParseUint<uint64_t>(buf, &max_size)) {
179       LOG(ERROR) << "Group size for " << group_name << " = " << buf
180                  << " is not an integer.";
181       return false;
182     }
183     group->set_size(max_size);
184 
185     if (store.GetString("super_" + group_name + "_partition_list", &buf) ||
186         store.GetString(group_name + "_partition_list", &buf)) {
187       auto partition_names = brillo::string_utils::Split(buf, " ");
188       for (const auto& partition_name : partition_names) {
189         group->add_partition_names()->assign(partition_name);
190       }
191     }
192   }
193 
194   bool snapshot_enabled = false;
195   store.GetBoolean("virtual_ab", &snapshot_enabled);
196   metadata->set_snapshot_enabled(snapshot_enabled);
197   bool vabc_enabled = false;
198   if (store.GetBoolean("virtual_ab_compression", &vabc_enabled) &&
199       vabc_enabled) {
200     LOG(INFO) << "Target build supports VABC";
201     metadata->set_vabc_enabled(vabc_enabled);
202   }
203   // We use "gz" compression by default for VABC.
204   if (metadata->vabc_enabled()) {
205     std::string compression_method;
206     if (store.GetString("virtual_ab_compression_method", &compression_method)) {
207       LOG(INFO) << "Using VABC compression method '" << compression_method
208                 << "'";
209     } else {
210       LOG(INFO) << "No VABC compression method specified. Defaulting to 'gz'";
211       compression_method = "gz";
212     }
213     metadata->set_vabc_compression_param(compression_method);
214     std::string cow_version;
215     if (!store.GetString("virtual_ab_cow_version", &cow_version)) {
216       metadata->set_cow_version(2);
217     } else {
218       uint32_t cow_version_num{};
219       android::base::ParseUint(cow_version, &cow_version_num);
220       metadata->set_cow_version(cow_version_num);
221     }
222     std::string compression_factor;
223     if (store.GetString("virtual_ab_compression_factor", &compression_factor)) {
224       LOG(INFO) << "Using VABC compression factor " << compression_factor;
225     } else {
226       LOG(INFO) << "No compression factor specified. Defaulting to 4k";
227       compression_factor = "4096";
228     }
229     size_t compression_factor_value{};
230     if (!android::base::ParseUint(compression_factor,
231                                   &compression_factor_value)) {
232       LOG(ERROR) << "failed to parse compression factor value: "
233                  << compression_factor;
234       return false;
235     }
236     CHECK_EQ(static_cast<int>(compression_factor_value % kBlockSize), 0);
237     CHECK_EQ(static_cast<int>(compression_factor_value &
238                               (compression_factor_value - 1)),
239              0);
240     metadata->set_compression_factor(compression_factor_value);
241   }
242   dynamic_partition_metadata = std::move(metadata);
243   return true;
244 }
245 
ValidateDynamicPartitionMetadata() const246 bool ImageConfig::ValidateDynamicPartitionMetadata() const {
247   if (dynamic_partition_metadata == nullptr) {
248     LOG(ERROR) << "dynamic_partition_metadata is not loaded.";
249     return false;
250   }
251 
252   for (const auto& group : dynamic_partition_metadata->groups()) {
253     uint64_t sum_size = 0;
254     for (const auto& partition_name : group.partition_names()) {
255       auto partition_config = std::find_if(partitions.begin(),
256                                            partitions.end(),
257                                            [&partition_name](const auto& e) {
258                                              return e.name == partition_name;
259                                            });
260 
261       if (partition_config == partitions.end()) {
262         LOG(ERROR) << "Cannot find partition " << partition_name
263                    << " which is in " << group.name() << "_partition_list";
264         return false;
265       }
266       sum_size += partition_config->size;
267     }
268 
269     if (sum_size > group.size()) {
270       LOG(ERROR) << "Sum of sizes in " << group.name() << "_partition_list is "
271                  << sum_size << ", which is greater than " << group.name()
272                  << "_size (" << group.size() << ")";
273       return false;
274     }
275   }
276   return true;
277 }
278 
PayloadVersion(uint64_t major_version,uint32_t minor_version)279 PayloadVersion::PayloadVersion(uint64_t major_version, uint32_t minor_version) {
280   major = major_version;
281   minor = minor_version;
282 }
283 
Validate() const284 bool PayloadVersion::Validate() const {
285   TEST_AND_RETURN_FALSE(major == kBrilloMajorPayloadVersion);
286   TEST_AND_RETURN_FALSE(minor == kFullPayloadMinorVersion ||
287                         minor == kSourceMinorPayloadVersion ||
288                         minor == kOpSrcHashMinorPayloadVersion ||
289                         minor == kBrotliBsdiffMinorPayloadVersion ||
290                         minor == kPuffdiffMinorPayloadVersion ||
291                         minor == kVerityMinorPayloadVersion ||
292                         minor == kPartialUpdateMinorPayloadVersion ||
293                         minor == kZucchiniMinorPayloadVersion ||
294                         minor == kLZ4DIFFMinorPayloadVersion);
295   return true;
296 }
297 
OperationAllowed(InstallOperation::Type operation) const298 bool PayloadVersion::OperationAllowed(InstallOperation::Type operation) const {
299   switch (operation) {
300     // Full operations:
301     case InstallOperation::REPLACE:
302     case InstallOperation::REPLACE_BZ:
303       // These operations were included in the original payload format.
304     case InstallOperation::REPLACE_XZ:
305       // These operations are included minor version 3 or newer and full
306       // payloads.
307       return true;
308 
309     case InstallOperation::ZERO:
310     case InstallOperation::DISCARD:
311       // The implementation of these operations had a bug in earlier versions
312       // that prevents them from being used in any payload. We will enable
313       // them for delta payloads for now.
314       return minor >= kBrotliBsdiffMinorPayloadVersion;
315 
316     case InstallOperation::SOURCE_COPY:
317     case InstallOperation::SOURCE_BSDIFF:
318       return minor >= kSourceMinorPayloadVersion;
319 
320     case InstallOperation::BROTLI_BSDIFF:
321       return minor >= kBrotliBsdiffMinorPayloadVersion;
322 
323     case InstallOperation::PUFFDIFF:
324       return minor >= kPuffdiffMinorPayloadVersion;
325 
326     case InstallOperation::ZUCCHINI:
327       return minor >= kZucchiniMinorPayloadVersion;
328     case InstallOperation::LZ4DIFF_BSDIFF:
329     case InstallOperation::LZ4DIFF_PUFFDIFF:
330       return minor >= kLZ4DIFFMinorPayloadVersion;
331 
332     case InstallOperation::MOVE:
333     case InstallOperation::BSDIFF:
334       NOTREACHED();
335   }
336   return false;
337 }
338 
IsDeltaOrPartial() const339 bool PayloadVersion::IsDeltaOrPartial() const {
340   return minor != kFullPayloadMinorVersion;
341 }
342 
Validate() const343 bool PayloadGenerationConfig::Validate() const {
344   TEST_AND_RETURN_FALSE(version.Validate());
345   TEST_AND_RETURN_FALSE(version.IsDeltaOrPartial() ==
346                         (is_delta || is_partial_update));
347   if (is_delta) {
348     for (const PartitionConfig& part : source.partitions) {
349       if (!part.path.empty()) {
350         TEST_AND_RETURN_FALSE(part.ValidateExists());
351         TEST_AND_RETURN_FALSE(part.size % block_size == 0);
352       }
353       // Source partition should not have postinstall or verity config.
354       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
355       TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
356     }
357 
358   } else {
359     // All the "source" image fields must be empty for full payloads.
360     TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
361   }
362 
363   // In all cases, the target image must exists.
364   for (const PartitionConfig& part : target.partitions) {
365     TEST_AND_RETURN_FALSE(part.ValidateExists());
366     TEST_AND_RETURN_FALSE(part.size % block_size == 0);
367     if (version.minor < kVerityMinorPayloadVersion)
368       TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
369   }
370 
371   if (version.minor < kPartialUpdateMinorPayloadVersion) {
372     TEST_AND_RETURN_FALSE(!is_partial_update);
373   }
374 
375   TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
376                         hard_chunk_size % block_size == 0);
377   TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
378 
379   TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
380 
381   return true;
382 }
383 
ParseCompressorTypes(const std::string & compressor_types)384 void PayloadGenerationConfig::ParseCompressorTypes(
385     const std::string& compressor_types) {
386   auto types = brillo::string_utils::Split(compressor_types, ":");
387   CHECK_LE(types.size(), 2UL)
388       << "Only two compressor types are allowed: bz2 and brotli";
389   CHECK_GT(types.size(), 0UL) << "Please pass in at least 1 valid compressor. "
390                                  "Allowed values are bz2 and brotli.";
391   compressors.clear();
392   for (const auto& type : types) {
393     if (type == "bz2") {
394       compressors.emplace_back(bsdiff::CompressorType::kBZ2);
395     } else if (type == "brotli") {
396       compressors.emplace_back(bsdiff::CompressorType::kBrotli);
397     } else {
398       LOG(FATAL) << "Unknown compressor type: " << type;
399     }
400   }
401 }
402 
OperationEnabled(InstallOperation::Type op) const403 bool PayloadGenerationConfig::OperationEnabled(
404     InstallOperation::Type op) const noexcept {
405   if (!version.OperationAllowed(op)) {
406     return false;
407   }
408   switch (op) {
409     case InstallOperation::ZUCCHINI:
410       return enable_zucchini;
411     case InstallOperation::LZ4DIFF_BSDIFF:
412     case InstallOperation::LZ4DIFF_PUFFDIFF:
413       return enable_lz4diff;
414     case InstallOperation::PUFFDIFF:
415       return enable_puffdiff;
416     default:
417       return true;
418   }
419 }
420 
ParseCompressionParam(std::string_view param)421 CompressionAlgorithm PartitionConfig::ParseCompressionParam(
422     std::string_view param) {
423   CompressionAlgorithm algo;
424   auto algo_name = param;
425   const auto pos = param.find_first_of(',');
426   if (pos != std::string::npos) {
427     algo_name = param.substr(0, pos);
428   }
429   if (algo_name == "lz4") {
430     algo.set_type(CompressionAlgorithm::LZ4);
431     CHECK_EQ(pos, std::string::npos)
432         << "Invalid compression param " << param
433         << ", compression level not supported for lz4";
434   } else if (algo_name == "lz4hc") {
435     algo.set_type(CompressionAlgorithm::LZ4HC);
436     if (pos != std::string::npos) {
437       const auto level = param.substr(pos + 1);
438       int level_num = 0;
439       const auto [ptr, ec] =
440           std::from_chars(level.data(), level.data() + level.size(), level_num);
441       CHECK_EQ(ec, std::errc()) << "Failed to parse compression level " << level
442                                 << ", compression param: " << param;
443       algo.set_level(level_num);
444     } else {
445       LOG(FATAL) << "Unrecognized compression type: " << algo_name
446                  << ", param: " << param;
447     }
448   }
449   return algo;
450 }
451 
452 }  // namespace chromeos_update_engine
453