1 // 2 // Copyright (C) 2023 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 <memory> 20 #include <string> 21 #include <unordered_map> 22 #include <unordered_set> 23 24 #include <android-base/unique_fd.h> 25 #include <liblp/liblp.h> 26 #include <liblp/super_layout_builder.h> 27 28 #include "util.h" 29 30 class SuperFlashHelper final { 31 public: 32 explicit SuperFlashHelper(const ImageSource& source); 33 34 bool Open(android::base::borrowed_fd fd); 35 bool IncludeInSuper(const std::string& partition); 36 bool AddPartition(const std::string& partition, const std::string& image_name, bool optional); 37 38 // Note: the SparsePtr if non-null should not outlive SuperFlashHelper, since 39 // it depends on open fds and data pointers. 40 SparsePtr GetSparseLayout(); 41 WillFlash(const std::string & partition)42 bool WillFlash(const std::string& partition) const { 43 return will_flash_.find(partition) != will_flash_.end(); 44 } 45 46 private: 47 const ImageSource& source_; 48 android::fs_mgr::SuperLayoutBuilder builder_; 49 std::unique_ptr<android::fs_mgr::LpMetadata> base_metadata_; 50 std::vector<android::fs_mgr::SuperImageExtent> extents_; 51 52 // Cache open image fds. This keeps them alive while we flash the sparse 53 // file. 54 std::unordered_map<std::string, android::base::unique_fd> image_fds_; 55 std::unordered_set<std::string> will_flash_; 56 }; 57