blob: d45de6a6997751131c2cc7b0aca32e175194785a [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Alex Deymof1cbe172015-03-05 15:58:37 -080016
17#include "update_engine/payload_generator/payload_generation_config.h"
18
Yifan Hong398cb542018-10-18 11:29:40 -070019#include <algorithm>
20#include <map>
21#include <utility>
22
Alex Deymoa26432a2015-03-12 16:08:04 -070023#include <base/logging.h>
Yifan Hong398cb542018-10-18 11:29:40 -070024#include <base/strings/string_number_conversions.h>
25#include <brillo/strings/string_utils.h>
Alex Deymoa26432a2015-03-12 16:08:04 -070026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/utils.h"
28#include "update_engine/payload_consumer/delta_performer.h"
Sen Jiang0a582fb2018-06-26 19:27:21 -070029#include "update_engine/payload_generator/boot_img_filesystem.h"
Alex Deymof1cbe172015-03-05 15:58:37 -080030#include "update_engine/payload_generator/delta_diff_generator.h"
Sen Jiangdcbc0ae2016-03-18 15:33:19 -070031#include "update_engine/payload_generator/delta_diff_utils.h"
Alex Deymob42b98d2015-07-06 17:42:38 -070032#include "update_engine/payload_generator/ext2_filesystem.h"
Alex Deymo20bdc702016-12-07 21:07:11 -080033#include "update_engine/payload_generator/mapfile_filesystem.h"
Alex Deymob42b98d2015-07-06 17:42:38 -070034#include "update_engine/payload_generator/raw_filesystem.h"
Amin Hassani77c25fc2019-01-29 10:24:19 -080035#include "update_engine/payload_generator/squashfs_filesystem.h"
Alex Deymof1cbe172015-03-05 15:58:37 -080036
Yifan Hong398cb542018-10-18 11:29:40 -070037using std::string;
38
Alex Deymof1cbe172015-03-05 15:58:37 -080039namespace chromeos_update_engine {
40
Sen Jiang05feee02015-11-11 15:59:49 -080041bool PostInstallConfig::IsEmpty() const {
Alex Deymo5b91c6b2016-08-04 20:33:36 -070042 return !run && path.empty() && filesystem_type.empty() && !optional;
Sen Jiang05feee02015-11-11 15:59:49 -080043}
44
Sen Jiang3a4dfac2018-08-30 16:57:38 -070045bool VerityConfig::IsEmpty() const {
46 return hash_tree_data_extent.num_blocks() == 0 &&
47 hash_tree_extent.num_blocks() == 0 && hash_tree_algorithm.empty() &&
48 hash_tree_salt.empty() && fec_data_extent.num_blocks() == 0 &&
49 fec_extent.num_blocks() == 0 && fec_roots == 0;
50}
51
Alex Deymo35589c22015-06-07 17:33:18 +020052bool PartitionConfig::ValidateExists() const {
53 TEST_AND_RETURN_FALSE(!path.empty());
54 TEST_AND_RETURN_FALSE(utils::FileExists(path.c_str()));
55 TEST_AND_RETURN_FALSE(size > 0);
Alex Deymof1cbe172015-03-05 15:58:37 -080056 // The requested size is within the limits of the file.
Alex Deymo35589c22015-06-07 17:33:18 +020057 TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
58 utils::FileSize(path.c_str()));
Alex Deymof1cbe172015-03-05 15:58:37 -080059 return true;
60}
61
Alex Deymob42b98d2015-07-06 17:42:38 -070062bool PartitionConfig::OpenFilesystem() {
63 if (path.empty())
64 return true;
65 fs_interface.reset();
Sen Jiangdcbc0ae2016-03-18 15:33:19 -070066 if (diff_utils::IsExtFilesystem(path)) {
Alex Deymob42b98d2015-07-06 17:42:38 -070067 fs_interface = Ext2Filesystem::CreateFromFile(path);
Sen Jiangdcbc0ae2016-03-18 15:33:19 -070068 // TODO(deymo): The delta generator algorithm doesn't support a block size
69 // different than 4 KiB. Remove this check once that's fixed. b/26972455
Alex Deymo20bdc702016-12-07 21:07:11 -080070 if (fs_interface) {
Sen Jiangdcbc0ae2016-03-18 15:33:19 -070071 TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
Alex Deymo20bdc702016-12-07 21:07:11 -080072 return true;
73 }
Alex Deymob42b98d2015-07-06 17:42:38 -070074 }
75
Alex Deymo20bdc702016-12-07 21:07:11 -080076 if (!mapfile_path.empty()) {
77 fs_interface = MapfileFilesystem::CreateFromFile(path, mapfile_path);
78 if (fs_interface) {
79 TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
80 return true;
81 }
Alex Deymob42b98d2015-07-06 17:42:38 -070082 }
Alex Deymo20bdc702016-12-07 21:07:11 -080083
Sen Jiang0a582fb2018-06-26 19:27:21 -070084 fs_interface = BootImgFilesystem::CreateFromFile(path);
85 if (fs_interface) {
86 TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
87 return true;
88 }
89
Amin Hassani77c25fc2019-01-29 10:24:19 -080090 fs_interface = SquashfsFilesystem::CreateFromFile(path,
91 /*extract_deflates=*/true,
92 /*load_settings=*/true);
93 if (fs_interface) {
94 TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
95 return true;
96 }
97
Alex Deymo20bdc702016-12-07 21:07:11 -080098 // Fall back to a RAW filesystem.
99 TEST_AND_RETURN_FALSE(size % kBlockSize == 0);
100 fs_interface = RawFilesystem::Create(
101 "<" + name + "-partition>", kBlockSize, size / kBlockSize);
Alex Deymob42b98d2015-07-06 17:42:38 -0700102 return true;
103}
104
Alex Deymof1cbe172015-03-05 15:58:37 -0800105bool ImageConfig::ValidateIsEmpty() const {
Sen Jiang981eb112015-08-25 17:03:18 -0700106 return partitions.empty();
Alex Deymof1cbe172015-03-05 15:58:37 -0800107}
108
109bool ImageConfig::LoadImageSize() {
Sen Jiang981eb112015-08-25 17:03:18 -0700110 for (PartitionConfig& part : partitions) {
111 if (part.path.empty())
112 continue;
113 part.size = utils::FileSize(part.path);
114 }
Alex Deymof1cbe172015-03-05 15:58:37 -0800115 return true;
116}
117
Sen Jiang05feee02015-11-11 15:59:49 -0800118bool ImageConfig::LoadPostInstallConfig(const brillo::KeyValueStore& store) {
119 bool found_postinstall = false;
120 for (PartitionConfig& part : partitions) {
121 bool run_postinstall;
122 if (!store.GetBoolean("RUN_POSTINSTALL_" + part.name, &run_postinstall) ||
123 !run_postinstall)
124 continue;
125 found_postinstall = true;
126 part.postinstall.run = true;
127 store.GetString("POSTINSTALL_PATH_" + part.name, &part.postinstall.path);
128 store.GetString("FILESYSTEM_TYPE_" + part.name,
129 &part.postinstall.filesystem_type);
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700130 store.GetBoolean("POSTINSTALL_OPTIONAL_" + part.name,
131 &part.postinstall.optional);
Sen Jiang05feee02015-11-11 15:59:49 -0800132 }
133 if (!found_postinstall) {
134 LOG(ERROR) << "No valid postinstall config found.";
135 return false;
136 }
137 return true;
138}
139
Yifan Hong398cb542018-10-18 11:29:40 -0700140bool ImageConfig::LoadDynamicPartitionMetadata(
141 const brillo::KeyValueStore& store) {
142 auto metadata = std::make_unique<DynamicPartitionMetadata>();
143 string buf;
144 if (!store.GetString("super_partition_groups", &buf)) {
145 LOG(ERROR) << "Dynamic partition info missing super_partition_groups.";
146 return false;
147 }
148 auto group_names = brillo::string_utils::Split(buf, " ");
149 for (const auto& group_name : group_names) {
150 DynamicPartitionGroup* group = metadata->add_groups();
151 group->set_name(group_name);
Yifan Hong8683ed42019-11-01 16:58:29 -0700152 if (!store.GetString("super_" + group_name + "_group_size", &buf) &&
153 !store.GetString(group_name + "_size", &buf)) {
154 LOG(ERROR) << "Missing super_" << group_name + "_group_size or "
155 << group_name << "_size.";
Yifan Hong398cb542018-10-18 11:29:40 -0700156 return false;
157 }
158
159 uint64_t max_size;
160 if (!base::StringToUint64(buf, &max_size)) {
Yifan Hong8683ed42019-11-01 16:58:29 -0700161 LOG(ERROR) << "Group size for " << group_name << " = " << buf
162 << " is not an integer.";
Yifan Hong398cb542018-10-18 11:29:40 -0700163 return false;
164 }
165 group->set_size(max_size);
166
Yifan Hong8683ed42019-11-01 16:58:29 -0700167 if (store.GetString("super_" + group_name + "_partition_list", &buf) ||
168 store.GetString(group_name + "_partition_list", &buf)) {
Yifan Hong398cb542018-10-18 11:29:40 -0700169 auto partition_names = brillo::string_utils::Split(buf, " ");
170 for (const auto& partition_name : partition_names) {
171 group->add_partition_names()->assign(partition_name);
172 }
173 }
174 }
Yifan Hong05b3b962019-09-26 17:19:21 -0700175
176 bool snapshot_enabled = false;
177 store.GetBoolean("virtual_ab", &snapshot_enabled);
178 metadata->set_snapshot_enabled(snapshot_enabled);
Kelvin Zhangad8ea102021-01-14 10:14:44 -0500179 bool vabc_enabled = false;
Kelvin Zhang413982e2021-03-02 15:34:50 -0500180 if (store.GetBoolean("virtual_ab_compression", &vabc_enabled) &&
181 vabc_enabled) {
182 LOG(INFO) << "Target build supports VABC";
Kelvin Zhangad8ea102021-01-14 10:14:44 -0500183 metadata->set_vabc_enabled(vabc_enabled);
184 }
Kelvin Zhang4ca06c12021-02-04 17:16:40 -0500185 // We use "gz" compression by default for VABC.
186 if (metadata->vabc_enabled()) {
187 metadata->set_vabc_compression_param("gz");
188 }
Yifan Hong398cb542018-10-18 11:29:40 -0700189 dynamic_partition_metadata = std::move(metadata);
190 return true;
191}
192
193bool ImageConfig::ValidateDynamicPartitionMetadata() const {
194 if (dynamic_partition_metadata == nullptr) {
195 LOG(ERROR) << "dynamic_partition_metadata is not loaded.";
196 return false;
197 }
198
199 for (const auto& group : dynamic_partition_metadata->groups()) {
200 uint64_t sum_size = 0;
201 for (const auto& partition_name : group.partition_names()) {
202 auto partition_config = std::find_if(partitions.begin(),
203 partitions.end(),
204 [&partition_name](const auto& e) {
205 return e.name == partition_name;
206 });
207
208 if (partition_config == partitions.end()) {
209 LOG(ERROR) << "Cannot find partition " << partition_name
210 << " which is in " << group.name() << "_partition_list";
211 return false;
212 }
213 sum_size += partition_config->size;
214 }
215
216 if (sum_size > group.size()) {
217 LOG(ERROR) << "Sum of sizes in " << group.name() << "_partition_list is "
218 << sum_size << ", which is greater than " << group.name()
219 << "_size (" << group.size() << ")";
220 return false;
221 }
222 }
223 return true;
224}
225
Alex Deymoa4073ef2016-03-22 23:40:53 -0700226PayloadVersion::PayloadVersion(uint64_t major_version, uint32_t minor_version) {
227 major = major_version;
228 minor = minor_version;
229}
230
231bool PayloadVersion::Validate() const {
Amin Hassani55c75412019-10-07 11:20:39 -0700232 TEST_AND_RETURN_FALSE(major == kBrilloMajorPayloadVersion);
Alex Deymoa4073ef2016-03-22 23:40:53 -0700233 TEST_AND_RETURN_FALSE(minor == kFullPayloadMinorVersion ||
Alex Deymoa4073ef2016-03-22 23:40:53 -0700234 minor == kSourceMinorPayloadVersion ||
235 minor == kOpSrcHashMinorPayloadVersion ||
Amin Hassani77d7cbc2018-02-07 16:21:33 -0800236 minor == kBrotliBsdiffMinorPayloadVersion ||
Sen Jiang3a4dfac2018-08-30 16:57:38 -0700237 minor == kPuffdiffMinorPayloadVersion ||
Tianjief5baff42020-07-17 21:43:22 -0700238 minor == kVerityMinorPayloadVersion ||
239 minor == kPartialUpdateMinorPayloadVersion);
Alex Deymoa4073ef2016-03-22 23:40:53 -0700240 return true;
241}
242
Sen Jiangcebb6882019-02-26 16:23:28 +0800243bool PayloadVersion::OperationAllowed(InstallOperation::Type operation) const {
Alex Deymoa4073ef2016-03-22 23:40:53 -0700244 switch (operation) {
245 // Full operations:
246 case InstallOperation::REPLACE:
247 case InstallOperation::REPLACE_BZ:
248 // These operations were included in the original payload format.
Alex Deymoa4073ef2016-03-22 23:40:53 -0700249 case InstallOperation::REPLACE_XZ:
Amin Hassani55c75412019-10-07 11:20:39 -0700250 // These operations are included minor version 3 or newer and full
251 // payloads.
252 return true;
Alex Deymoa4073ef2016-03-22 23:40:53 -0700253
Alex Deymo0497d052016-03-23 09:16:59 -0700254 case InstallOperation::ZERO:
255 case InstallOperation::DISCARD:
256 // The implementation of these operations had a bug in earlier versions
257 // that prevents them from being used in any payload. We will enable
258 // them for delta payloads for now.
Amin Hassani77d7cbc2018-02-07 16:21:33 -0800259 return minor >= kBrotliBsdiffMinorPayloadVersion;
Alex Deymo0497d052016-03-23 09:16:59 -0700260
Alex Deymoa4073ef2016-03-22 23:40:53 -0700261 case InstallOperation::SOURCE_COPY:
262 case InstallOperation::SOURCE_BSDIFF:
263 return minor >= kSourceMinorPayloadVersion;
264
Amin Hassaniefa62d92017-11-09 13:46:56 -0800265 case InstallOperation::BROTLI_BSDIFF:
Amin Hassani77d7cbc2018-02-07 16:21:33 -0800266 return minor >= kBrotliBsdiffMinorPayloadVersion;
267
Tianjie Xu15de2fd2018-02-05 17:46:48 -0800268 case InstallOperation::PUFFDIFF:
Amin Hassani77d7cbc2018-02-07 16:21:33 -0800269 return minor >= kPuffdiffMinorPayloadVersion;
Amin Hassani2fe84322020-10-28 21:43:23 +0000270
271 case InstallOperation::MOVE:
272 case InstallOperation::BSDIFF:
273 NOTREACHED();
Alex Deymoa4073ef2016-03-22 23:40:53 -0700274 }
275 return false;
276}
277
Tianjief5baff42020-07-17 21:43:22 -0700278bool PayloadVersion::IsDeltaOrPartial() const {
Alex Deymoa4073ef2016-03-22 23:40:53 -0700279 return minor != kFullPayloadMinorVersion;
280}
281
Alex Deymof1cbe172015-03-05 15:58:37 -0800282bool PayloadGenerationConfig::Validate() const {
Alex Deymoa4073ef2016-03-22 23:40:53 -0700283 TEST_AND_RETURN_FALSE(version.Validate());
Tianjief5baff42020-07-17 21:43:22 -0700284 TEST_AND_RETURN_FALSE(version.IsDeltaOrPartial() ==
285 (is_delta || is_partial_update));
Alex Deymof1cbe172015-03-05 15:58:37 -0800286 if (is_delta) {
Sen Jiang981eb112015-08-25 17:03:18 -0700287 for (const PartitionConfig& part : source.partitions) {
288 if (!part.path.empty()) {
289 TEST_AND_RETURN_FALSE(part.ValidateExists());
290 TEST_AND_RETURN_FALSE(part.size % block_size == 0);
291 }
Sen Jiang3a4dfac2018-08-30 16:57:38 -0700292 // Source partition should not have postinstall or verity config.
Sen Jiang05feee02015-11-11 15:59:49 -0800293 TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
Sen Jiang3a4dfac2018-08-30 16:57:38 -0700294 TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
Alex Deymof1cbe172015-03-05 15:58:37 -0800295 }
296
Alex Deymof1cbe172015-03-05 15:58:37 -0800297 } else {
298 // All the "source" image fields must be empty for full payloads.
299 TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
Alex Deymof1cbe172015-03-05 15:58:37 -0800300 }
301
302 // In all cases, the target image must exists.
Sen Jiang981eb112015-08-25 17:03:18 -0700303 for (const PartitionConfig& part : target.partitions) {
304 TEST_AND_RETURN_FALSE(part.ValidateExists());
305 TEST_AND_RETURN_FALSE(part.size % block_size == 0);
Sen Jiang3a4dfac2018-08-30 16:57:38 -0700306 if (version.minor < kVerityMinorPayloadVersion)
307 TEST_AND_RETURN_FALSE(part.verity.IsEmpty());
Sen Jiang981eb112015-08-25 17:03:18 -0700308 }
Alex Deymof1cbe172015-03-05 15:58:37 -0800309
Tianjief5baff42020-07-17 21:43:22 -0700310 if (version.minor < kPartialUpdateMinorPayloadVersion) {
311 TEST_AND_RETURN_FALSE(!is_partial_update);
312 }
313
Alex Deymo2d3b2d62015-07-17 17:34:36 -0700314 TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
315 hard_chunk_size % block_size == 0);
316 TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
Alex Deymo9b244df2015-03-11 21:51:18 -0700317
318 TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
Alex Deymo9b244df2015-03-11 21:51:18 -0700319
Alex Deymof1cbe172015-03-05 15:58:37 -0800320 return true;
321}
322
323} // namespace chromeos_update_engine