blob: 080dd9c4d06051e435612c1f31292f3e7fb0f14e [file] [log] [blame]
Patrick Venture4d49ae62018-09-17 11:35:32 -07001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "eth.hpp"
18
Patrick Venturef085d912019-03-15 08:50:00 -070019#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070020#include "main.hpp"
21
22#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070023#include <cstring>
Patrick Venture4d49ae62018-09-17 11:35:32 -070024#include <string>
Patrick Venturef085d912019-03-15 08:50:00 -070025#include <tuple>
Patrick Venture4d49ae62018-09-17 11:35:32 -070026
27namespace google
28{
29namespace ipmi
30{
31
32struct EthDeviceRequest
33{
34 uint8_t subcommand;
35} __attribute__((packed));
36
Patrick Venture4d49ae62018-09-17 11:35:32 -070037// TOOD(venture): The ipmid.h has this macro, which is a header we
38// can't normally access.
39#ifndef MAX_IPMI_BUFFER
40#define MAX_IPMI_BUFFER 64
41#endif
42
Patrick Venture4d49ae62018-09-17 11:35:32 -070043ipmi_ret_t GetEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Venturef085d912019-03-15 08:50:00 -070044 size_t* dataLen, const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070045{
46 if ((*dataLen) < sizeof(struct EthDeviceRequest))
47 {
Patrick Venturece07ee02018-09-19 18:09:32 -070048 std::fprintf(stderr, "Invalid command length: %u\n",
49 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080050 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070051 }
52
Patrick Venturef085d912019-03-15 08:50:00 -070053 std::tuple<std::uint8_t, std::string> details = handler->getEthDetails();
54
55 std::string device = std::get<1>(details);
Patrick Venture4d49ae62018-09-17 11:35:32 -070056 if (device.length() == 0)
57 {
Patrick Venturece07ee02018-09-19 18:09:32 -070058 std::fprintf(stderr, "Invalid eth string\n");
Patrick Venturefff98612018-11-12 09:05:54 -080059 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070060 }
61
62 if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
63 {
Patrick Venturece07ee02018-09-19 18:09:32 -070064 std::fprintf(stderr, "Response would overflow response buffer\n");
Patrick Venturefff98612018-11-12 09:05:54 -080065 return IPMI_CC_REQUESTED_TOO_MANY_BYTES;
Patrick Venture4d49ae62018-09-17 11:35:32 -070066 }
67
68 // Fill in the response buffer.
69 auto reply = reinterpret_cast<struct EthDeviceReply*>(&replyBuf[0]);
70 reply->subcommand = SysGetEthDevice;
Patrick Venturef085d912019-03-15 08:50:00 -070071 reply->channel = std::get<0>(details);
Patrick Venture4d49ae62018-09-17 11:35:32 -070072 reply->if_name_len = device.length();
Patrick Venturece07ee02018-09-19 18:09:32 -070073 std::memcpy(reply->if_name, device.c_str(), device.length());
Patrick Venture4d49ae62018-09-17 11:35:32 -070074
75 (*dataLen) = sizeof(struct EthDeviceReply) + device.length();
76
77 return IPMI_CC_OK;
78}
79
80} // namespace ipmi
81} // namespace google