blob: 07ce281043c00f68de51b6f901558435f7b149b0 [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
Yifan Hong2c62c132019-10-24 14:53:40 -070026#include <libsnapshot/auto_device.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070027#include <libsnapshot/snapshot.h>
28
Yifan Hong537802d2018-08-15 13:15:42 -070029namespace chromeos_update_engine {
30
31class DynamicPartitionControlAndroid : public DynamicPartitionControlInterface {
32 public:
Yifan Hong420db9b2019-07-23 20:50:33 -070033 DynamicPartitionControlAndroid();
Yifan Hong537802d2018-08-15 13:15:42 -070034 ~DynamicPartitionControlAndroid();
Yifan Hong186bb682019-07-23 14:04:39 -070035 FeatureFlag GetDynamicPartitionsFeatureFlag() override;
Yifan Hong413d5722019-07-23 14:21:09 -070036 FeatureFlag GetVirtualAbFeatureFlag() override;
Yifan Hong537802d2018-08-15 13:15:42 -070037 bool MapPartitionOnDeviceMapper(const std::string& super_device,
38 const std::string& target_partition_name,
39 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070040 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070041 std::string* path) override;
Yifan Hong537802d2018-08-15 13:15:42 -070042 void Cleanup() override;
43 bool DeviceExists(const std::string& path) override;
44 android::dm::DmDeviceState GetState(const std::string& name) override;
45 bool GetDmDevicePathByName(const std::string& name,
46 std::string* path) override;
47 std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -070048 const std::string& super_device, uint32_t source_slot) override;
49
Yifan Hongf0f4a912019-09-26 17:51:33 -070050 bool PreparePartitionsForUpdate(uint32_t source_slot,
51 uint32_t target_slot,
52 const DeltaArchiveManifest& manifest,
53 bool update) override;
Yifan Hong012508e2019-07-22 18:30:40 -070054 bool GetDeviceDir(std::string* path) override;
Yifan Hong700d7c12019-07-23 20:49:16 -070055 std::string GetSuperPartitionName(uint32_t slot) override;
Yifan Honga33bca42019-09-03 20:29:45 -070056 bool FinishUpdate() override;
Yifan Hong012508e2019-07-22 18:30:40 -070057
58 protected:
59 // These functions are exposed for testing.
60
61 // Unmap logical partition on device mapper. This is the reverse operation
62 // of MapPartitionOnDeviceMapper.
63 // Returns true if unmapped successfully.
64 virtual bool UnmapPartitionOnDeviceMapper(
65 const std::string& target_partition_name);
66
67 // Retrieve metadata from |super_device| at slot |source_slot|.
68 //
69 // If |target_slot| != kInvalidSlot, before returning the metadata, this
70 // function modifies the metadata so that during updates, the metadata can be
71 // written to |target_slot|. In particular, on retrofit devices, the returned
72 // metadata automatically includes block devices at |target_slot|.
73 //
74 // If |target_slot| == kInvalidSlot, this function returns metadata at
75 // |source_slot| without modifying it. This is the same as
76 // LoadMetadataBuilder(const std::string&, uint32_t).
77 virtual std::unique_ptr<android::fs_mgr::MetadataBuilder> LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -080078 const std::string& super_device,
79 uint32_t source_slot,
Yifan Hong012508e2019-07-22 18:30:40 -070080 uint32_t target_slot);
81
82 // Write metadata |builder| to |super_device| at slot |target_slot|.
83 virtual bool StoreMetadata(const std::string& super_device,
84 android::fs_mgr::MetadataBuilder* builder,
85 uint32_t target_slot);
Yifan Hong537802d2018-08-15 13:15:42 -070086
87 private:
Yifan Hongc049f932019-07-23 15:06:05 -070088 friend class DynamicPartitionControlAndroidTest;
89
Yifan Hong02513dc2019-10-30 11:23:04 -070090 void CleanupInternal();
Yifan Hong8546a712019-03-28 14:42:53 -070091 bool MapPartitionInternal(const std::string& super_device,
92 const std::string& target_partition_name,
93 uint32_t slot,
94 bool force_writable,
95 std::string* path);
Yifan Hong537802d2018-08-15 13:15:42 -070096
Yifan Hong012508e2019-07-22 18:30:40 -070097 // Update |builder| according to |partition_metadata|, assuming the device
98 // does not have Virtual A/B.
Yifan Hong13d41cb2019-09-16 13:18:22 -070099 bool UpdatePartitionMetadata(android::fs_mgr::MetadataBuilder* builder,
100 uint32_t target_slot,
101 const DeltaArchiveManifest& manifest);
Yifan Hong012508e2019-07-22 18:30:40 -0700102
Yifan Hong420db9b2019-07-23 20:50:33 -0700103 // Helper for PreparePartitionsForUpdate. Used for dynamic partitions without
104 // Virtual A/B update.
105 bool PrepareDynamicPartitionsForUpdate(uint32_t source_slot,
106 uint32_t target_slot,
107 const DeltaArchiveManifest& manifest);
108
109 // Helper for PreparePartitionsForUpdate. Used for snapshotted partitions for
110 // Virtual A/B update.
111 bool PrepareSnapshotPartitionsForUpdate(uint32_t source_slot,
112 uint32_t target_slot,
113 const DeltaArchiveManifest& manifest);
114
115 std::set<std::string> mapped_devices_;
Yifan Hongb38e1af2019-10-17 14:59:22 -0700116 const FeatureFlag dynamic_partitions_;
117 const FeatureFlag virtual_ab_;
Yifan Hong420db9b2019-07-23 20:50:33 -0700118 std::unique_ptr<android::snapshot::SnapshotManager> snapshot_;
Yifan Hong2c62c132019-10-24 14:53:40 -0700119 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700120 bool target_supports_snapshot_ = false;
Yifan Hong420db9b2019-07-23 20:50:33 -0700121
Yifan Hong537802d2018-08-15 13:15:42 -0700122 DISALLOW_COPY_AND_ASSIGN(DynamicPartitionControlAndroid);
123};
124
125} // namespace chromeos_update_engine
126
127#endif // UPDATE_ENGINE_DYNAMIC_PARTITION_CONTROL_ANDROID_H_