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