• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
21 #include <utility>
22 
23 #include <base/logging.h>
24 #include <base/strings/string_number_conversions.h>
25 #include <brillo/strings/string_utils.h>
26 #include <libsnapshot/cow_format.h>
27 
28 #include "update_engine/common/utils.h"
29 #include "update_engine/payload_consumer/delta_performer.h"
30 #include "update_engine/payload_generator/boot_img_filesystem.h"
31 #include "update_engine/payload_generator/delta_diff_generator.h"
32 #include "update_engine/payload_generator/delta_diff_utils.h"
33 #include "update_engine/payload_generator/ext2_filesystem.h"
34 #include "update_engine/payload_generator/mapfile_filesystem.h"
35 #include "update_engine/payload_generator/raw_filesystem.h"
36 #include "update_engine/payload_generator/squashfs_filesystem.h"
37 
38 using std::string;
39 
40 namespace chromeos_update_engine {
41 
IsEmpty() const42 bool PostInstallConfig::IsEmpty() const {
43   return !run && path.empty() && filesystem_type.empty() && !optional;
44 }
45 
IsEmpty() const46 bool VerityConfig::IsEmpty() const {
47   return hash_tree_data_extent.num_blocks() == 0 &&
48          hash_tree_extent.num_blocks() == 0 && hash_tree_algorithm.empty() &&
49          hash_tree_salt.empty() && fec_data_extent.num_blocks() == 0 &&
50          fec_extent.num_blocks() == 0 && fec_roots == 0;
51 }
52 
ValidateExists() const53 bool PartitionConfig::ValidateExists() const {
54   TEST_AND_RETURN_FALSE(!path.empty());
55   TEST_AND_RETURN_FALSE(utils::FileExists(path.c_str()));
56   TEST_AND_RETURN_FALSE(size > 0);
57   // The requested size is within the limits of the file.
58   TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
59                         utils::FileSize(path.c_str()));
60   return true;
61 }
62 
OpenFilesystem()63 bool PartitionConfig::OpenFilesystem() {
64   if (path.empty())
65     return true;
66   fs_interface.reset();
67   if (diff_utils::IsExtFilesystem(path)) {
68     fs_interface = Ext2Filesystem::CreateFromFile(path);
69     // TODO(deymo): The delta generator algorithm doesn't support a block size
70     // different than 4 KiB. Remove this check once that's fixed. b/26972455
71     if (fs_interface) {
72       TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
73       return true;
74     }
75   }
76 
77   if (!mapfile_path.empty()) {
78     fs_interface = MapfileFilesystem::CreateFromFile(path, mapfile_path);
79     if (fs_interface) {
80       TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
81       return true;
82     }
83   }
84 
85   fs_interface = BootImgFilesystem::CreateFromFile(path);
86   if (fs_interface) {
87     TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
88     return true;
89   }
90 
91   fs_interface = SquashfsFilesystem::CreateFromFile(path,
92                                                     /*extract_deflates=*/true,
93                                                     /*load_settings=*/true);
94   if (fs_interface) {
95     TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
96     return true;
97   }
98 
99   // Fall back to a RAW filesystem.
100   TEST_AND_RETURN_FALSE(size % kBlockSize == 0);
101   fs_interface = RawFilesystem::Create(
102       "<" + name + "-partition>", kBlockSize, size / kBlockSize);
103   return true;
104 }
105 
ValidateIsEmpty() const106 bool ImageConfig::ValidateIsEmpty() const {
107   return partitions.empty();
108 }
109 
LoadImageSize()110 bool ImageConfig::LoadImageSize() {
111   for (PartitionConfig& part : partitions) {
112     if (part.path.empty())
113       continue;
114     part.size = utils::FileSize(part.path);
115   }
116   return true;
117 }
118 
LoadPostInstallConfig(const brillo::KeyValueStore & store)119 bool ImageConfig::LoadPostInstallConfig(const brillo::KeyValueStore& store) {
120   bool found_postinstall = false;
121   for (PartitionConfig& part : partitions) {
122     bool run_postinstall;
123     if (!store.GetBoolean("RUN_POSTINSTALL_" + part.name, &run_postinstall) ||
124         !run_postinstall)
125       continue;
126     found_postinstall = true;
127     part.postinstall.run = true;
128     store.GetString("POSTINSTALL_PATH_" + part.name, &part.postinstall.path);
129     store.GetString("FILESYSTEM_TYPE_" + part.name,
130                     &part.postinstall.filesystem_type);
131     store.GetBoolean("POSTINSTALL_OPTIONAL_" + part.name,
132                      &part.postinstall.optional);
133   }
134   if (!found_postinstall) {
135     LOG(ERROR) << "No valid postinstall config found.";
136     return false;
137   }
138   return true;
139 }
140 
LoadDynamicPartitionMetadata(const brillo::KeyValueStore & store)141 bool ImageConfig::LoadDynamicPartitionMetadata(
142     const brillo::KeyValueStore& store) {
143   auto metadata = std::make_unique<DynamicPartitionMetadata>();
144   string buf;
145   if (!store.GetString("super_partition_groups", &buf)) {
146     LOG(ERROR) << "Dynamic partition info missing super_partition_groups.";
147     return false;
148   }
149   auto group_names = brillo::string_utils::Split(buf, " ");
150   for (const auto& group_name : group_names) {
151     DynamicPartitionGroup* group = metadata->add_groups();
152     group->set_name(group_name);
153     if (!store.GetString("super_" + group_name + "_group_size", &buf) &&
154         !store.GetString(group_name + "_size", &buf)) {
155       LOG(ERROR) << "Missing super_" << group_name + "_group_size or "
156                  << group_name << "_size.";
157       return false;
158     }
159 
160     uint64_t max_size;
161     if (!base::StringToUint64(buf, &max_size)) {
162       LOG(ERROR) << "Group size for " << group_name << " = " << buf
163                  << " is not an integer.";
164       return false;
165     }
166     group->set_size(max_size);
167 
168     if (store.GetString("super_" + group_name + "_partition_list", &buf) ||
169         store.GetString(group_name + "_partition_list", &buf)) {
170       auto partition_names = brillo::string_utils::Split(buf, " ");
171       for (const auto& partition_name : partition_names) {
172         group->add_partition_names()->assign(partition_name);
173       }
174     }
175   }
176 
177   bool snapshot_enabled = false;
178   store.GetBoolean("virtual_ab", &snapshot_enabled);
179   metadata->set_snapshot_enabled(snapshot_enabled);
180   bool vabc_enabled = false;
181   if (store.GetBoolean("virtual_ab_compression", &vabc_enabled) &&
182       vabc_enabled) {
183     LOG(INFO) << "Target build supports VABC";
184     metadata->set_vabc_enabled(vabc_enabled);
185   }
186   // We use "gz" compression by default for VABC.
187   if (metadata->vabc_enabled()) {
188     metadata->set_vabc_compression_param("gz");
189     metadata->set_cow_version(android::snapshot::kCowVersionManifest);
190   }
191   dynamic_partition_metadata = std::move(metadata);
192   return true;
193 }
194 
ValidateDynamicPartitionMetadata() const195 bool ImageConfig::ValidateDynamicPartitionMetadata() const {
196   if (dynamic_partition_metadata == nullptr) {
197     LOG(ERROR) << "dynamic_partition_metadata is not loaded.";
198     return false;
199   }
200 
201   for (const auto& group : dynamic_partition_metadata->groups()) {
202     uint64_t sum_size = 0;
203     for (const auto& partition_name : group.partition_names()) {
204       auto partition_config = std::find_if(partitions.begin(),
205                                            partitions.end(),
206                                            [&partition_name](const auto& e) {
207                                              return e.name == partition_name;
208                                            });
209 
210       if (partition_config == partitions.end()) {
211         LOG(ERROR) << "Cannot find partition " << partition_name
212                    << " which is in " << group.name() << "_partition_list";
213         return false;
214       }
215       sum_size += partition_config->size;
216     }
217 
218     if (sum_size > group.size()) {
219       LOG(ERROR) << "Sum of sizes in " << group.name() << "_partition_list is "
220                  << sum_size << ", which is greater than " << group.name()
221                  << "_size (" << group.size() << ")";
222       return false;
223     }
224   }
225   return true;
226 }
227 
PayloadVersion(uint64_t major_version,uint32_t minor_version)228 PayloadVersion::PayloadVersion(uint64_t major_version, uint32_t minor_version) {
229   major = major_version;
230   minor = minor_version;
231 }
232 
Validate() const233 bool PayloadVersion::Validate() const {
234   TEST_AND_RETURN_FALSE(major == kBrilloMajorPayloadVersion);
235   TEST_AND_RETURN_FALSE(minor == kFullPayloadMinorVersion ||
236                         minor == kSourceMinorPayloadVersion ||
237                         minor == kOpSrcHashMinorPayloadVersion ||
238                         minor == kBrotliBsdiffMinorPayloadVersion ||
239                         minor == kPuffdiffMinorPayloadVersion ||
240                         minor == kVerityMinorPayloadVersion ||
241                         minor == kPartialUpdateMinorPayloadVersion);
242   return true;
243 }
244 
OperationAllowed(InstallOperation::Type operation) const245 bool PayloadVersion::OperationAllowed(InstallOperation::Type operation) const {
246   switch (operation) {
247     // Full operations:
248     case InstallOperation::REPLACE:
249     case InstallOperation::REPLACE_BZ:
250       // These operations were included in the original payload format.
251     case InstallOperation::REPLACE_XZ:
252       // These operations are included minor version 3 or newer and full
253       // payloads.
254       return true;
255 
256     case InstallOperation::ZERO:
257     case InstallOperation::DISCARD:
258       // The implementation of these operations had a bug in earlier versions
259       // that prevents them from being used in any payload. We will enable
260       // them for delta payloads for now.
261       return minor >= kBrotliBsdiffMinorPayloadVersion;
262 
263     case InstallOperation::SOURCE_COPY:
264     case InstallOperation::SOURCE_BSDIFF:
265       return minor >= kSourceMinorPayloadVersion;
266 
267     case InstallOperation::BROTLI_BSDIFF:
268       return minor >= kBrotliBsdiffMinorPayloadVersion;
269 
270     case InstallOperation::PUFFDIFF:
271       return minor >= kPuffdiffMinorPayloadVersion;
272 
273     case InstallOperation::MOVE:
274     case InstallOperation::BSDIFF:
275       NOTREACHED();
276   }
277   return false;
278 }
279 
IsDeltaOrPartial() const280 bool PayloadVersion::IsDeltaOrPartial() const {
281   return minor != kFullPayloadMinorVersion;
282 }
283 
Validate() const284 bool PayloadGenerationConfig::Validate() const {
285   TEST_AND_RETURN_FALSE(version.Validate());
286   TEST_AND_RETURN_FALSE(version.IsDeltaOrPartial() ==
287                         (is_delta || is_partial_update));
288   if (is_delta) {
289     for (const PartitionConfig& part : source.partitions) {
290       if (!part.path.empty()) {
291         TEST_AND_RETURN_FALSE(part.ValidateExists());
292         TEST_AND_RETURN_FALSE(part.size % block_size == 0);
293       }
294       // Source partition should not have postinstall or verity config.
295       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
296       TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
297     }
298 
299   } else {
300     // All the "source" image fields must be empty for full payloads.
301     TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
302   }
303 
304   // In all cases, the target image must exists.
305   for (const PartitionConfig& part : target.partitions) {
306     TEST_AND_RETURN_FALSE(part.ValidateExists());
307     TEST_AND_RETURN_FALSE(part.size % block_size == 0);
308     if (version.minor < kVerityMinorPayloadVersion)
309       TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
310   }
311 
312   if (version.minor < kPartialUpdateMinorPayloadVersion) {
313     TEST_AND_RETURN_FALSE(!is_partial_update);
314   }
315 
316   TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
317                         hard_chunk_size % block_size == 0);
318   TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
319 
320   TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
321 
322   return true;
323 }
324 
325 }  // namespace chromeos_update_engine
326