xref: /openbmc/estoraged/include/util.hpp (revision e7f6975e)
1 #pragma once
2 #include "getConfig.hpp"
3 
4 #include <filesystem>
5 #include <optional>
6 #include <string>
7 
8 namespace estoraged
9 {
10 namespace util
11 {
12 
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;
21     std::string driveType;
22 
23     DeviceInfo(std::filesystem::path& deviceFile,
24                std::filesystem::path& sysfsDir, std::string& luksName,
25                std::string& locationCode, uint64_t eraseMaxGeometry,
26                uint64_t eraseMinGeometry, std::string& driveType) :
27         deviceFile(deviceFile),
28         sysfsDir(sysfsDir), luksName(luksName), locationCode(locationCode),
29         eraseMaxGeometry(eraseMaxGeometry), eraseMinGeometry(eraseMinGeometry),
30         driveType(driveType)
31     {}
32 };
33 
34 /** @brief finds the size of the linux block device in bytes
35  *  @param[in] devpath - the name of the linux block device
36  *  @return size of a block device using the devPath
37  */
38 uint64_t findSizeOfBlockDevice(const std::string& devPath);
39 
40 /** @brief finds the predicted life left for a eMMC device
41  *  @param[in] sysfsPath - The path to the linux sysfs interface
42  *  @return the life remaing for the emmc, as a percentage.
43  */
44 uint8_t findPredictedMediaLifeLeftPercent(const std::string& sysfsPath);
45 
46 /** @brief Get the part number (aka part name) for the storage device
47  *  @param[in] sysfsPath - The path to the linux sysfs interface.
48  *  @return part name as a string (or "unknown" if it couldn't be retrieved)
49  */
50 std::string getPartNumber(const std::filesystem::path& sysfsPath);
51 
52 /** @brief Get the serial number for the storage device
53  *  @param[in] sysfsPath - The path to the linux sysfs interface.
54  *  @return serial name as a string (or "unknown" if it couldn't be retrieved)
55  */
56 std::string getSerialNumber(const std::filesystem::path& sysfsPath);
57 
58 /** @brief Look for the device described by the provided StorageData.
59  *  @details Currently, this function assumes that there's only one eMMC.
60  *    When we need to support multiple eMMCs, we will put more information in
61  *    the EntityManager config, to differentiate between them. Also, if we
62  *    want to support other types of storage devices, this function will need
63  *    to be updated.
64  *
65  *  @param[in] data - map of properties from the config object.
66  *  @param[in] searchDir - directory to search for devices in sysfs, e.g.
67  *    /sys/block
68  *  @return DeviceInfo - metadata for the device if device is found. Null
69  *  otherwise.
70  */
71 std::optional<DeviceInfo> findDevice(const StorageData& data,
72                                      const std::filesystem::path& searchDir);
73 
74 } // namespace util
75 
76 } // namespace estoraged
77