blob: f93a95a4e0f7a7e6c8ffe8f54998d0e57f13cb1a [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
Alex Deymoa26432a2015-03-12 16:08:04 -070019#include <base/logging.h>
20
Alex Deymo39910dc2015-11-09 17:04:30 -080021#include "update_engine/common/utils.h"
22#include "update_engine/payload_consumer/delta_performer.h"
Alex Deymof1cbe172015-03-05 15:58:37 -080023#include "update_engine/payload_generator/delta_diff_generator.h"
Alex Deymob42b98d2015-07-06 17:42:38 -070024#include "update_engine/payload_generator/ext2_filesystem.h"
25#include "update_engine/payload_generator/raw_filesystem.h"
Alex Deymof1cbe172015-03-05 15:58:37 -080026
27namespace chromeos_update_engine {
28
Sen Jiang05feee02015-11-11 15:59:49 -080029bool PostInstallConfig::IsEmpty() const {
30 return run == false && path.empty() && filesystem_type.empty();
31}
32
Alex Deymo35589c22015-06-07 17:33:18 +020033bool 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);
Alex Deymof1cbe172015-03-05 15:58:37 -080037 // The requested size is within the limits of the file.
Alex Deymo35589c22015-06-07 17:33:18 +020038 TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
39 utils::FileSize(path.c_str()));
Sen Jiangf2af4c62015-09-30 16:38:09 -070040 // 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 }
Alex Deymof1cbe172015-03-05 15:58:37 -080050 return true;
51}
52
Alex Deymob42b98d2015-07-06 17:42:38 -070053bool PartitionConfig::OpenFilesystem() {
54 if (path.empty())
55 return true;
56 fs_interface.reset();
Sen Jiang625406c2015-09-16 16:35:23 -070057 if (utils::IsExtFilesystem(path)) {
Alex Deymob42b98d2015-07-06 17:42:38 -070058 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);
Alex Deymob42b98d2015-07-06 17:42:38 -070064 fs_interface = RawFilesystem::Create(
Sen Jiang625406c2015-09-16 16:35:23 -070065 "<" + name + "-partition>",
Alex Deymob42b98d2015-07-06 17:42:38 -070066 kBlockSize,
67 size / kBlockSize);
68 }
69 return true;
70}
71
Alex Deymof1cbe172015-03-05 15:58:37 -080072bool ImageConfig::ValidateIsEmpty() const {
73 TEST_AND_RETURN_FALSE(ImageInfoIsEmpty());
Sen Jiang981eb112015-08-25 17:03:18 -070074 return partitions.empty();
Alex Deymof1cbe172015-03-05 15:58:37 -080075}
76
77bool ImageConfig::LoadImageSize() {
Sen Jiang981eb112015-08-25 17:03:18 -070078 for (PartitionConfig& part : partitions) {
79 if (part.path.empty())
80 continue;
81 part.size = utils::FileSize(part.path);
82 }
Alex Deymof1cbe172015-03-05 15:58:37 -080083 return true;
84}
85
Sen Jiang05feee02015-11-11 15:59:49 -080086bool 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
Alex Deymof1cbe172015-03-05 15:58:37 -0800106bool 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
115bool PayloadGenerationConfig::Validate() const {
116 if (is_delta) {
Sen Jiang981eb112015-08-25 17:03:18 -0700117 for (const PartitionConfig& part : source.partitions) {
118 if (!part.path.empty()) {
119 TEST_AND_RETURN_FALSE(part.ValidateExists());
120 TEST_AND_RETURN_FALSE(part.size % block_size == 0);
121 }
Sen Jiang05feee02015-11-11 15:59:49 -0800122 // Source partition should not have postinstall.
123 TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
Alex Deymof1cbe172015-03-05 15:58:37 -0800124 }
125
Alex Deymof1cbe172015-03-05 15:58:37 -0800126 // Check for the supported minor_version values.
127 TEST_AND_RETURN_FALSE(minor_version == kInPlaceMinorPayloadVersion ||
Sen Jiang82352f92015-11-09 16:36:53 -0800128 minor_version == kSourceMinorPayloadVersion ||
Sen Jiang55c4f9b2016-02-10 11:26:20 -0800129 minor_version == kOpSrcHashMinorPayloadVersion ||
130 minor_version == kImgdiffMinorPayloadVersion);
131
132 if (imgdiff_allowed)
133 TEST_AND_RETURN_FALSE(minor_version >= kImgdiffMinorPayloadVersion);
Alex Deymof1cbe172015-03-05 15:58:37 -0800134
135 // If new_image_info is present, old_image_info must be present.
136 TEST_AND_RETURN_FALSE(source.ImageInfoIsEmpty() ==
137 target.ImageInfoIsEmpty());
138 } else {
139 // All the "source" image fields must be empty for full payloads.
140 TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
Alex Deymocbf09892015-09-11 16:13:16 -0700141 TEST_AND_RETURN_FALSE(minor_version == kFullPayloadMinorVersion);
Alex Deymof1cbe172015-03-05 15:58:37 -0800142 }
143
144 // In all cases, the target image must exists.
Sen Jiang981eb112015-08-25 17:03:18 -0700145 for (const PartitionConfig& part : target.partitions) {
146 TEST_AND_RETURN_FALSE(part.ValidateExists());
147 TEST_AND_RETURN_FALSE(part.size % block_size == 0);
148 if (minor_version == kInPlaceMinorPayloadVersion &&
149 part.name == kLegacyPartitionNameRoot)
150 TEST_AND_RETURN_FALSE(rootfs_partition_size >= part.size);
Sen Jiang05feee02015-11-11 15:59:49 -0800151 if (major_version == kChromeOSMajorPayloadVersion)
152 TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
Sen Jiang981eb112015-08-25 17:03:18 -0700153 }
Alex Deymof1cbe172015-03-05 15:58:37 -0800154
Alex Deymo2d3b2d62015-07-17 17:34:36 -0700155 TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
156 hard_chunk_size % block_size == 0);
157 TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
Alex Deymo9b244df2015-03-11 21:51:18 -0700158
159 TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
Alex Deymo9b244df2015-03-11 21:51:18 -0700160
Alex Deymof1cbe172015-03-05 15:58:37 -0800161 return true;
162}
163
164} // namespace chromeos_update_engine