boot_control_chromeos.h 3.6 KB

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