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 <ipmid/api-types.hpp>
22 #include <stdplus/print.hpp>
23
24 #include <cstring>
25 #include <span>
26 #include <vector>
27
28 namespace google
29 {
30 namespace ipmi
31 {
32
33 struct CpldRequest
34 {
35 uint8_t id;
36 } __attribute__((packed));
37
38 //
39 // Handle reading the cpld version from the tmpfs.
40 //
cpldVersion(std::span<const uint8_t> data,const HandlerInterface * handler)41 Resp cpldVersion(std::span<const uint8_t> data, const HandlerInterface* handler)
42 {
43 struct CpldRequest request;
44
45 if (data.size() < sizeof(request))
46 {
47 stdplus::print(stderr, "Invalid command length: {}\n", data.size());
48 return ::ipmi::responseReqDataLenInvalid();
49 }
50
51 // data[0] is the CPLD id. "/run/cpld{id}.version" is what we read.
52 // Verified that this cast actually returns the value 255 and not something
53 // negative in the case where data[0] is 0xff. However, it looks weird
54 // since I would expect int(uint8(0xff)) to be -1. So, just cast it
55 // unsigned. we're casting to an int width to avoid it thinking it's a
56 // letter, because it does that.
57 std::memcpy(&request, data.data(), sizeof(request));
58
59 try
60 {
61 auto values =
62 handler->getCpldVersion(static_cast<unsigned int>(request.id));
63
64 // Truncate if the version is too high (documented).
65 auto major = std::get<0>(values);
66 auto minor = std::get<1>(values);
67 auto point = std::get<2>(values);
68 auto subpoint = std::get<3>(values);
69
70 return ::ipmi::responseSuccess(
71 SysOEMCommands::SysCpldVersion,
72 std::vector<std::uint8_t>{major, minor, point, subpoint});
73 }
74 catch (const IpmiException& e)
75 {
76 return ::ipmi::response(e.getIpmiError());
77 }
78 }
79
80 } // namespace ipmi
81 } // namespace google
82