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