xref: /openbmc/estoraged/include/util.hpp (revision 15b63e12bdc3f3116fb841349dd4f1cd17a8398b)
1a6e3b99dSJohn Edward Broadbent #pragma once
2d32b9667SJohn Wedig #include "getConfig.hpp"
3d32b9667SJohn Wedig 
4d32b9667SJohn Wedig #include <filesystem>
5043af59fSTom Tung #include <optional>
65d799bb9SJohn Edward Broadbent #include <string>
7a6e3b99dSJohn Edward Broadbent 
8a6e3b99dSJohn Edward Broadbent namespace estoraged
9a6e3b99dSJohn Edward Broadbent {
10a6e3b99dSJohn Edward Broadbent namespace util
11a6e3b99dSJohn Edward Broadbent {
12a6e3b99dSJohn Edward Broadbent 
13043af59fSTom Tung struct DeviceInfo
14043af59fSTom Tung {
15043af59fSTom Tung     std::filesystem::path deviceFile;
16043af59fSTom Tung     std::filesystem::path sysfsDir;
17043af59fSTom Tung     std::string luksName;
18043af59fSTom Tung     std::string locationCode;
19043af59fSTom Tung     uint64_t eraseMaxGeometry;
20043af59fSTom Tung     uint64_t eraseMinGeometry;
21d7be42bdSJohn Wedig     std::string driveType;
22c0d66eb7SJohn Wedig     std::string driveProtocol;
23043af59fSTom Tung 
DeviceInfoestoraged::util::DeviceInfo24043af59fSTom Tung     DeviceInfo(std::filesystem::path& deviceFile,
25043af59fSTom Tung                std::filesystem::path& sysfsDir, std::string& luksName,
26043af59fSTom Tung                std::string& locationCode, uint64_t eraseMaxGeometry,
27c0d66eb7SJohn Wedig                uint64_t eraseMinGeometry, std::string& driveType,
28c0d66eb7SJohn Wedig                std::string& driveProtocol) :
29*15b63e12SPatrick Williams         deviceFile(deviceFile), sysfsDir(sysfsDir), luksName(luksName),
30*15b63e12SPatrick Williams         locationCode(locationCode), eraseMaxGeometry(eraseMaxGeometry),
31*15b63e12SPatrick Williams         eraseMinGeometry(eraseMinGeometry), driveType(driveType),
32*15b63e12SPatrick Williams         driveProtocol(driveProtocol)
33043af59fSTom Tung     {}
34043af59fSTom Tung };
35043af59fSTom Tung 
36a6e3b99dSJohn Edward Broadbent /** @brief finds the size of the linux block device in bytes
37a6e3b99dSJohn Edward Broadbent  *  @param[in] devpath - the name of the linux block device
38a6e3b99dSJohn Edward Broadbent  *  @return size of a block device using the devPath
39a6e3b99dSJohn Edward Broadbent  */
405d799bb9SJohn Edward Broadbent uint64_t findSizeOfBlockDevice(const std::string& devPath);
415d799bb9SJohn Edward Broadbent 
425d799bb9SJohn Edward Broadbent /** @brief finds the predicted life left for a eMMC device
435d799bb9SJohn Edward Broadbent  *  @param[in] sysfsPath - The path to the linux sysfs interface
44d4554f2aSManojkiran Eda  *  @return the life remaining for the emmc, as a percentage.
455d799bb9SJohn Edward Broadbent  */
465d799bb9SJohn Edward Broadbent uint8_t findPredictedMediaLifeLeftPercent(const std::string& sysfsPath);
47a6e3b99dSJohn Edward Broadbent 
48b4838308SJohn Wedig /** @brief Get the part number (aka part name) for the storage device
49b4838308SJohn Wedig  *  @param[in] sysfsPath - The path to the linux sysfs interface.
50b4838308SJohn Wedig  *  @return part name as a string (or "unknown" if it couldn't be retrieved)
51b4838308SJohn Wedig  */
52b4838308SJohn Wedig std::string getPartNumber(const std::filesystem::path& sysfsPath);
53b4838308SJohn Wedig 
54b4838308SJohn Wedig /** @brief Get the serial number for the storage device
55b4838308SJohn Wedig  *  @param[in] sysfsPath - The path to the linux sysfs interface.
56b4838308SJohn Wedig  *  @return serial name as a string (or "unknown" if it couldn't be retrieved)
57b4838308SJohn Wedig  */
58b4838308SJohn Wedig std::string getSerialNumber(const std::filesystem::path& sysfsPath);
59b4838308SJohn Wedig 
60d32b9667SJohn Wedig /** @brief Look for the device described by the provided StorageData.
61d32b9667SJohn Wedig  *  @details Currently, this function assumes that there's only one eMMC.
62d32b9667SJohn Wedig  *    When we need to support multiple eMMCs, we will put more information in
63d32b9667SJohn Wedig  *    the EntityManager config, to differentiate between them. Also, if we
64d32b9667SJohn Wedig  *    want to support other types of storage devices, this function will need
65d32b9667SJohn Wedig  *    to be updated.
66d32b9667SJohn Wedig  *
67d32b9667SJohn Wedig  *  @param[in] data - map of properties from the config object.
68d32b9667SJohn Wedig  *  @param[in] searchDir - directory to search for devices in sysfs, e.g.
69d32b9667SJohn Wedig  *    /sys/block
70043af59fSTom Tung  *  @return DeviceInfo - metadata for the device if device is found. Null
71043af59fSTom Tung  *  otherwise.
72d32b9667SJohn Wedig  */
73043af59fSTom Tung std::optional<DeviceInfo> findDevice(const StorageData& data,
74043af59fSTom Tung                                      const std::filesystem::path& searchDir);
75d32b9667SJohn Wedig 
76a6e3b99dSJohn Edward Broadbent } // namespace util
77a6e3b99dSJohn Edward Broadbent 
78a6e3b99dSJohn Edward Broadbent } // namespace estoraged
79