blob: 92d97520235d691b7f700273a163910580504dab [file] [log] [blame]
Adriana Kobylak40814c62015-10-27 15:58:44 -05001#include "chassishandler.h"
2#include "ipmid-api.h"
3#include <stdio.h>
4#include <string.h>
5#include <stdint.h>
Brad Bishop35518682016-07-22 08:35:41 -04006#include <mapper.h>
Adriana Kobylak40814c62015-10-27 15:58:44 -05007
ratagupta6f6bff2016-04-04 06:20:11 -05008
9//Defines
10#define SET_PARM_VERSION 1
11#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40 //boot flags data1 7th bit on
12#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80 //boot flags data1 8th bit on
13#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0 //boot flags data1 7 & 8 bit on
14
15
16
vishwa36993272015-11-20 12:43:49 -060017// OpenBMC Chassis Manager dbus framework
vishwa36993272015-11-20 12:43:49 -060018const char *chassis_object_name = "/org/openbmc/control/chassis0";
19const char *chassis_intf_name = "org.openbmc.control.Chassis";
20
shgoupfd84fbbf2015-12-17 10:05:51 +080021
Adriana Kobylak40814c62015-10-27 15:58:44 -050022void register_netfn_chassis_functions() __attribute__((constructor));
23
shgoupfd84fbbf2015-12-17 10:05:51 +080024// Host settings in dbus
25// Service name should be referenced by connection name got via object mapper
26const char *settings_object_name = "/org/openbmc/settings/host0";
27const char *settings_intf_name = "org.freedesktop.DBus.Properties";
28const char *host_intf_name = "org.openbmc.settings.Host";
29
ratagupta6f6bff2016-04-04 06:20:11 -050030int dbus_get_property(const char *name, char **buf)
shgoupfd84fbbf2015-12-17 10:05:51 +080031{
32 sd_bus_error error = SD_BUS_ERROR_NULL;
33 sd_bus_message *m = NULL;
34 sd_bus *bus = NULL;
35 char *temp_buf = NULL;
36 char *connection = NULL;
37 int r;
38
Brad Bishop35518682016-07-22 08:35:41 -040039 // Get the system bus where most system services are provided.
40 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +080041
Brad Bishop35518682016-07-22 08:35:41 -040042 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +080043 if (r < 0) {
Sergey Solomineb9b8142016-08-23 09:07:28 -050044 fprintf(stderr, "Failed to get connection, return value: %s.\n", strerror(-r));
shgoupfd84fbbf2015-12-17 10:05:51 +080045 goto finish;
46 }
47
shgoupfd84fbbf2015-12-17 10:05:51 +080048 /*
49 * Bus, service, object path, interface and method are provided to call
50 * the method.
51 * Signatures and input arguments are provided by the arguments at the
52 * end.
53 */
54 r = sd_bus_call_method(bus,
55 connection, /* service to contact */
56 settings_object_name, /* object path */
57 settings_intf_name, /* interface name */
58 "Get", /* method name */
59 &error, /* object to return error in */
60 &m, /* return message on success */
61 "ss", /* input signature */
62 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -050063 name); /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +080064
65 if (r < 0) {
66 fprintf(stderr, "Failed to issue method call: %s\n", error.message);
67 goto finish;
68 }
69
70 /*
71 * The output should be parsed exactly the same as the output formatting
72 * specified.
73 */
74 r = sd_bus_message_read(m, "v", "s", &temp_buf);
75 if (r < 0) {
76 fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r));
77 goto finish;
78 }
79
80 asprintf(buf, "%s", temp_buf);
81/* *buf = (char*) malloc(strlen(temp_buf));
82 if (*buf) {
83 strcpy(*buf, temp_buf);
84 }
85*/
86 printf("IPMID boot option property get: {%s}.\n", (char *) temp_buf);
87
88finish:
89 sd_bus_error_free(&error);
90 sd_bus_message_unref(m);
91 free(connection);
92
93 return r;
94}
95
ratagupta6f6bff2016-04-04 06:20:11 -050096int dbus_set_property(const char * name, const char *value)
shgoupfd84fbbf2015-12-17 10:05:51 +080097{
98 sd_bus_error error = SD_BUS_ERROR_NULL;
99 sd_bus_message *m = NULL;
100 sd_bus *bus = NULL;
101 char *connection = NULL;
102 int r;
103
Brad Bishop35518682016-07-22 08:35:41 -0400104 // Get the system bus where most system services are provided.
105 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800106
Brad Bishop35518682016-07-22 08:35:41 -0400107 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +0800108 if (r < 0) {
Sergey Solomineb9b8142016-08-23 09:07:28 -0500109 fprintf(stderr, "Failed to get connection, return value: %s.\n", strerror(-r));
shgoupfd84fbbf2015-12-17 10:05:51 +0800110 goto finish;
111 }
112
shgoupfd84fbbf2015-12-17 10:05:51 +0800113 /*
114 * Bus, service, object path, interface and method are provided to call
115 * the method.
116 * Signatures and input arguments are provided by the arguments at the
117 * end.
118 */
119 r = sd_bus_call_method(bus,
120 connection, /* service to contact */
121 settings_object_name, /* object path */
122 settings_intf_name, /* interface name */
123 "Set", /* method name */
124 &error, /* object to return error in */
125 &m, /* return message on success */
126 "ssv", /* input signature */
127 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500128 name, /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800129 "s", /* third argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500130 value); /* fourth argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800131
132 if (r < 0) {
133 fprintf(stderr, "Failed to issue method call: %s\n", error.message);
134 goto finish;
135 }
136
ratagupta6f6bff2016-04-04 06:20:11 -0500137 printf("IPMID boot option property set: {%s}.\n", value);
shgoupfd84fbbf2015-12-17 10:05:51 +0800138
139finish:
140 sd_bus_error_free(&error);
141 sd_bus_message_unref(m);
142 free(connection);
143
144 return r;
145}
146
Adriana Kobylak40814c62015-10-27 15:58:44 -0500147struct get_sys_boot_options_t {
148 uint8_t parameter;
149 uint8_t set;
150 uint8_t block;
151} __attribute__ ((packed));
152
shgoupfd84fbbf2015-12-17 10:05:51 +0800153struct get_sys_boot_options_response_t {
154 uint8_t version;
155 uint8_t parm;
156 uint8_t data[5];
157} __attribute__ ((packed));
158
159struct set_sys_boot_options_t {
160 uint8_t parameter;
161 uint8_t data[8];
162} __attribute__ ((packed));
163
Adriana Kobylak40814c62015-10-27 15:58:44 -0500164ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
165 ipmi_request_t request, ipmi_response_t response,
166 ipmi_data_len_t data_len, ipmi_context_t context)
167{
168 printf("Handling CHASSIS WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
169 // Status code.
170 ipmi_ret_t rc = IPMI_CC_OK;
171 *data_len = 0;
172 return rc;
173}
174
vishwa36993272015-11-20 12:43:49 -0600175//------------------------------------------------------------
176// Calls into Chassis Control Dbus object to do the power off
177//------------------------------------------------------------
Chris Austen7888c4d2015-12-03 15:26:20 -0600178int ipmi_chassis_power_control(const char *method)
vishwa36993272015-11-20 12:43:49 -0600179{
180 // sd_bus error
181 int rc = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500182 char *busname = NULL;
vishwa36993272015-11-20 12:43:49 -0600183
Sergey Solomineb9b8142016-08-23 09:07:28 -0500184 // SD Bus error report mechanism.
185 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
vishwa36993272015-11-20 12:43:49 -0600186
187 // Response from the call. Although there is no response for this call,
188 // obligated to mention this to make compiler happy.
189 sd_bus_message *response = NULL;
190
191 // Gets a hook onto either a SYSTEM or SESSION bus
192 sd_bus *bus_type = ipmid_get_sd_bus_connection();
Sergey Solomineb9b8142016-08-23 09:07:28 -0500193 rc = mapper_get_service(bus_type, chassis_object_name, &busname);
194 if (rc < 0) {
195 fprintf(stderr, "Failed to get bus name, return value: %s.\n", strerror(-rc));
196 goto finish;
197 }
vishwa36993272015-11-20 12:43:49 -0600198 rc = sd_bus_call_method(bus_type, // On the System Bus
Sergey Solomineb9b8142016-08-23 09:07:28 -0500199 busname, // Service to contact
vishwa36993272015-11-20 12:43:49 -0600200 chassis_object_name, // Object path
201 chassis_intf_name, // Interface name
Chris Austen7888c4d2015-12-03 15:26:20 -0600202 method, // Method to be called
vishwa36993272015-11-20 12:43:49 -0600203 &bus_error, // object to return error
204 &response, // Response buffer if any
205 NULL); // No input arguments
206 if(rc < 0)
207 {
208 fprintf(stderr,"ERROR initiating Power Off:[%s]\n",bus_error.message);
209 }
210 else
211 {
212 printf("Chassis Power Off initiated successfully\n");
213 }
214
Sergey Solomineb9b8142016-08-23 09:07:28 -0500215finish:
vishwa36993272015-11-20 12:43:49 -0600216 sd_bus_error_free(&bus_error);
217 sd_bus_message_unref(response);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500218 free(busname);
vishwa36993272015-11-20 12:43:49 -0600219
Sergey Solomineb9b8142016-08-23 09:07:28 -0500220 return rc;
vishwa36993272015-11-20 12:43:49 -0600221}
222
Chris Austen7888c4d2015-12-03 15:26:20 -0600223
vishwa36993272015-11-20 12:43:49 -0600224//----------------------------------------------------------------------
225// Chassis Control commands
226//----------------------------------------------------------------------
227ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
228 ipmi_request_t request, ipmi_response_t response,
229 ipmi_data_len_t data_len, ipmi_context_t context)
230{
231 // Error from power off.
232 int rc = 0;
233
234 // No response for this command.
235 *data_len = 0;
236
237 // Catch the actual operaton by peeking into request buffer
238 uint8_t chassis_ctrl_cmd = *(uint8_t *)request;
239 printf("Chassis Control Command: Operation:[0x%X]\n",chassis_ctrl_cmd);
240
241 switch(chassis_ctrl_cmd)
242 {
243 case CMD_POWER_OFF:
Chris Austen7888c4d2015-12-03 15:26:20 -0600244 rc = ipmi_chassis_power_control("powerOff");
vishwa36993272015-11-20 12:43:49 -0600245 break;
Chris Austen7888c4d2015-12-03 15:26:20 -0600246 case CMD_HARD_RESET:
247 rc = ipmi_chassis_power_control("reboot");
248 break;
vishwa36993272015-11-20 12:43:49 -0600249 default:
250 {
251 fprintf(stderr, "Invalid Chassis Control command:[0x%X] received\n",chassis_ctrl_cmd);
252 rc = -1;
253 }
254 }
255
256 return ( (rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
257}
258
shgoupfd84fbbf2015-12-17 10:05:51 +0800259struct bootOptionTypeMap_t {
260 uint8_t ipmibootflag;
261 char dbusname[8];
262};
263
264#define INVALID_STRING "Invalid"
265// dbus supports this list of boot devices.
266bootOptionTypeMap_t g_bootOptionTypeMap_t[] = {
267
268 {0x01, "Network"},
269 {0x02, "Disk"},
270 {0x03, "Safe"},
271 {0x05, "CDROM"},
272 {0x06, "Setup"},
273 {0x00, "Default"},
274 {0xFF, INVALID_STRING}
275};
276
277uint8_t get_ipmi_boot_option(char *p) {
278
279 bootOptionTypeMap_t *s = g_bootOptionTypeMap_t;
280
281 while (s->ipmibootflag != 0xFF) {
282 if (!strcmp(s->dbusname,p))
283 break;
284 s++;
285 }
286
287 if (!s->ipmibootflag)
288 printf("Failed to find Sensor Type %s\n", p);
289
290 return s->ipmibootflag;
291}
292
293char* get_boot_option_by_ipmi(uint8_t p) {
294
295 bootOptionTypeMap_t *s = g_bootOptionTypeMap_t;
296
297 while (s->ipmibootflag != 0xFF) {
298
299 if (s->ipmibootflag == p)
300 break;
301
302 s++;
303 }
304
305
306 if (!s->ipmibootflag)
307 printf("Failed to find Sensor Type 0x%x\n", p);
308
309 return s->dbusname;
310}
311
Adriana Kobylak40814c62015-10-27 15:58:44 -0500312ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
313 ipmi_request_t request, ipmi_response_t response,
314 ipmi_data_len_t data_len, ipmi_context_t context)
315{
shgoupfd84fbbf2015-12-17 10:05:51 +0800316 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
317 char *p = NULL;
318 get_sys_boot_options_response_t *resp = (get_sys_boot_options_response_t *) response;
319 get_sys_boot_options_t *reqptr = (get_sys_boot_options_t*) request;
320 uint8_t s;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500321
322 printf("IPMI GET_SYS_BOOT_OPTIONS\n");
323
shgoupfd84fbbf2015-12-17 10:05:51 +0800324 memset(resp,0,sizeof(*resp));
325 resp->version = SET_PARM_VERSION;
326 resp->parm = 5;
ratagupta6f6bff2016-04-04 06:20:11 -0500327 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500328
shgoupfd84fbbf2015-12-17 10:05:51 +0800329 *data_len = sizeof(*resp);
Adriana Kobylak40814c62015-10-27 15:58:44 -0500330
shgoupfd84fbbf2015-12-17 10:05:51 +0800331 /*
332 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
333 * This is the only parameter used by petitboot.
334 */
335 if (reqptr->parameter == 5) {
336
ratagupta6f6bff2016-04-04 06:20:11 -0500337 /* Get the boot device */
338 int r = dbus_get_property("boot_flags",&p);
shgoupfd84fbbf2015-12-17 10:05:51 +0800339
340 if (r < 0) {
ratagupta6f6bff2016-04-04 06:20:11 -0500341 fprintf(stderr, "Dbus get property(boot_flags) failed for get_sys_boot_options.\n");
shgoupfd84fbbf2015-12-17 10:05:51 +0800342 rc = IPMI_CC_UNSPECIFIED_ERROR;
343
344 } else {
345
346 s = get_ipmi_boot_option(p);
347 resp->data[1] = (s << 2);
348 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -0500349
shgoupfd84fbbf2015-12-17 10:05:51 +0800350 }
351
ratagupta6f6bff2016-04-04 06:20:11 -0500352 if (p)
353 {
354 free(p);
355 p = NULL;
356 }
357
358 /* Get the boot policy */
359 r = dbus_get_property("boot_policy",&p);
360
361 if (r < 0) {
362 fprintf(stderr, "Dbus get property(boot_policy) failed for get_sys_boot_options.\n");
363 rc = IPMI_CC_UNSPECIFIED_ERROR;
364
365 } else {
366
367 printf("BootPolicy is[%s]", p);
368 resp->data[0] = (strncmp(p,"ONETIME",strlen("ONETIME"))==0) ?
369 SET_PARM_BOOT_FLAGS_VALID_ONE_TIME:
370 SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
371 rc = IPMI_CC_OK;
372
373 }
374
375
shgoupfd84fbbf2015-12-17 10:05:51 +0800376 } else {
Adriana Kobylak40814c62015-10-27 15:58:44 -0500377 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
shgoupfd84fbbf2015-12-17 10:05:51 +0800378 }
379
380 if (p)
381 free(p);
382
383 return rc;
384}
385
386
387
388ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
389 ipmi_request_t request, ipmi_response_t response,
390 ipmi_data_len_t data_len, ipmi_context_t context)
391{
392 ipmi_ret_t rc = IPMI_CC_OK;
393 char *s;
394
395 printf("IPMI SET_SYS_BOOT_OPTIONS\n");
396
397 set_sys_boot_options_t *reqptr = (set_sys_boot_options_t *) request;
398
399 // This IPMI command does not have any resposne data
400 *data_len = 0;
401
402 /* 000101
403 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
404 * This is the only parameter used by petitboot.
405 */
406 if (reqptr->parameter == 5) {
407
408 s = get_boot_option_by_ipmi(((reqptr->data[1] & 0x3C) >> 2));
409
410 printf("%d: %s\n", __LINE__, s);
411 if (!strcmp(s,INVALID_STRING)) {
412
413 rc = IPMI_CC_PARM_NOT_SUPPORTED;
414
415 } else {
416
ratagupta6f6bff2016-04-04 06:20:11 -0500417 int r = dbus_set_property("boot_flags",s);
shgoupfd84fbbf2015-12-17 10:05:51 +0800418
419 if (r < 0) {
ratagupta6f6bff2016-04-04 06:20:11 -0500420 fprintf(stderr, "Dbus set property(boot_flags) failed for set_sys_boot_options.\n");
shgoupfd84fbbf2015-12-17 10:05:51 +0800421 rc = IPMI_CC_UNSPECIFIED_ERROR;
422 }
423 }
ratagupta6f6bff2016-04-04 06:20:11 -0500424
425 /* setting the boot policy */
426 s = (char *)(((reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
427 SET_PARM_BOOT_FLAGS_PERMANENT) ?"PERMANENT":"ONETIME");
428
429 printf ( "\nBoot Policy is %s",s);
430 int r = dbus_set_property("boot_policy",s);
431
432 if (r < 0) {
433 fprintf(stderr, "Dbus set property(boot_policy) failed for set_sys_boot_options.\n");
434 rc = IPMI_CC_UNSPECIFIED_ERROR;
435 }
shgoupfd84fbbf2015-12-17 10:05:51 +0800436
437 } else {
438 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
439 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500440 }
441
442 return rc;
443}
444
445void register_netfn_chassis_functions()
446{
447 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_WILDCARD);
448 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL, ipmi_chassis_wildcard);
449
450 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS);
451 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL, ipmi_chassis_get_sys_boot_options);
Adriana Kobylak40814c62015-10-27 15:58:44 -0500452
vishwa36993272015-11-20 12:43:49 -0600453 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL);
454 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL, ipmi_chassis_control);
shgoupfd84fbbf2015-12-17 10:05:51 +0800455
456 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS);
457 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL, ipmi_chassis_set_sys_boot_options);
vishwa36993272015-11-20 12:43:49 -0600458}
shgoupfd84fbbf2015-12-17 10:05:51 +0800459