John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 1 | #pragma once |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 2 | #include "getConfig.hpp" |
| 3 | |
| 4 | #include <filesystem> |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 5 | #include <optional> |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 6 | #include <string> |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 7 | |
| 8 | namespace estoraged |
| 9 | { |
| 10 | namespace util |
| 11 | { |
| 12 | |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 13 | struct DeviceInfo |
| 14 | { |
| 15 | std::filesystem::path deviceFile; |
| 16 | std::filesystem::path sysfsDir; |
| 17 | std::string luksName; |
| 18 | std::string locationCode; |
| 19 | uint64_t eraseMaxGeometry; |
| 20 | uint64_t eraseMinGeometry; |
John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame] | 21 | std::string driveType; |
John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 22 | std::string driveProtocol; |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 23 | |
| 24 | DeviceInfo(std::filesystem::path& deviceFile, |
| 25 | std::filesystem::path& sysfsDir, std::string& luksName, |
| 26 | std::string& locationCode, uint64_t eraseMaxGeometry, |
John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 27 | uint64_t eraseMinGeometry, std::string& driveType, |
| 28 | std::string& driveProtocol) : |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 29 | deviceFile(deviceFile), sysfsDir(sysfsDir), luksName(luksName), |
| 30 | locationCode(locationCode), eraseMaxGeometry(eraseMaxGeometry), |
| 31 | eraseMinGeometry(eraseMinGeometry), driveType(driveType), |
| 32 | driveProtocol(driveProtocol) |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 33 | {} |
| 34 | }; |
| 35 | |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 36 | /** @brief finds the size of the linux block device in bytes |
| 37 | * @param[in] devpath - the name of the linux block device |
| 38 | * @return size of a block device using the devPath |
| 39 | */ |
| 40 | uint64_t findSizeOfBlockDevice(const std::string& devPath); |
| 41 | |
| 42 | /** @brief finds the predicted life left for a eMMC device |
| 43 | * @param[in] sysfsPath - The path to the linux sysfs interface |
Manojkiran Eda | d4554f2 | 2024-06-17 14:11:30 +0530 | [diff] [blame] | 44 | * @return the life remaining for the emmc, as a percentage. |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 45 | */ |
| 46 | uint8_t findPredictedMediaLifeLeftPercent(const std::string& sysfsPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 47 | |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 48 | /** @brief Get the part number (aka part name) for the storage device |
| 49 | * @param[in] sysfsPath - The path to the linux sysfs interface. |
| 50 | * @return part name as a string (or "unknown" if it couldn't be retrieved) |
| 51 | */ |
| 52 | std::string getPartNumber(const std::filesystem::path& sysfsPath); |
| 53 | |
| 54 | /** @brief Get the serial number for the storage device |
| 55 | * @param[in] sysfsPath - The path to the linux sysfs interface. |
| 56 | * @return serial name as a string (or "unknown" if it couldn't be retrieved) |
| 57 | */ |
| 58 | std::string getSerialNumber(const std::filesystem::path& sysfsPath); |
| 59 | |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 60 | /** @brief Look for the device described by the provided StorageData. |
| 61 | * @details Currently, this function assumes that there's only one eMMC. |
| 62 | * When we need to support multiple eMMCs, we will put more information in |
| 63 | * the EntityManager config, to differentiate between them. Also, if we |
| 64 | * want to support other types of storage devices, this function will need |
| 65 | * to be updated. |
| 66 | * |
| 67 | * @param[in] data - map of properties from the config object. |
| 68 | * @param[in] searchDir - directory to search for devices in sysfs, e.g. |
| 69 | * /sys/block |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 70 | * @return DeviceInfo - metadata for the device if device is found. Null |
| 71 | * otherwise. |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 72 | */ |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 73 | std::optional<DeviceInfo> findDevice(const StorageData& data, |
| 74 | const std::filesystem::path& searchDir); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 75 | |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 76 | } // namespace util |
| 77 | |
| 78 | } // namespace estoraged |