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