Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "ipmid.hpp" |
| 2 | |
| 3 | #include "host-ipmid/oemrouter.hpp" |
| 4 | #include "settings.hpp" |
| 5 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 6 | #include <assert.h> |
| 7 | #include <dirent.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 8 | #include <dlfcn.h> |
| 9 | #include <errno.h> |
| 10 | #include <mapper.h> |
| 11 | #include <stdio.h> |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 12 | #include <stdlib.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <systemd/sd-bus.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <host-cmd-manager.hpp> |
| 20 | #include <host-ipmid/ipmid-host-cmd.hpp> |
| 21 | #include <iostream> |
| 22 | #include <ipmiwhitelist.hpp> |
| 23 | #include <iterator> |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 24 | #include <map> |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 25 | #include <memory> |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 27 | #include <sdbusplus/bus.hpp> |
| 28 | #include <sdbusplus/bus/match.hpp> |
Vishwanatha Subbanna | 6e8979d | 2017-07-13 16:48:20 +0530 | [diff] [blame] | 29 | #include <timer.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 30 | #include <vector> |
| 31 | #include <xyz/openbmc_project/Control/Security/RestrictionMode/server.hpp> |
| 32 | |
| 33 | #include "sensorhandler.h" |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 34 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 35 | using namespace phosphor::logging; |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 36 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 37 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 38 | sd_bus* bus = NULL; |
| 39 | sd_bus_slot* ipmid_slot = NULL; |
| 40 | sd_event* events = nullptr; |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 41 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 42 | // Need this to use new sdbusplus compatible interfaces |
| 43 | sdbusPtr sdbusp; |
| 44 | |
| 45 | // Global Host Bound Command manager |
| 46 | using cmdManagerPtr = std::unique_ptr<phosphor::host::command::Manager>; |
| 47 | cmdManagerPtr cmdManager; |
| 48 | |
Ratan Gupta | 7a7f012 | 2018-03-07 12:31:05 +0530 | [diff] [blame] | 49 | // Global timer for network changes |
| 50 | std::unique_ptr<phosphor::ipmi::Timer> networkTimer = nullptr; |
| 51 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 52 | // Command and handler tuple. Used when clients ask the command to be put |
| 53 | // into host message queue |
| 54 | using CommandHandler = phosphor::host::command::CommandHandler; |
| 55 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 56 | // Initialise restricted mode to true |
| 57 | bool restricted_mode = true; |
| 58 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 59 | FILE *ipmiio, *ipmidbus, *ipmicmddetails; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 60 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 61 | void print_usage(void) |
| 62 | { |
| 63 | fprintf(stderr, "Options: [-d mask]\n"); |
| 64 | fprintf(stderr, " mask : 0x01 - Print ipmi packets\n"); |
| 65 | fprintf(stderr, " mask : 0x02 - Print DBUS operations\n"); |
| 66 | fprintf(stderr, " mask : 0x04 - Print ipmi command details\n"); |
| 67 | fprintf(stderr, " mask : 0xFF - Print all trace\n"); |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 70 | const char* DBUS_INTF = "org.openbmc.HostIpmi"; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 71 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 72 | const char* FILTER = |
| 73 | "type='signal',interface='org.openbmc.HostIpmi',member='ReceivedMessage'"; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 74 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 75 | typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t; |
| 76 | typedef std::pair<ipmid_callback_t, ipmi_context_t> ipmi_fn_context_t; |
| 77 | |
| 78 | // Global data structure that contains the IPMI command handler's registrations. |
| 79 | std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map; |
| 80 | |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 81 | // IPMI Spec, shared Reservation ID. |
| 82 | unsigned short g_sel_reserve = 0xFFFF; |
| 83 | |
| 84 | unsigned short get_sel_reserve_id(void) |
| 85 | { |
| 86 | return g_sel_reserve; |
| 87 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 88 | |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 89 | namespace internal |
| 90 | { |
| 91 | |
| 92 | constexpr auto restrictionModeIntf = |
| 93 | "xyz.openbmc_project.Control.Security.RestrictionMode"; |
| 94 | |
| 95 | namespace cache |
| 96 | { |
| 97 | |
| 98 | std::unique_ptr<settings::Objects> objects = nullptr; |
| 99 | |
| 100 | } // namespace cache |
| 101 | } // namespace internal |
| 102 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 103 | #ifndef HEXDUMP_COLS |
| 104 | #define HEXDUMP_COLS 16 |
| 105 | #endif |
| 106 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 107 | void hexdump(FILE* s, void* mem, size_t len) |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 108 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 109 | unsigned int i, j; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 110 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 111 | for (i = 0; |
| 112 | i < |
| 113 | len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); |
| 114 | i++) |
| 115 | { |
| 116 | /* print offset */ |
| 117 | if (i % HEXDUMP_COLS == 0) |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 118 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 119 | fprintf(s, "0x%06x: ", i); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 120 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 121 | |
| 122 | /* print hex data */ |
| 123 | if (i < len) |
| 124 | { |
| 125 | fprintf(s, "%02x ", 0xFF & ((char*)mem)[i]); |
| 126 | } |
| 127 | else /* end of block, just aligning for ASCII dump */ |
| 128 | { |
| 129 | fprintf(s, " "); |
| 130 | } |
| 131 | |
| 132 | /* print ASCII dump */ |
| 133 | if (i % HEXDUMP_COLS == (HEXDUMP_COLS - 1)) |
| 134 | { |
| 135 | for (j = i - (HEXDUMP_COLS - 1); j <= i; j++) |
| 136 | { |
| 137 | if (j >= len) /* end of block, not really printing */ |
| 138 | { |
| 139 | fputc(' ', s); |
| 140 | } |
| 141 | else if (isprint(((char*)mem)[j])) /* printable char */ |
| 142 | { |
| 143 | fputc(0xFF & ((char*)mem)[j], s); |
| 144 | } |
| 145 | else /* other char */ |
| 146 | { |
| 147 | fputc('.', s); |
| 148 | } |
| 149 | } |
| 150 | fputc('\n', s); |
| 151 | } |
| 152 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 155 | // Method that gets called by shared libraries to get their command handlers |
| 156 | // registered |
| 157 | void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 158 | ipmi_context_t context, ipmid_callback_t handler, |
| 159 | ipmi_cmd_privilege_t priv) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 160 | { |
| 161 | // Pack NetFn and Command in one. |
| 162 | auto netfn_and_cmd = std::make_pair(netfn, cmd); |
| 163 | |
| 164 | // Pack Function handler and Data in another. |
| 165 | auto handler_and_context = std::make_pair(handler, context); |
| 166 | |
| 167 | // Check if the registration has already been made.. |
| 168 | auto iter = g_ipmid_router_map.find(netfn_and_cmd); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 169 | if (iter != g_ipmid_router_map.end()) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 170 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 171 | log<level::ERR>("Duplicate registration", entry("NETFN=0x%X", netfn), |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 172 | entry("CMD=0x%X", cmd)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
| 176 | // This is a fresh registration.. Add it to the map. |
| 177 | g_ipmid_router_map.emplace(netfn_and_cmd, handler_and_context); |
| 178 | } |
| 179 | |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // Looks at the map and calls corresponding handler functions. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 184 | ipmi_ret_t ipmi_netfn_router(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 185 | ipmi_request_t request, ipmi_response_t response, |
| 186 | ipmi_data_len_t data_len) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 187 | { |
| 188 | // return from the Command handlers. |
| 189 | ipmi_ret_t rc = IPMI_CC_INVALID; |
| 190 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 191 | // If restricted mode is true and command is not whitelisted, don't |
| 192 | // execute the command |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 193 | if (restricted_mode) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 194 | { |
| 195 | if (!std::binary_search(whitelist.cbegin(), whitelist.cend(), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 196 | std::make_pair(netfn, cmd))) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 197 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 198 | log<level::ERR>("Net function not whitelisted", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 199 | entry("NETFN=0x%X", netfn), entry("CMD=0x%X", cmd)); |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 200 | rc = IPMI_CC_INSUFFICIENT_PRIVILEGE; |
| 201 | memcpy(response, &rc, IPMI_CC_LEN); |
| 202 | *data_len = IPMI_CC_LEN; |
| 203 | return rc; |
| 204 | } |
| 205 | } |
| 206 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 207 | // Walk the map that has the registered handlers and invoke the approprite |
| 208 | // handlers for matching commands. |
| 209 | auto iter = g_ipmid_router_map.find(std::make_pair(netfn, cmd)); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 210 | if (iter == g_ipmid_router_map.end()) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 211 | { |
Patrick Venture | 03f84ba | 2017-09-20 09:15:33 -0700 | [diff] [blame] | 212 | /* By default should only print on failure to find wildcard command. */ |
| 213 | #ifdef __IPMI_DEBUG__ |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 214 | log<level::ERR>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 215 | "No registered handlers for NetFn, trying Wilcard implementation", |
| 216 | entry("NET_FUN=0x%X", netfn) entry("CMD=0x%X", IPMI_CMD_WILDCARD)); |
Patrick Venture | 03f84ba | 2017-09-20 09:15:33 -0700 | [diff] [blame] | 217 | #endif |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 218 | |
| 219 | // Now that we did not find any specific [NetFn,Cmd], tuple, check for |
| 220 | // NetFn, WildCard command present. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 221 | iter = |
| 222 | g_ipmid_router_map.find(std::make_pair(netfn, IPMI_CMD_WILDCARD)); |
| 223 | if (iter == g_ipmid_router_map.end()) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 224 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 225 | log<level::ERR>("No Registered handlers for NetFn", |
| 226 | entry("NET_FUN=0x%X", netfn), |
| 227 | entry("CMD=0x%X", IPMI_CMD_WILDCARD)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 228 | |
| 229 | // Respond with a 0xC1 |
| 230 | memcpy(response, &rc, IPMI_CC_LEN); |
| 231 | *data_len = IPMI_CC_LEN; |
| 232 | return rc; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | #ifdef __IPMI_DEBUG__ |
| 237 | // We have either a perfect match -OR- a wild card atleast, |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 238 | log<level::ERR>("Calling Net function", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 239 | entry("NET_FUN=0x%X", netfn) entry("CMD=0x%X", cmd)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 240 | #endif |
| 241 | |
| 242 | // Extract the map data onto appropriate containers |
| 243 | auto handler_and_context = iter->second; |
| 244 | |
| 245 | // Creating a pointer type casted to char* to make sure we advance 1 byte |
| 246 | // when we advance pointer to next's address. advancing void * would not |
| 247 | // make sense. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 248 | char* respo = &((char*)response)[IPMI_CC_LEN]; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 249 | |
Richard Marian Thomaiyar | ebc886f | 2018-06-06 14:33:59 +0530 | [diff] [blame] | 250 | try |
| 251 | { |
| 252 | // Response message from the plugin goes into a byte post the base |
| 253 | // response |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 254 | rc = (handler_and_context.first)(netfn, cmd, request, respo, data_len, |
| 255 | handler_and_context.second); |
Richard Marian Thomaiyar | ebc886f | 2018-06-06 14:33:59 +0530 | [diff] [blame] | 256 | } |
| 257 | // IPMI command handlers can throw unhandled exceptions, catch those |
| 258 | // and return sane error code. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 259 | catch (const std::exception& e) |
Richard Marian Thomaiyar | ebc886f | 2018-06-06 14:33:59 +0530 | [diff] [blame] | 260 | { |
| 261 | log<level::ERR>(e.what(), entry("NET_FUN=0x%X", netfn), |
| 262 | entry("CMD=0x%X", cmd)); |
| 263 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 264 | *data_len = 0; |
| 265 | // fall through |
| 266 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 267 | // Now copy the return code that we got from handler and pack it in first |
| 268 | // byte. |
| 269 | memcpy(response, &rc, IPMI_CC_LEN); |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 270 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 271 | // Data length is now actual data + completion code. |
| 272 | *data_len = *data_len + IPMI_CC_LEN; |
| 273 | |
| 274 | return rc; |
| 275 | } |
| 276 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 277 | static int send_ipmi_message(sd_bus_message* req, unsigned char seq, |
| 278 | unsigned char netfn, unsigned char lun, |
| 279 | unsigned char cmd, unsigned char cc, |
| 280 | unsigned char* buf, unsigned char len) |
| 281 | { |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 282 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 283 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 284 | sd_bus_message *reply = NULL, *m = NULL; |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 285 | const char *dest, *path; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 286 | int r, pty; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 287 | |
Jeremy Kerr | e41081f | 2015-10-27 12:11:36 +0800 | [diff] [blame] | 288 | dest = sd_bus_message_get_sender(req); |
| 289 | path = sd_bus_message_get_path(req); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 290 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 291 | r = sd_bus_message_new_method_call(bus, &m, dest, path, DBUS_INTF, |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 292 | "sendMessage"); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 293 | if (r < 0) |
| 294 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 295 | log<level::ERR>("Failed to add the method object", |
| 296 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 297 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 298 | } |
| 299 | |
Chris Austen | abfb5e8 | 2015-10-13 12:29:24 -0500 | [diff] [blame] | 300 | // Responses in IPMI require a bit set. So there ya go... |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 301 | netfn |= 0x01; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 302 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 303 | // Add the bytes needed for the methods to be called |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 304 | r = sd_bus_message_append(m, "yyyyy", seq, netfn, lun, cmd, cc); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 305 | if (r < 0) |
| 306 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 307 | log<level::ERR>("Failed add the netfn and others", |
| 308 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 309 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 310 | } |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 311 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 312 | r = sd_bus_message_append_array(m, 'y', buf, len); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 313 | if (r < 0) |
| 314 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 315 | log<level::ERR>("Failed to add the string of response bytes", |
| 316 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 317 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 320 | // Call the IPMI responder on the bus so the message can be sent to the CEC |
| 321 | r = sd_bus_call(bus, m, 0, &error, &reply); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 322 | if (r < 0) |
| 323 | { |
| 324 | log<level::ERR>("Failed to call the method", entry("DEST=%s", dest), |
| 325 | entry("PATH=%s", path), entry("ERRNO=0x%X", -r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 326 | goto final; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | r = sd_bus_message_read(reply, "x", &pty); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 330 | if (r < 0) |
| 331 | { |
| 332 | log<level::ERR>("Failed to get a reply from the method", |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 333 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 336 | final: |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 337 | sd_bus_error_free(&error); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 338 | m = sd_bus_message_unref(m); |
| 339 | reply = sd_bus_message_unref(reply); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 340 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 341 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 344 | void cache_restricted_mode() |
| 345 | { |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 346 | restricted_mode = false; |
| 347 | using namespace sdbusplus::xyz::openbmc_project::Control::Security::server; |
| 348 | using namespace internal; |
| 349 | using namespace internal::cache; |
| 350 | sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection()); |
| 351 | const auto& restrictionModeSetting = |
Deepak Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [diff] [blame] | 352 | objects->map.at(restrictionModeIntf).front(); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 353 | auto method = dbus.new_method_call( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 354 | objects->service(restrictionModeSetting, restrictionModeIntf).c_str(), |
| 355 | restrictionModeSetting.c_str(), "org.freedesktop.DBus.Properties", |
| 356 | "Get"); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 357 | method.append(restrictionModeIntf, "RestrictionMode"); |
| 358 | auto resp = dbus.call(method); |
| 359 | if (resp.is_method_error()) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 360 | { |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 361 | log<level::ERR>("Error in RestrictionMode Get"); |
| 362 | // Fail-safe to true. |
| 363 | restricted_mode = true; |
| 364 | return; |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 365 | } |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 366 | sdbusplus::message::variant<std::string> result; |
| 367 | resp.read(result); |
| 368 | auto restrictionMode = |
| 369 | RestrictionMode::convertModesFromString(result.get<std::string>()); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 370 | if (RestrictionMode::Modes::Whitelist == restrictionMode) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 371 | { |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 372 | restricted_mode = true; |
| 373 | } |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 374 | } |
| 375 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 376 | static int handle_restricted_mode_change(sd_bus_message* m, void* user_data, |
| 377 | sd_bus_error* ret_error) |
Tom Joseph | 9a61b4f | 2016-07-11 06:56:11 -0500 | [diff] [blame] | 378 | { |
| 379 | cache_restricted_mode(); |
| 380 | return 0; |
| 381 | } |
| 382 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 383 | static int handle_ipmi_command(sd_bus_message* m, void* user_data, |
| 384 | sd_bus_error* ret_error) |
| 385 | { |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 386 | int r = 0; |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 387 | unsigned char sequence, netfn, lun, cmd; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 388 | const void* request; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 389 | size_t sz; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 390 | size_t resplen = MAX_IPMI_BUFFER; |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 391 | unsigned char response[MAX_IPMI_BUFFER]; |
| 392 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 393 | memset(response, 0, MAX_IPMI_BUFFER); |
| 394 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 395 | r = sd_bus_message_read(m, "yyyy", &sequence, &netfn, &lun, &cmd); |
| 396 | if (r < 0) |
| 397 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 398 | log<level::ERR>("Failed to parse signal message", |
| 399 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 400 | return -1; |
| 401 | } |
| 402 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 403 | r = sd_bus_message_read_array(m, 'y', &request, &sz); |
| 404 | if (r < 0) |
| 405 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 406 | log<level::ERR>("Failed to parse signal message", |
| 407 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 408 | return -1; |
| 409 | } |
| 410 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 411 | fprintf(ipmiio, "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", |
| 412 | sequence, netfn, cmd); |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 413 | hexdump(ipmiio, (void*)request, sz); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 414 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 415 | // Allow the length field to be used for both input and output of the |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 416 | // ipmi call |
| 417 | resplen = sz; |
| 418 | |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 419 | // Now that we have parsed the entire byte array from the caller |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 420 | // we can call the ipmi router to do the work... |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 421 | r = ipmi_netfn_router(netfn, cmd, (void*)request, (void*)response, |
| 422 | &resplen); |
| 423 | if (r != 0) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 424 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 425 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 426 | log<level::ERR>("ERROR in handling NetFn", entry("ERRNO=0x%X", -r), |
| 427 | entry("NET_FUN=0x%X", netfn), entry("CMD=0x%X", cmd)); |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 428 | #endif |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 429 | resplen = 0; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | resplen = resplen - 1; // first byte is for return code. |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 434 | } |
| 435 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 436 | fprintf(ipmiio, "IPMI Response:\n"); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 437 | hexdump(ipmiio, (void*)response, resplen); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 438 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 439 | // Send the response buffer from the ipmi command |
Jeremy Kerr | 2564b1a | 2015-10-27 13:37:17 +0800 | [diff] [blame] | 440 | r = send_ipmi_message(m, sequence, netfn, lun, cmd, response[0], |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 441 | ((unsigned char*)response) + 1, resplen); |
| 442 | if (r < 0) |
| 443 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 444 | log<level::ERR>("Failed to send the response message"); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 445 | return -1; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 446 | } |
| 447 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 448 | return 0; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | //---------------------------------------------------------------------- |
| 452 | // handler_select |
| 453 | // Select all the files ending with with .so. in the given diretcory |
| 454 | // @d: dirent structure containing the file name |
| 455 | //---------------------------------------------------------------------- |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 456 | int handler_select(const struct dirent* entry) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 457 | { |
| 458 | // To hold ".so" from entry->d_name; |
| 459 | char dname_copy[4] = {0}; |
| 460 | |
| 461 | // We want to avoid checking for everything and isolate to the ones having |
Adriana Kobylak | 87e080b | 2016-07-10 13:16:53 -0500 | [diff] [blame] | 462 | // .so.* or .so in them. |
| 463 | // Check for versioned libraries .so.* |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 464 | if (strstr(entry->d_name, IPMI_PLUGIN_SONAME_EXTN)) |
Adriana Kobylak | 87e080b | 2016-07-10 13:16:53 -0500 | [diff] [blame] | 465 | { |
| 466 | return 1; |
| 467 | } |
| 468 | // Check for non versioned libraries .so |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 469 | else if (strstr(entry->d_name, IPMI_PLUGIN_EXTN)) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 470 | { |
| 471 | // It is possible that .so could be anywhere in the string but unlikely |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 472 | // But being careful here. Get the base address of the string, move |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 473 | // until end and come back 3 steps and that gets what we need. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 474 | strcpy(dname_copy, (entry->d_name + strlen(entry->d_name) - |
| 475 | strlen(IPMI_PLUGIN_EXTN))); |
| 476 | if (strcmp(dname_copy, IPMI_PLUGIN_EXTN) == 0) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 477 | { |
| 478 | return 1; |
| 479 | } |
| 480 | } |
| 481 | return 0; |
| 482 | } |
| 483 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 484 | // This will do a dlopen of every .so in ipmi_lib_path and will dlopen |
| 485 | // everything so that they will register a callback handler |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 486 | void ipmi_register_callback_handlers(const char* ipmi_lib_path) |
| 487 | { |
| 488 | // For walking the ipmi_lib_path |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 489 | struct dirent** handler_list; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 490 | int num_handlers = 0; |
| 491 | |
| 492 | // This is used to check and abort if someone tries to register a bad one. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 493 | void* lib_handler = NULL; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 494 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 495 | if (ipmi_lib_path == NULL) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 496 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 497 | log<level::ERR>("No handlers to be registered for ipmi.. Aborting"); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 498 | assert(0); |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | // 1: Open ipmi_lib_path. Its usually "/usr/lib/phosphor-host-ipmid" |
| 503 | // 2: Scan the directory for the files that end with .so |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 504 | // 3: For each one of them, just do a 'dlopen' so that they register |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 505 | // the handlers for callback routines. |
| 506 | |
| 507 | std::string handler_fqdn = ipmi_lib_path; |
Chris Austen | 120f732 | 2015-10-14 23:27:31 -0500 | [diff] [blame] | 508 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 509 | // Append a "/" since we need to add the name of the .so. If there is |
| 510 | // already a .so, adding one more is not any harm. |
| 511 | handler_fqdn += "/"; |
| 512 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 513 | num_handlers = |
| 514 | scandir(ipmi_lib_path, &handler_list, handler_select, alphasort); |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 515 | if (num_handlers < 0) |
| 516 | return; |
Jeremy Kerr | 5e8f85e | 2015-10-27 13:43:54 +0800 | [diff] [blame] | 517 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 518 | while (num_handlers--) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 519 | { |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 520 | handler_fqdn = ipmi_lib_path; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 521 | handler_fqdn += handler_list[num_handlers]->d_name; |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 522 | #ifdef __IPMI_DEBUG__ |
| 523 | log<level::DEBUG>("Registering handler", |
| 524 | entry("HANDLER=%s", handler_fqdn.c_str())); |
| 525 | #endif |
Chris Austen | 5403026 | 2015-10-13 12:30:46 -0500 | [diff] [blame] | 526 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 527 | lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW); |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 528 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 529 | if (lib_handler == NULL) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 530 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 531 | log<level::ERR>("ERROR opening", |
| 532 | entry("HANDLER=%s", handler_fqdn.c_str()), |
| 533 | entry("ERROR=%s", dlerror())); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 534 | } |
| 535 | // Wipe the memory allocated for this particular entry. |
| 536 | free(handler_list[num_handlers]); |
| 537 | } |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 538 | |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 539 | // Done with all registration. |
| 540 | free(handler_list); |
| 541 | } |
| 542 | |
| 543 | // TODO : What to be done on the memory that is given by dlopen ?. |
| 544 | return; |
| 545 | } |
| 546 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 547 | sd_bus* ipmid_get_sd_bus_connection(void) |
| 548 | { |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 549 | return bus; |
| 550 | } |
| 551 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 552 | sd_event* ipmid_get_sd_event_connection(void) |
| 553 | { |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 554 | return events; |
| 555 | } |
| 556 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 557 | sd_bus_slot* ipmid_get_sd_bus_slot(void) |
| 558 | { |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 559 | return ipmid_slot; |
| 560 | } |
| 561 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 562 | // Calls host command manager to do the right thing for the command |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 563 | void ipmid_send_cmd_to_host(CommandHandler&& cmd) |
| 564 | { |
| 565 | return cmdManager->execute(std::move(cmd)); |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 566 | } |
| 567 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 568 | cmdManagerPtr& ipmid_get_host_cmd_manager() |
| 569 | { |
| 570 | return cmdManager; |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 571 | } |
| 572 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 573 | sdbusPtr& ipmid_get_sdbus_plus_handler() |
| 574 | { |
| 575 | return sdbusp; |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 576 | } |
| 577 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 578 | int main(int argc, char* argv[]) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 579 | { |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 580 | int r; |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 581 | unsigned long tvalue; |
| 582 | int c; |
| 583 | |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 584 | // This file and subsequient switch is for turning on levels |
| 585 | // of trace |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 586 | ipmicmddetails = ipmiio = ipmidbus = fopen("/dev/null", "w"); |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 587 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 588 | while ((c = getopt(argc, argv, "h:d:")) != -1) |
| 589 | switch (c) |
| 590 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 591 | case 'd': |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 592 | tvalue = strtoul(optarg, NULL, 16); |
| 593 | if (1 & tvalue) |
| 594 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 595 | ipmiio = stdout; |
| 596 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 597 | if (2 & tvalue) |
| 598 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 599 | ipmidbus = stdout; |
| 600 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 601 | if (4 & tvalue) |
| 602 | { |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 603 | ipmicmddetails = stdout; |
| 604 | } |
| 605 | break; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 606 | case 'h': |
| 607 | case '?': |
Chris Austen | 9949731 | 2015-10-22 13:00:16 -0500 | [diff] [blame] | 608 | print_usage(); |
| 609 | return 1; |
| 610 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 611 | |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 612 | /* Connect to system bus */ |
| 613 | r = sd_bus_open_system(&bus); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 614 | if (r < 0) |
| 615 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 616 | log<level::ERR>("Failed to connect to system bus", |
| 617 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 618 | goto finish; |
| 619 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 620 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 621 | /* Get an sd event handler */ |
| 622 | r = sd_event_default(&events); |
| 623 | if (r < 0) |
| 624 | { |
| 625 | log<level::ERR>("Failure to create sd_event handler", |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 626 | entry("ERRNO=0x%X", -r)); |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 627 | goto finish; |
| 628 | } |
| 629 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 630 | // Now create the Host Bound Command manager. Need sdbusplus |
| 631 | // to use the generated bindings |
| 632 | sdbusp = std::make_unique<sdbusplus::bus::bus>(bus); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 633 | cmdManager = |
| 634 | std::make_unique<phosphor::host::command::Manager>(*sdbusp, events); |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 635 | |
Peter Hanson | 4a58985 | 2017-06-07 17:40:45 -0700 | [diff] [blame] | 636 | // Activate OemRouter. |
| 637 | oem::mutableRouter()->activate(); |
| 638 | |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 639 | // Register all the handlers that provider implementation to IPMI commands. |
| 640 | ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH); |
| 641 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 642 | // Watch for BT messages |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 643 | r = sd_bus_add_match(bus, &ipmid_slot, FILTER, handle_ipmi_command, NULL); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 644 | if (r < 0) |
| 645 | { |
| 646 | log<level::ERR>("Failed: sd_bus_add_match", entry("FILTER=%s", FILTER), |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 647 | entry("ERRNO=0x%X", -r)); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 648 | goto finish; |
| 649 | } |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 650 | |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 651 | // Attach the bus to sd_event to service user requests |
| 652 | sd_bus_attach_event(bus, events, SD_EVENT_PRIORITY_NORMAL); |
| 653 | |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 654 | { |
| 655 | using namespace internal; |
| 656 | using namespace internal::cache; |
| 657 | sdbusplus::bus::bus dbus{bus}; |
| 658 | objects = std::make_unique<settings::Objects>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 659 | dbus, std::vector<settings::Interface>({restrictionModeIntf})); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 660 | // Initialize restricted mode |
| 661 | cache_restricted_mode(); |
| 662 | // Wait for changes on Restricted mode |
| 663 | sdbusplus::bus::match_t restrictedModeMatch( |
| 664 | dbus, |
| 665 | sdbusRule::propertiesChanged( |
Deepak Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [diff] [blame] | 666 | objects->map.at(restrictionModeIntf).front(), |
| 667 | restrictionModeIntf), |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 668 | handle_restricted_mode_change); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 669 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 670 | for (;;) |
| 671 | { |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 672 | /* Process requests */ |
| 673 | r = sd_event_run(events, (uint64_t)-1); |
| 674 | if (r < 0) |
| 675 | { |
| 676 | log<level::ERR>("Failure in processing request", |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 677 | entry("ERRNO=0x%X", -r)); |
Deepak Kodihalli | 84b3a08 | 2017-07-21 23:44:44 -0500 | [diff] [blame] | 678 | goto finish; |
| 679 | } |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | finish: |
Andrew Geissler | 93c679b | 2017-04-02 10:06:43 -0500 | [diff] [blame] | 684 | sd_event_unref(events); |
| 685 | sd_bus_detach_event(bus); |
vishwa | b9f559a | 2016-01-13 01:53:08 -0600 | [diff] [blame] | 686 | sd_bus_slot_unref(ipmid_slot); |
Chris Austen | 0ba649e | 2015-10-13 12:28:13 -0500 | [diff] [blame] | 687 | sd_bus_unref(bus); |
| 688 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 689 | } |