blob: 5d81fb4b4ec0b8ffc80d00c0c5076693aba2b760 [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";
64 const char *iface = "org.openbmc.control.Chassis";
65 sd_bus_message *reply = NULL, *m = NULL;
66 sd_bus_error error = SD_BUS_ERROR_NULL;
67 int r = 0;
68 char *uuid = NULL;
69
70 // Status code.
71 ipmi_ret_t rc = IPMI_CC_OK;
72 *data_len = 0;
73
74 printf("IPMI GET DEVICE GUID\n");
75
76 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"getID");
77 if (r < 0) {
78 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
79 return IPMI_CC_UNSPECIFIED_ERROR;
80 }
81 r = sd_bus_call(bus, m, 0, &error, &reply);
82 if (r < 0) {
83 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
84 return IPMI_CC_UNSPECIFIED_ERROR;
85 }
86 r = sd_bus_message_read(reply, "s", &uuid);
87 if (r < 0) {
88 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
89 return IPMI_CC_RESPONSE_ERROR;
90 }
91 if (uuid == NULL)
92 {
93 fprintf(stderr, "Failed to get a valid response: %s", strerror(-r));
94 return IPMI_CC_RESPONSE_ERROR;
95 }
96
97 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
98 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
99 // Ex: 0x2332fc2c40e66298e511f2782395a361
100
101 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
102 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
103 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
104 int i = 0;
105 char *tokptr = NULL;
106
107 // Traverse the UUID
108 char* id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
109
110 if (id_octet == NULL)
111 { // Error
112 fprintf(stderr, "Unexpected UUID format: %s", uuid);
113 return IPMI_CC_RESPONSE_ERROR;
114 }
115
116 while (id_octet != NULL)
117 {
118 // Calculate the octet string size since it varies
119 // Divide it by 2 for the array size since 1 byte is built from 2 chars
120 int tmp_size = strlen(id_octet)/2;
121
122 for(i = 0; i < tmp_size; i++)
123 {
124 char tmp_array[3]; // Holder of the 2 chars that will become a byte
125 tmp_array[3] = '\0';
126 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
127
128 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
129 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
130 resp_loc--;
131 id_octet+=2; // Finished with the 2 chars, advance
132 }
133 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
134 }
135
136 // Data length
137 *data_len = resp_size;
138
139 // Pack the actual response
140 memcpy(response, &resp_uuid, *data_len);
141
142 sd_bus_error_free(&error);
143 sd_bus_message_unref(m);
144
145 return rc;
146}
Chris Austen6caf28b2015-10-13 12:40:40 -0500147
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500148ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
149 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530150 ipmi_data_len_t data_len, ipmi_context_t context)
151{
152 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
153
154 // Status code.
155 ipmi_ret_t rc = IPMI_CC_OK;
156
Chris Austen6caf28b2015-10-13 12:40:40 -0500157 uint8_t str[] = {0x01, MAX_IPMI_BUFFER, MAX_IPMI_BUFFER, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530158
159 // Data length
160 *data_len = sizeof(str);
161
162 // Pack the actual response
163 memcpy(response, &str, *data_len);
164
165 return rc;
166}
167
Chris Austen6caf28b2015-10-13 12:40:40 -0500168
169struct set_wd_data_t {
170 uint8_t t_use;
171 uint8_t t_action;
172 uint8_t preset;
173 uint8_t flags;
174 uint8_t ls;
175 uint8_t ms;
176} __attribute__ ((packed));
177
178
179
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500180ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
181 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500182 ipmi_data_len_t data_len, ipmi_context_t context)
183{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500184 const char *busname = "org.openbmc.watchdog.Host";
Chris Austen454acfe2015-10-29 23:03:34 -0500185 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500186 const char *iface = "org.openbmc.Watchdog";
187 sd_bus_message *reply = NULL, *m = NULL;
188 sd_bus_error error = SD_BUS_ERROR_NULL;
189 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500190
191 set_wd_data_t *reqptr = (set_wd_data_t*) request;
192 uint16_t timer = 0;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500193 uint32_t timer_ms = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500194 // Status code.
195 ipmi_ret_t rc = IPMI_CC_OK;
196
197 *data_len = 0;
198
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500199 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500200 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500201 // Get timer value in ms
202 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500203
204 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
205
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500206 // Set watchdog timer
207 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"set");
208 if (r < 0) {
209 fprintf(stderr, "Failed to add the set method object: %s\n", strerror(-r));
210 return -1;
211 }
212 r = sd_bus_message_append(m, "i", timer_ms);
213 if (r < 0) {
214 fprintf(stderr, "Failed to add timer value: %s\n", strerror(-r));
215 return -1;
216 }
217 r = sd_bus_call(bus, m, 0, &error, &reply);
218 if (r < 0) {
219 fprintf(stderr, "Failed to call the set method: %s\n", strerror(-r));
220 return -1;
221 }
222
Adriana Kobylak0896be32015-10-22 13:27:23 -0500223 // Stop the current watchdog if any
224 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"stop");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500225 if (r < 0) {
226 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
227 return -1;
228 }
229 r = sd_bus_call(bus, m, 0, &error, &reply);
230 if (r < 0) {
231 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
232 return -1;
233 }
234
Adriana Kobylak0896be32015-10-22 13:27:23 -0500235 // Start the watchdog if requested
236 if (reqptr->t_use & 0x40)
237 {
238 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"start");
239 if (r < 0) {
240 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
241 return -1;
242 }
243 r = sd_bus_call(bus, m, 0, &error, &reply);
244 if (r < 0) {
245 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
246 return -1;
247 }
248 }
249
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500250 sd_bus_error_free(&error);
251 sd_bus_message_unref(m);
Chris Austen6caf28b2015-10-13 12:40:40 -0500252
253 return rc;
254}
255
256
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500257ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
258 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500259 ipmi_data_len_t data_len, ipmi_context_t context)
260{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500261 const char *busname = "org.openbmc.watchdog.Host";
Chris Austen454acfe2015-10-29 23:03:34 -0500262 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500263 const char *iface = "org.openbmc.Watchdog";
264 sd_bus_message *reply = NULL, *m = NULL;
265 sd_bus_error error = SD_BUS_ERROR_NULL;
266 int r = 0;
267
Chris Austen6caf28b2015-10-13 12:40:40 -0500268 // Status code.
269 ipmi_ret_t rc = IPMI_CC_OK;
270 *data_len = 0;
271
272 printf("WATCHDOG RESET\n");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500273
274 // Refresh watchdog
275 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"poke");
276 if (r < 0) {
277 fprintf(stderr, "Failed to add the method object: %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 method: %s\n", strerror(-r));
283 return -1;
284 }
285
286 sd_bus_error_free(&error);
287 sd_bus_message_unref(m);
288
Chris Austen6caf28b2015-10-13 12:40:40 -0500289 return rc;
290}
291
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500292ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
293 ipmi_request_t request, ipmi_response_t response,
294 ipmi_data_len_t data_len, ipmi_context_t context)
295{
296 ipmi_ret_t rc = IPMI_CC_OK;
297 *data_len = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500298
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500299 // Event and message logging enabled by default so return for now
300 printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
301
302 return rc;
303}
Chris Austen6caf28b2015-10-13 12:40:40 -0500304
305
306
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500307ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
308 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530309 ipmi_data_len_t data_len, ipmi_context_t context)
310{
311 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
312
313 // Status code.
314 ipmi_ret_t rc = IPMI_CC_OK;
315
316 *data_len = strlen("THIS IS WILDCARD");
317
318 // Now pack actual response
319 memcpy(response, "THIS IS WILDCARD", *data_len);
320
321 return rc;
322}
323
Chris Austen6caf28b2015-10-13 12:40:40 -0500324void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530325{
326 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Chris Austen6caf28b2015-10-13 12:40:40 -0500327 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities);
328
329 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
330 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler);
331
332 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
333 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog);
334
335 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
336 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog);
337
338 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
339 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id);
340
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500341 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
342 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid);
343
Chris Austen6caf28b2015-10-13 12:40:40 -0500344 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
345 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state);
346
347 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_READ_EVENT);
348 ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL, ipmi_app_read_event);
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500349
350 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
351 IPMI_CMD_SET_BMC_GLOBAL_ENABLES);
352 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
353 ipmi_app_set_bmc_global_enables);
354
vishwabmcba0bd5f2015-09-30 16:50:23 +0530355 return;
356}
357
Chris Austen6caf28b2015-10-13 12:40:40 -0500358