blob: 431cbf04979b7d0efc680998b10ca7e01b3e5639 [file] [log] [blame]
vishwabmcba0bd5f2015-09-30 16:50:23 +05301#include "apphandler.h"
Patrick Williams37af7332016-09-02 21:21:42 -05002#include "host-ipmid/ipmid-api.h"
Patrick Williams53a360e2016-08-12 22:01:02 -05003#include "ipmid.hpp"
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>
Sergey Solomineb9b8142016-08-23 09:07:28 -05008#include <mapper.h>
Nan Liee0cb902016-07-11 15:38:03 +08009#include <array>
Nan Li3d0df912016-10-18 19:51:41 +080010#include <arpa/inet.h>
11#include "transporthandler.h"
Adriana Kobylak3a552e12015-10-19 16:11:00 -050012
13extern sd_bus *bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053014
Nan Li3d0df912016-10-18 19:51:41 +080015constexpr auto app_obj = "/org/openbmc/NetworkManager/Interface";
16constexpr auto app_ifc = "org.openbmc.NetworkManager";
17constexpr auto app_nwinterface = "eth0";
18
Chris Austen6caf28b2015-10-13 12:40:40 -050019void register_netfn_app_functions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053020
Nan Liee0cb902016-07-11 15:38:03 +080021// Offset in get device id command.
22typedef struct
23{
24 uint8_t id;
25 uint8_t revision;
26 uint8_t fw[2];
27 uint8_t ipmi_ver;
28 uint8_t addn_dev_support;
29 uint8_t manuf_id[3];
30 uint8_t prod_id[2];
31 uint8_t aux[4];
32}__attribute__((packed)) ipmi_device_id_t;
Chris Austen7303bdc2016-04-17 11:50:54 -050033
vishwa36993272015-11-20 12:43:49 -060034//---------------------------------------------------------------------
Patrick Williams53a360e2016-08-12 22:01:02 -050035// Called by Host on seeing a SMS_ATN bit set. Return a hardcoded
vishwa36993272015-11-20 12:43:49 -060036// value of 0x2 indicating we need Host read some data.
37//-------------------------------------------------------------------
38ipmi_ret_t ipmi_app_get_msg_flags(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
39 ipmi_request_t request, ipmi_response_t response,
40 ipmi_data_len_t data_len, ipmi_context_t context)
41{
42 // Generic return from IPMI commands.
43 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -050044
vishwa36993272015-11-20 12:43:49 -060045 printf("IPMI APP GET MSG FLAGS returning with [bit:2] set\n");
46
47 // From IPMI spec V2.0 for Get Message Flags Command :
Patrick Williams53a360e2016-08-12 22:01:02 -050048 // bit:[1] from LSB : 1b = Event Message Buffer Full.
49 // Return as 0 if Event Message Buffer is not supported,
vishwa36993272015-11-20 12:43:49 -060050 // or when the Event Message buffer is disabled.
51 // TODO. For now. assume its not disabled and send "0x2" anyway:
52
53 uint8_t set_event_msg_buffer_full = 0x2;
54 *data_len = sizeof(set_event_msg_buffer_full);
55
56 // Pack the actual response
57 memcpy(response, &set_event_msg_buffer_full, *data_len);
58
59 return rc;
60}
61
62//-------------------------------------------------------------------
63// Called by Host post response from Get_Message_Flags
64//-------------------------------------------------------------------
Adriana Kobylak3a552e12015-10-19 16:11:00 -050065ipmi_ret_t ipmi_app_read_event(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
66 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050067 ipmi_data_len_t data_len, ipmi_context_t context)
68{
69 ipmi_ret_t rc = IPMI_CC_OK;
vishwa36993272015-11-20 12:43:49 -060070 printf("IPMI APP READ EVENT command received\n");
Chris Austen6caf28b2015-10-13 12:40:40 -050071
vishwa36993272015-11-20 12:43:49 -060072 // TODO : For now, this is catering only to the Soft Power Off via OEM SEL
73 // mechanism. If we need to make this generically used for some
74 // other conditions, then we can take advantage of context pointer.
75
76 struct oem_sel_timestamped soft_off = {0};
77 *data_len = sizeof(struct oem_sel_timestamped);
78
79 // either id[0] -or- id[1] can be filled in. We will use id[0]
80 soft_off.id[0] = SEL_OEM_ID_0;
81 soft_off.id[1] = SEL_OEM_ID_0;
82 soft_off.type = SEL_RECORD_TYPE_OEM;
83
84 // Following 3 bytes are from IANA Manufactre_Id field. See below
85 soft_off.manuf_id[0]= 0x41;
86 soft_off.manuf_id[1]= 0xA7;
87 soft_off.manuf_id[2]= 0x00;
88
89 // per IPMI spec NetFuntion for OEM
90 soft_off.netfun = 0x3A;
91
92 // Mechanism to kick start soft shutdown.
93 soft_off.cmd = CMD_POWER;
94 soft_off.data[0] = SOFT_OFF;
95
96 // All '0xFF' since unused.
97 memset(&soft_off.data[1], 0xFF, 3);
98
99 // Pack the actual response
100 memcpy(response, &soft_off, *data_len);
Chris Austen6caf28b2015-10-13 12:40:40 -0500101 return rc;
Chris Austen6caf28b2015-10-13 12:40:40 -0500102}
103
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500104ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
105 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500106 ipmi_data_len_t data_len, ipmi_context_t context)
107{
108 ipmi_ret_t rc = IPMI_CC_OK;
109 *data_len = 0;
110
111 printf("IPMI SET ACPI STATE Ignoring for now\n");
112 return rc;
113}
114
Chris Austen7303bdc2016-04-17 11:50:54 -0500115
116typedef struct
117{
118 char major;
119 char minor;
Chris Austen176c9652016-04-30 16:32:17 -0500120 uint16_t d[2];
Chris Austen7303bdc2016-04-17 11:50:54 -0500121} rev_t;
122
123
124/* Currently only supports the vx.x-x-[-x] format Will return -1 if not in */
125/* the format this routine knows how to parse */
126/* version = v0.6-19-gf363f61-dirty */
127/* ^ ^ ^^ ^ */
128/* | | |----------|-- additional details */
129/* | |---------------- Minor */
130/* |------------------ Major */
131/* Additional details : If the option group exists it will force Auxiliary */
132/* Firmware Revision Information 4th byte to 1 indicating the build was */
133/* derived with additional edits */
134int convert_version(const char *p, rev_t *rev)
135{
136 char *s, *token;
Chris Austen176c9652016-04-30 16:32:17 -0500137 uint16_t commits;
Chris Austen7303bdc2016-04-17 11:50:54 -0500138
139 if (*p != 'v')
140 return -1;
141 p++;
142
143 s = strdup(p);
144 token = strtok(s,".-");
145
146 rev->major = (int8_t) atoi(token);
147
148 token = strtok(NULL, ".-");
149 rev->minor = (int8_t) atoi(token);
150
151 // Capture the number of commits on top of the minor tag.
152 // I'm using BE format like the ipmi spec asked for
153 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -0500154
Chris Austen176c9652016-04-30 16:32:17 -0500155 if (token) {
156 commits = (int16_t) atoi(token);
157 rev->d[0] = (commits>>8) | (commits<<8);
Chris Austen7303bdc2016-04-17 11:50:54 -0500158
Chris Austen176c9652016-04-30 16:32:17 -0500159 // commit number we skip
160 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -0500161
Chris Austen176c9652016-04-30 16:32:17 -0500162 } else {
163 rev->d[0] = 0;
164 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500165
166 // Any value of the optional parameter forces it to 1
Chris Austen176c9652016-04-30 16:32:17 -0500167 if (token)
168 token = strtok(NULL,".-");
169
170 rev->d[1] = (token != NULL) ? 1 : 0;
Chris Austen7303bdc2016-04-17 11:50:54 -0500171
172 free(s);
173 return 0;
174}
175
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500176ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
177 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500178 ipmi_data_len_t data_len, ipmi_context_t context)
179{
180 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austen7303bdc2016-04-17 11:50:54 -0500181 const char *objname = "/org/openbmc/inventory/system/chassis/motherboard/bmc";
182 const char *iface = "org.openbmc.InventoryItem";
183 char *ver = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500184 char *busname = NULL;
Chris Austen7303bdc2016-04-17 11:50:54 -0500185 int r;
186 rev_t rev = {0};
Nan Liee0cb902016-07-11 15:38:03 +0800187 ipmi_device_id_t dev_id{};
Chris Austen6caf28b2015-10-13 12:40:40 -0500188
189 // Data length
Chris Austen7303bdc2016-04-17 11:50:54 -0500190 *data_len = sizeof(dev_id);
191
Nan Liee0cb902016-07-11 15:38:03 +0800192 // From IPMI spec, controller that have different application commands, or different
193 // definitions of OEM fields, are expected to have different Device ID values.
194 // Set to 0 now.
195
196 // Device Revision is set to 0 now.
197 // Bit7 identifies if device provide Device SDRs, obmc don't have SDR,we use ipmi to
198 // simulate SDR, hence the value:
199 dev_id.revision = 0x80;
200
201 // Firmware revision is already implemented, so get it from appropriate position.
Sergey Solomineb9b8142016-08-23 09:07:28 -0500202 r = mapper_get_service(bus, objname, &busname);
203 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400204 fprintf(stderr, "Failed to get %s bus name: %s\n",
205 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500206 goto finish;
207 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500208 r = sd_bus_get_property_string(bus,busname,objname,iface,"version", NULL, &ver);
209 if ( r < 0 ) {
210 fprintf(stderr, "Failed to obtain version property: %s\n", strerror(-r));
211 } else {
212 r = convert_version(ver, &rev);
213 if( r >= 0 ) {
Nan Liee0cb902016-07-11 15:38:03 +0800214 // bit7 identifies if the device is available, 0=normal operation,
215 // 1=device firmware, SDR update or self-initialization in progress.
216 // our SDR is normal working condition, so mask:
217 dev_id.fw[0] = 0x7F & rev.major;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500218
219 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Nan Liee0cb902016-07-11 15:38:03 +0800220 dev_id.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
221 memcpy(&dev_id.aux, rev.d, 4);
Chris Austen7303bdc2016-04-17 11:50:54 -0500222 }
223 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500224
Nan Liee0cb902016-07-11 15:38:03 +0800225 // IPMI Spec verison 2.0
226 dev_id.ipmi_ver = 2;
227
228 // Additional device Support.
229 // List the 'logical device' commands and functions that the controller supports
230 // that are in addition to the mandatory IPM and Application commands.
231 // [7] Chassis Device (device functions as chassis device per ICMB spec.)
232 // [6] Bridge (device responds to Bridge NetFn commands)
233 // [5] IPMB Event Generator
234 // [4] IPMB Event Receiver
235 // [3] FRU Inventory Device
236 // [2] SEL Device
237 // [1] SDR Repository Device
238 // [0] Sensor Device
239 // We support FRU/SEL/Sensor now:
240 dev_id.addn_dev_support = 0x8D;
241
242 // This value is the IANA number assigned to "IBM Platform Firmware
243 // Division", which is also used by our service processor. We may want
244 // a different number or at least a different version?
245 dev_id.manuf_id[0] = 0x41;
246 dev_id.manuf_id[1] = 0xA7;
247 dev_id.manuf_id[2] = 0x00;
248
249 // Witherspoon's product ID is hardcoded to 4F42(ASCII 'OB').
250 // TODO: openbmc/openbmc#495
251 dev_id.prod_id[0] = 0x4F;
252 dev_id.prod_id[1] = 0x42;
253
Chris Austen6caf28b2015-10-13 12:40:40 -0500254 // Pack the actual response
Chris Austen7303bdc2016-04-17 11:50:54 -0500255 memcpy(response, &dev_id, *data_len);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500256finish:
257 free(busname);
Chris Austen6caf28b2015-10-13 12:40:40 -0500258 return rc;
259}
260
Nan Li41fa24a2016-11-10 20:12:37 +0800261ipmi_ret_t ipmi_app_get_self_test_results(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
262 ipmi_request_t request, ipmi_response_t response,
263 ipmi_data_len_t data_len, ipmi_context_t context)
264{
265 ipmi_ret_t rc = IPMI_CC_OK;
266
267 // Byte 2:
268 // 55h - No error.
269 // 56h - Self Test funciton not implemented in this controller.
270 // 57h - Corrupted or inaccesssible data or devices.
271 // 58h - Fatal hardware error.
272 // FFh - reserved.
273 // all other: Device-specific 'internal failure'.
274 // Byte 3:
275 // For byte 2 = 55h, 56h, FFh: 00h
276 // For byte 2 = 58h, all other: Device-specific
277 // For byte 2 = 57h: self-test error bitfield.
278 // Note: returning 57h does not imply that all test were run.
279 // [7] 1b = Cannot access SEL device.
280 // [6] 1b = Cannot access SDR Repository.
281 // [5] 1b = Cannot access BMC FRU device.
282 // [4] 1b = IPMB signal lines do not respond.
283 // [3] 1b = SDR Repository empty.
284 // [2] 1b = Internal Use Area of BMC FRU corrupted.
285 // [1] 1b = controller update 'boot block' firmware corrupted.
286 // [0] 1b = controller operational firmware corrupted.
287
288 char selftestresults[2] = {0};
289
290 *data_len = 2;
291
292 selftestresults[0] = 0x56;
293 selftestresults[1] = 0;
294
295 memcpy(response, selftestresults, *data_len);
296
297 return rc;
298}
299
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500300ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
301 ipmi_request_t request, ipmi_response_t response,
302 ipmi_data_len_t data_len, ipmi_context_t context)
303{
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500304 const char *objname = "/org/openbmc/control/chassis0";
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600305 const char *iface = "org.freedesktop.DBus.Properties";
306 const char *chassis_iface = "org.openbmc.control.Chassis";
vishwa1eaea4f2016-02-26 11:57:40 -0600307 sd_bus_message *reply = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500308 sd_bus_error error = SD_BUS_ERROR_NULL;
309 int r = 0;
310 char *uuid = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500311 char *busname = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500312
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500313 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
314 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
315 // Ex: 0x2332fc2c40e66298e511f2782395a361
316
317 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
318 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
319 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
320 int i = 0;
321 char *tokptr = NULL;
vishwa1eaea4f2016-02-26 11:57:40 -0600322 char *id_octet = NULL;
323
324 // Status code.
325 ipmi_ret_t rc = IPMI_CC_OK;
326 *data_len = 0;
327
328 printf("IPMI GET DEVICE GUID\n");
329
330 // Call Get properties method with the interface and property name
Sergey Solomineb9b8142016-08-23 09:07:28 -0500331 r = mapper_get_service(bus, objname, &busname);
332 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400333 fprintf(stderr, "Failed to get %s bus name: %s\n",
334 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500335 goto finish;
336 }
vishwa1eaea4f2016-02-26 11:57:40 -0600337 r = sd_bus_call_method(bus,busname,objname,iface,
338 "Get",&error, &reply, "ss",
339 chassis_iface, "uuid");
340 if (r < 0)
341 {
342 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
343 rc = IPMI_CC_UNSPECIFIED_ERROR;
344 goto finish;
345 }
346
347 r = sd_bus_message_read(reply, "v", "s", &uuid);
348 if (r < 0 || uuid == NULL)
349 {
350 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
351 rc = IPMI_CC_RESPONSE_ERROR;
352 goto finish;
353 }
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500354
355 // Traverse the UUID
vishwa1eaea4f2016-02-26 11:57:40 -0600356 id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500357
358 if (id_octet == NULL)
vishwa1eaea4f2016-02-26 11:57:40 -0600359 {
360 // Error
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500361 fprintf(stderr, "Unexpected UUID format: %s", uuid);
vishwa1eaea4f2016-02-26 11:57:40 -0600362 rc = IPMI_CC_RESPONSE_ERROR;
363 goto finish;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500364 }
365
366 while (id_octet != NULL)
367 {
368 // Calculate the octet string size since it varies
369 // Divide it by 2 for the array size since 1 byte is built from 2 chars
370 int tmp_size = strlen(id_octet)/2;
371
372 for(i = 0; i < tmp_size; i++)
373 {
Joel Stanley38310cd2015-11-25 17:32:08 +1030374 char tmp_array[3] = {0}; // Holder of the 2 chars that will become a byte
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500375 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
376
377 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
378 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
379 resp_loc--;
380 id_octet+=2; // Finished with the 2 chars, advance
381 }
382 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
383 }
384
385 // Data length
386 *data_len = resp_size;
387
388 // Pack the actual response
389 memcpy(response, &resp_uuid, *data_len);
390
vishwa1eaea4f2016-02-26 11:57:40 -0600391finish:
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500392 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600393 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500394 free(busname);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500395
396 return rc;
397}
Chris Austen6caf28b2015-10-13 12:40:40 -0500398
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500399ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
400 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530401 ipmi_data_len_t data_len, ipmi_context_t context)
402{
403 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
404
405 // Status code.
406 ipmi_ret_t rc = IPMI_CC_OK;
407
Chris Austen6caf28b2015-10-13 12:40:40 -0500408 uint8_t str[] = {0x01, MAX_IPMI_BUFFER, MAX_IPMI_BUFFER, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530409
410 // Data length
411 *data_len = sizeof(str);
412
413 // Pack the actual response
414 memcpy(response, &str, *data_len);
415
416 return rc;
417}
418
Chris Austen6caf28b2015-10-13 12:40:40 -0500419
420struct set_wd_data_t {
421 uint8_t t_use;
422 uint8_t t_action;
423 uint8_t preset;
424 uint8_t flags;
425 uint8_t ls;
426 uint8_t ms;
427} __attribute__ ((packed));
428
429
430
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500431ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
432 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500433 ipmi_data_len_t data_len, ipmi_context_t context)
434{
Chris Austen454acfe2015-10-29 23:03:34 -0500435 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500436 const char *iface = "org.openbmc.Watchdog";
vishwa1eaea4f2016-02-26 11:57:40 -0600437 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500438 sd_bus_error error = SD_BUS_ERROR_NULL;
439 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500440
441 set_wd_data_t *reqptr = (set_wd_data_t*) request;
442 uint16_t timer = 0;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500443 uint32_t timer_ms = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500444 char *busname = NULL;
Chris Austen6caf28b2015-10-13 12:40:40 -0500445 *data_len = 0;
446
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500447 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500448 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500449 // Get timer value in ms
450 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500451
452 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
453
Sergey Solomineb9b8142016-08-23 09:07:28 -0500454 // Get bus name
455 r = mapper_get_service(bus, objname, &busname);
456 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400457 fprintf(stderr, "Failed to get %s bus name: %s\n",
458 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500459 goto finish;
460 }
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500461 // Set watchdog timer
vishwa1eaea4f2016-02-26 11:57:40 -0600462 r = sd_bus_call_method(bus, busname, objname, iface,
463 "set", &error, &reply, "i", timer_ms);
464 if(r < 0)
Adriana Kobylak0896be32015-10-22 13:27:23 -0500465 {
vishwa1eaea4f2016-02-26 11:57:40 -0600466 fprintf(stderr, "Failed to call the SET method: %s\n", strerror(-r));
467 goto finish;
Adriana Kobylak0896be32015-10-22 13:27:23 -0500468 }
469
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500470 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600471 reply = sd_bus_message_unref(reply);
Chris Austen6caf28b2015-10-13 12:40:40 -0500472
vishwa1eaea4f2016-02-26 11:57:40 -0600473 // Stop the current watchdog if any
474 r = sd_bus_call_method(bus, busname, objname, iface,
475 "stop", &error, &reply, NULL);
476 if(r < 0)
477 {
478 fprintf(stderr, "Failed to call the STOP method: %s\n", strerror(-r));
479 goto finish;
480 }
481
482 if (reqptr->t_use & 0x40)
483 {
484 sd_bus_error_free(&error);
485 reply = sd_bus_message_unref(reply);
486
487 // Start the watchdog if requested
488 r = sd_bus_call_method(bus, busname, objname, iface,
489 "start", &error, &reply, NULL);
490 if(r < 0)
491 {
492 fprintf(stderr, "Failed to call the START method: %s\n", strerror(-r));
493 }
494 }
495
496finish:
497 sd_bus_error_free(&error);
498 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500499 free(busname);
vishwa1eaea4f2016-02-26 11:57:40 -0600500
501 return (r < 0) ? -1 : IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -0500502}
503
504
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500505ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
506 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500507 ipmi_data_len_t data_len, ipmi_context_t context)
508{
Chris Austen454acfe2015-10-29 23:03:34 -0500509 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500510 const char *iface = "org.openbmc.Watchdog";
vishwa1eaea4f2016-02-26 11:57:40 -0600511 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500512 sd_bus_error error = SD_BUS_ERROR_NULL;
513 int r = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500514 char *busname = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500515
Chris Austen6caf28b2015-10-13 12:40:40 -0500516 // Status code.
517 ipmi_ret_t rc = IPMI_CC_OK;
518 *data_len = 0;
519
520 printf("WATCHDOG RESET\n");
Sergey Solomineb9b8142016-08-23 09:07:28 -0500521 // Get bus name
522 r = mapper_get_service(bus, objname, &busname);
523 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400524 fprintf(stderr, "Failed to get %s bus name: %s\n",
525 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500526 goto finish;
527 }
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500528 // Refresh watchdog
vishwa1eaea4f2016-02-26 11:57:40 -0600529 r = sd_bus_call_method(bus, busname, objname, iface,
530 "poke", &error, &reply, NULL);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500531 if (r < 0) {
vishwa1eaea4f2016-02-26 11:57:40 -0600532 fprintf(stderr, "Failed to add reset watchdog: %s\n", strerror(-r));
533 rc = -1;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500534 }
535
Sergey Solomineb9b8142016-08-23 09:07:28 -0500536finish:
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500537 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600538 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500539 free(busname);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500540
Chris Austen6caf28b2015-10-13 12:40:40 -0500541 return rc;
542}
543
Nan Li3d0df912016-10-18 19:51:41 +0800544extern struct channel_config_t channel_config;
545
546ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
547 ipmi_request_t request, ipmi_response_t response,
548 ipmi_data_len_t data_len, ipmi_context_t context)
549{
550 ipmi_ret_t rc = IPMI_CC_OK;
551
552 sd_bus *bus = ipmid_get_sd_bus_connection();
553 sd_bus_message *reply = nullptr;
554 sd_bus_error error = SD_BUS_ERROR_NULL;
555 int r = 0;
556 char *app = nullptr;
557 int family = 0;
558 unsigned char prefixlen = 0;
559 char* ipaddr = nullptr;
560 uint32_t mask = 0xFFFFFFFF;
561 char* gateway = nullptr;
562 char tmp_netmask[INET_ADDRSTRLEN];
563
564 // Todo: parse the request data if needed.
565
566 // Using Set Channel cmd to apply changes of Set Lan Cmd.
567
568 r = mapper_get_service(bus, app_obj, &app);
569 if (r < 0) {
570 fprintf(stderr, "Failed to get %s bus name: %s\n",
571 app_obj, strerror(-r));
572 rc = IPMI_CC_UNSPECIFIED_ERROR;
573 goto finish;
574 }
575
576 r = sd_bus_call_method(bus, app, app_obj, app_ifc, "GetAddress4", &error,
577 &reply, "s", app_nwinterface);
578 if (r < 0) {
579 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
580 rc = IPMI_CC_UNSPECIFIED_ERROR;
581 goto finish;
582 }
583
584 r = sd_bus_message_read(reply, "iyss",
585 &family, &prefixlen, &ipaddr, &gateway);
586 if (r < 0) {
587 fprintf(stderr, "Failed to get a response: %s\n", strerror(-r));
588 rc = IPMI_CC_RESPONSE_ERROR;
589 goto finish;
590 }
591
592 printf("N/W data from Cache: %s:%s:%s\n",
593 channel_config.new_ipaddr.c_str(),
594 channel_config.new_netmask.c_str(),
595 channel_config.new_gateway.c_str());
596
597 if(channel_config.new_ipaddr.empty()) {
598 channel_config.new_ipaddr.assign(ipaddr);
599 }
600
601 if(channel_config.new_netmask.empty()) {
602 mask = htonl(mask<<(32-prefixlen));
603 uint8_t* p = (uint8_t*)&mask;
604
605 snprintf(tmp_netmask, INET_ADDRSTRLEN, "%d.%d.%d.%d",
606 *p, *(p+1), *(p+2), *(p+3));
607 channel_config.new_netmask.assign(tmp_netmask);
608 }
609
610 if(channel_config.new_gateway.empty()) {
611 channel_config.new_gateway.assign(gateway);
612 }
613
614 printf("N/W data from HW %s:%d:%s:%s\n",
615 family==AF_INET?"IPv4":"IPv6", prefixlen, ipaddr,gateway);
616 printf("N/W data from Cache: %s:%s:%s\n",
617 channel_config.new_ipaddr.c_str(),
618 channel_config.new_netmask.c_str(),
619 channel_config.new_gateway.c_str());
620
621 r = sd_bus_call_method(bus, // On the System Bus
622 app, // Service to contact
623 app_obj, // Object path
624 app_ifc, // Interface name
625 "SetAddress4", // Method to be called
626 &error, // object to return error
627 &reply, // Response message on success
628 "ssss", // input message (Interface,
629 // IP Address, Netmask, Gateway)
630 app_nwinterface, // eth0
631 channel_config.new_ipaddr.c_str(),
632 channel_config.new_netmask.c_str(),
633 channel_config.new_gateway.c_str());
634 if(r < 0) {
635 fprintf(stderr, "Failed to set network data %s:%s:%s %s\n",
636 channel_config.new_ipaddr.c_str(),
637 channel_config.new_netmask.c_str(),
638 channel_config.new_gateway.c_str(),
639 error.message);
640 rc = IPMI_CC_UNSPECIFIED_ERROR;
641 }
642
643 channel_config.new_ipaddr.clear();
644 channel_config.new_netmask.clear();
645 channel_config.new_gateway.clear();
646
647finish:
648 sd_bus_error_free(&error);
649 reply = sd_bus_message_unref(reply);
650 free(app);
651
652 return rc;
653}
654
Chris Austenc2cd29d2016-02-05 20:02:29 -0600655// ATTENTION: This ipmi function is very hardcoded on purpose
656// OpenBMC does not fully support IPMI. This command is useful
657// to have around because it enables testing of interfaces with
658// the IPMI tool.
659#define GET_CHANNEL_INFO_CHANNEL_OFFSET 0
660// IPMI Table 6-2
661#define IPMI_CHANNEL_TYPE_IPMB 1
662// IPMI Table 6-3
663#define IPMI_CHANNEL_MEDIUM_TYPE_OTHER 6
664
665ipmi_ret_t ipmi_app_channel_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
666 ipmi_request_t request, ipmi_response_t response,
667 ipmi_data_len_t data_len, ipmi_context_t context)
668{
669 ipmi_ret_t rc = IPMI_CC_OK;
670 uint8_t resp[] = {
671 1,
672 IPMI_CHANNEL_MEDIUM_TYPE_OTHER,
673 IPMI_CHANNEL_TYPE_IPMB,
674 1,0x41,0xA7,0x00,0,0};
675 uint8_t *p = (uint8_t*) request;
676
677 printf("IPMI APP GET CHANNEL INFO\n");
678
tomjose7ec0add2016-06-27 07:59:28 -0500679 // The supported channels numbers are 1 and 8.
680 // Channel Number E is used as way to identify the current channel
681 // that the command is being is received from.
tomjose13fb4412016-03-08 14:02:34 -0600682 if (*p == 0xe || *p == 1 || *p == 8) {
Chris Austenc2cd29d2016-02-05 20:02:29 -0600683
684 *data_len = sizeof(resp);
685 memcpy(response, resp, *data_len);
686
687 } else {
688 rc = IPMI_CC_PARM_OUT_OF_RANGE;
689 *data_len = 0;
690 }
691
692 return rc;
693}
694
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500695ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
696 ipmi_request_t request, ipmi_response_t response,
697 ipmi_data_len_t data_len, ipmi_context_t context)
698{
699 ipmi_ret_t rc = IPMI_CC_OK;
700 *data_len = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500701
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500702 // Event and message logging enabled by default so return for now
703 printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
704
705 return rc;
706}
Chris Austen6caf28b2015-10-13 12:40:40 -0500707
708
709
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500710ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
711 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530712 ipmi_data_len_t data_len, ipmi_context_t context)
713{
714 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
715
716 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800717 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530718
719 *data_len = strlen("THIS IS WILDCARD");
720
721 // Now pack actual response
722 memcpy(response, "THIS IS WILDCARD", *data_len);
723
724 return rc;
725}
726
Chris Austen6caf28b2015-10-13 12:40:40 -0500727void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530728{
729 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Chris Austen6caf28b2015-10-13 12:40:40 -0500730 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities);
731
732 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
733 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler);
734
735 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
736 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog);
737
738 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
739 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog);
740
741 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
742 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id);
743
Nan Li41fa24a2016-11-10 20:12:37 +0800744 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS);
745 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL, ipmi_app_get_self_test_results);
746
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500747 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
748 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid);
749
Chris Austen6caf28b2015-10-13 12:40:40 -0500750 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
751 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state);
752
753 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_READ_EVENT);
754 ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL, ipmi_app_read_event);
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500755
756 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
757 IPMI_CMD_SET_BMC_GLOBAL_ENABLES);
758 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
759 ipmi_app_set_bmc_global_enables);
760
vishwa36993272015-11-20 12:43:49 -0600761 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS);
762 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS, NULL, ipmi_app_get_msg_flags);
763
Nan Li3d0df912016-10-18 19:51:41 +0800764 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
765 IPMI_CMD_SET_CHAN_ACCESS);
766 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
767 ipmi_set_channel_access);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600768
769 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
770 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info);
771
772
773
vishwabmcba0bd5f2015-09-30 16:50:23 +0530774 return;
775}
776
Chris Austen6caf28b2015-10-13 12:40:40 -0500777