blob: e0859ed9430ae24ac757a5e3eae6ca9390c921d0 [file] [log] [blame]
Yifan Hong537802d2018-08-15 13:15:42 -07001//
2// Copyright (C) 2018 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_DYNAMIC_PARTITION_CONTROL_ANDROID_H_
18#define UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_
19
20#include "update_engine/dynamic_partition_control_interface.h"
21
22#include <memory>
23#include <set>
24#include <string>
25
26namespace chromeos_update_engine {
27
28class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
29 public:
30 DynamicPartitionControlAndroid() = default;
31 ~DynamicPartitionControlAndroid();
32 bool IsDynamicPartitionsEnabled() override;
Yifan Hong6e38b352018-11-19 14:12:37 -080033 bool IsDynamicPartitionsRetrofit() override;
Yifan Hong537802d2018-08-15 13:15:42 -070034 bool MapPartitionOnDeviceMapper(const std::string& super_device,
35 const std::string& target_partition_name,
36 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070037 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070038 std::string* path) override;
Yifan Hong537802d2018-08-15 13:15:42 -070039 void Cleanup() override;
40 bool DeviceExists(const std::string& path) override;
41 android::dm::DmDeviceState GetState(const std::string& name) override;
42 bool GetDmDevicePathByName(const std::string& name,
43 std::string* path) override;
44 std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -070045 const std::string& super_device, uint32_t source_slot) override;
46
47 bool PreparePartitionsForUpdate(uint32_t source_slot,
48 uint32_t target_slot,
49 const BootControlInterface::PartitionMetadata&
50 partition_metadata) override;
51 bool GetDeviceDir(std::string* path) override;
52
53 protected:
54 // These functions are exposed for testing.
55
56 // Unmap logical partition on device mapper. This is the reverse operation
57 // of MapPartitionOnDeviceMapper.
58 // Returns true if unmapped successfully.
59 virtual bool UnmapPartitionOnDeviceMapper(
60 const std::string& target_partition_name);
61
62 // Retrieve metadata from |super_device| at slot |source_slot|.
63 //
64 // If |target_slot| != kInvalidSlot, before returning the metadata, this
65 // function modifies the metadata so that during updates, the metadata can be
66 // written to |target_slot|. In particular, on retrofit devices, the returned
67 // metadata automatically includes block devices at |target_slot|.
68 //
69 // If |target_slot| == kInvalidSlot, this function returns metadata at
70 // |source_slot| without modifying it. This is the same as
71 // LoadMetadataBuilder(const std::string&, uint32_t).
72 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -080073 const std::string& super_device,
74 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -070075 uint32_t target_slot);
76
77 // Write metadata |builder| to |super_device| at slot |target_slot|.
78 virtual bool StoreMetadata(const std::string& super_device,
79 android::fs_mgr::MetadataBuilder* builder,
80 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -070081
82 private:
83 std::set<std::string> mapped_devices_;
84
85 void CleanupInternal(bool wait);
Yifan Hong8546a712019-03-28 14:42:53 -070086 bool MapPartitionInternal(const std::string& super_device,
87 const std::string& target_partition_name,
88 uint32_t slot,
89 bool force_writable,
90 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -070091
Yifan Hong012508e2019-07-22 18:30:40 -070092 // Update |builder| according to |partition_metadata|, assuming the device
93 // does not have Virtual A/B.
94 bool UpdatePartitionMetadata(
95 android::fs_mgr::MetadataBuilder* builder,
96 uint32_t target_slot,
97 const BootControlInterface::PartitionMetadata& partition_metadata);
98
Yifan Hong537802d2018-08-15 13:15:42 -070099 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
100};
101
102} // namespace chromeos_update_engine
103
104#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_