Home | History | Annotate | Download | only in update_engine
      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 #ifndef UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_
     18 #define UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_
     19 
     20 #include <string>
     21 
     22 #include <base/callback.h>
     23 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
     24 
     25 #include "update_engine/common/boot_control_interface.h"
     26 
     27 namespace chromeos_update_engine {
     28 
     29 // The Chrome OS implementation of the BootControlInterface. This interface
     30 // assumes the partition names and numbers used in Chrome OS devices.
     31 class BootControlChromeOS : public BootControlInterface {
     32  public:
     33   BootControlChromeOS() = default;
     34   ~BootControlChromeOS() = default;
     35 
     36   // Initialize the BootControl instance loading the constant values. Returns
     37   // whether the operation succeeded. In case of failure, normally meaning
     38   // some critical failure such as we couldn't determine the slot that we
     39   // booted from, the implementation will pretend that there's only one slot and
     40   // therefore A/B updates are disabled.
     41   bool Init();
     42 
     43   // BootControlInterface overrides.
     44   unsigned int GetNumSlots() const override;
     45   BootControlInterface::Slot GetCurrentSlot() const override;
     46   bool GetPartitionDevice(const std::string& partition_name,
     47                           BootControlInterface::Slot slot,
     48                           std::string* device) const override;
     49   bool IsSlotBootable(BootControlInterface::Slot slot) const override;
     50   bool MarkSlotUnbootable(BootControlInterface::Slot slot) override;
     51   bool SetActiveBootSlot(BootControlInterface::Slot slot) override;
     52   bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override;
     53 
     54  private:
     55   friend class BootControlChromeOSTest;
     56   FRIEND_TEST(BootControlChromeOSTest, SysfsBlockDeviceTest);
     57   FRIEND_TEST(BootControlChromeOSTest, GetPartitionNumberTest);
     58 
     59   // Returns the sysfs block device for a root block device. For example,
     60   // SysfsBlockDevice("/dev/sda") returns "/sys/block/sda". Returns an empty
     61   // string if the input device is not of the "/dev/xyz" form.
     62   static std::string SysfsBlockDevice(const std::string& device);
     63 
     64   // Returns true if the root |device| (e.g., "/dev/sdb") is known to be
     65   // removable, false otherwise.
     66   static bool IsRemovableDevice(const std::string& device);
     67 
     68   // Return the hard-coded partition number used in Chrome OS for the passed
     69   // |partition_name| and |slot|. In case of invalid data, returns -1.
     70   int GetPartitionNumber(const std::string partition_name,
     71                          BootControlInterface::Slot slot) const;
     72 
     73   // Cached values for GetNumSlots() and GetCurrentSlot().
     74   BootControlInterface::Slot num_slots_{1};
     75   BootControlInterface::Slot current_slot_{BootControlInterface::kInvalidSlot};
     76 
     77   // The block device of the disk we booted from, without the partition number.
     78   std::string boot_disk_name_;
     79 
     80   DISALLOW_COPY_AND_ASSIGN(BootControlChromeOS);
     81 };
     82 
     83 }  // namespace chromeos_update_engine
     84 
     85 #endif  // UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_
     86