William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 1 | #include "cmd.hpp" |
| 2 | |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
| 4 | #include <sdbusplus/exception.hpp> |
| 5 | #include <sdbusplus/message.hpp> |
| 6 | #include <sdbusplus/slot.hpp> |
| 7 | #include <stdplus/exception.hpp> |
| 8 | #include <stdplus/fd/ops.hpp> |
Patrick Williams | c0c95be | 2024-08-19 16:02:44 -0400 | [diff] [blame^] | 9 | #include <stdplus/print.hpp> |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 10 | |
| 11 | #include <array> |
Patrick Williams | e0602aa | 2023-07-17 11:20:00 -0500 | [diff] [blame] | 12 | #include <cstdio> |
| 13 | #include <format> |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 14 | #include <map> |
Patrick Williams | 08041c6 | 2021-10-06 11:08:34 -0500 | [diff] [blame] | 15 | #include <span> |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 16 | #include <stdexcept> |
| 17 | #include <tuple> |
| 18 | #include <utility> |
| 19 | #include <variant> |
| 20 | #include <vector> |
| 21 | |
| 22 | namespace kcsbridge |
| 23 | { |
| 24 | |
Patrick Williams | 0efeb17 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 25 | using sdbusplus::bus_t; |
| 26 | using sdbusplus::message_t; |
| 27 | using sdbusplus::slot_t; |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 28 | |
Patrick Williams | 0efeb17 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 29 | void write(stdplus::Fd& kcs, message_t&& m) |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 30 | { |
| 31 | std::array<uint8_t, 1024> buffer; |
Patrick Williams | 08041c6 | 2021-10-06 11:08:34 -0500 | [diff] [blame] | 32 | std::span<uint8_t> out(buffer.begin(), 3); |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 33 | try |
| 34 | { |
| 35 | if (m.is_method_error()) |
| 36 | { |
| 37 | // Extra copy to workaround lack of `const sd_bus_error` constructor |
| 38 | auto error = *m.get_error(); |
| 39 | throw sdbusplus::exception::SdBusError(&error, "ipmid response"); |
| 40 | } |
| 41 | std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>> |
| 42 | ret; |
| 43 | m.read(ret); |
| 44 | const auto& [netfn, lun, cmd, cc, data] = ret; |
| 45 | // Based on the IPMI KCS spec Figure 9-2 |
| 46 | // netfn needs to be changed to odd in KCS responses |
| 47 | if (data.size() + 3 > buffer.size()) |
| 48 | { |
Patrick Williams | e0602aa | 2023-07-17 11:20:00 -0500 | [diff] [blame] | 49 | throw std::runtime_error(std::format( |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 50 | "too large {} > {}", data.size() + 3, buffer.size())); |
| 51 | } |
| 52 | buffer[0] = (netfn | 1) << 2; |
| 53 | buffer[0] |= lun; |
| 54 | buffer[1] = cmd; |
| 55 | buffer[2] = cc; |
| 56 | memcpy(&buffer[3], data.data(), data.size()); |
Patrick Williams | 08041c6 | 2021-10-06 11:08:34 -0500 | [diff] [blame] | 57 | out = std::span<uint8_t>(buffer.begin(), data.size() + 3); |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 58 | } |
| 59 | catch (const std::exception& e) |
| 60 | { |
Patrick Williams | c0c95be | 2024-08-19 16:02:44 -0400 | [diff] [blame^] | 61 | stdplus::print(stderr, "IPMI response failure: {}\n", e.what()); |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 62 | buffer[0] |= 1 << 2; |
| 63 | buffer[2] = 0xff; |
| 64 | } |
| 65 | stdplus::fd::writeExact(kcs, out); |
| 66 | } |
| 67 | |
Patrick Williams | 0efeb17 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 68 | void read(stdplus::Fd& kcs, bus_t& bus, slot_t& outstanding) |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 69 | { |
| 70 | std::array<uint8_t, 1024> buffer; |
| 71 | auto in = stdplus::fd::read(kcs, buffer); |
| 72 | if (in.empty()) |
| 73 | { |
| 74 | return; |
| 75 | } |
| 76 | if (outstanding) |
| 77 | { |
Patrick Williams | c0c95be | 2024-08-19 16:02:44 -0400 | [diff] [blame^] | 78 | stdplus::print(stderr, "Canceling outstanding request\n"); |
Patrick Williams | 0efeb17 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 79 | outstanding = slot_t(nullptr); |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 80 | } |
| 81 | if (in.size() < 2) |
| 82 | { |
Patrick Williams | c0c95be | 2024-08-19 16:02:44 -0400 | [diff] [blame^] | 83 | stdplus::print(stderr, "Read too small, ignoring\n"); |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | auto m = bus.new_method_call("xyz.openbmc_project.Ipmi.Host", |
| 87 | "/xyz/openbmc_project/Ipmi", |
| 88 | "xyz.openbmc_project.Ipmi.Server", "execute"); |
| 89 | std::map<std::string, std::variant<int>> options; |
| 90 | // Based on the IPMI KCS spec Figure 9-1 |
| 91 | uint8_t netfn = in[0] >> 2, lun = in[0] & 3, cmd = in[1]; |
| 92 | m.append(netfn, lun, cmd, in.subspan(2), options); |
| 93 | outstanding = m.call_async( |
Patrick Williams | 0efeb17 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 94 | stdplus::exception::ignore([&outstanding, &kcs](message_t&& m) { |
Patrick Williams | 4bf990a | 2024-08-16 15:21:15 -0400 | [diff] [blame] | 95 | outstanding = slot_t(nullptr); |
| 96 | write(kcs, std::move(m)); |
| 97 | })); |
William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace kcsbridge |