// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "cpld.hpp" #include "commands.hpp" #include "errors.hpp" #include "handler.hpp" #include namespace google { namespace ipmi { struct CpldRequest { uint8_t subcommand; uint8_t id; } __attribute__((packed)); struct CpldReply { uint8_t subcommand; uint8_t major; uint8_t minor; uint8_t point; uint8_t subpoint; } __attribute__((packed)); // // Handle reading the cpld version from the tmpfs. // ipmi_ret_t cpldVersion(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen, const HandlerInterface* handler) { struct CpldRequest request; if ((*dataLen) < sizeof(request)) { std::fprintf(stderr, "Invalid command length: %u\n", static_cast(*dataLen)); return IPMI_CC_REQ_DATA_LEN_INVALID; } // reqBuf[0] is the subcommand. // reqBuf[1] is the CPLD id. "/run/cpld{id}.version" is what we read. // Verified that this cast actually returns the value 255 and not something // negative in the case where reqBuf[1] is 0xff. However, it looks weird // since I would expect int(uint8(0xff)) to be -1. So, just cast it // unsigned. we're casting to an int width to avoid it thinking it's a // letter, because it does that. std::memcpy(&request, &reqBuf[0], sizeof(request)); try { auto values = handler->getCpldVersion(static_cast(request.id)); // Truncate if the version is too high (documented). struct CpldReply reply; reply.subcommand = SysCpldVersion; reply.major = std::get<0>(values); reply.minor = std::get<1>(values); reply.point = std::get<2>(values); reply.subpoint = std::get<3>(values); std::memcpy(&replyBuf[0], &reply, sizeof(reply)); (*dataLen) = sizeof(reply); return IPMI_CC_OK; } catch (const IpmiException& e) { return e.getIpmiError(); } } } // namespace ipmi } // namespace google