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 #include <android-base/strings.h>
30
31 #include "utility.h"
32
33 namespace android {
34 namespace fs_mgr {
35
36 using android::base::unique_fd;
37
38 namespace {
39
GetPartitionAbsolutePath(const std::string & path)40 std::string GetPartitionAbsolutePath(const std::string& path) {
41 if (android::base::StartsWith(path, "/")) {
42 return path;
43 }
44
45 auto by_name = "/dev/block/by-name/" + path;
46 if (access(by_name.c_str(), F_OK) != 0) {
47 // If the by-name symlink doesn't exist, as a special case we allow
48 // certain devices to be used as partition names. This can happen if a
49 // Dynamic System Update is installed to an sdcard, which won't be in
50 // the boot device list.
51 //
52 // mmcblk* is allowed because most devices in /dev/block are not valid for
53 // storing fiemaps.
54 if (android::base::StartsWith(path, "mmcblk")) {
55 return "/dev/block/" + path;
56 }
57 }
58 return by_name;
59 }
60
GetBlockDeviceInfo(const std::string & block_device,BlockDeviceInfo * device_info)61 bool GetBlockDeviceInfo(const std::string& block_device, BlockDeviceInfo* device_info) {
62 #if defined(__linux__)
63 unique_fd fd = GetControlFileOrOpen(block_device.c_str(), O_RDONLY);
64 if (fd < 0) {
65 PERROR << __PRETTY_FUNCTION__ << "open '" << block_device << "' failed";
66 return false;
67 }
68 if (!GetDescriptorSize(fd, &device_info->size)) {
69 return false;
70 }
71 if (ioctl(fd, BLKIOMIN, &device_info->alignment) < 0) {
72 PERROR << __PRETTY_FUNCTION__ << "BLKIOMIN failed on " << block_device;
73 return false;
74 }
75
76 int alignment_offset;
77 if (ioctl(fd, BLKALIGNOFF, &alignment_offset) < 0) {
78 PERROR << __PRETTY_FUNCTION__ << "BLKALIGNOFF failed on " << block_device;
79 return false;
80 }
81 // The kernel can return -1 here when misaligned devices are stacked (i.e.
82 // device-mapper).
83 if (alignment_offset == -1) {
84 alignment_offset = 0;
85 }
86
87 int logical_block_size;
88 if (ioctl(fd, BLKSSZGET, &logical_block_size) < 0) {
89 PERROR << __PRETTY_FUNCTION__ << "BLKSSZGET failed on " << block_device;
90 return false;
91 }
92
93 device_info->alignment_offset = static_cast<uint32_t>(alignment_offset);
94 device_info->logical_block_size = static_cast<uint32_t>(logical_block_size);
95 device_info->partition_name = android::base::Basename(block_device);
96 return true;
97 #else
98 (void)block_device;
99 (void)device_info;
100 LERROR << __PRETTY_FUNCTION__ << ": Not supported on this operating system.";
101 return false;
102 #endif
103 }
104
105 } // namespace
106
Open(const std::string & partition_name,int flags) const107 unique_fd PartitionOpener::Open(const std::string& partition_name, int flags) const {
108 std::string path = GetPartitionAbsolutePath(partition_name);
109 return GetControlFileOrOpen(path.c_str(), flags | O_CLOEXEC);
110 }
111
GetInfo(const std::string & partition_name,BlockDeviceInfo * info) const112 bool PartitionOpener::GetInfo(const std::string& partition_name, BlockDeviceInfo* info) const {
113 std::string path = GetPartitionAbsolutePath(partition_name);
114 return GetBlockDeviceInfo(path, info);
115 }
116
GetDeviceString(const std::string & partition_name) const117 std::string PartitionOpener::GetDeviceString(const std::string& partition_name) const {
118 return GetPartitionAbsolutePath(partition_name);
119 }
120
121 } // namespace fs_mgr
122 } // namespace android
123