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> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 22 | #include <ipmid/api-types.hpp> |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 23 | #include <span> |
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> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 26 | #include <vector> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 27 | |
| 28 | namespace google |
| 29 | { |
| 30 | namespace ipmi |
| 31 | { |
| 32 | |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 33 | // TOOD(venture): The ipmid.h has this macro, which is a header we |
| 34 | // can't normally access. |
| 35 | #ifndef MAX_IPMI_BUFFER |
| 36 | #define MAX_IPMI_BUFFER 64 |
| 37 | #endif |
| 38 | |
Willy Tu | b4e3704 | 2021-10-12 17:12:30 -0700 | [diff] [blame] | 39 | Resp getEthDevice(std::span<const uint8_t> data, |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 40 | const HandlerInterface* handler) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 41 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 42 | std::tuple<std::uint8_t, std::string> details = |
| 43 | handler->getEthDetails(std::string(data.begin(), data.end())); |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 44 | |
| 45 | std::string device = std::get<1>(details); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 46 | if (device.length() == 0) |
| 47 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 48 | std::fprintf(stderr, "Invalid eth string\n"); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 49 | return ::ipmi::responseReqDataLenInvalid(); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER) |
| 53 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 54 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 55 | return ::ipmi::responseRetBytesUnavailable(); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 58 | std::vector<std::uint8_t> reply; |
| 59 | reply.reserve(device.length() + sizeof(struct EthDeviceReply)); |
| 60 | reply.emplace_back(std::get<0>(details)); /* channel */ |
| 61 | reply.emplace_back(device.length()); /* ifNameLength */ |
| 62 | reply.insert(reply.end(), device.begin(), device.end()); /* name */ |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 63 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame] | 64 | return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace ipmi |
| 68 | } // namespace google |