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 "liblp/partition_opener.h"
18
19 #if defined(__linux__)
20 #include <linux/fs.h>
21 #endif
22 #if !defined(_WIN32)
23 #include <sys/ioctl.h>
24 #endif
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include <android-base/file.h>
29
30 #include "utility.h"
31
32 namespace android {
33 namespace fs_mgr {
34
35 using android::base::unique_fd;
36
37 namespace {
38
GetPartitionAbsolutePath(const std::string & path)39 std::string GetPartitionAbsolutePath(const std::string& path) {
40 if (path[0] == '/') {
41 return path;
42 }
43 return "/dev/block/by-name/" + path;
44 }
45
GetBlockDeviceInfo(const std::string & block_device,BlockDeviceInfo * device_info)46 bool GetBlockDeviceInfo(const std::string& block_device, BlockDeviceInfo* device_info) {
47 #if defined(__linux__)
48 unique_fd fd = GetControlFileOrOpen(block_device.c_str(), O_RDONLY);
49 if (fd < 0) {
50 PERROR << __PRETTY_FUNCTION__ << "open '" << block_device << "' failed";
51 return false;
52 }
53 if (!GetDescriptorSize(fd, &device_info->size)) {
54 return false;
55 }
56 if (ioctl(fd, BLKIOMIN, &device_info->alignment) < 0) {
57 PERROR << __PRETTY_FUNCTION__ << "BLKIOMIN failed on " << block_device;
58 return false;
59 }
60
61 int alignment_offset;
62 if (ioctl(fd, BLKALIGNOFF, &alignment_offset) < 0) {
63 PERROR << __PRETTY_FUNCTION__ << "BLKALIGNOFF failed on " << block_device;
64 return false;
65 }
66 int logical_block_size;
67 if (ioctl(fd, BLKSSZGET, &logical_block_size) < 0) {
68 PERROR << __PRETTY_FUNCTION__ << "BLKSSZGET failed on " << block_device;
69 return false;
70 }
71
72 device_info->alignment_offset = static_cast<uint32_t>(alignment_offset);
73 device_info->logical_block_size = static_cast<uint32_t>(logical_block_size);
74 device_info->partition_name = android::base::Basename(block_device);
75 return true;
76 #else
77 (void)block_device;
78 (void)device_info;
79 LERROR << __PRETTY_FUNCTION__ << ": Not supported on this operating system.";
80 return false;
81 #endif
82 }
83
84 } // namespace
85
Open(const std::string & partition_name,int flags) const86 unique_fd PartitionOpener::Open(const std::string& partition_name, int flags) const {
87 std::string path = GetPartitionAbsolutePath(partition_name);
88 return GetControlFileOrOpen(path.c_str(), flags | O_CLOEXEC);
89 }
90
GetInfo(const std::string & partition_name,BlockDeviceInfo * info) const91 bool PartitionOpener::GetInfo(const std::string& partition_name, BlockDeviceInfo* info) const {
92 std::string path = GetPartitionAbsolutePath(partition_name);
93 return GetBlockDeviceInfo(path, info);
94 }
95
96 } // namespace fs_mgr
97 } // namespace android
98