-
Notifications
You must be signed in to change notification settings - Fork 76
/
chassishandler.cpp
2400 lines (2149 loc) · 82.2 KB
/
chassishandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#include "chassishandler.hpp"
#include <arpa/inet.h>
#include <endian.h>
#include <limits.h>
#include <netinet/in.h>
#include <ipmid/api.hpp>
#include <ipmid/types.hpp>
#include <ipmid/utils.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/message/types.hpp>
#include <sdbusplus/server/object.hpp>
#include <sdbusplus/timer.hpp>
#include <settings.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
#include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
#include <xyz/openbmc_project/Control/Boot/Type/server.hpp>
#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
#include <xyz/openbmc_project/State/Chassis/server.hpp>
#include <xyz/openbmc_project/State/Host/server.hpp>
#include <xyz/openbmc_project/State/PowerOnHours/server.hpp>
#include <array>
#include <chrono>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <future>
#include <map>
#include <sstream>
#include <string>
std::unique_ptr<sdbusplus::Timer> identifyTimer
__attribute__((init_priority(101)));
static ChassisIDState chassisIDState = ChassisIDState::reserved;
constexpr size_t sizeVersion = 2;
constexpr size_t DEFAULT_IDENTIFY_TIME_OUT = 15;
// PetiBoot-Specific
static constexpr uint8_t netConfInitialBytes[] = {0x80, 0x21, 0x70, 0x62,
0x21, 0x00, 0x01, 0x06};
static constexpr uint8_t oemParmStart = 96;
static constexpr uint8_t oemParmEnd = 127;
static constexpr size_t cookieOffset = 1;
static constexpr size_t versionOffset = 5;
static constexpr size_t addrSizeOffset = 8;
static constexpr size_t macOffset = 9;
static constexpr size_t addrTypeOffset = 16;
static constexpr size_t ipAddrOffset = 17;
namespace ipmi
{
constexpr Cc ccParmNotSupported = 0x80;
static inline auto responseParmNotSupported()
{
return response(ccParmNotSupported);
}
} // namespace ipmi
void register_netfn_chassis_functions() __attribute__((constructor));
// Host settings in dbus
// Service name should be referenced by connection name got via object mapper
const char* settings_object_name = "/org/openbmc/settings/host0";
const char* settings_intf_name = "org.freedesktop.DBus.Properties";
const char* identify_led_object_name =
"/xyz/openbmc_project/led/groups/enclosure_identify";
constexpr auto SETTINGS_ROOT = "/";
constexpr auto SETTINGS_MATCH = "host0";
constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
static constexpr auto chassisStateRoot = "/xyz/openbmc_project/state";
static constexpr auto chassisPOHStateIntf =
"xyz.openbmc_project.State.PowerOnHours";
static constexpr auto pohCounterProperty = "POHCounter";
static constexpr auto match = "chassis0";
const static constexpr char chassisCapIntf[] =
"xyz.openbmc_project.Control.ChassisCapabilities";
const static constexpr char chassisIntrusionProp[] = "ChassisIntrusionEnabled";
const static constexpr char chassisFrontPanelLockoutProp[] =
"ChassisFrontPanelLockoutEnabled";
const static constexpr char chassisNMIProp[] = "ChassisNMIEnabled";
const static constexpr char chassisPowerInterlockProp[] =
"ChassisPowerInterlockEnabled";
const static constexpr char chassisFRUDevAddrProp[] = "FRUDeviceAddress";
const static constexpr char chassisSDRDevAddrProp[] = "SDRDeviceAddress";
const static constexpr char chassisSELDevAddrProp[] = "SELDeviceAddress";
const static constexpr char chassisSMDevAddrProp[] = "SMDeviceAddress";
const static constexpr char chassisBridgeDevAddrProp[] = "BridgeDeviceAddress";
static constexpr uint8_t chassisCapAddrMask = 0xfe;
static constexpr const char* powerButtonIntf =
"xyz.openbmc_project.Chassis.Buttons.Power";
static constexpr const char* powerButtonPath =
"/xyz/openbmc_project/Chassis/Buttons/Power0";
static constexpr const char* resetButtonIntf =
"xyz.openbmc_project.Chassis.Buttons.Reset";
static constexpr const char* resetButtonPath =
"/xyz/openbmc_project/Chassis/Buttons/Reset0";
// Phosphor Host State manager
namespace State = sdbusplus::server::xyz::openbmc_project::state;
namespace fs = std::filesystem;
using namespace phosphor::logging;
using namespace sdbusplus::error::xyz::openbmc_project::common;
using namespace sdbusplus::server::xyz::openbmc_project::control::boot;
namespace chassis
{
namespace internal
{
constexpr auto bootSettingsPath = "/xyz/openbmc_project/control/host0/boot";
constexpr auto bootEnableIntf = "xyz.openbmc_project.Object.Enable";
constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
constexpr auto bootTypeIntf = "xyz.openbmc_project.Control.Boot.Type";
constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
constexpr auto bootSettingsOneTimePath =
"/xyz/openbmc_project/control/host0/boot/one_time";
constexpr auto bootOneTimeIntf = "xyz.openbmc_project.Object.Enable";
constexpr auto powerRestoreIntf =
"xyz.openbmc_project.Control.Power.RestorePolicy";
sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection());
namespace cache
{
std::unique_ptr<settings::Objects> objectsPtr = nullptr;
settings::Objects& getObjects()
{
if (objectsPtr == nullptr)
{
objectsPtr = std::make_unique<settings::Objects>(
dbus, std::vector<std::string>{bootModeIntf, bootTypeIntf,
bootSourceIntf, powerRestoreIntf});
}
return *objectsPtr;
}
} // namespace cache
} // namespace internal
} // namespace chassis
namespace poh
{
constexpr auto minutesPerCount = 60;
} // namespace poh
int getHostNetworkData(ipmi::message::Payload& payload)
{
ipmi::PropertyMap properties;
int rc = 0;
uint8_t addrSize = ipmi::network::IPV4_ADDRESS_SIZE_BYTE;
try
{
// TODO There may be cases where an interface is implemented by multiple
// objects,to handle such cases we are interested on that object
// which are on interested busname.
// Currenlty mapper doesn't give the readable busname(gives busid)
// so we can't match with bus name so giving some object specific info
// as SETTINGS_MATCH.
// Later SETTINGS_MATCH will be replaced with busname.
sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
SETTINGS_ROOT, SETTINGS_MATCH);
auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
SETTINGS_ROOT, SETTINGS_MATCH);
properties = ipmi::getAllDbusProperties(
bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE);
auto variant = ipmi::getDbusProperty(
bus, macObjectInfo.second, macObjectInfo.first, MAC_INTERFACE,
"MACAddress");
auto ipAddress = std::get<std::string>(properties["Address"]);
auto gateway = std::get<std::string>(properties["Gateway"]);
auto prefix = std::get<uint8_t>(properties["PrefixLength"]);
uint8_t isStatic =
(std::get<std::string>(properties["Origin"]) ==
"xyz.openbmc_project.Network.IP.AddressOrigin.Static")
? 1
: 0;
auto MACAddress = std::get<std::string>(variant);
// it is expected here that we should get the valid data
// but we may also get the default values.
// Validation of the data is done by settings.
//
// if mac address is default mac address then
// don't send blank override.
if ((MACAddress == ipmi::network::DEFAULT_MAC_ADDRESS))
{
rc = -1;
return rc;
}
// if addr is static then ipaddress,gateway,prefix
// should not be default one,don't send blank override.
if (isStatic)
{
if ((ipAddress == ipmi::network::DEFAULT_ADDRESS) ||
(gateway == ipmi::network::DEFAULT_ADDRESS) || (!prefix))
{
rc = -1;
return rc;
}
}
std::string token;
std::stringstream ss(MACAddress);
// First pack macOffset no of bytes in payload.
// Latter this PetiBoot-Specific data will be populated.
std::vector<uint8_t> payloadInitialBytes(macOffset);
payload.pack(payloadInitialBytes);
while (std::getline(ss, token, ':'))
{
payload.pack(stoi(token, nullptr, 16));
}
payload.pack(0x00);
payload.pack(isStatic);
uint8_t addressFamily = (std::get<std::string>(properties["Type"]) ==
"xyz.openbmc_project.Network.IP.Protocol.IPv4")
? AF_INET
: AF_INET6;
addrSize = (addressFamily == AF_INET)
? ipmi::network::IPV4_ADDRESS_SIZE_BYTE
: ipmi::network::IPV6_ADDRESS_SIZE_BYTE;
// ipaddress and gateway would be in IPv4 format
std::vector<uint8_t> addrInBinary(addrSize);
inet_pton(addressFamily, ipAddress.c_str(),
reinterpret_cast<void*>(addrInBinary.data()));
payload.pack(addrInBinary);
payload.pack(prefix);
std::vector<uint8_t> gatewayDetails(addrSize);
inet_pton(addressFamily, gateway.c_str(),
reinterpret_cast<void*>(gatewayDetails.data()));
payload.pack(gatewayDetails);
}
catch (const InternalFailure& e)
{
commit<InternalFailure>();
rc = -1;
return rc;
}
// PetiBoot-Specific
// If success then copy the first 9 bytes to the payload message
// payload first 2 bytes contain the parameter values. Skip that 2 bytes.
uint8_t skipFirstTwoBytes = 2;
size_t payloadSize = payload.size();
uint8_t* configDataStartingAddress = payload.data() + skipFirstTwoBytes;
if (payloadSize < skipFirstTwoBytes + sizeof(netConfInitialBytes))
{
lg2::error("Invalid net config");
rc = -1;
return rc;
}
std::copy(netConfInitialBytes,
netConfInitialBytes + sizeof(netConfInitialBytes),
configDataStartingAddress);
if (payloadSize < skipFirstTwoBytes + addrSizeOffset + sizeof(addrSize))
{
lg2::error("Invalid length of address size");
rc = -1;
return rc;
}
std::copy(&addrSize, &(addrSize) + sizeof(addrSize),
configDataStartingAddress + addrSizeOffset);
#ifdef _IPMI_DEBUG_
std::printf("\n===Printing the IPMI Formatted Data========\n");
for (uint8_t pos = 0; pos < index; pos++)
{
std::printf("%02x ", payloadStartingAddress[pos]);
}
#endif
return rc;
}
/** @brief convert IPv4 and IPv6 addresses from binary to text form.
* @param[in] family - IPv4/Ipv6
* @param[in] data - req data pointer.
* @param[in] offset - offset in the data.
* @param[in] addrSize - size of the data which needs to be read from offset.
* @returns address in text form.
*/
std::string getAddrStr(uint8_t family, uint8_t* data, uint8_t offset,
uint8_t addrSize)
{
char ipAddr[INET6_ADDRSTRLEN] = {};
switch (family)
{
case AF_INET:
{
struct sockaddr_in addr4
{};
std::memcpy(&addr4.sin_addr.s_addr, &data[offset], addrSize);
inet_ntop(AF_INET, &addr4.sin_addr, ipAddr, INET_ADDRSTRLEN);
break;
}
case AF_INET6:
{
struct sockaddr_in6 addr6
{};
std::memcpy(&addr6.sin6_addr.s6_addr, &data[offset], addrSize);
inet_ntop(AF_INET6, &addr6.sin6_addr, ipAddr, INET6_ADDRSTRLEN);
break;
}
default:
{
return {};
}
}
return ipAddr;
}
ipmi::Cc setHostNetworkData(ipmi::message::Payload& data)
{
using namespace std::string_literals;
std::string hostNetworkConfig;
std::string mac("00:00:00:00:00:00");
std::string ipAddress, gateway;
std::string addrOrigin{0};
uint8_t addrSize{0};
std::string addressOrigin =
"xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
std::string addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
uint8_t prefix{0};
uint8_t family = AF_INET;
// cookie starts from second byte
// version starts from sixth byte
try
{
do
{
// cookie == 0x21 0x70 0x62 0x21
data.trailingOk = true;
auto msgLen = data.size();
std::vector<uint8_t> msgPayloadBytes(msgLen);
if (data.unpack(msgPayloadBytes) != 0 || !data.fullyUnpacked())
{
lg2::error("Error in unpacking message of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
uint8_t* msgPayloadStartingPos = msgPayloadBytes.data();
constexpr size_t cookieSize = 4;
if (msgLen < cookieOffset + cookieSize)
{
lg2::error("Error in cookie getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
if (std::equal(msgPayloadStartingPos + cookieOffset,
msgPayloadStartingPos + cookieOffset + cookieSize,
(netConfInitialBytes + cookieOffset)) != 0)
{
// all cookie == 0
if (std::all_of(msgPayloadStartingPos + cookieOffset,
msgPayloadStartingPos + cookieOffset +
cookieSize,
[](int i) { return i == 0; }) == true)
{
// need to zero out the network settings.
break;
}
lg2::error("Invalid Cookie");
elog<InternalFailure>();
}
// vesion == 0x00 0x01
if (msgLen < versionOffset + sizeVersion)
{
lg2::error("Error in version getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
if (std::equal(msgPayloadStartingPos + versionOffset,
msgPayloadStartingPos + versionOffset + sizeVersion,
(netConfInitialBytes + versionOffset)) != 0)
{
lg2::error("Invalid Version");
elog<InternalFailure>();
}
if (msgLen < macOffset + 6)
{
lg2::error(
"Error in mac address getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
std::stringstream result;
std::copy((msgPayloadStartingPos + macOffset),
(msgPayloadStartingPos + macOffset + 5),
std::ostream_iterator<int>(result, ":"));
mac = result.str();
if (msgLen < addrTypeOffset + sizeof(decltype(addrOrigin)))
{
lg2::error(
"Error in original address getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
std::copy(msgPayloadStartingPos + addrTypeOffset,
msgPayloadStartingPos + addrTypeOffset +
sizeof(decltype(addrOrigin)),
std::ostream_iterator<int>(result, ""));
addrOrigin = result.str();
if (!addrOrigin.empty())
{
addressOrigin =
"xyz.openbmc_project.Network.IP.AddressOrigin.Static";
}
if (msgLen < addrSizeOffset + sizeof(decltype(addrSize)))
{
lg2::error(
"Error in address size getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
// Get the address size
std::copy(msgPayloadStartingPos + addrSizeOffset,
(msgPayloadStartingPos + addrSizeOffset +
sizeof(decltype(addrSize))),
&addrSize);
uint8_t prefixOffset = ipAddrOffset + addrSize;
if (msgLen < prefixOffset + sizeof(decltype(prefix)))
{
lg2::error("Error in prefix getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
// std::copy(msgPayloadStartingPos + prefixOffset,
// msgPayloadStartingPos + prefixOffset +
// sizeof(decltype(prefix)),
// &prefix);
// Workaround compiler misdetecting out of bounds memcpy
prefix = msgPayloadStartingPos[prefixOffset];
uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
if (addrSize != ipmi::network::IPV4_ADDRESS_SIZE_BYTE)
{
addressType = "xyz.openbmc_project.Network.IP.Protocol.IPv6";
family = AF_INET6;
}
if (msgLen < ipAddrOffset + addrSize)
{
lg2::error("Error in IP address getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
ipAddress = getAddrStr(family, msgPayloadStartingPos, ipAddrOffset,
addrSize);
if (msgLen < gatewayOffset + addrSize)
{
lg2::error(
"Error in gateway address getting of setHostNetworkData");
return ipmi::ccReqDataLenInvalid;
}
gateway = getAddrStr(family, msgPayloadStartingPos, gatewayOffset,
addrSize);
} while (0);
// Cookie == 0 or it is a valid cookie
hostNetworkConfig +=
"ipaddress="s + ipAddress + ",prefix="s + std::to_string(prefix) +
",gateway="s + gateway + ",mac="s + mac + ",addressOrigin="s +
addressOrigin;
sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
auto ipObjectInfo = ipmi::getDbusObject(bus, IP_INTERFACE,
SETTINGS_ROOT, SETTINGS_MATCH);
auto macObjectInfo = ipmi::getDbusObject(bus, MAC_INTERFACE,
SETTINGS_ROOT, SETTINGS_MATCH);
// set the dbus property
ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
IP_INTERFACE, "Address", std::string(ipAddress));
ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
IP_INTERFACE, "PrefixLength", prefix);
ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
IP_INTERFACE, "Origin", addressOrigin);
ipmi::setDbusProperty(bus, ipObjectInfo.second, ipObjectInfo.first,
IP_INTERFACE, "Gateway", std::string(gateway));
ipmi::setDbusProperty(
bus, ipObjectInfo.second, ipObjectInfo.first, IP_INTERFACE, "Type",
std::string("xyz.openbmc_project.Network.IP.Protocol.IPv4"));
ipmi::setDbusProperty(bus, macObjectInfo.second, macObjectInfo.first,
MAC_INTERFACE, "MACAddress", std::string(mac));
lg2::debug("Network configuration changed: {NETWORKCONFIG}",
"NETWORKCONFIG", hostNetworkConfig);
}
catch (const sdbusplus::exception_t& e)
{
commit<InternalFailure>();
lg2::error("Error in ipmiChassisSetSysBootOptions call");
return ipmi::ccUnspecifiedError;
}
return ipmi::ccSuccess;
}
uint32_t getPOHCounter()
{
sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
auto chassisStateObj =
ipmi::getDbusObject(bus, chassisPOHStateIntf, chassisStateRoot, match);
auto service =
ipmi::getService(bus, chassisPOHStateIntf, chassisStateObj.first);
auto propValue =
ipmi::getDbusProperty(bus, service, chassisStateObj.first,
chassisPOHStateIntf, pohCounterProperty);
return std::get<uint32_t>(propValue);
}
/** @brief Implements the get chassis capabilities command
*
* @returns IPMI completion code plus response data
* chassisCapFlags - chassis capability flag
* chassisFRUInfoDevAddr - chassis FRU info Device Address
* chassisSDRDevAddr - chassis SDR device address
* chassisSELDevAddr - chassis SEL device address
* chassisSMDevAddr - chassis system management device address
* chassisBridgeDevAddr - chassis bridge device address
*/
ipmi::RspType<bool, // chassis intrusion sensor
bool, // chassis Front panel lockout
bool, // chassis NMI
bool, // chassis power interlock
uint4_t, // reserved
uint8_t, // chassis FRU info Device Address
uint8_t, // chassis SDR device address
uint8_t, // chassis SEL device address
uint8_t, // chassis system management device address
uint8_t // chassis bridge device address
>
ipmiGetChassisCap()
{
ipmi::PropertyMap properties;
try
{
sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
ipmi::DbusObjectInfo chassisCapObject =
ipmi::getDbusObject(bus, chassisCapIntf);
// capabilities flags
// [7..4] - reserved
// [3] – 1b = provides power interlock (IPM 1.5)
// [2] – 1b = provides Diagnostic Interrupt (FP NMI)
// [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis
// has capabilities
// to lock out external power control and reset button or
// front panel interfaces and/or detect tampering with those
// interfaces).
// [0] -1b = Chassis provides intrusion (physical security) sensor.
// set to default value 0x0.
properties =
ipmi::getAllDbusProperties(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf);
}
catch (const std::exception& e)
{
lg2::error("Failed to fetch Chassis Capability properties: {ERROR}",
"ERROR", e);
return ipmi::responseUnspecifiedError();
}
bool* chassisIntrusionFlag =
std::get_if<bool>(&properties[chassisIntrusionProp]);
if (chassisIntrusionFlag == nullptr)
{
lg2::error("Error to get chassis Intrusion flags");
return ipmi::responseUnspecifiedError();
}
bool* chassisFrontPanelFlag =
std::get_if<bool>(&properties[chassisFrontPanelLockoutProp]);
if (chassisFrontPanelFlag == nullptr)
{
lg2::error("Error to get chassis intrusion flags");
return ipmi::responseUnspecifiedError();
}
bool* chassisNMIFlag = std::get_if<bool>(&properties[chassisNMIProp]);
if (chassisNMIFlag == nullptr)
{
lg2::error("Error to get chassis NMI flags");
return ipmi::responseUnspecifiedError();
}
bool* chassisPowerInterlockFlag =
std::get_if<bool>(&properties[chassisPowerInterlockProp]);
if (chassisPowerInterlockFlag == nullptr)
{
lg2::error("Error to get chassis power interlock flags");
return ipmi::responseUnspecifiedError();
}
uint8_t* chassisFRUInfoDevAddr =
std::get_if<uint8_t>(&properties[chassisFRUDevAddrProp]);
if (chassisFRUInfoDevAddr == nullptr)
{
lg2::error("Error to get chassis FRU info device address");
return ipmi::responseUnspecifiedError();
}
uint8_t* chassisSDRDevAddr =
std::get_if<uint8_t>(&properties[chassisSDRDevAddrProp]);
if (chassisSDRDevAddr == nullptr)
{
lg2::error("Error to get chassis SDR device address");
return ipmi::responseUnspecifiedError();
}
uint8_t* chassisSELDevAddr =
std::get_if<uint8_t>(&properties[chassisSELDevAddrProp]);
if (chassisSELDevAddr == nullptr)
{
lg2::error("Error to get chassis SEL device address");
return ipmi::responseUnspecifiedError();
}
uint8_t* chassisSMDevAddr =
std::get_if<uint8_t>(&properties[chassisSMDevAddrProp]);
if (chassisSMDevAddr == nullptr)
{
lg2::error("Error to get chassis SM device address");
return ipmi::responseUnspecifiedError();
}
uint8_t* chassisBridgeDevAddr =
std::get_if<uint8_t>(&properties[chassisBridgeDevAddrProp]);
if (chassisBridgeDevAddr == nullptr)
{
lg2::error("Error to get chassis bridge device address");
return ipmi::responseUnspecifiedError();
}
return ipmi::responseSuccess(
*chassisIntrusionFlag, *chassisFrontPanelFlag, *chassisNMIFlag,
*chassisPowerInterlockFlag, 0, *chassisFRUInfoDevAddr,
*chassisSDRDevAddr, *chassisSELDevAddr, *chassisSMDevAddr,
*chassisBridgeDevAddr);
}
/** @brief implements set chassis capalibities command
* @param intrusion - chassis intrusion
* @param fpLockout - frontpannel lockout
* @param reserved1 - skip one bit
* @param fruDeviceAddr - chassis FRU info Device Address
* @param sdrDeviceAddr - chassis SDR device address
* @param selDeviceAddr - chassis SEL device address
* @param smDeviceAddr - chassis system management device address
* @param bridgeDeviceAddr - chassis bridge device address
*
* @returns IPMI completion code
*/
ipmi::RspType<> ipmiSetChassisCap(
bool intrusion, bool fpLockout, uint6_t reserved1,
uint8_t fruDeviceAddr,
uint8_t sdrDeviceAddr,
uint8_t selDeviceAddr,
uint8_t smDeviceAddr,
uint8_t bridgeDeviceAddr)
{
// check input data
if (reserved1 != 0)
{
lg2::error("Unsupported request parameter");
return ipmi::responseInvalidFieldRequest();
}
if ((fruDeviceAddr & ~chassisCapAddrMask) != 0)
{
lg2::error("Unsupported request parameter(FRU Addr) for REQ={REQ}",
"REQ", lg2::hex, fruDeviceAddr);
return ipmi::responseInvalidFieldRequest();
}
if ((sdrDeviceAddr & ~chassisCapAddrMask) != 0)
{
lg2::error("Unsupported request parameter(SDR Addr) for REQ={REQ}",
"REQ", lg2::hex, sdrDeviceAddr);
return ipmi::responseInvalidFieldRequest();
}
if ((selDeviceAddr & ~chassisCapAddrMask) != 0)
{
lg2::error("Unsupported request parameter(SEL Addr) for REQ={REQ}",
"REQ", lg2::hex, selDeviceAddr);
return ipmi::responseInvalidFieldRequest();
}
if ((smDeviceAddr & ~chassisCapAddrMask) != 0)
{
lg2::error("Unsupported request parameter(SM Addr) for REQ={REQ}",
"REQ", lg2::hex, smDeviceAddr);
return ipmi::responseInvalidFieldRequest();
}
if ((bridgeDeviceAddr & ~chassisCapAddrMask) != 0)
{
lg2::error("Unsupported request parameter(Bridge Addr) for REQ={REQ}",
"REQ", lg2::hex, bridgeDeviceAddr);
return ipmi::responseInvalidFieldRequest();
}
try
{
sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
ipmi::DbusObjectInfo chassisCapObject =
ipmi::getDbusObject(bus, chassisCapIntf);
ipmi::setDbusProperty(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf,
chassisIntrusionProp, intrusion);
ipmi::setDbusProperty(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf,
chassisFrontPanelLockoutProp, fpLockout);
ipmi::setDbusProperty(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf,
chassisFRUDevAddrProp, fruDeviceAddr);
ipmi::setDbusProperty(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf,
chassisSDRDevAddrProp, sdrDeviceAddr);
ipmi::setDbusProperty(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf,
chassisSELDevAddrProp, selDeviceAddr);
ipmi::setDbusProperty(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf,
chassisSMDevAddrProp, smDeviceAddr);
ipmi::setDbusProperty(bus, chassisCapObject.second,
chassisCapObject.first, chassisCapIntf,
chassisBridgeDevAddrProp, bridgeDeviceAddr);
}
catch (const std::exception& e)
{
lg2::error("Failed to set chassis capability properties: {ERR}", "ERR",
e);
return ipmi::responseUnspecifiedError();
}
return ipmi::responseSuccess();
}
//------------------------------------------
// Calls into Host State Manager Dbus object
//------------------------------------------
int initiateHostStateTransition(ipmi::Context::ptr& ctx,
State::Host::Transition transition)
{
// OpenBMC Host State Manager dbus framework
constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0";
constexpr auto hostStateIntf = "xyz.openbmc_project.State.Host";
// Convert to string equivalent of the passed in transition enum.
auto request =
sdbusplus::common::xyz::openbmc_project::state::convertForMessage(
transition);
std::string service;
boost::system::error_code ec =
ipmi::getService(ctx, hostStateIntf, hostStatePath, service);
if (!ec)
{
ec = ipmi::setDbusProperty(ctx, service, hostStatePath, hostStateIntf,
"RequestedHostTransition", request);
}
if (ec)
{
lg2::error(
"Failed to initiate transition for request {REQUEST}: {EXCEPTION}",
"REQUEST", request, "EXCEPTION", ec.message());
return -1;
}
lg2::info(
"Transition request {REQUEST} initiated successfully by user {USERID}",
"REQUEST", request, "USERID", ctx->userId);
return 0;
}
//------------------------------------------
// Calls into Chassis State Manager Dbus object
//------------------------------------------
int initiateChassisStateTransition(ipmi::Context::ptr& ctx,
State::Chassis::Transition transition)
{
// OpenBMC Chassis State Manager dbus framework
constexpr auto chassisStatePath = "/xyz/openbmc_project/state/chassis0";
constexpr auto chassisStateIntf = "xyz.openbmc_project.State.Chassis";
std::string service;
boost::system::error_code ec =
ipmi::getService(ctx, chassisStateIntf, chassisStatePath, service);
// Convert to string equivalent of the passed in transition enum.
auto request =
sdbusplus::common::xyz::openbmc_project::state::convertForMessage(
transition);
if (!ec)
{
ec = ipmi::setDbusProperty(ctx, service, chassisStatePath,
chassisStateIntf, "RequestedPowerTransition",
request);
}
if (ec)
{
lg2::error("Failed to initiate transition {REQUEST}: {EXCEPTION}",
"REQUEST", request, "EXCEPTION", ec.message());
return -1;
}
return 0;
}
//------------------------------------------
// Trigger an NMI on the host via dbus
//------------------------------------------
static int doNmi(ipmi::Context::ptr& ctx)
{
constexpr const char* nmiIntfName = "xyz.openbmc_project.Control.Host.NMI";
ipmi::DbusObjectInfo nmiObj{};
boost::system::error_code ec;
ec = ipmi::getDbusObject(ctx, nmiIntfName, nmiObj);
if (ec)
{
lg2::error("Failed to find NMI service: {ERROR}", "ERROR",
ec.message());
return -1;
}
ctx->bus->yield_method_call<void>(ctx->yield, ec, nmiObj.second,
nmiObj.first, nmiIntfName, "NMI");
if (ec)
{
lg2::error("NMI call failed: {ERROR}", "ERROR", ec.message());
elog<InternalFailure>();
return -1;
}
return 0;
}
namespace power_policy
{
using namespace sdbusplus::server::xyz::openbmc_project::control::power;
using IpmiValue = uint8_t;
using DbusValue = RestorePolicy::Policy;
const std::map<DbusValue, IpmiValue> dbusToIpmi = {
{RestorePolicy::Policy::AlwaysOff, 0x00},
{RestorePolicy::Policy::Restore, 0x01},
{RestorePolicy::Policy::AlwaysOn, 0x02},
{RestorePolicy::Policy::None, 0x03}};
static constexpr uint8_t noChange = 0x03;
static constexpr uint8_t allSupport = 0x01 | 0x02 | 0x04;
/* helper function for Get Chassis Status Command
*/
std::optional<uint2_t> getPowerRestorePolicy()
{
uint2_t restorePolicy = 0;
using namespace chassis::internal;
settings::Objects& objects = cache::getObjects();
try
{
const auto& powerRestoreSetting =
objects.map.at(powerRestoreIntf).front();
ipmi::Value result = ipmi::getDbusProperty(
*getSdBus(),
objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
powerRestoreSetting.c_str(), powerRestoreIntf,
"PowerRestorePolicy");
auto powerRestore = RestorePolicy::convertPolicyFromString(
std::get<std::string>(result));
restorePolicy = dbusToIpmi.at(powerRestore);
}
catch (const std::exception& e)
{
lg2::error(
"Failed to fetch pgood property ({PATH}/{INTERFACE}): {ERROR}",
"PATH", objects.map.at(powerRestoreIntf).front(), "INTERFACE",
powerRestoreIntf, "ERROR", e);
cache::objectsPtr.reset();
return std::nullopt;
}
return std::make_optional(restorePolicy);
}
/*
* getPowerStatus
* helper function for Get Chassis Status Command
* return - optional value for pgood (no value on error)
*/
std::optional<bool> getPowerStatus()
{
bool powerGood = false;
std::shared_ptr<sdbusplus::asio::connection> busp = getSdBus();
try
{
constexpr const char* chassisStatePath =
"/xyz/openbmc_project/state/chassis0";
constexpr const char* chassisStateIntf =
"xyz.openbmc_project.State.Chassis";
auto service =
ipmi::getService(*busp, chassisStateIntf, chassisStatePath);
ipmi::Value powerState =
ipmi::getDbusProperty(*busp, service, chassisStatePath,
chassisStateIntf, "CurrentPowerState");
powerGood = std::get<std::string>(powerState) ==
"xyz.openbmc_project.State.Chassis.PowerState.On";
}
catch (const std::exception& e)
{
try
{
// FIXME: some legacy modules use the older path; try that next
constexpr const char* legacyPwrCtrlObj =
"/org/openbmc/control/power0";
constexpr const char* legacyPwrCtrlIntf =
"org.openbmc.control.Power";
auto service =
ipmi::getService(*busp, legacyPwrCtrlIntf, legacyPwrCtrlObj);
ipmi::Value variant = ipmi::getDbusProperty(
*busp, service, legacyPwrCtrlObj, legacyPwrCtrlIntf, "pgood");
powerGood = static_cast<bool>(std::get<int>(variant));
}
catch (const std::exception& e)
{
lg2::error("Failed to fetch pgood property: {ERROR}", "ERROR", e);
return std::nullopt;
}
}