blob: 9d12dee2f7affc8505a38dd52a6a5619f8005324 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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.
Patrick Venture4d49ae62018-09-17 11:35:32 -070014
15#include "cpld.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Patrick Venturebb90d4f2019-03-15 13:42:06 -070018#include "errors.hpp"
19#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070020
Patrick Venturece07ee02018-09-19 18:09:32 -070021#include <cstring>
Patrick Venture4d49ae62018-09-17 11:35:32 -070022
23namespace google
24{
25namespace ipmi
26{
Patrick Venture4d49ae62018-09-17 11:35:32 -070027
28struct CpldRequest
29{
30 uint8_t subcommand;
31 uint8_t id;
32} __attribute__((packed));
33
34struct CpldReply
35{
36 uint8_t subcommand;
37 uint8_t major;
38 uint8_t minor;
39 uint8_t point;
40 uint8_t subpoint;
41} __attribute__((packed));
42
43//
44// Handle reading the cpld version from the tmpfs.
45//
Patrick Venture45fad1b2019-03-18 16:52:14 -070046ipmi_ret_t cpldVersion(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Venturebb90d4f2019-03-15 13:42:06 -070047 size_t* dataLen, const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070048{
Patrick Ventureacd54232018-10-16 16:58:59 -070049 struct CpldRequest request;
50
51 if ((*dataLen) < sizeof(request))
Patrick Venture4d49ae62018-09-17 11:35:32 -070052 {
Patrick Venturece07ee02018-09-19 18:09:32 -070053 std::fprintf(stderr, "Invalid command length: %u\n",
54 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080055 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070056 }
57
58 // reqBuf[0] is the subcommand.
59 // reqBuf[1] is the CPLD id. "/run/cpld{id}.version" is what we read.
60 // Verified that this cast actually returns the value 255 and not something
61 // negative in the case where reqBuf[1] is 0xff. However, it looks weird
62 // since I would expect int(uint8(0xff)) to be -1. So, just cast it
63 // unsigned. we're casting to an int width to avoid it thinking it's a
64 // letter, because it does that.
Patrick Ventureacd54232018-10-16 16:58:59 -070065 std::memcpy(&request, &reqBuf[0], sizeof(request));
Patrick Venture4d49ae62018-09-17 11:35:32 -070066
Patrick Venture4d49ae62018-09-17 11:35:32 -070067 try
68 {
Patrick Venturebb90d4f2019-03-15 13:42:06 -070069 auto values =
70 handler->getCpldVersion(static_cast<unsigned int>(request.id));
71
72 // Truncate if the version is too high (documented).
73 struct CpldReply reply;
74 reply.subcommand = SysCpldVersion;
75 reply.major = std::get<0>(values);
76 reply.minor = std::get<1>(values);
77 reply.point = std::get<2>(values);
78 reply.subpoint = std::get<3>(values);
79
80 std::memcpy(&replyBuf[0], &reply, sizeof(reply));
81 (*dataLen) = sizeof(reply);
82 return IPMI_CC_OK;
Patrick Venture4d49ae62018-09-17 11:35:32 -070083 }
Patrick Venturebb90d4f2019-03-15 13:42:06 -070084 catch (const IpmiException& e)
Patrick Venture4d49ae62018-09-17 11:35:32 -070085 {
Patrick Venturebb90d4f2019-03-15 13:42:06 -070086 return e.getIpmiError();
Patrick Venture4d49ae62018-09-17 11:35:32 -070087 }
Patrick Venture4d49ae62018-09-17 11:35:32 -070088}
89
90} // namespace ipmi
91} // namespace google