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/delta_diff_utils.h"
     25 #include "update_engine/payload_generator/ext2_filesystem.h"
     26 #include "update_engine/payload_generator/mapfile_filesystem.h"
     27 #include "update_engine/payload_generator/raw_filesystem.h"
     28 
     29 namespace chromeos_update_engine {
     30 
     31 bool PostInstallConfig::IsEmpty() const {
     32   return !run && path.empty() && filesystem_type.empty() && !optional;
     33 }
     34 
     35 bool PartitionConfig::ValidateExists() const {
     36   TEST_AND_RETURN_FALSE(!path.empty());
     37   TEST_AND_RETURN_FALSE(utils::FileExists(path.c_str()));
     38   TEST_AND_RETURN_FALSE(size > 0);
     39   // The requested size is within the limits of the file.
     40   TEST_AND_RETURN_FALSE(static_cast<off_t>(size) <=
     41                         utils::FileSize(path.c_str()));
     42   return true;
     43 }
     44 
     45 bool PartitionConfig::OpenFilesystem() {
     46   if (path.empty())
     47     return true;
     48   fs_interface.reset();
     49   if (diff_utils::IsExtFilesystem(path)) {
     50     fs_interface = Ext2Filesystem::CreateFromFile(path);
     51     // TODO(deymo): The delta generator algorithm doesn't support a block size
     52     // different than 4 KiB. Remove this check once that's fixed. b/26972455
     53     if (fs_interface) {
     54       TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
     55       return true;
     56     }
     57   }
     58 
     59   if (!mapfile_path.empty()) {
     60     fs_interface = MapfileFilesystem::CreateFromFile(path, mapfile_path);
     61     if (fs_interface) {
     62       TEST_AND_RETURN_FALSE(fs_interface->GetBlockSize() == kBlockSize);
     63       return true;
     64     }
     65   }
     66 
     67   // Fall back to a RAW filesystem.
     68   TEST_AND_RETURN_FALSE(size % kBlockSize == 0);
     69   fs_interface = RawFilesystem::Create(
     70       "<" + name + "-partition>", kBlockSize, size / kBlockSize);
     71   return true;
     72 }
     73 
     74 bool ImageConfig::ValidateIsEmpty() const {
     75   TEST_AND_RETURN_FALSE(ImageInfoIsEmpty());
     76   return partitions.empty();
     77 }
     78 
     79 bool ImageConfig::LoadImageSize() {
     80   for (PartitionConfig& part : partitions) {
     81     if (part.path.empty())
     82       continue;
     83     part.size = utils::FileSize(part.path);
     84   }
     85   return true;
     86 }
     87 
     88 bool ImageConfig::LoadPostInstallConfig(const brillo::KeyValueStore& store) {
     89   bool found_postinstall = false;
     90   for (PartitionConfig& part : partitions) {
     91     bool run_postinstall;
     92     if (!store.GetBoolean("RUN_POSTINSTALL_" + part.name, &run_postinstall) ||
     93         !run_postinstall)
     94       continue;
     95     found_postinstall = true;
     96     part.postinstall.run = true;
     97     store.GetString("POSTINSTALL_PATH_" + part.name, &part.postinstall.path);
     98     store.GetString("FILESYSTEM_TYPE_" + part.name,
     99                     &part.postinstall.filesystem_type);
    100     store.GetBoolean("POSTINSTALL_OPTIONAL_" + part.name,
    101                      &part.postinstall.optional);
    102   }
    103   if (!found_postinstall) {
    104     LOG(ERROR) << "No valid postinstall config found.";
    105     return false;
    106   }
    107   return true;
    108 }
    109 
    110 bool ImageConfig::ImageInfoIsEmpty() const {
    111   return image_info.board().empty()
    112     && image_info.key().empty()
    113     && image_info.channel().empty()
    114     && image_info.version().empty()
    115     && image_info.build_channel().empty()
    116     && image_info.build_version().empty();
    117 }
    118 
    119 PayloadVersion::PayloadVersion(uint64_t major_version, uint32_t minor_version) {
    120   major = major_version;
    121   minor = minor_version;
    122 }
    123 
    124 bool PayloadVersion::Validate() const {
    125   TEST_AND_RETURN_FALSE(major == kChromeOSMajorPayloadVersion ||
    126                         major == kBrilloMajorPayloadVersion);
    127   TEST_AND_RETURN_FALSE(minor == kFullPayloadMinorVersion ||
    128                         minor == kInPlaceMinorPayloadVersion ||
    129                         minor == kSourceMinorPayloadVersion ||
    130                         minor == kOpSrcHashMinorPayloadVersion ||
    131                         minor == kBrotliBsdiffMinorPayloadVersion ||
    132                         minor == kPuffdiffMinorPayloadVersion);
    133   return true;
    134 }
    135 
    136 bool PayloadVersion::OperationAllowed(InstallOperation_Type operation) const {
    137   switch (operation) {
    138     // Full operations:
    139     case InstallOperation::REPLACE:
    140     case InstallOperation::REPLACE_BZ:
    141       // These operations were included in the original payload format.
    142       return true;
    143 
    144     case InstallOperation::REPLACE_XZ:
    145       // These operations are included in the major version used in Brillo, but
    146       // can also be used with minor version 3 or newer.
    147       return major == kBrilloMajorPayloadVersion ||
    148              minor >= kOpSrcHashMinorPayloadVersion;
    149 
    150     case InstallOperation::ZERO:
    151     case InstallOperation::DISCARD:
    152       // The implementation of these operations had a bug in earlier versions
    153       // that prevents them from being used in any payload. We will enable
    154       // them for delta payloads for now.
    155       return minor >= kBrotliBsdiffMinorPayloadVersion;
    156 
    157     // Delta operations:
    158     case InstallOperation::MOVE:
    159     case InstallOperation::BSDIFF:
    160       // MOVE and BSDIFF were replaced by SOURCE_COPY and SOURCE_BSDIFF and
    161       // should not be used in newer delta versions, since the idempotent checks
    162       // were removed.
    163       return minor == kInPlaceMinorPayloadVersion;
    164 
    165     case InstallOperation::SOURCE_COPY:
    166     case InstallOperation::SOURCE_BSDIFF:
    167       return minor >= kSourceMinorPayloadVersion;
    168 
    169     case InstallOperation::BROTLI_BSDIFF:
    170       return minor >= kBrotliBsdiffMinorPayloadVersion;
    171 
    172     case InstallOperation::PUFFDIFF:
    173       return minor >= kPuffdiffMinorPayloadVersion;
    174   }
    175   return false;
    176 }
    177 
    178 bool PayloadVersion::IsDelta() const {
    179   return minor != kFullPayloadMinorVersion;
    180 }
    181 
    182 bool PayloadVersion::InplaceUpdate() const {
    183   return minor == kInPlaceMinorPayloadVersion;
    184 }
    185 
    186 bool PayloadGenerationConfig::Validate() const {
    187   TEST_AND_RETURN_FALSE(version.Validate());
    188   TEST_AND_RETURN_FALSE(version.IsDelta() == is_delta);
    189   if (is_delta) {
    190     for (const PartitionConfig& part : source.partitions) {
    191       if (!part.path.empty()) {
    192         TEST_AND_RETURN_FALSE(part.ValidateExists());
    193         TEST_AND_RETURN_FALSE(part.size % block_size == 0);
    194       }
    195       // Source partition should not have postinstall.
    196       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
    197     }
    198 
    199     // If new_image_info is present, old_image_info must be present.
    200     TEST_AND_RETURN_FALSE(source.ImageInfoIsEmpty() ==
    201                           target.ImageInfoIsEmpty());
    202   } else {
    203     // All the "source" image fields must be empty for full payloads.
    204     TEST_AND_RETURN_FALSE(source.ValidateIsEmpty());
    205   }
    206 
    207   // In all cases, the target image must exists.
    208   for (const PartitionConfig& part : target.partitions) {
    209     TEST_AND_RETURN_FALSE(part.ValidateExists());
    210     TEST_AND_RETURN_FALSE(part.size % block_size == 0);
    211     if (version.minor == kInPlaceMinorPayloadVersion &&
    212         part.name == kLegacyPartitionNameRoot)
    213       TEST_AND_RETURN_FALSE(rootfs_partition_size >= part.size);
    214     if (version.major == kChromeOSMajorPayloadVersion)
    215       TEST_AND_RETURN_FALSE(part.postinstall.IsEmpty());
    216   }
    217 
    218   TEST_AND_RETURN_FALSE(hard_chunk_size == -1 ||
    219                         hard_chunk_size % block_size == 0);
    220   TEST_AND_RETURN_FALSE(soft_chunk_size % block_size == 0);
    221 
    222   TEST_AND_RETURN_FALSE(rootfs_partition_size % block_size == 0);
    223 
    224   return true;
    225 }
    226 
    227 }  // namespace chromeos_update_engine
    228