blob: be6eefefe619f8b498a47289deea948af1cf7426 [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 "eth.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Patrick Venturef085d912019-03-15 08:50:00 -070018#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070019
20#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070021#include <cstring>
Patrick Venture4d49ae62018-09-17 11:35:32 -070022#include <string>
Patrick Venturef085d912019-03-15 08:50:00 -070023#include <tuple>
Patrick Venture4d49ae62018-09-17 11:35:32 -070024
25namespace google
26{
27namespace ipmi
28{
29
30struct EthDeviceRequest
31{
32 uint8_t subcommand;
33} __attribute__((packed));
34
Patrick Venture4d49ae62018-09-17 11:35:32 -070035// TOOD(venture): The ipmid.h has this macro, which is a header we
36// can't normally access.
37#ifndef MAX_IPMI_BUFFER
38#define MAX_IPMI_BUFFER 64
39#endif
40
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070041ipmi_ret_t getEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf,
42 size_t* dataLen, const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070043{
44 if ((*dataLen) < sizeof(struct EthDeviceRequest))
45 {
Patrick Venturece07ee02018-09-19 18:09:32 -070046 std::fprintf(stderr, "Invalid command length: %u\n",
47 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080048 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070049 }
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070050 reqBuf += sizeof(struct EthDeviceRequest);
51 *dataLen -= sizeof(struct EthDeviceRequest);
Patrick Venture4d49ae62018-09-17 11:35:32 -070052
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070053 std::tuple<std::uint8_t, std::string> details = handler->getEthDetails(
54 std::string(reinterpret_cast<const char*>(reqBuf), *dataLen));
Patrick Venturef085d912019-03-15 08:50:00 -070055
56 std::string device = std::get<1>(details);
Patrick Venture4d49ae62018-09-17 11:35:32 -070057 if (device.length() == 0)
58 {
Patrick Venturece07ee02018-09-19 18:09:32 -070059 std::fprintf(stderr, "Invalid eth string\n");
Patrick Venturefff98612018-11-12 09:05:54 -080060 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070061 }
62
63 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
64 {
Patrick Venturece07ee02018-09-19 18:09:32 -070065 std::fprintf(stderr, "Response would overflow response buffer\n");
Patrick Venturefff98612018-11-12 09:05:54 -080066 return IPMI_CC_REQUESTED_TOO_MANY_BYTES;
Patrick Venture4d49ae62018-09-17 11:35:32 -070067 }
68
69 // Fill in the response buffer.
70 auto reply = reinterpret_cast<struct EthDeviceReply*>(&replyBuf[0]);
71 reply->subcommand = SysGetEthDevice;
Patrick Venturef085d912019-03-15 08:50:00 -070072 reply->channel = std::get<0>(details);
Patrick Venture45fad1b2019-03-18 16:52:14 -070073 reply->ifNameLength = device.length();
William A. Kennington III5d789732021-06-24 03:39:31 -070074 std::memcpy(reply + 1, device.c_str(), device.length());
Patrick Venture4d49ae62018-09-17 11:35:32 -070075
76 (*dataLen) = sizeof(struct EthDeviceReply) + device.length();
77
78 return IPMI_CC_OK;
79}
80
81} // namespace ipmi
82} // namespace google