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 bool WriteToImageFile(const char* file, const LpMetadata& metadata); 33 bool WriteToImageFile(int fd, const LpMetadata& metadata); 34 35 // We use an object to build the image file since it requires that data 36 // pointers be held alive until the sparse file is destroyed. It's easier 37 // to do this when the data pointers are all in one place. 38 class ImageBuilder { 39 public: 40 ImageBuilder(const LpMetadata& metadata, uint32_t block_size, 41 const std::map<std::string, std::string>& images, bool sparsify); 42 43 bool Build(); 44 bool Export(const char* file); 45 bool ExportFiles(const std::string& dir); 46 bool IsValid() const; 47 48 using SparsePtr = std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)>; device_images()49 const std::vector<SparsePtr>& device_images() const { return device_images_; } 50 51 private: 52 bool AddData(sparse_file* file, const std::string& blob, uint64_t sector); 53 bool AddPartitionImage(const LpMetadataPartition& partition, const std::string& file); 54 int OpenImageFile(const std::string& file); 55 bool SectorToBlock(uint64_t sector, uint32_t* block); 56 uint64_t BlockToSector(uint64_t block) const; 57 bool CheckExtentOrdering(); 58 uint64_t ComputePartitionSize(const LpMetadataPartition& partition) const; 59 60 const LpMetadata& metadata_; 61 const LpMetadataGeometry& geometry_; 62 uint32_t block_size_; 63 bool sparsify_; 64 65 std::vector<SparsePtr> device_images_; 66 std::string all_metadata_; 67 std::map<std::string, std::string> images_; 68 std::vector<android::base::unique_fd> temp_fds_; 69 }; 70 71 } // namespace fs_mgr 72 } // namespace android 73