• 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 <base/logging.h>
20 
21 #include "update_engine/common/utils.h"
22 #include "update_engine/payload_consumer/delta_performer.h"
23 #include "update_engine/payload_generator/delta_diff_generator.h"
24 #include "update_engine/payload_generator/ext2_filesystem.h"
25 #include "update_engine/payload_generator/mapfile_filesystem.h"
26 #include "update_engine/payload_generator/raw_filesystem.h"
27 
28 namespace chromeos_update_engine {
29 
IsEmpty() const30 bool PostInstallConfig::IsEmpty() const {
31   return !run && path.empty() && filesystem_type.empty() && !optional;
32 }
33 
ValidateExists() const34 bool PartitionConfig::ValidateExists() const {
35   TEST_AND_RETURN_FALSE(!path.empty());
36   TEST_AND_RETURN_FALSE(utils::FileExists(path.c_str()));
37   TEST_AND_RETURN_FALSE(size > 0);
38   // The requested size is within the limits of the file.
39   TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
40                         utils::FileSize(path.c_str()));
41   // TODO(deymo): The delta generator algorithm doesn't support a block size
42   // different than 4 KiB. Remove this check once that's fixed. crbug.com/455045
43   int block_count, block_size;
44   if (utils::GetFilesystemSize(path, &block_count, &block_size) &&
45       block_size != 4096) {
46    LOG(ERROR) << "The filesystem provided in " << path
47               << " has a block size of " << block_size
48               << " but delta_generator only supports 4096.";
49    return false;
50   }
51   return true;
52 }
53 
OpenFilesystem()54 bool PartitionConfig::OpenFilesystem() {
55   if (path.empty())
56     return true;
57   fs_interface.reset();
58   if (utils::IsExtFilesystem(path)) {
59     fs_interface = Ext2Filesystem::CreateFromFile(path);
60   }
61 
62   if (!mapfile_path.empty()) {
63     fs_interface = MapfileFilesystem::CreateFromFile(path, mapfile_path);
64     if (fs_interface) {
65       TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
66       return true;
67     }
68   }
69 
70   // Fall back to a RAW filesystem.
71   TEST_AND_RETURN_FALSE(size % kBlockSize == 0);
72   fs_interface = RawFilesystem::Create(
73       "<" + name + "-partition>", kBlockSize, size / kBlockSize);
74   return true;
75 }
76 
ValidateIsEmpty() const77 bool ImageConfig::ValidateIsEmpty() const {
78   TEST_AND_RETURN_FALSE(ImageInfoIsEmpty());
79   return partitions.empty();
80 }
81 
LoadImageSize()82 bool ImageConfig::LoadImageSize() {
83   for (PartitionConfig& part : partitions) {
84     if (part.path.empty())
85       continue;
86     part.size = utils::FileSize(part.path);
87   }
88   return true;
89 }
90 
LoadPostInstallConfig(const brillo::KeyValueStore & store)91 bool ImageConfig::LoadPostInstallConfig(const brillo::KeyValueStore& store) {
92   bool found_postinstall = false;
93   for (PartitionConfig& part : partitions) {
94     bool run_postinstall;
95     if (!store.GetBoolean("RUN_POSTINSTALL_" + part.name, &run_postinstall) ||
96         !run_postinstall)
97       continue;
98     found_postinstall = true;
99     part.postinstall.run = true;
100     store.GetString("POSTINSTALL_PATH_" + part.name, &part.postinstall.path);
101     store.GetString("FILESYSTEM_TYPE_" + part.name,
102                     &part.postinstall.filesystem_type);
103     store.GetBoolean("POSTINSTALL_OPTIONAL_" + part.name,
104                      &part.postinstall.optional);
105   }
106   if (!found_postinstall) {
107     LOG(ERROR) << "No valid postinstall config found.";
108     return false;
109   }
110   return true;
111 }
112 
ImageInfoIsEmpty() const113 bool ImageConfig::ImageInfoIsEmpty() const {
114   return image_info.board().empty()
115     && image_info.key().empty()
116     && image_info.channel().empty()
117     && image_info.version().empty()
118     && image_info.build_channel().empty()
119     && image_info.build_version().empty();
120 }
121 
PayloadVersion(uint64_t major_version,uint32_t minor_version)122 PayloadVersion::PayloadVersion(uint64_t major_version, uint32_t minor_version) {
123   major = major_version;
124   minor = minor_version;
125 }
126 
Validate() const127 bool PayloadVersion::Validate() const {
128   TEST_AND_RETURN_FALSE(major == kChromeOSMajorPayloadVersion ||
129                         major == kBrilloMajorPayloadVersion);
130   TEST_AND_RETURN_FALSE(minor == kFullPayloadMinorVersion ||
131                         minor == kInPlaceMinorPayloadVersion ||
132                         minor == kSourceMinorPayloadVersion ||
133                         minor == kOpSrcHashMinorPayloadVersion ||
134                         minor == kImgdiffMinorPayloadVersion);
135   return true;
136 }
137 
OperationAllowed(InstallOperation_Type operation) const138 bool PayloadVersion::OperationAllowed(InstallOperation_Type operation) const {
139   switch (operation) {
140     // Full operations:
141     case InstallOperation::REPLACE:
142     case InstallOperation::REPLACE_BZ:
143       // These operations were included in the original payload format.
144       return true;
145 
146     case InstallOperation::REPLACE_XZ:
147       // These operations are included in the major version used in Brillo, but
148       // can also be used with minor version 3 or newer.
149       return major == kBrilloMajorPayloadVersion ||
150              minor >= kOpSrcHashMinorPayloadVersion;
151 
152     case InstallOperation::ZERO:
153     case InstallOperation::DISCARD:
154       // The implementation of these operations had a bug in earlier versions
155       // that prevents them from being used in any payload. We will enable
156       // them for delta payloads for now.
157       return minor >= kImgdiffMinorPayloadVersion;
158 
159     // Delta operations:
160     case InstallOperation::MOVE:
161     case InstallOperation::BSDIFF:
162       // MOVE and BSDIFF were replaced by SOURCE_COPY and SOURCE_BSDIFF and
163       // should not be used in newer delta versions, since the idempotent checks
164       // were removed.
165       return minor == kInPlaceMinorPayloadVersion;
166 
167     case InstallOperation::SOURCE_COPY:
168     case InstallOperation::SOURCE_BSDIFF:
169       return minor >= kSourceMinorPayloadVersion;
170 
171     case InstallOperation::IMGDIFF:
172       return minor >= kImgdiffMinorPayloadVersion && imgdiff_allowed;
173   }
174   return false;
175 }
176 
IsDelta() const177 bool PayloadVersion::IsDelta() const {
178   return minor != kFullPayloadMinorVersion;
179 }
180 
InplaceUpdate() const181 bool PayloadVersion::InplaceUpdate() const {
182   return minor == kInPlaceMinorPayloadVersion;
183 }
184 
Validate() const185 bool PayloadGenerationConfig::Validate() const {
186   TEST_AND_RETURN_FALSE(version.Validate());
187   TEST_AND_RETURN_FALSE(version.IsDelta() == is_delta);
188   if (is_delta) {
189     for (const PartitionConfig& part : source.partitions) {
190       if (!part.path.empty()) {
191         TEST_AND_RETURN_FALSE(part.ValidateExists());
192         TEST_AND_RETURN_FALSE(part.size % block_size == 0);
193       }
194       // Source partition should not have postinstall.
195       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
196     }
197 
198     // If new_image_info is present, old_image_info must be present.
199     TEST_AND_RETURN_FALSE(source.ImageInfoIsEmpty() ==
200                           target.ImageInfoIsEmpty());
201   } else {
202     // All the "source" image fields must be empty for full payloads.
203     TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
204   }
205 
206   // In all cases, the target image must exists.
207   for (const PartitionConfig& part : target.partitions) {
208     TEST_AND_RETURN_FALSE(part.ValidateExists());
209     TEST_AND_RETURN_FALSE(part.size % block_size == 0);
210     if (version.minor == kInPlaceMinorPayloadVersion &&
211         part.name == kLegacyPartitionNameRoot)
212       TEST_AND_RETURN_FALSE(rootfs_partition_size >= part.size);
213     if (version.major == kChromeOSMajorPayloadVersion)
214       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
215   }
216 
217   TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
218                         hard_chunk_size % block_size == 0);
219   TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
220 
221   TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
222 
223   return true;
224 }
225 
226 }  // namespace chromeos_update_engine
227