Home | History | Annotate | Download | only in payload_generator
      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 
     29 bool PostInstallConfig::IsEmpty() const {
     30   return !run && path.empty() && filesystem_type.empty() && !optional;
     31 }
     32 
     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);
     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 
     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 
     72 bool ImageConfig::ValidateIsEmpty() const {
     73   TEST_AND_RETURN_FALSE(ImageInfoIsEmpty());
     74   return partitions.empty();
     75 }
     76 
     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 
     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     store.GetBoolean("POSTINSTALL_OPTIONAL_" + part.name,
     99                      &part.postinstall.optional);
    100   }
    101   if (!found_postinstall) {
    102     LOG(ERROR) << "No valid postinstall config found.";
    103     return false;
    104   }
    105   return true;
    106 }
    107 
    108 bool ImageConfig::ImageInfoIsEmpty() const {
    109   return image_info.board().empty()
    110     && image_info.key().empty()
    111     && image_info.channel().empty()
    112     && image_info.version().empty()
    113     && image_info.build_channel().empty()
    114     && image_info.build_version().empty();
    115 }
    116 
    117 PayloadVersion::PayloadVersion(uint64_t major_version, uint32_t minor_version) {
    118   major = major_version;
    119   minor = minor_version;
    120 }
    121 
    122 bool PayloadVersion::Validate() const {
    123   TEST_AND_RETURN_FALSE(major == kChromeOSMajorPayloadVersion ||
    124                         major == kBrilloMajorPayloadVersion);
    125   TEST_AND_RETURN_FALSE(minor == kFullPayloadMinorVersion ||
    126                         minor == kInPlaceMinorPayloadVersion ||
    127                         minor == kSourceMinorPayloadVersion ||
    128                         minor == kOpSrcHashMinorPayloadVersion ||
    129                         minor == kImgdiffMinorPayloadVersion);
    130   return true;
    131 }
    132 
    133 bool PayloadVersion::OperationAllowed(InstallOperation_Type operation) const {
    134   switch (operation) {
    135     // Full operations:
    136     case InstallOperation::REPLACE:
    137     case InstallOperation::REPLACE_BZ:
    138       // These operations were included in the original payload format.
    139       return true;
    140 
    141     case InstallOperation::REPLACE_XZ:
    142       // These operations are included in the major version used in Brillo, but
    143       // can also be used with minor version 3 or newer.
    144       return major == kBrilloMajorPayloadVersion ||
    145              minor >= kOpSrcHashMinorPayloadVersion;
    146 
    147     case InstallOperation::ZERO:
    148     case InstallOperation::DISCARD:
    149       // The implementation of these operations had a bug in earlier versions
    150       // that prevents them from being used in any payload. We will enable
    151       // them for delta payloads for now.
    152       return minor >= kImgdiffMinorPayloadVersion;
    153 
    154     // Delta operations:
    155     case InstallOperation::MOVE:
    156     case InstallOperation::BSDIFF:
    157       // MOVE and BSDIFF were replaced by SOURCE_COPY and SOURCE_BSDIFF and
    158       // should not be used in newer delta versions, since the idempotent checks
    159       // were removed.
    160       return minor == kInPlaceMinorPayloadVersion;
    161 
    162     case InstallOperation::SOURCE_COPY:
    163     case InstallOperation::SOURCE_BSDIFF:
    164       return minor >= kSourceMinorPayloadVersion;
    165 
    166     case InstallOperation::IMGDIFF:
    167       return minor >= kImgdiffMinorPayloadVersion && imgdiff_allowed;
    168   }
    169   return false;
    170 }
    171 
    172 bool PayloadVersion::IsDelta() const {
    173   return minor != kFullPayloadMinorVersion;
    174 }
    175 
    176 bool PayloadVersion::InplaceUpdate() const {
    177   return minor == kInPlaceMinorPayloadVersion;
    178 }
    179 
    180 bool PayloadGenerationConfig::Validate() const {
    181   TEST_AND_RETURN_FALSE(version.Validate());
    182   TEST_AND_RETURN_FALSE(version.IsDelta() == is_delta);
    183   if (is_delta) {
    184     for (const PartitionConfig& part : source.partitions) {
    185       if (!part.path.empty()) {
    186         TEST_AND_RETURN_FALSE(part.ValidateExists());
    187         TEST_AND_RETURN_FALSE(part.size % block_size == 0);
    188       }
    189       // Source partition should not have postinstall.
    190       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
    191     }
    192 
    193     // If new_image_info is present, old_image_info must be present.
    194     TEST_AND_RETURN_FALSE(source.ImageInfoIsEmpty() ==
    195                           target.ImageInfoIsEmpty());
    196   } else {
    197     // All the "source" image fields must be empty for full payloads.
    198     TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
    199   }
    200 
    201   // In all cases, the target image must exists.
    202   for (const PartitionConfig& part : target.partitions) {
    203     TEST_AND_RETURN_FALSE(part.ValidateExists());
    204     TEST_AND_RETURN_FALSE(part.size % block_size == 0);
    205     if (version.minor == kInPlaceMinorPayloadVersion &&
    206         part.name == kLegacyPartitionNameRoot)
    207       TEST_AND_RETURN_FALSE(rootfs_partition_size >= part.size);
    208     if (version.major == kChromeOSMajorPayloadVersion)
    209       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
    210   }
    211 
    212   TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
    213                         hard_chunk_size % block_size == 0);
    214   TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
    215 
    216   TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
    217 
    218   return true;
    219 }
    220 
    221 }  // namespace chromeos_update_engine
    222