blob: 646739797a563e86c30cb42fbc4ec6fde1c86128 [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
Chris Austen6caf28b2015-10-13 12:40:40 -050013
Adriana Kobylak3a552e12015-10-19 16:11:00 -050014ipmi_ret_t ipmi_app_read_event(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
15 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050016 ipmi_data_len_t data_len, ipmi_context_t context)
17{
18 ipmi_ret_t rc = IPMI_CC_OK;
19 *data_len = 0;
20
21 printf("IPMI APP READ EVENT Ignoring for now\n");
22 return rc;
23
24}
25
26
Adriana Kobylak3a552e12015-10-19 16:11:00 -050027ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
28 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050029 ipmi_data_len_t data_len, ipmi_context_t context)
30{
31 ipmi_ret_t rc = IPMI_CC_OK;
32 *data_len = 0;
33
34 printf("IPMI SET ACPI STATE Ignoring for now\n");
35 return rc;
36}
37
Adriana Kobylak3a552e12015-10-19 16:11:00 -050038ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
39 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050040 ipmi_data_len_t data_len, ipmi_context_t context)
41{
42 ipmi_ret_t rc = IPMI_CC_OK;
43
Patrick Williams76b51f62015-10-28 12:14:56 -050044 // TODO:
45 // This value is the IANA number assigned to "IBM Platform Firmware
46 // Division", which is also used by our service processor. We may want
47 // a different number or at least a different version?
Chris Austen6caf28b2015-10-13 12:40:40 -050048 uint8_t str[] = {0x00, 0, 1, 1,2, 0xD, 0x41, 0xA7, 0x00, 0x43, 0x40};
49
50 // Data length
51 *data_len = sizeof(str);
52
53 // Pack the actual response
54 memcpy(response, &str, *data_len);
55 return rc;
56}
57
Adriana Kobylakd100ee52015-10-20 17:02:37 -050058ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
59 ipmi_request_t request, ipmi_response_t response,
60 ipmi_data_len_t data_len, ipmi_context_t context)
61{
62 const char *busname = "org.openbmc.control.Chassis";
63 const char *objname = "/org/openbmc/control/chassis0";
Adriana Kobylak31bccae2015-11-05 13:31:06 -060064 const char *iface = "org.freedesktop.DBus.Properties";
65 const char *chassis_iface = "org.openbmc.control.Chassis";
Adriana Kobylakd100ee52015-10-20 17:02:37 -050066 sd_bus_message *reply = NULL, *m = NULL;
67 sd_bus_error error = SD_BUS_ERROR_NULL;
68 int r = 0;
69 char *uuid = NULL;
70
71 // Status code.
72 ipmi_ret_t rc = IPMI_CC_OK;
73 *data_len = 0;
74
75 printf("IPMI GET DEVICE GUID\n");
76
Adriana Kobylak31bccae2015-11-05 13:31:06 -060077 // Call Get properties method with the interface and property name
78 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"Get");
Adriana Kobylakd100ee52015-10-20 17:02:37 -050079 if (r < 0) {
Adriana Kobylak31bccae2015-11-05 13:31:06 -060080 fprintf(stderr, "Failed to add the Get method object: %s\n", strerror(-r));
Adriana Kobylakd100ee52015-10-20 17:02:37 -050081 return IPMI_CC_UNSPECIFIED_ERROR;
82 }
Adriana Kobylak31bccae2015-11-05 13:31:06 -060083 r = sd_bus_message_append(m, "ss", chassis_iface, "uuid");
84 if (r < 0) {
85 fprintf(stderr, "Failed to append arguments: %s\n", strerror(-r));
86 return -1;
87 }
Adriana Kobylakd100ee52015-10-20 17:02:37 -050088 r = sd_bus_call(bus, m, 0, &error, &reply);
89 if (r < 0) {
Adriana Kobylak31bccae2015-11-05 13:31:06 -060090 fprintf(stderr, "Failed to call the Get method: %s\n", strerror(-r));
Adriana Kobylakd100ee52015-10-20 17:02:37 -050091 return IPMI_CC_UNSPECIFIED_ERROR;
92 }
Adriana Kobylak31bccae2015-11-05 13:31:06 -060093 r = sd_bus_message_read(reply, "v", "s", &uuid);
Adriana Kobylakd100ee52015-10-20 17:02:37 -050094 if (r < 0) {
95 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
96 return IPMI_CC_RESPONSE_ERROR;
97 }
98 if (uuid == NULL)
99 {
100 fprintf(stderr, "Failed to get a valid response: %s", strerror(-r));
101 return IPMI_CC_RESPONSE_ERROR;
102 }
103
104 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
105 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
106 // Ex: 0x2332fc2c40e66298e511f2782395a361
107
108 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
109 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
110 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
111 int i = 0;
112 char *tokptr = NULL;
113
114 // Traverse the UUID
115 char* id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
116
117 if (id_octet == NULL)
118 { // Error
119 fprintf(stderr, "Unexpected UUID format: %s", uuid);
120 return IPMI_CC_RESPONSE_ERROR;
121 }
122
123 while (id_octet != NULL)
124 {
125 // Calculate the octet string size since it varies
126 // Divide it by 2 for the array size since 1 byte is built from 2 chars
127 int tmp_size = strlen(id_octet)/2;
128
129 for(i = 0; i < tmp_size; i++)
130 {
131 char tmp_array[3]; // Holder of the 2 chars that will become a byte
132 tmp_array[3] = '\0';
133 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
134
135 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
136 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
137 resp_loc--;
138 id_octet+=2; // Finished with the 2 chars, advance
139 }
140 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
141 }
142
143 // Data length
144 *data_len = resp_size;
145
146 // Pack the actual response
147 memcpy(response, &resp_uuid, *data_len);
148
149 sd_bus_error_free(&error);
150 sd_bus_message_unref(m);
151
152 return rc;
153}
Chris Austen6caf28b2015-10-13 12:40:40 -0500154
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500155ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
156 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530157 ipmi_data_len_t data_len, ipmi_context_t context)
158{
159 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
160
161 // Status code.
162 ipmi_ret_t rc = IPMI_CC_OK;
163
Chris Austen6caf28b2015-10-13 12:40:40 -0500164 uint8_t str[] = {0x01, MAX_IPMI_BUFFER, MAX_IPMI_BUFFER, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530165
166 // Data length
167 *data_len = sizeof(str);
168
169 // Pack the actual response
170 memcpy(response, &str, *data_len);
171
172 return rc;
173}
174
Chris Austen6caf28b2015-10-13 12:40:40 -0500175
176struct set_wd_data_t {
177 uint8_t t_use;
178 uint8_t t_action;
179 uint8_t preset;
180 uint8_t flags;
181 uint8_t ls;
182 uint8_t ms;
183} __attribute__ ((packed));
184
185
186
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500187ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
188 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500189 ipmi_data_len_t data_len, ipmi_context_t context)
190{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500191 const char *busname = "org.openbmc.watchdog.Host";
Chris Austen454acfe2015-10-29 23:03:34 -0500192 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500193 const char *iface = "org.openbmc.Watchdog";
194 sd_bus_message *reply = NULL, *m = NULL;
195 sd_bus_error error = SD_BUS_ERROR_NULL;
196 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500197
198 set_wd_data_t *reqptr = (set_wd_data_t*) request;
199 uint16_t timer = 0;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500200 uint32_t timer_ms = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500201 // Status code.
202 ipmi_ret_t rc = IPMI_CC_OK;
203
204 *data_len = 0;
205
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500206 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500207 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500208 // Get timer value in ms
209 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500210
211 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
212
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500213 // Set watchdog timer
214 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"set");
215 if (r < 0) {
216 fprintf(stderr, "Failed to add the set method object: %s\n", strerror(-r));
217 return -1;
218 }
219 r = sd_bus_message_append(m, "i", timer_ms);
220 if (r < 0) {
221 fprintf(stderr, "Failed to add timer value: %s\n", strerror(-r));
222 return -1;
223 }
224 r = sd_bus_call(bus, m, 0, &error, &reply);
225 if (r < 0) {
226 fprintf(stderr, "Failed to call the set method: %s\n", strerror(-r));
227 return -1;
228 }
229
Adriana Kobylak0896be32015-10-22 13:27:23 -0500230 // Stop the current watchdog if any
231 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"stop");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500232 if (r < 0) {
233 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
234 return -1;
235 }
236 r = sd_bus_call(bus, m, 0, &error, &reply);
237 if (r < 0) {
238 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
239 return -1;
240 }
241
Adriana Kobylak0896be32015-10-22 13:27:23 -0500242 // Start the watchdog if requested
243 if (reqptr->t_use & 0x40)
244 {
245 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"start");
246 if (r < 0) {
247 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
248 return -1;
249 }
250 r = sd_bus_call(bus, m, 0, &error, &reply);
251 if (r < 0) {
252 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
253 return -1;
254 }
255 }
256
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500257 sd_bus_error_free(&error);
258 sd_bus_message_unref(m);
Chris Austen6caf28b2015-10-13 12:40:40 -0500259
260 return rc;
261}
262
263
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500264ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
265 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500266 ipmi_data_len_t data_len, ipmi_context_t context)
267{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500268 const char *busname = "org.openbmc.watchdog.Host";
Chris Austen454acfe2015-10-29 23:03:34 -0500269 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500270 const char *iface = "org.openbmc.Watchdog";
271 sd_bus_message *reply = NULL, *m = NULL;
272 sd_bus_error error = SD_BUS_ERROR_NULL;
273 int r = 0;
274
Chris Austen6caf28b2015-10-13 12:40:40 -0500275 // Status code.
276 ipmi_ret_t rc = IPMI_CC_OK;
277 *data_len = 0;
278
279 printf("WATCHDOG RESET\n");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500280
281 // Refresh watchdog
282 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"poke");
283 if (r < 0) {
284 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
285 return -1;
286 }
287 r = sd_bus_call(bus, m, 0, &error, &reply);
288 if (r < 0) {
289 fprintf(stderr, "Failed to call the method: %s\n", strerror(-r));
290 return -1;
291 }
292
293 sd_bus_error_free(&error);
294 sd_bus_message_unref(m);
295
Chris Austen6caf28b2015-10-13 12:40:40 -0500296 return rc;
297}
298
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500299ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
300 ipmi_request_t request, ipmi_response_t response,
301 ipmi_data_len_t data_len, ipmi_context_t context)
302{
303 ipmi_ret_t rc = IPMI_CC_OK;
304 *data_len = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500305
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500306 // Event and message logging enabled by default so return for now
307 printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
308
309 return rc;
310}
Chris Austen6caf28b2015-10-13 12:40:40 -0500311
312
313
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500314ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
315 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530316 ipmi_data_len_t data_len, ipmi_context_t context)
317{
318 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
319
320 // Status code.
321 ipmi_ret_t rc = IPMI_CC_OK;
322
323 *data_len = strlen("THIS IS WILDCARD");
324
325 // Now pack actual response
326 memcpy(response, "THIS IS WILDCARD", *data_len);
327
328 return rc;
329}
330
Chris Austen6caf28b2015-10-13 12:40:40 -0500331void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530332{
333 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Chris Austen6caf28b2015-10-13 12:40:40 -0500334 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities);
335
336 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
337 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler);
338
339 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
340 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog);
341
342 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
343 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog);
344
345 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
346 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id);
347
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500348 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
349 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid);
350
Chris Austen6caf28b2015-10-13 12:40:40 -0500351 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
352 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state);
353
354 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_READ_EVENT);
355 ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL, ipmi_app_read_event);
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500356
357 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
358 IPMI_CMD_SET_BMC_GLOBAL_ENABLES);
359 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
360 ipmi_app_set_bmc_global_enables);
361
vishwabmcba0bd5f2015-09-30 16:50:23 +0530362 return;
363}
364
Chris Austen6caf28b2015-10-13 12:40:40 -0500365