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