blob: 07be496faddfaa7b8d6304f54787b480616a34ca [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
Adriana Kobylak3a552e12015-10-19 16:11:00 -050044 // TODO GET REAL VALUES HERE.... I made these ones up because
Chris Austen6caf28b2015-10-13 12:40:40 -050045 // we are in bringup mode. Version Major and Minor can be what we
Adriana Kobylak3a552e12015-10-19 16:11:00 -050046 // want like v1.03 but the IANA really should be something that
47 // we own. I would suggest getting the IANA from Hostboot as
48 // long as IBM owns it then no problem. If some other company
49 // gave us the IANA to use then use the one we have from the
Chris Austen6caf28b2015-10-13 12:40:40 -050050 // FSP ipmi code.
51 uint8_t str[] = {0x00, 0, 1, 1,2, 0xD, 0x41, 0xA7, 0x00, 0x43, 0x40};
52
53 // Data length
54 *data_len = sizeof(str);
55
56 // Pack the actual response
57 memcpy(response, &str, *data_len);
58 return rc;
59}
60
Adriana Kobylakd100ee52015-10-20 17:02:37 -050061ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
62 ipmi_request_t request, ipmi_response_t response,
63 ipmi_data_len_t data_len, ipmi_context_t context)
64{
65 const char *busname = "org.openbmc.control.Chassis";
66 const char *objname = "/org/openbmc/control/chassis0";
67 const char *iface = "org.openbmc.control.Chassis";
68 sd_bus_message *reply = NULL, *m = NULL;
69 sd_bus_error error = SD_BUS_ERROR_NULL;
70 int r = 0;
71 char *uuid = NULL;
72
73 // Status code.
74 ipmi_ret_t rc = IPMI_CC_OK;
75 *data_len = 0;
76
77 printf("IPMI GET DEVICE GUID\n");
78
79 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"getID");
80 if (r < 0) {
81 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
82 return IPMI_CC_UNSPECIFIED_ERROR;
83 }
84 r = sd_bus_call(bus, m, 0, &error, &reply);
85 if (r < 0) {
86 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
87 return IPMI_CC_UNSPECIFIED_ERROR;
88 }
89 r = sd_bus_message_read(reply, "s", &uuid);
90 if (r < 0) {
91 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
92 return IPMI_CC_RESPONSE_ERROR;
93 }
94 if (uuid == NULL)
95 {
96 fprintf(stderr, "Failed to get a valid response: %s", strerror(-r));
97 return IPMI_CC_RESPONSE_ERROR;
98 }
99
100 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
101 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
102 // Ex: 0x2332fc2c40e66298e511f2782395a361
103
104 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
105 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
106 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
107 int i = 0;
108 char *tokptr = NULL;
109
110 // Traverse the UUID
111 char* id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
112
113 if (id_octet == NULL)
114 { // Error
115 fprintf(stderr, "Unexpected UUID format: %s", uuid);
116 return IPMI_CC_RESPONSE_ERROR;
117 }
118
119 while (id_octet != NULL)
120 {
121 // Calculate the octet string size since it varies
122 // Divide it by 2 for the array size since 1 byte is built from 2 chars
123 int tmp_size = strlen(id_octet)/2;
124
125 for(i = 0; i < tmp_size; i++)
126 {
127 char tmp_array[3]; // Holder of the 2 chars that will become a byte
128 tmp_array[3] = '\0';
129 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
130
131 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
132 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
133 resp_loc--;
134 id_octet+=2; // Finished with the 2 chars, advance
135 }
136 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
137 }
138
139 // Data length
140 *data_len = resp_size;
141
142 // Pack the actual response
143 memcpy(response, &resp_uuid, *data_len);
144
145 sd_bus_error_free(&error);
146 sd_bus_message_unref(m);
147
148 return rc;
149}
Chris Austen6caf28b2015-10-13 12:40:40 -0500150
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500151ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
152 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530153 ipmi_data_len_t data_len, ipmi_context_t context)
154{
155 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
156
157 // Status code.
158 ipmi_ret_t rc = IPMI_CC_OK;
159
Chris Austen6caf28b2015-10-13 12:40:40 -0500160 uint8_t str[] = {0x01, MAX_IPMI_BUFFER, MAX_IPMI_BUFFER, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530161
162 // Data length
163 *data_len = sizeof(str);
164
165 // Pack the actual response
166 memcpy(response, &str, *data_len);
167
168 return rc;
169}
170
Chris Austen6caf28b2015-10-13 12:40:40 -0500171
172struct set_wd_data_t {
173 uint8_t t_use;
174 uint8_t t_action;
175 uint8_t preset;
176 uint8_t flags;
177 uint8_t ls;
178 uint8_t ms;
179} __attribute__ ((packed));
180
181
182
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500183ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
184 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500185 ipmi_data_len_t data_len, ipmi_context_t context)
186{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500187 const char *busname = "org.openbmc.watchdog.Host";
188 const char *objname = "/org/openbmc/watchdog/HostWatchdog_0";
189 const char *iface = "org.openbmc.Watchdog";
190 sd_bus_message *reply = NULL, *m = NULL;
191 sd_bus_error error = SD_BUS_ERROR_NULL;
192 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500193
194 set_wd_data_t *reqptr = (set_wd_data_t*) request;
195 uint16_t timer = 0;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500196 uint32_t timer_ms = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500197 // Status code.
198 ipmi_ret_t rc = IPMI_CC_OK;
199
200 *data_len = 0;
201
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500202 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500203 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500204 // Get timer value in ms
205 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500206
207 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
208
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500209 // Set watchdog timer
210 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"set");
211 if (r < 0) {
212 fprintf(stderr, "Failed to add the set method object: %s\n", strerror(-r));
213 return -1;
214 }
215 r = sd_bus_message_append(m, "i", timer_ms);
216 if (r < 0) {
217 fprintf(stderr, "Failed to add timer value: %s\n", strerror(-r));
218 return -1;
219 }
220 r = sd_bus_call(bus, m, 0, &error, &reply);
221 if (r < 0) {
222 fprintf(stderr, "Failed to call the set method: %s\n", strerror(-r));
223 return -1;
224 }
225
Adriana Kobylak0896be32015-10-22 13:27:23 -0500226 // Stop the current watchdog if any
227 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"stop");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500228 if (r < 0) {
229 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
230 return -1;
231 }
232 r = sd_bus_call(bus, m, 0, &error, &reply);
233 if (r < 0) {
234 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
235 return -1;
236 }
237
Adriana Kobylak0896be32015-10-22 13:27:23 -0500238 // Start the watchdog if requested
239 if (reqptr->t_use & 0x40)
240 {
241 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"start");
242 if (r < 0) {
243 fprintf(stderr, "Failed to add the start method object: %s\n", strerror(-r));
244 return -1;
245 }
246 r = sd_bus_call(bus, m, 0, &error, &reply);
247 if (r < 0) {
248 fprintf(stderr, "Failed to call the start method: %s\n", strerror(-r));
249 return -1;
250 }
251 }
252
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500253 sd_bus_error_free(&error);
254 sd_bus_message_unref(m);
Chris Austen6caf28b2015-10-13 12:40:40 -0500255
256 return rc;
257}
258
259
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500260ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
261 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500262 ipmi_data_len_t data_len, ipmi_context_t context)
263{
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500264 const char *busname = "org.openbmc.watchdog.Host";
265 const char *objname = "/org/openbmc/watchdog/HostWatchdog_0";
266 const char *iface = "org.openbmc.Watchdog";
267 sd_bus_message *reply = NULL, *m = NULL;
268 sd_bus_error error = SD_BUS_ERROR_NULL;
269 int r = 0;
270
Chris Austen6caf28b2015-10-13 12:40:40 -0500271 // Status code.
272 ipmi_ret_t rc = IPMI_CC_OK;
273 *data_len = 0;
274
275 printf("WATCHDOG RESET\n");
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500276
277 // Refresh watchdog
278 r = sd_bus_message_new_method_call(bus,&m,busname,objname,iface,"poke");
279 if (r < 0) {
280 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
281 return -1;
282 }
283 r = sd_bus_call(bus, m, 0, &error, &reply);
284 if (r < 0) {
285 fprintf(stderr, "Failed to call the method: %s\n", strerror(-r));
286 return -1;
287 }
288
289 sd_bus_error_free(&error);
290 sd_bus_message_unref(m);
291
Chris Austen6caf28b2015-10-13 12:40:40 -0500292 return rc;
293}
294
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500295ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
296 ipmi_request_t request, ipmi_response_t response,
297 ipmi_data_len_t data_len, ipmi_context_t context)
298{
299 ipmi_ret_t rc = IPMI_CC_OK;
300 *data_len = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500301
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500302 // Event and message logging enabled by default so return for now
303 printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
304
305 return rc;
306}
Chris Austen6caf28b2015-10-13 12:40:40 -0500307
308
309
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500310ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
311 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530312 ipmi_data_len_t data_len, ipmi_context_t context)
313{
314 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
315
316 // Status code.
317 ipmi_ret_t rc = IPMI_CC_OK;
318
319 *data_len = strlen("THIS IS WILDCARD");
320
321 // Now pack actual response
322 memcpy(response, "THIS IS WILDCARD", *data_len);
323
324 return rc;
325}
326
Chris Austen6caf28b2015-10-13 12:40:40 -0500327void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530328{
329 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Chris Austen6caf28b2015-10-13 12:40:40 -0500330 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities);
331
332 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
333 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler);
334
335 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
336 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog);
337
338 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
339 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog);
340
341 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
342 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id);
343
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500344 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
345 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid);
346
Chris Austen6caf28b2015-10-13 12:40:40 -0500347 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
348 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state);
349
350 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_READ_EVENT);
351 ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL, ipmi_app_read_event);
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500352
353 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
354 IPMI_CMD_SET_BMC_GLOBAL_ENABLES);
355 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
356 ipmi_app_set_bmc_global_enables);
357
vishwabmcba0bd5f2015-09-30 16:50:23 +0530358 return;
359}
360
Chris Austen6caf28b2015-10-13 12:40:40 -0500361