util.cpp (5d799bb97ca8759f39a35f1121f77b03c4efbfba) util.cpp (d32b966771e03788af7b912d4cf701d98c391097)
1#include "util.hpp"
2
1#include "util.hpp"
2
3#include "getConfig.hpp"
4
3#include <linux/fs.h>
4
5#include <phosphor-logging/lg2.hpp>
6#include <stdplus/fd/create.hpp>
7#include <stdplus/fd/managed.hpp>
8#include <stdplus/handle/managed.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
10
5#include <linux/fs.h>
6
7#include <phosphor-logging/lg2.hpp>
8#include <stdplus/fd/create.hpp>
9#include <stdplus/fd/managed.hpp>
10#include <stdplus/handle/managed.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12
13#include <filesystem>
11#include <fstream>
12#include <iostream>
13#include <string>
14
15namespace estoraged
16{
17namespace util
18{

--- 65 unchanged lines hidden (view full) ---

84 else
85 {
86 maxLifeUsed = estB;
87 }
88
89 return static_cast<uint8_t>(11 - maxLifeUsed) * 10;
90}
91
14#include <fstream>
15#include <iostream>
16#include <string>
17
18namespace estoraged
19{
20namespace util
21{

--- 65 unchanged lines hidden (view full) ---

87 else
88 {
89 maxLifeUsed = estB;
90 }
91
92 return static_cast<uint8_t>(11 - maxLifeUsed) * 10;
93}
94
95bool findDevice(const StorageData& data, const std::filesystem::path& searchDir,
96 std::filesystem::path& deviceFile,
97 std::filesystem::path& sysfsDir, std::string& luksName)
98{
99 /* Check what type of storage device this is. */
100 estoraged::BasicVariantType typeVariant;
101 try
102 {
103 /* The EntityManager config should have this property set. */
104 typeVariant = data.at("Type");
105 }
106 catch (const boost::container::out_of_range& e)
107 {
108 lg2::error("Could not read device type", "REDFISH_MESSAGE_ID",
109 std::string("OpenBMC.0.1.FindDeviceFail"));
110 return false;
111 }
112
113 /*
114 * Currently, we only support eMMC, so report an error for any other device
115 * types.
116 */
117 std::string type = std::get<std::string>(typeVariant);
118 if (type.compare("EmmcDevice") != 0)
119 {
120 lg2::error("Unsupported device type {TYPE}", "TYPE", type,
121 "REDFISH_MESSAGE_ID",
122 std::string("OpenBMC.0.1.FindDeviceFail"));
123 return false;
124 }
125
126 /* Look for the eMMC in the specified searchDir directory. */
127 for (auto const& dirEntry : std::filesystem::directory_iterator{searchDir})
128 {
129 std::filesystem::path curDevice(dirEntry.path().filename());
130 if (curDevice.string().starts_with("mmcblk"))
131 {
132 sysfsDir = dirEntry.path();
133 sysfsDir /= "device";
134
135 deviceFile = "/dev";
136 deviceFile /= curDevice;
137
138 luksName = "luks-" + curDevice.string();
139 return true;
140 }
141 }
142
143 /* Device wasn't found. */
144 return false;
145}
146
92} // namespace util
93
94} // namespace estoraged
147} // namespace util
148
149} // namespace estoraged