1 // 2 // Copyright (C) 2019 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 #pragma once 18 19 #include <stdint.h> 20 21 #include <string> 22 #include <vector> 23 24 #include <liblp/builder.h> 25 #include <update_engine/update_metadata.pb.h> 26 27 #include "utility.h" 28 29 namespace android { 30 namespace snapshot { 31 32 // Helper class that modifies a super partition metadata for an update for 33 // Virtual A/B devices. 34 class SnapshotMetadataUpdater { 35 using DeltaArchiveManifest = chromeos_update_engine::DeltaArchiveManifest; 36 using DynamicPartitionMetadata = chromeos_update_engine::DynamicPartitionMetadata; 37 using DynamicPartitionGroup = chromeos_update_engine::DynamicPartitionGroup; 38 using PartitionUpdate = chromeos_update_engine::PartitionUpdate; 39 40 public: 41 // Caller is responsible for ensuring the lifetime of manifest to be longer 42 // than SnapshotMetadataUpdater. 43 SnapshotMetadataUpdater(android::fs_mgr::MetadataBuilder* builder, uint32_t target_slot, 44 const DeltaArchiveManifest& manifest); 45 bool Update() const; 46 47 private: 48 bool RenameGroupSuffix() const; 49 bool ShrinkPartitions() const; 50 bool DeletePartitions() const; 51 bool MovePartitionsToDefault() const; 52 bool ShrinkGroups() const; 53 bool DeleteGroups() const; 54 bool AddGroups() const; 55 bool GrowGroups() const; 56 bool AddPartitions() const; 57 bool GrowPartitions() const; 58 bool MovePartitionsToCorrectGroup() const; 59 60 // Wraps a DynamicPartitionGroup with a slot-suffixed name. Always use 61 // .name instead of ->name() because .name has the slot suffix (e.g. 62 // .name is "group_b" and ->name() is "group".) 63 struct Group { 64 std::string name; 65 const DynamicPartitionGroup* group; 66 const DynamicPartitionGroup* operator->() const { return group; } 67 }; 68 // Wraps a PartitionUpdate with a slot-suffixed name / group name. Always use 69 // .name instead of ->partition_name() because .name has the slot suffix (e.g. 70 // .name is "system_b" and ->partition_name() is "system".) 71 struct Partition { 72 std::string name; 73 std::string group_name; 74 const PartitionUpdate* partition; 75 const PartitionUpdate* operator->() const { return partition; } 76 }; 77 78 android::fs_mgr::MetadataBuilder* const builder_; 79 const std::string target_suffix_; 80 std::vector<Group> groups_; 81 std::vector<Partition> partitions_; 82 bool partial_update_{false}; 83 }; 84 85 } // namespace snapshot 86 } // namespace android 87