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