xref: /openbmc/google-ipmi-sys/cpld.cpp (revision a2056e9c)
1*a2056e9cSWilly Tu // Copyright 2021 Google LLC
2*a2056e9cSWilly Tu //
3*a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4*a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5*a2056e9cSWilly Tu // You may obtain a copy of the License at
6*a2056e9cSWilly Tu //
7*a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8*a2056e9cSWilly Tu //
9*a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10*a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11*a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13*a2056e9cSWilly Tu // limitations under the License.
144d49ae65SPatrick Venture 
154d49ae65SPatrick Venture #include "cpld.hpp"
164d49ae65SPatrick Venture 
170e9aae5dSPatrick Venture #include "commands.hpp"
18bb90d4fdSPatrick Venture #include "errors.hpp"
19bb90d4fdSPatrick Venture #include "handler.hpp"
204d49ae65SPatrick Venture 
21ce07ee0aSPatrick Venture #include <cstring>
224d49ae65SPatrick Venture 
234d49ae65SPatrick Venture namespace google
244d49ae65SPatrick Venture {
254d49ae65SPatrick Venture namespace ipmi
264d49ae65SPatrick Venture {
274d49ae65SPatrick Venture 
284d49ae65SPatrick Venture struct CpldRequest
294d49ae65SPatrick Venture {
304d49ae65SPatrick Venture     uint8_t subcommand;
314d49ae65SPatrick Venture     uint8_t id;
324d49ae65SPatrick Venture } __attribute__((packed));
334d49ae65SPatrick Venture 
344d49ae65SPatrick Venture struct CpldReply
354d49ae65SPatrick Venture {
364d49ae65SPatrick Venture     uint8_t subcommand;
374d49ae65SPatrick Venture     uint8_t major;
384d49ae65SPatrick Venture     uint8_t minor;
394d49ae65SPatrick Venture     uint8_t point;
404d49ae65SPatrick Venture     uint8_t subpoint;
414d49ae65SPatrick Venture } __attribute__((packed));
424d49ae65SPatrick Venture 
434d49ae65SPatrick Venture //
444d49ae65SPatrick Venture // Handle reading the cpld version from the tmpfs.
454d49ae65SPatrick Venture //
4645fad1bbSPatrick Venture ipmi_ret_t cpldVersion(const uint8_t* reqBuf, uint8_t* replyBuf,
47bb90d4fdSPatrick Venture                        size_t* dataLen, const HandlerInterface* handler)
484d49ae65SPatrick Venture {
49acd5423aSPatrick Venture     struct CpldRequest request;
50acd5423aSPatrick Venture 
51acd5423aSPatrick Venture     if ((*dataLen) < sizeof(request))
524d49ae65SPatrick Venture     {
53ce07ee0aSPatrick Venture         std::fprintf(stderr, "Invalid command length: %u\n",
540dede335SPatrick Venture                      static_cast<uint32_t>(*dataLen));
55fff98617SPatrick Venture         return IPMI_CC_REQ_DATA_LEN_INVALID;
564d49ae65SPatrick Venture     }
574d49ae65SPatrick Venture 
584d49ae65SPatrick Venture     // reqBuf[0] is the subcommand.
594d49ae65SPatrick Venture     // reqBuf[1] is the CPLD id. "/run/cpld{id}.version" is what we read.
604d49ae65SPatrick Venture     // Verified that this cast actually returns the value 255 and not something
614d49ae65SPatrick Venture     // negative in the case where reqBuf[1] is 0xff.  However, it looks weird
624d49ae65SPatrick Venture     // since I would expect int(uint8(0xff)) to be -1.  So, just cast it
634d49ae65SPatrick Venture     // unsigned. we're casting to an int width to avoid it thinking it's a
644d49ae65SPatrick Venture     // letter, because it does that.
65acd5423aSPatrick Venture     std::memcpy(&request, &reqBuf[0], sizeof(request));
664d49ae65SPatrick Venture 
674d49ae65SPatrick Venture     try
684d49ae65SPatrick Venture     {
69bb90d4fdSPatrick Venture         auto values =
70bb90d4fdSPatrick Venture             handler->getCpldVersion(static_cast<unsigned int>(request.id));
714d49ae65SPatrick Venture 
724d49ae65SPatrick Venture         // Truncate if the version is too high (documented).
734d49ae65SPatrick Venture         struct CpldReply reply;
744d49ae65SPatrick Venture         reply.subcommand = SysCpldVersion;
75bb90d4fdSPatrick Venture         reply.major = std::get<0>(values);
76bb90d4fdSPatrick Venture         reply.minor = std::get<1>(values);
77bb90d4fdSPatrick Venture         reply.point = std::get<2>(values);
78bb90d4fdSPatrick Venture         reply.subpoint = std::get<3>(values);
794d49ae65SPatrick Venture 
80acd5423aSPatrick Venture         std::memcpy(&replyBuf[0], &reply, sizeof(reply));
81acd5423aSPatrick Venture         (*dataLen) = sizeof(reply);
824d49ae65SPatrick Venture         return IPMI_CC_OK;
834d49ae65SPatrick Venture     }
84bb90d4fdSPatrick Venture     catch (const IpmiException& e)
85bb90d4fdSPatrick Venture     {
86bb90d4fdSPatrick Venture         return e.getIpmiError();
87bb90d4fdSPatrick Venture     }
88bb90d4fdSPatrick Venture }
894d49ae65SPatrick Venture 
904d49ae65SPatrick Venture } // namespace ipmi
914d49ae65SPatrick Venture } // namespace google
92