blob: 2430e8e334801f95847c28cfa78c42466bb789ce [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include <arpa/inet.h>
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05302#include <fcntl.h>
Xo Wang87651332017-08-11 10:17:59 -07003#include <limits.h>
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05304#include <linux/i2c-dev.h>
5#include <linux/i2c.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07006#include <mapper.h>
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05307#include <sys/ioctl.h>
8#include <sys/stat.h>
9#include <sys/types.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070010#include <systemd/sd-bus.h>
Xo Wang87651332017-08-11 10:17:59 -070011#include <unistd.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070012
Patrick Venture3a5071a2018-09-12 13:27:42 -070013#include <algorithm>
Vernon Mauery0120b682019-03-25 13:08:54 -070014#include <app/channel.hpp>
15#include <app/watchdog.hpp>
16#include <apphandler.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070017#include <array>
18#include <cstddef>
Vernon Mauery0120b682019-03-25 13:08:54 -070019#include <cstdint>
Vernon Mauerybdda8002019-02-26 10:18:51 -080020#include <filesystem>
Patrick Venture3a5071a2018-09-12 13:27:42 -070021#include <fstream>
Vernon Mauery0120b682019-03-25 13:08:54 -070022#include <ipmid/api.hpp>
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +000023#include <ipmid/sessiondef.hpp>
24#include <ipmid/sessionhelper.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070025#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070026#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070027#include <memory>
Patrick Venture46470a32018-09-07 19:26:25 -070028#include <nlohmann/json.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070029#include <phosphor-logging/elog-errors.hpp>
30#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070031#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070032#include <string>
Vernon Mauery0120b682019-03-25 13:08:54 -070033#include <sys_info_param.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070034#include <tuple>
35#include <vector>
36#include <xyz/openbmc_project/Common/error.hpp>
Yong Li18d77262018-10-09 01:59:45 +080037#include <xyz/openbmc_project/Control/Power/ACPIPowerState/server.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070038#include <xyz/openbmc_project/Software/Activation/server.hpp>
39#include <xyz/openbmc_project/Software/Version/server.hpp>
40#include <xyz/openbmc_project/State/BMC/server.hpp>
Ratan Guptab8e99552017-07-27 07:07:48 +053041
Patrick Venture0b02be92018-08-31 11:55:55 -070042extern sd_bus* bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053043
Alexander Amelkinba19c182018-09-04 15:49:36 +030044constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC";
45constexpr auto bmc_state_property = "CurrentBMCState";
Marri Devender Rao5e007a52018-01-08 06:18:36 -060046
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060047static constexpr auto redundancyIntf =
48 "xyz.openbmc_project.Software.RedundancyPriority";
Patrick Venture0b02be92018-08-31 11:55:55 -070049static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060050static constexpr auto activationIntf =
51 "xyz.openbmc_project.Software.Activation";
52static constexpr auto softwareRoot = "/xyz/openbmc_project/software";
53
Chris Austen6caf28b2015-10-13 12:40:40 -050054void register_netfn_app_functions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053055
Ratan Guptab8e99552017-07-27 07:07:48 +053056using namespace phosphor::logging;
57using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060058using Version = sdbusplus::xyz::openbmc_project::Software::server::Version;
59using Activation =
60 sdbusplus::xyz::openbmc_project::Software::server::Activation;
Alexander Amelkinba19c182018-09-04 15:49:36 +030061using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC;
Vernon Mauery185b9f82018-07-20 10:52:36 -070062namespace fs = std::filesystem;
Ratan Guptab8e99552017-07-27 07:07:48 +053063
Yong Libd0503a2019-08-22 17:17:17 +080064#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053065typedef struct
66{
67 uint8_t busId;
68 uint8_t slaveAddr;
69 uint8_t slaveAddrMask;
70 std::vector<uint8_t> data;
71 std::vector<uint8_t> dataMask;
72} i2cMasterWRWhitelist;
73
74static std::vector<i2cMasterWRWhitelist>& getWRWhitelist()
75{
76 static std::vector<i2cMasterWRWhitelist> wrWhitelist;
77 return wrWhitelist;
78}
79
80static constexpr const char* i2cMasterWRWhitelistFile =
81 "/usr/share/ipmi-providers/master_write_read_white_list.json";
82
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053083static constexpr const char* filtersStr = "filters";
84static constexpr const char* busIdStr = "busId";
85static constexpr const char* slaveAddrStr = "slaveAddr";
86static constexpr const char* slaveAddrMaskStr = "slaveAddrMask";
87static constexpr const char* cmdStr = "command";
88static constexpr const char* cmdMaskStr = "commandMask";
89static constexpr int base_16 = 16;
Yong Libd0503a2019-08-22 17:17:17 +080090#endif // ENABLE_I2C_WHITELIST_CHECK
91static constexpr uint8_t maxIPMIWriteReadSize = 144;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053092
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060093/**
94 * @brief Returns the Version info from primary s/w object
95 *
96 * Get the Version info from the active s/w object which is having high
97 * "Priority" value(a smaller number is a higher priority) and "Purpose"
98 * is "BMC" from the list of all s/w objects those are implementing
99 * RedundancyPriority interface from the given softwareRoot path.
100 *
101 * @return On success returns the Version info from primary s/w object.
102 *
103 */
Vernon Maueryea1c4012019-05-24 13:26:16 -0700104std::string getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx)
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600105{
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600106 std::string revision{};
Vernon Mauery86a50822019-03-25 13:11:36 -0700107 ipmi::ObjectTree objectTree;
108 try
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600109 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700110 objectTree =
Vernon Maueryea1c4012019-05-24 13:26:16 -0700111 ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, redundancyIntf);
Vernon Mauery86a50822019-03-25 13:11:36 -0700112 }
113 catch (sdbusplus::exception::SdBusError& e)
114 {
115 log<level::ERR>("Failed to fetch redundancy object from dbus",
116 entry("INTERFACE=%s", redundancyIntf),
117 entry("ERRMSG=%s", e.what()));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600118 elog<InternalFailure>();
119 }
120
121 auto objectFound = false;
122 for (auto& softObject : objectTree)
123 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700124 auto service =
Vernon Maueryea1c4012019-05-24 13:26:16 -0700125 ipmi::getService(*ctx->bus, redundancyIntf, softObject.first);
Vernon Mauery86a50822019-03-25 13:11:36 -0700126 auto objValueTree =
Vernon Maueryea1c4012019-05-24 13:26:16 -0700127 ipmi::getManagedObjects(*ctx->bus, service, softwareRoot);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600128
129 auto minPriority = 0xFF;
130 for (const auto& objIter : objValueTree)
131 {
132 try
133 {
134 auto& intfMap = objIter.second;
135 auto& redundancyPriorityProps = intfMap.at(redundancyIntf);
136 auto& versionProps = intfMap.at(versionIntf);
137 auto& activationProps = intfMap.at(activationIntf);
Vernon Maueryf442e112019-04-09 11:44:36 -0700138 auto priority =
139 std::get<uint8_t>(redundancyPriorityProps.at("Priority"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700140 auto purpose =
Vernon Maueryf442e112019-04-09 11:44:36 -0700141 std::get<std::string>(versionProps.at("Purpose"));
142 auto activation =
143 std::get<std::string>(activationProps.at("Activation"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700144 auto version =
Vernon Maueryf442e112019-04-09 11:44:36 -0700145 std::get<std::string>(versionProps.at("Version"));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600146 if ((Version::convertVersionPurposeFromString(purpose) ==
147 Version::VersionPurpose::BMC) &&
148 (Activation::convertActivationsFromString(activation) ==
149 Activation::Activations::Active))
150 {
151 if (priority < minPriority)
152 {
153 minPriority = priority;
154 objectFound = true;
155 revision = std::move(version);
156 }
157 }
158 }
159 catch (const std::exception& e)
160 {
161 log<level::ERR>(e.what());
162 }
163 }
164 }
165
166 if (!objectFound)
167 {
168 log<level::ERR>("Could not found an BMC software Object");
169 elog<InternalFailure>();
170 }
171
172 return revision;
173}
174
Alexander Amelkinba19c182018-09-04 15:49:36 +0300175bool getCurrentBmcState()
176{
177 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
178
179 // Get the Inventory object implementing the BMC interface
180 ipmi::DbusObjectInfo bmcObject =
181 ipmi::getDbusObject(bus, bmc_state_interface);
182 auto variant =
183 ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
184 bmc_state_interface, bmc_state_property);
185
Vernon Maueryf442e112019-04-09 11:44:36 -0700186 return std::holds_alternative<std::string>(variant) &&
187 BMC::convertBMCStateFromString(std::get<std::string>(variant)) ==
188 BMC::BMCState::Ready;
Alexander Amelkinba19c182018-09-04 15:49:36 +0300189}
190
Patrick Venture94930a12019-04-30 10:01:58 -0700191bool getCurrentBmcStateWithFallback(const bool fallbackAvailability)
192{
193 try
194 {
195 return getCurrentBmcState();
196 }
197 catch (...)
198 {
199 // Nothing provided the BMC interface, therefore return whatever was
200 // configured as the default.
201 return fallbackAvailability;
202 }
203}
204
Yong Li18d77262018-10-09 01:59:45 +0800205namespace acpi_state
206{
207using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
208
209const static constexpr char* acpiObjPath =
210 "/xyz/openbmc_project/control/host0/acpi_power_state";
211const static constexpr char* acpiInterface =
212 "xyz.openbmc_project.Control.Power.ACPIPowerState";
213const static constexpr char* sysACPIProp = "SysACPIStatus";
214const static constexpr char* devACPIProp = "DevACPIStatus";
215
216enum class PowerStateType : uint8_t
217{
218 sysPowerState = 0x00,
219 devPowerState = 0x01,
220};
221
222// Defined in 20.6 of ipmi doc
223enum class PowerState : uint8_t
224{
225 s0G0D0 = 0x00,
226 s1D1 = 0x01,
227 s2D2 = 0x02,
228 s3D3 = 0x03,
229 s4 = 0x04,
230 s5G2 = 0x05,
231 s4S5 = 0x06,
232 g3 = 0x07,
233 sleep = 0x08,
234 g1Sleep = 0x09,
235 override = 0x0a,
236 legacyOn = 0x20,
237 legacyOff = 0x21,
238 unknown = 0x2a,
239 noChange = 0x7f,
240};
241
242static constexpr uint8_t stateChanged = 0x80;
243
Yong Li18d77262018-10-09 01:59:45 +0800244std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = {
245 {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0},
246 {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1},
247 {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2},
248 {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3},
249 {ACPIPowerState::ACPI::S4, PowerState::s4},
250 {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2},
251 {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5},
252 {ACPIPowerState::ACPI::G3, PowerState::g3},
253 {ACPIPowerState::ACPI::SLEEP, PowerState::sleep},
254 {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep},
255 {ACPIPowerState::ACPI::OVERRIDE, PowerState::override},
256 {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn},
257 {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff},
258 {ACPIPowerState::ACPI::Unknown, PowerState::unknown}};
259
260bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state)
261{
262 if (type == acpi_state::PowerStateType::sysPowerState)
263 {
264 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) ||
265 (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) ||
266 (state ==
267 static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) ||
268 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
269 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
270 {
271 return true;
272 }
273 else
274 {
275 return false;
276 }
277 }
278 else if (type == acpi_state::PowerStateType::devPowerState)
279 {
280 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) ||
281 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
282 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
283 {
284 return true;
285 }
286 else
287 {
288 return false;
289 }
290 }
291 else
292 {
293 return false;
294 }
295 return false;
296}
297} // namespace acpi_state
298
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000299/** @brief implements Set ACPI Power State command
300 * @param sysAcpiState - ACPI system power state to set
301 * @param devAcpiState - ACPI device power state to set
302 *
303 * @return IPMI completion code on success
304 **/
305ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState,
306 uint8_t devAcpiState)
Chris Austen6caf28b2015-10-13 12:40:40 -0500307{
Yong Li18d77262018-10-09 01:59:45 +0800308 auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
Yong Li18d77262018-10-09 01:59:45 +0800309
310 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
311
312 auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
313
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000314 if (sysAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800315 {
316 // set system power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000317 s = sysAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800318
319 if (!acpi_state::isValidACPIState(
320 acpi_state::PowerStateType::sysPowerState, s))
321 {
322 log<level::ERR>("set_acpi_power sys invalid input",
323 entry("S=%x", s));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000324 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800325 }
326
327 // valid input
328 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
329 {
330 log<level::DEBUG>("No change for system power state");
331 }
332 else
333 {
334 auto found = std::find_if(
335 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
336 [&s](const auto& iter) {
337 return (static_cast<uint8_t>(iter.second) == s);
338 });
339
340 value = found->first;
341
342 try
343 {
344 auto acpiObject =
345 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
346 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
347 acpi_state::acpiInterface,
348 acpi_state::sysACPIProp,
349 convertForMessage(value));
350 }
351 catch (const InternalFailure& e)
352 {
353 log<level::ERR>("Failed in set ACPI system property",
354 entry("EXCEPTION=%s", e.what()));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000355 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800356 }
357 }
358 }
359 else
360 {
361 log<level::DEBUG>("Do not change system power state");
362 }
363
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000364 if (devAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800365 {
366 // set device power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000367 s = devAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800368 if (!acpi_state::isValidACPIState(
369 acpi_state::PowerStateType::devPowerState, s))
370 {
371 log<level::ERR>("set_acpi_power dev invalid input",
372 entry("S=%x", s));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000373 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800374 }
375
376 // valid input
377 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
378 {
379 log<level::DEBUG>("No change for device power state");
380 }
381 else
382 {
383 auto found = std::find_if(
384 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
385 [&s](const auto& iter) {
386 return (static_cast<uint8_t>(iter.second) == s);
387 });
388
389 value = found->first;
390
391 try
392 {
393 auto acpiObject =
394 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
395 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
396 acpi_state::acpiInterface,
397 acpi_state::devACPIProp,
398 convertForMessage(value));
399 }
400 catch (const InternalFailure& e)
401 {
402 log<level::ERR>("Failed in set ACPI device property",
403 entry("EXCEPTION=%s", e.what()));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000404 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800405 }
406 }
407 }
408 else
409 {
410 log<level::DEBUG>("Do not change device power state");
411 }
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000412 return ipmi::responseSuccess();
Yong Li18d77262018-10-09 01:59:45 +0800413}
414
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000415/**
416 * @brief implements the get ACPI power state command
417 *
418 * @return IPMI completion code plus response data on success.
419 * - ACPI system power state
420 * - ACPI device power state
421 **/
422ipmi::RspType<uint8_t, // acpiSystemPowerState
423 uint8_t // acpiDevicePowerState
424 >
425 ipmiGetAcpiPowerState()
Yong Li18d77262018-10-09 01:59:45 +0800426{
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000427 uint8_t sysAcpiState;
428 uint8_t devAcpiState;
Yong Li18d77262018-10-09 01:59:45 +0800429
430 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
431
Yong Li18d77262018-10-09 01:59:45 +0800432 try
433 {
434 auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface);
435
436 auto sysACPIVal = ipmi::getDbusProperty(
437 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
438 acpi_state::sysACPIProp);
439 auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700440 std::get<std::string>(sysACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000441 sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
Yong Li18d77262018-10-09 01:59:45 +0800442
443 auto devACPIVal = ipmi::getDbusProperty(
444 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
445 acpi_state::devACPIProp);
446 auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700447 std::get<std::string>(devACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000448 devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
Yong Li18d77262018-10-09 01:59:45 +0800449 }
450 catch (const InternalFailure& e)
451 {
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000452 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800453 }
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000454
455 return ipmi::responseSuccess(sysAcpiState, devAcpiState);
Chris Austen6caf28b2015-10-13 12:40:40 -0500456}
457
Chris Austen7303bdc2016-04-17 11:50:54 -0500458typedef struct
459{
460 char major;
461 char minor;
Chris Austen176c9652016-04-30 16:32:17 -0500462 uint16_t d[2];
Vernon Mauery86a50822019-03-25 13:11:36 -0700463} Revision;
Chris Austen7303bdc2016-04-17 11:50:54 -0500464
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600465/* Currently supports the vx.x-x-[-x] and v1.x.x-x-[-x] format. It will */
466/* return -1 if not in those formats, this routine knows how to parse */
Chris Austen7303bdc2016-04-17 11:50:54 -0500467/* version = v0.6-19-gf363f61-dirty */
468/* ^ ^ ^^ ^ */
469/* | | |----------|-- additional details */
470/* | |---------------- Minor */
471/* |------------------ Major */
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600472/* and version = v1.99.10-113-g65edf7d-r3-0-g9e4f715 */
473/* ^ ^ ^^ ^ */
474/* | | |--|---------- additional details */
475/* | |---------------- Minor */
476/* |------------------ Major */
Chris Austen7303bdc2016-04-17 11:50:54 -0500477/* Additional details : If the option group exists it will force Auxiliary */
478/* Firmware Revision Information 4th byte to 1 indicating the build was */
479/* derived with additional edits */
Vernon Mauery86a50822019-03-25 13:11:36 -0700480int convertVersion(std::string s, Revision& rev)
Chris Austen7303bdc2016-04-17 11:50:54 -0500481{
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600482 std::string token;
Chris Austen176c9652016-04-30 16:32:17 -0500483 uint16_t commits;
Chris Austen7303bdc2016-04-17 11:50:54 -0500484
Patrick Venture0b02be92018-08-31 11:55:55 -0700485 auto location = s.find_first_of('v');
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600486 if (location != std::string::npos)
487 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700488 s = s.substr(location + 1);
Chris Austen176c9652016-04-30 16:32:17 -0500489 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500490
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600491 if (!s.empty())
492 {
493 location = s.find_first_of(".");
494 if (location != std::string::npos)
495 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700496 rev.major =
Patrick Venture0b02be92018-08-31 11:55:55 -0700497 static_cast<char>(std::stoi(s.substr(0, location), 0, 16));
498 token = s.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600499 }
Chris Austen176c9652016-04-30 16:32:17 -0500500
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600501 if (!token.empty())
502 {
503 location = token.find_first_of(".-");
504 if (location != std::string::npos)
505 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700506 rev.minor = static_cast<char>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700507 std::stoi(token.substr(0, location), 0, 16));
508 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600509 }
510 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500511
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600512 // Capture the number of commits on top of the minor tag.
513 // I'm using BE format like the ipmi spec asked for
514 location = token.find_first_of(".-");
515 if (!token.empty())
516 {
517 commits = std::stoi(token.substr(0, location), 0, 16);
Vernon Mauery86a50822019-03-25 13:11:36 -0700518 rev.d[0] = (commits >> 8) | (commits << 8);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600519
520 // commit number we skip
521 location = token.find_first_of(".-");
522 if (location != std::string::npos)
523 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700524 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600525 }
526 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700527 else
528 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700529 rev.d[0] = 0;
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600530 }
531
532 if (location != std::string::npos)
533 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700534 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600535 }
536
537 // Any value of the optional parameter forces it to 1
538 location = token.find_first_of(".-");
539 if (location != std::string::npos)
540 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700541 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600542 }
543 commits = (!token.empty()) ? 1 : 0;
544
Patrick Venture0b02be92018-08-31 11:55:55 -0700545 // We do this operation to get this displayed in least significant bytes
546 // of ipmitool device id command.
Vernon Mauery86a50822019-03-25 13:11:36 -0700547 rev.d[1] = (commits >> 8) | (commits << 8);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600548 }
549
Chris Austen7303bdc2016-04-17 11:50:54 -0500550 return 0;
551}
552
Vernon Maueryea1c4012019-05-24 13:26:16 -0700553/* @brief: Implement the Get Device ID IPMI command per the IPMI spec
554 * @param[in] ctx - shared_ptr to an IPMI context struct
555 *
556 * @returns IPMI completion code plus response data
557 * - Device ID (manufacturer defined)
558 * - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit]
559 * - FW revision major[7 bits] (binary encoded); available[1 bit]
560 * - FW Revision minor (BCD encoded)
561 * - IPMI version (0x02 for IPMI 2.0)
562 * - device support (bitfield of supported options)
563 * - MFG IANA ID (3 bytes)
564 * - product ID (2 bytes)
565 * - AUX info (4 bytes)
566 */
567ipmi::RspType<uint8_t, // Device ID
568 uint8_t, // Device Revision
569 uint8_t, // Firmware Revision Major
570 uint8_t, // Firmware Revision minor
571 uint8_t, // IPMI version
572 uint8_t, // Additional device support
573 uint24_t, // MFG ID
574 uint16_t, // Product ID
575 uint32_t // AUX info
576 >
577 ipmiAppGetDeviceId(ipmi::Context::ptr ctx)
Chris Austen6caf28b2015-10-13 12:40:40 -0500578{
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600579 int r = -1;
Vernon Mauery86a50822019-03-25 13:11:36 -0700580 Revision rev = {0};
581 static struct
582 {
583 uint8_t id;
584 uint8_t revision;
585 uint8_t fw[2];
586 uint8_t ipmiVer;
587 uint8_t addnDevSupport;
588 uint24_t manufId;
589 uint16_t prodId;
590 uint32_t aux;
591 } devId;
David Cobbleya1adb072017-11-21 15:58:13 -0800592 static bool dev_id_initialized = false;
Patrick Venture94930a12019-04-30 10:01:58 -0700593 static bool defaultActivationSetting = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800594 const char* filename = "/usr/share/ipmi-providers/dev_id.json";
Alexander Amelkinba19c182018-09-04 15:49:36 +0300595 constexpr auto ipmiDevIdStateShift = 7;
596 constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
Chris Austen6caf28b2015-10-13 12:40:40 -0500597
David Cobbleya1adb072017-11-21 15:58:13 -0800598 if (!dev_id_initialized)
599 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600600 try
601 {
Vernon Maueryea1c4012019-05-24 13:26:16 -0700602 auto version = getActiveSoftwareVersionInfo(ctx);
Vernon Mauery86a50822019-03-25 13:11:36 -0700603 r = convertVersion(version, rev);
David Cobbleya1adb072017-11-21 15:58:13 -0800604 }
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600605 catch (const std::exception& e)
606 {
607 log<level::ERR>(e.what());
608 }
Nan Liee0cb902016-07-11 15:38:03 +0800609
Patrick Venture0b02be92018-08-31 11:55:55 -0700610 if (r >= 0)
611 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600612 // bit7 identifies if the device is available
613 // 0=normal operation
614 // 1=device firmware, SDR update,
615 // or self-initialization in progress.
Alexander Amelkinba19c182018-09-04 15:49:36 +0300616 // The availability may change in run time, so mask here
617 // and initialize later.
Vernon Mauery86a50822019-03-25 13:11:36 -0700618 devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600619
620 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Vernon Mauery86a50822019-03-25 13:11:36 -0700621 devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
622 std::memcpy(&devId.aux, rev.d, 4);
David Cobbleya1adb072017-11-21 15:58:13 -0800623 }
Nan Liee0cb902016-07-11 15:38:03 +0800624
David Cobbleya1adb072017-11-21 15:58:13 -0800625 // IPMI Spec version 2.0
Vernon Mauery86a50822019-03-25 13:11:36 -0700626 devId.ipmiVer = 2;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500627
Vernon Mauery86a50822019-03-25 13:11:36 -0700628 std::ifstream devIdFile(filename);
629 if (devIdFile.is_open())
David Cobbleya1adb072017-11-21 15:58:13 -0800630 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700631 auto data = nlohmann::json::parse(devIdFile, nullptr, false);
David Cobbleya1adb072017-11-21 15:58:13 -0800632 if (!data.is_discarded())
633 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700634 devId.id = data.value("id", 0);
635 devId.revision = data.value("revision", 0);
636 devId.addnDevSupport = data.value("addn_dev_support", 0);
637 devId.manufId = data.value("manuf_id", 0);
638 devId.prodId = data.value("prod_id", 0);
639 devId.aux = data.value("aux", 0);
David Cobbleya1adb072017-11-21 15:58:13 -0800640
Patrick Venture94930a12019-04-30 10:01:58 -0700641 // Set the availablitity of the BMC.
642 defaultActivationSetting = data.value("availability", true);
643
Patrick Venture0b02be92018-08-31 11:55:55 -0700644 // Don't read the file every time if successful
David Cobbleya1adb072017-11-21 15:58:13 -0800645 dev_id_initialized = true;
646 }
647 else
648 {
649 log<level::ERR>("Device ID JSON parser failure");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700650 return ipmi::responseUnspecifiedError();
David Cobbleya1adb072017-11-21 15:58:13 -0800651 }
652 }
653 else
654 {
655 log<level::ERR>("Device ID file not found");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700656 return ipmi::responseUnspecifiedError();
Chris Austen7303bdc2016-04-17 11:50:54 -0500657 }
658 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500659
Alexander Amelkinba19c182018-09-04 15:49:36 +0300660 // Set availability to the actual current BMC state
Vernon Mauery86a50822019-03-25 13:11:36 -0700661 devId.fw[0] &= ipmiDevIdFw1Mask;
Patrick Venture94930a12019-04-30 10:01:58 -0700662 if (!getCurrentBmcStateWithFallback(defaultActivationSetting))
Alexander Amelkinba19c182018-09-04 15:49:36 +0300663 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700664 devId.fw[0] |= (1 << ipmiDevIdStateShift);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300665 }
666
Vernon Mauery86a50822019-03-25 13:11:36 -0700667 return ipmi::responseSuccess(
668 devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer,
669 devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux);
Chris Austen6caf28b2015-10-13 12:40:40 -0500670}
671
Vernon Maueryb84a5282019-03-25 13:39:03 -0700672auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
Nan Li41fa24a2016-11-10 20:12:37 +0800673{
Nan Li41fa24a2016-11-10 20:12:37 +0800674 // Byte 2:
675 // 55h - No error.
Gunnar Mills8991dd62017-10-25 17:11:29 -0500676 // 56h - Self Test function not implemented in this controller.
Nan Li41fa24a2016-11-10 20:12:37 +0800677 // 57h - Corrupted or inaccesssible data or devices.
678 // 58h - Fatal hardware error.
679 // FFh - reserved.
680 // all other: Device-specific 'internal failure'.
681 // Byte 3:
682 // For byte 2 = 55h, 56h, FFh: 00h
683 // For byte 2 = 58h, all other: Device-specific
684 // For byte 2 = 57h: self-test error bitfield.
685 // Note: returning 57h does not imply that all test were run.
686 // [7] 1b = Cannot access SEL device.
687 // [6] 1b = Cannot access SDR Repository.
688 // [5] 1b = Cannot access BMC FRU device.
689 // [4] 1b = IPMB signal lines do not respond.
690 // [3] 1b = SDR Repository empty.
691 // [2] 1b = Internal Use Area of BMC FRU corrupted.
692 // [1] 1b = controller update 'boot block' firmware corrupted.
693 // [0] 1b = controller operational firmware corrupted.
Vernon Maueryb84a5282019-03-25 13:39:03 -0700694 constexpr uint8_t notImplemented = 0x56;
695 constexpr uint8_t zero = 0;
696 return ipmi::responseSuccess(notImplemented, zero);
Nan Li41fa24a2016-11-10 20:12:37 +0800697}
698
Vernon Mauery15541322019-03-25 13:33:03 -0700699static constexpr size_t uuidBinaryLength = 16;
700static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500701{
Vernon Mauery15541322019-03-25 13:33:03 -0700702 using Argument = xyz::openbmc_project::Common::InvalidArgument;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500703 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Ventured2117022018-02-06 08:54:37 -0800704 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
705 // order
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500706 // Ex: 0x2332fc2c40e66298e511f2782395a361
Vernon Mauery15541322019-03-25 13:33:03 -0700707 constexpr size_t uuidHexLength = (2 * uuidBinaryLength);
708 constexpr size_t uuidRfc4122Length = (uuidHexLength + 4);
709 std::array<uint8_t, uuidBinaryLength> uuid;
710 if (rfc4122.size() == uuidRfc4122Length)
Patrick Venture0b02be92018-08-31 11:55:55 -0700711 {
Vernon Mauery15541322019-03-25 13:33:03 -0700712 rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
713 rfc4122.end());
Sergey Solomineb9b8142016-08-23 09:07:28 -0500714 }
Vernon Mauery15541322019-03-25 13:33:03 -0700715 if (rfc4122.size() != uuidHexLength)
vishwa1eaea4f2016-02-26 11:57:40 -0600716 {
Vernon Mauery15541322019-03-25 13:33:03 -0700717 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
718 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
vishwa1eaea4f2016-02-26 11:57:40 -0600719 }
Vernon Mauery15541322019-03-25 13:33:03 -0700720 for (size_t ind = 0; ind < uuidHexLength; ind += 2)
vishwa1eaea4f2016-02-26 11:57:40 -0600721 {
Vernon Mauery15541322019-03-25 13:33:03 -0700722 char v[3];
723 v[0] = rfc4122[ind];
724 v[1] = rfc4122[ind + 1];
725 v[2] = 0;
726 size_t err;
727 long b;
728 try
Emily Shafferedb8bb02018-09-27 14:50:15 -0700729 {
Vernon Mauery15541322019-03-25 13:33:03 -0700730 b = std::stoul(v, &err, 16);
Emily Shafferedb8bb02018-09-27 14:50:15 -0700731 }
Vernon Mauery15541322019-03-25 13:33:03 -0700732 catch (std::exception& e)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500733 {
Vernon Mauery15541322019-03-25 13:33:03 -0700734 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
735 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500736 }
Vernon Mauery15541322019-03-25 13:33:03 -0700737 // check that exactly two ascii bytes were converted
738 if (err != 2)
739 {
740 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
741 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
742 }
743 uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500744 }
Vernon Mauery15541322019-03-25 13:33:03 -0700745 return uuid;
746}
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500747
Vernon Mauery15541322019-03-25 13:33:03 -0700748auto ipmiAppGetDeviceGuid()
749 -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>>
750{
751 // return a fixed GUID based on /etc/machine-id
752 // This should match the /redfish/v1/Managers/bmc's UUID data
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500753
Vernon Mauery15541322019-03-25 13:33:03 -0700754 // machine specific application ID (for BMC ID)
755 // generated by systemd-id128 -p new as per man page
756 static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE(
757 e0, e1, 73, 76, 64, 61, 47, da, a5, 0c, d0, cc, 64, 12, 45, 78);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500758
Vernon Mauery15541322019-03-25 13:33:03 -0700759 sd_id128_t bmcUuid;
760 // create the UUID from /etc/machine-id via the systemd API
761 sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500762
Vernon Mauery15541322019-03-25 13:33:03 -0700763 char bmcUuidCstr[SD_ID128_STRING_MAX];
764 std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr);
765
766 std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid);
767 return ipmi::responseSuccess(uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500768}
Chris Austen6caf28b2015-10-13 12:40:40 -0500769
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700770auto ipmiAppGetBtCapabilities()
771 -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530772{
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600773 // Per IPMI 2.0 spec, the input and output buffer size must be the max
774 // buffer size minus one byte to allocate space for the length byte.
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700775 constexpr uint8_t nrOutstanding = 0x01;
776 constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1;
777 constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1;
778 constexpr uint8_t transactionTime = 0x0A;
779 constexpr uint8_t nrRetries = 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530780
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700781 return ipmi::responseSuccess(nrOutstanding, inputBufferSize,
782 outputBufferSize, transactionTime, nrRetries);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530783}
784
Vernon Maueryb90a5322019-03-25 13:36:55 -0700785auto ipmiAppGetSystemGuid() -> ipmi::RspType<std::array<uint8_t, 16>>
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600786{
Vernon Maueryb90a5322019-03-25 13:36:55 -0700787 static constexpr auto bmcInterface =
788 "xyz.openbmc_project.Inventory.Item.Bmc";
789 static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
790 static constexpr auto uuidProperty = "UUID";
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600791
Vernon Maueryb90a5322019-03-25 13:36:55 -0700792 ipmi::Value propValue;
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600793 try
794 {
795 // Get the Inventory object implementing BMC interface
Vernon Maueryb90a5322019-03-25 13:36:55 -0700796 auto busPtr = getSdBus();
797 auto objectInfo = ipmi::getDbusObject(*busPtr, bmcInterface);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600798
799 // Read UUID property value from bmcObject
800 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Vernon Maueryb90a5322019-03-25 13:36:55 -0700801 propValue =
802 ipmi::getDbusProperty(*busPtr, objectInfo.second, objectInfo.first,
803 uuidInterface, uuidProperty);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600804 }
805 catch (const InternalFailure& e)
806 {
807 log<level::ERR>("Failed in reading BMC UUID property",
Vernon Maueryb90a5322019-03-25 13:36:55 -0700808 entry("INTERFACE=%s", uuidInterface),
809 entry("PROPERTY=%s", uuidProperty));
810 return ipmi::responseUnspecifiedError();
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600811 }
Vernon Maueryb90a5322019-03-25 13:36:55 -0700812 std::array<uint8_t, 16> uuid;
813 std::string rfc4122Uuid = std::get<std::string>(propValue);
814 try
815 {
816 // convert to IPMI format
817 uuid = rfc4122ToIpmi(rfc4122Uuid);
818 }
819 catch (const InvalidArgument& e)
820 {
821 log<level::ERR>("Failed in parsing BMC UUID property",
822 entry("INTERFACE=%s", uuidInterface),
823 entry("PROPERTY=%s", uuidProperty),
824 entry("VALUE=%s", rfc4122Uuid.c_str()));
825 return ipmi::responseUnspecifiedError();
826 }
827 return ipmi::responseSuccess(uuid);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600828}
829
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000830/**
831 * @brief set the session state as teardown
832 *
833 * This function is to set the session state to tear down in progress if the
834 * state is active.
835 *
836 * @param[in] busp - Dbus obj
837 * @param[in] service - service name
838 * @param[in] obj - object path
839 *
840 * @return success completion code if it sets the session state to
841 * tearDownInProgress else return the corresponding error completion code.
842 **/
843uint8_t setSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp,
844 const std::string& service, const std::string& obj)
845{
846 try
847 {
848 uint8_t sessionState = std::get<uint8_t>(ipmi::getDbusProperty(
849 *busp, service, obj, session::sessionIntf, "State"));
850
851 if (sessionState == static_cast<uint8_t>(session::State::active))
852 {
853 ipmi::setDbusProperty(
854 *busp, service, obj, session::sessionIntf, "State",
855 static_cast<uint8_t>(session::State::tearDownInProgress));
856 return ipmi::ccSuccess;
857 }
858 }
859 catch (std::exception& e)
860 {
861 log<level::ERR>("Failed in getting session state property",
862 entry("service=%s", service.c_str()),
863 entry("object path=%s", obj.c_str()),
864 entry("interface=%s", session::sessionIntf));
865 return ipmi::ccUnspecifiedError;
866 }
867
868 return ipmi::ccInvalidFieldRequest;
869}
870
871ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId,
872 std::optional<uint8_t> requestSessionHandle)
873{
874 auto busp = getSdBus();
875 uint8_t reqSessionHandle =
876 requestSessionHandle.value_or(session::defaultSessionHandle);
877
878 if (reqSessionId == session::sessionZero &&
879 reqSessionHandle == session::defaultSessionHandle)
880 {
881 return ipmi::response(session::ccInvalidSessionId);
882 }
883
884 if (reqSessionId == session::sessionZero &&
885 reqSessionHandle == session::invalidSessionHandle)
886 {
887 return ipmi::response(session::ccInvalidSessionHandle);
888 }
889
890 if (reqSessionId != session::sessionZero &&
891 reqSessionHandle != session::defaultSessionHandle)
892 {
893 return ipmi::response(ipmi::ccInvalidFieldRequest);
894 }
895
896 try
897 {
898 ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
899 *busp, session::sessionManagerRootPath, session::sessionIntf);
900
901 for (auto& objectTreeItr : objectTree)
902 {
903 const std::string obj = objectTreeItr.first;
904
905 if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle))
906 {
907 auto& serviceMap = objectTreeItr.second;
908
909 // Session id and session handle are unique for each session.
910 // Session id and handler are retrived from the object path and
911 // object path will be unique for each session. Checking if
912 // multiple objects exist with same object path under multiple
913 // services.
914 if (serviceMap.size() != 1)
915 {
916 return ipmi::responseUnspecifiedError();
917 }
918
919 auto itr = serviceMap.begin();
920 const std::string service = itr->first;
921 return ipmi::response(setSessionState(busp, service, obj));
922 }
923 }
924 }
925 catch (sdbusplus::exception::SdBusError& e)
926 {
927 log<level::ERR>("Failed to fetch object from dbus",
928 entry("INTERFACE=%s", session::sessionIntf),
929 entry("ERRMSG=%s", e.what()));
930 return ipmi::responseUnspecifiedError();
931 }
932
933 return ipmi::responseInvalidFieldRequest();
934}
935
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000936uint8_t getTotalSessionCount()
937{
938 uint8_t count = 0, ch = 1;
939
940 while (ch < ipmi::maxIpmiChannels &&
941 count < session::maxNetworkInstanceSupported)
942 {
943 ipmi::ChannelInfo chInfo;
944 ipmi::getChannelInfo(ch, chInfo);
945 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
946 ipmi::EChannelMediumType::lan8032)
947 {
948 count++;
949 }
950 ch++;
951 }
952 return count * session::maxSessionCountPerChannel;
953}
954
955/**
956 * @brief get session info request data.
957 *
958 * This function validates the request data and retrive request session id,
959 * session handle.
960 *
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +0530961 * @param[in] ctx - context of current session.
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000962 * @param[in] sessionIndex - request session index
963 * @param[in] payload - input payload
964 * @param[in] reqSessionId - unpacked session Id will be asigned
965 * @param[in] reqSessionHandle - unpacked session handle will be asigned
966 *
967 * @return success completion code if request data is valid
968 * else return the correcponding error completion code.
969 **/
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +0530970uint8_t getSessionInfoRequestData(const ipmi::Context::ptr ctx,
971 const uint8_t sessionIndex,
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000972 ipmi::message::Payload& payload,
973 uint32_t& reqSessionId,
974 uint8_t& reqSessionHandle)
975{
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +0530976 if ((sessionIndex > session::maxSessionCountPerChannel) &&
977 (sessionIndex < session::searchSessionByHandle))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000978 {
979 return ipmi::ccInvalidFieldRequest;
980 }
981
982 switch (sessionIndex)
983 {
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +0530984 case session::searchCurrentSession:
985
986 ipmi::ChannelInfo chInfo;
987 ipmi::getChannelInfo(ctx->channel, chInfo);
988
989 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) !=
990 ipmi::EChannelMediumType::lan8032)
991 {
992 return ipmi::ccInvalidFieldRequest;
993 }
994
995 if (!payload.fullyUnpacked())
996 {
997 return ipmi::ccReqDataLenInvalid;
998 }
999 // Check if current sessionId is 0, sessionId 0 is reserved.
1000 if (ctx->sessionId == session::sessionZero)
1001 {
1002 return session::ccInvalidSessionId;
1003 }
1004 reqSessionId = ctx->sessionId;
1005 break;
1006
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001007 case session::searchSessionByHandle:
1008
1009 if ((payload.unpack(reqSessionHandle)) ||
1010 (!payload.fullyUnpacked()))
1011 {
1012 return ipmi::ccReqDataLenInvalid;
1013 }
1014
1015 if ((reqSessionHandle == session::sessionZero) ||
1016 ((reqSessionHandle & session::multiIntfaceSessionHandleMask) >
1017 session::maxSessionCountPerChannel))
1018 {
1019 return session::ccInvalidSessionHandle;
1020 }
1021 break;
1022
1023 case session::searchSessionById:
1024
1025 if ((payload.unpack(reqSessionId)) || (!payload.fullyUnpacked()))
1026 {
1027 return ipmi::ccReqDataLenInvalid;
1028 }
1029
1030 if (reqSessionId == session::sessionZero)
1031 {
1032 return session::ccInvalidSessionId;
1033 }
1034 break;
1035
1036 default:
1037 if (!payload.fullyUnpacked())
1038 {
1039 return ipmi::ccReqDataLenInvalid;
1040 }
1041 break;
1042 }
1043 return ipmi::ccSuccess;
1044}
1045
1046uint8_t getSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp,
1047 const std::string& service, const std::string& objPath,
1048 uint8_t& sessionState)
1049{
1050 try
1051 {
1052 sessionState = std::get<uint8_t>(ipmi::getDbusProperty(
1053 *busp, service, objPath, session::sessionIntf, "State"));
1054 }
1055 catch (sdbusplus::exception::SdBusError& e)
1056 {
1057 log<level::ERR>("Failed to fetch state property ",
1058 entry("SERVICE=%s", service.c_str()),
1059 entry("OBJECTPATH=%s", objPath.c_str()),
1060 entry("INTERFACE=%s", session::sessionIntf),
1061 entry("ERRMSG=%s", e.what()));
1062 return ipmi::ccUnspecifiedError;
1063 }
1064
1065 return ipmi::ccSuccess;
1066}
1067
1068static constexpr uint8_t macAddrLen = 6;
1069struct GetSessionInfoRes
1070{
1071 uint8_t sessionHandle;
1072 uint8_t totalSessionCount;
1073 uint8_t activeSessionCount;
1074 uint8_t userID;
1075 uint8_t privLevel;
1076 uint8_t channelNumber;
1077 uint32_t remoteIpAddr;
1078 std::array<uint8_t, macAddrLen> macAddr = {0};
1079 uint16_t remotePort;
1080};
1081
1082uint8_t
1083 fillGetSessionInfoRes(std::shared_ptr<sdbusplus::asio::connection>& busp,
1084 const std::string& service,
1085 const std::string& objPath,
1086 struct GetSessionInfoRes& resp, uint8_t& sessionState)
1087{
1088 try
1089 {
1090 ipmi::PropertyMap sessionProps = ipmi::getAllDbusProperties(
1091 *busp, service, objPath, session::sessionIntf);
1092
1093 sessionState = std::get<uint8_t>(sessionProps.at("State"));
1094 if (sessionState == static_cast<uint8_t>(session::State::active))
1095 {
1096 resp.sessionHandle =
1097 std::get<uint8_t>(sessionProps["SessionHandle"]);
1098 resp.userID = std::get<uint8_t>(sessionProps["UserID"]);
1099 resp.privLevel =
1100 std::get<uint8_t>(sessionProps["CurrentPrivilege"]);
1101 resp.channelNumber = std::get<uint8_t>(sessionProps["ChannelNum"]);
1102 resp.remoteIpAddr =
1103 std::get<uint32_t>(sessionProps["RemoteIPAddr"]);
1104 resp.remotePort = std::get<uint16_t>(sessionProps["RemotePort"]);
1105 }
1106 }
1107 catch (sdbusplus::exception::SdBusError& e)
1108 {
1109 log<level::ERR>("Failed to fetch state property ",
1110 entry("SERVICE=%s", service.c_str()),
1111 entry("OBJECTPATH=%s", objPath.c_str()),
1112 entry("INTERFACE=%s", session::sessionIntf),
1113 entry("ERRMSG=%s", e.what()));
1114 return ipmi::ccUnspecifiedError;
1115 }
1116
1117 return ipmi::ccSuccess;
1118}
1119
1120ipmi::RspType<
1121 uint8_t, // session handle,
1122 uint8_t, // total session count
1123 uint8_t, // active session count
1124 std::optional<std::tuple<uint8_t, // user ID
1125 uint8_t, // privilege level
1126 uint8_t, // channel number
1127 uint32_t, // remote ip address,
1128 std::array<uint8_t, macAddrLen>, // mac address
1129 uint16_t // remote port
1130 >>>
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301131 ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex,
1132 ipmi::message::Payload& payload)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001133{
1134 uint32_t reqSessionId = 0;
1135 uint8_t reqSessionHandle = session::defaultSessionHandle;
1136 // initializing state to 0xff as 0 represents state as inactive.
1137 uint8_t state = 0xFF;
1138
1139 uint8_t completionCode = getSessionInfoRequestData(
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301140 ctx, sessionIndex, payload, reqSessionId, reqSessionHandle);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001141
1142 if (completionCode)
1143 {
1144 return ipmi::response(completionCode);
1145 }
1146 struct GetSessionInfoRes res = {0};
1147 res.totalSessionCount = getTotalSessionCount();
1148 res.activeSessionCount = 0;
1149 auto busp = getSdBus();
1150
1151 try
1152 {
1153 uint8_t index = 0;
1154 ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
1155 *busp, session::sessionManagerRootPath, session::sessionIntf);
1156
1157 for (auto& objectTreeItr : objectTree)
1158 {
1159 uint32_t sessionId = 0;
1160 uint8_t sessionHandle = session::defaultSessionHandle;
1161 std::string objectPath = objectTreeItr.first;
1162
1163 if (!parseCloseSessionInputPayload(objectPath, sessionId,
1164 sessionHandle))
1165 {
1166 continue;
1167 }
1168 index++;
1169 auto& serviceMap = objectTreeItr.second;
1170 auto itr = serviceMap.begin();
1171
1172 if (serviceMap.size() != 1)
1173 {
1174 return ipmi::responseUnspecifiedError();
1175 }
1176
1177 std::string service = itr->first;
1178 uint8_t sessionState = 0;
1179 completionCode =
1180 getSessionState(busp, service, objectPath, sessionState);
1181 if (completionCode)
1182 {
1183 return ipmi::response(completionCode);
1184 }
1185
1186 if (sessionState == static_cast<uint8_t>(session::State::active))
1187 {
1188 res.activeSessionCount++;
1189 }
1190
1191 if (index != sessionIndex && reqSessionId != sessionId &&
1192 reqSessionHandle != sessionHandle)
1193 {
1194 continue;
1195 }
1196
1197 completionCode =
1198 fillGetSessionInfoRes(busp, service, objectPath, res, state);
1199
1200 if (completionCode)
1201 {
1202 return ipmi::response(completionCode);
1203 }
1204 }
1205 }
1206
1207 catch (sdbusplus::exception::SdBusError& e)
1208 {
1209 log<level::ERR>("Failed to fetch object from dbus",
1210 entry("INTERFACE=%s", session::sessionIntf),
1211 entry("ERRMSG=%s", e.what()));
1212 return ipmi::responseUnspecifiedError();
1213 }
1214
1215 if (state == static_cast<uint8_t>(session::State::active))
1216 {
1217 return ipmi::responseSuccess(
1218 res.sessionHandle, res.totalSessionCount, res.activeSessionCount,
1219 std::make_tuple(res.userID, res.privLevel, res.channelNumber,
1220 res.remoteIpAddr, res.macAddr, res.remotePort));
1221 }
1222 else if (state == static_cast<uint8_t>(session::State::tearDownInProgress))
1223 {
1224 res.sessionHandle = 0;
1225 return ipmi::responseSuccess(res.sessionHandle, res.totalSessionCount,
1226 res.activeSessionCount, std::nullopt);
1227 }
1228
1229 return ipmi::responseInvalidFieldRequest();
1230}
1231
Xo Wangf542e8b2017-08-09 15:34:16 -07001232static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
1233
Xo Wang87651332017-08-11 10:17:59 -07001234static std::string sysInfoReadSystemName()
1235{
1236 // Use the BMC hostname as the "System Name."
1237 char hostname[HOST_NAME_MAX + 1] = {};
1238 if (gethostname(hostname, HOST_NAME_MAX) != 0)
1239 {
1240 perror("System info parameter: system name");
1241 }
1242 return hostname;
1243}
1244
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001245static constexpr uint8_t revisionOnly = 0x80;
1246static constexpr uint8_t paramRevision = 0x11;
1247static constexpr size_t configParameterLength = 16;
Xo Wangf542e8b2017-08-09 15:34:16 -07001248
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001249static constexpr size_t smallChunkSize = 14;
1250static constexpr size_t fullChunkSize = 16;
Jia, chunhui449f2162019-09-11 16:51:51 +08001251static constexpr uint8_t progressMask = 0x3;
Xo Wangf542e8b2017-08-09 15:34:16 -07001252
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001253static constexpr uint8_t setComplete = 0x0;
1254static constexpr uint8_t setInProgress = 0x1;
1255static constexpr uint8_t commitWrite = 0x2;
1256static uint8_t transferStatus = setComplete;
1257
Jia, chunhui449f2162019-09-11 16:51:51 +08001258static constexpr uint8_t configDataOverhead = 2;
1259
1260// For EFI based system, 256 bytes is recommended.
1261static constexpr size_t maxBytesPerParameter = 256;
1262
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001263namespace ipmi
1264{
1265constexpr Cc ccParmNotSupported = 0x80;
Jia, chunhui449f2162019-09-11 16:51:51 +08001266constexpr Cc ccSetInProgressActive = 0x81;
1267constexpr Cc ccSystemInfoParameterSetReadOnly = 0x82;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001268
1269static inline auto responseParmNotSupported()
1270{
1271 return response(ccParmNotSupported);
Xo Wangf542e8b2017-08-09 15:34:16 -07001272}
Jia, chunhui449f2162019-09-11 16:51:51 +08001273static inline auto responseSetInProgressActive()
1274{
1275 return response(ccSetInProgressActive);
1276}
1277static inline auto responseSystemInfoParameterSetReadOnly()
1278{
1279 return response(ccSystemInfoParameterSetReadOnly);
1280}
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001281} // namespace ipmi
Xo Wangf542e8b2017-08-09 15:34:16 -07001282
Jia, chunhui449f2162019-09-11 16:51:51 +08001283ipmi::RspType<uint8_t, // Parameter revision
1284 std::optional<uint8_t>, // data1 / setSelector / ProgressStatus
1285 std::optional<std::vector<uint8_t>>> // data2-17
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001286 ipmiAppGetSystemInfo(uint8_t getRevision, uint8_t paramSelector,
1287 uint8_t setSelector, uint8_t BlockSelector)
Xo Wangf542e8b2017-08-09 15:34:16 -07001288{
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001289 if (getRevision & revisionOnly)
Xo Wangf542e8b2017-08-09 15:34:16 -07001290 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001291 return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001292 }
1293
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001294 if (paramSelector == 0)
Xo Wangf542e8b2017-08-09 15:34:16 -07001295 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001296 return ipmi::responseSuccess(paramRevision, transferStatus,
1297 std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001298 }
1299
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001300 if (BlockSelector != 0) // 00h if parameter does not require a block number
Xo Wangf542e8b2017-08-09 15:34:16 -07001301 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001302 return ipmi::responseParmNotSupported();
Xo Wangf542e8b2017-08-09 15:34:16 -07001303 }
1304
1305 if (sysInfoParamStore == nullptr)
1306 {
1307 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
Xo Wang87651332017-08-11 10:17:59 -07001308 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1309 sysInfoReadSystemName);
Xo Wangf542e8b2017-08-09 15:34:16 -07001310 }
1311
1312 // Parameters other than Set In Progress are assumed to be strings.
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001313 std::tuple<bool, std::string> ret =
1314 sysInfoParamStore->lookup(paramSelector);
1315 bool found = std::get<0>(ret);
Xo Wangf542e8b2017-08-09 15:34:16 -07001316 if (!found)
1317 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001318 return ipmi::responseParmNotSupported();
Xo Wangf542e8b2017-08-09 15:34:16 -07001319 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001320 std::string& paramString = std::get<1>(ret);
Jia, chunhui449f2162019-09-11 16:51:51 +08001321 std::vector<uint8_t> configData;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001322 size_t count = 0;
1323 if (setSelector == 0)
Jia, chunhui449f2162019-09-11 16:51:51 +08001324 { // First chunk has only 14 bytes.
1325 configData.emplace_back(0); // encoding
1326 configData.emplace_back(paramString.length()); // string length
1327 count = std::min(paramString.length(), smallChunkSize);
1328 configData.resize(count + configDataOverhead);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001329 std::copy_n(paramString.begin(), count,
Jia, chunhui449f2162019-09-11 16:51:51 +08001330 configData.begin() + configDataOverhead); // 14 bytes thunk
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001331 }
1332 else
Xo Wangf542e8b2017-08-09 15:34:16 -07001333 {
Jia, chunhui449f2162019-09-11 16:51:51 +08001334 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001335 if (offset >= paramString.length())
1336 {
1337 return ipmi::responseParmOutOfRange();
1338 }
Jia, chunhui449f2162019-09-11 16:51:51 +08001339 count = std::min(paramString.length() - offset, fullChunkSize);
1340 configData.resize(count);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001341 std::copy_n(paramString.begin() + offset, count,
1342 configData.begin()); // 16 bytes chunk
Xo Wangf542e8b2017-08-09 15:34:16 -07001343 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001344 return ipmi::responseSuccess(paramRevision, setSelector, configData);
Xo Wangf542e8b2017-08-09 15:34:16 -07001345}
1346
Jia, chunhui449f2162019-09-11 16:51:51 +08001347ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1,
1348 std::vector<uint8_t> configData)
1349{
1350 if (paramSelector == 0)
1351 {
1352 // attempt to set the 'set in progress' value (in parameter #0)
1353 // when not in the set complete state.
1354 if ((transferStatus != setComplete) && (data1 == setInProgress))
1355 {
1356 return ipmi::responseSetInProgressActive();
1357 }
1358 // only following 2 states are supported
1359 if (data1 > setInProgress)
1360 {
1361 phosphor::logging::log<phosphor::logging::level::ERR>(
1362 "illegal SetInProgress status");
1363 return ipmi::responseInvalidFieldRequest();
1364 }
1365
1366 transferStatus = data1 & progressMask;
1367 return ipmi::responseSuccess();
1368 }
1369
1370 if (configData.size() > configParameterLength)
1371 {
1372 return ipmi::responseInvalidFieldRequest();
1373 }
1374
1375 if (!sysInfoParamStore)
1376 {
1377 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1378 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1379 sysInfoReadSystemName);
1380 }
1381
1382 // lookup
1383 std::tuple<bool, std::string> ret =
1384 sysInfoParamStore->lookup(paramSelector);
1385 bool found = std::get<0>(ret);
1386 std::string& paramString = std::get<1>(ret);
1387 if (!found)
1388 {
1389 // parameter does not exist. Init new
1390 paramString = "";
1391 }
1392
1393 uint8_t setSelector = data1;
1394 size_t count = 0;
1395 if (setSelector == 0) // First chunk has only 14 bytes.
1396 {
1397 size_t stringLen = configData.at(1); // string length
1398 // maxBytesPerParamter is 256. It will always be greater than stringLen
1399 // (unit8_t) if maxBytes changes in future, then following line is
1400 // needed.
1401 // stringLen = std::min(stringLen, maxBytesPerParameter);
1402 count = std::min(stringLen, smallChunkSize);
1403 count = std::min(count, configData.size());
1404 paramString.resize(stringLen); // reserve space
1405 std::copy_n(configData.begin() + configDataOverhead, count,
1406 paramString.begin());
1407 }
1408 else
1409 {
1410 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1411 if (offset >= paramString.length())
1412 {
1413 return ipmi::responseParmOutOfRange();
1414 }
1415 count = std::min(paramString.length() - offset, configData.size());
1416 std::copy_n(configData.begin(), count, paramString.begin() + offset);
1417 }
1418 sysInfoParamStore->update(paramSelector, paramString);
1419 return ipmi::responseSuccess();
1420}
1421
Yong Libd0503a2019-08-22 17:17:17 +08001422#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301423inline std::vector<uint8_t> convertStringToData(const std::string& command)
1424{
1425 std::istringstream iss(command);
1426 std::string token;
1427 std::vector<uint8_t> dataValue;
1428 while (std::getline(iss, token, ' '))
1429 {
1430 dataValue.emplace_back(
1431 static_cast<uint8_t>(std::stoul(token, nullptr, base_16)));
1432 }
1433 return dataValue;
1434}
1435
1436static bool populateI2CMasterWRWhitelist()
1437{
1438 nlohmann::json data = nullptr;
1439 std::ifstream jsonFile(i2cMasterWRWhitelistFile);
1440
1441 if (!jsonFile.good())
1442 {
1443 log<level::WARNING>("i2c white list file not found!",
1444 entry("FILE_NAME: %s", i2cMasterWRWhitelistFile));
1445 return false;
1446 }
1447
1448 try
1449 {
1450 data = nlohmann::json::parse(jsonFile, nullptr, false);
1451 }
1452 catch (nlohmann::json::parse_error& e)
1453 {
1454 log<level::ERR>("Corrupted i2c white list config file",
1455 entry("FILE_NAME: %s", i2cMasterWRWhitelistFile),
1456 entry("MSG: %s", e.what()));
1457 return false;
1458 }
1459
1460 try
1461 {
1462 // Example JSON Structure format
1463 // "filters": [
1464 // {
1465 // "Description": "Allow full read - ignore first byte write value
1466 // for 0x40 to 0x4F",
1467 // "busId": "0x01",
1468 // "slaveAddr": "0x40",
1469 // "slaveAddrMask": "0x0F",
1470 // "command": "0x00",
1471 // "commandMask": "0xFF"
1472 // },
1473 // {
1474 // "Description": "Allow full read - first byte match 0x05 and
1475 // ignore second byte",
1476 // "busId": "0x01",
1477 // "slaveAddr": "0x57",
1478 // "slaveAddrMask": "0x00",
1479 // "command": "0x05 0x00",
1480 // "commandMask": "0x00 0xFF"
1481 // },]
1482
1483 nlohmann::json filters = data[filtersStr].get<nlohmann::json>();
1484 std::vector<i2cMasterWRWhitelist>& whitelist = getWRWhitelist();
1485 for (const auto& it : filters.items())
1486 {
1487 nlohmann::json filter = it.value();
1488 if (filter.is_null())
1489 {
1490 log<level::ERR>(
1491 "Corrupted I2C master write read whitelist config file",
1492 entry("FILE_NAME: %s", i2cMasterWRWhitelistFile));
1493 return false;
1494 }
1495 const std::vector<uint8_t>& writeData =
1496 convertStringToData(filter[cmdStr].get<std::string>());
1497 const std::vector<uint8_t>& writeDataMask =
1498 convertStringToData(filter[cmdMaskStr].get<std::string>());
1499 if (writeDataMask.size() != writeData.size())
1500 {
1501 log<level::ERR>("I2C master write read whitelist filter "
1502 "mismatch for command & mask size");
1503 return false;
1504 }
1505 whitelist.push_back(
1506 {static_cast<uint8_t>(std::stoul(
1507 filter[busIdStr].get<std::string>(), nullptr, base_16)),
1508 static_cast<uint8_t>(
1509 std::stoul(filter[slaveAddrStr].get<std::string>(),
1510 nullptr, base_16)),
1511 static_cast<uint8_t>(
1512 std::stoul(filter[slaveAddrMaskStr].get<std::string>(),
1513 nullptr, base_16)),
1514 writeData, writeDataMask});
1515 }
1516 if (whitelist.size() != filters.size())
1517 {
1518 log<level::ERR>(
1519 "I2C master write read whitelist filter size mismatch");
1520 return false;
1521 }
1522 }
1523 catch (std::exception& e)
1524 {
1525 log<level::ERR>("I2C master write read whitelist unexpected exception",
1526 entry("ERROR=%s", e.what()));
1527 return false;
1528 }
1529 return true;
1530}
1531
1532static inline bool isWriteDataWhitelisted(const std::vector<uint8_t>& data,
1533 const std::vector<uint8_t>& dataMask,
1534 const std::vector<uint8_t>& writeData)
1535{
1536 std::vector<uint8_t> processedDataBuf(data.size());
1537 std::vector<uint8_t> processedReqBuf(dataMask.size());
1538 std::transform(writeData.begin(), writeData.end(), dataMask.begin(),
1539 processedReqBuf.begin(), std::bit_or<uint8_t>());
1540 std::transform(data.begin(), data.end(), dataMask.begin(),
1541 processedDataBuf.begin(), std::bit_or<uint8_t>());
1542
1543 return (processedDataBuf == processedReqBuf);
1544}
1545
1546static bool isCmdWhitelisted(uint8_t busId, uint8_t slaveAddr,
1547 std::vector<uint8_t>& writeData)
1548{
1549 std::vector<i2cMasterWRWhitelist>& whiteList = getWRWhitelist();
1550 for (const auto& wlEntry : whiteList)
1551 {
1552 if ((busId == wlEntry.busId) &&
1553 ((slaveAddr | wlEntry.slaveAddrMask) ==
1554 (wlEntry.slaveAddr | wlEntry.slaveAddrMask)))
1555 {
1556 const std::vector<uint8_t>& dataMask = wlEntry.dataMask;
1557 // Skip as no-match, if requested write data is more than the
1558 // write data mask size
1559 if (writeData.size() > dataMask.size())
1560 {
1561 continue;
1562 }
1563 if (isWriteDataWhitelisted(wlEntry.data, dataMask, writeData))
1564 {
1565 return true;
1566 }
1567 }
1568 }
1569 return false;
1570}
Yong Libd0503a2019-08-22 17:17:17 +08001571#else
1572static bool populateI2CMasterWRWhitelist()
1573{
1574 log<level::INFO>(
1575 "I2C_WHITELIST_CHECK is disabled, do not populate whitelist");
1576 return true;
1577}
1578#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301579
1580/** @brief implements master write read IPMI command which can be used for
1581 * low-level I2C/SMBus write, read or write-read access
1582 * @param isPrivateBus -to indicate private bus usage
1583 * @param busId - bus id
1584 * @param channelNum - channel number
1585 * @param reserved - skip 1 bit
1586 * @param slaveAddr - slave address
1587 * @param read count - number of bytes to be read
1588 * @param writeData - data to be written
1589 *
1590 * @returns IPMI completion code plus response data
1591 * - readData - i2c response data
1592 */
1593ipmi::RspType<std::vector<uint8_t>>
1594 ipmiMasterWriteRead(bool isPrivateBus, uint3_t busId, uint4_t channelNum,
1595 bool reserved, uint7_t slaveAddr, uint8_t readCount,
1596 std::vector<uint8_t> writeData)
1597{
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301598 if (readCount > maxIPMIWriteReadSize)
1599 {
1600 log<level::ERR>("Master write read command: Read count exceeds limit");
1601 return ipmi::responseParmOutOfRange();
1602 }
1603 const size_t writeCount = writeData.size();
1604 if (!readCount && !writeCount)
1605 {
1606 log<level::ERR>("Master write read command: Read & write count are 0");
1607 return ipmi::responseInvalidFieldRequest();
1608 }
Yong Libd0503a2019-08-22 17:17:17 +08001609#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301610 if (!isCmdWhitelisted(static_cast<uint8_t>(busId),
1611 static_cast<uint8_t>(slaveAddr), writeData))
1612 {
1613 log<level::ERR>("Master write read request blocked!",
1614 entry("BUS=%d", static_cast<uint8_t>(busId)),
1615 entry("ADDR=0x%x", static_cast<uint8_t>(slaveAddr)));
1616 return ipmi::responseInvalidFieldRequest();
1617 }
Yong Libd0503a2019-08-22 17:17:17 +08001618#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301619 std::vector<uint8_t> readBuf(readCount);
1620 std::string i2cBus =
1621 "/dev/i2c-" + std::to_string(static_cast<uint8_t>(busId));
1622
Yong Li7dc4ac02019-08-23 17:44:32 +08001623 ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(slaveAddr),
1624 writeData, readBuf);
1625 if (ret != ipmi::ccSuccess)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301626 {
Yong Li7dc4ac02019-08-23 17:44:32 +08001627 return ipmi::response(ret);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301628 }
1629 return ipmi::responseSuccess(readBuf);
1630}
1631
Chris Austen6caf28b2015-10-13 12:40:40 -05001632void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +05301633{
Vernon Mauery86a50822019-03-25 13:11:36 -07001634 // <Get Device ID>
1635 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1636 ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1637 ipmiAppGetDeviceId);
1638
Tom05732372016-09-06 17:21:23 +05301639 // <Get BT Interface Capabilities>
Vernon Mauerycc99ba42019-03-25 13:40:11 -07001640 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1641 ipmi::app::cmdGetBtIfaceCapabilities,
1642 ipmi::Privilege::User, ipmiAppGetBtCapabilities);
Chris Austen6caf28b2015-10-13 12:40:40 -05001643
Tom05732372016-09-06 17:21:23 +05301644 // <Reset Watchdog Timer>
Vernon Mauery11df4f62019-03-25 14:17:54 -07001645 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1646 ipmi::app::cmdResetWatchdogTimer,
1647 ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001648
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001649 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301650 ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User,
1651 ipmiAppGetSessionInfo);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001652
Tom05732372016-09-06 17:21:23 +05301653 // <Set Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001654 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1655 ipmi::app::cmdSetWatchdogTimer,
1656 ipmi::Privilege::Operator, ipmiSetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001657
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +00001658 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1659 ipmi::app::cmdCloseSession, ipmi::Privilege::Callback,
1660 ipmiAppCloseSession);
1661
William A. Kennington III73f44512018-02-09 15:28:46 -08001662 // <Get Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001663 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301664 ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User,
1665 ipmiGetWatchdogTimer);
William A. Kennington III73f44512018-02-09 15:28:46 -08001666
Tom05732372016-09-06 17:21:23 +05301667 // <Get Self Test Results>
Vernon Maueryb84a5282019-03-25 13:39:03 -07001668 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1669 ipmi::app::cmdGetSelfTestResults,
1670 ipmi::Privilege::User, ipmiAppGetSelfTestResults);
Nan Li41fa24a2016-11-10 20:12:37 +08001671
Tom05732372016-09-06 17:21:23 +05301672 // <Get Device GUID>
Vernon Mauery15541322019-03-25 13:33:03 -07001673 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1674 ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User,
1675 ipmiAppGetDeviceGuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -05001676
Tom05732372016-09-06 17:21:23 +05301677 // <Set ACPI Power State>
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +00001678 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1679 ipmi::app::cmdSetAcpiPowerState,
1680 ipmi::Privilege::Admin, ipmiSetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001681 // <Get ACPI Power State>
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +00001682 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1683 ipmi::app::cmdGetAcpiPowerState,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301684 ipmi::Privilege::User, ipmiGetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001685
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301686 // Note: For security reason, this command will be registered only when
1687 // there are proper I2C Master write read whitelist
1688 if (populateI2CMasterWRWhitelist())
1689 {
1690 // Note: For security reasons, registering master write read as admin
1691 // privilege command, even though IPMI 2.0 specification allows it as
1692 // operator privilege.
1693 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1694 ipmi::app::cmdMasterWriteRead,
1695 ipmi::Privilege::Admin, ipmiMasterWriteRead);
1696 }
1697
Marri Devender Rao5e007a52018-01-08 06:18:36 -06001698 // <Get System GUID Command>
Vernon Maueryb90a5322019-03-25 13:36:55 -07001699 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1700 ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
1701 ipmiAppGetSystemGuid);
Tom Joseph7cbe2282018-03-21 21:17:33 +05301702
1703 // <Get Channel Cipher Suites Command>
Ayushi Smriti5c3b72c2019-08-30 13:47:31 +00001704 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1705 ipmi::app::cmdGetChannelCipherSuites,
Vernon Mauery79b4eea2019-11-07 09:51:39 -08001706 ipmi::Privilege::None, getChannelCipherSuites);
Vernon Maueryd2a57de2019-03-25 12:45:28 -07001707
Xo Wangf542e8b2017-08-09 15:34:16 -07001708 // <Get System Info Command>
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001709 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1710 ipmi::app::cmdGetSystemInfoParameters,
1711 ipmi::Privilege::User, ipmiAppGetSystemInfo);
Jia, chunhui449f2162019-09-11 16:51:51 +08001712 // <Set System Info Command>
1713 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1714 ipmi::app::cmdSetSystemInfoParameters,
1715 ipmi::Privilege::Admin, ipmiAppSetSystemInfo);
vishwabmcba0bd5f2015-09-30 16:50:23 +05301716 return;
1717}