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