blob: 6d92ecf58d5c22160e0c700bbabdaba9187ead8f [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 Williams04c28fa2023-05-10 07:51:24 -050044 size_t numConfigObj = storageConfigurations.size();
45 if (numConfigObj > 1)
46 {
47 lg2::error("eStoraged can only manage 1 eMMC device; found {NUM}",
48 "NUM", numConfigObj, "REDFISH_MESSAGE_ID",
49 std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
50 return;
51 }
52
53 for (const std::pair<sdbusplus::message::object_path,
54 estoraged::StorageData>& storage :
55 storageConfigurations)
56 {
57 const std::string& path = storage.first.str;
58
59 if (storageObjects.find(path) != storageObjects.end())
John Wedigd32b9662022-04-13 18:12:25 -070060 {
Patrick Williams04c28fa2023-05-10 07:51:24 -050061 /*
62 * We've already created this object, or at least
63 * attempted to.
64 */
65 continue;
John Wedigd32b9662022-04-13 18:12:25 -070066 }
67
Patrick Williams04c28fa2023-05-10 07:51:24 -050068 /* Get the properties from the config object. */
69 const estoraged::StorageData& data = storage.second;
70
71 /* Look for the device file. */
72 const std::filesystem::path blockDevDir{"/sys/block"};
Tom Tung043af592023-11-24 13:37:05 +080073 auto deviceInfo = estoraged::util::findDevice(data, blockDevDir);
74 if (!deviceInfo)
John Wedigd32b9662022-04-13 18:12:25 -070075 {
Patrick Williams04c28fa2023-05-10 07:51:24 -050076 lg2::error("Device not found for path {PATH}", "PATH", path,
77 "REDFISH_MESSAGE_ID",
78 std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
79 /*
80 * Set a NULL pointer as a placeholder, so that we don't
81 * try and fail again later.
82 */
83 storageObjects[path] = nullptr;
84 continue;
John Wedigd32b9662022-04-13 18:12:25 -070085 }
Patrick Williams04c28fa2023-05-10 07:51:24 -050086
Tom Tung043af592023-11-24 13:37:05 +080087 std::filesystem::path deviceFile =
88 std::move(deviceInfo->deviceFile);
89 std::filesystem::path sysfsDir = std::move(deviceInfo->sysfsDir);
90 std::string luksName = std::move(deviceInfo->luksName);
91 std::string locationCode = std::move(deviceInfo->locationCode);
92 uint64_t eraseMaxGeometry = deviceInfo->eraseMaxGeometry;
93 uint64_t eraseMinGeometry = deviceInfo->eraseMinGeometry;
94
Patrick Williams04c28fa2023-05-10 07:51:24 -050095 uint64_t size = estoraged::util::findSizeOfBlockDevice(deviceFile);
96
97 uint8_t lifeleft =
98 estoraged::util::findPredictedMediaLifeLeftPercent(sysfsDir);
99 std::string partNumber = estoraged::util::getPartNumber(sysfsDir);
100 std::string serialNumber =
101 estoraged::util::getSerialNumber(sysfsDir);
John Wedigc0d66eb2024-02-26 15:54:47 -0800102 const std::string& driveType = deviceInfo->driveType;
103 const std::string& driveProtocol = deviceInfo->driveProtocol;
Patrick Williams04c28fa2023-05-10 07:51:24 -0500104 /* Create the storage object. */
105 storageObjects[path] = std::make_unique<estoraged::EStoraged>(
106 objectServer, path, deviceFile, luksName, size, lifeleft,
Tom Tung043af592023-11-24 13:37:05 +0800107 partNumber, serialNumber, locationCode, eraseMaxGeometry,
John Wedigc0d66eb2024-02-26 15:54:47 -0800108 eraseMinGeometry, driveType, driveProtocol);
Patrick Williams04c28fa2023-05-10 07:51:24 -0500109 lg2::info("Created eStoraged object for path {PATH}", "PATH", path,
110 "REDFISH_MESSAGE_ID",
111 std::string("OpenBMC.0.1.CreateStorageObjects"));
112 }
Patrick Williamsff1b64f2023-10-20 11:19:56 -0500113 });
John Wedigd32b9662022-04-13 18:12:25 -0700114 getter->getConfiguration();
John Wedig2098dab2021-09-14 13:56:28 -0700115}
116
John Wedigd32b9662022-04-13 18:12:25 -0700117int main(void)
John Wedig2098dab2021-09-14 13:56:28 -0700118{
Ed Tanous82897c32022-02-21 14:11:59 -0800119 try
John Wedig2098dab2021-09-14 13:56:28 -0700120 {
John Wedig67a47442022-04-05 17:21:29 -0700121 // setup connection to dbus
122 boost::asio::io_context io;
123 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
124 // request D-Bus server name.
John Wedigd32b9662022-04-13 18:12:25 -0700125 conn->request_name("xyz.openbmc_project.eStoraged");
126 sdbusplus::asio::object_server server(conn);
127 boost::container::flat_map<std::string,
128 std::unique_ptr<estoraged::EStoraged>>
129 storageObjects;
Ed Tanous82897c32022-02-21 14:11:59 -0800130
John Wedig61cf4262023-03-17 14:32:04 -0700131 boost::asio::post(
132 io, [&]() { createStorageObjects(server, storageObjects, conn); });
John Wedigd32b9662022-04-13 18:12:25 -0700133
134 /*
135 * Set up an event handler to process any new configuration objects
136 * that show up later.
137 */
138 boost::asio::deadline_timer filterTimer(io);
Patrick Williamse3ef7652022-07-22 19:26:57 -0500139 std::function<void(sdbusplus::message_t&)> eventHandler =
140 [&](sdbusplus::message_t& message) {
Patrick Williams04c28fa2023-05-10 07:51:24 -0500141 if (message.is_method_error())
142 {
143 lg2::error("eventHandler callback method error");
144 return;
145 }
146 /*
147 * This implicitly cancels the timer, if it's already pending.
148 * If there's a burst of events within a short period, we want
149 * to handle them all at once. So, we will wait this long for no
150 * more events to occur, before processing them.
151 */
152 filterTimer.expires_from_now(boost::posix_time::seconds(1));
153
154 filterTimer.async_wait([&](const boost::system::error_code& ec) {
155 if (ec == boost::asio::error::operation_aborted)
John Wedigd32b9662022-04-13 18:12:25 -0700156 {
Patrick Williams04c28fa2023-05-10 07:51:24 -0500157 /* we were canceled */
John Wedigd32b9662022-04-13 18:12:25 -0700158 return;
159 }
Patrick Williams04c28fa2023-05-10 07:51:24 -0500160 if (ec)
161 {
162 lg2::error("timer error");
163 return;
164 }
165 createStorageObjects(server, storageObjects, conn);
166 });
167 };
John Wedigd32b9662022-04-13 18:12:25 -0700168
Patrick Williamse3ef7652022-07-22 19:26:57 -0500169 auto match = std::make_unique<sdbusplus::bus::match_t>(
170 static_cast<sdbusplus::bus_t&>(*conn),
John Wedigd32b9662022-04-13 18:12:25 -0700171 "type='signal',member='PropertiesChanged',path_namespace='" +
172 std::string("/xyz/openbmc_project/inventory") +
173 "',arg0namespace='" + estoraged::emmcConfigInterface + "'",
174 eventHandler);
175
Ed Tanous82897c32022-02-21 14:11:59 -0800176 lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID",
177 std::string("OpenBMC.1.0.ServiceStarted"));
178
John Wedig67a47442022-04-05 17:21:29 -0700179 io.run();
180 return 0;
John Wedig2098dab2021-09-14 13:56:28 -0700181 }
Ed Tanous82897c32022-02-21 14:11:59 -0800182 catch (const std::exception& e)
John Wedig2098dab2021-09-14 13:56:28 -0700183 {
Ed Tanous82897c32022-02-21 14:11:59 -0800184 lg2::error(e.what(), "REDFISH_MESSAGE_ID",
185 std::string("OpenBMC.1.0.ServiceException"));
John Wedig2098dab2021-09-14 13:56:28 -0700186
Ed Tanous82897c32022-02-21 14:11:59 -0800187 return 2;
188 }
John Wedig2098dab2021-09-14 13:56:28 -0700189 return 1;
190}