1 /* 2 * Copyright (c) 2018 Intel Corporation. 3 * Copyright (c) 2018-present Facebook. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include "xyz/openbmc_project/Common/error.hpp" 19 20 #include <boost/crc.hpp> 21 #include <commandutils.hpp> 22 #include <ipmid/api-types.hpp> 23 #include <ipmid/api.hpp> 24 #include <ipmid/utils.hpp> 25 #include <nlohmann/json.hpp> 26 #include <oemcommands.hpp> 27 #include <phosphor-logging/log.hpp> 28 #include <sdbusplus/bus.hpp> 29 #include <xyz/openbmc_project/Control/Boot/Mode/server.hpp> 30 #include <xyz/openbmc_project/Control/Boot/Source/server.hpp> 31 #include <xyz/openbmc_project/Control/Boot/Type/server.hpp> 32 33 #include <array> 34 #include <cstring> 35 #include <fstream> 36 #include <iomanip> 37 #include <iostream> 38 #include <regex> 39 #include <sstream> 40 #include <string> 41 #include <vector> 42 43 #define SIZE_IANA_ID 3 44 45 namespace ipmi 46 { 47 48 using namespace phosphor::logging; 49 50 void getSelectorPosition(size_t& position); 51 static void registerOEMFunctions() __attribute__((constructor)); 52 sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection()); // from ipmid/api.h 53 static constexpr size_t maxFRUStringLength = 0x3F; 54 constexpr uint8_t cmdSetSystemGuid = 0xEF; 55 56 constexpr uint8_t cmdSetQDimmInfo = 0x12; 57 constexpr uint8_t cmdGetQDimmInfo = 0x13; 58 59 constexpr ipmi_ret_t ccInvalidParam = 0x80; 60 61 int plat_udbg_get_post_desc(uint8_t, uint8_t*, uint8_t, uint8_t*, uint8_t*, 62 uint8_t*); 63 int plat_udbg_get_gpio_desc(uint8_t, uint8_t*, uint8_t*, uint8_t*, uint8_t*, 64 uint8_t*); 65 int plat_udbg_get_frame_data(uint8_t, uint8_t, uint8_t*, uint8_t*, uint8_t*); 66 ipmi_ret_t plat_udbg_control_panel(uint8_t, uint8_t, uint8_t, uint8_t*, 67 uint8_t*); 68 int sendMeCmd(uint8_t, uint8_t, std::vector<uint8_t>&, std::vector<uint8_t>&); 69 70 int sendBicCmd(uint8_t, uint8_t, uint8_t, std::vector<uint8_t>&, 71 std::vector<uint8_t>&); 72 73 nlohmann::json oemData __attribute__((init_priority(101))); 74 75 constexpr const char* certPath = "/mnt/data/host/bios-rootcert"; 76 77 static constexpr size_t GUID_SIZE = 16; 78 // TODO Make offset and location runtime configurable to ensure we 79 // can make each define their own locations. 80 static constexpr off_t OFFSET_SYS_GUID = 0x17F0; 81 static constexpr const char* FRU_EEPROM = "/sys/bus/i2c/devices/6-0054/eeprom"; 82 void flushOemData(); 83 84 enum class LanParam : uint8_t 85 { 86 INPROGRESS = 0, 87 AUTHSUPPORT = 1, 88 AUTHENABLES = 2, 89 IP = 3, 90 IPSRC = 4, 91 MAC = 5, 92 SUBNET = 6, 93 GATEWAY = 12, 94 VLAN = 20, 95 CIPHER_SUITE_COUNT = 22, 96 CIPHER_SUITE_ENTRIES = 23, 97 IPV6 = 59, 98 }; 99 100 namespace network 101 { 102 103 constexpr auto ROOT = "/xyz/openbmc_project/network"; 104 constexpr auto SERVICE = "xyz.openbmc_project.Network"; 105 constexpr auto IPV4_TYPE = "ipv4"; 106 constexpr auto IPV6_TYPE = "ipv6"; 107 constexpr auto IPV4_PREFIX = "169.254"; 108 constexpr auto IPV6_PREFIX = "fe80"; 109 constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP"; 110 constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress"; 111 constexpr auto IPV4_PROTOCOL = "xyz.openbmc_project.Network.IP.Protocol.IPv4"; 112 constexpr auto IPV6_PROTOCOL = "xyz.openbmc_project.Network.IP.Protocol.IPv6"; 113 114 bool isLinkLocalIP(const std::string& address) 115 { 116 return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0; 117 } 118 119 DbusObjectInfo getIPObject(sdbusplus::bus_t& bus, const std::string& interface, 120 const std::string& serviceRoot, 121 const std::string& protocol, 122 const std::string& ethdev) 123 { 124 auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, ethdev); 125 126 if (objectTree.empty()) 127 { 128 log<level::ERR>("No Object has implemented the IP interface", 129 entry("INTERFACE=%s", interface.c_str())); 130 } 131 132 DbusObjectInfo objectInfo; 133 134 for (auto& object : objectTree) 135 { 136 auto variant = 137 ipmi::getDbusProperty(bus, object.second.begin()->first, 138 object.first, IP_INTERFACE, "Type"); 139 if (std::get<std::string>(variant) != protocol) 140 { 141 continue; 142 } 143 144 variant = ipmi::getDbusProperty(bus, object.second.begin()->first, 145 object.first, IP_INTERFACE, "Address"); 146 147 objectInfo = std::make_pair(object.first, object.second.begin()->first); 148 149 // if LinkLocalIP found look for Non-LinkLocalIP 150 if (isLinkLocalIP(std::get<std::string>(variant))) 151 { 152 continue; 153 } 154 else 155 { 156 break; 157 } 158 } 159 return objectInfo; 160 } 161 162 } // namespace network 163 164 namespace boot 165 { 166 using BootSource = 167 sdbusplus::xyz::openbmc_project::Control::Boot::server::Source::Sources; 168 using BootMode = 169 sdbusplus::xyz::openbmc_project::Control::Boot::server::Mode::Modes; 170 using BootType = 171 sdbusplus::xyz::openbmc_project::Control::Boot::server::Type::Types; 172 173 using IpmiValue = uint8_t; 174 175 std::map<IpmiValue, BootSource> sourceIpmiToDbus = { 176 {0x0f, BootSource::Default}, {0x00, BootSource::RemovableMedia}, 177 {0x01, BootSource::Network}, {0x02, BootSource::Disk}, 178 {0x03, BootSource::ExternalMedia}, {0x04, BootSource::RemovableMedia}, 179 {0x09, BootSource::Network}}; 180 181 std::map<IpmiValue, BootMode> modeIpmiToDbus = {{0x04, BootMode::Setup}, 182 {0x00, BootMode::Regular}}; 183 184 std::map<IpmiValue, BootType> typeIpmiToDbus = {{0x00, BootType::Legacy}, 185 {0x01, BootType::EFI}}; 186 187 std::map<std::optional<BootSource>, IpmiValue> sourceDbusToIpmi = { 188 {BootSource::Default, 0x0f}, 189 {BootSource::RemovableMedia, 0x00}, 190 {BootSource::Network, 0x01}, 191 {BootSource::Disk, 0x02}, 192 {BootSource::ExternalMedia, 0x03}}; 193 194 std::map<std::optional<BootMode>, IpmiValue> modeDbusToIpmi = { 195 {BootMode::Setup, 0x04}, {BootMode::Regular, 0x00}}; 196 197 std::map<std::optional<BootType>, IpmiValue> typeDbusToIpmi = { 198 {BootType::Legacy, 0x00}, {BootType::EFI, 0x01}}; 199 200 static constexpr auto bootEnableIntf = "xyz.openbmc_project.Object.Enable"; 201 static constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode"; 202 static constexpr auto bootSourceIntf = 203 "xyz.openbmc_project.Control.Boot.Source"; 204 static constexpr auto bootTypeIntf = "xyz.openbmc_project.Control.Boot.Type"; 205 static constexpr auto bootSourceProp = "BootSource"; 206 static constexpr auto bootModeProp = "BootMode"; 207 static constexpr auto bootTypeProp = "BootType"; 208 static constexpr auto bootEnableProp = "Enabled"; 209 210 std::tuple<std::string, std::string> objPath(size_t id) 211 { 212 std::string hostName = "host" + std::to_string(id); 213 std::string bootObjPath = 214 "/xyz/openbmc_project/control/" + hostName + "/boot"; 215 return std::make_tuple(std::move(bootObjPath), std::move(hostName)); 216 } 217 218 /* Helper functions to set boot order */ 219 void setBootOrder(std::string bootObjPath, const std::vector<uint8_t>& bootSeq, 220 std::string bootOrderKey) 221 { 222 if (bootSeq.size() != SIZE_BOOT_ORDER) 223 { 224 phosphor::logging::log<phosphor::logging::level::ERR>( 225 "Invalid Boot order length received"); 226 return; 227 } 228 229 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus(); 230 231 uint8_t mode = bootSeq.front(); 232 233 // SETTING BOOT MODE PROPERTY 234 uint8_t bootModeBit = mode & 0x04; 235 auto bootValue = ipmi::boot::modeIpmiToDbus.at(bootModeBit); 236 237 std::string bootOption = 238 sdbusplus::message::convert_to_string<boot::BootMode>(bootValue); 239 240 std::string service = 241 getService(*dbus, ipmi::boot::bootModeIntf, bootObjPath); 242 setDbusProperty(*dbus, service, bootObjPath, ipmi::boot::bootModeIntf, 243 ipmi::boot::bootModeProp, bootOption); 244 245 // SETTING BOOT SOURCE PROPERTY 246 auto bootOrder = ipmi::boot::sourceIpmiToDbus.at(bootSeq.at(1)); 247 std::string bootSource = 248 sdbusplus::message::convert_to_string<boot::BootSource>(bootOrder); 249 250 service = getService(*dbus, ipmi::boot::bootSourceIntf, bootObjPath); 251 setDbusProperty(*dbus, service, bootObjPath, ipmi::boot::bootSourceIntf, 252 ipmi::boot::bootSourceProp, bootSource); 253 254 // SETTING BOOT TYPE PROPERTY 255 uint8_t bootTypeBit = mode & 0x01; 256 auto bootTypeVal = ipmi::boot::typeIpmiToDbus.at(bootTypeBit); 257 258 std::string bootType = 259 sdbusplus::message::convert_to_string<boot::BootType>(bootTypeVal); 260 261 service = getService(*dbus, ipmi::boot::bootTypeIntf, bootObjPath); 262 263 setDbusProperty(*dbus, service, bootObjPath, ipmi::boot::bootTypeIntf, 264 ipmi::boot::bootTypeProp, bootType); 265 266 // Set the valid bit to boot enabled property 267 service = getService(*dbus, ipmi::boot::bootEnableIntf, bootObjPath); 268 269 setDbusProperty(*dbus, service, bootObjPath, ipmi::boot::bootEnableIntf, 270 ipmi::boot::bootEnableProp, 271 (mode & BOOT_MODE_BOOT_FLAG) ? true : false); 272 273 nlohmann::json bootMode; 274 275 bootMode["UEFI"] = (mode & BOOT_MODE_UEFI) ? true : false; 276 bootMode["CMOS_CLR"] = (mode & BOOT_MODE_CMOS_CLR) ? true : false; 277 bootMode["FORCE_BOOT"] = (mode & BOOT_MODE_FORCE_BOOT) ? true : false; 278 bootMode["BOOT_FLAG"] = (mode & BOOT_MODE_BOOT_FLAG) ? true : false; 279 oemData[bootOrderKey][KEY_BOOT_MODE] = bootMode; 280 281 /* Initialize boot sequence array */ 282 oemData[bootOrderKey][KEY_BOOT_SEQ] = {}; 283 for (size_t i = 1; i < SIZE_BOOT_ORDER; i++) 284 { 285 if (bootSeq.at(i) >= BOOT_SEQ_ARRAY_SIZE) 286 oemData[bootOrderKey][KEY_BOOT_SEQ][i - 1] = "NA"; 287 else 288 oemData[bootOrderKey][KEY_BOOT_SEQ][i - 1] = 289 bootSeqDefine[bootSeq.at(i)]; 290 } 291 292 flushOemData(); 293 } 294 295 void getBootOrder(std::string bootObjPath, std::vector<uint8_t>& bootSeq, 296 std::string hostName) 297 { 298 if (oemData.find(hostName) == oemData.end()) 299 { 300 /* Return default boot order 0100090203ff */ 301 bootSeq.push_back(BOOT_MODE_UEFI); 302 bootSeq.push_back(static_cast<uint8_t>(bootMap["USB_DEV"])); 303 bootSeq.push_back(static_cast<uint8_t>(bootMap["NET_IPV6"])); 304 bootSeq.push_back(static_cast<uint8_t>(bootMap["SATA_HDD"])); 305 bootSeq.push_back(static_cast<uint8_t>(bootMap["SATA_CD"])); 306 bootSeq.push_back(0xff); 307 308 phosphor::logging::log<phosphor::logging::level::INFO>( 309 "Set default boot order"); 310 setBootOrder(bootObjPath, bootSeq, hostName); 311 return; 312 } 313 314 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus(); 315 316 // GETTING PROPERTY OF MODE INTERFACE 317 318 std::string service = 319 getService(*dbus, ipmi::boot::bootModeIntf, bootObjPath); 320 Value variant = 321 getDbusProperty(*dbus, service, bootObjPath, ipmi::boot::bootModeIntf, 322 ipmi::boot::bootModeProp); 323 324 auto bootMode = sdbusplus::message::convert_from_string<boot::BootMode>( 325 std::get<std::string>(variant)); 326 327 uint8_t bootOption = ipmi::boot::modeDbusToIpmi.at(bootMode); 328 329 // GETTING PROPERTY OF TYPE INTERFACE 330 331 service = getService(*dbus, ipmi::boot::bootTypeIntf, bootObjPath); 332 variant = 333 getDbusProperty(*dbus, service, bootObjPath, ipmi::boot::bootTypeIntf, 334 ipmi::boot::bootTypeProp); 335 336 auto bootType = sdbusplus::message::convert_from_string<boot::BootType>( 337 std::get<std::string>(variant)); 338 339 // Get the valid bit to boot enabled property 340 service = getService(*dbus, ipmi::boot::bootEnableIntf, bootObjPath); 341 variant = 342 getDbusProperty(*dbus, service, bootObjPath, ipmi::boot::bootEnableIntf, 343 ipmi::boot::bootEnableProp); 344 345 bool validFlag = std::get<bool>(variant); 346 347 uint8_t bootTypeVal = ipmi::boot::typeDbusToIpmi.at(bootType); 348 349 bootSeq.push_back(bootOption | bootTypeVal); 350 351 if (validFlag) 352 { 353 bootSeq.front() |= BOOT_MODE_BOOT_FLAG; 354 } 355 356 nlohmann::json bootModeJson = oemData[hostName][KEY_BOOT_MODE]; 357 if (bootModeJson["CMOS_CLR"]) 358 bootSeq.front() |= BOOT_MODE_CMOS_CLR; 359 360 for (int i = 1; i < SIZE_BOOT_ORDER; i++) 361 { 362 std::string seqStr = oemData[hostName][KEY_BOOT_SEQ][i - 1]; 363 if (bootMap.find(seqStr) != bootMap.end()) 364 bootSeq.push_back(bootMap[seqStr]); 365 else 366 bootSeq.push_back(0xff); 367 } 368 } 369 370 } // namespace boot 371 372 //---------------------------------------------------------------------- 373 // Helper functions for storing oem data 374 //---------------------------------------------------------------------- 375 376 void flushOemData() 377 { 378 std::ofstream file(JSON_OEM_DATA_FILE); 379 file << oemData; 380 file.close(); 381 return; 382 } 383 384 std::string bytesToStr(uint8_t* byte, int len) 385 { 386 std::stringstream ss; 387 int i; 388 389 ss << std::hex; 390 for (i = 0; i < len; i++) 391 { 392 ss << std::setw(2) << std::setfill('0') << (int)byte[i]; 393 } 394 395 return ss.str(); 396 } 397 398 int strToBytes(std::string& str, uint8_t* data) 399 { 400 std::string sstr; 401 size_t i; 402 403 for (i = 0; i < (str.length()) / 2; i++) 404 { 405 sstr = str.substr(i * 2, 2); 406 data[i] = (uint8_t)std::strtol(sstr.c_str(), NULL, 16); 407 } 408 return i; 409 } 410 411 int readDimmType(std::string& data, uint8_t param) 412 { 413 nlohmann::json dimmObj; 414 /* Get dimm type names stored in json file */ 415 std::ifstream file(JSON_DIMM_TYPE_FILE); 416 if (file) 417 { 418 file >> dimmObj; 419 file.close(); 420 } 421 else 422 { 423 phosphor::logging::log<phosphor::logging::level::ERR>( 424 "DIMM type names file not found", 425 phosphor::logging::entry("DIMM_TYPE_FILE=%s", JSON_DIMM_TYPE_FILE)); 426 return -1; 427 } 428 429 std::string dimmKey = "dimm_type" + std::to_string(param); 430 auto obj = dimmObj[dimmKey]["short_name"]; 431 data = obj; 432 return 0; 433 } 434 435 ipmi_ret_t getNetworkData(uint8_t lan_param, char* data) 436 { 437 ipmi_ret_t rc = ipmi::ccSuccess; 438 sdbusplus::bus_t bus(ipmid_get_sd_bus_connection()); 439 440 const std::string ethdevice = "eth0"; 441 442 switch (static_cast<LanParam>(lan_param)) 443 { 444 case LanParam::IP: 445 { 446 std::string ipaddress; 447 auto ipObjectInfo = ipmi::network::getIPObject( 448 bus, ipmi::network::IP_INTERFACE, ipmi::network::ROOT, 449 ipmi::network::IPV4_PROTOCOL, ethdevice); 450 451 auto properties = ipmi::getAllDbusProperties( 452 bus, ipObjectInfo.second, ipObjectInfo.first, 453 ipmi::network::IP_INTERFACE); 454 455 ipaddress = std::get<std::string>(properties["Address"]); 456 457 std::strcpy(data, ipaddress.c_str()); 458 } 459 break; 460 461 case LanParam::IPV6: 462 { 463 std::string ipaddress; 464 auto ipObjectInfo = ipmi::network::getIPObject( 465 bus, ipmi::network::IP_INTERFACE, ipmi::network::ROOT, 466 ipmi::network::IPV6_PROTOCOL, ethdevice); 467 468 auto properties = ipmi::getAllDbusProperties( 469 bus, ipObjectInfo.second, ipObjectInfo.first, 470 ipmi::network::IP_INTERFACE); 471 472 ipaddress = std::get<std::string>(properties["Address"]); 473 474 std::strcpy(data, ipaddress.c_str()); 475 } 476 break; 477 478 case LanParam::MAC: 479 { 480 std::string macAddress; 481 auto macObjectInfo = 482 ipmi::getDbusObject(bus, ipmi::network::MAC_INTERFACE, 483 ipmi::network::ROOT, ethdevice); 484 485 auto variant = ipmi::getDbusProperty( 486 bus, macObjectInfo.second, macObjectInfo.first, 487 ipmi::network::MAC_INTERFACE, "MACAddress"); 488 489 macAddress = std::get<std::string>(variant); 490 491 sscanf(macAddress.c_str(), ipmi::network::MAC_ADDRESS_FORMAT, 492 (data), (data + 1), (data + 2), (data + 3), (data + 4), 493 (data + 5)); 494 std::strcpy(data, macAddress.c_str()); 495 } 496 break; 497 498 default: 499 rc = ipmi::ccParmOutOfRange; 500 } 501 return rc; 502 } 503 504 bool isMultiHostPlatform() 505 { 506 bool platform; 507 if (hostInstances == "0") 508 { 509 platform = false; 510 } 511 else 512 { 513 platform = true; 514 } 515 return platform; 516 } 517 518 // return "" equals failed 519 std::string getMotherBoardFruPath() 520 { 521 std::vector<std::string> paths; 522 static constexpr const auto depth = 0; 523 sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection()); 524 525 auto mapperCall = dbus.new_method_call( 526 "xyz.openbmc_project.ObjectMapper", 527 "/xyz/openbmc_project/object_mapper", 528 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths"); 529 static constexpr auto interface = { 530 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"}; 531 532 mapperCall.append("/xyz/openbmc_project/inventory/", depth, interface); 533 try 534 { 535 auto reply = dbus.call(mapperCall); 536 reply.read(paths); 537 } 538 catch (sdbusplus::exception_t& e) 539 { 540 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 541 return ""; 542 } 543 544 for (const auto& path : paths) 545 { 546 return path; 547 } 548 549 return ""; 550 } 551 552 // return "" equals failed 553 std::string getMotherBoardFruName() 554 { 555 std::string path = getMotherBoardFruPath(); 556 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus(); 557 std::string service = "xyz.openbmc_project.EntityManager"; 558 559 try 560 { 561 auto value = ipmi::getDbusProperty( 562 *dbus, service, path, "xyz.openbmc_project.Inventory.Item.Board", 563 "Name"); 564 return std::get<std::string>(value); 565 } 566 catch (sdbusplus::exception_t& e) 567 { 568 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 569 return ""; 570 } 571 } 572 573 // return code: 0 successful 574 int8_t getFruData(std::string& data, std::string& name) 575 { 576 size_t pos; 577 static constexpr const auto depth = 0; 578 std::vector<std::string> paths; 579 std::string machinePath; 580 std::string baseBoard = getMotherBoardFruPath(); 581 baseBoard = baseBoard.empty() ? "Baseboard" : baseBoard; 582 583 bool platform = isMultiHostPlatform(); 584 if (platform == true) 585 { 586 getSelectorPosition(pos); 587 } 588 589 sd_bus* bus = NULL; 590 int ret = sd_bus_default_system(&bus); 591 if (ret < 0) 592 { 593 phosphor::logging::log<phosphor::logging::level::ERR>( 594 "Failed to connect to system bus", 595 phosphor::logging::entry("ERRNO=0x%X", -ret)); 596 sd_bus_unref(bus); 597 return -1; 598 } 599 sdbusplus::bus_t dbus(bus); 600 auto mapperCall = dbus.new_method_call( 601 "xyz.openbmc_project.ObjectMapper", 602 "/xyz/openbmc_project/object_mapper", 603 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths"); 604 static constexpr std::array<const char*, 1> interface = { 605 "xyz.openbmc_project.Inventory.Decorator.Asset"}; 606 mapperCall.append("/xyz/openbmc_project/inventory/", depth, interface); 607 608 try 609 { 610 auto reply = dbus.call(mapperCall); 611 reply.read(paths); 612 } 613 catch (sdbusplus::exception_t& e) 614 { 615 phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); 616 return -1; 617 } 618 619 for (const auto& path : paths) 620 { 621 if (platform == true) 622 { 623 if (pos == BMC_POS) 624 { 625 machinePath = baseBoard; 626 } 627 else 628 { 629 machinePath = "_" + std::to_string(pos); 630 } 631 } 632 else 633 { 634 machinePath = baseBoard; 635 } 636 637 auto found = path.find(machinePath); 638 if (found == std::string::npos) 639 { 640 continue; 641 } 642 643 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus(); 644 std::string service = getService( 645 *dbus, "xyz.openbmc_project.Inventory.Decorator.Asset", path); 646 647 auto Value = ipmi::getDbusProperty( 648 *dbus, service, path, 649 "xyz.openbmc_project.Inventory.Decorator.Asset", name); 650 651 data = std::get<std::string>(Value); 652 return 0; 653 } 654 return -1; 655 } 656 657 int8_t sysConfig(std::vector<std::string>& data, size_t pos) 658 { 659 nlohmann::json sysObj; 660 std::string dimmInfo = KEY_Q_DIMM_INFO + std::to_string(pos); 661 std::string result, typeName; 662 uint8_t res[MAX_BUF]; 663 664 /* Get sysConfig data stored in json file */ 665 std::ifstream file(JSON_OEM_DATA_FILE); 666 if (file) 667 { 668 file >> sysObj; 669 file.close(); 670 } 671 else 672 { 673 phosphor::logging::log<phosphor::logging::level::ERR>( 674 "oemData file not found", 675 phosphor::logging::entry("OEM_DATA_FILE=%s", JSON_OEM_DATA_FILE)); 676 return -1; 677 } 678 679 if (sysObj.find(dimmInfo) == sysObj.end()) 680 { 681 phosphor::logging::log<phosphor::logging::level::ERR>( 682 "sysconfig key not available", 683 phosphor::logging::entry("SYS_JSON_KEY=%s", dimmInfo.c_str())); 684 return -1; 685 } 686 /* Get dimm type names stored in json file */ 687 nlohmann::json dimmObj; 688 std::ifstream dimmFile(JSON_DIMM_TYPE_FILE); 689 if (file) 690 { 691 dimmFile >> dimmObj; 692 dimmFile.close(); 693 } 694 else 695 { 696 phosphor::logging::log<phosphor::logging::level::ERR>( 697 "DIMM type names file not found", 698 phosphor::logging::entry("DIMM_TYPE_FILE=%s", JSON_DIMM_TYPE_FILE)); 699 return -1; 700 } 701 std::vector<std::string> a; 702 for (auto& j : dimmObj.items()) 703 { 704 std::string name = j.key(); 705 a.push_back(name); 706 } 707 708 uint8_t len = a.size(); 709 for (uint8_t ii = 0; ii < len; ii++) 710 { 711 std::string indKey = std::to_string(ii); 712 std::string speedSize = sysObj[dimmInfo][indKey][DIMM_SPEED]; 713 strToBytes(speedSize, res); 714 auto speed = (res[1] << 8 | res[0]); 715 size_t dimmSize = ((res[3] << 8 | res[2]) / 1000); 716 717 if (dimmSize == 0) 718 { 719 std::cerr << "Dimm information not available for slot_" + 720 std::to_string(ii) 721 << std::endl; 722 continue; 723 } 724 std::string type = sysObj[dimmInfo][indKey][DIMM_TYPE]; 725 std::string dualInlineMem = sysObj[dimmInfo][indKey][KEY_DIMM_TYPE]; 726 strToBytes(type, res); 727 size_t dimmType = res[0]; 728 if (dimmVenMap.find(dimmType) == dimmVenMap.end()) 729 { 730 typeName = "unknown"; 731 } 732 else 733 { 734 typeName = dimmVenMap[dimmType]; 735 } 736 result = dualInlineMem + "/" + typeName + "/" + std::to_string(speed) + 737 "MHz" + "/" + std::to_string(dimmSize) + "GB"; 738 data.push_back(result); 739 } 740 return 0; 741 } 742 743 int8_t procInfo(std::string& result, size_t pos) 744 { 745 std::vector<char> data; 746 uint8_t res[MAX_BUF]; 747 std::string procIndex = "00"; 748 nlohmann::json proObj; 749 std::string procInfo = KEY_Q_PROC_INFO + std::to_string(pos); 750 /* Get processor data stored in json file */ 751 std::ifstream file(JSON_OEM_DATA_FILE); 752 if (file) 753 { 754 file >> proObj; 755 file.close(); 756 } 757 else 758 { 759 phosphor::logging::log<phosphor::logging::level::ERR>( 760 "oemData file not found", 761 phosphor::logging::entry("OEM_DATA_FILE=%s", JSON_OEM_DATA_FILE)); 762 return -1; 763 } 764 if (proObj.find(procInfo) == proObj.end()) 765 { 766 phosphor::logging::log<phosphor::logging::level::ERR>( 767 "processor info key not available", 768 phosphor::logging::entry("PROC_JSON_KEY=%s", procInfo.c_str())); 769 return -1; 770 } 771 std::string procName = proObj[procInfo][procIndex][KEY_PROC_NAME]; 772 std::string basicInfo = proObj[procInfo][procIndex][KEY_BASIC_INFO]; 773 // Processor Product Name 774 strToBytes(procName, res); 775 data.assign(reinterpret_cast<char*>(&res), 776 reinterpret_cast<char*>(&res) + sizeof(res)); 777 778 std::string s(data.begin(), data.end()); 779 std::regex regex(" "); 780 std::vector<std::string> productName( 781 std::sregex_token_iterator(s.begin(), s.end(), regex, -1), 782 std::sregex_token_iterator()); 783 784 // Processor core and frequency 785 strToBytes(basicInfo, res); 786 uint16_t coreNum = res[0]; 787 double procFrequency = (float)(res[4] << 8 | res[3]) / 1000; 788 result = "CPU:" + productName[2] + "/" + std::to_string(procFrequency) + 789 "GHz" + "/" + std::to_string(coreNum) + "c"; 790 return 0; 791 } 792 793 typedef struct 794 { 795 uint8_t cur_power_state; 796 uint8_t last_power_event; 797 uint8_t misc_power_state; 798 uint8_t front_panel_button_cap_status; 799 } ipmi_get_chassis_status_t; 800 801 //---------------------------------------------------------------------- 802 // Get Debug Frame Info 803 //---------------------------------------------------------------------- 804 ipmi_ret_t ipmiOemDbgGetFrameInfo( 805 ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, ipmi_response_t response, 806 ipmi_data_len_t data_len, ipmi_context_t) 807 { 808 uint8_t* req = reinterpret_cast<uint8_t*>(request); 809 uint8_t* res = reinterpret_cast<uint8_t*>(response); 810 uint8_t num_frames = debugCardFrameSize; 811 812 std::memcpy(res, req, SIZE_IANA_ID); // IANA ID 813 res[SIZE_IANA_ID] = num_frames; 814 *data_len = SIZE_IANA_ID + 1; 815 816 return ipmi::ccSuccess; 817 } 818 819 //---------------------------------------------------------------------- 820 // Get Debug Updated Frames 821 //---------------------------------------------------------------------- 822 ipmi_ret_t ipmiOemDbgGetUpdFrames( 823 ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, ipmi_response_t response, 824 ipmi_data_len_t data_len, ipmi_context_t) 825 { 826 uint8_t* req = reinterpret_cast<uint8_t*>(request); 827 uint8_t* res = reinterpret_cast<uint8_t*>(response); 828 uint8_t num_updates = 3; 829 *data_len = 4; 830 831 std::memcpy(res, req, SIZE_IANA_ID); // IANA ID 832 res[SIZE_IANA_ID] = num_updates; 833 *data_len = SIZE_IANA_ID + num_updates + 1; 834 res[SIZE_IANA_ID + 1] = 1; // info page update 835 res[SIZE_IANA_ID + 2] = 2; // cri sel update 836 res[SIZE_IANA_ID + 3] = 3; // cri sensor update 837 838 return ipmi::ccSuccess; 839 } 840 841 //---------------------------------------------------------------------- 842 // Get Debug POST Description 843 //---------------------------------------------------------------------- 844 ipmi_ret_t ipmiOemDbgGetPostDesc( 845 ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, ipmi_response_t response, 846 ipmi_data_len_t data_len, ipmi_context_t) 847 { 848 uint8_t* req = reinterpret_cast<uint8_t*>(request); 849 uint8_t* res = reinterpret_cast<uint8_t*>(response); 850 uint8_t index = 0; 851 uint8_t next = 0; 852 uint8_t end = 0; 853 uint8_t phase = 0; 854 uint8_t descLen = 0; 855 int ret; 856 857 index = req[3]; 858 phase = req[4]; 859 860 ret = plat_udbg_get_post_desc(index, &next, phase, &end, &descLen, &res[8]); 861 if (ret) 862 { 863 memcpy(res, req, SIZE_IANA_ID); // IANA ID 864 *data_len = SIZE_IANA_ID; 865 return ipmi::ccUnspecifiedError; 866 } 867 868 memcpy(res, req, SIZE_IANA_ID); // IANA ID 869 res[3] = index; 870 res[4] = next; 871 res[5] = phase; 872 res[6] = end; 873 res[7] = descLen; 874 *data_len = SIZE_IANA_ID + 5 + descLen; 875 876 return ipmi::ccSuccess; 877 } 878 879 //---------------------------------------------------------------------- 880 // Get Debug GPIO Description 881 //---------------------------------------------------------------------- 882 ipmi_ret_t ipmiOemDbgGetGpioDesc( 883 ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, ipmi_response_t response, 884 ipmi_data_len_t data_len, ipmi_context_t) 885 { 886 uint8_t* req = reinterpret_cast<uint8_t*>(request); 887 uint8_t* res = reinterpret_cast<uint8_t*>(response); 888 889 uint8_t index = 0; 890 uint8_t next = 0; 891 uint8_t level = 0; 892 uint8_t pinDef = 0; 893 uint8_t descLen = 0; 894 int ret; 895 896 index = req[3]; 897 898 ret = plat_udbg_get_gpio_desc(index, &next, &level, &pinDef, &descLen, 899 &res[8]); 900 if (ret) 901 { 902 memcpy(res, req, SIZE_IANA_ID); // IANA ID 903 *data_len = SIZE_IANA_ID; 904 return ipmi::ccUnspecifiedError; 905 } 906 907 memcpy(res, req, SIZE_IANA_ID); // IANA ID 908 res[3] = index; 909 res[4] = next; 910 res[5] = level; 911 res[6] = pinDef; 912 res[7] = descLen; 913 *data_len = SIZE_IANA_ID + 5 + descLen; 914 915 return ipmi::ccSuccess; 916 } 917 918 //---------------------------------------------------------------------- 919 // Get Debug Frame Data 920 //---------------------------------------------------------------------- 921 ipmi_ret_t ipmiOemDbgGetFrameData( 922 ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, ipmi_response_t response, 923 ipmi_data_len_t data_len, ipmi_context_t) 924 { 925 uint8_t* req = reinterpret_cast<uint8_t*>(request); 926 uint8_t* res = reinterpret_cast<uint8_t*>(response); 927 uint8_t frame; 928 uint8_t page; 929 uint8_t next; 930 uint8_t count; 931 int ret; 932 933 frame = req[3]; 934 page = req[4]; 935 936 ret = plat_udbg_get_frame_data(frame, page, &next, &count, &res[7]); 937 if (ret) 938 { 939 memcpy(res, req, SIZE_IANA_ID); // IANA ID 940 *data_len = SIZE_IANA_ID; 941 return ipmi::ccUnspecifiedError; 942 } 943 944 memcpy(res, req, SIZE_IANA_ID); // IANA ID 945 res[3] = frame; 946 res[4] = page; 947 res[5] = next; 948 res[6] = count; 949 *data_len = SIZE_IANA_ID + 4 + count; 950 951 return ipmi::ccSuccess; 952 } 953 954 //---------------------------------------------------------------------- 955 // Get Debug Control Panel 956 //---------------------------------------------------------------------- 957 ipmi_ret_t ipmiOemDbgGetCtrlPanel( 958 ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, ipmi_response_t response, 959 ipmi_data_len_t data_len, ipmi_context_t) 960 { 961 uint8_t* req = reinterpret_cast<uint8_t*>(request); 962 uint8_t* res = reinterpret_cast<uint8_t*>(response); 963 964 uint8_t panel; 965 uint8_t operation; 966 uint8_t item; 967 uint8_t count; 968 ipmi_ret_t ret; 969 970 panel = req[3]; 971 operation = req[4]; 972 item = req[5]; 973 974 ret = plat_udbg_control_panel(panel, operation, item, &count, &res[3]); 975 976 std::memcpy(res, req, SIZE_IANA_ID); // IANA ID 977 *data_len = SIZE_IANA_ID + count; 978 979 return ret; 980 } 981 982 //---------------------------------------------------------------------- 983 // Set Dimm Info (CMD_OEM_SET_DIMM_INFO) 984 //---------------------------------------------------------------------- 985 ipmi_ret_t ipmiOemSetDimmInfo(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, 986 ipmi_response_t, ipmi_data_len_t data_len, 987 ipmi_context_t) 988 { 989 uint8_t* req = reinterpret_cast<uint8_t*>(request); 990 991 uint8_t index = req[0]; 992 uint8_t type = req[1]; 993 uint16_t speed; 994 uint32_t size; 995 996 memcpy(&speed, &req[2], 2); 997 memcpy(&size, &req[4], 4); 998 999 std::stringstream ss; 1000 ss << std::hex; 1001 ss << std::setw(2) << std::setfill('0') << (int)index; 1002 1003 oemData[KEY_SYS_CONFIG][ss.str()][KEY_DIMM_INDEX] = index; 1004 oemData[KEY_SYS_CONFIG][ss.str()][KEY_DIMM_TYPE] = type; 1005 oemData[KEY_SYS_CONFIG][ss.str()][KEY_DIMM_SPEED] = speed; 1006 oemData[KEY_SYS_CONFIG][ss.str()][KEY_DIMM_SIZE] = size; 1007 1008 flushOemData(); 1009 1010 *data_len = 0; 1011 1012 return ipmi::ccSuccess; 1013 } 1014 1015 //---------------------------------------------------------------------- 1016 // Get Board ID (CMD_OEM_GET_BOARD_ID) 1017 //---------------------------------------------------------------------- 1018 ipmi_ret_t ipmiOemGetBoardID(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t, 1019 ipmi_response_t, ipmi_data_len_t data_len, 1020 ipmi_context_t) 1021 { 1022 /* TODO: Needs to implement this after GPIO implementation */ 1023 *data_len = 0; 1024 1025 return ipmi::ccSuccess; 1026 } 1027 1028 //---------------------------------------------------------------------- 1029 // Get port 80 record (CMD_OEM_GET_80PORT_RECORD) 1030 //---------------------------------------------------------------------- 1031 ipmi::RspType<std::vector<uint8_t>> ipmiOemGet80PortRecord( 1032 ipmi::Context::ptr ctx) 1033 { 1034 auto postCodeService = "xyz.openbmc_project.State.Boot.PostCode" + 1035 std::to_string(ctx->hostIdx + 1); 1036 auto postCodeObjPath = "/xyz/openbmc_project/State/Boot/PostCode" + 1037 std::to_string(ctx->hostIdx + 1); 1038 constexpr auto postCodeInterface = 1039 "xyz.openbmc_project.State.Boot.PostCode"; 1040 const static uint16_t lastestPostCodeIndex = 1; 1041 constexpr const auto maxPostCodeLen = 1042 224; // The length must be lower than IPMB limitation 1043 size_t startIndex = 0; 1044 1045 std::vector<std::tuple<uint64_t, std::vector<uint8_t>>> postCodes; 1046 std::vector<uint8_t> resData; 1047 1048 auto conn = getSdBus(); 1049 /* Get the post codes by calling GetPostCodes method */ 1050 auto msg = 1051 conn->new_method_call(postCodeService.c_str(), postCodeObjPath.c_str(), 1052 postCodeInterface, "GetPostCodes"); 1053 msg.append(lastestPostCodeIndex); 1054 1055 try 1056 { 1057 auto reply = conn->call(msg); 1058 reply.read(postCodes); 1059 } 1060 catch (const sdbusplus::exception::SdBusError& e) 1061 { 1062 phosphor::logging::log<phosphor::logging::level::ERR>( 1063 "IPMI Get80PortRecord Failed in call method", 1064 phosphor::logging::entry("ERROR=%s", e.what())); 1065 return ipmi::responseUnspecifiedError(); 1066 } 1067 1068 /* Get post code data */ 1069 for (size_t i = 0; i < postCodes.size(); ++i) 1070 { 1071 uint64_t primaryPostCode = std::get<uint64_t>(postCodes[i]); 1072 for (int j = postCodeSize - 1; j >= 0; --j) 1073 { 1074 uint8_t postCode = 1075 ((primaryPostCode >> (sizeof(uint64_t) * j)) & 0xFF); 1076 resData.emplace_back(postCode); 1077 } 1078 } 1079 1080 std::vector<uint8_t> response; 1081 if (resData.size() > maxPostCodeLen) 1082 { 1083 startIndex = resData.size() - maxPostCodeLen; 1084 } 1085 1086 response.assign(resData.begin() + startIndex, resData.end()); 1087 1088 return ipmi::responseSuccess(response); 1089 } 1090 1091 //---------------------------------------------------------------------- 1092 // Set Boot Order (CMD_OEM_SET_BOOT_ORDER) 1093 //---------------------------------------------------------------------- 1094 ipmi::RspType<std::vector<uint8_t>> ipmiOemSetBootOrder( 1095 ipmi::Context::ptr ctx, std::vector<uint8_t> bootSeq) 1096 { 1097 size_t len = bootSeq.size(); 1098 1099 if (len != SIZE_BOOT_ORDER) 1100 { 1101 phosphor::logging::log<phosphor::logging::level::ERR>( 1102 "Invalid Boot order length received"); 1103 return ipmi::responseReqDataLenInvalid(); 1104 } 1105 1106 std::optional<size_t> hostId = findHost(ctx->hostIdx); 1107 1108 if (!hostId) 1109 { 1110 phosphor::logging::log<phosphor::logging::level::ERR>( 1111 "Invalid Host Id received"); 1112 return ipmi::responseInvalidCommand(); 1113 } 1114 auto [bootObjPath, hostName] = ipmi::boot::objPath(*hostId); 1115 1116 ipmi::boot::setBootOrder(bootObjPath, bootSeq, hostName); 1117 1118 return ipmi::responseSuccess(bootSeq); 1119 } 1120 1121 //---------------------------------------------------------------------- 1122 // Get Boot Order (CMD_OEM_GET_BOOT_ORDER) 1123 //---------------------------------------------------------------------- 1124 ipmi::RspType<std::vector<uint8_t>> ipmiOemGetBootOrder(ipmi::Context::ptr ctx) 1125 { 1126 std::vector<uint8_t> bootSeq; 1127 1128 std::optional<size_t> hostId = findHost(ctx->hostIdx); 1129 1130 if (!hostId) 1131 { 1132 phosphor::logging::log<phosphor::logging::level::ERR>( 1133 "Invalid Host Id received"); 1134 return ipmi::responseInvalidCommand(); 1135 } 1136 auto [bootObjPath, hostName] = ipmi::boot::objPath(*hostId); 1137 1138 ipmi::boot::getBootOrder(bootObjPath, bootSeq, hostName); 1139 1140 return ipmi::responseSuccess(bootSeq); 1141 } 1142 // Set Machine Config Info (CMD_OEM_SET_MACHINE_CONFIG_INFO) 1143 //---------------------------------------------------------------------- 1144 ipmi_ret_t ipmiOemSetMachineCfgInfo(ipmi_netfn_t, ipmi_cmd_t, 1145 ipmi_request_t request, ipmi_response_t, 1146 ipmi_data_len_t data_len, ipmi_context_t) 1147 { 1148 machineConfigInfo_t* req = reinterpret_cast<machineConfigInfo_t*>(request); 1149 uint8_t len = *data_len; 1150 1151 *data_len = 0; 1152 1153 if (len < sizeof(machineConfigInfo_t)) 1154 { 1155 phosphor::logging::log<phosphor::logging::level::ERR>( 1156 "Invalid machine configuration length received"); 1157 return ipmi::ccReqDataLenInvalid; 1158 } 1159 1160 if (req->chassis_type >= sizeof(chassisType) / sizeof(uint8_t*)) 1161 oemData[KEY_MC_CONFIG][KEY_MC_CHAS_TYPE] = "UNKNOWN"; 1162 else 1163 oemData[KEY_MC_CONFIG][KEY_MC_CHAS_TYPE] = 1164 chassisType[req->chassis_type]; 1165 1166 if (req->mb_type >= sizeof(mbType) / sizeof(uint8_t*)) 1167 oemData[KEY_MC_CONFIG][KEY_MC_MB_TYPE] = "UNKNOWN"; 1168 else 1169 oemData[KEY_MC_CONFIG][KEY_MC_MB_TYPE] = mbType[req->mb_type]; 1170 1171 oemData[KEY_MC_CONFIG][KEY_MC_PROC_CNT] = req->proc_cnt; 1172 oemData[KEY_MC_CONFIG][KEY_MC_MEM_CNT] = req->mem_cnt; 1173 oemData[KEY_MC_CONFIG][KEY_MC_HDD35_CNT] = req->hdd35_cnt; 1174 oemData[KEY_MC_CONFIG][KEY_MC_HDD25_CNT] = req->hdd25_cnt; 1175 1176 if (req->riser_type >= sizeof(riserType) / sizeof(uint8_t*)) 1177 oemData[KEY_MC_CONFIG][KEY_MC_RSR_TYPE] = "UNKNOWN"; 1178 else 1179 oemData[KEY_MC_CONFIG][KEY_MC_RSR_TYPE] = riserType[req->riser_type]; 1180 1181 oemData[KEY_MC_CONFIG][KEY_MC_PCIE_LOC] = {}; 1182 int i = 0; 1183 if (req->pcie_card_loc & BIT_0) 1184 oemData[KEY_MC_CONFIG][KEY_MC_PCIE_LOC][i++] = "SLOT1"; 1185 if (req->pcie_card_loc & BIT_1) 1186 oemData[KEY_MC_CONFIG][KEY_MC_PCIE_LOC][i++] = "SLOT2"; 1187 if (req->pcie_card_loc & BIT_2) 1188 oemData[KEY_MC_CONFIG][KEY_MC_PCIE_LOC][i++] = "SLOT3"; 1189 if (req->pcie_card_loc & BIT_3) 1190 oemData[KEY_MC_CONFIG][KEY_MC_PCIE_LOC][i++] = "SLOT4"; 1191 1192 if (req->slot1_pcie_type >= sizeof(pcieType) / sizeof(uint8_t*)) 1193 oemData[KEY_MC_CONFIG][KEY_MC_SLOT1_TYPE] = "UNKNOWN"; 1194 else 1195 oemData[KEY_MC_CONFIG][KEY_MC_SLOT1_TYPE] = 1196 pcieType[req->slot1_pcie_type]; 1197 1198 if (req->slot2_pcie_type >= sizeof(pcieType) / sizeof(uint8_t*)) 1199 oemData[KEY_MC_CONFIG][KEY_MC_SLOT2_TYPE] = "UNKNOWN"; 1200 else 1201 oemData[KEY_MC_CONFIG][KEY_MC_SLOT2_TYPE] = 1202 pcieType[req->slot2_pcie_type]; 1203 1204 if (req->slot3_pcie_type >= sizeof(pcieType) / sizeof(uint8_t*)) 1205 oemData[KEY_MC_CONFIG][KEY_MC_SLOT3_TYPE] = "UNKNOWN"; 1206 else 1207 oemData[KEY_MC_CONFIG][KEY_MC_SLOT3_TYPE] = 1208 pcieType[req->slot3_pcie_type]; 1209 1210 if (req->slot4_pcie_type >= sizeof(pcieType) / sizeof(uint8_t*)) 1211 oemData[KEY_MC_CONFIG][KEY_MC_SLOT4_TYPE] = "UNKNOWN"; 1212 else 1213 oemData[KEY_MC_CONFIG][KEY_MC_SLOT4_TYPE] = 1214 pcieType[req->slot4_pcie_type]; 1215 1216 oemData[KEY_MC_CONFIG][KEY_MC_AEP_CNT] = req->aep_mem_cnt; 1217 1218 flushOemData(); 1219 1220 return ipmi::ccSuccess; 1221 } 1222 1223 //---------------------------------------------------------------------- 1224 // Set POST start (CMD_OEM_SET_POST_START) 1225 //---------------------------------------------------------------------- 1226 ipmi_ret_t ipmiOemSetPostStart(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t, 1227 ipmi_response_t, ipmi_data_len_t data_len, 1228 ipmi_context_t) 1229 { 1230 phosphor::logging::log<phosphor::logging::level::INFO>("POST Start Event"); 1231 1232 /* Do nothing, return success */ 1233 *data_len = 0; 1234 return ipmi::ccSuccess; 1235 } 1236 1237 //---------------------------------------------------------------------- 1238 // Set POST End (CMD_OEM_SET_POST_END) 1239 //---------------------------------------------------------------------- 1240 ipmi_ret_t ipmiOemSetPostEnd(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t, 1241 ipmi_response_t, ipmi_data_len_t data_len, 1242 ipmi_context_t) 1243 { 1244 struct timespec ts; 1245 1246 phosphor::logging::log<phosphor::logging::level::INFO>("POST End Event"); 1247 1248 *data_len = 0; 1249 1250 // Timestamp post end time. 1251 clock_gettime(CLOCK_REALTIME, &ts); 1252 oemData[KEY_TS_SLED] = ts.tv_sec; 1253 flushOemData(); 1254 1255 // Sync time with system 1256 // TODO: Add code for syncing time 1257 1258 return ipmi::ccSuccess; 1259 } 1260 1261 //---------------------------------------------------------------------- 1262 // Set PPIN Info (CMD_OEM_SET_PPIN_INFO) 1263 //---------------------------------------------------------------------- 1264 // Inform BMC about PPIN data of 8 bytes for each CPU 1265 // 1266 // Request: 1267 // Byte 1:8 – CPU0 PPIN data 1268 // Optional: 1269 // Byte 9:16 – CPU1 PPIN data 1270 // 1271 // Response: 1272 // Byte 1 – Completion Code 1273 ipmi_ret_t ipmiOemSetPPINInfo(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, 1274 ipmi_response_t, ipmi_data_len_t data_len, 1275 ipmi_context_t) 1276 { 1277 uint8_t* req = reinterpret_cast<uint8_t*>(request); 1278 std::string ppinStr; 1279 int len; 1280 1281 if (*data_len > SIZE_CPU_PPIN * 2) 1282 len = SIZE_CPU_PPIN * 2; 1283 else 1284 len = *data_len; 1285 *data_len = 0; 1286 1287 ppinStr = bytesToStr(req, len); 1288 oemData[KEY_PPIN_INFO] = ppinStr.c_str(); 1289 flushOemData(); 1290 1291 return ipmi::ccSuccess; 1292 } 1293 1294 //---------------------------------------------------------------------- 1295 // Set ADR Trigger (CMD_OEM_SET_ADR_TRIGGER) 1296 //---------------------------------------------------------------------- 1297 ipmi_ret_t ipmiOemSetAdrTrigger(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t, 1298 ipmi_response_t, ipmi_data_len_t data_len, 1299 ipmi_context_t) 1300 { 1301 /* Do nothing, return success */ 1302 *data_len = 0; 1303 return ipmi::ccSuccess; 1304 } 1305 1306 // Helper function to set guid at offset in EEPROM 1307 [[maybe_unused]] static int setGUID(off_t offset, uint8_t* guid) 1308 { 1309 int fd = -1; 1310 ssize_t len; 1311 int ret = 0; 1312 std::string eepromPath = FRU_EEPROM; 1313 1314 // find the eeprom path of MB FRU 1315 auto device = getMbFruDevice(); 1316 if (device) 1317 { 1318 auto [bus, address] = *device; 1319 std::stringstream ss; 1320 ss << "/sys/bus/i2c/devices/" << static_cast<int>(bus) << "-" 1321 << std::setw(4) << std::setfill('0') << std::hex 1322 << static_cast<int>(address) << "/eeprom"; 1323 eepromPath = ss.str(); 1324 } 1325 1326 errno = 0; 1327 1328 // Check if file is present 1329 if (access(eepromPath.c_str(), F_OK) == -1) 1330 { 1331 std::cerr << "Unable to access: " << eepromPath << std::endl; 1332 return errno; 1333 } 1334 1335 // Open the file 1336 fd = open(eepromPath.c_str(), O_WRONLY); 1337 if (fd == -1) 1338 { 1339 std::cerr << "Unable to open: " << eepromPath << std::endl; 1340 return errno; 1341 } 1342 1343 // seek to the offset 1344 lseek(fd, offset, SEEK_SET); 1345 1346 // Write bytes to location 1347 len = write(fd, guid, GUID_SIZE); 1348 if (len != GUID_SIZE) 1349 { 1350 phosphor::logging::log<phosphor::logging::level::ERR>( 1351 "GUID write data to EEPROM failed"); 1352 ret = errno; 1353 } 1354 1355 close(fd); 1356 return ret; 1357 } 1358 1359 //---------------------------------------------------------------------- 1360 // Set System GUID (CMD_OEM_SET_SYSTEM_GUID) 1361 //---------------------------------------------------------------------- 1362 #if BIC_ENABLED 1363 ipmi::RspType<> ipmiOemSetSystemGuid(ipmi::Context::ptr ctx, 1364 std::vector<uint8_t> reqData) 1365 { 1366 std::vector<uint8_t> respData; 1367 1368 if (reqData.size() != GUID_SIZE) // 16bytes 1369 { 1370 return ipmi::responseReqDataLenInvalid(); 1371 } 1372 1373 uint8_t bicAddr = (uint8_t)ctx->hostIdx << 2; 1374 1375 if (sendBicCmd(ctx->netFn, ctx->cmd, bicAddr, reqData, respData)) 1376 return ipmi::responseUnspecifiedError(); 1377 1378 return ipmi::responseSuccess(); 1379 } 1380 1381 #else 1382 ipmi_ret_t ipmiOemSetSystemGuid(ipmi_netfn_t, ipmi_cmd_t, 1383 ipmi_request_t request, ipmi_response_t, 1384 ipmi_data_len_t data_len, ipmi_context_t) 1385 { 1386 uint8_t* req = reinterpret_cast<uint8_t*>(request); 1387 1388 if (*data_len != GUID_SIZE) // 16bytes 1389 { 1390 *data_len = 0; 1391 return ipmi::ccReqDataLenInvalid; 1392 } 1393 1394 *data_len = 0; 1395 1396 if (setGUID(OFFSET_SYS_GUID, req)) 1397 { 1398 return ipmi::ccUnspecifiedError; 1399 } 1400 return ipmi::ccSuccess; 1401 } 1402 #endif 1403 1404 //---------------------------------------------------------------------- 1405 // Set Bios Flash Info (CMD_OEM_SET_BIOS_FLASH_INFO) 1406 //---------------------------------------------------------------------- 1407 ipmi_ret_t ipmiOemSetBiosFlashInfo(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t, 1408 ipmi_response_t, ipmi_data_len_t data_len, 1409 ipmi_context_t) 1410 { 1411 /* Do nothing, return success */ 1412 *data_len = 0; 1413 return ipmi::ccSuccess; 1414 } 1415 1416 //---------------------------------------------------------------------- 1417 // Set PPR (CMD_OEM_SET_PPR) 1418 //---------------------------------------------------------------------- 1419 ipmi_ret_t ipmiOemSetPpr(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, 1420 ipmi_response_t, ipmi_data_len_t data_len, 1421 ipmi_context_t) 1422 { 1423 uint8_t* req = reinterpret_cast<uint8_t*>(request); 1424 uint8_t pprCnt, pprAct, pprIndex; 1425 uint8_t selParam = req[0]; 1426 uint8_t len = *data_len; 1427 std::stringstream ss; 1428 std::string str; 1429 1430 *data_len = 0; 1431 1432 switch (selParam) 1433 { 1434 case PPR_ACTION: 1435 if (oemData[KEY_PPR].find(KEY_PPR_ROW_COUNT) == 1436 oemData[KEY_PPR].end()) 1437 return CC_PARAM_NOT_SUPP_IN_CURR_STATE; 1438 1439 pprCnt = oemData[KEY_PPR][KEY_PPR_ROW_COUNT]; 1440 if (pprCnt == 0) 1441 return CC_PARAM_NOT_SUPP_IN_CURR_STATE; 1442 1443 pprAct = req[1]; 1444 /* Check if ppr is enabled or disabled */ 1445 if (!(pprAct & 0x80)) 1446 pprAct = 0; 1447 1448 oemData[KEY_PPR][KEY_PPR_ACTION] = pprAct; 1449 break; 1450 case PPR_ROW_COUNT: 1451 if (req[1] > 100) 1452 return ipmi::ccParmOutOfRange; 1453 1454 oemData[KEY_PPR][KEY_PPR_ROW_COUNT] = req[1]; 1455 break; 1456 case PPR_ROW_ADDR: 1457 pprIndex = req[1]; 1458 if (pprIndex > 100) 1459 return ipmi::ccParmOutOfRange; 1460 1461 if (len < PPR_ROW_ADDR_LEN + 1) 1462 { 1463 phosphor::logging::log<phosphor::logging::level::ERR>( 1464 "Invalid PPR Row Address length received"); 1465 return ipmi::ccReqDataLenInvalid; 1466 } 1467 1468 ss << std::hex; 1469 ss << std::setw(2) << std::setfill('0') << (int)pprIndex; 1470 1471 oemData[KEY_PPR][ss.str()][KEY_PPR_INDEX] = pprIndex; 1472 1473 str = bytesToStr(&req[1], PPR_ROW_ADDR_LEN); 1474 oemData[KEY_PPR][ss.str()][KEY_PPR_ROW_ADDR] = str.c_str(); 1475 break; 1476 case PPR_HISTORY_DATA: 1477 pprIndex = req[1]; 1478 if (pprIndex > 100) 1479 return ipmi::ccParmOutOfRange; 1480 1481 if (len < PPR_HST_DATA_LEN + 1) 1482 { 1483 phosphor::logging::log<phosphor::logging::level::ERR>( 1484 "Invalid PPR history data length received"); 1485 return ipmi::ccReqDataLenInvalid; 1486 } 1487 1488 ss << std::hex; 1489 ss << std::setw(2) << std::setfill('0') << (int)pprIndex; 1490 1491 oemData[KEY_PPR][ss.str()][KEY_PPR_INDEX] = pprIndex; 1492 1493 str = bytesToStr(&req[1], PPR_HST_DATA_LEN); 1494 oemData[KEY_PPR][ss.str()][KEY_PPR_HST_DATA] = str.c_str(); 1495 break; 1496 default: 1497 return ipmi::ccParmOutOfRange; 1498 break; 1499 } 1500 1501 flushOemData(); 1502 1503 return ipmi::ccSuccess; 1504 } 1505 1506 //---------------------------------------------------------------------- 1507 // Get PPR (CMD_OEM_GET_PPR) 1508 //---------------------------------------------------------------------- 1509 ipmi_ret_t ipmiOemGetPpr(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, 1510 ipmi_response_t response, ipmi_data_len_t data_len, 1511 ipmi_context_t) 1512 { 1513 uint8_t* req = reinterpret_cast<uint8_t*>(request); 1514 uint8_t* res = reinterpret_cast<uint8_t*>(response); 1515 uint8_t pprCnt, pprIndex; 1516 uint8_t selParam = req[0]; 1517 std::stringstream ss; 1518 std::string str; 1519 1520 /* Any failure will return zero length data */ 1521 *data_len = 0; 1522 1523 switch (selParam) 1524 { 1525 case PPR_ACTION: 1526 res[0] = 0; 1527 *data_len = 1; 1528 1529 if (oemData[KEY_PPR].find(KEY_PPR_ROW_COUNT) != 1530 oemData[KEY_PPR].end()) 1531 { 1532 pprCnt = oemData[KEY_PPR][KEY_PPR_ROW_COUNT]; 1533 if (pprCnt != 0) 1534 { 1535 if (oemData[KEY_PPR].find(KEY_PPR_ACTION) != 1536 oemData[KEY_PPR].end()) 1537 { 1538 res[0] = oemData[KEY_PPR][KEY_PPR_ACTION]; 1539 } 1540 } 1541 } 1542 break; 1543 case PPR_ROW_COUNT: 1544 res[0] = 0; 1545 *data_len = 1; 1546 if (oemData[KEY_PPR].find(KEY_PPR_ROW_COUNT) != 1547 oemData[KEY_PPR].end()) 1548 res[0] = oemData[KEY_PPR][KEY_PPR_ROW_COUNT]; 1549 break; 1550 case PPR_ROW_ADDR: 1551 pprIndex = req[1]; 1552 if (pprIndex > 100) 1553 return ipmi::ccParmOutOfRange; 1554 1555 ss << std::hex; 1556 ss << std::setw(2) << std::setfill('0') << (int)pprIndex; 1557 1558 if (oemData[KEY_PPR].find(ss.str()) == oemData[KEY_PPR].end()) 1559 return ipmi::ccParmOutOfRange; 1560 1561 if (oemData[KEY_PPR][ss.str()].find(KEY_PPR_ROW_ADDR) == 1562 oemData[KEY_PPR][ss.str()].end()) 1563 return ipmi::ccParmOutOfRange; 1564 1565 str = oemData[KEY_PPR][ss.str()][KEY_PPR_ROW_ADDR]; 1566 *data_len = strToBytes(str, res); 1567 break; 1568 case PPR_HISTORY_DATA: 1569 pprIndex = req[1]; 1570 if (pprIndex > 100) 1571 return ipmi::ccParmOutOfRange; 1572 1573 ss << std::hex; 1574 ss << std::setw(2) << std::setfill('0') << (int)pprIndex; 1575 1576 if (oemData[KEY_PPR].find(ss.str()) == oemData[KEY_PPR].end()) 1577 return ipmi::ccParmOutOfRange; 1578 1579 if (oemData[KEY_PPR][ss.str()].find(KEY_PPR_HST_DATA) == 1580 oemData[KEY_PPR][ss.str()].end()) 1581 return ipmi::ccParmOutOfRange; 1582 1583 str = oemData[KEY_PPR][ss.str()][KEY_PPR_HST_DATA]; 1584 *data_len = strToBytes(str, res); 1585 break; 1586 default: 1587 return ipmi::ccParmOutOfRange; 1588 break; 1589 } 1590 1591 return ipmi::ccSuccess; 1592 } 1593 1594 /* FB OEM QC Commands */ 1595 1596 //---------------------------------------------------------------------- 1597 // Set Proc Info (CMD_OEM_Q_SET_PROC_INFO) 1598 //---------------------------------------------------------------------- 1599 //"Request: 1600 // Byte 1:3 – Manufacturer ID – XXYYZZ h, LSB first 1601 // Byte 4 – Processor Index, 0 base 1602 // Byte 5 – Parameter Selector 1603 // Byte 6..N – Configuration parameter data (see below for Parameters 1604 // of Processor Information) 1605 // Response: 1606 // Byte 1 – Completion code 1607 // 1608 // Parameter#1: (Processor Product Name) 1609 // 1610 // Byte 1..48 –Product name(ASCII code) 1611 // Ex. Intel(R) Xeon(R) CPU E5-2685 v3 @ 2.60GHz 1612 // 1613 // Param#2: Processor Basic Information 1614 // Byte 1 – Core Number 1615 // Byte 2 – Thread Number (LSB) 1616 // Byte 3 – Thread Number (MSB) 1617 // Byte 4 – Processor frequency in MHz (LSB) 1618 // Byte 5 – Processor frequency in MHz (MSB) 1619 // Byte 6..7 – Revision 1620 // 1621 1622 ipmi::RspType<> ipmiOemQSetProcInfo( 1623 ipmi::Context::ptr ctx, uint8_t, uint8_t, uint8_t, uint8_t procIndex, 1624 uint8_t paramSel, std::vector<uint8_t> request) 1625 { 1626 uint8_t numParam = sizeof(cpuInfoKey) / sizeof(uint8_t*); 1627 std::stringstream ss; 1628 std::string str; 1629 uint8_t len = request.size(); 1630 auto hostId = findHost(ctx->hostIdx); 1631 if (!hostId) 1632 { 1633 phosphor::logging::log<phosphor::logging::level::ERR>( 1634 "Invalid Host Id received"); 1635 return ipmi::responseInvalidCommand(); 1636 } 1637 std::string procInfo = KEY_Q_PROC_INFO + std::to_string(*hostId); 1638 /* check for requested data params */ 1639 if (len < 5 || paramSel < 1 || paramSel >= numParam) 1640 { 1641 phosphor::logging::log<phosphor::logging::level::ERR>( 1642 "Invalid parameter received"); 1643 return ipmi::responseParmOutOfRange(); 1644 } 1645 ss << std::hex; 1646 ss << std::setw(2) << std::setfill('0') << (int)procIndex; 1647 oemData[procInfo][ss.str()][KEY_PROC_INDEX] = procIndex; 1648 str = bytesToStr(request.data(), len); 1649 oemData[procInfo][ss.str()][cpuInfoKey[paramSel]] = str.c_str(); 1650 flushOemData(); 1651 return ipmi::responseSuccess(); 1652 } 1653 1654 //---------------------------------------------------------------------- 1655 // Get Proc Info (CMD_OEM_Q_GET_PROC_INFO) 1656 //---------------------------------------------------------------------- 1657 // Request: 1658 // Byte 1:3 – Manufacturer ID – XXYYZZ h, LSB first 1659 // Byte 4 – Processor Index, 0 base 1660 // Byte 5 – Parameter Selector 1661 // Response: 1662 // Byte 1 – Completion code 1663 // Byte 2..N – Configuration Parameter Data (see below for Parameters 1664 // of Processor Information) 1665 // 1666 // Parameter#1: (Processor Product Name) 1667 // 1668 // Byte 1..48 –Product name(ASCII code) 1669 // Ex. Intel(R) Xeon(R) CPU E5-2685 v3 @ 2.60GHz 1670 // 1671 // Param#2: Processor Basic Information 1672 // Byte 1 – Core Number 1673 // Byte 2 – Thread Number (LSB) 1674 // Byte 3 – Thread Number (MSB) 1675 // Byte 4 – Processor frequency in MHz (LSB) 1676 // Byte 5 – Processor frequency in MHz (MSB) 1677 // Byte 6..7 – Revision 1678 // 1679 1680 ipmi::RspType<std::vector<uint8_t>> ipmiOemQGetProcInfo( 1681 ipmi::Context::ptr ctx, uint8_t, uint8_t, uint8_t, uint8_t procIndex, 1682 uint8_t paramSel) 1683 { 1684 uint8_t numParam = sizeof(cpuInfoKey) / sizeof(uint8_t*); 1685 std::stringstream ss; 1686 std::string str; 1687 uint8_t res[MAX_BUF]; 1688 auto hostId = findHost(ctx->hostIdx); 1689 if (!hostId) 1690 { 1691 phosphor::logging::log<phosphor::logging::level::ERR>( 1692 "Invalid Host Id received"); 1693 return ipmi::responseInvalidCommand(); 1694 } 1695 std::string procInfo = KEY_Q_PROC_INFO + std::to_string(*hostId); 1696 if (paramSel < 1 || paramSel >= numParam) 1697 { 1698 phosphor::logging::log<phosphor::logging::level::ERR>( 1699 "Invalid parameter received"); 1700 return ipmi::responseParmOutOfRange(); 1701 } 1702 ss << std::hex; 1703 ss << std::setw(2) << std::setfill('0') << (int)procIndex; 1704 if (oemData[procInfo].find(ss.str()) == oemData[procInfo].end()) 1705 return ipmi::responseCommandNotAvailable(); 1706 if (oemData[procInfo][ss.str()].find(cpuInfoKey[paramSel]) == 1707 oemData[procInfo][ss.str()].end()) 1708 return ipmi::responseCommandNotAvailable(); 1709 str = oemData[procInfo][ss.str()][cpuInfoKey[paramSel]]; 1710 int dataLen = strToBytes(str, res); 1711 std::vector<uint8_t> response(&res[0], &res[dataLen]); 1712 return ipmi::responseSuccess(response); 1713 } 1714 1715 //---------------------------------------------------------------------- 1716 // Set Dimm Info (CMD_OEM_Q_SET_DIMM_INFO) 1717 //---------------------------------------------------------------------- 1718 // Request: 1719 // Byte 1:3 – Manufacturer ID – XXYYZZh, LSB first 1720 // Byte 4 – DIMM Index, 0 base 1721 // Byte 5 – Parameter Selector 1722 // Byte 6..N – Configuration parameter data (see below for Parameters 1723 // of DIMM Information) 1724 // Response: 1725 // Byte 1 – Completion code 1726 // 1727 // Param#1 (DIMM Location): 1728 // Byte 1 – DIMM Present 1729 // Byte 1 – DIMM Present 1730 // 01h – Present 1731 // FFh – Not Present 1732 // Byte 2 – Node Number, 0 base 1733 // Byte 3 – Channel Number , 0 base 1734 // Byte 4 – DIMM Number , 0 base 1735 // 1736 // Param#2 (DIMM Type): 1737 // Byte 1 – DIMM Type 1738 // Bit [7:6] 1739 // For DDR3 1740 // 00 – Normal Voltage (1.5V) 1741 // 01 – Ultra Low Voltage (1.25V) 1742 // 10 – Low Voltage (1.35V) 1743 // 11 – Reserved 1744 // For DDR4 1745 // 00 – Reserved 1746 // 01 – Reserved 1747 // 10 – Reserved 1748 // 11 – Normal Voltage (1.2V) 1749 // Bit [5:0] 1750 // 0x00 – SDRAM 1751 // 0x01 – DDR-1 RAM 1752 // 0x02 – Rambus 1753 // 0x03 – DDR-2 RAM 1754 // 0x04 – FBDIMM 1755 // 0x05 – DDR-3 RAM 1756 // 0x06 – DDR-4 RAM 1757 // 1758 // Param#3 (DIMM Speed): 1759 // Byte 1..2 – DIMM speed in MHz, LSB 1760 // Byte 3..6 – DIMM size in Mbytes, LSB 1761 // 1762 // Param#4 (Module Part Number): 1763 // Byte 1..20 –Module Part Number (JEDEC Standard No. 21-C) 1764 // 1765 // Param#5 (Module Serial Number): 1766 // Byte 1..4 –Module Serial Number (JEDEC Standard No. 21-C) 1767 // 1768 // Param#6 (Module Manufacturer ID): 1769 // Byte 1 - Module Manufacturer ID, LSB 1770 // Byte 2 - Module Manufacturer ID, MSB 1771 // 1772 ipmi::RspType<> ipmiOemQSetDimmInfo( 1773 ipmi::Context::ptr ctx, uint8_t, uint8_t, uint8_t, uint8_t dimmIndex, 1774 uint8_t paramSel, std::vector<uint8_t> request) 1775 { 1776 uint8_t numParam = sizeof(dimmInfoKey) / sizeof(uint8_t*); 1777 std::stringstream ss; 1778 std::string str; 1779 uint8_t len = request.size(); 1780 std::string dimmType; 1781 readDimmType(dimmType, dimmIndex); 1782 auto hostId = findHost(ctx->hostIdx); 1783 if (!hostId) 1784 { 1785 phosphor::logging::log<phosphor::logging::level::ERR>( 1786 "Invalid Host Id received"); 1787 return ipmi::responseInvalidCommand(); 1788 } 1789 1790 std::string dimmInfo = KEY_Q_DIMM_INFO + std::to_string(*hostId); 1791 1792 if (len < 3 || paramSel < 1 || paramSel >= numParam) 1793 { 1794 phosphor::logging::log<phosphor::logging::level::ERR>( 1795 "Invalid parameter received"); 1796 return ipmi::responseParmOutOfRange(); 1797 } 1798 1799 ss << std::hex; 1800 ss << (int)dimmIndex; 1801 oemData[dimmInfo][ss.str()][KEY_DIMM_INDEX] = dimmIndex; 1802 oemData[dimmInfo][ss.str()][KEY_DIMM_TYPE] = dimmType; 1803 str = bytesToStr(request.data(), len); 1804 oemData[dimmInfo][ss.str()][dimmInfoKey[paramSel]] = str.c_str(); 1805 flushOemData(); 1806 return ipmi::responseSuccess(); 1807 } 1808 1809 // Get Dimm Info (CMD_OEM_Q_GET_DIMM_INFO) 1810 //---------------------------------------------------------------------- 1811 // Request: 1812 // Byte 1:3 – Manufacturer ID – XXYYZZh, LSB first 1813 // Byte 4 – DIMM Index, 0 base 1814 // Byte 5 – Parameter Selector 1815 // Byte 6..N – Configuration parameter data (see below for Parameters 1816 // of DIMM Information) 1817 // Response: 1818 // Byte 1 – Completion code 1819 // Byte 2..N – Configuration Parameter Data (see Table_1213h Parameters 1820 // of DIMM Information) 1821 // 1822 // Param#1 (DIMM Location): 1823 // Byte 1 – DIMM Present 1824 // Byte 1 – DIMM Present 1825 // 01h – Present 1826 // FFh – Not Present 1827 // Byte 2 – Node Number, 0 base 1828 // Byte 3 – Channel Number , 0 base 1829 // Byte 4 – DIMM Number , 0 base 1830 // 1831 // Param#2 (DIMM Type): 1832 // Byte 1 – DIMM Type 1833 // Bit [7:6] 1834 // For DDR3 1835 // 00 – Normal Voltage (1.5V) 1836 // 01 – Ultra Low Voltage (1.25V) 1837 // 10 – Low Voltage (1.35V) 1838 // 11 – Reserved 1839 // For DDR4 1840 // 00 – Reserved 1841 // 01 – Reserved 1842 // 10 – Reserved 1843 // 11 – Normal Voltage (1.2V) 1844 // Bit [5:0] 1845 // 0x00 – SDRAM 1846 // 0x01 – DDR-1 RAM 1847 // 0x02 – Rambus 1848 // 0x03 – DDR-2 RAM 1849 // 0x04 – FBDIMM 1850 // 0x05 – DDR-3 RAM 1851 // 0x06 – DDR-4 RAM 1852 // 1853 // Param#3 (DIMM Speed): 1854 // Byte 1..2 – DIMM speed in MHz, LSB 1855 // Byte 3..6 – DIMM size in Mbytes, LSB 1856 // 1857 // Param#4 (Module Part Number): 1858 // Byte 1..20 –Module Part Number (JEDEC Standard No. 21-C) 1859 // 1860 // Param#5 (Module Serial Number): 1861 // Byte 1..4 –Module Serial Number (JEDEC Standard No. 21-C) 1862 // 1863 // Param#6 (Module Manufacturer ID): 1864 // Byte 1 - Module Manufacturer ID, LSB 1865 // Byte 2 - Module Manufacturer ID, MSB 1866 // 1867 ipmi::RspType<std::vector<uint8_t>> ipmiOemQGetDimmInfo( 1868 ipmi::Context::ptr ctx, uint8_t, uint8_t, uint8_t, uint8_t dimmIndex, 1869 uint8_t paramSel) 1870 { 1871 uint8_t numParam = sizeof(dimmInfoKey) / sizeof(uint8_t*); 1872 uint8_t res[MAX_BUF]; 1873 std::stringstream ss; 1874 std::string str; 1875 std::string dimmType; 1876 readDimmType(dimmType, dimmIndex); 1877 auto hostId = findHost(ctx->hostIdx); 1878 if (!hostId) 1879 { 1880 phosphor::logging::log<phosphor::logging::level::ERR>( 1881 "Invalid Host Id received"); 1882 return ipmi::responseInvalidCommand(); 1883 } 1884 std::string dimmInfo = KEY_Q_DIMM_INFO + std::to_string(*hostId); 1885 1886 if (paramSel < 1 || paramSel >= numParam) 1887 { 1888 phosphor::logging::log<phosphor::logging::level::ERR>( 1889 "Invalid parameter received"); 1890 return ipmi::responseParmOutOfRange(); 1891 } 1892 ss << std::hex; 1893 ss << (int)dimmIndex; 1894 oemData[dimmInfo][ss.str()][KEY_DIMM_TYPE] = dimmType; 1895 if (oemData[dimmInfo].find(ss.str()) == oemData[dimmInfo].end()) 1896 return ipmi::responseCommandNotAvailable(); 1897 if (oemData[dimmInfo][ss.str()].find(dimmInfoKey[paramSel]) == 1898 oemData[dimmInfo][ss.str()].end()) 1899 return ipmi::responseCommandNotAvailable(); 1900 str = oemData[dimmInfo][ss.str()][dimmInfoKey[paramSel]]; 1901 int data_length = strToBytes(str, res); 1902 std::vector<uint8_t> response(&res[0], &res[data_length]); 1903 return ipmi::responseSuccess(response); 1904 } 1905 1906 //---------------------------------------------------------------------- 1907 // Set Drive Info (CMD_OEM_Q_SET_DRIVE_INFO) 1908 //---------------------------------------------------------------------- 1909 // BIOS issue this command to provide HDD information to BMC. 1910 // 1911 // BIOS just can get information by standard ATA / SMART command for 1912 // OB SATA controller. 1913 // BIOS can get 1914 // 1. Serial Number 1915 // 2. Model Name 1916 // 3. HDD FW Version 1917 // 4. HDD Capacity 1918 // 5. HDD WWN 1919 // 1920 // Use Get HDD info Param #5 to know the MAX HDD info index. 1921 // 1922 // Request: 1923 // Byte 1:3 – Quanta Manufacturer ID – 001C4Ch, LSB first 1924 // Byte 4 – 1925 // [7:4] Reserved 1926 // [3:0] HDD Controller Type 1927 // 0x00 – BIOS 1928 // 0x01 – Expander 1929 // 0x02 – LSI 1930 // Byte 5 – HDD Info Index, 0 base 1931 // Byte 6 – Parameter Selector 1932 // Byte 7..N – Configuration parameter data (see Table_1415h Parameters of HDD 1933 // Information) 1934 // 1935 // Response: 1936 // Byte 1 – Completion Code 1937 // 1938 // Param#0 (HDD Location): 1939 // Byte 1 – Controller 1940 // [7:3] Device Number 1941 // [2:0] Function Number 1942 // For Intel C610 series (Wellsburg) 1943 // D31:F2 (0xFA) – SATA control 1 1944 // D31:F5 (0xFD) – SATA control 2 1945 // D17:F4 (0x8C) – sSata control 1946 // Byte 2 – Port Number 1947 // Byte 3 – Location (0xFF: No HDD Present) 1948 // BIOS default set Byte 3 to 0xFF, if No HDD Present. And then skip send param 1949 // #1~4, #6, #7 to BMC (still send param #5) BIOS default set Byte 3 to 0, if 1950 // the HDD present. BMC or other people who know the HDD location has 1951 // responsibility for update Location info 1952 // 1953 // Param#1 (Serial Number): 1954 // Bytes 1..33: HDD Serial Number 1955 // 1956 // Param#2 (Model Name): 1957 // Byte 1..33 – HDD Model Name 1958 // 1959 // Param#3 (HDD FW Version): 1960 // Byte 1..17 –HDD FW version 1961 // 1962 // Param#4 (Capacity): 1963 // Byte 1..4 –HDD Block Size, LSB 1964 // Byte 5..12 - HDD Block Number, LSB 1965 // HDD Capacity = HDD Block size * HDD BLock number (Unit Byte) 1966 // 1967 // Param#5 (Max HDD Quantity): 1968 // Byte 1 - Max HDD Quantity 1969 // Max supported port numbers in this PCH 1970 // 1971 // Param#6 (HDD Type) 1972 // Byte 1 – HDD Type 1973 // 0h – Reserved 1974 // 1h – SAS 1975 // 2h – SATA 1976 // 3h – PCIE SSD (NVME) 1977 // 1978 // Param#7 (HDD WWN) 1979 // Data 1...8: HDD World Wide Name, LSB 1980 // 1981 ipmi_ret_t ipmiOemQSetDriveInfo(ipmi_netfn_t, ipmi_cmd_t, 1982 ipmi_request_t request, ipmi_response_t, 1983 ipmi_data_len_t data_len, ipmi_context_t) 1984 { 1985 qDriveInfo_t* req = reinterpret_cast<qDriveInfo_t*>(request); 1986 uint8_t numParam = sizeof(driveInfoKey) / sizeof(uint8_t*); 1987 uint8_t ctrlType = req->hddCtrlType & 0x0f; 1988 std::stringstream ss; 1989 std::string str; 1990 uint8_t len = *data_len; 1991 1992 *data_len = 0; 1993 1994 /* check for requested data params */ 1995 if (len < 6 || req->paramSel >= numParam || ctrlType > 2) 1996 { 1997 phosphor::logging::log<phosphor::logging::level::ERR>( 1998 "Invalid parameter received"); 1999 return ipmi::ccParmOutOfRange; 2000 } 2001 2002 len = len - 6; // Get Actual data length 2003 2004 ss << std::hex; 2005 ss << std::setw(2) << std::setfill('0') << (int)req->hddIndex; 2006 oemData[KEY_Q_DRIVE_INFO][KEY_HDD_CTRL_TYPE] = req->hddCtrlType; 2007 oemData[KEY_Q_DRIVE_INFO][ctrlTypeKey[ctrlType]][ss.str()][KEY_HDD_INDEX] = 2008 req->hddIndex; 2009 2010 str = bytesToStr(req->data, len); 2011 oemData[KEY_Q_DRIVE_INFO][ctrlTypeKey[ctrlType]][ss.str()] 2012 [driveInfoKey[req->paramSel]] = str.c_str(); 2013 flushOemData(); 2014 2015 return ipmi::ccSuccess; 2016 } 2017 2018 //---------------------------------------------------------------------- 2019 // Get Drive Info (CMD_OEM_Q_GET_DRIVE_INFO) 2020 //---------------------------------------------------------------------- 2021 // BMC needs to check HDD presented or not first. If NOT presented, return 2022 // completion code 0xD5. 2023 // 2024 // Request: 2025 // Byte 1:3 – Quanta Manufacturer ID – 001C4Ch, LSB first 2026 // Byte 4 – 2027 //[7:4] Reserved 2028 //[3:0] HDD Controller Type 2029 // 0x00 – BIOS 2030 // 0x01 – Expander 2031 // 0x02 – LSI 2032 // Byte 5 – HDD Index, 0 base 2033 // Byte 6 – Parameter Selector (See Above Set HDD Information) 2034 // Response: 2035 // Byte 1 – Completion Code 2036 // 0xD5 – Not support in current status (HDD Not Present) 2037 // Byte 2..N – Configuration parameter data (see Table_1415h Parameters of HDD 2038 // Information) 2039 // 2040 ipmi_ret_t ipmiOemQGetDriveInfo( 2041 ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, ipmi_response_t response, 2042 ipmi_data_len_t data_len, ipmi_context_t) 2043 { 2044 qDriveInfo_t* req = reinterpret_cast<qDriveInfo_t*>(request); 2045 uint8_t numParam = sizeof(driveInfoKey) / sizeof(uint8_t*); 2046 uint8_t* res = reinterpret_cast<uint8_t*>(response); 2047 uint8_t ctrlType = req->hddCtrlType & 0x0f; 2048 std::stringstream ss; 2049 std::string str; 2050 2051 *data_len = 0; 2052 2053 /* check for requested data params */ 2054 if (req->paramSel >= numParam || ctrlType > 2) 2055 { 2056 phosphor::logging::log<phosphor::logging::level::ERR>( 2057 "Invalid parameter received"); 2058 return ipmi::ccParmOutOfRange; 2059 } 2060 2061 if (oemData[KEY_Q_DRIVE_INFO].find(ctrlTypeKey[ctrlType]) == 2062 oemData[KEY_Q_DRIVE_INFO].end()) 2063 return CC_PARAM_NOT_SUPP_IN_CURR_STATE; 2064 2065 ss << std::hex; 2066 ss << std::setw(2) << std::setfill('0') << (int)req->hddIndex; 2067 2068 if (oemData[KEY_Q_DRIVE_INFO][ctrlTypeKey[ctrlType]].find(ss.str()) == 2069 oemData[KEY_Q_DRIVE_INFO][ctrlTypeKey[ctrlType]].end()) 2070 return CC_PARAM_NOT_SUPP_IN_CURR_STATE; 2071 2072 if (oemData[KEY_Q_DRIVE_INFO][ctrlTypeKey[ctrlType]][ss.str()].find( 2073 driveInfoKey[req->paramSel]) == 2074 oemData[KEY_Q_DRIVE_INFO][ctrlTypeKey[ctrlType]][ss.str()].end()) 2075 return CC_PARAM_NOT_SUPP_IN_CURR_STATE; 2076 2077 str = oemData[KEY_Q_DRIVE_INFO][ctrlTypeKey[ctrlType]][ss.str()] 2078 [driveInfoKey[req->paramSel]]; 2079 *data_len = strToBytes(str, res); 2080 2081 return ipmi::ccSuccess; 2082 } 2083 2084 /* Helper function for sending DCMI commands to ME/BIC and 2085 * getting response back 2086 */ 2087 ipmi::RspType<std::vector<uint8_t>> sendDCMICmd( 2088 [[maybe_unused]] ipmi::Context::ptr ctx, [[maybe_unused]] uint8_t cmd, 2089 std::vector<uint8_t>& cmdData) 2090 { 2091 std::vector<uint8_t> respData; 2092 2093 #if BIC_ENABLED 2094 2095 uint8_t bicAddr = (uint8_t)ctx->hostIdx << 2; 2096 2097 if (sendBicCmd(ctx->netFn, ctx->cmd, bicAddr, cmdData, respData)) 2098 { 2099 return ipmi::responseUnspecifiedError(); 2100 } 2101 2102 #else 2103 2104 /* Add group id as first byte to request for ME command */ 2105 cmdData.insert(cmdData.begin(), groupDCMI); 2106 2107 if (sendMeCmd(ipmi::netFnGroup, cmd, cmdData, respData)) 2108 { 2109 return ipmi::responseUnspecifiedError(); 2110 } 2111 2112 /* Remove group id as first byte as it will be added by IPMID */ 2113 respData.erase(respData.begin()); 2114 2115 #endif 2116 2117 return ipmi::responseSuccess(std::move(respData)); 2118 } 2119 2120 /* DCMI Command handellers. */ 2121 2122 ipmi::RspType<std::vector<uint8_t>> ipmiOemDCMIGetPowerReading( 2123 ipmi::Context::ptr ctx, std::vector<uint8_t> reqData) 2124 { 2125 return sendDCMICmd(ctx, ipmi::dcmi::cmdGetPowerReading, reqData); 2126 } 2127 2128 ipmi::RspType<std::vector<uint8_t>> ipmiOemDCMIGetPowerLimit( 2129 ipmi::Context::ptr ctx, std::vector<uint8_t> reqData) 2130 { 2131 return sendDCMICmd(ctx, ipmi::dcmi::cmdGetPowerLimit, reqData); 2132 } 2133 2134 ipmi::RspType<std::vector<uint8_t>> ipmiOemDCMISetPowerLimit( 2135 ipmi::Context::ptr ctx, std::vector<uint8_t> reqData) 2136 { 2137 return sendDCMICmd(ctx, ipmi::dcmi::cmdSetPowerLimit, reqData); 2138 } 2139 2140 ipmi::RspType<std::vector<uint8_t>> ipmiOemDCMIApplyPowerLimit( 2141 ipmi::Context::ptr ctx, std::vector<uint8_t> reqData) 2142 { 2143 return sendDCMICmd(ctx, ipmi::dcmi::cmdActDeactivatePwrLimit, reqData); 2144 } 2145 2146 // Https Boot related functions 2147 ipmi::RspType<std::vector<uint8_t>> ipmiOemGetHttpsData( 2148 [[maybe_unused]] ipmi::Context::ptr ctx, std::vector<uint8_t> reqData) 2149 { 2150 if (reqData.size() < sizeof(HttpsDataReq)) 2151 return ipmi::responseReqDataLenInvalid(); 2152 2153 const auto* pReq = reinterpret_cast<const HttpsDataReq*>(reqData.data()); 2154 std::error_code ec; 2155 auto fileSize = std::filesystem::file_size(certPath, ec); 2156 if (ec) 2157 return ipmi::responseUnspecifiedError(); 2158 2159 if (pReq->offset >= fileSize) 2160 return ipmi::responseInvalidFieldRequest(); 2161 2162 std::ifstream file(certPath, std::ios::binary); 2163 if (!file) 2164 return ipmi::responseUnspecifiedError(); 2165 2166 auto readLen = std::min<uint16_t>(pReq->length, fileSize - pReq->offset); 2167 std::vector<uint8_t> resData(readLen + 1); 2168 resData[0] = readLen; 2169 file.seekg(pReq->offset); 2170 file.read(reinterpret_cast<char*>(resData.data() + 1), readLen); 2171 2172 return ipmi::responseSuccess(resData); 2173 } 2174 2175 ipmi::RspType<std::vector<uint8_t>> ipmiOemGetHttpsAttr( 2176 [[maybe_unused]] ipmi::Context::ptr ctx, std::vector<uint8_t> reqData) 2177 { 2178 if (reqData.size() < sizeof(HttpsBootAttr)) 2179 return ipmi::responseReqDataLenInvalid(); 2180 2181 std::vector<uint8_t> resData; 2182 2183 switch (static_cast<HttpsBootAttr>(reqData[0])) 2184 { 2185 case HttpsBootAttr::certSize: 2186 { 2187 std::error_code ec; 2188 auto fileSize = std::filesystem::file_size(certPath, ec); 2189 if (ec || fileSize > std::numeric_limits<uint16_t>::max()) 2190 return ipmi::responseUnspecifiedError(); 2191 2192 uint16_t size = static_cast<uint16_t>(fileSize); 2193 resData.resize(sizeof(uint16_t)); 2194 std::memcpy(resData.data(), &size, sizeof(uint16_t)); 2195 break; 2196 } 2197 case HttpsBootAttr::certCrc: 2198 { 2199 std::ifstream file(certPath, std::ios::binary); 2200 if (!file) 2201 return ipmi::responseUnspecifiedError(); 2202 2203 boost::crc_32_type result; 2204 char data[1024]; 2205 while (file.read(data, sizeof(data))) 2206 result.process_bytes(data, file.gcount()); 2207 if (file.gcount() > 0) 2208 result.process_bytes(data, file.gcount()); 2209 2210 uint32_t crc = result.checksum(); 2211 resData.resize(sizeof(uint32_t)); 2212 std::memcpy(resData.data(), &crc, sizeof(uint32_t)); 2213 break; 2214 } 2215 default: 2216 return ipmi::responseInvalidFieldRequest(); 2217 } 2218 2219 return ipmi::responseSuccess(resData); 2220 } 2221 2222 // OEM Crashdump related functions 2223 static ipmi_ret_t setDumpState(CrdState& currState, CrdState newState) 2224 { 2225 switch (newState) 2226 { 2227 case CrdState::waitData: 2228 if (currState == CrdState::packing) 2229 return CC_PARAM_NOT_SUPP_IN_CURR_STATE; 2230 break; 2231 case CrdState::packing: 2232 if (currState != CrdState::waitData) 2233 return CC_PARAM_NOT_SUPP_IN_CURR_STATE; 2234 break; 2235 case CrdState::free: 2236 break; 2237 default: 2238 return ipmi::ccUnspecifiedError; 2239 } 2240 currState = newState; 2241 2242 return ipmi::ccSuccess; 2243 } 2244 2245 static ipmi_ret_t handleMcaBank(const CrashDumpHdr& hdr, 2246 std::span<const uint8_t> data, 2247 CrdState& currState, std::stringstream& ss) 2248 { 2249 if (data.size() < sizeof(CrdMcaBank)) 2250 return ipmi::ccReqDataLenInvalid; 2251 2252 ipmi_ret_t res = setDumpState(currState, CrdState::waitData); 2253 if (res) 2254 return res; 2255 2256 const auto* pBank = reinterpret_cast<const CrdMcaBank*>(data.data()); 2257 ss << std::format(" Bank ID : 0x{:02X}, Core ID : 0x{:02X}\n", 2258 hdr.bankHdr.bankId, hdr.bankHdr.coreId); 2259 ss << std::format(" MCA_CTRL : 0x{:016X}\n", pBank->mcaCtrl); 2260 ss << std::format(" MCA_STATUS : 0x{:016X}\n", pBank->mcaSts); 2261 ss << std::format(" MCA_ADDR : 0x{:016X}\n", pBank->mcaAddr); 2262 ss << std::format(" MCA_MISC0 : 0x{:016X}\n", pBank->mcaMisc0); 2263 ss << std::format(" MCA_CTRL_MASK : 0x{:016X}\n", pBank->mcaCtrlMask); 2264 ss << std::format(" MCA_CONFIG : 0x{:016X}\n", pBank->mcaConfig); 2265 ss << std::format(" MCA_IPID : 0x{:016X}\n", pBank->mcaIpid); 2266 ss << std::format(" MCA_SYND : 0x{:016X}\n", pBank->mcaSynd); 2267 ss << std::format(" MCA_DESTAT : 0x{:016X}\n", pBank->mcaDestat); 2268 ss << std::format(" MCA_DEADDR : 0x{:016X}\n", pBank->mcaDeaddr); 2269 ss << std::format(" MCA_MISC1 : 0x{:016X}\n", pBank->mcaMisc1); 2270 ss << "\n"; 2271 2272 return ipmi::ccSuccess; 2273 } 2274 2275 template <typename T> 2276 static ipmi_ret_t handleVirtualBank(std::span<const uint8_t> data, 2277 CrdState& currState, std::stringstream& ss) 2278 { 2279 if (data.size() < sizeof(T)) 2280 return ipmi::ccReqDataLenInvalid; 2281 2282 const auto* pBank = reinterpret_cast<const T*>(data.data()); 2283 2284 if (data.size() < sizeof(T) + sizeof(BankCorePair) * pBank->mcaCount) 2285 return ipmi::ccReqDataLenInvalid; 2286 2287 ipmi_ret_t res = setDumpState(currState, CrdState::waitData); 2288 if (res) 2289 return res; 2290 2291 ss << " Virtual Bank\n"; 2292 ss << std::format(" S5_RESET_STATUS : 0x{:08X}\n", pBank->s5ResetSts); 2293 ss << std::format(" PM_BREAKEVENT : 0x{:08X}\n", pBank->breakevent); 2294 if constexpr (std::is_same_v<T, CrdVirtualBankV3>) 2295 { 2296 ss << std::format(" WARMCOLDRSTSTATUS : 0x{:08X}\n", pBank->rstSts); 2297 } 2298 ss << std::format(" PROCESSOR NUMBER : 0x{:04X}\n", pBank->procNum); 2299 ss << std::format(" APIC ID : 0x{:08X}\n", pBank->apicId); 2300 ss << std::format(" EAX : 0x{:08X}\n", pBank->eax); 2301 ss << std::format(" EBX : 0x{:08X}\n", pBank->ebx); 2302 ss << std::format(" ECX : 0x{:08X}\n", pBank->ecx); 2303 ss << std::format(" EDX : 0x{:08X}\n", pBank->edx); 2304 ss << " VALID LIST : "; 2305 for (size_t i = 0; i < pBank->mcaCount; i++) 2306 { 2307 ss << std::format("(0x{:02X},0x{:02X}) ", pBank->mcaList[i].bankId, 2308 pBank->mcaList[i].coreId); 2309 } 2310 ss << "\n\n"; 2311 2312 return ipmi::ccSuccess; 2313 } 2314 2315 static ipmi_ret_t handleCpuWdtBank(std::span<const uint8_t> data, 2316 CrdState& currState, std::stringstream& ss) 2317 { 2318 if (data.size() < sizeof(CrdCpuWdtBank)) 2319 return ipmi::ccReqDataLenInvalid; 2320 2321 ipmi_ret_t res = setDumpState(currState, CrdState::waitData); 2322 if (res) 2323 return res; 2324 2325 const auto* pBank = reinterpret_cast<const CrdCpuWdtBank*>(data.data()); 2326 for (size_t i = 0; i < ccmNum; i++) 2327 { 2328 ss << std::format(" [CCM{}]\n", i); 2329 ss << std::format(" HwAssertStsHi : 0x{:08X}\n", 2330 pBank->hwAssertStsHi[i]); 2331 ss << std::format(" HwAssertStsLo : 0x{:08X}\n", 2332 pBank->hwAssertStsLo[i]); 2333 ss << std::format(" OrigWdtAddrLogHi : 0x{:08X}\n", 2334 pBank->origWdtAddrLogHi[i]); 2335 ss << std::format(" OrigWdtAddrLogLo : 0x{:08X}\n", 2336 pBank->origWdtAddrLogLo[i]); 2337 ss << std::format(" HwAssertMskHi : 0x{:08X}\n", 2338 pBank->hwAssertMskHi[i]); 2339 ss << std::format(" HwAssertMskLo : 0x{:08X}\n", 2340 pBank->hwAssertMskLo[i]); 2341 ss << std::format(" OrigWdtAddrLogStat : 0x{:08X}\n", 2342 pBank->origWdtAddrLogStat[i]); 2343 } 2344 ss << "\n"; 2345 2346 return ipmi::ccSuccess; 2347 } 2348 2349 template <size_t N> 2350 static ipmi_ret_t handleHwAssertBank(const char* name, 2351 std::span<const uint8_t> data, 2352 CrdState& currState, std::stringstream& ss) 2353 { 2354 if (data.size() < sizeof(CrdHwAssertBank<N>)) 2355 return ipmi::ccReqDataLenInvalid; 2356 2357 ipmi_ret_t res = setDumpState(currState, CrdState::waitData); 2358 if (res) 2359 return res; 2360 2361 const CrdHwAssertBank<N>* pBank = 2362 reinterpret_cast<const CrdHwAssertBank<N>*>(data.data()); 2363 2364 for (size_t i = 0; i < N; i++) 2365 { 2366 ss << std::format(" [{}{}]\n", name, i); 2367 ss << std::format(" HwAssertStsHi : 0x{:08X}\n", 2368 pBank->hwAssertStsHi[i]); 2369 ss << std::format(" HwAssertStsLo : 0x{:08X}\n", 2370 pBank->hwAssertStsLo[i]); 2371 ss << std::format(" HwAssertMskHi : 0x{:08X}\n", 2372 pBank->hwAssertMskHi[i]); 2373 ss << std::format(" HwAssertMskLo : 0x{:08X}\n", 2374 pBank->hwAssertMskLo[i]); 2375 } 2376 ss << "\n"; 2377 2378 return ipmi::ccSuccess; 2379 } 2380 2381 static ipmi_ret_t handlePcieAerBank(std::span<const uint8_t> data, 2382 CrdState& currState, std::stringstream& ss) 2383 { 2384 if (data.size() < sizeof(CrdPcieAerBank)) 2385 return ipmi::ccReqDataLenInvalid; 2386 2387 ipmi_ret_t res = setDumpState(currState, CrdState::waitData); 2388 if (res) 2389 return res; 2390 2391 const auto* pBank = reinterpret_cast<const CrdPcieAerBank*>(data.data()); 2392 ss << std::format(" [Bus{} Dev{} Fun{}]\n", pBank->bus, pBank->dev, 2393 pBank->fun); 2394 ss << std::format(" Command : 0x{:04X}\n", 2395 pBank->cmd); 2396 ss << std::format(" Status : 0x{:04X}\n", 2397 pBank->sts); 2398 ss << std::format(" Slot : 0x{:04X}\n", 2399 pBank->slot); 2400 ss << std::format(" Secondary Bus : 0x{:02X}\n", 2401 pBank->secondBus); 2402 ss << std::format(" Vendor ID : 0x{:04X}\n", 2403 pBank->vendorId); 2404 ss << std::format(" Device ID : 0x{:04X}\n", 2405 pBank->devId); 2406 ss << std::format(" Class Code : 0x{:02X}{:04X}\n", 2407 pBank->classCodeHi, pBank->classCodeLo); 2408 ss << std::format(" Bridge: Secondary Status : 0x{:04X}\n", 2409 pBank->secondSts); 2410 ss << std::format(" Bridge: Control : 0x{:04X}\n", 2411 pBank->ctrl); 2412 ss << std::format(" Uncorrectable Error Status : 0x{:08X}\n", 2413 pBank->uncorrErrSts); 2414 ss << std::format(" Uncorrectable Error Mask : 0x{:08X}\n", 2415 pBank->uncorrErrMsk); 2416 ss << std::format(" Uncorrectable Error Severity : 0x{:08X}\n", 2417 pBank->uncorrErrSeverity); 2418 ss << std::format(" Correctable Error Status : 0x{:08X}\n", 2419 pBank->corrErrSts); 2420 ss << std::format(" Correctable Error Mask : 0x{:08X}\n", 2421 pBank->corrErrMsk); 2422 ss << std::format(" Header Log DW0 : 0x{:08X}\n", 2423 pBank->hdrLogDw0); 2424 ss << std::format(" Header Log DW1 : 0x{:08X}\n", 2425 pBank->hdrLogDw1); 2426 ss << std::format(" Header Log DW2 : 0x{:08X}\n", 2427 pBank->hdrLogDw2); 2428 ss << std::format(" Header Log DW3 : 0x{:08X}\n", 2429 pBank->hdrLogDw3); 2430 ss << std::format(" Root Error Status : 0x{:08X}\n", 2431 pBank->rootErrSts); 2432 ss << std::format(" Correctable Error Source ID : 0x{:04X}\n", 2433 pBank->corrErrSrcId); 2434 ss << std::format(" Error Source ID : 0x{:04X}\n", 2435 pBank->errSrcId); 2436 ss << std::format(" Lane Error Status : 0x{:08X}\n", 2437 pBank->laneErrSts); 2438 ss << "\n"; 2439 2440 return ipmi::ccSuccess; 2441 } 2442 2443 static ipmi_ret_t handleWdtRegBank(std::span<const uint8_t> data, 2444 CrdState& currState, std::stringstream& ss) 2445 { 2446 if (data.size() < sizeof(CrdWdtRegBank)) 2447 return ipmi::ccReqDataLenInvalid; 2448 2449 const auto* pBank = reinterpret_cast<const CrdWdtRegBank*>(data.data()); 2450 if (data.size() < sizeof(CrdWdtRegBank) + sizeof(uint32_t) * pBank->count) 2451 return ipmi::ccReqDataLenInvalid; 2452 2453 ipmi_ret_t res = setDumpState(currState, CrdState::waitData); 2454 if (res) 2455 return res; 2456 2457 ss << std::format(" [NBIO{}] {}\n", pBank->nbio, pBank->name); 2458 ss << std::format(" Address: 0x{:08X}\n", pBank->addr); 2459 ss << std::format(" Data Count: {}\n", pBank->count); 2460 ss << " Data:\n"; 2461 for (size_t i = 0; i < pBank->count; i++) 2462 { 2463 ss << std::format(" {}: 0x{:08X}\n", i, pBank->data[i]); 2464 } 2465 ss << "\n"; 2466 2467 return ipmi::ccSuccess; 2468 } 2469 2470 static ipmi_ret_t handleCrdHdrBank(std::span<const uint8_t> data, 2471 CrdState& currState, std::stringstream& ss) 2472 { 2473 if (data.size() < sizeof(CrdHdrBank)) 2474 return ipmi::ccReqDataLenInvalid; 2475 2476 ipmi_ret_t res = setDumpState(currState, CrdState::waitData); 2477 if (res) 2478 return res; 2479 2480 const auto* pBank = reinterpret_cast<const CrdHdrBank*>(data.data()); 2481 ss << " Crashdump Header\n"; 2482 ss << std::format(" CPU PPIN : 0x{:016X}\n", pBank->ppin); 2483 ss << std::format(" UCODE VERSION : 0x{:08X}\n", pBank->ucodeVer); 2484 ss << std::format(" PMIO 80h : 0x{:08X}\n", pBank->pmio); 2485 ss << std::format( 2486 " BIT0 - SMN Parity/SMN Timeouts PSP/SMU Parity and ECC/SMN On-Package Link Error : {}\n", 2487 pBank->pmio & 0x1); 2488 ss << std::format(" BIT2 - PSP Parity and ECC : {}\n", 2489 (pBank->pmio & 0x4) >> 2); 2490 ss << std::format(" BIT3 - SMN Timeouts SMU : {}\n", 2491 (pBank->pmio & 0x8) >> 3); 2492 ss << std::format(" BIT4 - SMN Off-Package Link Packet Error : {}\n", 2493 (pBank->pmio & 0x10) >> 4); 2494 ss << "\n"; 2495 2496 return ipmi::ccSuccess; 2497 } 2498 2499 static std::string getFilename(const std::filesystem::path& dir, 2500 const std::string& prefix) 2501 { 2502 std::vector<int> indices; 2503 std::regex pattern(prefix + "(\\d+)\\.txt"); 2504 2505 for (const auto& entry : std::filesystem::directory_iterator(dir)) 2506 { 2507 std::string filename = entry.path().filename().string(); 2508 std::smatch match; 2509 if (std::regex_match(filename, match, pattern)) 2510 indices.push_back(std::stoi(match[1])); 2511 } 2512 2513 std::sort(indices.rbegin(), indices.rend()); 2514 while (indices.size() > 2) // keep 3 files, so remove if more than 2 2515 { 2516 std::filesystem::remove( 2517 dir / (prefix + std::to_string(indices.back()) + ".txt")); 2518 indices.pop_back(); 2519 } 2520 2521 int nextIndex = indices.empty() ? 1 : indices.front() + 1; 2522 return prefix + std::to_string(nextIndex) + ".txt"; 2523 } 2524 2525 static ipmi_ret_t handleCtrlBank(std::span<const uint8_t> data, 2526 CrdState& currState, std::stringstream& ss) 2527 { 2528 if (data.empty()) 2529 return ipmi::ccReqDataLenInvalid; 2530 2531 switch (static_cast<CrdCtrl>(data[0])) 2532 { 2533 case CrdCtrl::getState: 2534 break; 2535 case CrdCtrl::finish: 2536 { 2537 ipmi_ret_t res = setDumpState(currState, CrdState::packing); 2538 if (res) 2539 return res; 2540 2541 const std::filesystem::path dumpDir = "/var/lib/fb-ipmi-oem"; 2542 std::string filename = getFilename(dumpDir, "crashdump_"); 2543 std::ofstream outFile(dumpDir / filename); 2544 if (!outFile.is_open()) 2545 return ipmi::ccUnspecifiedError; 2546 2547 auto now = std::chrono::system_clock::to_time_t( 2548 std::chrono::system_clock::now()); 2549 outFile << "Crash Dump generated at: " 2550 << std::put_time(std::localtime(&now), "%Y-%m-%d %H:%M:%S") 2551 << "\n\n"; 2552 outFile << ss.str(); 2553 outFile.close(); 2554 ss.str(""); 2555 ss.clear(); 2556 setDumpState(currState, CrdState::free); 2557 break; 2558 } 2559 default: 2560 return ccInvalidParam; 2561 } 2562 2563 return ipmi::ccSuccess; 2564 } 2565 2566 ipmi::RspType<std::vector<uint8_t>> ipmiOemCrashdump( 2567 [[maybe_unused]] ipmi::Context::ptr ctx, std::vector<uint8_t> reqData) 2568 { 2569 static CrdState dumpState = CrdState::free; 2570 static std::stringstream ss; 2571 2572 if (reqData.size() < sizeof(CrashDumpHdr)) 2573 return ipmi::responseReqDataLenInvalid(); 2574 2575 const auto* pHdr = reinterpret_cast<const CrashDumpHdr*>(reqData.data()); 2576 std::span<const uint8_t> bData{reqData.data() + sizeof(CrashDumpHdr), 2577 reqData.size() - sizeof(CrashDumpHdr)}; 2578 ipmi_ret_t res; 2579 2580 switch (pHdr->bankHdr.bankType) 2581 { 2582 case BankType::mca: 2583 res = handleMcaBank(*pHdr, bData, dumpState, ss); 2584 break; 2585 case BankType::virt: 2586 if (pHdr->bankHdr.version >= 3) 2587 { 2588 res = handleVirtualBank<CrdVirtualBankV3>(bData, dumpState, ss); 2589 break; 2590 } 2591 res = handleVirtualBank<CrdVirtualBankV2>(bData, dumpState, ss); 2592 break; 2593 case BankType::cpuWdt: 2594 res = handleCpuWdtBank(bData, dumpState, ss); 2595 break; 2596 case BankType::tcdx: 2597 res = handleHwAssertBank<tcdxNum>("TCDX", bData, dumpState, ss); 2598 break; 2599 case BankType::cake: 2600 res = handleHwAssertBank<cakeNum>("CAKE", bData, dumpState, ss); 2601 break; 2602 case BankType::pie0: 2603 res = handleHwAssertBank<pie0Num>("PIE", bData, dumpState, ss); 2604 break; 2605 case BankType::iom: 2606 res = handleHwAssertBank<iomNum>("IOM", bData, dumpState, ss); 2607 break; 2608 case BankType::ccix: 2609 res = handleHwAssertBank<ccixNum>("CCIX", bData, dumpState, ss); 2610 break; 2611 case BankType::cs: 2612 res = handleHwAssertBank<csNum>("CS", bData, dumpState, ss); 2613 break; 2614 case BankType::pcieAer: 2615 res = handlePcieAerBank(bData, dumpState, ss); 2616 break; 2617 case BankType::wdtReg: 2618 res = handleWdtRegBank(bData, dumpState, ss); 2619 break; 2620 case BankType::ctrl: 2621 res = handleCtrlBank(bData, dumpState, ss); 2622 if (res == ipmi::ccSuccess && 2623 static_cast<CrdCtrl>(bData[0]) == CrdCtrl::getState) 2624 { 2625 return ipmi::responseSuccess( 2626 std::vector<uint8_t>{static_cast<uint8_t>(dumpState)}); 2627 } 2628 break; 2629 case BankType::crdHdr: 2630 res = handleCrdHdrBank(bData, dumpState, ss); 2631 break; 2632 default: 2633 return ipmi::responseInvalidFieldRequest(); 2634 } 2635 2636 return ipmi::response(res); 2637 } 2638 2639 static void registerOEMFunctions(void) 2640 { 2641 /* Get OEM data from json file */ 2642 std::ifstream file(JSON_OEM_DATA_FILE); 2643 if (file) 2644 { 2645 try 2646 { 2647 file >> oemData; 2648 } 2649 // If parsing fails, initialize oemData as an empty JSON and 2650 // overwrite the file 2651 catch (const nlohmann::json::parse_error& e) 2652 { 2653 lg2::error("Error parsing JSON file: {ERROR}", "ERROR", e); 2654 oemData = nlohmann::json::object(); 2655 std::ofstream outFile(JSON_OEM_DATA_FILE, std::ofstream::trunc); 2656 outFile << oemData.dump(4); // Write empty JSON object to the file 2657 outFile.close(); 2658 } 2659 file.close(); 2660 } 2661 else 2662 { 2663 lg2::info("Failed to open JSON file."); 2664 } 2665 2666 lg2::info("Registering OEM commands."); 2667 2668 ipmiPrintAndRegister(NETFN_OEM_USB_DBG_REQ, CMD_OEM_USB_DBG_GET_FRAME_INFO, 2669 NULL, ipmiOemDbgGetFrameInfo, 2670 PRIVILEGE_USER); // get debug frame info 2671 ipmiPrintAndRegister(NETFN_OEM_USB_DBG_REQ, 2672 CMD_OEM_USB_DBG_GET_UPDATED_FRAMES, NULL, 2673 ipmiOemDbgGetUpdFrames, 2674 PRIVILEGE_USER); // get debug updated frames 2675 ipmiPrintAndRegister(NETFN_OEM_USB_DBG_REQ, CMD_OEM_USB_DBG_GET_POST_DESC, 2676 NULL, ipmiOemDbgGetPostDesc, 2677 PRIVILEGE_USER); // get debug post description 2678 ipmiPrintAndRegister(NETFN_OEM_USB_DBG_REQ, CMD_OEM_USB_DBG_GET_GPIO_DESC, 2679 NULL, ipmiOemDbgGetGpioDesc, 2680 PRIVILEGE_USER); // get debug gpio description 2681 ipmiPrintAndRegister(NETFN_OEM_USB_DBG_REQ, CMD_OEM_USB_DBG_GET_FRAME_DATA, 2682 NULL, ipmiOemDbgGetFrameData, 2683 PRIVILEGE_USER); // get debug frame data 2684 ipmiPrintAndRegister(NETFN_OEM_USB_DBG_REQ, CMD_OEM_USB_DBG_CTRL_PANEL, 2685 NULL, ipmiOemDbgGetCtrlPanel, 2686 PRIVILEGE_USER); // get debug control panel 2687 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_DIMM_INFO, NULL, 2688 ipmiOemSetDimmInfo, 2689 PRIVILEGE_USER); // Set Dimm Info 2690 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_GET_BOARD_ID, NULL, 2691 ipmiOemGetBoardID, 2692 PRIVILEGE_USER); // Get Board ID 2693 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnOemOne, 2694 CMD_OEM_GET_80PORT_RECORD, ipmi::Privilege::User, 2695 ipmiOemGet80PortRecord); // Get 80 Port Record 2696 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_MACHINE_CONFIG_INFO, 2697 NULL, ipmiOemSetMachineCfgInfo, 2698 PRIVILEGE_USER); // Set Machine Config Info 2699 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_POST_START, NULL, 2700 ipmiOemSetPostStart, 2701 PRIVILEGE_USER); // Set POST start 2702 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_POST_END, NULL, 2703 ipmiOemSetPostEnd, 2704 PRIVILEGE_USER); // Set POST End 2705 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_PPIN_INFO, NULL, 2706 ipmiOemSetPPINInfo, 2707 PRIVILEGE_USER); // Set PPIN Info 2708 #if BIC_ENABLED 2709 2710 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemOne, 2711 ipmi::cmdSetSystemGuid, ipmi::Privilege::User, 2712 ipmiOemSetSystemGuid); 2713 #else 2714 2715 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_SYSTEM_GUID, NULL, 2716 ipmiOemSetSystemGuid, 2717 PRIVILEGE_USER); // Set System GUID 2718 #endif 2719 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_ADR_TRIGGER, NULL, 2720 ipmiOemSetAdrTrigger, 2721 PRIVILEGE_USER); // Set ADR Trigger 2722 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_BIOS_FLASH_INFO, NULL, 2723 ipmiOemSetBiosFlashInfo, 2724 PRIVILEGE_USER); // Set Bios Flash Info 2725 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_SET_PPR, NULL, 2726 ipmiOemSetPpr, 2727 PRIVILEGE_USER); // Set PPR 2728 ipmiPrintAndRegister(ipmi::netFnOemOne, CMD_OEM_GET_PPR, NULL, 2729 ipmiOemGetPpr, 2730 PRIVILEGE_USER); // Get PPR 2731 /* FB OEM QC Commands */ 2732 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemFour, 2733 CMD_OEM_Q_SET_PROC_INFO, ipmi::Privilege::User, 2734 ipmiOemQSetProcInfo); // Set Proc Info 2735 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemFour, 2736 CMD_OEM_Q_GET_PROC_INFO, ipmi::Privilege::User, 2737 ipmiOemQGetProcInfo); // Get Proc Info 2738 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemFour, 2739 ipmi::cmdSetQDimmInfo, ipmi::Privilege::User, 2740 ipmiOemQSetDimmInfo); // Set Dimm Info 2741 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemFour, 2742 ipmi::cmdGetQDimmInfo, ipmi::Privilege::User, 2743 ipmiOemQGetDimmInfo); // Get Dimm Info 2744 ipmiPrintAndRegister(NETFUN_FB_OEM_QC, CMD_OEM_Q_SET_DRIVE_INFO, NULL, 2745 ipmiOemQSetDriveInfo, 2746 PRIVILEGE_USER); // Set Drive Info 2747 ipmiPrintAndRegister(NETFUN_FB_OEM_QC, CMD_OEM_Q_GET_DRIVE_INFO, NULL, 2748 ipmiOemQGetDriveInfo, 2749 PRIVILEGE_USER); // Get Drive Info 2750 2751 /* FB OEM DCMI Commands as per DCMI spec 1.5 Section 6 */ 2752 ipmi::registerGroupHandler( 2753 ipmi::prioOpenBmcBase, groupDCMI, ipmi::dcmi::cmdGetPowerReading, 2754 ipmi::Privilege::User, 2755 ipmiOemDCMIGetPowerReading); // Get Power Reading 2756 2757 ipmi::registerGroupHandler( 2758 ipmi::prioOpenBmcBase, groupDCMI, ipmi::dcmi::cmdGetPowerLimit, 2759 ipmi::Privilege::User, 2760 ipmiOemDCMIGetPowerLimit); // Get Power Limit 2761 2762 ipmi::registerGroupHandler( 2763 ipmi::prioOpenBmcBase, groupDCMI, ipmi::dcmi::cmdSetPowerLimit, 2764 ipmi::Privilege::Operator, 2765 ipmiOemDCMISetPowerLimit); // Set Power Limit 2766 2767 ipmi::registerGroupHandler( 2768 ipmi::prioOpenBmcBase, groupDCMI, ipmi::dcmi::cmdActDeactivatePwrLimit, 2769 ipmi::Privilege::Operator, 2770 ipmiOemDCMIApplyPowerLimit); // Apply Power Limit 2771 2772 /* FB OEM BOOT ORDER COMMANDS */ 2773 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemOne, 2774 CMD_OEM_GET_BOOT_ORDER, ipmi::Privilege::User, 2775 ipmiOemGetBootOrder); // Get Boot Order 2776 2777 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemOne, 2778 CMD_OEM_SET_BOOT_ORDER, ipmi::Privilege::User, 2779 ipmiOemSetBootOrder); // Set Boot Order 2780 2781 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemOne, 2782 CMD_OEM_GET_HTTPS_BOOT_DATA, ipmi::Privilege::User, 2783 ipmiOemGetHttpsData); 2784 2785 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemOne, 2786 CMD_OEM_GET_HTTPS_BOOT_ATTR, ipmi::Privilege::User, 2787 ipmiOemGetHttpsAttr); 2788 2789 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnOemOne, 2790 CMD_OEM_CRASHDUMP, ipmi::Privilege::User, 2791 ipmiOemCrashdump); 2792 2793 return; 2794 } 2795 2796 } // namespace ipmi 2797