• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/logging.h>
25 #include <android-base/unique_fd.h>
26 
27 #include "liblp/liblp.h"
28 
29 #define LP_TAG "[liblp]"
30 #define LWARN LOG(WARNING) << LP_TAG
31 #define LINFO LOG(INFO) << LP_TAG
32 #define LERROR LOG(ERROR) << LP_TAG
33 #define PWARNING PLOG(WARNING) << LP_TAG
34 #define PERROR PLOG(ERROR) << LP_TAG
35 
36 namespace android {
37 namespace fs_mgr {
38 
39 // Determine the size of a block device (or file). Logs and returns false on
40 // error. After calling this, the position of |fd| may have changed.
41 bool GetDescriptorSize(int fd, uint64_t* size);
42 
43 // Return the offset of the primary or backup geometry.
44 int64_t GetPrimaryGeometryOffset();
45 int64_t GetBackupGeometryOffset();
46 
47 // Return the offset of a primary metadata slot, relative to the start of the
48 // device.
49 int64_t GetPrimaryMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number);
50 
51 // Return the offset of a backup metadata slot, relative to the end of the
52 // device.
53 int64_t GetBackupMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number);
54 
55 // Return the total space at the start of the super partition that must be set
56 // aside from headers/metadata and backups.
57 uint64_t GetTotalMetadataSize(uint32_t metadata_max_size, uint32_t max_slots);
58 
59 // Cross-platform helper for lseek64().
60 int64_t SeekFile64(int fd, int64_t offset, int whence);
61 
62 // Compute a SHA256 hash.
63 void SHA256(const void* data, size_t length, uint8_t out[32]);
64 
65 // Align |base| such that it is evenly divisible by |alignment|, which does not
66 // have to be a power of two.
AlignTo(uint64_t base,uint32_t alignment)67 constexpr uint64_t AlignTo(uint64_t base, uint32_t alignment) {
68     if (!alignment) {
69         return base;
70     }
71     uint64_t remainder = base % alignment;
72     if (remainder == 0) {
73         return base;
74     }
75     return base + (alignment - remainder);
76 }
77 
78 // Same as the above |AlignTo|, except that |base| is only aligned when added to
79 // |alignment_offset|.
AlignTo(uint64_t base,uint32_t alignment,uint32_t alignment_offset)80 constexpr uint64_t AlignTo(uint64_t base, uint32_t alignment, uint32_t alignment_offset) {
81     uint64_t aligned = AlignTo(base, alignment) + alignment_offset;
82     if (aligned - alignment >= base) {
83         // We overaligned (base < alignment_offset).
84         return aligned - alignment;
85     }
86     return aligned;
87 }
88 
89 // Update names from C++ strings.
90 bool UpdateBlockDevicePartitionName(LpMetadataBlockDevice* device, const std::string& name);
91 bool UpdatePartitionGroupName(LpMetadataPartitionGroup* group, const std::string& name);
92 
93 // Call BLKROSET ioctl on fd so that fd is readonly / read-writable.
94 bool SetBlockReadonly(int fd, bool readonly);
95 
96 ::android::base::unique_fd GetControlFileOrOpen(const char* path, int flags);
97 
98 }  // namespace fs_mgr
99 }  // namespace android
100 
101 #endif  // LIBLP_UTILITY_H
102