blob: 9427999bf546314640c37f464c182528d8067245 [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include <arpa/inet.h>
William A. Kennington III194375f2018-12-14 02:14:33 -08002#include <ipmid/api.h>
Xo Wang87651332017-08-11 10:17:59 -07003#include <limits.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07004#include <mapper.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07005#include <systemd/sd-bus.h>
Xo Wang87651332017-08-11 10:17:59 -07006#include <unistd.h>
Patrick Venture0b02be92018-08-31 11:55:55 -07007
Patrick Venture3a5071a2018-09-12 13:27:42 -07008#include <algorithm>
Vernon Mauery0120b682019-03-25 13:08:54 -07009#include <app/channel.hpp>
10#include <app/watchdog.hpp>
11#include <apphandler.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070012#include <array>
13#include <cstddef>
Vernon Mauery0120b682019-03-25 13:08:54 -070014#include <cstdint>
Vernon Mauerybdda8002019-02-26 10:18:51 -080015#include <filesystem>
Patrick Venture3a5071a2018-09-12 13:27:42 -070016#include <fstream>
Vernon Mauery0120b682019-03-25 13:08:54 -070017#include <ipmid/api.hpp>
18#include <ipmid/registration.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -070019#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070020#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070021#include <memory>
Patrick Venture46470a32018-09-07 19:26:25 -070022#include <nlohmann/json.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070023#include <phosphor-logging/elog-errors.hpp>
24#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070025#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070026#include <string>
Vernon Mauery0120b682019-03-25 13:08:54 -070027#include <sys_info_param.hpp>
28#include <transporthandler.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070029#include <tuple>
30#include <vector>
31#include <xyz/openbmc_project/Common/error.hpp>
Yong Li18d77262018-10-09 01:59:45 +080032#include <xyz/openbmc_project/Control/Power/ACPIPowerState/server.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070033#include <xyz/openbmc_project/Software/Activation/server.hpp>
34#include <xyz/openbmc_project/Software/Version/server.hpp>
35#include <xyz/openbmc_project/State/BMC/server.hpp>
Ratan Guptab8e99552017-07-27 07:07:48 +053036
Patrick Venture0b02be92018-08-31 11:55:55 -070037extern sd_bus* bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053038
Alexander Amelkinba19c182018-09-04 15:49:36 +030039constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC";
40constexpr auto bmc_state_property = "CurrentBMCState";
Marri Devender Rao5e007a52018-01-08 06:18:36 -060041constexpr auto bmc_interface = "xyz.openbmc_project.Inventory.Item.Bmc";
42constexpr auto bmc_guid_interface = "xyz.openbmc_project.Common.UUID";
43constexpr auto bmc_guid_property = "UUID";
44constexpr auto bmc_guid_len = 16;
45
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060046static constexpr auto redundancyIntf =
47 "xyz.openbmc_project.Software.RedundancyPriority";
Patrick Venture0b02be92018-08-31 11:55:55 -070048static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060049static constexpr auto activationIntf =
50 "xyz.openbmc_project.Software.Activation";
51static constexpr auto softwareRoot = "/xyz/openbmc_project/software";
52
Chris Austen6caf28b2015-10-13 12:40:40 -050053void register_netfn_app_functions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053054
Ratan Guptab8e99552017-07-27 07:07:48 +053055using namespace phosphor::logging;
56using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060057using Version = sdbusplus::xyz::openbmc_project::Software::server::Version;
58using Activation =
59 sdbusplus::xyz::openbmc_project::Software::server::Activation;
Alexander Amelkinba19c182018-09-04 15:49:36 +030060using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC;
Vernon Mauery185b9f82018-07-20 10:52:36 -070061namespace fs = std::filesystem;
William A. Kennington III4c008022018-10-12 17:18:14 -070062namespace variant_ns = sdbusplus::message::variant_ns;
Ratan Guptab8e99552017-07-27 07:07:48 +053063
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060064/**
65 * @brief Returns the Version info from primary s/w object
66 *
67 * Get the Version info from the active s/w object which is having high
68 * "Priority" value(a smaller number is a higher priority) and "Purpose"
69 * is "BMC" from the list of all s/w objects those are implementing
70 * RedundancyPriority interface from the given softwareRoot path.
71 *
72 * @return On success returns the Version info from primary s/w object.
73 *
74 */
75std::string getActiveSoftwareVersionInfo()
76{
Vernon Mauery86a50822019-03-25 13:11:36 -070077 auto busp = getSdBus();
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060078
79 std::string revision{};
Vernon Mauery86a50822019-03-25 13:11:36 -070080 ipmi::ObjectTree objectTree;
81 try
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060082 {
Vernon Mauery86a50822019-03-25 13:11:36 -070083 objectTree =
84 ipmi::getAllDbusObjects(*busp, softwareRoot, redundancyIntf);
85 }
86 catch (sdbusplus::exception::SdBusError& e)
87 {
88 log<level::ERR>("Failed to fetch redundancy object from dbus",
89 entry("INTERFACE=%s", redundancyIntf),
90 entry("ERRMSG=%s", e.what()));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -060091 elog<InternalFailure>();
92 }
93
94 auto objectFound = false;
95 for (auto& softObject : objectTree)
96 {
Vernon Mauery86a50822019-03-25 13:11:36 -070097 auto service =
98 ipmi::getService(*busp, redundancyIntf, softObject.first);
99 auto objValueTree =
100 ipmi::getManagedObjects(*busp, service, softwareRoot);
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600101
102 auto minPriority = 0xFF;
103 for (const auto& objIter : objValueTree)
104 {
105 try
106 {
107 auto& intfMap = objIter.second;
108 auto& redundancyPriorityProps = intfMap.at(redundancyIntf);
109 auto& versionProps = intfMap.at(versionIntf);
110 auto& activationProps = intfMap.at(activationIntf);
William A. Kennington III4c008022018-10-12 17:18:14 -0700111 auto priority = variant_ns::get<uint8_t>(
112 redundancyPriorityProps.at("Priority"));
113 auto purpose =
114 variant_ns::get<std::string>(versionProps.at("Purpose"));
115 auto activation = variant_ns::get<std::string>(
116 activationProps.at("Activation"));
117 auto version =
118 variant_ns::get<std::string>(versionProps.at("Version"));
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600119 if ((Version::convertVersionPurposeFromString(purpose) ==
120 Version::VersionPurpose::BMC) &&
121 (Activation::convertActivationsFromString(activation) ==
122 Activation::Activations::Active))
123 {
124 if (priority < minPriority)
125 {
126 minPriority = priority;
127 objectFound = true;
128 revision = std::move(version);
129 }
130 }
131 }
132 catch (const std::exception& e)
133 {
134 log<level::ERR>(e.what());
135 }
136 }
137 }
138
139 if (!objectFound)
140 {
141 log<level::ERR>("Could not found an BMC software Object");
142 elog<InternalFailure>();
143 }
144
145 return revision;
146}
147
Alexander Amelkinba19c182018-09-04 15:49:36 +0300148bool getCurrentBmcState()
149{
150 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
151
152 // Get the Inventory object implementing the BMC interface
153 ipmi::DbusObjectInfo bmcObject =
154 ipmi::getDbusObject(bus, bmc_state_interface);
155 auto variant =
156 ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
157 bmc_state_interface, bmc_state_property);
158
William A. Kennington III4c008022018-10-12 17:18:14 -0700159 return variant_ns::holds_alternative<std::string>(variant) &&
160 BMC::convertBMCStateFromString(
161 variant_ns::get<std::string>(variant)) == BMC::BMCState::Ready;
Alexander Amelkinba19c182018-09-04 15:49:36 +0300162}
163
Yong Li18d77262018-10-09 01:59:45 +0800164namespace acpi_state
165{
166using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
167
168const static constexpr char* acpiObjPath =
169 "/xyz/openbmc_project/control/host0/acpi_power_state";
170const static constexpr char* acpiInterface =
171 "xyz.openbmc_project.Control.Power.ACPIPowerState";
172const static constexpr char* sysACPIProp = "SysACPIStatus";
173const static constexpr char* devACPIProp = "DevACPIStatus";
174
175enum class PowerStateType : uint8_t
176{
177 sysPowerState = 0x00,
178 devPowerState = 0x01,
179};
180
181// Defined in 20.6 of ipmi doc
182enum class PowerState : uint8_t
183{
184 s0G0D0 = 0x00,
185 s1D1 = 0x01,
186 s2D2 = 0x02,
187 s3D3 = 0x03,
188 s4 = 0x04,
189 s5G2 = 0x05,
190 s4S5 = 0x06,
191 g3 = 0x07,
192 sleep = 0x08,
193 g1Sleep = 0x09,
194 override = 0x0a,
195 legacyOn = 0x20,
196 legacyOff = 0x21,
197 unknown = 0x2a,
198 noChange = 0x7f,
199};
200
201static constexpr uint8_t stateChanged = 0x80;
202
203struct ACPIState
204{
205 uint8_t sysACPIState;
206 uint8_t devACPIState;
207} __attribute__((packed));
208
209std::map<ACPIPowerState::ACPI, PowerState> dbusToIPMI = {
210 {ACPIPowerState::ACPI::S0_G0_D0, PowerState::s0G0D0},
211 {ACPIPowerState::ACPI::S1_D1, PowerState::s1D1},
212 {ACPIPowerState::ACPI::S2_D2, PowerState::s2D2},
213 {ACPIPowerState::ACPI::S3_D3, PowerState::s3D3},
214 {ACPIPowerState::ACPI::S4, PowerState::s4},
215 {ACPIPowerState::ACPI::S5_G2, PowerState::s5G2},
216 {ACPIPowerState::ACPI::S4_S5, PowerState::s4S5},
217 {ACPIPowerState::ACPI::G3, PowerState::g3},
218 {ACPIPowerState::ACPI::SLEEP, PowerState::sleep},
219 {ACPIPowerState::ACPI::G1_SLEEP, PowerState::g1Sleep},
220 {ACPIPowerState::ACPI::OVERRIDE, PowerState::override},
221 {ACPIPowerState::ACPI::LEGACY_ON, PowerState::legacyOn},
222 {ACPIPowerState::ACPI::LEGACY_OFF, PowerState::legacyOff},
223 {ACPIPowerState::ACPI::Unknown, PowerState::unknown}};
224
225bool isValidACPIState(acpi_state::PowerStateType type, uint8_t state)
226{
227 if (type == acpi_state::PowerStateType::sysPowerState)
228 {
229 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::override)) ||
230 (state == static_cast<uint8_t>(acpi_state::PowerState::legacyOn)) ||
231 (state ==
232 static_cast<uint8_t>(acpi_state::PowerState::legacyOff)) ||
233 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
234 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
235 {
236 return true;
237 }
238 else
239 {
240 return false;
241 }
242 }
243 else if (type == acpi_state::PowerStateType::devPowerState)
244 {
245 if ((state <= static_cast<uint8_t>(acpi_state::PowerState::s3D3)) ||
246 (state == static_cast<uint8_t>(acpi_state::PowerState::unknown)) ||
247 (state == static_cast<uint8_t>(acpi_state::PowerState::noChange)))
248 {
249 return true;
250 }
251 else
252 {
253 return false;
254 }
255 }
256 else
257 {
258 return false;
259 }
260 return false;
261}
262} // namespace acpi_state
263
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500264ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700265 ipmi_request_t request,
266 ipmi_response_t response,
267 ipmi_data_len_t data_len,
268 ipmi_context_t context)
Chris Austen6caf28b2015-10-13 12:40:40 -0500269{
Yong Li18d77262018-10-09 01:59:45 +0800270 auto s = static_cast<uint8_t>(acpi_state::PowerState::unknown);
Chris Austen6caf28b2015-10-13 12:40:40 -0500271 ipmi_ret_t rc = IPMI_CC_OK;
Yong Li18d77262018-10-09 01:59:45 +0800272
273 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
274
275 auto value = acpi_state::ACPIPowerState::ACPI::Unknown;
276
277 auto* req = reinterpret_cast<acpi_state::ACPIState*>(request);
278
279 if (*data_len != sizeof(acpi_state::ACPIState))
280 {
281 log<level::ERR>("set_acpi invalid len");
282 *data_len = 0;
283 return IPMI_CC_REQ_DATA_LEN_INVALID;
284 }
285
Chris Austen6caf28b2015-10-13 12:40:40 -0500286 *data_len = 0;
287
Yong Li18d77262018-10-09 01:59:45 +0800288 if (req->sysACPIState & acpi_state::stateChanged)
289 {
290 // set system power state
291 s = req->sysACPIState & ~acpi_state::stateChanged;
292
293 if (!acpi_state::isValidACPIState(
294 acpi_state::PowerStateType::sysPowerState, s))
295 {
296 log<level::ERR>("set_acpi_power sys invalid input",
297 entry("S=%x", s));
298 return IPMI_CC_PARM_OUT_OF_RANGE;
299 }
300
301 // valid input
302 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
303 {
304 log<level::DEBUG>("No change for system power state");
305 }
306 else
307 {
308 auto found = std::find_if(
309 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
310 [&s](const auto& iter) {
311 return (static_cast<uint8_t>(iter.second) == s);
312 });
313
314 value = found->first;
315
316 try
317 {
318 auto acpiObject =
319 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
320 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
321 acpi_state::acpiInterface,
322 acpi_state::sysACPIProp,
323 convertForMessage(value));
324 }
325 catch (const InternalFailure& e)
326 {
327 log<level::ERR>("Failed in set ACPI system property",
328 entry("EXCEPTION=%s", e.what()));
329 return IPMI_CC_UNSPECIFIED_ERROR;
330 }
331 }
332 }
333 else
334 {
335 log<level::DEBUG>("Do not change system power state");
336 }
337
338 if (req->devACPIState & acpi_state::stateChanged)
339 {
340 // set device power state
341 s = req->devACPIState & ~acpi_state::stateChanged;
342 if (!acpi_state::isValidACPIState(
343 acpi_state::PowerStateType::devPowerState, s))
344 {
345 log<level::ERR>("set_acpi_power dev invalid input",
346 entry("S=%x", s));
347 return IPMI_CC_PARM_OUT_OF_RANGE;
348 }
349
350 // valid input
351 if (s == static_cast<uint8_t>(acpi_state::PowerState::noChange))
352 {
353 log<level::DEBUG>("No change for device power state");
354 }
355 else
356 {
357 auto found = std::find_if(
358 acpi_state::dbusToIPMI.begin(), acpi_state::dbusToIPMI.end(),
359 [&s](const auto& iter) {
360 return (static_cast<uint8_t>(iter.second) == s);
361 });
362
363 value = found->first;
364
365 try
366 {
367 auto acpiObject =
368 ipmi::getDbusObject(bus, acpi_state::acpiInterface);
369 ipmi::setDbusProperty(bus, acpiObject.second, acpiObject.first,
370 acpi_state::acpiInterface,
371 acpi_state::devACPIProp,
372 convertForMessage(value));
373 }
374 catch (const InternalFailure& e)
375 {
376 log<level::ERR>("Failed in set ACPI device property",
377 entry("EXCEPTION=%s", e.what()));
378 return IPMI_CC_UNSPECIFIED_ERROR;
379 }
380 }
381 }
382 else
383 {
384 log<level::DEBUG>("Do not change device power state");
385 }
386
387 return rc;
388}
389
390ipmi_ret_t ipmi_app_get_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
391 ipmi_request_t request,
392 ipmi_response_t response,
393 ipmi_data_len_t data_len,
394 ipmi_context_t context)
395{
396 ipmi_ret_t rc = IPMI_CC_OK;
397
398 auto* res = reinterpret_cast<acpi_state::ACPIState*>(response);
399
400 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
401
402 *data_len = 0;
403
404 try
405 {
406 auto acpiObject = ipmi::getDbusObject(bus, acpi_state::acpiInterface);
407
408 auto sysACPIVal = ipmi::getDbusProperty(
409 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
410 acpi_state::sysACPIProp);
411 auto sysACPI = acpi_state::ACPIPowerState::convertACPIFromString(
William A. Kennington IIIdfad4862018-11-19 17:45:35 -0800412 variant_ns::get<std::string>(sysACPIVal));
Yong Li18d77262018-10-09 01:59:45 +0800413 res->sysACPIState =
414 static_cast<uint8_t>(acpi_state::dbusToIPMI.at(sysACPI));
415
416 auto devACPIVal = ipmi::getDbusProperty(
417 bus, acpiObject.second, acpiObject.first, acpi_state::acpiInterface,
418 acpi_state::devACPIProp);
419 auto devACPI = acpi_state::ACPIPowerState::convertACPIFromString(
William A. Kennington IIIdfad4862018-11-19 17:45:35 -0800420 variant_ns::get<std::string>(devACPIVal));
Yong Li18d77262018-10-09 01:59:45 +0800421 res->devACPIState =
422 static_cast<uint8_t>(acpi_state::dbusToIPMI.at(devACPI));
423
424 *data_len = sizeof(acpi_state::ACPIState);
425 }
426 catch (const InternalFailure& e)
427 {
428 log<level::ERR>("Failed in get ACPI property");
429 return IPMI_CC_UNSPECIFIED_ERROR;
430 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500431 return rc;
432}
433
Chris Austen7303bdc2016-04-17 11:50:54 -0500434typedef struct
435{
436 char major;
437 char minor;
Chris Austen176c9652016-04-30 16:32:17 -0500438 uint16_t d[2];
Vernon Mauery86a50822019-03-25 13:11:36 -0700439} Revision;
Chris Austen7303bdc2016-04-17 11:50:54 -0500440
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600441/* Currently supports the vx.x-x-[-x] and v1.x.x-x-[-x] format. It will */
442/* return -1 if not in those formats, this routine knows how to parse */
Chris Austen7303bdc2016-04-17 11:50:54 -0500443/* version = v0.6-19-gf363f61-dirty */
444/* ^ ^ ^^ ^ */
445/* | | |----------|-- additional details */
446/* | |---------------- Minor */
447/* |------------------ Major */
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600448/* and version = v1.99.10-113-g65edf7d-r3-0-g9e4f715 */
449/* ^ ^ ^^ ^ */
450/* | | |--|---------- additional details */
451/* | |---------------- Minor */
452/* |------------------ Major */
Chris Austen7303bdc2016-04-17 11:50:54 -0500453/* Additional details : If the option group exists it will force Auxiliary */
454/* Firmware Revision Information 4th byte to 1 indicating the build was */
455/* derived with additional edits */
Vernon Mauery86a50822019-03-25 13:11:36 -0700456int convertVersion(std::string s, Revision& rev)
Chris Austen7303bdc2016-04-17 11:50:54 -0500457{
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600458 std::string token;
Chris Austen176c9652016-04-30 16:32:17 -0500459 uint16_t commits;
Chris Austen7303bdc2016-04-17 11:50:54 -0500460
Patrick Venture0b02be92018-08-31 11:55:55 -0700461 auto location = s.find_first_of('v');
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600462 if (location != std::string::npos)
463 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700464 s = s.substr(location + 1);
Chris Austen176c9652016-04-30 16:32:17 -0500465 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500466
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600467 if (!s.empty())
468 {
469 location = s.find_first_of(".");
470 if (location != std::string::npos)
471 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700472 rev.major =
Patrick Venture0b02be92018-08-31 11:55:55 -0700473 static_cast<char>(std::stoi(s.substr(0, location), 0, 16));
474 token = s.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600475 }
Chris Austen176c9652016-04-30 16:32:17 -0500476
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600477 if (!token.empty())
478 {
479 location = token.find_first_of(".-");
480 if (location != std::string::npos)
481 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700482 rev.minor = static_cast<char>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700483 std::stoi(token.substr(0, location), 0, 16));
484 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600485 }
486 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500487
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600488 // Capture the number of commits on top of the minor tag.
489 // I'm using BE format like the ipmi spec asked for
490 location = token.find_first_of(".-");
491 if (!token.empty())
492 {
493 commits = std::stoi(token.substr(0, location), 0, 16);
Vernon Mauery86a50822019-03-25 13:11:36 -0700494 rev.d[0] = (commits >> 8) | (commits << 8);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600495
496 // commit number we skip
497 location = token.find_first_of(".-");
498 if (location != std::string::npos)
499 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700500 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600501 }
502 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700503 else
504 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700505 rev.d[0] = 0;
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600506 }
507
508 if (location != std::string::npos)
509 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700510 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600511 }
512
513 // Any value of the optional parameter forces it to 1
514 location = token.find_first_of(".-");
515 if (location != std::string::npos)
516 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700517 token = token.substr(location + 1);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600518 }
519 commits = (!token.empty()) ? 1 : 0;
520
Patrick Venture0b02be92018-08-31 11:55:55 -0700521 // We do this operation to get this displayed in least significant bytes
522 // of ipmitool device id command.
Vernon Mauery86a50822019-03-25 13:11:36 -0700523 rev.d[1] = (commits >> 8) | (commits << 8);
Dinesh Chinari2b7e07d2017-11-08 15:38:50 -0600524 }
525
Chris Austen7303bdc2016-04-17 11:50:54 -0500526 return 0;
527}
528
Vernon Mauery86a50822019-03-25 13:11:36 -0700529auto ipmiAppGetDeviceId() -> ipmi::RspType<uint8_t, // Device ID
530 uint8_t, // Device Revision
531 uint8_t, // Firmware Revision Major
532 uint8_t, // Firmware Revision minor
533 uint8_t, // IPMI version
534 uint8_t, // Additional device support
535 uint24_t, // MFG ID
536 uint16_t, // Product ID
537 uint32_t // AUX info
538 >
Chris Austen6caf28b2015-10-13 12:40:40 -0500539{
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600540 int r = -1;
Vernon Mauery86a50822019-03-25 13:11:36 -0700541 Revision rev = {0};
542 static struct
543 {
544 uint8_t id;
545 uint8_t revision;
546 uint8_t fw[2];
547 uint8_t ipmiVer;
548 uint8_t addnDevSupport;
549 uint24_t manufId;
550 uint16_t prodId;
551 uint32_t aux;
552 } devId;
David Cobbleya1adb072017-11-21 15:58:13 -0800553 static bool dev_id_initialized = false;
554 const char* filename = "/usr/share/ipmi-providers/dev_id.json";
Alexander Amelkinba19c182018-09-04 15:49:36 +0300555 constexpr auto ipmiDevIdStateShift = 7;
556 constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift);
Chris Austen6caf28b2015-10-13 12:40:40 -0500557
David Cobbleya1adb072017-11-21 15:58:13 -0800558 if (!dev_id_initialized)
559 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600560 try
561 {
562 auto version = getActiveSoftwareVersionInfo();
Vernon Mauery86a50822019-03-25 13:11:36 -0700563 r = convertVersion(version, rev);
David Cobbleya1adb072017-11-21 15:58:13 -0800564 }
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600565 catch (const std::exception& e)
566 {
567 log<level::ERR>(e.what());
568 }
Nan Liee0cb902016-07-11 15:38:03 +0800569
Patrick Venture0b02be92018-08-31 11:55:55 -0700570 if (r >= 0)
571 {
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600572 // bit7 identifies if the device is available
573 // 0=normal operation
574 // 1=device firmware, SDR update,
575 // or self-initialization in progress.
Alexander Amelkinba19c182018-09-04 15:49:36 +0300576 // The availability may change in run time, so mask here
577 // and initialize later.
Vernon Mauery86a50822019-03-25 13:11:36 -0700578 devId.fw[0] = rev.major & ipmiDevIdFw1Mask;
Nagaraju Goruganti744398d2018-02-20 09:52:00 -0600579
580 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Vernon Mauery86a50822019-03-25 13:11:36 -0700581 devId.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
582 std::memcpy(&devId.aux, rev.d, 4);
David Cobbleya1adb072017-11-21 15:58:13 -0800583 }
Nan Liee0cb902016-07-11 15:38:03 +0800584
David Cobbleya1adb072017-11-21 15:58:13 -0800585 // IPMI Spec version 2.0
Vernon Mauery86a50822019-03-25 13:11:36 -0700586 devId.ipmiVer = 2;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500587
Vernon Mauery86a50822019-03-25 13:11:36 -0700588 std::ifstream devIdFile(filename);
589 if (devIdFile.is_open())
David Cobbleya1adb072017-11-21 15:58:13 -0800590 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700591 auto data = nlohmann::json::parse(devIdFile, nullptr, false);
David Cobbleya1adb072017-11-21 15:58:13 -0800592 if (!data.is_discarded())
593 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700594 devId.id = data.value("id", 0);
595 devId.revision = data.value("revision", 0);
596 devId.addnDevSupport = data.value("addn_dev_support", 0);
597 devId.manufId = data.value("manuf_id", 0);
598 devId.prodId = data.value("prod_id", 0);
599 devId.aux = data.value("aux", 0);
David Cobbleya1adb072017-11-21 15:58:13 -0800600
Patrick Venture0b02be92018-08-31 11:55:55 -0700601 // Don't read the file every time if successful
David Cobbleya1adb072017-11-21 15:58:13 -0800602 dev_id_initialized = true;
603 }
604 else
605 {
606 log<level::ERR>("Device ID JSON parser failure");
Vernon Mauery86a50822019-03-25 13:11:36 -0700607 return ipmi::response(ipmi::ccUnspecifiedError);
David Cobbleya1adb072017-11-21 15:58:13 -0800608 }
609 }
610 else
611 {
612 log<level::ERR>("Device ID file not found");
Vernon Mauery86a50822019-03-25 13:11:36 -0700613 return ipmi::response(ipmi::ccUnspecifiedError);
Chris Austen7303bdc2016-04-17 11:50:54 -0500614 }
615 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500616
Alexander Amelkinba19c182018-09-04 15:49:36 +0300617 // Set availability to the actual current BMC state
Vernon Mauery86a50822019-03-25 13:11:36 -0700618 devId.fw[0] &= ipmiDevIdFw1Mask;
Alexander Amelkinba19c182018-09-04 15:49:36 +0300619 if (!getCurrentBmcState())
620 {
Vernon Mauery86a50822019-03-25 13:11:36 -0700621 devId.fw[0] |= (1 << ipmiDevIdStateShift);
Alexander Amelkinba19c182018-09-04 15:49:36 +0300622 }
623
Vernon Mauery86a50822019-03-25 13:11:36 -0700624 return ipmi::responseSuccess(
625 devId.id, devId.revision, devId.fw[0], devId.fw[1], devId.ipmiVer,
626 devId.addnDevSupport, devId.manufId, devId.prodId, devId.aux);
Chris Austen6caf28b2015-10-13 12:40:40 -0500627}
628
Vernon Maueryb84a5282019-03-25 13:39:03 -0700629auto ipmiAppGetSelfTestResults() -> ipmi::RspType<uint8_t, uint8_t>
Nan Li41fa24a2016-11-10 20:12:37 +0800630{
Nan Li41fa24a2016-11-10 20:12:37 +0800631 // Byte 2:
632 // 55h - No error.
Gunnar Mills8991dd62017-10-25 17:11:29 -0500633 // 56h - Self Test function not implemented in this controller.
Nan Li41fa24a2016-11-10 20:12:37 +0800634 // 57h - Corrupted or inaccesssible data or devices.
635 // 58h - Fatal hardware error.
636 // FFh - reserved.
637 // all other: Device-specific 'internal failure'.
638 // Byte 3:
639 // For byte 2 = 55h, 56h, FFh: 00h
640 // For byte 2 = 58h, all other: Device-specific
641 // For byte 2 = 57h: self-test error bitfield.
642 // Note: returning 57h does not imply that all test were run.
643 // [7] 1b = Cannot access SEL device.
644 // [6] 1b = Cannot access SDR Repository.
645 // [5] 1b = Cannot access BMC FRU device.
646 // [4] 1b = IPMB signal lines do not respond.
647 // [3] 1b = SDR Repository empty.
648 // [2] 1b = Internal Use Area of BMC FRU corrupted.
649 // [1] 1b = controller update 'boot block' firmware corrupted.
650 // [0] 1b = controller operational firmware corrupted.
Vernon Maueryb84a5282019-03-25 13:39:03 -0700651 constexpr uint8_t notImplemented = 0x56;
652 constexpr uint8_t zero = 0;
653 return ipmi::responseSuccess(notImplemented, zero);
Nan Li41fa24a2016-11-10 20:12:37 +0800654}
655
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500656ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700657 ipmi_request_t request,
658 ipmi_response_t response,
659 ipmi_data_len_t data_len,
660 ipmi_context_t context)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500661{
Patrick Venture0b02be92018-08-31 11:55:55 -0700662 const char* objname = "/org/openbmc/control/chassis0";
663 const char* iface = "org.freedesktop.DBus.Properties";
664 const char* chassis_iface = "org.openbmc.control.Chassis";
665 sd_bus_message* reply = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500666 sd_bus_error error = SD_BUS_ERROR_NULL;
667 int r = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700668 char* uuid = NULL;
669 char* busname = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500670
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500671 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Ventured2117022018-02-06 08:54:37 -0800672 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
673 // order
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500674 // Ex: 0x2332fc2c40e66298e511f2782395a361
675
Patrick Venture0b02be92018-08-31 11:55:55 -0700676 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500677 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
Patrick Ventured2117022018-02-06 08:54:37 -0800678 // Point resp end of array to save in reverse order
Patrick Venture0b02be92018-08-31 11:55:55 -0700679 int resp_loc = resp_size - 1;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500680 int i = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -0700681 char* tokptr = NULL;
682 char* id_octet = NULL;
Emily Shafferedb8bb02018-09-27 14:50:15 -0700683 size_t total_uuid_size = 0;
684 // 1 byte of resp is built from 2 chars of uuid.
685 constexpr size_t max_uuid_size = 2 * resp_size;
vishwa1eaea4f2016-02-26 11:57:40 -0600686
687 // Status code.
688 ipmi_ret_t rc = IPMI_CC_OK;
689 *data_len = 0;
690
vishwa1eaea4f2016-02-26 11:57:40 -0600691 // Call Get properties method with the interface and property name
Sergey Solomineb9b8142016-08-23 09:07:28 -0500692 r = mapper_get_service(bus, objname, &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700693 if (r < 0)
694 {
695 log<level::ERR>("Failed to get bus name", entry("BUS=%s", objname),
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530696 entry("ERRNO=0x%X", -r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500697 goto finish;
698 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700699 r = sd_bus_call_method(bus, busname, objname, iface, "Get", &error, &reply,
700 "ss", chassis_iface, "uuid");
vishwa1eaea4f2016-02-26 11:57:40 -0600701 if (r < 0)
702 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700703 log<level::ERR>("Failed to call Get Method", entry("ERRNO=0x%X", -r));
vishwa1eaea4f2016-02-26 11:57:40 -0600704 rc = IPMI_CC_UNSPECIFIED_ERROR;
705 goto finish;
706 }
707
708 r = sd_bus_message_read(reply, "v", "s", &uuid);
709 if (r < 0 || uuid == NULL)
710 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700711 log<level::ERR>("Failed to get a response", entry("ERRNO=0x%X", -r));
vishwa1eaea4f2016-02-26 11:57:40 -0600712 rc = IPMI_CC_RESPONSE_ERROR;
713 goto finish;
714 }
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500715
716 // Traverse the UUID
Patrick Ventured2117022018-02-06 08:54:37 -0800717 // Get the UUID octects separated by dash
718 id_octet = strtok_r(uuid, "-", &tokptr);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500719
720 if (id_octet == NULL)
vishwa1eaea4f2016-02-26 11:57:40 -0600721 {
722 // Error
Patrick Venture0b02be92018-08-31 11:55:55 -0700723 log<level::ERR>("Unexpected UUID format", entry("UUID=%s", uuid));
vishwa1eaea4f2016-02-26 11:57:40 -0600724 rc = IPMI_CC_RESPONSE_ERROR;
725 goto finish;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500726 }
727
728 while (id_octet != NULL)
729 {
730 // Calculate the octet string size since it varies
731 // Divide it by 2 for the array size since 1 byte is built from 2 chars
Patrick Venture0b02be92018-08-31 11:55:55 -0700732 int tmp_size = strlen(id_octet) / 2;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500733
Emily Shafferedb8bb02018-09-27 14:50:15 -0700734 // Check if total UUID size has been exceeded
735 if ((total_uuid_size += strlen(id_octet)) > max_uuid_size)
736 {
737 // Error - UUID too long to store
738 log<level::ERR>("UUID too long", entry("UUID=%s", uuid));
739 rc = IPMI_CC_RESPONSE_ERROR;
740 goto finish;
741 }
742
Patrick Venture0b02be92018-08-31 11:55:55 -0700743 for (i = 0; i < tmp_size; i++)
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500744 {
Patrick Ventured2117022018-02-06 08:54:37 -0800745 // Holder of the 2 chars that will become a byte
746 char tmp_array[3] = {0};
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500747 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
748
749 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
Patrick Ventured2117022018-02-06 08:54:37 -0800750 // Copy end to first
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700751 std::memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500752 resp_loc--;
Patrick Venture0b02be92018-08-31 11:55:55 -0700753 id_octet += 2; // Finished with the 2 chars, advance
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500754 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700755 id_octet = strtok_r(NULL, "-", &tokptr); // Get next octet
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500756 }
757
758 // Data length
759 *data_len = resp_size;
760
761 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700762 std::memcpy(response, &resp_uuid, *data_len);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500763
vishwa1eaea4f2016-02-26 11:57:40 -0600764finish:
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500765 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600766 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500767 free(busname);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500768
769 return rc;
770}
Chris Austen6caf28b2015-10-13 12:40:40 -0500771
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500772ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700773 ipmi_request_t request,
774 ipmi_response_t response,
775 ipmi_data_len_t data_len,
776 ipmi_context_t context)
vishwabmcba0bd5f2015-09-30 16:50:23 +0530777{
vishwabmcba0bd5f2015-09-30 16:50:23 +0530778
779 // Status code.
780 ipmi_ret_t rc = IPMI_CC_OK;
781
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600782 // Per IPMI 2.0 spec, the input and output buffer size must be the max
783 // buffer size minus one byte to allocate space for the length byte.
Patrick Venture0b02be92018-08-31 11:55:55 -0700784 uint8_t str[] = {0x01, MAX_IPMI_BUFFER - 1, MAX_IPMI_BUFFER - 1, 0x0A,
785 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530786
787 // Data length
788 *data_len = sizeof(str);
789
790 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700791 std::memcpy(response, &str, *data_len);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530792
793 return rc;
794}
795
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500796ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700797 ipmi_request_t request,
798 ipmi_response_t response,
799 ipmi_data_len_t data_len,
800 ipmi_context_t context)
vishwabmcba0bd5f2015-09-30 16:50:23 +0530801{
vishwabmcba0bd5f2015-09-30 16:50:23 +0530802 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800803 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530804
805 *data_len = strlen("THIS IS WILDCARD");
806
807 // Now pack actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700808 std::memcpy(response, "THIS IS WILDCARD", *data_len);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530809
810 return rc;
811}
812
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600813ipmi_ret_t ipmi_app_get_sys_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700814 ipmi_request_t request,
815 ipmi_response_t response,
816 ipmi_data_len_t data_len,
817 ipmi_context_t context)
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600818
819{
820 ipmi_ret_t rc = IPMI_CC_OK;
821 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
822
823 try
824 {
825 // Get the Inventory object implementing BMC interface
826 ipmi::DbusObjectInfo bmcObject =
827 ipmi::getDbusObject(bus, bmc_interface);
828
829 // Read UUID property value from bmcObject
830 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Patrick Venture0b02be92018-08-31 11:55:55 -0700831 auto variant =
832 ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
833 bmc_guid_interface, bmc_guid_property);
William A. Kennington III4c008022018-10-12 17:18:14 -0700834 std::string guidProp = variant_ns::get<std::string>(variant);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600835
836 // Erase "-" characters from the property value
837 guidProp.erase(std::remove(guidProp.begin(), guidProp.end(), '-'),
Patrick Venture0b02be92018-08-31 11:55:55 -0700838 guidProp.end());
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600839
840 auto guidPropLen = guidProp.length();
841 // Validate UUID data
842 // Divide by 2 as 1 byte is built from 2 chars
Patrick Venture0b02be92018-08-31 11:55:55 -0700843 if ((guidPropLen <= 0) || ((guidPropLen / 2) != bmc_guid_len))
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600844
845 {
846 log<level::ERR>("Invalid UUID property value",
Patrick Venture0b02be92018-08-31 11:55:55 -0700847 entry("UUID_LENGTH=%d", guidPropLen));
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600848 return IPMI_CC_RESPONSE_ERROR;
849 }
850
851 // Convert data in RFC4122(MSB) format to LSB format
852 // Get 2 characters at a time as 1 byte is built from 2 chars and
853 // convert to hex byte
854 // TODO: Data printed for GUID command is not as per the
855 // GUID format defined in IPMI specification 2.0 section 20.8
856 // Ticket raised: https://sourceforge.net/p/ipmitool/bugs/501/
857 uint8_t respGuid[bmc_guid_len];
858 for (size_t i = 0, respLoc = (bmc_guid_len - 1);
Patrick Venture0b02be92018-08-31 11:55:55 -0700859 i < guidPropLen && respLoc >= 0; i += 2, respLoc--)
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600860 {
861 auto value = static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700862 std::stoi(guidProp.substr(i, 2).c_str(), NULL, 16));
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600863 respGuid[respLoc] = value;
864 }
865
866 *data_len = bmc_guid_len;
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700867 std::memcpy(response, &respGuid, bmc_guid_len);
Marri Devender Rao5e007a52018-01-08 06:18:36 -0600868 }
869 catch (const InternalFailure& e)
870 {
871 log<level::ERR>("Failed in reading BMC UUID property",
872 entry("INTERFACE=%s", bmc_interface),
873 entry("PROPERTY_INTERFACE=%s", bmc_guid_interface),
874 entry("PROPERTY=%s", bmc_guid_property));
875 return IPMI_CC_UNSPECIFIED_ERROR;
876 }
877 return rc;
878}
879
Xo Wangf542e8b2017-08-09 15:34:16 -0700880static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
881
Xo Wang87651332017-08-11 10:17:59 -0700882static std::string sysInfoReadSystemName()
883{
884 // Use the BMC hostname as the "System Name."
885 char hostname[HOST_NAME_MAX + 1] = {};
886 if (gethostname(hostname, HOST_NAME_MAX) != 0)
887 {
888 perror("System info parameter: system name");
889 }
890 return hostname;
891}
892
Xo Wangf542e8b2017-08-09 15:34:16 -0700893struct IpmiSysInfoResp
894{
895 uint8_t paramRevision;
896 uint8_t setSelector;
897 union
898 {
899 struct
900 {
901 uint8_t encoding;
902 uint8_t stringLen;
903 uint8_t stringData0[14];
904 } __attribute__((packed));
905 uint8_t stringDataN[16];
906 uint8_t byteData;
907 };
908} __attribute__((packed));
909
910/**
911 * Split a string into (up to) 16-byte chunks as expected in response for get
912 * system info parameter.
913 *
914 * @param[in] fullString: Input string to be split
915 * @param[in] chunkIndex: Index of the chunk to be written out
916 * @param[in,out] chunk: Output data buffer; must have 14 byte capacity if
917 * chunk_index = 0 and 16-byte capacity otherwise
918 * @return the number of bytes written into the output buffer, or -EINVAL for
919 * invalid arguments.
920 */
921static int splitStringParam(const std::string& fullString, int chunkIndex,
922 uint8_t* chunk)
923{
924 constexpr int maxChunk = 255;
925 constexpr int smallChunk = 14;
926 constexpr int chunkSize = 16;
927 if (chunkIndex > maxChunk || chunk == nullptr)
928 {
929 return -EINVAL;
930 }
931 try
932 {
933 std::string output;
934 if (chunkIndex == 0)
935 {
936 // Output must have 14 byte capacity.
937 output = fullString.substr(0, smallChunk);
938 }
939 else
940 {
941 // Output must have 16 byte capacity.
942 output = fullString.substr((chunkIndex * chunkSize) - 2, chunkSize);
943 }
944
945 std::memcpy(chunk, output.c_str(), output.length());
946 return output.length();
947 }
948 catch (const std::out_of_range& e)
949 {
950 // The position was beyond the end.
951 return -EINVAL;
952 }
953}
954
955/**
956 * Packs the Get Sys Info Request Item into the response.
957 *
958 * @param[in] paramString - the parameter.
959 * @param[in] setSelector - the selector
960 * @param[in,out] resp - the System info response.
961 * @return The number of bytes packed or failure from splitStringParam().
962 */
963static int packGetSysInfoResp(const std::string& paramString,
964 uint8_t setSelector, IpmiSysInfoResp* resp)
965{
966 uint8_t* dataBuffer = resp->stringDataN;
967 resp->setSelector = setSelector;
968 if (resp->setSelector == 0) // First chunk has only 14 bytes.
969 {
970 resp->encoding = 0;
971 resp->stringLen = paramString.length();
972 dataBuffer = resp->stringData0;
973 }
974 return splitStringParam(paramString, resp->setSelector, dataBuffer);
975}
976
977ipmi_ret_t ipmi_app_get_system_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
978 ipmi_request_t request,
979 ipmi_response_t response,
980 ipmi_data_len_t dataLen,
981 ipmi_context_t context)
982{
983 IpmiSysInfoResp resp = {};
984 size_t respLen = 0;
985 uint8_t* const reqData = static_cast<uint8_t*>(request);
986 std::string paramString;
987 bool found;
988 std::tuple<bool, std::string> ret;
989 constexpr int minRequestSize = 4;
990 constexpr int paramSelector = 1;
991 constexpr uint8_t revisionOnly = 0x80;
992 const uint8_t paramRequested = reqData[paramSelector];
993 int rc;
994
995 if (*dataLen < minRequestSize)
996 {
997 return IPMI_CC_REQ_DATA_LEN_INVALID;
998 }
999
1000 *dataLen = 0; // default to 0.
1001
1002 // Parameters revision as of IPMI spec v2.0 rev. 1.1 (Feb 11, 2014 E6)
1003 resp.paramRevision = 0x11;
1004 if (reqData[0] & revisionOnly) // Get parameter revision only
1005 {
1006 respLen = 1;
1007 goto writeResponse;
1008 }
1009
1010 // The "Set In Progress" parameter can be used for rollback of parameter
1011 // data and is not implemented.
1012 if (paramRequested == 0)
1013 {
1014 resp.byteData = 0;
1015 respLen = 2;
1016 goto writeResponse;
1017 }
1018
1019 if (sysInfoParamStore == nullptr)
1020 {
1021 sysInfoParamStore = std::make_unique<SysInfoParamStore>();
Xo Wang87651332017-08-11 10:17:59 -07001022 sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME,
1023 sysInfoReadSystemName);
Xo Wangf542e8b2017-08-09 15:34:16 -07001024 }
1025
1026 // Parameters other than Set In Progress are assumed to be strings.
1027 ret = sysInfoParamStore->lookup(paramRequested);
1028 found = std::get<0>(ret);
1029 paramString = std::get<1>(ret);
1030 if (!found)
1031 {
1032 return IPMI_CC_SYSTEM_INFO_PARAMETER_NOT_SUPPORTED;
1033 }
1034 // TODO: Cache each parameter across multiple calls, until the whole string
1035 // has been read out. Otherwise, it's possible for a parameter to change
1036 // between requests for its chunks, returning chunks incoherent with each
1037 // other. For now, the parameter store is simply required to have only
1038 // idempotent callbacks.
1039 rc = packGetSysInfoResp(paramString, reqData[2], &resp);
1040 if (rc == -EINVAL)
1041 {
1042 return IPMI_CC_RESPONSE_ERROR;
1043 }
1044
1045 respLen = sizeof(resp); // Write entire string data chunk in response.
1046
1047writeResponse:
1048 std::memcpy(response, &resp, sizeof(resp));
1049 *dataLen = respLen;
1050 return IPMI_CC_OK;
1051}
1052
Chris Austen6caf28b2015-10-13 12:40:40 -05001053void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +05301054{
Vernon Mauery86a50822019-03-25 13:11:36 -07001055 // <Get Device ID>
1056 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1057 ipmi::app::cmdGetDeviceId, ipmi::Privilege::User,
1058 ipmiAppGetDeviceId);
1059
Tom05732372016-09-06 17:21:23 +05301060 // <Get BT Interface Capabilities>
Patrick Venture0b02be92018-08-31 11:55:55 -07001061 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL,
1062 ipmi_app_get_bt_capabilities, PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -05001063
Tom05732372016-09-06 17:21:23 +05301064 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001065 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL,
1066 ipmi_app_wildcard_handler, PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -05001067
Tom05732372016-09-06 17:21:23 +05301068 // <Reset Watchdog Timer>
Patrick Venture0b02be92018-08-31 11:55:55 -07001069 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL,
1070 ipmi_app_watchdog_reset, PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -05001071
Tom05732372016-09-06 17:21:23 +05301072 // <Set Watchdog Timer>
Patrick Venture0b02be92018-08-31 11:55:55 -07001073 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL,
1074 ipmi_app_watchdog_set, PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -05001075
William A. Kennington III73f44512018-02-09 15:28:46 -08001076 // <Get Watchdog Timer>
Patrick Venture0b02be92018-08-31 11:55:55 -07001077 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_WD, NULL,
1078 ipmi_app_watchdog_get, PRIVILEGE_OPERATOR);
William A. Kennington III73f44512018-02-09 15:28:46 -08001079
Tom05732372016-09-06 17:21:23 +05301080 // <Get Self Test Results>
Vernon Maueryb84a5282019-03-25 13:39:03 -07001081 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
1082 ipmi::app::cmdGetSelfTestResults,
1083 ipmi::Privilege::User, ipmiAppGetSelfTestResults);
Nan Li41fa24a2016-11-10 20:12:37 +08001084
Tom05732372016-09-06 17:21:23 +05301085 // <Get Device GUID>
Patrick Venture0b02be92018-08-31 11:55:55 -07001086 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL,
1087 ipmi_app_get_device_guid, PRIVILEGE_USER);
Adriana Kobylakd100ee52015-10-20 17:02:37 -05001088
Tom05732372016-09-06 17:21:23 +05301089 // <Set ACPI Power State>
Patrick Venture0b02be92018-08-31 11:55:55 -07001090 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL,
1091 ipmi_app_set_acpi_power_state, PRIVILEGE_ADMIN);
Chris Austen6caf28b2015-10-13 12:40:40 -05001092
Yong Li18d77262018-10-09 01:59:45 +08001093 // <Get ACPI Power State>
1094 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_ACPI, NULL,
1095 ipmi_app_get_acpi_power_state, PRIVILEGE_ADMIN);
1096
Marri Devender Rao5e007a52018-01-08 06:18:36 -06001097 // <Get System GUID Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001098 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SYS_GUID, NULL,
1099 ipmi_app_get_sys_guid, PRIVILEGE_USER);
Tom Joseph7cbe2282018-03-21 21:17:33 +05301100
1101 // <Get Channel Cipher Suites Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001102 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_CIPHER_SUITES, NULL,
1103 getChannelCipherSuites, PRIVILEGE_CALLBACK);
Vernon Maueryd2a57de2019-03-25 12:45:28 -07001104
Xo Wangf542e8b2017-08-09 15:34:16 -07001105 // <Get System Info Command>
1106 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SYSTEM_INFO, NULL,
1107 ipmi_app_get_system_info, PRIVILEGE_USER);
vishwabmcba0bd5f2015-09-30 16:50:23 +05301108 return;
1109}