1 /* 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 #include <stdint.h> 18 #include <map> 19 #include <memory> 20 #include <string> 21 22 #include <android-base/unique_fd.h> 23 #include <liblp/liblp.h> 24 #include <sparse/sparse.h> 25 26 namespace android { 27 namespace fs_mgr { 28 29 // Helper function to serialize geometry and metadata to a normal file, for 30 // flashing or debugging. 31 std::unique_ptr<LpMetadata> ReadFromImageFile(int fd); 32 33 // We use an object to build the image file since it requires that data 34 // pointers be held alive until the sparse file is destroyed. It's easier 35 // to do this when the data pointers are all in one place. 36 class ImageBuilder { 37 public: 38 ImageBuilder(const LpMetadata& metadata, uint32_t block_size, 39 const std::map<std::string, std::string>& images, bool sparsify); 40 41 bool Build(); 42 bool Export(const std::string& file); 43 bool ExportFiles(const std::string& dir); 44 bool IsValid() const; 45 46 using SparsePtr = std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)>; device_images()47 const std::vector<SparsePtr>& device_images() const { return device_images_; } 48 49 private: 50 bool AddData(sparse_file* file, const std::string& blob, uint64_t sector); 51 bool AddPartitionImage(const LpMetadataPartition& partition, const std::string& file); 52 int OpenImageFile(const std::string& file); 53 bool SectorToBlock(uint64_t sector, uint32_t* block); 54 uint64_t BlockToSector(uint64_t block) const; 55 bool CheckExtentOrdering(); 56 uint64_t ComputePartitionSize(const LpMetadataPartition& partition) const; 57 58 const LpMetadata& metadata_; 59 const LpMetadataGeometry& geometry_; 60 uint32_t block_size_; 61 bool sparsify_; 62 63 std::vector<SparsePtr> device_images_; 64 std::string all_metadata_; 65 std::map<std::string, std::string> images_; 66 std::vector<android::base::unique_fd> temp_fds_; 67 }; 68 69 } // namespace fs_mgr 70 } // namespace android 71