Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/payload_generator/payload_generation_config.h" |
| 18 | |
Alex Deymo | a26432a | 2015-03-12 16:08:04 -0700 | [diff] [blame] | 19 | #include <base/logging.h> |
| 20 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 21 | #include "update_engine/common/utils.h" |
| 22 | #include "update_engine/payload_consumer/delta_performer.h" |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 23 | #include "update_engine/payload_generator/delta_diff_generator.h" |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 24 | #include "update_engine/payload_generator/ext2_filesystem.h" |
| 25 | #include "update_engine/payload_generator/raw_filesystem.h" |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 26 | |
| 27 | namespace chromeos_update_engine { |
| 28 | |
Sen Jiang | 05feee0 | 2015-11-11 15:59:49 -0800 | [diff] [blame] | 29 | bool PostInstallConfig::IsEmpty() const { |
| 30 | return run == false && path.empty() && filesystem_type.empty(); |
| 31 | } |
| 32 | |
Alex Deymo | 35589c2 | 2015-06-07 17:33:18 +0200 | [diff] [blame] | 33 | 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); |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 37 | // The requested size is within the limits of the file. |
Alex Deymo | 35589c2 | 2015-06-07 17:33:18 +0200 | [diff] [blame] | 38 | TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <= |
| 39 | utils::FileSize(path.c_str())); |
Sen Jiang | f2af4c6 | 2015-09-30 16:38:09 -0700 | [diff] [blame] | 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 | } |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 53 | bool PartitionConfig::OpenFilesystem() { |
| 54 | if (path.empty()) |
| 55 | return true; |
| 56 | fs_interface.reset(); |
Sen Jiang | 625406c | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 57 | if (utils::IsExtFilesystem(path)) { |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 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); |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 64 | fs_interface = RawFilesystem::Create( |
Sen Jiang | 625406c | 2015-09-16 16:35:23 -0700 | [diff] [blame] | 65 | "<" + name + "-partition>", |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 66 | kBlockSize, |
| 67 | size / kBlockSize); |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 72 | bool ImageConfig::ValidateIsEmpty() const { |
| 73 | TEST_AND_RETURN_FALSE(ImageInfoIsEmpty()); |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 74 | return partitions.empty(); |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool ImageConfig::LoadImageSize() { |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 78 | for (PartitionConfig& part : partitions) { |
| 79 | if (part.path.empty()) |
| 80 | continue; |
| 81 | part.size = utils::FileSize(part.path); |
| 82 | } |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
Sen Jiang | 05feee0 | 2015-11-11 15:59:49 -0800 | [diff] [blame] | 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 | |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 106 | 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 | |
| 115 | bool PayloadGenerationConfig::Validate() const { |
| 116 | if (is_delta) { |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 117 | 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 Jiang | 05feee0 | 2015-11-11 15:59:49 -0800 | [diff] [blame] | 122 | // Source partition should not have postinstall. |
| 123 | TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty()); |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 126 | // Check for the supported minor_version values. |
| 127 | TEST_AND_RETURN_FALSE(minor_version == kInPlaceMinorPayloadVersion || |
Sen Jiang | 82352f9 | 2015-11-09 16:36:53 -0800 | [diff] [blame] | 128 | minor_version == kSourceMinorPayloadVersion || |
Sen Jiang | 55c4f9b | 2016-02-10 11:26:20 -0800 | [diff] [blame] | 129 | minor_version == kOpSrcHashMinorPayloadVersion || |
| 130 | minor_version == kImgdiffMinorPayloadVersion); |
| 131 | |
| 132 | if (imgdiff_allowed) |
| 133 | TEST_AND_RETURN_FALSE(minor_version >= kImgdiffMinorPayloadVersion); |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 134 | |
| 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 Deymo | cbf0989 | 2015-09-11 16:13:16 -0700 | [diff] [blame] | 141 | TEST_AND_RETURN_FALSE(minor_version == kFullPayloadMinorVersion); |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // In all cases, the target image must exists. |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 145 | 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 Jiang | 05feee0 | 2015-11-11 15:59:49 -0800 | [diff] [blame] | 151 | if (major_version == kChromeOSMajorPayloadVersion) |
| 152 | TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty()); |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 153 | } |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 154 | |
Alex Deymo | 2d3b2d6 | 2015-07-17 17:34:36 -0700 | [diff] [blame] | 155 | 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 Deymo | 9b244df | 2015-03-11 21:51:18 -0700 | [diff] [blame] | 158 | |
| 159 | TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0); |
Alex Deymo | 9b244df | 2015-03-11 21:51:18 -0700 | [diff] [blame] | 160 | |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | } // namespace chromeos_update_engine |