blob: ded9157e6b851f5c2dfe5a58f956cba5fc2cef17 [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
18const char *chassis_bus_name = "org.openbmc.control.Chassis";
19const char *chassis_object_name = "/org/openbmc/control/chassis0";
20const char *chassis_intf_name = "org.openbmc.control.Chassis";
21
shgoupfd84fbbf2015-12-17 10:05:51 +080022
Adriana Kobylak40814c62015-10-27 15:58:44 -050023void register_netfn_chassis_functions() __attribute__((constructor));
24
shgoupfd84fbbf2015-12-17 10:05:51 +080025// Host settings in dbus
26// Service name should be referenced by connection name got via object mapper
27const char *settings_object_name = "/org/openbmc/settings/host0";
28const char *settings_intf_name = "org.freedesktop.DBus.Properties";
29const char *host_intf_name = "org.openbmc.settings.Host";
30
ratagupta6f6bff2016-04-04 06:20:11 -050031int dbus_get_property(const char *name, char **buf)
shgoupfd84fbbf2015-12-17 10:05:51 +080032{
33 sd_bus_error error = SD_BUS_ERROR_NULL;
34 sd_bus_message *m = NULL;
35 sd_bus *bus = NULL;
36 char *temp_buf = NULL;
37 char *connection = NULL;
38 int r;
39
Brad Bishop35518682016-07-22 08:35:41 -040040 // Get the system bus where most system services are provided.
41 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +080042
Brad Bishop35518682016-07-22 08:35:41 -040043 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +080044 if (r < 0) {
45 fprintf(stderr, "Failed to get connection, return value: %d.\n", r);
46 goto finish;
47 }
48
shgoupfd84fbbf2015-12-17 10:05:51 +080049 /*
50 * Bus, service, object path, interface and method are provided to call
51 * the method.
52 * Signatures and input arguments are provided by the arguments at the
53 * end.
54 */
55 r = sd_bus_call_method(bus,
56 connection, /* service to contact */
57 settings_object_name, /* object path */
58 settings_intf_name, /* interface name */
59 "Get", /* method name */
60 &error, /* object to return error in */
61 &m, /* return message on success */
62 "ss", /* input signature */
63 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -050064 name); /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +080065
66 if (r < 0) {
67 fprintf(stderr, "Failed to issue method call: %s\n", error.message);
68 goto finish;
69 }
70
71 /*
72 * The output should be parsed exactly the same as the output formatting
73 * specified.
74 */
75 r = sd_bus_message_read(m, "v", "s", &temp_buf);
76 if (r < 0) {
77 fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r));
78 goto finish;
79 }
80
81 asprintf(buf, "%s", temp_buf);
82/* *buf = (char*) malloc(strlen(temp_buf));
83 if (*buf) {
84 strcpy(*buf, temp_buf);
85 }
86*/
87 printf("IPMID boot option property get: {%s}.\n", (char *) temp_buf);
88
89finish:
90 sd_bus_error_free(&error);
91 sd_bus_message_unref(m);
92 free(connection);
93
94 return r;
95}
96
ratagupta6f6bff2016-04-04 06:20:11 -050097int dbus_set_property(const char * name, const char *value)
shgoupfd84fbbf2015-12-17 10:05:51 +080098{
99 sd_bus_error error = SD_BUS_ERROR_NULL;
100 sd_bus_message *m = NULL;
101 sd_bus *bus = NULL;
102 char *connection = NULL;
103 int r;
104
Brad Bishop35518682016-07-22 08:35:41 -0400105 // Get the system bus where most system services are provided.
106 bus = ipmid_get_sd_bus_connection();
shgoupfd84fbbf2015-12-17 10:05:51 +0800107
Brad Bishop35518682016-07-22 08:35:41 -0400108 r = mapper_get_service(bus, settings_object_name, &connection);
shgoupfd84fbbf2015-12-17 10:05:51 +0800109 if (r < 0) {
110 fprintf(stderr, "Failed to get connection, return value: %d.\n", r);
111 goto finish;
112 }
113
shgoupfd84fbbf2015-12-17 10:05:51 +0800114 /*
115 * Bus, service, object path, interface and method are provided to call
116 * the method.
117 * Signatures and input arguments are provided by the arguments at the
118 * end.
119 */
120 r = sd_bus_call_method(bus,
121 connection, /* service to contact */
122 settings_object_name, /* object path */
123 settings_intf_name, /* interface name */
124 "Set", /* method name */
125 &error, /* object to return error in */
126 &m, /* return message on success */
127 "ssv", /* input signature */
128 host_intf_name, /* first argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500129 name, /* second argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800130 "s", /* third argument */
ratagupta6f6bff2016-04-04 06:20:11 -0500131 value); /* fourth argument */
shgoupfd84fbbf2015-12-17 10:05:51 +0800132
133 if (r < 0) {
134 fprintf(stderr, "Failed to issue method call: %s\n", error.message);
135 goto finish;
136 }
137
ratagupta6f6bff2016-04-04 06:20:11 -0500138 printf("IPMID boot option property set: {%s}.\n", value);
shgoupfd84fbbf2015-12-17 10:05:51 +0800139
140finish:
141 sd_bus_error_free(&error);
142 sd_bus_message_unref(m);
143 free(connection);
144
145 return r;
146}
147
Adriana Kobylak40814c62015-10-27 15:58:44 -0500148struct get_sys_boot_options_t {
149 uint8_t parameter;
150 uint8_t set;
151 uint8_t block;
152} __attribute__ ((packed));
153
shgoupfd84fbbf2015-12-17 10:05:51 +0800154struct get_sys_boot_options_response_t {
155 uint8_t version;
156 uint8_t parm;
157 uint8_t data[5];
158} __attribute__ ((packed));
159
160struct set_sys_boot_options_t {
161 uint8_t parameter;
162 uint8_t data[8];
163} __attribute__ ((packed));
164
Adriana Kobylak40814c62015-10-27 15:58:44 -0500165ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
166 ipmi_request_t request, ipmi_response_t response,
167 ipmi_data_len_t data_len, ipmi_context_t context)
168{
169 printf("Handling CHASSIS WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
170 // Status code.
171 ipmi_ret_t rc = IPMI_CC_OK;
172 *data_len = 0;
173 return rc;
174}
175
vishwa36993272015-11-20 12:43:49 -0600176//------------------------------------------------------------
177// Calls into Chassis Control Dbus object to do the power off
178//------------------------------------------------------------
Chris Austen7888c4d2015-12-03 15:26:20 -0600179int ipmi_chassis_power_control(const char *method)
vishwa36993272015-11-20 12:43:49 -0600180{
181 // sd_bus error
182 int rc = 0;
183
184 // SD Bus error report mechanism.
185 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
186
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();
193
194 rc = sd_bus_call_method(bus_type, // On the System Bus
195 chassis_bus_name, // Service to contact
196 chassis_object_name, // Object path
197 chassis_intf_name, // Interface name
Chris Austen7888c4d2015-12-03 15:26:20 -0600198 method, // Method to be called
vishwa36993272015-11-20 12:43:49 -0600199 &bus_error, // object to return error
200 &response, // Response buffer if any
201 NULL); // No input arguments
202 if(rc < 0)
203 {
204 fprintf(stderr,"ERROR initiating Power Off:[%s]\n",bus_error.message);
205 }
206 else
207 {
208 printf("Chassis Power Off initiated successfully\n");
209 }
210
211 sd_bus_error_free(&bus_error);
212 sd_bus_message_unref(response);
213
214 return rc;
215}
216
Chris Austen7888c4d2015-12-03 15:26:20 -0600217
vishwa36993272015-11-20 12:43:49 -0600218//----------------------------------------------------------------------
219// Chassis Control commands
220//----------------------------------------------------------------------
221ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
222 ipmi_request_t request, ipmi_response_t response,
223 ipmi_data_len_t data_len, ipmi_context_t context)
224{
225 // Error from power off.
226 int rc = 0;
227
228 // No response for this command.
229 *data_len = 0;
230
231 // Catch the actual operaton by peeking into request buffer
232 uint8_t chassis_ctrl_cmd = *(uint8_t *)request;
233 printf("Chassis Control Command: Operation:[0x%X]\n",chassis_ctrl_cmd);
234
235 switch(chassis_ctrl_cmd)
236 {
237 case CMD_POWER_OFF:
Chris Austen7888c4d2015-12-03 15:26:20 -0600238 rc = ipmi_chassis_power_control("powerOff");
vishwa36993272015-11-20 12:43:49 -0600239 break;
Chris Austen7888c4d2015-12-03 15:26:20 -0600240 case CMD_HARD_RESET:
241 rc = ipmi_chassis_power_control("reboot");
242 break;
vishwa36993272015-11-20 12:43:49 -0600243 default:
244 {
245 fprintf(stderr, "Invalid Chassis Control command:[0x%X] received\n",chassis_ctrl_cmd);
246 rc = -1;
247 }
248 }
249
250 return ( (rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK);
251}
252
shgoupfd84fbbf2015-12-17 10:05:51 +0800253struct bootOptionTypeMap_t {
254 uint8_t ipmibootflag;
255 char dbusname[8];
256};
257
258#define INVALID_STRING "Invalid"
259// dbus supports this list of boot devices.
260bootOptionTypeMap_t g_bootOptionTypeMap_t[] = {
261
262 {0x01, "Network"},
263 {0x02, "Disk"},
264 {0x03, "Safe"},
265 {0x05, "CDROM"},
266 {0x06, "Setup"},
267 {0x00, "Default"},
268 {0xFF, INVALID_STRING}
269};
270
271uint8_t get_ipmi_boot_option(char *p) {
272
273 bootOptionTypeMap_t *s = g_bootOptionTypeMap_t;
274
275 while (s->ipmibootflag != 0xFF) {
276 if (!strcmp(s->dbusname,p))
277 break;
278 s++;
279 }
280
281 if (!s->ipmibootflag)
282 printf("Failed to find Sensor Type %s\n", p);
283
284 return s->ipmibootflag;
285}
286
287char* get_boot_option_by_ipmi(uint8_t p) {
288
289 bootOptionTypeMap_t *s = g_bootOptionTypeMap_t;
290
291 while (s->ipmibootflag != 0xFF) {
292
293 if (s->ipmibootflag == p)
294 break;
295
296 s++;
297 }
298
299
300 if (!s->ipmibootflag)
301 printf("Failed to find Sensor Type 0x%x\n", p);
302
303 return s->dbusname;
304}
305
Adriana Kobylak40814c62015-10-27 15:58:44 -0500306ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
307 ipmi_request_t request, ipmi_response_t response,
308 ipmi_data_len_t data_len, ipmi_context_t context)
309{
shgoupfd84fbbf2015-12-17 10:05:51 +0800310 ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED;
311 char *p = NULL;
312 get_sys_boot_options_response_t *resp = (get_sys_boot_options_response_t *) response;
313 get_sys_boot_options_t *reqptr = (get_sys_boot_options_t*) request;
314 uint8_t s;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500315
316 printf("IPMI GET_SYS_BOOT_OPTIONS\n");
317
shgoupfd84fbbf2015-12-17 10:05:51 +0800318 memset(resp,0,sizeof(*resp));
319 resp->version = SET_PARM_VERSION;
320 resp->parm = 5;
ratagupta6f6bff2016-04-04 06:20:11 -0500321 resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500322
shgoupfd84fbbf2015-12-17 10:05:51 +0800323 *data_len = sizeof(*resp);
Adriana Kobylak40814c62015-10-27 15:58:44 -0500324
shgoupfd84fbbf2015-12-17 10:05:51 +0800325 /*
326 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
327 * This is the only parameter used by petitboot.
328 */
329 if (reqptr->parameter == 5) {
330
ratagupta6f6bff2016-04-04 06:20:11 -0500331 /* Get the boot device */
332 int r = dbus_get_property("boot_flags",&p);
shgoupfd84fbbf2015-12-17 10:05:51 +0800333
334 if (r < 0) {
ratagupta6f6bff2016-04-04 06:20:11 -0500335 fprintf(stderr, "Dbus get property(boot_flags) failed for get_sys_boot_options.\n");
shgoupfd84fbbf2015-12-17 10:05:51 +0800336 rc = IPMI_CC_UNSPECIFIED_ERROR;
337
338 } else {
339
340 s = get_ipmi_boot_option(p);
341 resp->data[1] = (s << 2);
342 rc = IPMI_CC_OK;
ratagupta6f6bff2016-04-04 06:20:11 -0500343
shgoupfd84fbbf2015-12-17 10:05:51 +0800344 }
345
ratagupta6f6bff2016-04-04 06:20:11 -0500346 if (p)
347 {
348 free(p);
349 p = NULL;
350 }
351
352 /* Get the boot policy */
353 r = dbus_get_property("boot_policy",&p);
354
355 if (r < 0) {
356 fprintf(stderr, "Dbus get property(boot_policy) failed for get_sys_boot_options.\n");
357 rc = IPMI_CC_UNSPECIFIED_ERROR;
358
359 } else {
360
361 printf("BootPolicy is[%s]", p);
362 resp->data[0] = (strncmp(p,"ONETIME",strlen("ONETIME"))==0) ?
363 SET_PARM_BOOT_FLAGS_VALID_ONE_TIME:
364 SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
365 rc = IPMI_CC_OK;
366
367 }
368
369
shgoupfd84fbbf2015-12-17 10:05:51 +0800370 } else {
Adriana Kobylak40814c62015-10-27 15:58:44 -0500371 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
shgoupfd84fbbf2015-12-17 10:05:51 +0800372 }
373
374 if (p)
375 free(p);
376
377 return rc;
378}
379
380
381
382ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
383 ipmi_request_t request, ipmi_response_t response,
384 ipmi_data_len_t data_len, ipmi_context_t context)
385{
386 ipmi_ret_t rc = IPMI_CC_OK;
387 char *s;
388
389 printf("IPMI SET_SYS_BOOT_OPTIONS\n");
390
391 set_sys_boot_options_t *reqptr = (set_sys_boot_options_t *) request;
392
393 // This IPMI command does not have any resposne data
394 *data_len = 0;
395
396 /* 000101
397 * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
398 * This is the only parameter used by petitboot.
399 */
400 if (reqptr->parameter == 5) {
401
402 s = get_boot_option_by_ipmi(((reqptr->data[1] & 0x3C) >> 2));
403
404 printf("%d: %s\n", __LINE__, s);
405 if (!strcmp(s,INVALID_STRING)) {
406
407 rc = IPMI_CC_PARM_NOT_SUPPORTED;
408
409 } else {
410
ratagupta6f6bff2016-04-04 06:20:11 -0500411 int r = dbus_set_property("boot_flags",s);
shgoupfd84fbbf2015-12-17 10:05:51 +0800412
413 if (r < 0) {
ratagupta6f6bff2016-04-04 06:20:11 -0500414 fprintf(stderr, "Dbus set property(boot_flags) failed for set_sys_boot_options.\n");
shgoupfd84fbbf2015-12-17 10:05:51 +0800415 rc = IPMI_CC_UNSPECIFIED_ERROR;
416 }
417 }
ratagupta6f6bff2016-04-04 06:20:11 -0500418
419 /* setting the boot policy */
420 s = (char *)(((reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) ==
421 SET_PARM_BOOT_FLAGS_PERMANENT) ?"PERMANENT":"ONETIME");
422
423 printf ( "\nBoot Policy is %s",s);
424 int r = dbus_set_property("boot_policy",s);
425
426 if (r < 0) {
427 fprintf(stderr, "Dbus set property(boot_policy) failed for set_sys_boot_options.\n");
428 rc = IPMI_CC_UNSPECIFIED_ERROR;
429 }
shgoupfd84fbbf2015-12-17 10:05:51 +0800430
431 } else {
432 fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
433 rc = IPMI_CC_PARM_NOT_SUPPORTED;
Adriana Kobylak40814c62015-10-27 15:58:44 -0500434 }
435
436 return rc;
437}
438
439void register_netfn_chassis_functions()
440{
441 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_WILDCARD);
442 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL, ipmi_chassis_wildcard);
443
444 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS);
445 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 -0500446
vishwa36993272015-11-20 12:43:49 -0600447 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL);
448 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL, ipmi_chassis_control);
shgoupfd84fbbf2015-12-17 10:05:51 +0800449
450 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS);
451 ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL, ipmi_chassis_set_sys_boot_options);
vishwa36993272015-11-20 12:43:49 -0600452}
shgoupfd84fbbf2015-12-17 10:05:51 +0800453