xref: /openbmc/google-ipmi-sys/cpld.cpp (revision ff3cd8e9)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "cpld.hpp"
16 
17 #include "commands.hpp"
18 #include "errors.hpp"
19 #include "handler.hpp"
20 
21 #include <cstring>
22 #include <ipmid/api-types.hpp>
23 #include <vector>
24 
25 namespace google
26 {
27 namespace ipmi
28 {
29 
30 struct CpldRequest
31 {
32     uint8_t id;
33 } __attribute__((packed));
34 
35 //
36 // Handle reading the cpld version from the tmpfs.
37 //
38 Resp cpldVersion(const std::vector<std::uint8_t>& data,
39                  const HandlerInterface* handler)
40 {
41     struct CpldRequest request;
42 
43     if (data.size() < sizeof(request))
44     {
45         std::fprintf(stderr, "Invalid command length: %u\n",
46                      static_cast<uint32_t>(data.size()));
47         return ::ipmi::responseReqDataLenInvalid();
48     }
49 
50     // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read.
51     // Verified that this cast actually returns the value 255 and not something
52     // negative in the case where data[0] is 0xff.  However, it looks weird
53     // since I would expect int(uint8(0xff)) to be -1.  So, just cast it
54     // unsigned. we're casting to an int width to avoid it thinking it's a
55     // letter, because it does that.
56     std::memcpy(&request, data.data(), sizeof(request));
57 
58     try
59     {
60         auto values =
61             handler->getCpldVersion(static_cast<unsigned int>(request.id));
62 
63         // Truncate if the version is too high (documented).
64         auto major = std::get<0>(values);
65         auto minor = std::get<1>(values);
66         auto point = std::get<2>(values);
67         auto subpoint = std::get<3>(values);
68 
69         return ::ipmi::responseSuccess(
70             SysOEMCommands::SysCpldVersion,
71             std::vector<std::uint8_t>{major, minor, point, subpoint});
72     }
73     catch (const IpmiException& e)
74     {
75         return ::ipmi::response(e.getIpmiError());
76     }
77 }
78 
79 } // namespace ipmi
80 } // namespace google
81