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_UTILITY_H
18 #define LIBLP_UTILITY_H
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <limits>
25 #include <string>
26 #include <string_view>
27 
28 #include <android-base/logging.h>
29 #include <android-base/unique_fd.h>
30 
31 #include "liblp/liblp.h"
32 
33 #define LP_TAG "[liblp] "
34 #define LWARN LOG(WARNING) << LP_TAG
35 #define LINFO LOG(INFO) << LP_TAG
36 #define LERROR LOG(ERROR) << LP_TAG
37 #define PWARNING PLOG(WARNING) << LP_TAG
38 #define PERROR PLOG(ERROR) << LP_TAG
39 
40 namespace android {
41 namespace fs_mgr {
42 
43 // Determine the size of a block device (or file). Logs and returns false on
44 // error. After calling this, the position of |fd| may have changed.
45 bool GetDescriptorSize(int fd, uint64_t* size);
46 
47 // Return the offset of the primary or backup geometry.
48 int64_t GetPrimaryGeometryOffset();
49 int64_t GetBackupGeometryOffset();
50 
51 // Return the offset of a primary metadata slot, relative to the start of the
52 // device.
53 int64_t GetPrimaryMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number);
54 
55 // Return the offset of a backup metadata slot, relative to the end of the
56 // device.
57 int64_t GetBackupMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number);
58 
59 // Return the total space at the start of the super partition that must be set
60 // aside from headers/metadata and backups.
61 uint64_t GetTotalMetadataSize(uint32_t metadata_max_size, uint32_t max_slots);
62 
63 // Cross-platform helper for lseek64().
64 int64_t SeekFile64(int fd, int64_t offset, int whence);
65 
66 // Compute a SHA256 hash.
67 void SHA256(const void* data, size_t length, uint8_t out[32]);
68 
69 // Align |base| such that it is evenly divisible by |alignment|, which does not
70 // have to be a power of two. Return false on overflow.
71 template <typename T>
AlignTo(T base,uint32_t alignment,T * out)72 bool AlignTo(T base, uint32_t alignment, T* out) {
73     static_assert(std::numeric_limits<T>::is_integer);
74     static_assert(!std::numeric_limits<T>::is_signed);
75     if (!alignment) {
76         *out = base;
77         return true;
78     }
79     T remainder = base % alignment;
80     if (remainder == 0) {
81         *out = base;
82         return true;
83     }
84     T to_add = alignment - remainder;
85     if (to_add > std::numeric_limits<T>::max() - base) {
86         return false;
87     }
88     *out = base + to_add;
89     return true;
90 }
91 
92 // Update names from C++ strings.
93 bool UpdateBlockDevicePartitionName(LpMetadataBlockDevice* device, const std::string& name);
94 bool UpdatePartitionGroupName(LpMetadataPartitionGroup* group, const std::string& name);
95 bool UpdatePartitionName(LpMetadataPartition* partition, const std::string& name);
96 
97 // Call BLKROSET ioctl on fd so that fd is readonly / read-writable.
98 bool SetBlockReadonly(int fd, bool readonly);
99 
100 ::android::base::unique_fd GetControlFileOrOpen(std::string_view path, int flags);
101 
102 // For Virtual A/B updates, modify |metadata| so that it can be written to |target_slot_number|.
103 bool UpdateMetadataForInPlaceSnapshot(LpMetadata* metadata, uint32_t source_slot_number,
104                                       uint32_t target_slot_number);
105 
106 // Forcefully set metadata header version to 1.0, clearing any incompatible flags and attributes
107 // so that when downgrading to a build with liblp V0, the device still boots.
108 void SetMetadataHeaderV0(LpMetadata* metadata);
109 
110 }  // namespace fs_mgr
111 }  // namespace android
112 
113 #endif  // LIBLP_UTILITY_H
114