xref: /openbmc/google-ipmi-sys/cpld.cpp (revision 8d618532)
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 
21ff3cd8e9SWilly Tu #include <ipmid/api-types.hpp>
22*8d618532SMichael Shen #include <stdplus/print.hpp>
23444b5ea4SPatrick Williams 
24444b5ea4SPatrick Williams #include <cstring>
25b4e3704cSWilly Tu #include <span>
26ff3cd8e9SWilly Tu #include <vector>
274d49ae65SPatrick Venture 
284d49ae65SPatrick Venture namespace google
294d49ae65SPatrick Venture {
304d49ae65SPatrick Venture namespace ipmi
314d49ae65SPatrick Venture {
324d49ae65SPatrick Venture 
334d49ae65SPatrick Venture struct CpldRequest
344d49ae65SPatrick Venture {
354d49ae65SPatrick Venture     uint8_t id;
364d49ae65SPatrick Venture } __attribute__((packed));
374d49ae65SPatrick Venture 
384d49ae65SPatrick Venture //
394d49ae65SPatrick Venture // Handle reading the cpld version from the tmpfs.
404d49ae65SPatrick Venture //
cpldVersion(std::span<const uint8_t> data,const HandlerInterface * handler)41b4e3704cSWilly Tu Resp cpldVersion(std::span<const uint8_t> data, const HandlerInterface* handler)
424d49ae65SPatrick Venture {
43acd5423aSPatrick Venture     struct CpldRequest request;
44acd5423aSPatrick Venture 
45ff3cd8e9SWilly Tu     if (data.size() < sizeof(request))
464d49ae65SPatrick Venture     {
47*8d618532SMichael Shen         stdplus::print(stderr, "Invalid command length: {}\n", data.size());
48ff3cd8e9SWilly Tu         return ::ipmi::responseReqDataLenInvalid();
494d49ae65SPatrick Venture     }
504d49ae65SPatrick Venture 
51ff3cd8e9SWilly Tu     // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read.
524d49ae65SPatrick Venture     // Verified that this cast actually returns the value 255 and not something
53ff3cd8e9SWilly Tu     // negative in the case where data[0] is 0xff.  However, it looks weird
544d49ae65SPatrick Venture     // since I would expect int(uint8(0xff)) to be -1.  So, just cast it
554d49ae65SPatrick Venture     // unsigned. we're casting to an int width to avoid it thinking it's a
564d49ae65SPatrick Venture     // letter, because it does that.
57ff3cd8e9SWilly Tu     std::memcpy(&request, data.data(), sizeof(request));
584d49ae65SPatrick Venture 
594d49ae65SPatrick Venture     try
604d49ae65SPatrick Venture     {
61bb90d4fdSPatrick Venture         auto values =
62bb90d4fdSPatrick Venture             handler->getCpldVersion(static_cast<unsigned int>(request.id));
634d49ae65SPatrick Venture 
644d49ae65SPatrick Venture         // Truncate if the version is too high (documented).
65ff3cd8e9SWilly Tu         auto major = std::get<0>(values);
66ff3cd8e9SWilly Tu         auto minor = std::get<1>(values);
67ff3cd8e9SWilly Tu         auto point = std::get<2>(values);
68ff3cd8e9SWilly Tu         auto subpoint = std::get<3>(values);
694d49ae65SPatrick Venture 
70ff3cd8e9SWilly Tu         return ::ipmi::responseSuccess(
71ff3cd8e9SWilly Tu             SysOEMCommands::SysCpldVersion,
72ff3cd8e9SWilly Tu             std::vector<std::uint8_t>{major, minor, point, subpoint});
734d49ae65SPatrick Venture     }
74bb90d4fdSPatrick Venture     catch (const IpmiException& e)
75bb90d4fdSPatrick Venture     {
76ff3cd8e9SWilly Tu         return ::ipmi::response(e.getIpmiError());
77bb90d4fdSPatrick Venture     }
78bb90d4fdSPatrick Venture }
794d49ae65SPatrick Venture 
804d49ae65SPatrick Venture } // namespace ipmi
814d49ae65SPatrick Venture } // namespace google
82