blob: 0209052290ba257b0ccb196d917f789b647ab267 [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
Alex Deymo1b03f9f2015-12-09 00:38:36 -080017#ifndef UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_
18#define UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_
Alex Deymo763e7db2015-08-27 21:08:08 -070019
Yifan Hongdaac7322019-11-07 10:48:26 -080020#include <memory>
Alex Deymo763e7db2015-08-27 21:08:08 -070021#include <string>
22
Alex Deymoaa26f622015-09-16 18:21:27 -070023#include <base/callback.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070024#include <gtest/gtest_prod.h> // for FRIEND_TEST
25
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/boot_control_interface.h"
Yifan Hongdaac7322019-11-07 10:48:26 -080027#include "update_engine/common/dynamic_partition_control_interface.h"
Alex Deymo763e7db2015-08-27 21:08:08 -070028
29namespace chromeos_update_engine {
30
31// The Chrome OS implementation of the BootControlInterface. This interface
32// assumes the partition names and numbers used in Chrome OS devices.
33class BootControlChromeOS : public BootControlInterface {
34 public:
35 BootControlChromeOS() = default;
36 ~BootControlChromeOS() = default;
37
38 // Initialize the BootControl instance loading the constant values. Returns
39 // whether the operation succeeded. In case of failure, normally meaning
40 // some critical failure such as we couldn't determine the slot that we
41 // booted from, the implementation will pretend that there's only one slot and
42 // therefore A/B updates are disabled.
43 bool Init();
44
45 // BootControlInterface overrides.
46 unsigned int GetNumSlots() const override;
47 BootControlInterface::Slot GetCurrentSlot() const override;
48 bool GetPartitionDevice(const std::string& partition_name,
49 BootControlInterface::Slot slot,
50 std::string* device) const override;
51 bool IsSlotBootable(BootControlInterface::Slot slot) const override;
52 bool MarkSlotUnbootable(BootControlInterface::Slot slot) override;
Alex Deymo31d95ac2015-09-17 11:56:18 -070053 bool SetActiveBootSlot(BootControlInterface::Slot slot) override;
Alex Deymoaa26f622015-09-16 18:21:27 -070054 bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override;
Yifan Hong0f9dbb12020-02-24 18:34:49 -080055 bool IsSlotMarkedSuccessful(BootControlInterface::Slot slot) const override;
Yifan Hongdaac7322019-11-07 10:48:26 -080056 DynamicPartitionControlInterface* GetDynamicPartitionControl() override;
Alex Deymo763e7db2015-08-27 21:08:08 -070057
58 private:
59 friend class BootControlChromeOSTest;
60 FRIEND_TEST(BootControlChromeOSTest, SysfsBlockDeviceTest);
61 FRIEND_TEST(BootControlChromeOSTest, GetPartitionNumberTest);
62
63 // Returns the sysfs block device for a root block device. For example,
64 // SysfsBlockDevice("/dev/sda") returns "/sys/block/sda". Returns an empty
65 // string if the input device is not of the "/dev/xyz" form.
66 static std::string SysfsBlockDevice(const std::string& device);
67
68 // Returns true if the root |device| (e.g., "/dev/sdb") is known to be
69 // removable, false otherwise.
70 static bool IsRemovableDevice(const std::string& device);
71
72 // Return the hard-coded partition number used in Chrome OS for the passed
73 // |partition_name| and |slot|. In case of invalid data, returns -1.
74 int GetPartitionNumber(const std::string partition_name,
75 BootControlInterface::Slot slot) const;
76
77 // Cached values for GetNumSlots() and GetCurrentSlot().
78 BootControlInterface::Slot num_slots_{1};
79 BootControlInterface::Slot current_slot_{BootControlInterface::kInvalidSlot};
80
81 // The block device of the disk we booted from, without the partition number.
82 std::string boot_disk_name_;
83
Yifan Hongdaac7322019-11-07 10:48:26 -080084 std::unique_ptr<DynamicPartitionControlInterface> dynamic_partition_control_;
85
Alex Deymo763e7db2015-08-27 21:08:08 -070086 DISALLOW_COPY_AND_ASSIGN(BootControlChromeOS);
87};
88
89} // namespace chromeos_update_engine
90
Alex Deymo1b03f9f2015-12-09 00:38:36 -080091#endif // UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_