blob: 2bfbcb151fa3dbce9c73625ac90278aa21ac47ed [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
Yifan Hong537802d2018-08-15 13:15:42 -070020#include <memory>
21#include <set>
22#include <string>
23
Yifan Hong3a1a5612019-11-05 16:34:32 -080024#include <base/files/file_util.h>
Yifan Hong2c62c132019-10-24 14:53:40 -070025#include <libsnapshot/auto_device.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070026#include <libsnapshot/snapshot.h>
27
Yifan Hong15726b92019-11-05 19:06:48 -080028#include "update_engine/common/dynamic_partition_control_interface.h"
29
Yifan Hong537802d2018-08-15 13:15:42 -070030namespace chromeos_update_engine {
31
32class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
33 public:
Yifan Hong420db9b2019-07-23 20:50:33 -070034 DynamicPartitionControlAndroid();
Yifan Hong537802d2018-08-15 13:15:42 -070035 ~DynamicPartitionControlAndroid();
Yifan Hong186bb682019-07-23 14:04:39 -070036 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong413d5722019-07-23 14:21:09 -070037 FeatureFlag GetVirtualAbFeatureFlag() override;
Alessio Balsini14980e22019-11-26 11:46:06 +000038 bool ShouldSkipOperation(const InstallOperation& operation) override;
Yifan Hong537802d2018-08-15 13:15:42 -070039 void Cleanup() override;
Yifan Hong012508e2019-07-22 18:30:40 -070040
Yifan Hongf0f4a912019-09-26 17:51:33 -070041 bool PreparePartitionsForUpdate(uint32_t source_slot,
42 uint32_t target_slot,
43 const DeltaArchiveManifest& manifest,
44 bool update) override;
Yifan Honga33bca42019-09-03 20:29:45 -070045 bool FinishUpdate() override;
Yifan Hong012508e2019-07-22 18:30:40 -070046
Yifan Hong3a1a5612019-11-05 16:34:32 -080047 // Return the device for partition |partition_name| at slot |slot|.
48 // |current_slot| should be set to the current active slot.
49 // Note: this function is only used by BootControl*::GetPartitionDevice.
50 // Other callers should prefer BootControl*::GetPartitionDevice over
51 // BootControl*::GetDynamicPartitionControl()->GetPartitionDevice().
52 bool GetPartitionDevice(const std::string& partition_name,
53 uint32_t slot,
54 uint32_t current_slot,
55 std::string* device);
56
Yifan Hong012508e2019-07-22 18:30:40 -070057 protected:
58 // These functions are exposed for testing.
59
60 // Unmap logical partition on device mapper. This is the reverse operation
61 // of MapPartitionOnDeviceMapper.
62 // Returns true if unmapped successfully.
63 virtual bool UnmapPartitionOnDeviceMapper(
64 const std::string& target_partition_name);
65
66 // Retrieve metadata from |super_device| at slot |source_slot|.
67 //
68 // If |target_slot| != kInvalidSlot, before returning the metadata, this
69 // function modifies the metadata so that during updates, the metadata can be
70 // written to |target_slot|. In particular, on retrofit devices, the returned
71 // metadata automatically includes block devices at |target_slot|.
72 //
73 // If |target_slot| == kInvalidSlot, this function returns metadata at
74 // |source_slot| without modifying it. This is the same as
75 // LoadMetadataBuilder(const std::string&, uint32_t).
76 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -080077 const std::string& super_device,
78 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -070079 uint32_t target_slot);
80
81 // Write metadata |builder| to |super_device| at slot |target_slot|.
82 virtual bool StoreMetadata(const std::string& super_device,
83 android::fs_mgr::MetadataBuilder* builder,
84 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -070085
Yifan Hong3a1a5612019-11-05 16:34:32 -080086 // Map logical partition on device-mapper.
87 // |super_device| is the device path of the physical partition ("super").
88 // |target_partition_name| is the identifier used in metadata; for example,
89 // "vendor_a"
90 // |slot| is the selected slot to mount; for example, 0 for "_a".
91 // Returns true if mapped successfully; if so, |path| is set to the device
92 // path of the mapped logical partition.
93 virtual bool MapPartitionOnDeviceMapper(
94 const std::string& super_device,
95 const std::string& target_partition_name,
96 uint32_t slot,
97 bool force_writable,
98 std::string* path);
99
100 // Return true if a static partition exists at device path |path|.
101 virtual bool DeviceExists(const std::string& path);
102
103 // Returns the current state of the underlying device mapper device
104 // with given name.
105 // One of INVALID, SUSPENDED or ACTIVE.
106 virtual android::dm::DmDeviceState GetState(const std::string& name);
107
108 // Returns the path to the device mapper device node in '/dev' corresponding
109 // to 'name'. If the device does not exist, false is returned, and the path
110 // parameter is not set.
111 virtual bool GetDmDevicePathByName(const std::string& name,
112 std::string* path);
113
114 // Retrieve metadata from |super_device| at slot |source_slot|.
115 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
116 const std::string& super_device, uint32_t source_slot);
117
118 // Return a possible location for devices listed by name.
119 virtual bool GetDeviceDir(std::string* path);
120
121 // Return the name of the super partition (which stores super partition
122 // metadata) for a given slot.
123 virtual std::string GetSuperPartitionName(uint32_t slot);
124
Yifan Hong537802d2018-08-15 13:15:42 -0700125 private:
Yifan Hongc049f932019-07-23 15:06:05 -0700126 friend class DynamicPartitionControlAndroidTest;
127
Yifan Hong02513dc2019-10-30 11:23:04 -0700128 void CleanupInternal();
Yifan Hong8546a712019-03-28 14:42:53 -0700129 bool MapPartitionInternal(const std::string& super_device,
130 const std::string& target_partition_name,
131 uint32_t slot,
132 bool force_writable,
133 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -0700134
Yifan Hong012508e2019-07-22 18:30:40 -0700135 // Update |builder| according to |partition_metadata|, assuming the device
136 // does not have Virtual A/B.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700137 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
138 uint32_t target_slot,
139 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700140
Yifan Hong420db9b2019-07-23 20:50:33 -0700141 // Helper for PreparePartitionsForUpdate. Used for dynamic partitions without
142 // Virtual A/B update.
143 bool PrepareDynamicPartitionsForUpdate(uint32_t source_slot,
144 uint32_t target_slot,
145 const DeltaArchiveManifest& manifest);
146
147 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for
148 // Virtual A/B update.
149 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
150 uint32_t target_slot,
151 const DeltaArchiveManifest& manifest);
152
Yifan Hong3a1a5612019-11-05 16:34:32 -0800153 enum class DynamicPartitionDeviceStatus {
154 SUCCESS,
155 ERROR,
156 TRY_STATIC,
157 };
158
159 // Return SUCCESS and path in |device| if partition is dynamic.
160 // Return ERROR if any error.
161 // Return TRY_STATIC if caller should resolve the partition as a static
162 // partition instead.
163 DynamicPartitionDeviceStatus GetDynamicPartitionDevice(
164 const base::FilePath& device_dir,
165 const std::string& partition_name_suffix,
166 uint32_t slot,
167 uint32_t current_slot,
168 std::string* device);
169
170 // Return true if |partition_name_suffix| is a block device of
171 // super partition metadata slot |slot|.
172 bool IsSuperBlockDevice(const base::FilePath& device_dir,
173 uint32_t current_slot,
174 const std::string& partition_name_suffix);
175
Yifan Hong420db9b2019-07-23 20:50:33 -0700176 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700177 const FeatureFlag dynamic_partitions_;
178 const FeatureFlag virtual_ab_;
Yifan Hong420db9b2019-07-23 20:50:33 -0700179 std::unique_ptr<android::snapshot::SnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700180 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700181 bool target_supports_snapshot_ = false;
Yifan Hong3a1a5612019-11-05 16:34:32 -0800182 // Whether the target partitions should be loaded as dynamic partitions. Set
183 // by PreparePartitionsForUpdate() per each update.
184 bool is_target_dynamic_ = false;
Yifan Hong420db9b2019-07-23 20:50:33 -0700185
Yifan Hong537802d2018-08-15 13:15:42 -0700186 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
187};
188
189} // namespace chromeos_update_engine
190
191#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_