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 #ifndef LIBLP_LIBLP_H
18 #define LIBLP_LIBLP_H
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include <map>
24 #include <memory>
25 #include <string>
26 
27 #include <android-base/unique_fd.h>
28 
29 #include "metadata_format.h"
30 #include "partition_opener.h"
31 
32 namespace android {
33 namespace fs_mgr {
34 
35 // Helper structure for easily interpreting deserialized metadata, or
36 // re-serializing metadata.
37 struct LpMetadata {
38     LpMetadataGeometry geometry;
39     LpMetadataHeader header;
40     std::vector<LpMetadataPartition> partitions;
41     std::vector<LpMetadataExtent> extents;
42     std::vector<LpMetadataPartitionGroup> groups;
43     std::vector<LpMetadataBlockDevice> block_devices;
44 };
45 
46 // Place an initial partition table on the device. This will overwrite the
47 // existing geometry, and should not be used for normal partition table
48 // updates. False can be returned if the geometry is incompatible with the
49 // block device or an I/O error occurs.
50 bool FlashPartitionTable(const IPartitionOpener& opener, const std::string& super_partition,
51                          const LpMetadata& metadata);
52 
53 // Update the partition table for a given metadata slot number. False is
54 // returned if an error occurs, which can include:
55 //  - Invalid slot number.
56 //  - I/O error.
57 //  - Corrupt or missing metadata geometry on disk.
58 //  - Incompatible geometry.
59 bool UpdatePartitionTable(const IPartitionOpener& opener, const std::string& super_partition,
60                           const LpMetadata& metadata, uint32_t slot_number);
61 
62 // Read logical partition metadata from its predetermined location on a block
63 // device. If readback fails, we also attempt to load from a backup copy.
64 std::unique_ptr<LpMetadata> ReadMetadata(const IPartitionOpener& opener,
65                                          const std::string& super_partition, uint32_t slot_number);
66 
67 // Helper functions that use the default PartitionOpener.
68 bool FlashPartitionTable(const std::string& super_partition, const LpMetadata& metadata);
69 bool UpdatePartitionTable(const std::string& super_partition, const LpMetadata& metadata,
70                           uint32_t slot_number);
71 std::unique_ptr<LpMetadata> ReadMetadata(const std::string& super_partition, uint32_t slot_number);
72 
73 // Returns whether an image is an "empty" image or not. An empty image contains
74 // only metadata. Unlike a flashed block device, there are no reserved bytes or
75 // backup sections, and only one slot is stored (even if multiple slots are
76 // supported). It is a format specifically for storing only metadata.
77 bool IsEmptySuperImage(const std::string& file);
78 
79 // Read/Write logical partition metadata and contents to an image file, for
80 // flashing.
81 bool WriteToImageFile(const std::string& file, const LpMetadata& metadata, uint32_t block_size,
82                       const std::map<std::string, std::string>& images, bool sparsify);
83 
84 // Read/Write logical partition metadata to an image file, for producing a
85 // super_empty.img (for fastboot wipe-super/update-super) or for diagnostics.
86 bool WriteToImageFile(const std::string& file, const LpMetadata& metadata);
87 bool WriteToImageFile(android::base::borrowed_fd fd, const LpMetadata& metadata);
88 std::unique_ptr<LpMetadata> ReadFromImageFile(const std::string& image_file);
89 std::unique_ptr<LpMetadata> ReadFromImageBlob(const void* data, size_t bytes);
90 
91 // Similar to WriteToSparseFile, this will generate an image that can be
92 // flashed to a device directly. However unlike WriteToSparseFile, it
93 // is intended for retrofit devices, and will generate one sparse file per
94 // block device (each named super_<name>.img) and placed in the specified
95 // output folder.
96 bool WriteSplitImageFiles(const std::string& output_dir, const LpMetadata& metadata,
97                           uint32_t block_size, const std::map<std::string, std::string>& images,
98                           bool sparsify);
99 
100 // Helper to extract safe C++ strings from partition info.
101 std::string GetPartitionName(const LpMetadataPartition& partition);
102 std::string GetPartitionGroupName(const LpMetadataPartitionGroup& group);
103 std::string GetBlockDevicePartitionName(const LpMetadataBlockDevice& block_device);
104 
105 // Return the block device that houses the super partition metadata; returns
106 // null on failure.
107 const LpMetadataBlockDevice* GetMetadataSuperBlockDevice(const LpMetadata& metadata);
108 
109 // Return the total size of all partitions comprising the super partition.
110 uint64_t GetTotalSuperPartitionSize(const LpMetadata& metadata);
111 
112 // Get the list of block device names required by the given metadata.
113 std::vector<std::string> GetBlockDevicePartitionNames(const LpMetadata& metadata);
114 
115 // Slot suffix helpers.
116 uint32_t SlotNumberForSlotSuffix(const std::string& suffix);
117 std::string SlotSuffixForSlotNumber(uint32_t slot_number);
118 std::string GetPartitionSlotSuffix(const std::string& partition_name);
119 
120 // Helpers for common functions.
121 const LpMetadataPartition* FindPartition(const LpMetadata& metadata, const std::string& name);
122 uint64_t GetPartitionSize(const LpMetadata& metadata, const LpMetadataPartition& partition);
123 
124 }  // namespace fs_mgr
125 }  // namespace android
126 
127 #endif  // LIBLP_LIBLP_H
128