Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 17 | #include "VoldUtil.h" |
Daniel Lee | 32d2d25 | 2024-08-30 11:09:36 -0700 | [diff] [blame] | 18 | #include "Utils.h" |
| 19 | |
| 20 | #include <libdm/dm.h> |
| 21 | |
| 22 | #include <android-base/file.h> |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <android-base/strings.h> |
| 26 | |
| 27 | using android::base::Basename; |
| 28 | using android::base::Realpath; |
| 29 | using namespace android::dm; |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 30 | |
Tom Cherry | 4c5bde2 | 2019-01-29 14:34:01 -0800 | [diff] [blame] | 31 | android::fs_mgr::Fstab fstab_default; |
Daniel Lee | 32d2d25 | 2024-08-30 11:09:36 -0700 | [diff] [blame] | 32 | |
| 33 | static std::string GetUfsHostControllerSysfsPathOnce() { |
| 34 | android::fs_mgr::FstabEntry* entry = |
| 35 | android::fs_mgr::GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT); |
| 36 | if (entry == nullptr) { |
| 37 | LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT; |
| 38 | return ""; |
| 39 | } |
| 40 | |
| 41 | // Handle symlinks. |
| 42 | std::string real_path; |
| 43 | if (!Realpath(entry->blk_device, &real_path)) { |
| 44 | real_path = entry->blk_device; |
| 45 | } |
| 46 | |
| 47 | // Handle logical volumes. |
| 48 | android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance(); |
| 49 | for (;;) { |
| 50 | std::optional<std::string> parent = dm.GetParentBlockDeviceByPath(real_path); |
| 51 | if (!parent.has_value()) break; |
| 52 | real_path = *parent; |
| 53 | } |
| 54 | |
| 55 | std::string path = "/sys/class/block/" + Basename(real_path); |
| 56 | |
| 57 | // Walk up the sysfs directory tree from the partition (e.g, /sys/class/block/sda34) |
| 58 | // or from the disk (e.g., /sys/class/block/sda) to reach the UFS host controller's directory |
| 59 | // (e.g., /sys/class/block/sda34/../device/../../.. --> /sys/devices/platform/00000000.ufs). |
| 60 | if (android::vold::pathExists(path + "/../device")) { |
| 61 | path += "/../device/../../.."; |
| 62 | } else if (android::vold::pathExists(path + "/device")) { |
| 63 | path += "/device/../../.."; |
| 64 | } else { |
| 65 | LOG(WARNING) << "Failed to get sysfs_path for user data partition"; |
| 66 | return ""; |
| 67 | } |
| 68 | |
| 69 | // Verify the block device is UFS by checking for the presence of "uic_link_state", |
| 70 | // which is UFS interconnect layer link state. |
| 71 | // If not UFS, return an empty path. |
| 72 | if (!android::vold::pathExists(path + "/uic_link_state")) { |
| 73 | LOG(ERROR) << "The block device (" << Basename(real_path) << ") of " << DATA_MNT_POINT |
| 74 | << " is not UFS."; |
| 75 | return ""; |
| 76 | } |
| 77 | |
| 78 | LOG(DEBUG) << "The sysfs directory for the ufs host controller is found at " << path; |
| 79 | return path; |
| 80 | } |
| 81 | |
| 82 | // get sysfs directory for the ufs host controller containing userdata |
| 83 | // returns an empty string on failures. |
| 84 | std::string GetUfsHostControllerSysfsPath() { |
| 85 | static std::string ufshc_sysfs_path = GetUfsHostControllerSysfsPathOnce(); |
| 86 | return ufshc_sysfs_path; |
| 87 | } |