Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 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. |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 14 | |
| 15 | #include "eth.hpp" |
| 16 | |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 17 | #include "commands.hpp" |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 18 | #include "handler.hpp" |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 19 | |
| 20 | #include <cstdint> |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 21 | #include <cstring> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 22 | #include <string> |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 23 | #include <tuple> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 24 | |
| 25 | namespace google |
| 26 | { |
| 27 | namespace ipmi |
| 28 | { |
| 29 | |
| 30 | struct EthDeviceRequest |
| 31 | { |
| 32 | uint8_t subcommand; |
| 33 | } __attribute__((packed)); |
| 34 | |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 35 | // 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 III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame] | 41 | ipmi_ret_t getEthDevice(const uint8_t* reqBuf, uint8_t* replyBuf, |
| 42 | size_t* dataLen, const HandlerInterface* handler) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 43 | { |
| 44 | if ((*dataLen) < sizeof(struct EthDeviceRequest)) |
| 45 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 46 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 47 | static_cast<uint32_t>(*dataLen)); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 48 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 49 | } |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame] | 50 | reqBuf += sizeof(struct EthDeviceRequest); |
| 51 | *dataLen -= sizeof(struct EthDeviceRequest); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 52 | |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame] | 53 | std::tuple<std::uint8_t, std::string> details = handler->getEthDetails( |
| 54 | std::string(reinterpret_cast<const char*>(reqBuf), *dataLen)); |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 55 | |
| 56 | std::string device = std::get<1>(details); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 57 | if (device.length() == 0) |
| 58 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 59 | std::fprintf(stderr, "Invalid eth string\n"); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 60 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER) |
| 64 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 65 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 66 | return IPMI_CC_REQUESTED_TOO_MANY_BYTES; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Fill in the response buffer. |
| 70 | auto reply = reinterpret_cast<struct EthDeviceReply*>(&replyBuf[0]); |
| 71 | reply->subcommand = SysGetEthDevice; |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 72 | reply->channel = std::get<0>(details); |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 73 | reply->ifNameLength = device.length(); |
William A. Kennington III | 5d78973 | 2021-06-24 03:39:31 -0700 | [diff] [blame] | 74 | std::memcpy(reply + 1, device.c_str(), device.length()); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 75 | |
| 76 | (*dataLen) = sizeof(struct EthDeviceReply) + device.length(); |
| 77 | |
| 78 | return IPMI_CC_OK; |
| 79 | } |
| 80 | |
| 81 | } // namespace ipmi |
| 82 | } // namespace google |