Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 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 | |
Alex Deymo | 1b03f9f | 2015-12-09 00:38:36 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_ |
| 18 | #define UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_ |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 19 | |
Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 20 | #include <memory> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 23 | #include <base/callback.h> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 24 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 25 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/boot_control_interface.h" |
Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 27 | #include "update_engine/common/dynamic_partition_control_interface.h" |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 28 | |
| 29 | namespace 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. |
| 33 | class 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, |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 50 | bool not_in_payload, |
| 51 | std::string* device, |
| 52 | bool* is_dynamic) const override; |
| 53 | bool GetPartitionDevice(const std::string& partition_name, |
| 54 | BootControlInterface::Slot slot, |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 55 | std::string* device) const override; |
| 56 | bool IsSlotBootable(BootControlInterface::Slot slot) const override; |
| 57 | bool MarkSlotUnbootable(BootControlInterface::Slot slot) override; |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 58 | bool SetActiveBootSlot(BootControlInterface::Slot slot) override; |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 59 | bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override; |
Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 60 | bool IsSlotMarkedSuccessful(BootControlInterface::Slot slot) const override; |
Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 61 | DynamicPartitionControlInterface* GetDynamicPartitionControl() override; |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | friend class BootControlChromeOSTest; |
| 65 | FRIEND_TEST(BootControlChromeOSTest, SysfsBlockDeviceTest); |
| 66 | FRIEND_TEST(BootControlChromeOSTest, GetPartitionNumberTest); |
Amin Hassani | 73733a0 | 2019-03-18 14:20:46 -0700 | [diff] [blame] | 67 | FRIEND_TEST(BootControlChromeOSTest, ParseDlcPartitionNameTest); |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 68 | |
| 69 | // Returns the sysfs block device for a root block device. For example, |
| 70 | // SysfsBlockDevice("/dev/sda") returns "/sys/block/sda". Returns an empty |
| 71 | // string if the input device is not of the "/dev/xyz" form. |
| 72 | static std::string SysfsBlockDevice(const std::string& device); |
| 73 | |
| 74 | // Returns true if the root |device| (e.g., "/dev/sdb") is known to be |
| 75 | // removable, false otherwise. |
| 76 | static bool IsRemovableDevice(const std::string& device); |
| 77 | |
| 78 | // Return the hard-coded partition number used in Chrome OS for the passed |
| 79 | // |partition_name| and |slot|. In case of invalid data, returns -1. |
| 80 | int GetPartitionNumber(const std::string partition_name, |
| 81 | BootControlInterface::Slot slot) const; |
| 82 | |
Amin Hassani | 73733a0 | 2019-03-18 14:20:46 -0700 | [diff] [blame] | 83 | // Extracts DLC module ID and package ID from partition name. The structure of |
| 84 | // the partition name is dlc/<dlc-id>/<dlc-package>. For example: |
Kelvin Zhang | ebd5e25 | 2020-07-22 18:27:06 -0400 | [diff] [blame] | 85 | // dlc/fake-dlc/fake-package |
Amin Hassani | 73733a0 | 2019-03-18 14:20:46 -0700 | [diff] [blame] | 86 | bool ParseDlcPartitionName(const std::string partition_name, |
| 87 | std::string* dlc_id, |
| 88 | std::string* dlc_package) const; |
| 89 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 90 | // Cached values for GetNumSlots() and GetCurrentSlot(). |
| 91 | BootControlInterface::Slot num_slots_{1}; |
| 92 | BootControlInterface::Slot current_slot_{BootControlInterface::kInvalidSlot}; |
| 93 | |
| 94 | // The block device of the disk we booted from, without the partition number. |
| 95 | std::string boot_disk_name_; |
| 96 | |
Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 97 | std::unique_ptr<DynamicPartitionControlInterface> dynamic_partition_control_; |
| 98 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 99 | DISALLOW_COPY_AND_ASSIGN(BootControlChromeOS); |
| 100 | }; |
| 101 | |
| 102 | } // namespace chromeos_update_engine |
| 103 | |
Alex Deymo | 1b03f9f | 2015-12-09 00:38:36 -0800 | [diff] [blame] | 104 | #endif // UPDATE_ENGINE_BOOT_CONTROL_CHROMEOS_H_ |