blob: 2689117091ea4046cb42f09f2448327bfbe41b14 [file] [log] [blame]
vishwabmcba0bd5f2015-09-30 16:50:23 +05301#include <stdio.h>
2#include <dlfcn.h>
3#include <iostream>
4#include <unistd.h>
5#include <assert.h>
6#include <dirent.h>
Chris Austen0ba649e2015-10-13 12:28:13 -05007#include <systemd/sd-bus.h>
vishwabmcba0bd5f2015-09-30 16:50:23 +05308#include <string.h>
9#include <stdlib.h>
10#include <map>
Patrick Williams53a360e2016-08-12 22:01:02 -050011#include "ipmid.hpp"
Chris Austen0ba649e2015-10-13 12:28:13 -050012#include <sys/time.h>
13#include <errno.h>
Brad Bishop35518682016-07-22 08:35:41 -040014#include <mapper.h>
Chris Austen0012e9b2015-10-22 01:37:46 -050015#include "sensorhandler.h"
Tom Joseph9a61b4f2016-07-11 06:56:11 -050016#include <vector>
17#include <algorithm>
18#include <iterator>
Patrick Williams4b9efaa2016-08-12 21:59:51 -050019#include <ipmiwhitelist.hpp>
Chris Austen0ba649e2015-10-13 12:28:13 -050020
Chris Austen0ba649e2015-10-13 12:28:13 -050021sd_bus *bus = NULL;
vishwab9f559a2016-01-13 01:53:08 -060022sd_bus_slot *ipmid_slot = NULL;
Chris Austen30195fa2015-11-13 14:39:19 -060023
Tom Joseph9a61b4f2016-07-11 06:56:11 -050024// Initialise restricted mode to true
25bool restricted_mode = true;
26
Chris Austen41a4b312015-10-25 03:45:42 -050027FILE *ipmiio, *ipmidbus, *ipmicmddetails;
vishwabmcba0bd5f2015-09-30 16:50:23 +053028
Chris Austen99497312015-10-22 13:00:16 -050029void print_usage(void) {
30 fprintf(stderr, "Options: [-d mask]\n");
31 fprintf(stderr, " mask : 0x01 - Print ipmi packets\n");
32 fprintf(stderr, " mask : 0x02 - Print DBUS operations\n");
33 fprintf(stderr, " mask : 0x04 - Print ipmi command details\n");
34 fprintf(stderr, " mask : 0xFF - Print all trace\n");
35}
36
Tom Joseph9a61b4f2016-07-11 06:56:11 -050037// Host settings in DBUS
Tom Joseph9a61b4f2016-07-11 06:56:11 -050038constexpr char settings_host_object[] = "/org/openbmc/settings/host0";
39constexpr char settings_host_intf[] = "org.freedesktop.DBus.Properties";
40
Jeremy Kerre41081f2015-10-27 12:11:36 +080041const char * DBUS_INTF = "org.openbmc.HostIpmi";
vishwabmcba0bd5f2015-09-30 16:50:23 +053042
Jeremy Kerre41081f2015-10-27 12:11:36 +080043const char * FILTER = "type='signal',interface='org.openbmc.HostIpmi',member='ReceivedMessage'";
Tom Joseph9a61b4f2016-07-11 06:56:11 -050044constexpr char RESTRICTED_MODE_FILTER[] = "type='signal',interface='org.freedesktop.DBus.Properties',path='/org/openbmc/settings/host0'";
Chris Austen0ba649e2015-10-13 12:28:13 -050045
vishwabmcba0bd5f2015-09-30 16:50:23 +053046typedef std::pair<ipmi_netfn_t, ipmi_cmd_t> ipmi_fn_cmd_t;
47typedef std::pair<ipmid_callback_t, ipmi_context_t> ipmi_fn_context_t;
48
49// Global data structure that contains the IPMI command handler's registrations.
50std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map;
51
Nan Li36c0cb62016-03-31 11:16:08 +080052// IPMI Spec, shared Reservation ID.
53unsigned short g_sel_reserve = 0xFFFF;
54
55unsigned short get_sel_reserve_id(void)
56{
57 return g_sel_reserve;
58}
Chris Austen0ba649e2015-10-13 12:28:13 -050059
Chris Austen0ba649e2015-10-13 12:28:13 -050060#ifndef HEXDUMP_COLS
61#define HEXDUMP_COLS 16
62#endif
63
Chris Austen99497312015-10-22 13:00:16 -050064void hexdump(FILE *s, void *mem, size_t len)
Chris Austen0ba649e2015-10-13 12:28:13 -050065{
66 unsigned int i, j;
Chris Austen120f7322015-10-14 23:27:31 -050067
Chris Austen0ba649e2015-10-13 12:28:13 -050068 for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
69 {
70 /* print offset */
71 if(i % HEXDUMP_COLS == 0)
72 {
Chris Austen99497312015-10-22 13:00:16 -050073 fprintf(s,"0x%06x: ", i);
Chris Austen0ba649e2015-10-13 12:28:13 -050074 }
Chris Austen120f7322015-10-14 23:27:31 -050075
Chris Austen0ba649e2015-10-13 12:28:13 -050076 /* print hex data */
77 if(i < len)
78 {
Chris Austen99497312015-10-22 13:00:16 -050079 fprintf(s,"%02x ", 0xFF & ((char*)mem)[i]);
Chris Austen0ba649e2015-10-13 12:28:13 -050080 }
81 else /* end of block, just aligning for ASCII dump */
82 {
Chris Austen99497312015-10-22 13:00:16 -050083 fprintf(s," ");
Chris Austen0ba649e2015-10-13 12:28:13 -050084 }
Chris Austen120f7322015-10-14 23:27:31 -050085
Chris Austen0ba649e2015-10-13 12:28:13 -050086 /* print ASCII dump */
87 if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
88 {
89 for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
90 {
91 if(j >= len) /* end of block, not really printing */
92 {
Chris Austen99497312015-10-22 13:00:16 -050093 fputc(' ', s);
Chris Austen0ba649e2015-10-13 12:28:13 -050094 }
95 else if(isprint(((char*)mem)[j])) /* printable char */
96 {
Chris Austen99497312015-10-22 13:00:16 -050097 fputc(0xFF & ((char*)mem)[j], s);
Chris Austen0ba649e2015-10-13 12:28:13 -050098 }
99 else /* other char */
100 {
Chris Austen99497312015-10-22 13:00:16 -0500101 fputc('.',s);
Chris Austen0ba649e2015-10-13 12:28:13 -0500102 }
103 }
Chris Austen99497312015-10-22 13:00:16 -0500104 fputc('\n',s);
Chris Austen0ba649e2015-10-13 12:28:13 -0500105 }
106 }
107}
108
109
vishwabmcba0bd5f2015-09-30 16:50:23 +0530110// Method that gets called by shared libraries to get their command handlers registered
111void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
112 ipmi_context_t context, ipmid_callback_t handler)
113{
114 // Pack NetFn and Command in one.
115 auto netfn_and_cmd = std::make_pair(netfn, cmd);
116
117 // Pack Function handler and Data in another.
118 auto handler_and_context = std::make_pair(handler, context);
119
120 // Check if the registration has already been made..
121 auto iter = g_ipmid_router_map.find(netfn_and_cmd);
122 if(iter != g_ipmid_router_map.end())
123 {
124 fprintf(stderr,"ERROR : Duplicate registration for NetFn [0x%X], Cmd:[0x%X]\n",netfn, cmd);
125 }
126 else
127 {
128 // This is a fresh registration.. Add it to the map.
129 g_ipmid_router_map.emplace(netfn_and_cmd, handler_and_context);
130 }
131
132 return;
133}
134
135// Looks at the map and calls corresponding handler functions.
136ipmi_ret_t ipmi_netfn_router(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request,
137 ipmi_response_t response, ipmi_data_len_t data_len)
138{
139 // return from the Command handlers.
140 ipmi_ret_t rc = IPMI_CC_INVALID;
141
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500142 // If restricted mode is true and command is not whitelisted, don't
143 // execute the command
144 if(restricted_mode)
145 {
146 if (!std::binary_search(whitelist.cbegin(), whitelist.cend(),
147 std::make_pair(netfn, cmd)))
148 {
149 printf("Net function:[0x%X], Command:[0x%X] is not whitelisted\n",
150 netfn, cmd);
151 rc = IPMI_CC_INSUFFICIENT_PRIVILEGE;
152 memcpy(response, &rc, IPMI_CC_LEN);
153 *data_len = IPMI_CC_LEN;
154 return rc;
155 }
156 }
157
vishwabmcba0bd5f2015-09-30 16:50:23 +0530158 // Walk the map that has the registered handlers and invoke the approprite
159 // handlers for matching commands.
160 auto iter = g_ipmid_router_map.find(std::make_pair(netfn, cmd));
161 if(iter == g_ipmid_router_map.end())
162 {
Chris Austen99497312015-10-22 13:00:16 -0500163 fprintf(stderr, "No registered handlers for NetFn:[0x%X], Cmd:[0x%X]"
vishwabmcba0bd5f2015-09-30 16:50:23 +0530164 " trying Wilcard implementation \n",netfn, cmd);
165
166 // Now that we did not find any specific [NetFn,Cmd], tuple, check for
167 // NetFn, WildCard command present.
168 iter = g_ipmid_router_map.find(std::make_pair(netfn, IPMI_CMD_WILDCARD));
169 if(iter == g_ipmid_router_map.end())
170 {
Chris Austen99497312015-10-22 13:00:16 -0500171 fprintf(stderr, "No Registered handlers for NetFn:[0x%X],Cmd:[0x%X]\n",netfn, IPMI_CMD_WILDCARD);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530172
173 // Respond with a 0xC1
174 memcpy(response, &rc, IPMI_CC_LEN);
175 *data_len = IPMI_CC_LEN;
176 return rc;
177 }
178 }
179
180#ifdef __IPMI_DEBUG__
181 // We have either a perfect match -OR- a wild card atleast,
182 printf("Calling Net function:[0x%X], Command:[0x%X]\n", netfn, cmd);
183#endif
184
185 // Extract the map data onto appropriate containers
186 auto handler_and_context = iter->second;
187
188 // Creating a pointer type casted to char* to make sure we advance 1 byte
189 // when we advance pointer to next's address. advancing void * would not
190 // make sense.
191 char *respo = &((char *)response)[IPMI_CC_LEN];
192
193 // Response message from the plugin goes into a byte post the base response
194 rc = (handler_and_context.first) (netfn, cmd, request, respo,
195 data_len, handler_and_context.second);
Chris Austen120f7322015-10-14 23:27:31 -0500196
vishwabmcba0bd5f2015-09-30 16:50:23 +0530197 // Now copy the return code that we got from handler and pack it in first
198 // byte.
199 memcpy(response, &rc, IPMI_CC_LEN);
Chris Austen120f7322015-10-14 23:27:31 -0500200
vishwabmcba0bd5f2015-09-30 16:50:23 +0530201 // Data length is now actual data + completion code.
202 *data_len = *data_len + IPMI_CC_LEN;
203
204 return rc;
205}
206
vishwabmcba0bd5f2015-09-30 16:50:23 +0530207
vishwabmcba0bd5f2015-09-30 16:50:23 +0530208
vishwabmcba0bd5f2015-09-30 16:50:23 +0530209
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800210static int send_ipmi_message(sd_bus_message *req, unsigned char seq, unsigned char netfn, unsigned char lun, unsigned char cmd, unsigned char cc, unsigned char *buf, unsigned char len) {
vishwabmcba0bd5f2015-09-30 16:50:23 +0530211
Chris Austen0ba649e2015-10-13 12:28:13 -0500212 sd_bus_error error = SD_BUS_ERROR_NULL;
213 sd_bus_message *reply = NULL, *m=NULL;
Jeremy Kerre41081f2015-10-27 12:11:36 +0800214 const char *dest, *path;
Chris Austen0ba649e2015-10-13 12:28:13 -0500215 int r, pty;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530216
Jeremy Kerre41081f2015-10-27 12:11:36 +0800217 dest = sd_bus_message_get_sender(req);
218 path = sd_bus_message_get_path(req);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530219
Jeremy Kerre41081f2015-10-27 12:11:36 +0800220 r = sd_bus_message_new_method_call(bus,&m,dest,path,DBUS_INTF,"sendMessage");
Chris Austen0ba649e2015-10-13 12:28:13 -0500221 if (r < 0) {
222 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
223 return -1;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530224 }
225
vishwabmcba0bd5f2015-09-30 16:50:23 +0530226
Chris Austenabfb5e82015-10-13 12:29:24 -0500227 // Responses in IPMI require a bit set. So there ya go...
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800228 netfn |= 0x01;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530229
Chris Austen0ba649e2015-10-13 12:28:13 -0500230
231 // Add the bytes needed for the methods to be called
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800232 r = sd_bus_message_append(m, "yyyyy", seq, netfn, lun, cmd, cc);
Chris Austen0ba649e2015-10-13 12:28:13 -0500233 if (r < 0) {
234 fprintf(stderr, "Failed add the netfn and others : %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600235 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500236 }
Chris Austen120f7322015-10-14 23:27:31 -0500237
Chris Austen0ba649e2015-10-13 12:28:13 -0500238 r = sd_bus_message_append_array(m, 'y', buf, len);
239 if (r < 0) {
240 fprintf(stderr, "Failed to add the string of response bytes: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600241 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500242 }
243
244
245
246 // Call the IPMI responder on the bus so the message can be sent to the CEC
247 r = sd_bus_call(bus, m, 0, &error, &reply);
248 if (r < 0) {
Chris Austen169395e2015-12-02 20:56:15 -0600249 fprintf(stderr, "Failed to call the method: %s\n", strerror(-r));
Chris Austen6bd23962015-12-07 21:31:48 -0600250 fprintf(stderr, "Dest: %s, Path: %s\n", dest, path);
Chris Austen169395e2015-12-02 20:56:15 -0600251 goto final;
Chris Austen0ba649e2015-10-13 12:28:13 -0500252 }
253
254 r = sd_bus_message_read(reply, "x", &pty);
Chris Austen0ba649e2015-10-13 12:28:13 -0500255 if (r < 0) {
256 fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r));
Chris Austen0ba649e2015-10-13 12:28:13 -0500257 }
258
Chris Austen169395e2015-12-02 20:56:15 -0600259final:
Chris Austen0ba649e2015-10-13 12:28:13 -0500260 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600261 m = sd_bus_message_unref(m);
262 reply = sd_bus_message_unref(reply);
Chris Austen0ba649e2015-10-13 12:28:13 -0500263
Chris Austen0ba649e2015-10-13 12:28:13 -0500264 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Chris Austen0ba649e2015-10-13 12:28:13 -0500265}
266
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500267void cache_restricted_mode()
268{
269 sd_bus *bus = ipmid_get_sd_bus_connection();
270 sd_bus_message *reply = NULL;
271 sd_bus_error error = SD_BUS_ERROR_NULL;
272 int rc = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500273 char *busname = NULL;
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500274
Sergey Solomineb9b8142016-08-23 09:07:28 -0500275 rc = mapper_get_service(bus, settings_host_object, &busname);
276 if (rc < 0) {
277 fprintf(stderr, "Failed to get HOST busname: %s\n", strerror(-rc));
278 goto cleanup;
279 }
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500280 rc = sd_bus_call_method(bus,
Sergey Solomineb9b8142016-08-23 09:07:28 -0500281 busname,
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500282 settings_host_object,
283 settings_host_intf,
284 "Get",
285 &error,
286 &reply,
287 "ss",
Sergey Solomineb9b8142016-08-23 09:07:28 -0500288 "org.openbmc.settings.Host",
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500289 "restricted_mode");
290 if(rc < 0)
291 {
292 fprintf(stderr, "Failed sd_bus_call_method method for restricted mode: %s\n",
293 strerror(-rc));
294 goto cleanup;
295 }
296
297 rc = sd_bus_message_read(reply, "v", "b", &restricted_mode);
298 if(rc < 0)
299 {
300 fprintf(stderr, "Failed to parse response message for restricted mode: %s\n",
301 strerror(-rc));
302 // Fail-safe to restricted mode
303 restricted_mode = true;
304 }
305
306 printf("Restricted mode = %d\n", restricted_mode);
307
308cleanup:
309 sd_bus_error_free(&error);
310 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500311 free(busname);
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500312}
313
314static int handle_restricted_mode_change(sd_bus_message *m, void *user_data,
315 sd_bus_error *ret_error)
316{
317 cache_restricted_mode();
318 return 0;
319}
320
Chris Austen0ba649e2015-10-13 12:28:13 -0500321static int handle_ipmi_command(sd_bus_message *m, void *user_data, sd_bus_error
322 *ret_error) {
323 int r = 0;
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800324 unsigned char sequence, netfn, lun, cmd;
Chris Austen0ba649e2015-10-13 12:28:13 -0500325 const void *request;
326 size_t sz;
327 size_t resplen =MAX_IPMI_BUFFER;
328 unsigned char response[MAX_IPMI_BUFFER];
329
Chris Austen0ba649e2015-10-13 12:28:13 -0500330 memset(response, 0, MAX_IPMI_BUFFER);
331
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800332 r = sd_bus_message_read(m, "yyyy", &sequence, &netfn, &lun, &cmd);
Chris Austen0ba649e2015-10-13 12:28:13 -0500333 if (r < 0) {
334 fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r));
335 return -1;
336 }
337
338 r = sd_bus_message_read_array(m, 'y', &request, &sz );
339 if (r < 0) {
340 fprintf(stderr, "Failed to parse signal message: %s\n", strerror(-r));
341 return -1;
342 }
343
Chris Austen99497312015-10-22 13:00:16 -0500344 fprintf(ipmiio, "IPMI Incoming: Seq 0x%02x, NetFn 0x%02x, CMD: 0x%02x \n", sequence, netfn, cmd);
345 hexdump(ipmiio, (void*)request, sz);
Chris Austen0ba649e2015-10-13 12:28:13 -0500346
Chris Austen120f7322015-10-14 23:27:31 -0500347 // Allow the length field to be used for both input and output of the
Chris Austen0ba649e2015-10-13 12:28:13 -0500348 // ipmi call
349 resplen = sz;
350
Chris Austen120f7322015-10-14 23:27:31 -0500351 // Now that we have parsed the entire byte array from the caller
vishwabmcba0bd5f2015-09-30 16:50:23 +0530352 // we can call the ipmi router to do the work...
Chris Austen0ba649e2015-10-13 12:28:13 -0500353 r = ipmi_netfn_router(netfn, cmd, (void *)request, (void *)response, &resplen);
354 if(r != 0)
vishwabmcba0bd5f2015-09-30 16:50:23 +0530355 {
Chris Austen0ba649e2015-10-13 12:28:13 -0500356 fprintf(stderr,"ERROR:[0x%X] handling NetFn:[0x%X], Cmd:[0x%X]\n",r, netfn, cmd);
Nan Li80be4b92016-05-23 19:30:49 +0800357
tomjose7ec0add2016-06-27 07:59:28 -0500358 if(r < 0) {
Nan Li80be4b92016-05-23 19:30:49 +0800359 response[0] = IPMI_CC_UNSPECIFIED_ERROR;
360 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530361 }
362
Chris Austen99497312015-10-22 13:00:16 -0500363 fprintf(ipmiio, "IPMI Response:\n");
364 hexdump(ipmiio, (void*)response, resplen);
vishwabmcba0bd5f2015-09-30 16:50:23 +0530365
Chris Austen0ba649e2015-10-13 12:28:13 -0500366 // Send the response buffer from the ipmi command
Jeremy Kerr2564b1a2015-10-27 13:37:17 +0800367 r = send_ipmi_message(m, sequence, netfn, lun, cmd, response[0],
368 ((unsigned char *)response) + 1, resplen - 1);
Chris Austen0ba649e2015-10-13 12:28:13 -0500369 if (r < 0) {
370 fprintf(stderr, "Failed to send the response message\n");
371 return -1;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530372 }
373
vishwabmcba0bd5f2015-09-30 16:50:23 +0530374
Chris Austen0ba649e2015-10-13 12:28:13 -0500375 return 0;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530376}
377
Chris Austen0ba649e2015-10-13 12:28:13 -0500378
vishwabmcba0bd5f2015-09-30 16:50:23 +0530379//----------------------------------------------------------------------
380// handler_select
381// Select all the files ending with with .so. in the given diretcory
382// @d: dirent structure containing the file name
383//----------------------------------------------------------------------
384int handler_select(const struct dirent *entry)
385{
386 // To hold ".so" from entry->d_name;
387 char dname_copy[4] = {0};
388
389 // We want to avoid checking for everything and isolate to the ones having
Adriana Kobylak87e080b2016-07-10 13:16:53 -0500390 // .so.* or .so in them.
391 // Check for versioned libraries .so.*
392 if(strstr(entry->d_name, IPMI_PLUGIN_SONAME_EXTN))
393 {
394 return 1;
395 }
396 // Check for non versioned libraries .so
397 else if(strstr(entry->d_name, IPMI_PLUGIN_EXTN))
vishwabmcba0bd5f2015-09-30 16:50:23 +0530398 {
399 // It is possible that .so could be anywhere in the string but unlikely
Chris Austen120f7322015-10-14 23:27:31 -0500400 // But being careful here. Get the base address of the string, move
vishwabmcba0bd5f2015-09-30 16:50:23 +0530401 // until end and come back 3 steps and that gets what we need.
402 strcpy(dname_copy, (entry->d_name + strlen(entry->d_name)-strlen(IPMI_PLUGIN_EXTN)));
403 if(strcmp(dname_copy, IPMI_PLUGIN_EXTN) == 0)
404 {
405 return 1;
406 }
407 }
408 return 0;
409}
410
411// This will do a dlopen of every .so in ipmi_lib_path and will dlopen everything so that they will
Chris Austen120f7322015-10-14 23:27:31 -0500412// register a callback handler
vishwabmcba0bd5f2015-09-30 16:50:23 +0530413void ipmi_register_callback_handlers(const char* ipmi_lib_path)
414{
415 // For walking the ipmi_lib_path
416 struct dirent **handler_list;
417 int num_handlers = 0;
418
419 // This is used to check and abort if someone tries to register a bad one.
420 void *lib_handler = NULL;
421
422 if(ipmi_lib_path == NULL)
423 {
424 fprintf(stderr,"ERROR; No handlers to be registered for ipmi.. Aborting\n");
425 assert(0);
426 }
427 else
428 {
429 // 1: Open ipmi_lib_path. Its usually "/usr/lib/phosphor-host-ipmid"
430 // 2: Scan the directory for the files that end with .so
Chris Austen120f7322015-10-14 23:27:31 -0500431 // 3: For each one of them, just do a 'dlopen' so that they register
vishwabmcba0bd5f2015-09-30 16:50:23 +0530432 // the handlers for callback routines.
433
434 std::string handler_fqdn = ipmi_lib_path;
Chris Austen120f7322015-10-14 23:27:31 -0500435
vishwabmcba0bd5f2015-09-30 16:50:23 +0530436 // Append a "/" since we need to add the name of the .so. If there is
437 // already a .so, adding one more is not any harm.
438 handler_fqdn += "/";
439
440 num_handlers = scandir(ipmi_lib_path, &handler_list, handler_select, alphasort);
Nan Li36c0cb62016-03-31 11:16:08 +0800441 if (num_handlers < 0)
442 return;
Jeremy Kerr5e8f85e2015-10-27 13:43:54 +0800443
vishwabmcba0bd5f2015-09-30 16:50:23 +0530444 while(num_handlers--)
445 {
Chris Austen54030262015-10-13 12:30:46 -0500446 handler_fqdn = ipmi_lib_path;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530447 handler_fqdn += handler_list[num_handlers]->d_name;
Chris Austen54030262015-10-13 12:30:46 -0500448 printf("Registering handler:[%s]\n",handler_fqdn.c_str());
449
vishwabmcba0bd5f2015-09-30 16:50:23 +0530450 lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW);
Nan Li36c0cb62016-03-31 11:16:08 +0800451
vishwabmcba0bd5f2015-09-30 16:50:23 +0530452 if(lib_handler == NULL)
453 {
Chris Austen120f7322015-10-14 23:27:31 -0500454 fprintf(stderr,"ERROR opening [%s]: %s\n",
455 handler_fqdn.c_str(), dlerror());
vishwabmcba0bd5f2015-09-30 16:50:23 +0530456 }
457 // Wipe the memory allocated for this particular entry.
458 free(handler_list[num_handlers]);
459 }
Nan Li36c0cb62016-03-31 11:16:08 +0800460
vishwabmcba0bd5f2015-09-30 16:50:23 +0530461 // Done with all registration.
462 free(handler_list);
463 }
464
465 // TODO : What to be done on the memory that is given by dlopen ?.
466 return;
467}
468
Chris Austen30195fa2015-11-13 14:39:19 -0600469sd_bus *ipmid_get_sd_bus_connection(void) {
470 return bus;
471}
472
vishwab9f559a2016-01-13 01:53:08 -0600473sd_bus_slot *ipmid_get_sd_bus_slot(void) {
474 return ipmid_slot;
475}
476
vishwabmcba0bd5f2015-09-30 16:50:23 +0530477int main(int argc, char *argv[])
478{
Chris Austen0ba649e2015-10-13 12:28:13 -0500479 int r;
Chris Austen99497312015-10-22 13:00:16 -0500480 unsigned long tvalue;
481 int c;
482
483
484
485 // This file and subsequient switch is for turning on levels
486 // of trace
487 ipmicmddetails = ipmiio = ipmidbus = fopen("/dev/null", "w");
488
489 while ((c = getopt (argc, argv, "h:d:")) != -1)
490 switch (c) {
491 case 'd':
492 tvalue = strtoul(optarg, NULL, 16);
493 if (1&tvalue) {
494 ipmiio = stdout;
495 }
496 if (2&tvalue) {
497 ipmidbus = stdout;
498 }
499 if (4&tvalue) {
500 ipmicmddetails = stdout;
501 }
502 break;
503 case 'h':
504 case '?':
505 print_usage();
506 return 1;
507 }
Chris Austen0ba649e2015-10-13 12:28:13 -0500508
509
Chris Austen0ba649e2015-10-13 12:28:13 -0500510 /* Connect to system bus */
511 r = sd_bus_open_system(&bus);
512 if (r < 0) {
513 fprintf(stderr, "Failed to connect to system bus: %s\n",
514 strerror(-r));
515 goto finish;
516 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530517
Chris Austen30195fa2015-11-13 14:39:19 -0600518 // Register all the handlers that provider implementation to IPMI commands.
519 ipmi_register_callback_handlers(HOST_IPMI_LIB_PATH);
520
vishwa36993272015-11-20 12:43:49 -0600521 // Watch for BT messages
vishwab9f559a2016-01-13 01:53:08 -0600522 r = sd_bus_add_match(bus, &ipmid_slot, FILTER, handle_ipmi_command, NULL);
Chris Austen0ba649e2015-10-13 12:28:13 -0500523 if (r < 0) {
524 fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), FILTER);
525 goto finish;
526 }
vishwabmcba0bd5f2015-09-30 16:50:23 +0530527
Tom Joseph9a61b4f2016-07-11 06:56:11 -0500528 // Wait for changes on Restricted mode
529 r = sd_bus_add_match(bus, &ipmid_slot, RESTRICTED_MODE_FILTER, handle_restricted_mode_change, NULL);
530 if (r < 0) {
531 fprintf(stderr, "Failed: sd_bus_add_match: %s : %s\n", strerror(-r), RESTRICTED_MODE_FILTER);
532 goto finish;
533 }
534
535 // Initialise restricted mode
536 cache_restricted_mode();
vishwabmcba0bd5f2015-09-30 16:50:23 +0530537
Chris Austen0ba649e2015-10-13 12:28:13 -0500538 for (;;) {
539 /* Process requests */
Chris Austen0ba649e2015-10-13 12:28:13 -0500540 r = sd_bus_process(bus, NULL);
541 if (r < 0) {
542 fprintf(stderr, "Failed to process bus: %s\n", strerror(-r));
543 goto finish;
544 }
545 if (r > 0) {
546 continue;
547 }
548
549 r = sd_bus_wait(bus, (uint64_t) - 1);
550 if (r < 0) {
551 fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r));
552 goto finish;
553 }
554 }
555
556finish:
vishwab9f559a2016-01-13 01:53:08 -0600557 sd_bus_slot_unref(ipmid_slot);
Chris Austen0ba649e2015-10-13 12:28:13 -0500558 sd_bus_unref(bus);
559 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
560
vishwabmcba0bd5f2015-09-30 16:50:23 +0530561}
Chris Austenb071c702015-10-15 00:00:09 -0500562
Chris Austen0012e9b2015-10-22 01:37:46 -0500563// Use a lookup table to find the interface name of a specific sensor
564// This will be used until an alternative is found. this is the first
565// step for mapping IPMI
566int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
Chris Austenb071c702015-10-15 00:00:09 -0500567
Joel Stanleyf19539e2015-11-25 17:24:05 +1030568 char *str1;
Chris Austen0012e9b2015-10-22 01:37:46 -0500569 sd_bus_error error = SD_BUS_ERROR_NULL;
570 sd_bus_message *reply = NULL, *m=NULL;
Chris Austenb071c702015-10-15 00:00:09 -0500571
572
Chris Austen0012e9b2015-10-22 01:37:46 -0500573 int r;
574
Chris Austen0012e9b2015-10-22 01:37:46 -0500575 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
576 if (r < 0) {
577 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
578 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
579 interface->bus, interface->path, interface->interface);
vishwa1eaea4f2016-02-26 11:57:40 -0600580 goto final;
Chris Austen0012e9b2015-10-22 01:37:46 -0500581 }
582
583 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
584 if (r < 0) {
585 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
586 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
587 interface->bus, interface->path, interface->interface);
vishwa1eaea4f2016-02-26 11:57:40 -0600588 goto final;
Chris Austen0012e9b2015-10-22 01:37:46 -0500589 }
590
591 r = sd_bus_call(bus, m, 0, &error, &reply);
592 if (r < 0) {
593 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
594 goto final;
595 }
596
597 r = sd_bus_message_read(reply, "v", "s", &str1) ;
598 if (r < 0) {
599 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
600 goto final;
601 }
602
603 strcpy(property_value, str1);
604
605final:
606
607 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600608 m = sd_bus_message_unref(m);
609 reply = sd_bus_message_unref(reply);
Chris Austen0012e9b2015-10-22 01:37:46 -0500610
611 return r;
612}
Chris Austenb071c702015-10-15 00:00:09 -0500613
Chris Austen41a4b312015-10-25 03:45:42 -0500614
Chris Austenb071c702015-10-15 00:00:09 -0500615// Use a lookup table to find the interface name of a specific sensor
616// This will be used until an alternative is found. this is the first
617// step for mapping IPMI
618int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
Brad Bishop35518682016-07-22 08:35:41 -0400619 char *busname = NULL;
620 const char *iface = "org.openbmc.managers.System";
Chris Austenb071c702015-10-15 00:00:09 -0500621 const char *objname = "/org/openbmc/managers/System";
Brad Bishop35518682016-07-22 08:35:41 -0400622 char *str1 = NULL, *str2, *str3;
Chris Austenb071c702015-10-15 00:00:09 -0500623 sd_bus_error error = SD_BUS_ERROR_NULL;
vishwa1eaea4f2016-02-26 11:57:40 -0600624 sd_bus_message *reply = NULL;
Chris Austenb071c702015-10-15 00:00:09 -0500625
626
627 int r;
Brad Bishop35518682016-07-22 08:35:41 -0400628 r = mapper_get_service(bus, objname, &busname);
629 if (r < 0) {
630 fprintf(stderr, "Failed to get system manager busname: %s\n", strerror(-r));
631 goto final;
632 }
Chris Austenb071c702015-10-15 00:00:09 -0500633
Brad Bishop35518682016-07-22 08:35:41 -0400634 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
vishwa1eaea4f2016-02-26 11:57:40 -0600635 &error, &reply, "sy", type, num);
Chris Austenb071c702015-10-15 00:00:09 -0500636 if (r < 0) {
637 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
Chris Austenb071c702015-10-15 00:00:09 -0500638 goto final;
639 }
640
Brad Bishop35518682016-07-22 08:35:41 -0400641 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
Chris Austenb071c702015-10-15 00:00:09 -0500642 if (r < 0) {
643 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
644 goto final;
645 }
646
Brad Bishop35518682016-07-22 08:35:41 -0400647 r = mapper_get_service(bus, str2, &str1);
648 if (r < 0) {
649 fprintf(stderr, "Failed to get item busname: %s\n", strerror(-r));
650 goto final;
651 }
652
Chris Austenb071c702015-10-15 00:00:09 -0500653 strncpy(interface->bus, str1, MAX_DBUS_PATH);
654 strncpy(interface->path, str2, MAX_DBUS_PATH);
655 strncpy(interface->interface, str3, MAX_DBUS_PATH);
656
657 interface->sensornumber = num;
658
Chris Austenb071c702015-10-15 00:00:09 -0500659final:
660
661 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600662 reply = sd_bus_message_unref(reply);
Brad Bishop35518682016-07-22 08:35:41 -0400663 free(busname);
664 free(str1);
Chris Austenb071c702015-10-15 00:00:09 -0500665
666 return r;
667}
668
669
670/////////////////////////////////////////////////////////////////////
671//
672// Routines used by ipmi commands wanting to interact on the dbus
673//
674/////////////////////////////////////////////////////////////////////
Chris Austen10ccc0f2015-12-10 18:27:04 -0600675int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
Chris Austen0130d6e2015-10-15 22:32:36 -0500676
677
678 dbus_interface_t a;
679 int r;
680 sd_bus_error error = SD_BUS_ERROR_NULL;
Joel Stanleyf19539e2015-11-25 17:24:05 +1030681 sd_bus_message *m=NULL;
Chris Austen0130d6e2015-10-15 22:32:36 -0500682
Chris Austen99497312015-10-22 13:00:16 -0500683 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
Chris Austen0130d6e2015-10-15 22:32:36 -0500684 number, method, value);
685
686 r = find_openbmc_path("SENSOR", number, &a);
687
Nan Li36deb762016-05-12 10:23:41 +0800688 if (r < 0) {
689 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
690 return 0;
691 }
692
Chris Austen0130d6e2015-10-15 22:32:36 -0500693 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
694 if (r < 0) {
695 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600696 goto final;
Chris Austen0130d6e2015-10-15 22:32:36 -0500697 }
698
699 r = sd_bus_message_append(m, "v", "s", value);
700 if (r < 0) {
701 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600702 goto final;
Chris Austen0130d6e2015-10-15 22:32:36 -0500703 }
704
705
Chris Austen0130d6e2015-10-15 22:32:36 -0500706 r = sd_bus_call(bus, m, 0, &error, NULL);
707 if (r < 0) {
Chris Austen10ccc0f2015-12-10 18:27:04 -0600708 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
Chris Austen0130d6e2015-10-15 22:32:36 -0500709 }
710
vishwa1eaea4f2016-02-26 11:57:40 -0600711final:
Chris Austen0130d6e2015-10-15 22:32:36 -0500712 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600713 m = sd_bus_message_unref(m);
Chris Austen0130d6e2015-10-15 22:32:36 -0500714
715 return 0;
716}
Chris Austen10ccc0f2015-12-10 18:27:04 -0600717int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
718
719
720 dbus_interface_t a;
721 int r;
722 sd_bus_error error = SD_BUS_ERROR_NULL;
723 sd_bus_message *m=NULL;
724
725 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
726 number, method, value);
727
728 r = find_openbmc_path("SENSOR", number, &a);
729
Nan Li36deb762016-05-12 10:23:41 +0800730 if (r < 0) {
731 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
732 return 0;
733 }
734
Chris Austen10ccc0f2015-12-10 18:27:04 -0600735 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
736 if (r < 0) {
737 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600738 goto final;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600739 }
740
Adriana Kobylak93125982016-03-01 12:48:10 -0600741 r = sd_bus_message_append(m, "v", "i", value);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600742 if (r < 0) {
743 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600744 goto final;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600745 }
746
747
748 r = sd_bus_call(bus, m, 0, &error, NULL);
749 if (r < 0) {
750 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
751 }
752
vishwa1eaea4f2016-02-26 11:57:40 -0600753final:
Chris Austen10ccc0f2015-12-10 18:27:04 -0600754 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600755 m = sd_bus_message_unref(m);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600756
757 return 0;
vishwa1eaea4f2016-02-26 11:57:40 -0600758}