blob: 32c9bcb7c25319709ddcc3fd2c12ea4ba7f37aa9 [file] [log] [blame]
William A. Kennington III03e6def2021-05-11 15:23:15 -07001#include "cmd.hpp"
2
William A. Kennington III03e6def2021-05-11 15:23:15 -07003#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 Williamsc0c95be2024-08-19 16:02:44 -04009#include <stdplus/print.hpp>
William A. Kennington III03e6def2021-05-11 15:23:15 -070010
11#include <array>
Patrick Williamse0602aa2023-07-17 11:20:00 -050012#include <cstdio>
13#include <format>
William A. Kennington III03e6def2021-05-11 15:23:15 -070014#include <map>
Patrick Williams08041c62021-10-06 11:08:34 -050015#include <span>
William A. Kennington III03e6def2021-05-11 15:23:15 -070016#include <stdexcept>
17#include <tuple>
18#include <utility>
19#include <variant>
20#include <vector>
21
22namespace kcsbridge
23{
24
Patrick Williams0efeb172022-07-22 19:26:56 -050025using sdbusplus::bus_t;
26using sdbusplus::message_t;
27using sdbusplus::slot_t;
William A. Kennington III03e6def2021-05-11 15:23:15 -070028
Patrick Williams0efeb172022-07-22 19:26:56 -050029void write(stdplus::Fd& kcs, message_t&& m)
William A. Kennington III03e6def2021-05-11 15:23:15 -070030{
31 std::array<uint8_t, 1024> buffer;
Patrick Williams08041c62021-10-06 11:08:34 -050032 std::span<uint8_t> out(buffer.begin(), 3);
William A. Kennington III03e6def2021-05-11 15:23:15 -070033 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 Williamse0602aa2023-07-17 11:20:00 -050049 throw std::runtime_error(std::format(
William A. Kennington III03e6def2021-05-11 15:23:15 -070050 "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 Williams08041c62021-10-06 11:08:34 -050057 out = std::span<uint8_t>(buffer.begin(), data.size() + 3);
William A. Kennington III03e6def2021-05-11 15:23:15 -070058 }
59 catch (const std::exception& e)
60 {
Patrick Williamsc0c95be2024-08-19 16:02:44 -040061 stdplus::print(stderr, "IPMI response failure: {}\n", e.what());
William A. Kennington III03e6def2021-05-11 15:23:15 -070062 buffer[0] |= 1 << 2;
63 buffer[2] = 0xff;
64 }
65 stdplus::fd::writeExact(kcs, out);
66}
67
Patrick Williams0efeb172022-07-22 19:26:56 -050068void read(stdplus::Fd& kcs, bus_t& bus, slot_t& outstanding)
William A. Kennington III03e6def2021-05-11 15:23:15 -070069{
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 Williamsc0c95be2024-08-19 16:02:44 -040078 stdplus::print(stderr, "Canceling outstanding request\n");
Patrick Williams0efeb172022-07-22 19:26:56 -050079 outstanding = slot_t(nullptr);
William A. Kennington III03e6def2021-05-11 15:23:15 -070080 }
81 if (in.size() < 2)
82 {
Patrick Williamsc0c95be2024-08-19 16:02:44 -040083 stdplus::print(stderr, "Read too small, ignoring\n");
William A. Kennington III03e6def2021-05-11 15:23:15 -070084 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 Williams0efeb172022-07-22 19:26:56 -050094 stdplus::exception::ignore([&outstanding, &kcs](message_t&& m) {
Patrick Williams4bf990a2024-08-16 15:21:15 -040095 outstanding = slot_t(nullptr);
96 write(kcs, std::move(m));
97 }));
William A. Kennington III03e6def2021-05-11 15:23:15 -070098}
99
100} // namespace kcsbridge