blob: c3ca67c4f715fe540ab9d23d434ed22ee329685b [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinlercad9c2b2019-12-02 15:42:01 -060016#include "config.h"
17
Matt Spinlerc8705e22019-09-11 12:36:07 -050018#include "data_interface.hpp"
19
Matt Spinlerf61f2922020-06-23 11:32:49 -050020#include "util.hpp"
21
Vijay Lobo81b4dca2021-04-29 00:04:00 -050022#include <fmt/format.h>
23
Matt Spinlercad9c2b2019-12-02 15:42:01 -060024#include <fstream>
Vijay Lobo81b4dca2021-04-29 00:04:00 -050025#include <phosphor-logging/log.hpp>
Matt Spinlerf10068d2020-12-02 10:44:08 -060026#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
Matt Spinlera7d9d962019-11-06 15:01:25 -060027
Matt Spinlerc8705e22019-09-11 12:36:07 -050028namespace openpower
29{
30namespace pels
31{
32
33namespace service_name
34{
35constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -050036constexpr auto vpdManager = "com.ibm.VPD.Manager";
Matt Spinler34a904c2020-08-05 14:53:28 -050037constexpr auto ledGroupManager = "xyz.openbmc_project.LED.GroupManager";
Matt Spinlerc8705e22019-09-11 12:36:07 -050038} // namespace service_name
39
40namespace object_path
41{
42constexpr auto objectMapper = "/xyz/openbmc_project/object_mapper";
43constexpr auto systemInv = "/xyz/openbmc_project/inventory/system";
Matt Spinler6ea4d5f2020-05-20 13:31:07 -050044constexpr auto chassisInv = "/xyz/openbmc_project/inventory/system/chassis";
Vijay Lobo81b4dca2021-04-29 00:04:00 -050045constexpr auto motherBoardInv =
46 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Matt Spinlerb3d488f2020-02-21 15:30:46 -060047constexpr auto baseInv = "/xyz/openbmc_project/inventory";
Matt Spinler4aa23a12020-02-03 15:05:09 -060048constexpr auto bmcState = "/xyz/openbmc_project/state/bmc0";
49constexpr auto chassisState = "/xyz/openbmc_project/state/chassis0";
Matt Spinlera7d9d962019-11-06 15:01:25 -060050constexpr auto hostState = "/xyz/openbmc_project/state/host0";
Matt Spinlerb3f51862019-12-09 13:55:10 -060051constexpr auto pldm = "/xyz/openbmc_project/pldm";
Matt Spinler9cf3cfd2020-02-03 14:41:55 -060052constexpr auto enableHostPELs =
53 "/xyz/openbmc_project/logging/send_event_logs_to_host";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -050054constexpr auto vpdManager = "/com/ibm/VPD/Manager";
Matt Spinlerc8705e22019-09-11 12:36:07 -050055} // namespace object_path
56
57namespace interface
58{
59constexpr auto dbusProperty = "org.freedesktop.DBus.Properties";
60constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper";
61constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset";
Matt Spinlerf10068d2020-12-02 10:44:08 -060062constexpr auto bootProgress = "xyz.openbmc_project.State.Boot.Progress";
Matt Spinlerb3f51862019-12-09 13:55:10 -060063constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester";
Matt Spinler9cf3cfd2020-02-03 14:41:55 -060064constexpr auto enable = "xyz.openbmc_project.Object.Enable";
Matt Spinler4aa23a12020-02-03 15:05:09 -060065constexpr auto bmcState = "xyz.openbmc_project.State.BMC";
66constexpr auto chassisState = "xyz.openbmc_project.State.Chassis";
67constexpr auto hostState = "xyz.openbmc_project.State.Host";
Matt Spinlerb3d488f2020-02-21 15:30:46 -060068constexpr auto invMotherboard =
69 "xyz.openbmc_project.Inventory.Item.Board.Motherboard";
70constexpr auto viniRecordVPD = "com.ibm.ipzvpd.VINI";
Matt Spinler60c4e792020-03-13 13:45:36 -050071constexpr auto locCode = "com.ibm.ipzvpd.Location";
Matt Spinler1ab66962020-10-29 13:21:44 -050072constexpr auto compatible =
73 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -050074constexpr auto vpdManager = "com.ibm.VPD.Manager";
Matt Spinler34a904c2020-08-05 14:53:28 -050075constexpr auto ledGroup = "xyz.openbmc_project.Led.Group";
Matt Spinler993168d2021-04-07 16:05:03 -050076constexpr auto operationalStatus =
77 "xyz.openbmc_project.State.Decorator.OperationalStatus";
Matt Spinlerc8705e22019-09-11 12:36:07 -050078} // namespace interface
79
Matt Spinlerf10068d2020-12-02 10:44:08 -060080using namespace sdbusplus::xyz::openbmc_project::State::Boot::server;
Matt Spinlerb3d488f2020-02-21 15:30:46 -060081using sdbusplus::exception::SdBusError;
Vijay Lobo81b4dca2021-04-29 00:04:00 -050082using namespace phosphor::logging;
Matt Spinlera7d9d962019-11-06 15:01:25 -060083
Matt Spinlerc8705e22019-09-11 12:36:07 -050084DataInterface::DataInterface(sdbusplus::bus::bus& bus) : _bus(bus)
85{
Matt Spinlercad9c2b2019-12-02 15:42:01 -060086 readBMCFWVersion();
87 readServerFWVersion();
Matt Spinler677381b2020-01-23 10:04:29 -060088 readBMCFWVersionID();
Matt Spinler2a28c932020-02-03 14:23:40 -060089
Matt Spinlerf10068d2020-12-02 10:44:08 -060090 // Watch the BootProgress property
Matt Spinler2a28c932020-02-03 14:23:40 -060091 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
Matt Spinlerf10068d2020-12-02 10:44:08 -060092 bus, object_path::hostState, interface::bootProgress, "BootProgress",
93 *this, [this](const auto& value) {
94 auto status = Progress::convertProgressStagesFromString(
95 std::get<std::string>(value));
Matt Spinler2a28c932020-02-03 14:23:40 -060096
Matt Spinlerf10068d2020-12-02 10:44:08 -060097 if ((status == Progress::ProgressStages::SystemInitComplete) ||
98 (status == Progress::ProgressStages::OSStart) ||
99 (status == Progress::ProgressStages::OSRunning))
Matt Spinler2a28c932020-02-03 14:23:40 -0600100 {
Matt Spinler4aa23a12020-02-03 15:05:09 -0600101 setHostUp(true);
Matt Spinler2a28c932020-02-03 14:23:40 -0600102 }
103 else
104 {
Matt Spinler4aa23a12020-02-03 15:05:09 -0600105 setHostUp(false);
Matt Spinler2a28c932020-02-03 14:23:40 -0600106 }
107 }));
Matt Spinler9cf3cfd2020-02-03 14:41:55 -0600108
109 // Watch the host PEL enable property
110 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
111 bus, object_path::enableHostPELs, interface::enable, "Enabled", *this,
112 [this](const auto& value) {
113 this->_sendPELsToHost = std::get<bool>(value);
114 }));
Matt Spinler4aa23a12020-02-03 15:05:09 -0600115
116 // Watch the BMCState property
117 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
118 bus, object_path::bmcState, interface::bmcState, "CurrentBMCState",
119 *this, [this](const auto& value) {
120 this->_bmcState = std::get<std::string>(value);
121 }));
122
123 // Watch the chassis current and requested power state properties
124 _properties.emplace_back(std::make_unique<InterfaceWatcher<DataInterface>>(
125 bus, object_path::chassisState, interface::chassisState, *this,
126 [this](const auto& properties) {
127 auto state = properties.find("CurrentPowerState");
128 if (state != properties.end())
129 {
130 this->_chassisState = std::get<std::string>(state->second);
131 }
132
133 auto trans = properties.find("RequestedPowerTransition");
134 if (trans != properties.end())
135 {
136 this->_chassisTransition = std::get<std::string>(trans->second);
137 }
138 }));
139
140 // Watch the CurrentHostState property
141 _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>(
142 bus, object_path::hostState, interface::hostState, "CurrentHostState",
143 *this, [this](const auto& value) {
144 this->_hostState = std::get<std::string>(value);
145 }));
Matt Spinlerc8705e22019-09-11 12:36:07 -0500146}
147
Matt Spinler2a28c932020-02-03 14:23:40 -0600148DBusPropertyMap
149 DataInterface::getAllProperties(const std::string& service,
150 const std::string& objectPath,
151 const std::string& interface) const
Matt Spinlerc8705e22019-09-11 12:36:07 -0500152{
153 DBusPropertyMap properties;
154
155 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
156 interface::dbusProperty, "GetAll");
157 method.append(interface);
158 auto reply = _bus.call(method);
159
160 reply.read(properties);
161
162 return properties;
163}
164
Matt Spinlera7d9d962019-11-06 15:01:25 -0600165void DataInterface::getProperty(const std::string& service,
166 const std::string& objectPath,
167 const std::string& interface,
Matt Spinler2a28c932020-02-03 14:23:40 -0600168 const std::string& property,
169 DBusValue& value) const
Matt Spinlera7d9d962019-11-06 15:01:25 -0600170{
171
172 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
173 interface::dbusProperty, "Get");
174 method.append(interface, property);
175 auto reply = _bus.call(method);
176
177 reply.read(value);
178}
179
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600180DBusPathList DataInterface::getPaths(const DBusInterfaceList& interfaces) const
181{
182
183 auto method = _bus.new_method_call(
184 service_name::objectMapper, object_path::objectMapper,
185 interface::objectMapper, "GetSubTreePaths");
186
187 method.append(std::string{"/"}, 0, interfaces);
188
189 auto reply = _bus.call(method);
190
191 DBusPathList paths;
192 reply.read(paths);
193
194 return paths;
195}
196
Matt Spinlerc8705e22019-09-11 12:36:07 -0500197DBusService DataInterface::getService(const std::string& objectPath,
Matt Spinlerb3f51862019-12-09 13:55:10 -0600198 const std::string& interface) const
Matt Spinlerc8705e22019-09-11 12:36:07 -0500199{
200 auto method = _bus.new_method_call(service_name::objectMapper,
201 object_path::objectMapper,
202 interface::objectMapper, "GetObject");
203
204 method.append(objectPath, std::vector<std::string>({interface}));
205
206 auto reply = _bus.call(method);
207
208 std::map<DBusService, DBusInterfaceList> response;
209 reply.read(response);
210
211 if (!response.empty())
212 {
213 return response.begin()->first;
214 }
215
216 return std::string{};
217}
Matt Spinlera7d9d962019-11-06 15:01:25 -0600218
Matt Spinler677381b2020-01-23 10:04:29 -0600219void DataInterface::readBMCFWVersion()
220{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500221 _bmcFWVersion =
222 phosphor::logging::util::getOSReleaseValue("VERSION").value_or("");
Matt Spinlercad9c2b2019-12-02 15:42:01 -0600223}
224
225void DataInterface::readServerFWVersion()
226{
227 // Not available yet
228}
229
Matt Spinler677381b2020-01-23 10:04:29 -0600230void DataInterface::readBMCFWVersionID()
231{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500232 _bmcFWVersionID =
233 phosphor::logging::util::getOSReleaseValue("VERSION_ID").value_or("");
Matt Spinler677381b2020-01-23 10:04:29 -0600234}
235
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500236std::string DataInterface::getMachineTypeModel() const
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600237{
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500238 std::string model;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600239 try
240 {
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600241
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500242 auto service = getService(object_path::systemInv, interface::invAsset);
243 if (!service.empty())
244 {
245 DBusValue value;
246 getProperty(service, object_path::systemInv, interface::invAsset,
247 "Model", value);
248
249 model = std::get<std::string>(value);
250 }
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600251 }
252 catch (const std::exception& e)
253 {
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500254 log<level::WARNING>(fmt::format("Failed reading Model property from "
255 "Interface: {} exception: {}",
256 interface::invAsset, e.what())
257 .c_str());
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600258 }
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500259
260 return model;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600261}
262
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500263std::string DataInterface::getMachineSerialNumber() const
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600264{
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500265 std::string sn;
266 try
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600267 {
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500268
269 auto service = getService(object_path::systemInv, interface::invAsset);
270 if (!service.empty())
271 {
272 DBusValue value;
273 getProperty(service, object_path::systemInv, interface::invAsset,
274 "SerialNumber", value);
275
276 sn = std::get<std::string>(value);
277 }
278 }
279 catch (const std::exception& e)
280 {
281 log<level::WARNING>(
282 fmt::format("Failed reading SerialNumber property from "
283 "Interface: {} exception: {}",
284 interface::invAsset, e.what())
285 .c_str());
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600286 }
287
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500288 return sn;
289}
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600290
Vijay Lobo81b4dca2021-04-29 00:04:00 -0500291std::string DataInterface::getMotherboardCCIN() const
292{
293 std::string ccin;
294
295 try
296 {
297 auto service =
298 getService(object_path::motherBoardInv, interface::viniRecordVPD);
299 if (!service.empty())
300 {
301 DBusValue value;
302 getProperty(service, object_path::motherBoardInv,
303 interface::viniRecordVPD, "CC", value);
304
305 auto cc = std::get<std::vector<uint8_t>>(value);
306 ccin = std::string{cc.begin(), cc.end()};
307 }
308 }
309 catch (const std::exception& e)
310 {
311 log<level::WARNING>(
312 fmt::format("Failed reading Motherboard CCIN property from "
313 "Interface: {} exception: {}",
314 interface::viniRecordVPD, e.what())
315 .c_str());
316 }
317
318 return ccin;
Matt Spinlerb3d488f2020-02-21 15:30:46 -0600319}
320
Matt Spinler60c4e792020-03-13 13:45:36 -0500321void DataInterface::getHWCalloutFields(const std::string& inventoryPath,
Matt Spinler60c4e792020-03-13 13:45:36 -0500322 std::string& fruPartNumber,
323 std::string& ccin,
324 std::string& serialNumber) const
325{
326 // For now, attempt to get all of the properties directly on the path
327 // passed in. In the future, may need to make use of an algorithm
328 // to figure out which inventory objects actually hold these
329 // interfaces in the case of non FRUs, or possibly another service
330 // will provide this info. Any missing interfaces will result
331 // in exceptions being thrown.
332
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500333 auto service = getService(inventoryPath, interface::viniRecordVPD);
Matt Spinler60c4e792020-03-13 13:45:36 -0500334
335 auto properties =
336 getAllProperties(service, inventoryPath, interface::viniRecordVPD);
337
338 auto value = std::get<std::vector<uint8_t>>(properties["FN"]);
339 fruPartNumber = std::string{value.begin(), value.end()};
340
341 value = std::get<std::vector<uint8_t>>(properties["CC"]);
342 ccin = std::string{value.begin(), value.end()};
343
344 value = std::get<std::vector<uint8_t>>(properties["SN"]);
345 serialNumber = std::string{value.begin(), value.end()};
346}
347
Matt Spinler9b90e2a2020-04-14 10:59:04 -0500348std::string
349 DataInterface::getLocationCode(const std::string& inventoryPath) const
350{
351 auto service = getService(inventoryPath, interface::locCode);
352
353 DBusValue locCode;
354 getProperty(service, inventoryPath, interface::locCode, "LocationCode",
355 locCode);
356
357 return std::get<std::string>(locCode);
358}
359
Matt Spinler5fb24c12020-06-04 11:21:33 -0500360std::string
361 DataInterface::addLocationCodePrefix(const std::string& locationCode)
362{
363 static const std::string locationCodePrefix{"Ufcs-"};
364
Matt Spinler0e4d72e2020-08-05 12:36:53 -0500365 // Technically there are 2 location code prefixes, Ufcs and Umts, so
366 // if it already starts with a U then don't need to do anything.
367 if (locationCode.front() != 'U')
Matt Spinler5fb24c12020-06-04 11:21:33 -0500368 {
369 return locationCodePrefix + locationCode;
370 }
371
372 return locationCode;
373}
374
375std::string DataInterface::expandLocationCode(const std::string& locationCode,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500376 uint16_t /*node*/) const
Matt Spinler5fb24c12020-06-04 11:21:33 -0500377{
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500378 auto method =
379 _bus.new_method_call(service_name::vpdManager, object_path::vpdManager,
380 interface::vpdManager, "GetExpandedLocationCode");
381
382 method.append(addLocationCodePrefix(locationCode),
383 static_cast<uint16_t>(0));
384
385 auto reply = _bus.call(method);
386
387 std::string expandedLocationCode;
388 reply.read(expandedLocationCode);
389
390 return expandedLocationCode;
Matt Spinler5fb24c12020-06-04 11:21:33 -0500391}
392
Matt Spinler2f9225a2020-08-05 12:58:49 -0500393std::string
394 DataInterface::getInventoryFromLocCode(const std::string& locationCode,
395 uint16_t node, bool expanded) const
Matt Spinler5fb24c12020-06-04 11:21:33 -0500396{
Matt Spinler2f9225a2020-08-05 12:58:49 -0500397 std::string methodName = expanded ? "GetFRUsByExpandedLocationCode"
398 : "GetFRUsByUnexpandedLocationCode";
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500399
Matt Spinler2f9225a2020-08-05 12:58:49 -0500400 auto method =
401 _bus.new_method_call(service_name::vpdManager, object_path::vpdManager,
402 interface::vpdManager, methodName.c_str());
403
404 if (expanded)
405 {
406 method.append(locationCode);
407 }
408 else
409 {
410 method.append(addLocationCodePrefix(locationCode), node);
411 }
Matt Spinlerfcf9a3f2020-07-28 13:21:07 -0500412
413 auto reply = _bus.call(method);
414
415 std::vector<sdbusplus::message::object_path> entries;
416 reply.read(entries);
417
418 // Get the shortest entry from the paths received, as this
419 // would be the path furthest up the inventory hierarchy so
420 // would be the parent FRU. There is guaranteed to at least
421 // be one entry if the call didn't fail.
422 std::string shortest{entries[0]};
423
424 std::for_each(entries.begin(), entries.end(),
425 [&shortest](const auto& path) {
426 if (path.str.size() < shortest.size())
427 {
428 shortest = path;
429 }
430 });
431
432 return shortest;
Matt Spinler5fb24c12020-06-04 11:21:33 -0500433}
434
Matt Spinler34a904c2020-08-05 14:53:28 -0500435void DataInterface::assertLEDGroup(const std::string& ledGroup,
436 bool value) const
437{
438 DBusValue variant = value;
439 auto method =
440 _bus.new_method_call(service_name::ledGroupManager, ledGroup.c_str(),
441 interface::dbusProperty, "Set");
442 method.append(interface::ledGroup, "Asserted", variant);
443 _bus.call(method);
444}
445
Matt Spinler993168d2021-04-07 16:05:03 -0500446void DataInterface::setFunctional(const std::string& objectPath,
447 bool value) const
448{
449 DBusValue variant = value;
450 auto service = getService(objectPath, interface::operationalStatus);
451
452 auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(),
453 interface::dbusProperty, "Set");
454
455 method.append(interface::operationalStatus, "Functional", variant);
456 _bus.call(method);
457}
458
Matt Spinler1ab66962020-10-29 13:21:44 -0500459std::vector<std::string> DataInterface::getSystemNames() const
460{
461 DBusSubTree subtree;
462 DBusValue names;
463
464 auto method = _bus.new_method_call(service_name::objectMapper,
465 object_path::objectMapper,
466 interface::objectMapper, "GetSubTree");
467 method.append(std::string{"/"}, 0,
468 std::vector<std::string>{interface::compatible});
469 auto reply = _bus.call(method);
470
471 reply.read(subtree);
472 if (subtree.empty())
473 {
474 throw std::runtime_error("Compatible interface not on D-Bus");
475 }
476
477 const auto& object = *(subtree.begin());
478 const auto& path = object.first;
479 const auto& service = object.second.begin()->first;
480
481 getProperty(service, path, interface::compatible, "Names", names);
482
483 return std::get<std::vector<std::string>>(names);
484}
485
Matt Spinlerc8705e22019-09-11 12:36:07 -0500486} // namespace pels
487} // namespace openpower