blob: f3682e9fb754223d7a802bcb3fa682f460752233 [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
20#include <string>
21
Alex Deymoaa26f622015-09-16 18:21:27 -070022#include <base/callback.h>
Alex Deymo763e7db2015-08-27 21:08:08 -070023#include <gtest/gtest_prod.h> // for FRIEND_TEST
24
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/boot_control_interface.h"
Alex Deymo763e7db2015-08-27 21:08:08 -070026
27namespace 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.
31class 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;
Alex Deymo31d95ac2015-09-17 11:56:18 -070051 bool SetActiveBootSlot(BootControlInterface::Slot slot) override;
Alex Deymoaa26f622015-09-16 18:21:27 -070052 bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override;
Tao Bao3406c772019-01-02 15:34:35 -080053 bool InitPartitionMetadata(Slot slot,
54 const PartitionMetadata& partition_metadata,
55 bool update_metadata) override;
Yifan Hong537802d2018-08-15 13:15:42 -070056 void Cleanup() 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
84 DISALLOW_COPY_AND_ASSIGN(BootControlChromeOS);
85};
86
87} // namespace chromeos_update_engine
88
Alex Deymo1b03f9f2015-12-09 00:38:36 -080089#endif // UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_