blob: 8c81dc8263500bf85ca665d3cde08ea12a9ebcb0 [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
Joshi-Mansi7fd91fa2021-06-11 07:18:15 +053091static constexpr uint8_t maxIPMIWriteReadSize = 255;
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +000092static constexpr uint8_t oemCmdStart = 192;
93static constexpr uint8_t oemCmdEnd = 255;
94static constexpr uint8_t invalidParamSelectorStart = 8;
95static constexpr uint8_t invalidParamSelectorEnd = 191;
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +053096
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060097/**
98 * @brief Returns the Version info from primary s/w object
99 *
100 * Get the Version info from the active s/w object which is having high
101 * "Priority" value(a smaller number is a higher priority) and "Purpose"
102 * is "BMC" from the list of all s/w objects those are implementing
103 * RedundancyPriority interface from the given softwareRoot path.
104 *
105 * @return On success returns the Version info from primary s/w object.
106 *
107 */
Vernon Maueryea1c4012019-05-24 13:26:16 -0700108std::string getActiveSoftwareVersionInfo(ipmi::Context::ptr ctx)
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600109{
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600110 std::string revision{};
Vernon Mauery86a50822019-03-25 13:11:36 -0700111 ipmi::ObjectTree objectTree;
112 try
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600113 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700114 objectTree =
Vernon Maueryea1c4012019-05-24 13:26:16 -0700115 ipmi::getAllDbusObjects(*ctx->bus, softwareRoot, redundancyIntf);
Vernon Mauery86a50822019-03-25 13:11:36 -0700116 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500117 catch (const sdbusplus::exception::exception& e)
Vernon Mauery86a50822019-03-25 13:11:36 -0700118 {
119 log<level::ERR>("Failed to fetch redundancy object from dbus",
120 entry("INTERFACE=%s", redundancyIntf),
121 entry("ERRMSG=%s", e.what()));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600122 elog<InternalFailure>();
123 }
124
125 auto objectFound = false;
126 for (auto& softObject : objectTree)
127 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700128 auto service =
Vernon Maueryea1c4012019-05-24 13:26:16 -0700129 ipmi::getService(*ctx->bus, redundancyIntf, softObject.first);
Vernon Mauery86a50822019-03-25 13:11:36 -0700130 auto objValueTree =
Vernon Maueryea1c4012019-05-24 13:26:16 -0700131 ipmi::getManagedObjects(*ctx->bus, service, softwareRoot);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600132
133 auto minPriority = 0xFF;
134 for (const auto& objIter : objValueTree)
135 {
136 try
137 {
138 auto& intfMap = objIter.second;
139 auto& redundancyPriorityProps = intfMap.at(redundancyIntf);
140 auto& versionProps = intfMap.at(versionIntf);
141 auto& activationProps = intfMap.at(activationIntf);
Vernon Maueryf442e112019-04-09 11:44:36 -0700142 auto priority =
143 std::get<uint8_t>(redundancyPriorityProps.at("Priority"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700144 auto purpose =
Vernon Maueryf442e112019-04-09 11:44:36 -0700145 std::get<std::string>(versionProps.at("Purpose"));
146 auto activation =
147 std::get<std::string>(activationProps.at("Activation"));
William A. Kennington III4c008022018-10-12 17:18:14 -0700148 auto version =
Vernon Maueryf442e112019-04-09 11:44:36 -0700149 std::get<std::string>(versionProps.at("Version"));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600150 if ((Version::convertVersionPurposeFromString(purpose) ==
151 Version::VersionPurpose::BMC) &&
152 (Activation::convertActivationsFromString(activation) ==
153 Activation::Activations::Active))
154 {
155 if (priority < minPriority)
156 {
157 minPriority = priority;
158 objectFound = true;
159 revision = std::move(version);
160 }
161 }
162 }
163 catch (const std::exception& e)
164 {
165 log<level::ERR>(e.what());
166 }
167 }
168 }
169
170 if (!objectFound)
171 {
172 log<level::ERR>("Could not found an BMC software Object");
173 elog<InternalFailure>();
174 }
175
176 return revision;
177}
178
Alexander Amelkinba19c182018-09-04 15:49:36 +0300179bool getCurrentBmcState()
180{
181 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
182
183 // Get the Inventory object implementing the BMC interface
184 ipmi::DbusObjectInfo bmcObject =
185 ipmi::getDbusObject(bus, bmc_state_interface);
186 auto variant =
187 ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
188 bmc_state_interface, bmc_state_property);
189
Vernon Maueryf442e112019-04-09 11:44:36 -0700190 return std::holds_alternative<std::string>(variant) &&
191 BMC::convertBMCStateFromString(std::get<std::string>(variant)) ==
192 BMC::BMCState::Ready;
Alexander Amelkinba19c182018-09-04 15:49:36 +0300193}
194
Patrick Venture94930a12019-04-30 10:01:58 -0700195bool getCurrentBmcStateWithFallback(const bool fallbackAvailability)
196{
197 try
198 {
199 return getCurrentBmcState();
200 }
201 catch (...)
202 {
203 // Nothing provided the BMC interface, therefore return whatever was
204 // configured as the default.
205 return fallbackAvailability;
206 }
207}
208
Yong Li18d77262018-10-09 01:59:45 +0800209namespace acpi_state
210{
211using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
212
213const static constexpr char* acpiObjPath =
214 "/xyz/openbmc_project/control/host0/acpi_power_state";
215const static constexpr char* acpiInterface =
216 "xyz.openbmc_project.Control.Power.ACPIPowerState";
217const static constexpr char* sysACPIProp = "SysACPIStatus";
218const static constexpr char* devACPIProp = "DevACPIStatus";
219
220enum class PowerStateType : uint8_t
221{
222 sysPowerState = 0x00,
223 devPowerState = 0x01,
224};
225
226// Defined in 20.6 of ipmi doc
227enum class PowerState : uint8_t
228{
229 s0G0D0 = 0x00,
230 s1D1 = 0x01,
231 s2D2 = 0x02,
232 s3D3 = 0x03,
233 s4 = 0x04,
234 s5G2 = 0x05,
235 s4S5 = 0x06,
236 g3 = 0x07,
237 sleep = 0x08,
238 g1Sleep = 0x09,
239 override = 0x0a,
240 legacyOn = 0x20,
241 legacyOff = 0x21,
242 unknown = 0x2a,
243 noChange = 0x7f,
244};
245
246static constexpr uint8_t stateChanged = 0x80;
247
Yong Li18d77262018-10-09 01:59:45 +0800248std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = {
249 {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0},
250 {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1},
251 {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2},
252 {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3},
253 {ACPIPowerState::ACPI::S4, PowerState::s4},
254 {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2},
255 {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5},
256 {ACPIPowerState::ACPI::G3, PowerState::g3},
257 {ACPIPowerState::ACPI::SLEEP, PowerState::sleep},
258 {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep},
259 {ACPIPowerState::ACPI::OVERRIDE, PowerState::override},
260 {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn},
261 {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff},
262 {ACPIPowerState::ACPI::Unknown, PowerState::unknown}};
263
264bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state)
265{
266 if (type == acpi_state::PowerStateType::sysPowerState)
267 {
268 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) ||
269 (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) ||
270 (state ==
271 static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) ||
272 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
273 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
274 {
275 return true;
276 }
277 else
278 {
279 return false;
280 }
281 }
282 else if (type == acpi_state::PowerStateType::devPowerState)
283 {
284 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) ||
285 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
286 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
287 {
288 return true;
289 }
290 else
291 {
292 return false;
293 }
294 }
295 else
296 {
297 return false;
298 }
299 return false;
300}
301} // namespace acpi_state
302
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000303/** @brief implements Set ACPI Power State command
304 * @param sysAcpiState - ACPI system power state to set
305 * @param devAcpiState - ACPI device power state to set
306 *
307 * @return IPMI completion code on success
308 **/
309ipmi::RspType<> ipmiSetAcpiPowerState(uint8_t sysAcpiState,
310 uint8_t devAcpiState)
Chris Austen6caf28b2015-10-13 12:40:40 -0500311{
Yong Li18d77262018-10-09 01:59:45 +0800312 auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
Yong Li18d77262018-10-09 01:59:45 +0800313
314 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
315
316 auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
317
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000318 if (sysAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800319 {
320 // set system power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000321 s = sysAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800322
323 if (!acpi_state::isValidACPIState(
324 acpi_state::PowerStateType::sysPowerState, s))
325 {
326 log<level::ERR>("set_acpi_power sys invalid input",
327 entry("S=%x", s));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000328 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800329 }
330
331 // valid input
332 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
333 {
334 log<level::DEBUG>("No change for system power state");
335 }
336 else
337 {
338 auto found = std::find_if(
339 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
340 [&s](const auto& iter) {
341 return (static_cast<uint8_t>(iter.second) == s);
342 });
343
344 value = found->first;
345
346 try
347 {
348 auto acpiObject =
349 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
350 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
351 acpi_state::acpiInterface,
352 acpi_state::sysACPIProp,
353 convertForMessage(value));
354 }
355 catch (const InternalFailure& e)
356 {
357 log<level::ERR>("Failed in set ACPI system property",
358 entry("EXCEPTION=%s", e.what()));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000359 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800360 }
361 }
362 }
363 else
364 {
365 log<level::DEBUG>("Do not change system power state");
366 }
367
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000368 if (devAcpiState & acpi_state::stateChanged)
Yong Li18d77262018-10-09 01:59:45 +0800369 {
370 // set device power state
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000371 s = devAcpiState & ~acpi_state::stateChanged;
Yong Li18d77262018-10-09 01:59:45 +0800372 if (!acpi_state::isValidACPIState(
373 acpi_state::PowerStateType::devPowerState, s))
374 {
375 log<level::ERR>("set_acpi_power dev invalid input",
376 entry("S=%x", s));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000377 return ipmi::responseParmOutOfRange();
Yong Li18d77262018-10-09 01:59:45 +0800378 }
379
380 // valid input
381 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
382 {
383 log<level::DEBUG>("No change for device power state");
384 }
385 else
386 {
387 auto found = std::find_if(
388 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
389 [&s](const auto& iter) {
390 return (static_cast<uint8_t>(iter.second) == s);
391 });
392
393 value = found->first;
394
395 try
396 {
397 auto acpiObject =
398 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
399 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
400 acpi_state::acpiInterface,
401 acpi_state::devACPIProp,
402 convertForMessage(value));
403 }
404 catch (const InternalFailure& e)
405 {
406 log<level::ERR>("Failed in set ACPI device property",
407 entry("EXCEPTION=%s", e.what()));
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000408 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800409 }
410 }
411 }
412 else
413 {
414 log<level::DEBUG>("Do not change device power state");
415 }
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +0000416 return ipmi::responseSuccess();
Yong Li18d77262018-10-09 01:59:45 +0800417}
418
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000419/**
420 * @brief implements the get ACPI power state command
421 *
422 * @return IPMI completion code plus response data on success.
423 * - ACPI system power state
424 * - ACPI device power state
425 **/
426ipmi::RspType<uint8_t, // acpiSystemPowerState
427 uint8_t // acpiDevicePowerState
428 >
429 ipmiGetAcpiPowerState()
Yong Li18d77262018-10-09 01:59:45 +0800430{
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000431 uint8_t sysAcpiState;
432 uint8_t devAcpiState;
Yong Li18d77262018-10-09 01:59:45 +0800433
434 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
435
Yong Li18d77262018-10-09 01:59:45 +0800436 try
437 {
438 auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface);
439
440 auto sysACPIVal = ipmi::getDbusProperty(
441 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
442 acpi_state::sysACPIProp);
443 auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700444 std::get<std::string>(sysACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000445 sysAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
Yong Li18d77262018-10-09 01:59:45 +0800446
447 auto devACPIVal = ipmi::getDbusProperty(
448 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
449 acpi_state::devACPIProp);
450 auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -0700451 std::get<std::string>(devACPIVal));
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000452 devAcpiState = static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
Yong Li18d77262018-10-09 01:59:45 +0800453 }
454 catch (const InternalFailure& e)
455 {
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000456 return ipmi::responseUnspecifiedError();
Yong Li18d77262018-10-09 01:59:45 +0800457 }
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +0000458
459 return ipmi::responseSuccess(sysAcpiState, devAcpiState);
Chris Austen6caf28b2015-10-13 12:40:40 -0500460}
461
Chris Austen7303bdc2016-04-17 11:50:54 -0500462typedef struct
463{
464 char major;
465 char minor;
Chris Austen176c9652016-04-30 16:32:17 -0500466 uint16_t d[2];
Vernon Mauery86a50822019-03-25 13:11:36 -0700467} Revision;
Chris Austen7303bdc2016-04-17 11:50:54 -0500468
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600469/* Currently supports the vx.x-x-[-x] and v1.x.x-x-[-x] format. It will */
470/* return -1 if not in those formats, this routine knows how to parse */
Chris Austen7303bdc2016-04-17 11:50:54 -0500471/* version = v0.6-19-gf363f61-dirty */
472/* ^ ^ ^^ ^ */
473/* | | |----------|-- additional details */
474/* | |---------------- Minor */
475/* |------------------ Major */
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600476/* and version = v1.99.10-113-g65edf7d-r3-0-g9e4f715 */
477/* ^ ^ ^^ ^ */
478/* | | |--|---------- additional details */
479/* | |---------------- Minor */
480/* |------------------ Major */
Chris Austen7303bdc2016-04-17 11:50:54 -0500481/* Additional details : If the option group exists it will force Auxiliary */
482/* Firmware Revision Information 4th byte to 1 indicating the build was */
483/* derived with additional edits */
Vernon Mauery86a50822019-03-25 13:11:36 -0700484int convertVersion(std::string s, Revision& rev)
Chris Austen7303bdc2016-04-17 11:50:54 -0500485{
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600486 std::string token;
Chris Austen176c9652016-04-30 16:32:17 -0500487 uint16_t commits;
Chris Austen7303bdc2016-04-17 11:50:54 -0500488
Patrick Venture0b02be92018-08-31 11:55:55 -0700489 auto location = s.find_first_of('v');
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600490 if (location != std::string::npos)
491 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700492 s = s.substr(location + 1);
Chris Austen176c9652016-04-30 16:32:17 -0500493 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500494
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600495 if (!s.empty())
496 {
497 location = s.find_first_of(".");
498 if (location != std::string::npos)
499 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700500 rev.major =
Hieu Huynh7adc5a12021-09-21 12:04:51 +0000501 static_cast<char>(std::stoi(s.substr(0, location), 0, 10));
Patrick Venture0b02be92018-08-31 11:55:55 -0700502 token = s.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600503 }
Chris Austen176c9652016-04-30 16:32:17 -0500504
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600505 if (!token.empty())
506 {
507 location = token.find_first_of(".-");
508 if (location != std::string::npos)
509 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700510 rev.minor = static_cast<char>(
Hieu Huynh7adc5a12021-09-21 12:04:51 +0000511 std::stoi(token.substr(0, location), 0, 10));
Patrick Venture0b02be92018-08-31 11:55:55 -0700512 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600513 }
514 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500515
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600516 // Capture the number of commits on top of the minor tag.
517 // I'm using BE format like the ipmi spec asked for
518 location = token.find_first_of(".-");
519 if (!token.empty())
520 {
521 commits = std::stoi(token.substr(0, location), 0, 16);
Vernon Mauery86a50822019-03-25 13:11:36 -0700522 rev.d[0] = (commits >> 8) | (commits << 8);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600523
524 // commit number we skip
525 location = token.find_first_of(".-");
526 if (location != std::string::npos)
527 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700528 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600529 }
530 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700531 else
532 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700533 rev.d[0] = 0;
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600534 }
535
536 if (location != std::string::npos)
537 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700538 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600539 }
540
541 // Any value of the optional parameter forces it to 1
542 location = token.find_first_of(".-");
543 if (location != std::string::npos)
544 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700545 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600546 }
547 commits = (!token.empty()) ? 1 : 0;
548
Patrick Venture0b02be92018-08-31 11:55:55 -0700549 // We do this operation to get this displayed in least significant bytes
550 // of ipmitool device id command.
Vernon Mauery86a50822019-03-25 13:11:36 -0700551 rev.d[1] = (commits >> 8) | (commits << 8);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600552 }
553
Chris Austen7303bdc2016-04-17 11:50:54 -0500554 return 0;
555}
556
Vernon Maueryea1c4012019-05-24 13:26:16 -0700557/* @brief: Implement the Get Device ID IPMI command per the IPMI spec
558 * @param[in] ctx - shared_ptr to an IPMI context struct
559 *
560 * @returns IPMI completion code plus response data
561 * - Device ID (manufacturer defined)
562 * - Device revision[4 bits]; reserved[3 bits]; SDR support[1 bit]
563 * - FW revision major[7 bits] (binary encoded); available[1 bit]
564 * - FW Revision minor (BCD encoded)
565 * - IPMI version (0x02 for IPMI 2.0)
566 * - device support (bitfield of supported options)
567 * - MFG IANA ID (3 bytes)
568 * - product ID (2 bytes)
569 * - AUX info (4 bytes)
570 */
571ipmi::RspType<uint8_t, // Device ID
572 uint8_t, // Device Revision
573 uint8_t, // Firmware Revision Major
574 uint8_t, // Firmware Revision minor
575 uint8_t, // IPMI version
576 uint8_t, // Additional device support
577 uint24_t, // MFG ID
578 uint16_t, // Product ID
579 uint32_t // AUX info
580 >
Willy Tu11d68892022-01-20 10:37:34 -0800581 ipmiAppGetDeviceId([[maybe_unused]] ipmi::Context::ptr ctx)
Chris Austen6caf28b2015-10-13 12:40:40 -0500582{
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600583 int r = -1;
Willy Tu11d68892022-01-20 10:37:34 -0800584 Revision rev = {0, 0, 0, 0};
Vernon Mauery86a50822019-03-25 13:11:36 -0700585 static struct
586 {
587 uint8_t id;
588 uint8_t revision;
589 uint8_t fw[2];
590 uint8_t ipmiVer;
591 uint8_t addnDevSupport;
592 uint24_t manufId;
593 uint16_t prodId;
594 uint32_t aux;
595 } devId;
David Cobbleya1adb072017-11-21 15:58:13 -0800596 static bool dev_id_initialized = false;
JeffLin27a62ec2021-11-24 15:40:33 +0800597 static bool haveBMCVersion = false;
Patrick Venture94930a12019-04-30 10:01:58 -0700598 static bool defaultActivationSetting = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800599 const char* filename = "/usr/share/ipmi-providers/dev_id.json";
Alexander Amelkinba19c182018-09-04 15:49:36 +0300600 constexpr auto ipmiDevIdStateShift = 7;
601 constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
JeffLin27a62ec2021-11-24 15:40:33 +0800602 if (!haveBMCVersion || !dev_id_initialized)
David Cobbleya1adb072017-11-21 15:58:13 -0800603 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600604 try
605 {
Vernon Maueryea1c4012019-05-24 13:26:16 -0700606 auto version = getActiveSoftwareVersionInfo(ctx);
Vernon Mauery86a50822019-03-25 13:11:36 -0700607 r = convertVersion(version, rev);
David Cobbleya1adb072017-11-21 15:58:13 -0800608 }
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600609 catch (const std::exception& e)
610 {
611 log<level::ERR>(e.what());
612 }
Nan Liee0cb902016-07-11 15:38:03 +0800613
Patrick Venture0b02be92018-08-31 11:55:55 -0700614 if (r >= 0)
615 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600616 // bit7 identifies if the device is available
617 // 0=normal operation
618 // 1=device firmware, SDR update,
619 // or self-initialization in progress.
Alexander Amelkinba19c182018-09-04 15:49:36 +0300620 // The availability may change in run time, so mask here
621 // and initialize later.
Vernon Mauery86a50822019-03-25 13:11:36 -0700622 devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600623
624 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Vernon Mauery86a50822019-03-25 13:11:36 -0700625 devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
626 std::memcpy(&devId.aux, rev.d, 4);
Brandon Kimc1f5aca2022-03-17 17:54:06 -0700627 haveBMCVersion = true;
David Cobbleya1adb072017-11-21 15:58:13 -0800628 }
JeffLin27a62ec2021-11-24 15:40:33 +0800629 }
630 if (!dev_id_initialized)
631 {
David Cobbleya1adb072017-11-21 15:58:13 -0800632 // IPMI Spec version 2.0
Vernon Mauery86a50822019-03-25 13:11:36 -0700633 devId.ipmiVer = 2;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500634
Vernon Mauery86a50822019-03-25 13:11:36 -0700635 std::ifstream devIdFile(filename);
636 if (devIdFile.is_open())
David Cobbleya1adb072017-11-21 15:58:13 -0800637 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700638 auto data = nlohmann::json::parse(devIdFile, nullptr, false);
David Cobbleya1adb072017-11-21 15:58:13 -0800639 if (!data.is_discarded())
640 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700641 devId.id = data.value("id", 0);
642 devId.revision = data.value("revision", 0);
643 devId.addnDevSupport = data.value("addn_dev_support", 0);
644 devId.manufId = data.value("manuf_id", 0);
645 devId.prodId = data.value("prod_id", 0);
646 devId.aux = data.value("aux", 0);
David Cobbleya1adb072017-11-21 15:58:13 -0800647
Willy Tubfd3a172022-05-31 13:57:54 -0700648 if (data.contains("firmware_revision"))
649 {
650 const auto& firmwareRevision = data.at("firmware_revision");
651 if (firmwareRevision.contains("major"))
652 {
653 firmwareRevision.at("major").get_to(devId.fw[0]);
654 }
655 if (firmwareRevision.contains("minor"))
656 {
657 firmwareRevision.at("minor").get_to(devId.fw[1]);
658 }
659 }
660
Patrick Venture94930a12019-04-30 10:01:58 -0700661 // Set the availablitity of the BMC.
662 defaultActivationSetting = data.value("availability", true);
663
Patrick Venture0b02be92018-08-31 11:55:55 -0700664 // Don't read the file every time if successful
David Cobbleya1adb072017-11-21 15:58:13 -0800665 dev_id_initialized = true;
666 }
667 else
668 {
669 log<level::ERR>("Device ID JSON parser failure");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700670 return ipmi::responseUnspecifiedError();
David Cobbleya1adb072017-11-21 15:58:13 -0800671 }
672 }
673 else
674 {
675 log<level::ERR>("Device ID file not found");
Vernon Maueryf2587fc2019-04-09 14:28:15 -0700676 return ipmi::responseUnspecifiedError();
Chris Austen7303bdc2016-04-17 11:50:54 -0500677 }
678 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500679
Alexander Amelkinba19c182018-09-04 15:49:36 +0300680 // Set availability to the actual current BMC state
Vernon Mauery86a50822019-03-25 13:11:36 -0700681 devId.fw[0] &= ipmiDevIdFw1Mask;
Patrick Venture94930a12019-04-30 10:01:58 -0700682 if (!getCurrentBmcStateWithFallback(defaultActivationSetting))
Alexander Amelkinba19c182018-09-04 15:49:36 +0300683 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700684 devId.fw[0] |= (1 << ipmiDevIdStateShift);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300685 }
686
Vernon Mauery86a50822019-03-25 13:11:36 -0700687 return ipmi::responseSuccess(
688 devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer,
689 devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux);
Chris Austen6caf28b2015-10-13 12:40:40 -0500690}
691
Vernon Maueryb84a5282019-03-25 13:39:03 -0700692auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
Nan Li41fa24a2016-11-10 20:12:37 +0800693{
Nan Li41fa24a2016-11-10 20:12:37 +0800694 // Byte 2:
695 // 55h - No error.
Gunnar Mills8991dd62017-10-25 17:11:29 -0500696 // 56h - Self Test function not implemented in this controller.
Nan Li41fa24a2016-11-10 20:12:37 +0800697 // 57h - Corrupted or inaccesssible data or devices.
698 // 58h - Fatal hardware error.
699 // FFh - reserved.
700 // all other: Device-specific 'internal failure'.
701 // Byte 3:
702 // For byte 2 = 55h, 56h, FFh: 00h
703 // For byte 2 = 58h, all other: Device-specific
704 // For byte 2 = 57h: self-test error bitfield.
705 // Note: returning 57h does not imply that all test were run.
706 // [7] 1b = Cannot access SEL device.
707 // [6] 1b = Cannot access SDR Repository.
708 // [5] 1b = Cannot access BMC FRU device.
709 // [4] 1b = IPMB signal lines do not respond.
710 // [3] 1b = SDR Repository empty.
711 // [2] 1b = Internal Use Area of BMC FRU corrupted.
712 // [1] 1b = controller update 'boot block' firmware corrupted.
713 // [0] 1b = controller operational firmware corrupted.
Vernon Maueryb84a5282019-03-25 13:39:03 -0700714 constexpr uint8_t notImplemented = 0x56;
715 constexpr uint8_t zero = 0;
716 return ipmi::responseSuccess(notImplemented, zero);
Nan Li41fa24a2016-11-10 20:12:37 +0800717}
718
Vernon Mauery15541322019-03-25 13:33:03 -0700719static constexpr size_t uuidBinaryLength = 16;
720static std::array<uint8_t, uuidBinaryLength> rfc4122ToIpmi(std::string rfc4122)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500721{
Vernon Mauery15541322019-03-25 13:33:03 -0700722 using Argument = xyz::openbmc_project::Common::InvalidArgument;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500723 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Ventured2117022018-02-06 08:54:37 -0800724 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
725 // order
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500726 // Ex: 0x2332fc2c40e66298e511f2782395a361
Vernon Mauery15541322019-03-25 13:33:03 -0700727 constexpr size_t uuidHexLength = (2 * uuidBinaryLength);
728 constexpr size_t uuidRfc4122Length = (uuidHexLength + 4);
729 std::array<uint8_t, uuidBinaryLength> uuid;
730 if (rfc4122.size() == uuidRfc4122Length)
Patrick Venture0b02be92018-08-31 11:55:55 -0700731 {
Vernon Mauery15541322019-03-25 13:33:03 -0700732 rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
733 rfc4122.end());
Sergey Solomineb9b8142016-08-23 09:07:28 -0500734 }
Vernon Mauery15541322019-03-25 13:33:03 -0700735 if (rfc4122.size() != uuidHexLength)
vishwa1eaea4f2016-02-26 11:57:40 -0600736 {
Vernon Mauery15541322019-03-25 13:33:03 -0700737 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
738 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
vishwa1eaea4f2016-02-26 11:57:40 -0600739 }
Vernon Mauery15541322019-03-25 13:33:03 -0700740 for (size_t ind = 0; ind < uuidHexLength; ind += 2)
vishwa1eaea4f2016-02-26 11:57:40 -0600741 {
Vernon Mauery15541322019-03-25 13:33:03 -0700742 char v[3];
743 v[0] = rfc4122[ind];
744 v[1] = rfc4122[ind + 1];
745 v[2] = 0;
746 size_t err;
747 long b;
748 try
Emily Shafferedb8bb02018-09-27 14:50:15 -0700749 {
Vernon Mauery15541322019-03-25 13:33:03 -0700750 b = std::stoul(v, &err, 16);
Emily Shafferedb8bb02018-09-27 14:50:15 -0700751 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500752 catch (const std::exception& e)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500753 {
Vernon Mauery15541322019-03-25 13:33:03 -0700754 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
755 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500756 }
Vernon Mauery15541322019-03-25 13:33:03 -0700757 // check that exactly two ascii bytes were converted
758 if (err != 2)
759 {
760 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
761 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
762 }
763 uuid[uuidBinaryLength - (ind / 2) - 1] = static_cast<uint8_t>(b);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500764 }
Vernon Mauery15541322019-03-25 13:33:03 -0700765 return uuid;
766}
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500767
Vernon Mauery15541322019-03-25 13:33:03 -0700768auto ipmiAppGetDeviceGuid()
769 -> ipmi::RspType<std::array<uint8_t, uuidBinaryLength>>
770{
771 // return a fixed GUID based on /etc/machine-id
772 // This should match the /redfish/v1/Managers/bmc's UUID data
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500773
Vernon Mauery15541322019-03-25 13:33:03 -0700774 // machine specific application ID (for BMC ID)
775 // generated by systemd-id128 -p new as per man page
776 static constexpr sd_id128_t bmcUuidAppId = SD_ID128_MAKE(
777 e0, e1, 73, 76, 64, 61, 47, da, a5, 0c, d0, cc, 64, 12, 45, 78);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500778
Vernon Mauery15541322019-03-25 13:33:03 -0700779 sd_id128_t bmcUuid;
780 // create the UUID from /etc/machine-id via the systemd API
781 sd_id128_get_machine_app_specific(bmcUuidAppId, &bmcUuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500782
Vernon Mauery15541322019-03-25 13:33:03 -0700783 char bmcUuidCstr[SD_ID128_STRING_MAX];
784 std::string systemUuid = sd_id128_to_string(bmcUuid, bmcUuidCstr);
785
786 std::array<uint8_t, uuidBinaryLength> uuid = rfc4122ToIpmi(systemUuid);
787 return ipmi::responseSuccess(uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500788}
Chris Austen6caf28b2015-10-13 12:40:40 -0500789
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700790auto ipmiAppGetBtCapabilities()
791 -> ipmi::RspType<uint8_t, uint8_t, uint8_t, uint8_t, uint8_t>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530792{
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600793 // Per IPMI 2.0 spec, the input and output buffer size must be the max
794 // buffer size minus one byte to allocate space for the length byte.
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700795 constexpr uint8_t nrOutstanding = 0x01;
796 constexpr uint8_t inputBufferSize = MAX_IPMI_BUFFER - 1;
797 constexpr uint8_t outputBufferSize = MAX_IPMI_BUFFER - 1;
798 constexpr uint8_t transactionTime = 0x0A;
799 constexpr uint8_t nrRetries = 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530800
Vernon Mauerycc99ba42019-03-25 13:40:11 -0700801 return ipmi::responseSuccess(nrOutstanding, inputBufferSize,
802 outputBufferSize, transactionTime, nrRetries);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530803}
804
Vernon Maueryb90a5322019-03-25 13:36:55 -0700805auto ipmiAppGetSystemGuid() -> ipmi::RspType<std::array<uint8_t, 16>>
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600806{
Vernon Maueryb90a5322019-03-25 13:36:55 -0700807 static constexpr auto bmcInterface =
808 "xyz.openbmc_project.Inventory.Item.Bmc";
809 static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
810 static constexpr auto uuidProperty = "UUID";
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600811
Vernon Maueryb90a5322019-03-25 13:36:55 -0700812 ipmi::Value propValue;
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600813 try
814 {
815 // Get the Inventory object implementing BMC interface
Vernon Maueryb90a5322019-03-25 13:36:55 -0700816 auto busPtr = getSdBus();
817 auto objectInfo = ipmi::getDbusObject(*busPtr, bmcInterface);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600818
819 // Read UUID property value from bmcObject
820 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Vernon Maueryb90a5322019-03-25 13:36:55 -0700821 propValue =
822 ipmi::getDbusProperty(*busPtr, objectInfo.second, objectInfo.first,
823 uuidInterface, uuidProperty);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600824 }
825 catch (const InternalFailure& e)
826 {
827 log<level::ERR>("Failed in reading BMC UUID property",
Vernon Maueryb90a5322019-03-25 13:36:55 -0700828 entry("INTERFACE=%s", uuidInterface),
829 entry("PROPERTY=%s", uuidProperty));
830 return ipmi::responseUnspecifiedError();
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600831 }
Vernon Maueryb90a5322019-03-25 13:36:55 -0700832 std::array<uint8_t, 16> uuid;
833 std::string rfc4122Uuid = std::get<std::string>(propValue);
834 try
835 {
836 // convert to IPMI format
837 uuid = rfc4122ToIpmi(rfc4122Uuid);
838 }
839 catch (const InvalidArgument& e)
840 {
841 log<level::ERR>("Failed in parsing BMC UUID property",
842 entry("INTERFACE=%s", uuidInterface),
843 entry("PROPERTY=%s", uuidProperty),
844 entry("VALUE=%s", rfc4122Uuid.c_str()));
845 return ipmi::responseUnspecifiedError();
846 }
847 return ipmi::responseSuccess(uuid);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600848}
849
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000850/**
851 * @brief set the session state as teardown
852 *
853 * This function is to set the session state to tear down in progress if the
854 * state is active.
855 *
856 * @param[in] busp - Dbus obj
857 * @param[in] service - service name
858 * @param[in] obj - object path
859 *
860 * @return success completion code if it sets the session state to
861 * tearDownInProgress else return the corresponding error completion code.
862 **/
863uint8_t setSessionState(std::shared_ptr<sdbusplus::asio::connection>& busp,
864 const std::string& service, const std::string& obj)
865{
866 try
867 {
868 uint8_t sessionState = std::get<uint8_t>(ipmi::getDbusProperty(
869 *busp, service, obj, session::sessionIntf, "State"));
870
871 if (sessionState == static_cast<uint8_t>(session::State::active))
872 {
873 ipmi::setDbusProperty(
874 *busp, service, obj, session::sessionIntf, "State",
875 static_cast<uint8_t>(session::State::tearDownInProgress));
876 return ipmi::ccSuccess;
877 }
878 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500879 catch (const std::exception& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000880 {
881 log<level::ERR>("Failed in getting session state property",
882 entry("service=%s", service.c_str()),
883 entry("object path=%s", obj.c_str()),
884 entry("interface=%s", session::sessionIntf));
885 return ipmi::ccUnspecifiedError;
886 }
887
888 return ipmi::ccInvalidFieldRequest;
889}
890
891ipmi::RspType<> ipmiAppCloseSession(uint32_t reqSessionId,
892 std::optional<uint8_t> requestSessionHandle)
893{
894 auto busp = getSdBus();
895 uint8_t reqSessionHandle =
896 requestSessionHandle.value_or(session::defaultSessionHandle);
897
898 if (reqSessionId == session::sessionZero &&
899 reqSessionHandle == session::defaultSessionHandle)
900 {
901 return ipmi::response(session::ccInvalidSessionId);
902 }
903
904 if (reqSessionId == session::sessionZero &&
905 reqSessionHandle == session::invalidSessionHandle)
906 {
907 return ipmi::response(session::ccInvalidSessionHandle);
908 }
909
910 if (reqSessionId != session::sessionZero &&
911 reqSessionHandle != session::defaultSessionHandle)
912 {
913 return ipmi::response(ipmi::ccInvalidFieldRequest);
914 }
915
916 try
917 {
918 ipmi::ObjectTree objectTree = ipmi::getAllDbusObjects(
919 *busp, session::sessionManagerRootPath, session::sessionIntf);
920
921 for (auto& objectTreeItr : objectTree)
922 {
923 const std::string obj = objectTreeItr.first;
924
925 if (isSessionObjectMatched(obj, reqSessionId, reqSessionHandle))
926 {
927 auto& serviceMap = objectTreeItr.second;
928
929 // Session id and session handle are unique for each session.
930 // Session id and handler are retrived from the object path and
931 // object path will be unique for each session. Checking if
932 // multiple objects exist with same object path under multiple
933 // services.
934 if (serviceMap.size() != 1)
935 {
936 return ipmi::responseUnspecifiedError();
937 }
938
939 auto itr = serviceMap.begin();
940 const std::string service = itr->first;
941 return ipmi::response(setSessionState(busp, service, obj));
942 }
943 }
944 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500945 catch (const sdbusplus::exception::exception& e)
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +0000946 {
947 log<level::ERR>("Failed to fetch object from dbus",
948 entry("INTERFACE=%s", session::sessionIntf),
949 entry("ERRMSG=%s", e.what()));
950 return ipmi::responseUnspecifiedError();
951 }
952
953 return ipmi::responseInvalidFieldRequest();
954}
955
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000956uint8_t getTotalSessionCount()
957{
Meera-Kattac1789482021-05-18 09:53:26 +0000958 uint8_t count = 0, ch = 0;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000959
960 while (ch < ipmi::maxIpmiChannels &&
961 count < session::maxNetworkInstanceSupported)
962 {
Jayaprakash Mutyala80207e62020-07-04 16:34:15 +0000963 ipmi::ChannelInfo chInfo{};
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000964 ipmi::getChannelInfo(ch, chInfo);
965 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
966 ipmi::EChannelMediumType::lan8032)
967 {
968 count++;
969 }
970 ch++;
971 }
972 return count * session::maxSessionCountPerChannel;
973}
974
975/**
976 * @brief get session info request data.
977 *
978 * This function validates the request data and retrive request session id,
979 * session handle.
980 *
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +0530981 * @param[in] ctx - context of current session.
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000982 * @param[in] sessionIndex - request session index
983 * @param[in] payload - input payload
984 * @param[in] reqSessionId - unpacked session Id will be asigned
985 * @param[in] reqSessionHandle - unpacked session handle will be asigned
986 *
987 * @return success completion code if request data is valid
988 * else return the correcponding error completion code.
989 **/
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +0530990uint8_t getSessionInfoRequestData(const ipmi::Context::ptr ctx,
991 const uint8_t sessionIndex,
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000992 ipmi::message::Payload& payload,
993 uint32_t& reqSessionId,
994 uint8_t& reqSessionHandle)
995{
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +0530996 if ((sessionIndex > session::maxSessionCountPerChannel) &&
997 (sessionIndex < session::searchSessionByHandle))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +0000998 {
999 return ipmi::ccInvalidFieldRequest;
1000 }
1001
1002 switch (sessionIndex)
1003 {
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301004 case session::searchCurrentSession:
1005
1006 ipmi::ChannelInfo chInfo;
1007 ipmi::getChannelInfo(ctx->channel, chInfo);
1008
1009 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) !=
1010 ipmi::EChannelMediumType::lan8032)
1011 {
1012 return ipmi::ccInvalidFieldRequest;
1013 }
1014
1015 if (!payload.fullyUnpacked())
1016 {
1017 return ipmi::ccReqDataLenInvalid;
1018 }
1019 // Check if current sessionId is 0, sessionId 0 is reserved.
1020 if (ctx->sessionId == session::sessionZero)
1021 {
1022 return session::ccInvalidSessionId;
1023 }
1024 reqSessionId = ctx->sessionId;
1025 break;
1026
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001027 case session::searchSessionByHandle:
1028
1029 if ((payload.unpack(reqSessionHandle)) ||
1030 (!payload.fullyUnpacked()))
1031 {
1032 return ipmi::ccReqDataLenInvalid;
1033 }
1034
1035 if ((reqSessionHandle == session::sessionZero) ||
1036 ((reqSessionHandle & session::multiIntfaceSessionHandleMask) >
1037 session::maxSessionCountPerChannel))
1038 {
1039 return session::ccInvalidSessionHandle;
1040 }
1041 break;
1042
1043 case session::searchSessionById:
1044
1045 if ((payload.unpack(reqSessionId)) || (!payload.fullyUnpacked()))
1046 {
1047 return ipmi::ccReqDataLenInvalid;
1048 }
1049
1050 if (reqSessionId == session::sessionZero)
1051 {
1052 return session::ccInvalidSessionId;
1053 }
1054 break;
1055
1056 default:
1057 if (!payload.fullyUnpacked())
1058 {
1059 return ipmi::ccReqDataLenInvalid;
1060 }
1061 break;
1062 }
1063 return ipmi::ccSuccess;
1064}
1065
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001066uint8_t getSessionState(ipmi::Context::ptr ctx, const std::string& service,
1067 const std::string& objPath, uint8_t& sessionState)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001068{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001069 boost::system::error_code ec = ipmi::getDbusProperty(
1070 ctx, service, objPath, session::sessionIntf, "State", sessionState);
1071 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001072 {
1073 log<level::ERR>("Failed to fetch state property ",
1074 entry("SERVICE=%s", service.c_str()),
1075 entry("OBJECTPATH=%s", objPath.c_str()),
1076 entry("INTERFACE=%s", session::sessionIntf),
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001077 entry("ERRMSG=%s", ec.message().c_str()));
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001078 return ipmi::ccUnspecifiedError;
1079 }
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001080 return ipmi::ccSuccess;
1081}
1082
1083static constexpr uint8_t macAddrLen = 6;
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001084/** Alias SessionDetails - contain the optional information about an
1085 * RMCP+ session.
1086 *
1087 * @param userID - uint6_t session user ID (0-63)
1088 * @param reserved - uint2_t reserved
1089 * @param privilege - uint4_t session privilege (0-5)
1090 * @param reserved - uint4_t reserved
1091 * @param channel - uint4_t session channel number
1092 * @param protocol - uint4_t session protocol
1093 * @param remoteIP - uint32_t remote IP address
1094 * @param macAddr - std::array<uint8_t, 6> mac address
1095 * @param port - uint16_t remote port
1096 */
1097using SessionDetails =
1098 std::tuple<uint2_t, uint6_t, uint4_t, uint4_t, uint4_t, uint4_t, uint32_t,
1099 std::array<uint8_t, macAddrLen>, uint16_t>;
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001100
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001101/** @brief get session details for a given session
1102 *
1103 * @param[in] ctx - ipmi::Context pointer for accessing D-Bus
1104 * @param[in] service - D-Bus service name to fetch details from
1105 * @param[in] objPath - D-Bus object path for session
1106 * @param[out] sessionHandle - return session handle for session
1107 * @param[out] sessionState - return session state for session
1108 * @param[out] details - return a SessionDetails tuple containing other
1109 * session info
1110 * @return - ipmi::Cc success or error code
1111 */
1112ipmi::Cc getSessionDetails(ipmi::Context::ptr ctx, const std::string& service,
1113 const std::string& objPath, uint8_t& sessionHandle,
1114 uint8_t& sessionState, SessionDetails& details)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001115{
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001116 ipmi::PropertyMap sessionProps;
1117 boost::system::error_code ec = ipmi::getAllDbusProperties(
1118 ctx, service, objPath, session::sessionIntf, sessionProps);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001119
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001120 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001121 {
1122 log<level::ERR>("Failed to fetch state property ",
1123 entry("SERVICE=%s", service.c_str()),
1124 entry("OBJECTPATH=%s", objPath.c_str()),
1125 entry("INTERFACE=%s", session::sessionIntf),
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001126 entry("ERRMSG=%s", ec.message().c_str()));
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001127 return ipmi::ccUnspecifiedError;
1128 }
1129
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001130 sessionState = ipmi::mappedVariant<uint8_t>(
1131 sessionProps, "State", static_cast<uint8_t>(session::State::inactive));
1132 if (sessionState == static_cast<uint8_t>(session::State::active))
1133 {
1134 sessionHandle =
1135 ipmi::mappedVariant<uint8_t>(sessionProps, "SessionHandle", 0);
1136 std::get<0>(details) =
1137 ipmi::mappedVariant<uint8_t>(sessionProps, "UserID", 0xff);
1138 // std::get<1>(details) = 0; // (default constructed to 0)
1139 std::get<2>(details) =
1140 ipmi::mappedVariant<uint8_t>(sessionProps, "CurrentPrivilege", 0);
1141 // std::get<3>(details) = 0; // (default constructed to 0)
1142 std::get<4>(details) =
1143 ipmi::mappedVariant<uint8_t>(sessionProps, "ChannelNum", 0xff);
1144 constexpr uint4_t rmcpPlusProtocol = 1;
1145 std::get<5>(details) = rmcpPlusProtocol;
1146 std::get<6>(details) =
1147 ipmi::mappedVariant<uint32_t>(sessionProps, "RemoteIPAddr", 0);
1148 // std::get<7>(details) = {{0}}; // default constructed to all 0
1149 std::get<8>(details) =
1150 ipmi::mappedVariant<uint16_t>(sessionProps, "RemotePort", 0);
1151 }
1152
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001153 return ipmi::ccSuccess;
1154}
1155
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001156ipmi::RspType<uint8_t, // session handle,
1157 uint8_t, // total session count
1158 uint8_t, // active session count
1159 std::optional<SessionDetails>>
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301160 ipmiAppGetSessionInfo(ipmi::Context::ptr ctx, uint8_t sessionIndex,
1161 ipmi::message::Payload& payload)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001162{
1163 uint32_t reqSessionId = 0;
1164 uint8_t reqSessionHandle = session::defaultSessionHandle;
1165 // initializing state to 0xff as 0 represents state as inactive.
1166 uint8_t state = 0xFF;
1167
1168 uint8_t completionCode = getSessionInfoRequestData(
Rajashekar Gade Reddy4d226402019-11-13 17:13:05 +05301169 ctx, sessionIndex, payload, reqSessionId, reqSessionHandle);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001170
1171 if (completionCode)
1172 {
1173 return ipmi::response(completionCode);
1174 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001175 ipmi::ObjectTree objectTree;
1176 boost::system::error_code ec = ipmi::getAllDbusObjects(
1177 ctx, session::sessionManagerRootPath, session::sessionIntf, objectTree);
1178 if (ec)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001179 {
1180 log<level::ERR>("Failed to fetch object from dbus",
1181 entry("INTERFACE=%s", session::sessionIntf),
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001182 entry("ERRMSG=%s", ec.message().c_str()));
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001183 return ipmi::responseUnspecifiedError();
1184 }
1185
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001186 uint8_t totalSessionCount = getTotalSessionCount();
1187 uint8_t activeSessionCount = 0;
1188 uint8_t sessionHandle = session::defaultSessionHandle;
1189 std::optional<SessionDetails> maybeDetails;
1190 uint8_t index = 0;
1191 for (auto& objectTreeItr : objectTree)
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001192 {
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001193 uint32_t sessionId = 0;
1194 std::string objectPath = objectTreeItr.first;
1195
1196 if (!parseCloseSessionInputPayload(objectPath, sessionId,
1197 sessionHandle))
1198 {
1199 continue;
1200 }
1201 index++;
1202 auto& serviceMap = objectTreeItr.second;
1203 auto itr = serviceMap.begin();
1204
1205 if (serviceMap.size() != 1)
1206 {
1207 return ipmi::responseUnspecifiedError();
1208 }
1209
1210 std::string service = itr->first;
1211 uint8_t sessionState = 0;
1212 completionCode =
1213 getSessionState(ctx, service, objectPath, sessionState);
1214 if (completionCode)
1215 {
1216 return ipmi::response(completionCode);
1217 }
1218
1219 if (sessionState == static_cast<uint8_t>(session::State::active))
1220 {
1221 activeSessionCount++;
1222 }
1223
1224 if (index != sessionIndex && reqSessionId != sessionId &&
1225 reqSessionHandle != sessionHandle)
1226 {
1227 continue;
1228 }
1229
1230 SessionDetails details{};
1231 completionCode = getSessionDetails(ctx, service, objectPath,
1232 sessionHandle, state, details);
1233
1234 if (completionCode)
1235 {
1236 return ipmi::response(completionCode);
1237 }
1238 maybeDetails = std::move(details);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001239 }
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001240
1241 if (state == static_cast<uint8_t>(session::State::active) ||
1242 state == static_cast<uint8_t>(session::State::tearDownInProgress))
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001243 {
Vernon Mauerye7e8b812019-10-28 16:00:34 -07001244 return ipmi::responseSuccess(sessionHandle, totalSessionCount,
1245 activeSessionCount, maybeDetails);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001246 }
1247
1248 return ipmi::responseInvalidFieldRequest();
1249}
1250
Xo Wangf542e8b2017-08-09 15:34:16 -07001251static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
1252
Xo Wang87651332017-08-11 10:17:59 -07001253static std::string sysInfoReadSystemName()
1254{
1255 // Use the BMC hostname as the "System Name."
1256 char hostname[HOST_NAME_MAX + 1] = {};
1257 if (gethostname(hostname, HOST_NAME_MAX) != 0)
1258 {
1259 perror("System info parameter: system name");
1260 }
1261 return hostname;
1262}
1263
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001264static constexpr uint8_t paramRevision = 0x11;
1265static constexpr size_t configParameterLength = 16;
Xo Wangf542e8b2017-08-09 15:34:16 -07001266
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001267static constexpr size_t smallChunkSize = 14;
1268static constexpr size_t fullChunkSize = 16;
Jia, chunhui449f2162019-09-11 16:51:51 +08001269static constexpr uint8_t progressMask = 0x3;
Xo Wangf542e8b2017-08-09 15:34:16 -07001270
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001271static constexpr uint8_t setComplete = 0x0;
1272static constexpr uint8_t setInProgress = 0x1;
1273static constexpr uint8_t commitWrite = 0x2;
1274static uint8_t transferStatus = setComplete;
1275
Jia, chunhui449f2162019-09-11 16:51:51 +08001276static constexpr uint8_t configDataOverhead = 2;
1277
1278// For EFI based system, 256 bytes is recommended.
1279static constexpr size_t maxBytesPerParameter = 256;
1280
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001281namespace ipmi
1282{
1283constexpr Cc ccParmNotSupported = 0x80;
Jia, chunhui449f2162019-09-11 16:51:51 +08001284constexpr Cc ccSetInProgressActive = 0x81;
1285constexpr Cc ccSystemInfoParameterSetReadOnly = 0x82;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001286
1287static inline auto responseParmNotSupported()
1288{
1289 return response(ccParmNotSupported);
Xo Wangf542e8b2017-08-09 15:34:16 -07001290}
Jia, chunhui449f2162019-09-11 16:51:51 +08001291static inline auto responseSetInProgressActive()
1292{
1293 return response(ccSetInProgressActive);
1294}
1295static inline auto responseSystemInfoParameterSetReadOnly()
1296{
1297 return response(ccSystemInfoParameterSetReadOnly);
1298}
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001299} // namespace ipmi
Xo Wangf542e8b2017-08-09 15:34:16 -07001300
Jia, chunhui449f2162019-09-11 16:51:51 +08001301ipmi::RspType<uint8_t, // Parameter revision
1302 std::optional<uint8_t>, // data1 / setSelector / ProgressStatus
1303 std::optional<std::vector<uint8_t>>> // data2-17
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001304 ipmiAppGetSystemInfo(uint7_t reserved, bool getRevision,
1305 uint8_t paramSelector, uint8_t setSelector,
1306 uint8_t BlockSelector)
Xo Wangf542e8b2017-08-09 15:34:16 -07001307{
Snehalatha Va5ae7722020-05-02 18:18:57 +00001308 if (reserved || (paramSelector >= invalidParamSelectorStart &&
1309 paramSelector <= invalidParamSelectorEnd))
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001310 {
1311 return ipmi::responseInvalidFieldRequest();
1312 }
Snehalatha Va5ae7722020-05-02 18:18:57 +00001313 if ((paramSelector >= oemCmdStart) && (paramSelector <= oemCmdEnd))
1314 {
1315 return ipmi::responseParmNotSupported();
1316 }
jayaprakash Mutyalac2566a92020-04-23 21:18:35 +00001317 if (getRevision)
Xo Wangf542e8b2017-08-09 15:34:16 -07001318 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001319 return ipmi::responseSuccess(paramRevision, std::nullopt, std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001320 }
1321
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001322 if (paramSelector == 0)
Xo Wangf542e8b2017-08-09 15:34:16 -07001323 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001324 return ipmi::responseSuccess(paramRevision, transferStatus,
1325 std::nullopt);
Xo Wangf542e8b2017-08-09 15:34:16 -07001326 }
1327
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001328 if (BlockSelector != 0) // 00h if parameter does not require a block number
Xo Wangf542e8b2017-08-09 15:34:16 -07001329 {
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001330 return ipmi::responseParmNotSupported();
Xo Wangf542e8b2017-08-09 15:34:16 -07001331 }
1332
1333 if (sysInfoParamStore == nullptr)
1334 {
1335 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
Xo Wang87651332017-08-11 10:17:59 -07001336 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1337 sysInfoReadSystemName);
Xo Wangf542e8b2017-08-09 15:34:16 -07001338 }
1339
1340 // Parameters other than Set In Progress are assumed to be strings.
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001341 std::tuple<bool, std::string> ret =
1342 sysInfoParamStore->lookup(paramSelector);
1343 bool found = std::get<0>(ret);
Xo Wangf542e8b2017-08-09 15:34:16 -07001344 if (!found)
1345 {
Snehalatha Va5ae7722020-05-02 18:18:57 +00001346 return ipmi::responseSensorInvalid();
Xo Wangf542e8b2017-08-09 15:34:16 -07001347 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001348 std::string& paramString = std::get<1>(ret);
Jia, chunhui449f2162019-09-11 16:51:51 +08001349 std::vector<uint8_t> configData;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001350 size_t count = 0;
1351 if (setSelector == 0)
Jia, chunhui449f2162019-09-11 16:51:51 +08001352 { // First chunk has only 14 bytes.
1353 configData.emplace_back(0); // encoding
1354 configData.emplace_back(paramString.length()); // string length
1355 count = std::min(paramString.length(), smallChunkSize);
1356 configData.resize(count + configDataOverhead);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001357 std::copy_n(paramString.begin(), count,
Snehalatha Va5ae7722020-05-02 18:18:57 +00001358 configData.begin() + configDataOverhead); // 14 bytes chunk
1359
1360 // Append zero's to remaining bytes
1361 if (configData.size() < configParameterLength)
1362 {
1363 std::fill_n(std::back_inserter(configData),
1364 configParameterLength - configData.size(), 0x00);
1365 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001366 }
1367 else
Xo Wangf542e8b2017-08-09 15:34:16 -07001368 {
Jia, chunhui449f2162019-09-11 16:51:51 +08001369 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001370 if (offset >= paramString.length())
1371 {
1372 return ipmi::responseParmOutOfRange();
1373 }
Jia, chunhui449f2162019-09-11 16:51:51 +08001374 count = std::min(paramString.length() - offset, fullChunkSize);
1375 configData.resize(count);
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001376 std::copy_n(paramString.begin() + offset, count,
1377 configData.begin()); // 16 bytes chunk
Xo Wangf542e8b2017-08-09 15:34:16 -07001378 }
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001379 return ipmi::responseSuccess(paramRevision, setSelector, configData);
Xo Wangf542e8b2017-08-09 15:34:16 -07001380}
1381
Jia, chunhui449f2162019-09-11 16:51:51 +08001382ipmi::RspType<> ipmiAppSetSystemInfo(uint8_t paramSelector, uint8_t data1,
1383 std::vector<uint8_t> configData)
1384{
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001385 if (paramSelector >= invalidParamSelectorStart &&
1386 paramSelector <= invalidParamSelectorEnd)
1387 {
1388 return ipmi::responseInvalidFieldRequest();
1389 }
1390 if ((paramSelector >= oemCmdStart) && (paramSelector <= oemCmdEnd))
1391 {
1392 return ipmi::responseParmNotSupported();
1393 }
1394
Jia, chunhui449f2162019-09-11 16:51:51 +08001395 if (paramSelector == 0)
1396 {
1397 // attempt to set the 'set in progress' value (in parameter #0)
1398 // when not in the set complete state.
1399 if ((transferStatus != setComplete) && (data1 == setInProgress))
1400 {
1401 return ipmi::responseSetInProgressActive();
1402 }
1403 // only following 2 states are supported
1404 if (data1 > setInProgress)
1405 {
1406 phosphor::logging::log<phosphor::logging::level::ERR>(
1407 "illegal SetInProgress status");
1408 return ipmi::responseInvalidFieldRequest();
1409 }
1410
1411 transferStatus = data1 & progressMask;
1412 return ipmi::responseSuccess();
1413 }
1414
1415 if (configData.size() > configParameterLength)
1416 {
1417 return ipmi::responseInvalidFieldRequest();
1418 }
1419
jayaprakash Mutyala3c5e4132020-04-27 23:00:05 +00001420 // Append zero's to remaining bytes
1421 if (configData.size() < configParameterLength)
1422 {
1423 fill_n(back_inserter(configData),
1424 (configParameterLength - configData.size()), 0x00);
1425 }
1426
Jia, chunhui449f2162019-09-11 16:51:51 +08001427 if (!sysInfoParamStore)
1428 {
1429 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
1430 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1431 sysInfoReadSystemName);
1432 }
1433
1434 // lookup
1435 std::tuple<bool, std::string> ret =
1436 sysInfoParamStore->lookup(paramSelector);
1437 bool found = std::get<0>(ret);
1438 std::string& paramString = std::get<1>(ret);
1439 if (!found)
1440 {
1441 // parameter does not exist. Init new
1442 paramString = "";
1443 }
1444
1445 uint8_t setSelector = data1;
1446 size_t count = 0;
1447 if (setSelector == 0) // First chunk has only 14 bytes.
1448 {
1449 size_t stringLen = configData.at(1); // string length
1450 // maxBytesPerParamter is 256. It will always be greater than stringLen
1451 // (unit8_t) if maxBytes changes in future, then following line is
1452 // needed.
1453 // stringLen = std::min(stringLen, maxBytesPerParameter);
1454 count = std::min(stringLen, smallChunkSize);
1455 count = std::min(count, configData.size());
1456 paramString.resize(stringLen); // reserve space
1457 std::copy_n(configData.begin() + configDataOverhead, count,
1458 paramString.begin());
1459 }
1460 else
1461 {
1462 size_t offset = (setSelector * fullChunkSize) - configDataOverhead;
1463 if (offset >= paramString.length())
1464 {
1465 return ipmi::responseParmOutOfRange();
1466 }
1467 count = std::min(paramString.length() - offset, configData.size());
1468 std::copy_n(configData.begin(), count, paramString.begin() + offset);
1469 }
1470 sysInfoParamStore->update(paramSelector, paramString);
1471 return ipmi::responseSuccess();
1472}
1473
Yong Libd0503a2019-08-22 17:17:17 +08001474#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301475inline std::vector<uint8_t> convertStringToData(const std::string& command)
1476{
1477 std::istringstream iss(command);
1478 std::string token;
1479 std::vector<uint8_t> dataValue;
1480 while (std::getline(iss, token, ' '))
1481 {
1482 dataValue.emplace_back(
1483 static_cast<uint8_t>(std::stoul(token, nullptr, base_16)));
1484 }
1485 return dataValue;
1486}
1487
1488static bool populateI2CMasterWRWhitelist()
1489{
1490 nlohmann::json data = nullptr;
1491 std::ifstream jsonFile(i2cMasterWRWhitelistFile);
1492
1493 if (!jsonFile.good())
1494 {
1495 log<level::WARNING>("i2c white list file not found!",
1496 entry("FILE_NAME: %s", i2cMasterWRWhitelistFile));
1497 return false;
1498 }
1499
1500 try
1501 {
1502 data = nlohmann::json::parse(jsonFile, nullptr, false);
1503 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001504 catch (const nlohmann::json::parse_error& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301505 {
1506 log<level::ERR>("Corrupted i2c white list config file",
1507 entry("FILE_NAME: %s", i2cMasterWRWhitelistFile),
1508 entry("MSG: %s", e.what()));
1509 return false;
1510 }
1511
1512 try
1513 {
1514 // Example JSON Structure format
1515 // "filters": [
1516 // {
1517 // "Description": "Allow full read - ignore first byte write value
1518 // for 0x40 to 0x4F",
1519 // "busId": "0x01",
1520 // "slaveAddr": "0x40",
1521 // "slaveAddrMask": "0x0F",
1522 // "command": "0x00",
1523 // "commandMask": "0xFF"
1524 // },
1525 // {
1526 // "Description": "Allow full read - first byte match 0x05 and
1527 // ignore second byte",
1528 // "busId": "0x01",
1529 // "slaveAddr": "0x57",
1530 // "slaveAddrMask": "0x00",
1531 // "command": "0x05 0x00",
1532 // "commandMask": "0x00 0xFF"
1533 // },]
1534
1535 nlohmann::json filters = data[filtersStr].get<nlohmann::json>();
1536 std::vector<i2cMasterWRWhitelist>& whitelist = getWRWhitelist();
1537 for (const auto& it : filters.items())
1538 {
1539 nlohmann::json filter = it.value();
1540 if (filter.is_null())
1541 {
1542 log<level::ERR>(
1543 "Corrupted I2C master write read whitelist config file",
1544 entry("FILE_NAME: %s", i2cMasterWRWhitelistFile));
1545 return false;
1546 }
1547 const std::vector<uint8_t>& writeData =
1548 convertStringToData(filter[cmdStr].get<std::string>());
1549 const std::vector<uint8_t>& writeDataMask =
1550 convertStringToData(filter[cmdMaskStr].get<std::string>());
1551 if (writeDataMask.size() != writeData.size())
1552 {
1553 log<level::ERR>("I2C master write read whitelist filter "
1554 "mismatch for command & mask size");
1555 return false;
1556 }
1557 whitelist.push_back(
1558 {static_cast<uint8_t>(std::stoul(
1559 filter[busIdStr].get<std::string>(), nullptr, base_16)),
1560 static_cast<uint8_t>(
1561 std::stoul(filter[slaveAddrStr].get<std::string>(),
1562 nullptr, base_16)),
1563 static_cast<uint8_t>(
1564 std::stoul(filter[slaveAddrMaskStr].get<std::string>(),
1565 nullptr, base_16)),
1566 writeData, writeDataMask});
1567 }
1568 if (whitelist.size() != filters.size())
1569 {
1570 log<level::ERR>(
1571 "I2C master write read whitelist filter size mismatch");
1572 return false;
1573 }
1574 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001575 catch (const std::exception& e)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301576 {
1577 log<level::ERR>("I2C master write read whitelist unexpected exception",
1578 entry("ERROR=%s", e.what()));
1579 return false;
1580 }
1581 return true;
1582}
1583
1584static inline bool isWriteDataWhitelisted(const std::vector<uint8_t>& data,
1585 const std::vector<uint8_t>& dataMask,
1586 const std::vector<uint8_t>& writeData)
1587{
1588 std::vector<uint8_t> processedDataBuf(data.size());
1589 std::vector<uint8_t> processedReqBuf(dataMask.size());
1590 std::transform(writeData.begin(), writeData.end(), dataMask.begin(),
1591 processedReqBuf.begin(), std::bit_or<uint8_t>());
1592 std::transform(data.begin(), data.end(), dataMask.begin(),
1593 processedDataBuf.begin(), std::bit_or<uint8_t>());
1594
1595 return (processedDataBuf == processedReqBuf);
1596}
1597
1598static bool isCmdWhitelisted(uint8_t busId, uint8_t slaveAddr,
1599 std::vector<uint8_t>& writeData)
1600{
1601 std::vector<i2cMasterWRWhitelist>& whiteList = getWRWhitelist();
1602 for (const auto& wlEntry : whiteList)
1603 {
1604 if ((busId == wlEntry.busId) &&
1605 ((slaveAddr | wlEntry.slaveAddrMask) ==
1606 (wlEntry.slaveAddr | wlEntry.slaveAddrMask)))
1607 {
1608 const std::vector<uint8_t>& dataMask = wlEntry.dataMask;
1609 // Skip as no-match, if requested write data is more than the
1610 // write data mask size
1611 if (writeData.size() > dataMask.size())
1612 {
1613 continue;
1614 }
1615 if (isWriteDataWhitelisted(wlEntry.data, dataMask, writeData))
1616 {
1617 return true;
1618 }
1619 }
1620 }
1621 return false;
1622}
Yong Libd0503a2019-08-22 17:17:17 +08001623#else
1624static bool populateI2CMasterWRWhitelist()
1625{
1626 log<level::INFO>(
1627 "I2C_WHITELIST_CHECK is disabled, do not populate whitelist");
1628 return true;
1629}
1630#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301631
1632/** @brief implements master write read IPMI command which can be used for
1633 * low-level I2C/SMBus write, read or write-read access
1634 * @param isPrivateBus -to indicate private bus usage
1635 * @param busId - bus id
1636 * @param channelNum - channel number
1637 * @param reserved - skip 1 bit
1638 * @param slaveAddr - slave address
1639 * @param read count - number of bytes to be read
1640 * @param writeData - data to be written
1641 *
1642 * @returns IPMI completion code plus response data
1643 * - readData - i2c response data
1644 */
1645ipmi::RspType<std::vector<uint8_t>>
Willy Tu11d68892022-01-20 10:37:34 -08001646 ipmiMasterWriteRead([[maybe_unused]] bool isPrivateBus, uint3_t busId,
1647 [[maybe_unused]] uint4_t channelNum, bool reserved,
1648 uint7_t slaveAddr, uint8_t readCount,
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301649 std::vector<uint8_t> writeData)
1650{
Jayaprakash Mutyalac2af98b2021-09-14 09:19:11 +00001651 if (reserved)
1652 {
1653 return ipmi::responseInvalidFieldRequest();
1654 }
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301655 if (readCount > maxIPMIWriteReadSize)
1656 {
1657 log<level::ERR>("Master write read command: Read count exceeds limit");
1658 return ipmi::responseParmOutOfRange();
1659 }
1660 const size_t writeCount = writeData.size();
1661 if (!readCount && !writeCount)
1662 {
1663 log<level::ERR>("Master write read command: Read & write count are 0");
1664 return ipmi::responseInvalidFieldRequest();
1665 }
Yong Libd0503a2019-08-22 17:17:17 +08001666#ifdef ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301667 if (!isCmdWhitelisted(static_cast<uint8_t>(busId),
1668 static_cast<uint8_t>(slaveAddr), writeData))
1669 {
1670 log<level::ERR>("Master write read request blocked!",
1671 entry("BUS=%d", static_cast<uint8_t>(busId)),
1672 entry("ADDR=0x%x", static_cast<uint8_t>(slaveAddr)));
1673 return ipmi::responseInvalidFieldRequest();
1674 }
Yong Libd0503a2019-08-22 17:17:17 +08001675#endif // ENABLE_I2C_WHITELIST_CHECK
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301676 std::vector<uint8_t> readBuf(readCount);
1677 std::string i2cBus =
1678 "/dev/i2c-" + std::to_string(static_cast<uint8_t>(busId));
1679
Yong Li7dc4ac02019-08-23 17:44:32 +08001680 ipmi::Cc ret = ipmi::i2cWriteRead(i2cBus, static_cast<uint8_t>(slaveAddr),
1681 writeData, readBuf);
1682 if (ret != ipmi::ccSuccess)
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301683 {
Yong Li7dc4ac02019-08-23 17:44:32 +08001684 return ipmi::response(ret);
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301685 }
1686 return ipmi::responseSuccess(readBuf);
1687}
1688
Chris Austen6caf28b2015-10-13 12:40:40 -05001689void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +05301690{
Vernon Mauery86a50822019-03-25 13:11:36 -07001691 // <Get Device ID>
1692 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1693 ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1694 ipmiAppGetDeviceId);
1695
Tom05732372016-09-06 17:21:23 +05301696 // <Get BT Interface Capabilities>
Vernon Mauerycc99ba42019-03-25 13:40:11 -07001697 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1698 ipmi::app::cmdGetBtIfaceCapabilities,
1699 ipmi::Privilege::User, ipmiAppGetBtCapabilities);
Chris Austen6caf28b2015-10-13 12:40:40 -05001700
Tom05732372016-09-06 17:21:23 +05301701 // <Reset Watchdog Timer>
Vernon Mauery11df4f62019-03-25 14:17:54 -07001702 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1703 ipmi::app::cmdResetWatchdogTimer,
1704 ipmi::Privilege::Operator, ipmiAppResetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001705
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001706 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301707 ipmi::app::cmdGetSessionInfo, ipmi::Privilege::User,
1708 ipmiAppGetSessionInfo);
Rajashekar Gade Reddyf71444d2019-07-25 15:12:17 +00001709
Tom05732372016-09-06 17:21:23 +05301710 // <Set Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001711 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1712 ipmi::app::cmdSetWatchdogTimer,
1713 ipmi::Privilege::Operator, ipmiSetWatchdogTimer);
Chris Austen6caf28b2015-10-13 12:40:40 -05001714
Rajashekar Gade Reddye7023922019-07-10 16:54:55 +00001715 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1716 ipmi::app::cmdCloseSession, ipmi::Privilege::Callback,
1717 ipmiAppCloseSession);
1718
William A. Kennington III73f44512018-02-09 15:28:46 -08001719 // <Get Watchdog Timer>
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +00001720 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301721 ipmi::app::cmdGetWatchdogTimer, ipmi::Privilege::User,
1722 ipmiGetWatchdogTimer);
William A. Kennington III73f44512018-02-09 15:28:46 -08001723
Tom05732372016-09-06 17:21:23 +05301724 // <Get Self Test Results>
Vernon Maueryb84a5282019-03-25 13:39:03 -07001725 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1726 ipmi::app::cmdGetSelfTestResults,
1727 ipmi::Privilege::User, ipmiAppGetSelfTestResults);
Nan Li41fa24a2016-11-10 20:12:37 +08001728
Tom05732372016-09-06 17:21:23 +05301729 // <Get Device GUID>
Vernon Mauery15541322019-03-25 13:33:03 -07001730 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1731 ipmi::app::cmdGetDeviceGuid, ipmi::Privilege::User,
1732 ipmiAppGetDeviceGuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -05001733
Tom05732372016-09-06 17:21:23 +05301734 // <Set ACPI Power State>
Deepak Kumar Sahu520c1312019-05-17 18:14:09 +00001735 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1736 ipmi::app::cmdSetAcpiPowerState,
1737 ipmi::Privilege::Admin, ipmiSetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001738 // <Get ACPI Power State>
Deepak Kumar Sahu4e6d2572019-05-07 19:53:37 +00001739 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1740 ipmi::app::cmdGetAcpiPowerState,
AppaRao Puli5a98ea62019-11-10 21:15:02 +05301741 ipmi::Privilege::User, ipmiGetAcpiPowerState);
Yong Li18d77262018-10-09 01:59:45 +08001742
Richard Marian Thomaiyar84bf9be2019-04-26 22:59:16 +05301743 // Note: For security reason, this command will be registered only when
1744 // there are proper I2C Master write read whitelist
1745 if (populateI2CMasterWRWhitelist())
1746 {
1747 // Note: For security reasons, registering master write read as admin
1748 // privilege command, even though IPMI 2.0 specification allows it as
1749 // operator privilege.
1750 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1751 ipmi::app::cmdMasterWriteRead,
1752 ipmi::Privilege::Admin, ipmiMasterWriteRead);
1753 }
1754
Marri Devender Rao5e007a52018-01-08 06:18:36 -06001755 // <Get System GUID Command>
Vernon Maueryb90a5322019-03-25 13:36:55 -07001756 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1757 ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
1758 ipmiAppGetSystemGuid);
Tom Joseph7cbe2282018-03-21 21:17:33 +05301759
1760 // <Get Channel Cipher Suites Command>
Ayushi Smriti5c3b72c2019-08-30 13:47:31 +00001761 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1762 ipmi::app::cmdGetChannelCipherSuites,
Vernon Mauery79b4eea2019-11-07 09:51:39 -08001763 ipmi::Privilege::None, getChannelCipherSuites);
Vernon Maueryd2a57de2019-03-25 12:45:28 -07001764
Xo Wangf542e8b2017-08-09 15:34:16 -07001765 // <Get System Info Command>
Jia, chunhui98b43ba2019-09-05 15:54:23 +08001766 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1767 ipmi::app::cmdGetSystemInfoParameters,
1768 ipmi::Privilege::User, ipmiAppGetSystemInfo);
Jia, chunhui449f2162019-09-11 16:51:51 +08001769 // <Set System Info Command>
1770 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1771 ipmi::app::cmdSetSystemInfoParameters,
1772 ipmi::Privilege::Admin, ipmiAppSetSystemInfo);
vishwabmcba0bd5f2015-09-30 16:50:23 +05301773 return;
1774}