payload_generation_config.cc revision cbf09896be9a627cd04dd54ef6875bed88daa3d8
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/delta_performer.h"
22#include "update_engine/payload_generator/delta_diff_generator.h"
23#include "update_engine/payload_generator/ext2_filesystem.h"
24#include "update_engine/payload_generator/raw_filesystem.h"
25#include "update_engine/utils.h"
26
27namespace chromeos_update_engine {
28
29bool PartitionConfig::ValidateExists() const {
30  TEST_AND_RETURN_FALSE(!path.empty());
31  TEST_AND_RETURN_FALSE(utils::FileExists(path.c_str()));
32  TEST_AND_RETURN_FALSE(size > 0);
33  // The requested size is within the limits of the file.
34  TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
35                        utils::FileSize(path.c_str()));
36  return true;
37}
38
39bool PartitionConfig::OpenFilesystem() {
40  if (path.empty())
41    return true;
42  fs_interface.reset();
43  if (utils::IsExtFilesystem(path)) {
44    fs_interface = Ext2Filesystem::CreateFromFile(path);
45  }
46
47  if (!fs_interface) {
48    // Fall back to a RAW filesystem.
49    TEST_AND_RETURN_FALSE(size % kBlockSize == 0);
50    fs_interface = RawFilesystem::Create(
51      "<" + name + "-partition>",
52      kBlockSize,
53      size / kBlockSize);
54  }
55  return true;
56}
57
58bool ImageConfig::ValidateIsEmpty() const {
59  TEST_AND_RETURN_FALSE(ImageInfoIsEmpty());
60
61  TEST_AND_RETURN_FALSE(rootfs.path.empty());
62  TEST_AND_RETURN_FALSE(rootfs.size == 0);
63  TEST_AND_RETURN_FALSE(kernel.path.empty());
64  TEST_AND_RETURN_FALSE(kernel.size == 0);
65  return true;
66}
67
68bool ImageConfig::LoadImageSize() {
69  TEST_AND_RETURN_FALSE(!rootfs.path.empty());
70  int rootfs_block_count, rootfs_block_size;
71  TEST_AND_RETURN_FALSE(utils::GetFilesystemSize(rootfs.path,
72                                                 &rootfs_block_count,
73                                                 &rootfs_block_size));
74  rootfs.size = static_cast<uint64_t>(rootfs_block_count) * rootfs_block_size;
75  if (!kernel.path.empty())
76    kernel.size = utils::FileSize(kernel.path);
77
78  // TODO(deymo): The delta generator algorithm doesn't support a block size
79  // different than 4 KiB. Remove this check once that's fixed. crbug.com/455045
80  if (rootfs_block_size != 4096) {
81    LOG(ERROR) << "The filesystem provided in " << rootfs.path
82               << " has a block size of " << rootfs_block_size
83               << " but delta_generator only supports 4096.";
84    return false;
85  }
86  return true;
87}
88
89bool ImageConfig::ImageInfoIsEmpty() const {
90  return image_info.board().empty()
91    && image_info.key().empty()
92    && image_info.channel().empty()
93    && image_info.version().empty()
94    && image_info.build_channel().empty()
95    && image_info.build_version().empty();
96}
97
98bool PayloadGenerationConfig::Validate() const {
99  if (is_delta) {
100    TEST_AND_RETURN_FALSE(source.rootfs.ValidateExists());
101    TEST_AND_RETURN_FALSE(source.rootfs.size % block_size == 0);
102
103    if (!source.kernel.path.empty()) {
104      TEST_AND_RETURN_FALSE(source.kernel.ValidateExists());
105      TEST_AND_RETURN_FALSE(source.kernel.size % block_size == 0);
106    }
107
108    // Check for the supported minor_version values.
109    TEST_AND_RETURN_FALSE(minor_version == kInPlaceMinorPayloadVersion ||
110                          minor_version == kSourceMinorPayloadVersion);
111
112    // If new_image_info is present, old_image_info must be present.
113    TEST_AND_RETURN_FALSE(source.ImageInfoIsEmpty() ==
114                          target.ImageInfoIsEmpty());
115  } else {
116    // All the "source" image fields must be empty for full payloads.
117    TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
118    TEST_AND_RETURN_FALSE(minor_version == kFullPayloadMinorVersion);
119  }
120
121  // In all cases, the target image must exists.
122  TEST_AND_RETURN_FALSE(target.rootfs.ValidateExists());
123  TEST_AND_RETURN_FALSE(target.kernel.ValidateExists());
124  TEST_AND_RETURN_FALSE(target.rootfs.size % block_size == 0);
125  TEST_AND_RETURN_FALSE(target.kernel.size % block_size == 0);
126
127  TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
128                        hard_chunk_size % block_size == 0);
129  TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
130
131  TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
132  TEST_AND_RETURN_FALSE(rootfs_partition_size >= target.rootfs.size);
133
134  return true;
135}
136
137}  // namespace chromeos_update_engine
138