blob: 5f4be199db7ebff28d0948b8f4cf4c7899278efc [file] [log] [blame]
John Wedig2098dab2021-09-14 13:56:28 -07001
2#include "estoraged.hpp"
John Wedigd32b9662022-04-13 18:12:25 -07003#include "getConfig.hpp"
4#include "util.hpp"
John Wedig2098dab2021-09-14 13:56:28 -07005
John Wedigd32b9662022-04-13 18:12:25 -07006#include <boost/asio/deadline_timer.hpp>
John Wedig67a47442022-04-05 17:21:29 -07007#include <boost/asio/io_context.hpp>
John Wedigd32b9662022-04-13 18:12:25 -07008#include <boost/asio/post.hpp>
9#include <boost/container/flat_map.hpp>
10#include <boost/container/throw_exception.hpp>
John Edward Broadbent4e13b0a2021-11-15 15:21:59 -080011#include <phosphor-logging/lg2.hpp>
John Wedig67a47442022-04-05 17:21:29 -070012#include <sdbusplus/asio/connection.hpp>
13#include <sdbusplus/asio/object_server.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070014#include <sdbusplus/bus.hpp>
John Wedigd32b9662022-04-13 18:12:25 -070015#include <sdbusplus/bus/match.hpp>
John Edward Broadbente35e7362022-03-22 16:14:24 -070016#include <util.hpp>
John Wedig2098dab2021-09-14 13:56:28 -070017
John Wedigd32b9662022-04-13 18:12:25 -070018#include <cstdlib>
John Wedig2098dab2021-09-14 13:56:28 -070019#include <filesystem>
20#include <iostream>
John Wedig67a47442022-04-05 17:21:29 -070021#include <memory>
Tom Tung043af592023-11-24 13:37:05 +080022#include <optional>
John Wedig2098dab2021-09-14 13:56:28 -070023#include <string>
24
John Wedigd32b9662022-04-13 18:12:25 -070025/*
26 * Get the configuration objects from Entity Manager and create new D-Bus
27 * objects for each one. This function can be called multiple times, in case
28 * new configuration objects show up later.
29 *
30 * Note: Currently, eStoraged can only support 1 eMMC device.
31 * Additional changes will be needed to support more than 1 eMMC, or to support
32 * more types of storage devices.
33 */
34void createStorageObjects(
John Wedig61cf4262023-03-17 14:32:04 -070035 sdbusplus::asio::object_server& objectServer,
John Wedigd32b9662022-04-13 18:12:25 -070036 boost::container::flat_map<
37 std::string, std::unique_ptr<estoraged::EStoraged>>& storageObjects,
38 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
John Wedig2098dab2021-09-14 13:56:28 -070039{
John Wedigd32b9662022-04-13 18:12:25 -070040 auto getter = std::make_shared<estoraged::GetStorageConfiguration>(
41 dbusConnection,
John Wedig61cf4262023-03-17 14:32:04 -070042 [&objectServer, &storageObjects](
John Wedigd32b9662022-04-13 18:12:25 -070043 const estoraged::ManagedStorageType& storageConfigurations) {
Patrick Williams15b63e12024-08-16 15:22:01 -040044 size_t numConfigObj = storageConfigurations.size();
45 if (numConfigObj > 1)
John Wedigd32b9662022-04-13 18:12:25 -070046 {
Patrick Williams15b63e12024-08-16 15:22:01 -040047 lg2::error(
48 "eStoraged can only manage 1 eMMC device; found {NUM}",
49 "NUM", numConfigObj, "REDFISH_MESSAGE_ID",
50 std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
51 return;
John Wedigd32b9662022-04-13 18:12:25 -070052 }
53
Patrick Williams15b63e12024-08-16 15:22:01 -040054 for (const std::pair<sdbusplus::message::object_path,
55 estoraged::StorageData>& storage :
56 storageConfigurations)
John Wedigd32b9662022-04-13 18:12:25 -070057 {
Patrick Williams15b63e12024-08-16 15:22:01 -040058 const std::string& path = storage.first.str;
59
60 if (storageObjects.find(path) != storageObjects.end())
61 {
62 /*
63 * We've already created this object, or at least
64 * attempted to.
65 */
66 continue;
67 }
68
69 /* Get the properties from the config object. */
70 const estoraged::StorageData& data = storage.second;
71
72 /* Look for the device file. */
73 const std::filesystem::path blockDevDir{"/sys/block"};
74 auto deviceInfo =
75 estoraged::util::findDevice(data, blockDevDir);
76 if (!deviceInfo)
77 {
78 lg2::error(
79 "Device not found for path {PATH}", "PATH", path,
80 "REDFISH_MESSAGE_ID",
81 std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
82 /*
83 * Set a NULL pointer as a placeholder, so that we don't
84 * try and fail again later.
85 */
86 storageObjects[path] = nullptr;
87 continue;
88 }
89
90 std::filesystem::path deviceFile =
91 std::move(deviceInfo->deviceFile);
92 std::filesystem::path sysfsDir =
93 std::move(deviceInfo->sysfsDir);
94 std::string luksName = std::move(deviceInfo->luksName);
95 std::string locationCode = std::move(deviceInfo->locationCode);
96 uint64_t eraseMaxGeometry = deviceInfo->eraseMaxGeometry;
97 uint64_t eraseMinGeometry = deviceInfo->eraseMinGeometry;
98
99 uint64_t size =
100 estoraged::util::findSizeOfBlockDevice(deviceFile);
101
102 uint8_t lifeleft =
103 estoraged::util::findPredictedMediaLifeLeftPercent(
104 sysfsDir);
105 std::string partNumber =
106 estoraged::util::getPartNumber(sysfsDir);
107 std::string serialNumber =
108 estoraged::util::getSerialNumber(sysfsDir);
109 const std::string& driveType = deviceInfo->driveType;
110 const std::string& driveProtocol = deviceInfo->driveProtocol;
111 /* Create the storage object. */
112 storageObjects[path] = std::make_unique<estoraged::EStoraged>(
113 objectServer, path, deviceFile, luksName, size, lifeleft,
114 partNumber, serialNumber, locationCode, eraseMaxGeometry,
115 eraseMinGeometry, driveType, driveProtocol);
116 lg2::info("Created eStoraged object for path {PATH}", "PATH",
117 path, "REDFISH_MESSAGE_ID",
118 std::string("OpenBMC.0.1.CreateStorageObjects"));
John Wedigd32b9662022-04-13 18:12:25 -0700119 }
Patrick Williams15b63e12024-08-16 15:22:01 -0400120 });
John Wedigd32b9662022-04-13 18:12:25 -0700121 getter->getConfiguration();
John Wedig2098dab2021-09-14 13:56:28 -0700122}
123
John Wedigd32b9662022-04-13 18:12:25 -0700124int main(void)
John Wedig2098dab2021-09-14 13:56:28 -0700125{
Ed Tanous82897c32022-02-21 14:11:59 -0800126 try
John Wedig2098dab2021-09-14 13:56:28 -0700127 {
John Wedig67a47442022-04-05 17:21:29 -0700128 // setup connection to dbus
129 boost::asio::io_context io;
130 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
131 // request D-Bus server name.
John Wedigd32b9662022-04-13 18:12:25 -0700132 conn->request_name("xyz.openbmc_project.eStoraged");
133 sdbusplus::asio::object_server server(conn);
134 boost::container::flat_map<std::string,
135 std::unique_ptr<estoraged::EStoraged>>
136 storageObjects;
Ed Tanous82897c32022-02-21 14:11:59 -0800137
Patrick Williams15b63e12024-08-16 15:22:01 -0400138 boost::asio::post(io, [&]() {
139 createStorageObjects(server, storageObjects, conn);
140 });
John Wedigd32b9662022-04-13 18:12:25 -0700141
142 /*
143 * Set up an event handler to process any new configuration objects
144 * that show up later.
145 */
146 boost::asio::deadline_timer filterTimer(io);
Patrick Williamse3ef7652022-07-22 19:26:57 -0500147 std::function<void(sdbusplus::message_t&)> eventHandler =
148 [&](sdbusplus::message_t& message) {
Patrick Williams15b63e12024-08-16 15:22:01 -0400149 if (message.is_method_error())
150 {
151 lg2::error("eventHandler callback method error");
152 return;
153 }
154 /*
155 * This implicitly cancels the timer, if it's already pending.
156 * If there's a burst of events within a short period, we want
157 * to handle them all at once. So, we will wait this long for no
158 * more events to occur, before processing them.
159 */
160 filterTimer.expires_from_now(boost::posix_time::seconds(1));
Patrick Williams04c28fa2023-05-10 07:51:24 -0500161
Patrick Williams15b63e12024-08-16 15:22:01 -0400162 filterTimer.async_wait(
163 [&](const boost::system::error_code& ec) {
164 if (ec == boost::asio::error::operation_aborted)
165 {
166 /* we were canceled */
167 return;
168 }
169 if (ec)
170 {
171 lg2::error("timer error");
172 return;
173 }
174 createStorageObjects(server, storageObjects, conn);
175 });
176 };
John Wedigd32b9662022-04-13 18:12:25 -0700177
Patrick Williamse3ef7652022-07-22 19:26:57 -0500178 auto match = std::make_unique<sdbusplus::bus::match_t>(
179 static_cast<sdbusplus::bus_t&>(*conn),
John Wedigd32b9662022-04-13 18:12:25 -0700180 "type='signal',member='PropertiesChanged',path_namespace='" +
181 std::string("/xyz/openbmc_project/inventory") +
182 "',arg0namespace='" + estoraged::emmcConfigInterface + "'",
183 eventHandler);
184
Ed Tanous82897c32022-02-21 14:11:59 -0800185 lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID",
186 std::string("OpenBMC.1.0.ServiceStarted"));
187
John Wedig67a47442022-04-05 17:21:29 -0700188 io.run();
189 return 0;
John Wedig2098dab2021-09-14 13:56:28 -0700190 }
Ed Tanous82897c32022-02-21 14:11:59 -0800191 catch (const std::exception& e)
John Wedig2098dab2021-09-14 13:56:28 -0700192 {
Ed Tanous82897c32022-02-21 14:11:59 -0800193 lg2::error(e.what(), "REDFISH_MESSAGE_ID",
194 std::string("OpenBMC.1.0.ServiceException"));
John Wedig2098dab2021-09-14 13:56:28 -0700195
Ed Tanous82897c32022-02-21 14:11:59 -0800196 return 2;
197 }
John Wedig2098dab2021-09-14 13:56:28 -0700198 return 1;
199}