blob: d3df33035e1636e499530856c210452c50f68a9f [file] [log] [blame]
vishwabmcba0bd5f2015-09-30 16:50:23 +05301#include "apphandler.h"
2#include "ipmid-api.h"
Chris Austen6caf28b2015-10-13 12:40:40 -05003#include "ipmid.H"
vishwabmcba0bd5f2015-09-30 16:50:23 +05304#include <stdio.h>
5#include <string.h>
Chris Austen6caf28b2015-10-13 12:40:40 -05006#include <stdint.h>
Adriana Kobylak3a552e12015-10-19 16:11:00 -05007#include <systemd/sd-bus.h>
8
9extern sd_bus *bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053010
Chris Austen6caf28b2015-10-13 12:40:40 -050011void register_netfn_app_functions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053012
vishwa36993272015-11-20 12:43:49 -060013//---------------------------------------------------------------------
14// Called by Host on seeing a SMS_ATN bit set. Return a hardcoded
15// value of 0x2 indicating we need Host read some data.
16//-------------------------------------------------------------------
17ipmi_ret_t ipmi_app_get_msg_flags(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
18 ipmi_request_t request, ipmi_response_t response,
19 ipmi_data_len_t data_len, ipmi_context_t context)
20{
21 // Generic return from IPMI commands.
22 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -050023
vishwa36993272015-11-20 12:43:49 -060024 printf("IPMI APP GET MSG FLAGS returning with [bit:2] set\n");
25
26 // From IPMI spec V2.0 for Get Message Flags Command :
27 // bit:[1] from LSB : 1b = Event Message Buffer Full.
28 // Return as 0 if Event Message Buffer is not supported,
29 // or when the Event Message buffer is disabled.
30 // TODO. For now. assume its not disabled and send "0x2" anyway:
31
32 uint8_t set_event_msg_buffer_full = 0x2;
33 *data_len = sizeof(set_event_msg_buffer_full);
34
35 // Pack the actual response
36 memcpy(response, &set_event_msg_buffer_full, *data_len);
37
38 return rc;
39}
40
41//-------------------------------------------------------------------
42// Called by Host post response from Get_Message_Flags
43//-------------------------------------------------------------------
Adriana Kobylak3a552e12015-10-19 16:11:00 -050044ipmi_ret_t ipmi_app_read_event(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
45 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050046 ipmi_data_len_t data_len, ipmi_context_t context)
47{
48 ipmi_ret_t rc = IPMI_CC_OK;
vishwa36993272015-11-20 12:43:49 -060049 printf("IPMI APP READ EVENT command received\n");
Chris Austen6caf28b2015-10-13 12:40:40 -050050
vishwa36993272015-11-20 12:43:49 -060051 // TODO : For now, this is catering only to the Soft Power Off via OEM SEL
52 // mechanism. If we need to make this generically used for some
53 // other conditions, then we can take advantage of context pointer.
54
55 struct oem_sel_timestamped soft_off = {0};
56 *data_len = sizeof(struct oem_sel_timestamped);
57
58 // either id[0] -or- id[1] can be filled in. We will use id[0]
59 soft_off.id[0] = SEL_OEM_ID_0;
60 soft_off.id[1] = SEL_OEM_ID_0;
61 soft_off.type = SEL_RECORD_TYPE_OEM;
62
63 // Following 3 bytes are from IANA Manufactre_Id field. See below
64 soft_off.manuf_id[0]= 0x41;
65 soft_off.manuf_id[1]= 0xA7;
66 soft_off.manuf_id[2]= 0x00;
67
68 // per IPMI spec NetFuntion for OEM
69 soft_off.netfun = 0x3A;
70
71 // Mechanism to kick start soft shutdown.
72 soft_off.cmd = CMD_POWER;
73 soft_off.data[0] = SOFT_OFF;
74
75 // All '0xFF' since unused.
76 memset(&soft_off.data[1], 0xFF, 3);
77
78 // Pack the actual response
79 memcpy(response, &soft_off, *data_len);
Chris Austen6caf28b2015-10-13 12:40:40 -050080 return rc;
Chris Austen6caf28b2015-10-13 12:40:40 -050081}
82
Adriana Kobylak3a552e12015-10-19 16:11:00 -050083ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
84 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050085 ipmi_data_len_t data_len, ipmi_context_t context)
86{
87 ipmi_ret_t rc = IPMI_CC_OK;
88 *data_len = 0;
89
90 printf("IPMI SET ACPI STATE Ignoring for now\n");
91 return rc;
92}
93
Adriana Kobylak3a552e12015-10-19 16:11:00 -050094ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
95 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050096 ipmi_data_len_t data_len, ipmi_context_t context)
97{
98 ipmi_ret_t rc = IPMI_CC_OK;
99
Patrick Williams76b51f62015-10-28 12:14:56 -0500100 // TODO:
101 // This value is the IANA number assigned to "IBM Platform Firmware
102 // Division", which is also used by our service processor. We may want
103 // a different number or at least a different version?
Chris Austen6caf28b2015-10-13 12:40:40 -0500104 uint8_t str[] = {0x00, 0, 1, 1,2, 0xD, 0x41, 0xA7, 0x00, 0x43, 0x40};
105
106 // Data length
107 *data_len = sizeof(str);
108
109 // Pack the actual response
110 memcpy(response, &str, *data_len);
111 return rc;
112}
113
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500114ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
115 ipmi_request_t request, ipmi_response_t response,
116 ipmi_data_len_t data_len, ipmi_context_t context)
117{
118 const char *busname = "org.openbmc.control.Chassis";
119 const char *objname = "/org/openbmc/control/chassis0";
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600120 const char *iface = "org.freedesktop.DBus.Properties";
121 const char *chassis_iface = "org.openbmc.control.Chassis";
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500122 sd_bus_message *reply = NULL, *m = NULL;
123 sd_bus_error error = SD_BUS_ERROR_NULL;
124 int r = 0;
125 char *uuid = NULL;
126
127 // Status code.
128 ipmi_ret_t rc = IPMI_CC_OK;
129 *data_len = 0;
130
131 printf("IPMI GET DEVICE GUID\n");
132
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600133 // Call Get properties method with the interface and property name
134 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"Get");
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500135 if (r < 0) {
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600136 fprintf(stderr, "Failed to add the Get method object: %s\n", strerror(-r));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500137 return IPMI_CC_UNSPECIFIED_ERROR;
138 }
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600139 r = sd_bus_message_append(m, "ss", chassis_iface, "uuid");
140 if (r < 0) {
141 fprintf(stderr, "Failed to append arguments: %s\n", strerror(-r));
142 return -1;
143 }
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500144 r = sd_bus_call(bus, m, 0, &error, &reply);
145 if (r < 0) {
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600146 fprintf(stderr, "Failed to call the Get method: %s\n", strerror(-r));
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500147 return IPMI_CC_UNSPECIFIED_ERROR;
148 }
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600149 r = sd_bus_message_read(reply, "v", "s", &uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500150 if (r < 0) {
151 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
152 return IPMI_CC_RESPONSE_ERROR;
153 }
154 if (uuid == NULL)
155 {
156 fprintf(stderr, "Failed to get a valid response: %s", strerror(-r));
157 return IPMI_CC_RESPONSE_ERROR;
158 }
159
160 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
161 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
162 // Ex: 0x2332fc2c40e66298e511f2782395a361
163
164 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
165 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
166 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
167 int i = 0;
168 char *tokptr = NULL;
169
170 // Traverse the UUID
171 char* id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
172
173 if (id_octet == NULL)
174 { // Error
175 fprintf(stderr, "Unexpected UUID format: %s", uuid);
176 return IPMI_CC_RESPONSE_ERROR;
177 }
178
179 while (id_octet != NULL)
180 {
181 // Calculate the octet string size since it varies
182 // Divide it by 2 for the array size since 1 byte is built from 2 chars
183 int tmp_size = strlen(id_octet)/2;
184
185 for(i = 0; i < tmp_size; i++)
186 {
187 char tmp_array[3]; // Holder of the 2 chars that will become a byte
188 tmp_array[3] = '\0';
189 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
190
191 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
192 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
193 resp_loc--;
194 id_octet+=2; // Finished with the 2 chars, advance
195 }
196 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
197 }
198
199 // Data length
200 *data_len = resp_size;
201
202 // Pack the actual response
203 memcpy(response, &resp_uuid, *data_len);
204
205 sd_bus_error_free(&error);
206 sd_bus_message_unref(m);
207
208 return rc;
209}
Chris Austen6caf28b2015-10-13 12:40:40 -0500210
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500211ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
212 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530213 ipmi_data_len_t data_len, ipmi_context_t context)
214{
215 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
216
217 // Status code.
218 ipmi_ret_t rc = IPMI_CC_OK;
219
Chris Austen6caf28b2015-10-13 12:40:40 -0500220 uint8_t str[] = {0x01, MAX_IPMI_BUFFER, MAX_IPMI_BUFFER, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530221
222 // Data length
223 *data_len = sizeof(str);
224
225 // Pack the actual response
226 memcpy(response, &str, *data_len);
227
228 return rc;
229}
230
Chris Austen6caf28b2015-10-13 12:40:40 -0500231
232struct set_wd_data_t {
233 uint8_t t_use;
234 uint8_t t_action;
235 uint8_t preset;
236 uint8_t flags;
237 uint8_t ls;
238 uint8_t ms;
239} __attribute__ ((packed));
240
241
242
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500243ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
244 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500245 ipmi_data_len_t data_len, ipmi_context_t context)
246{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500247 const char *busname = "org.openbmc.watchdog.Host";
Chris Austen454acfe2015-10-29 23:03:34 -0500248 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500249 const char *iface = "org.openbmc.Watchdog";
250 sd_bus_message *reply = NULL, *m = NULL;
251 sd_bus_error error = SD_BUS_ERROR_NULL;
252 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500253
254 set_wd_data_t *reqptr = (set_wd_data_t*) request;
255 uint16_t timer = 0;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500256 uint32_t timer_ms = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500257 // Status code.
258 ipmi_ret_t rc = IPMI_CC_OK;
259
260 *data_len = 0;
261
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500262 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500263 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500264 // Get timer value in ms
265 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500266
267 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
268
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500269 // Set watchdog timer
270 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"set");
271 if (r < 0) {
272 fprintf(stderr, "Failed to add the set method object: %s\n", strerror(-r));
273 return -1;
274 }
275 r = sd_bus_message_append(m, "i", timer_ms);
276 if (r < 0) {
277 fprintf(stderr, "Failed to add timer value: %s\n", strerror(-r));
278 return -1;
279 }
280 r = sd_bus_call(bus, m, 0, &error, &reply);
281 if (r < 0) {
282 fprintf(stderr, "Failed to call the set method: %s\n", strerror(-r));
283 return -1;
284 }
285
Adriana Kobylak0896be32015-10-22 13:27:23 -0500286 // Stop the current watchdog if any
287 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"stop");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500288 if (r < 0) {
289 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
290 return -1;
291 }
292 r = sd_bus_call(bus, m, 0, &error, &reply);
293 if (r < 0) {
294 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
295 return -1;
296 }
297
Adriana Kobylak0896be32015-10-22 13:27:23 -0500298 // Start the watchdog if requested
299 if (reqptr->t_use & 0x40)
300 {
301 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"start");
302 if (r < 0) {
303 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
304 return -1;
305 }
306 r = sd_bus_call(bus, m, 0, &error, &reply);
307 if (r < 0) {
308 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
309 return -1;
310 }
311 }
312
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500313 sd_bus_error_free(&error);
314 sd_bus_message_unref(m);
Chris Austen6caf28b2015-10-13 12:40:40 -0500315
316 return rc;
317}
318
319
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500320ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
321 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500322 ipmi_data_len_t data_len, ipmi_context_t context)
323{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500324 const char *busname = "org.openbmc.watchdog.Host";
Chris Austen454acfe2015-10-29 23:03:34 -0500325 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500326 const char *iface = "org.openbmc.Watchdog";
327 sd_bus_message *reply = NULL, *m = NULL;
328 sd_bus_error error = SD_BUS_ERROR_NULL;
329 int r = 0;
330
Chris Austen6caf28b2015-10-13 12:40:40 -0500331 // Status code.
332 ipmi_ret_t rc = IPMI_CC_OK;
333 *data_len = 0;
334
335 printf("WATCHDOG RESET\n");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500336
337 // Refresh watchdog
338 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"poke");
339 if (r < 0) {
340 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
341 return -1;
342 }
343 r = sd_bus_call(bus, m, 0, &error, &reply);
344 if (r < 0) {
345 fprintf(stderr, "Failed to call the method: %s\n", strerror(-r));
346 return -1;
347 }
348
349 sd_bus_error_free(&error);
350 sd_bus_message_unref(m);
351
Chris Austen6caf28b2015-10-13 12:40:40 -0500352 return rc;
353}
354
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500355ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
356 ipmi_request_t request, ipmi_response_t response,
357 ipmi_data_len_t data_len, ipmi_context_t context)
358{
359 ipmi_ret_t rc = IPMI_CC_OK;
360 *data_len = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500361
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500362 // Event and message logging enabled by default so return for now
363 printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
364
365 return rc;
366}
Chris Austen6caf28b2015-10-13 12:40:40 -0500367
368
369
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500370ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
371 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530372 ipmi_data_len_t data_len, ipmi_context_t context)
373{
374 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
375
376 // Status code.
377 ipmi_ret_t rc = IPMI_CC_OK;
378
379 *data_len = strlen("THIS IS WILDCARD");
380
381 // Now pack actual response
382 memcpy(response, "THIS IS WILDCARD", *data_len);
383
384 return rc;
385}
386
Chris Austen6caf28b2015-10-13 12:40:40 -0500387void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530388{
389 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Chris Austen6caf28b2015-10-13 12:40:40 -0500390 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities);
391
392 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
393 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler);
394
395 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
396 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog);
397
398 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
399 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog);
400
401 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
402 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id);
403
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500404 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
405 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid);
406
Chris Austen6caf28b2015-10-13 12:40:40 -0500407 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
408 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state);
409
410 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_READ_EVENT);
411 ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL, ipmi_app_read_event);
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500412
413 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
414 IPMI_CMD_SET_BMC_GLOBAL_ENABLES);
415 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
416 ipmi_app_set_bmc_global_enables);
417
vishwa36993272015-11-20 12:43:49 -0600418 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS);
419 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS, NULL, ipmi_app_get_msg_flags);
420
vishwabmcba0bd5f2015-09-30 16:50:23 +0530421 return;
422}
423
Chris Austen6caf28b2015-10-13 12:40:40 -0500424