xref: /openbmc/google-ipmi-sys/cpld.cpp (revision ff3cd8e9)
1a2056e9cSWilly Tu // Copyright 2021 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly 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>
22*ff3cd8e9SWilly Tu #include <ipmid/api-types.hpp>
23*ff3cd8e9SWilly Tu #include <vector>
244d49ae65SPatrick Venture 
254d49ae65SPatrick Venture namespace google
264d49ae65SPatrick Venture {
274d49ae65SPatrick Venture namespace ipmi
284d49ae65SPatrick Venture {
294d49ae65SPatrick Venture 
304d49ae65SPatrick Venture struct CpldRequest
314d49ae65SPatrick Venture {
324d49ae65SPatrick Venture     uint8_t id;
334d49ae65SPatrick Venture } __attribute__((packed));
344d49ae65SPatrick Venture 
354d49ae65SPatrick Venture //
364d49ae65SPatrick Venture // Handle reading the cpld version from the tmpfs.
374d49ae65SPatrick Venture //
38*ff3cd8e9SWilly Tu Resp cpldVersion(const std::vector<std::uint8_t>& data,
39*ff3cd8e9SWilly Tu                  const HandlerInterface* handler)
404d49ae65SPatrick Venture {
41acd5423aSPatrick Venture     struct CpldRequest request;
42acd5423aSPatrick Venture 
43*ff3cd8e9SWilly Tu     if (data.size() < sizeof(request))
444d49ae65SPatrick Venture     {
45ce07ee0aSPatrick Venture         std::fprintf(stderr, "Invalid command length: %u\n",
46*ff3cd8e9SWilly Tu                      static_cast<uint32_t>(data.size()));
47*ff3cd8e9SWilly Tu         return ::ipmi::responseReqDataLenInvalid();
484d49ae65SPatrick Venture     }
494d49ae65SPatrick Venture 
50*ff3cd8e9SWilly Tu     // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read.
514d49ae65SPatrick Venture     // Verified that this cast actually returns the value 255 and not something
52*ff3cd8e9SWilly Tu     // negative in the case where data[0] is 0xff.  However, it looks weird
534d49ae65SPatrick Venture     // since I would expect int(uint8(0xff)) to be -1.  So, just cast it
544d49ae65SPatrick Venture     // unsigned. we're casting to an int width to avoid it thinking it's a
554d49ae65SPatrick Venture     // letter, because it does that.
56*ff3cd8e9SWilly Tu     std::memcpy(&request, data.data(), sizeof(request));
574d49ae65SPatrick Venture 
584d49ae65SPatrick Venture     try
594d49ae65SPatrick Venture     {
60bb90d4fdSPatrick Venture         auto values =
61bb90d4fdSPatrick Venture             handler->getCpldVersion(static_cast<unsigned int>(request.id));
624d49ae65SPatrick Venture 
634d49ae65SPatrick Venture         // Truncate if the version is too high (documented).
64*ff3cd8e9SWilly Tu         auto major = std::get<0>(values);
65*ff3cd8e9SWilly Tu         auto minor = std::get<1>(values);
66*ff3cd8e9SWilly Tu         auto point = std::get<2>(values);
67*ff3cd8e9SWilly Tu         auto subpoint = std::get<3>(values);
684d49ae65SPatrick Venture 
69*ff3cd8e9SWilly Tu         return ::ipmi::responseSuccess(
70*ff3cd8e9SWilly Tu             SysOEMCommands::SysCpldVersion,
71*ff3cd8e9SWilly Tu             std::vector<std::uint8_t>{major, minor, point, subpoint});
724d49ae65SPatrick Venture     }
73bb90d4fdSPatrick Venture     catch (const IpmiException& e)
74bb90d4fdSPatrick Venture     {
75*ff3cd8e9SWilly Tu         return ::ipmi::response(e.getIpmiError());
76bb90d4fdSPatrick Venture     }
77bb90d4fdSPatrick Venture }
784d49ae65SPatrick Venture 
794d49ae65SPatrick Venture } // namespace ipmi
804d49ae65SPatrick Venture } // namespace google
81