blob: 28d97c94854ae6895a0008fb30894a3bb45ff4ff [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
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600408 // Per IPMI 2.0 spec, the input and output buffer size must be the max
409 // buffer size minus one byte to allocate space for the length byte.
410 uint8_t str[] = {0x01, MAX_IPMI_BUFFER-1, MAX_IPMI_BUFFER-1, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530411
412 // Data length
413 *data_len = sizeof(str);
414
415 // Pack the actual response
416 memcpy(response, &str, *data_len);
417
418 return rc;
419}
420
Chris Austen6caf28b2015-10-13 12:40:40 -0500421
422struct set_wd_data_t {
423 uint8_t t_use;
424 uint8_t t_action;
425 uint8_t preset;
426 uint8_t flags;
427 uint8_t ls;
428 uint8_t ms;
429} __attribute__ ((packed));
430
431
432
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500433ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
434 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500435 ipmi_data_len_t data_len, ipmi_context_t context)
436{
Chris Austen454acfe2015-10-29 23:03:34 -0500437 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500438 const char *iface = "org.openbmc.Watchdog";
vishwa1eaea4f2016-02-26 11:57:40 -0600439 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500440 sd_bus_error error = SD_BUS_ERROR_NULL;
441 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500442
443 set_wd_data_t *reqptr = (set_wd_data_t*) request;
444 uint16_t timer = 0;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500445 uint32_t timer_ms = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500446 char *busname = NULL;
Chris Austen6caf28b2015-10-13 12:40:40 -0500447 *data_len = 0;
448
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500449 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500450 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500451 // Get timer value in ms
452 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500453
454 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
455
Sergey Solomineb9b8142016-08-23 09:07:28 -0500456 // Get bus name
457 r = mapper_get_service(bus, objname, &busname);
458 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400459 fprintf(stderr, "Failed to get %s bus name: %s\n",
460 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500461 goto finish;
462 }
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500463 // Set watchdog timer
vishwa1eaea4f2016-02-26 11:57:40 -0600464 r = sd_bus_call_method(bus, busname, objname, iface,
465 "set", &error, &reply, "i", timer_ms);
466 if(r < 0)
Adriana Kobylak0896be32015-10-22 13:27:23 -0500467 {
vishwa1eaea4f2016-02-26 11:57:40 -0600468 fprintf(stderr, "Failed to call the SET method: %s\n", strerror(-r));
469 goto finish;
Adriana Kobylak0896be32015-10-22 13:27:23 -0500470 }
471
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500472 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600473 reply = sd_bus_message_unref(reply);
Chris Austen6caf28b2015-10-13 12:40:40 -0500474
vishwa1eaea4f2016-02-26 11:57:40 -0600475 // Stop the current watchdog if any
476 r = sd_bus_call_method(bus, busname, objname, iface,
477 "stop", &error, &reply, NULL);
478 if(r < 0)
479 {
480 fprintf(stderr, "Failed to call the STOP method: %s\n", strerror(-r));
481 goto finish;
482 }
483
484 if (reqptr->t_use & 0x40)
485 {
486 sd_bus_error_free(&error);
487 reply = sd_bus_message_unref(reply);
488
489 // Start the watchdog if requested
490 r = sd_bus_call_method(bus, busname, objname, iface,
491 "start", &error, &reply, NULL);
492 if(r < 0)
493 {
494 fprintf(stderr, "Failed to call the START method: %s\n", strerror(-r));
495 }
496 }
497
498finish:
499 sd_bus_error_free(&error);
500 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500501 free(busname);
vishwa1eaea4f2016-02-26 11:57:40 -0600502
503 return (r < 0) ? -1 : IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -0500504}
505
506
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500507ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
508 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500509 ipmi_data_len_t data_len, ipmi_context_t context)
510{
Chris Austen454acfe2015-10-29 23:03:34 -0500511 const char *objname = "/org/openbmc/watchdog/host0";
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500512 const char *iface = "org.openbmc.Watchdog";
vishwa1eaea4f2016-02-26 11:57:40 -0600513 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500514 sd_bus_error error = SD_BUS_ERROR_NULL;
515 int r = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500516 char *busname = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500517
Chris Austen6caf28b2015-10-13 12:40:40 -0500518 // Status code.
519 ipmi_ret_t rc = IPMI_CC_OK;
520 *data_len = 0;
521
522 printf("WATCHDOG RESET\n");
Sergey Solomineb9b8142016-08-23 09:07:28 -0500523 // Get bus name
524 r = mapper_get_service(bus, objname, &busname);
525 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400526 fprintf(stderr, "Failed to get %s bus name: %s\n",
527 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500528 goto finish;
529 }
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500530 // Refresh watchdog
vishwa1eaea4f2016-02-26 11:57:40 -0600531 r = sd_bus_call_method(bus, busname, objname, iface,
532 "poke", &error, &reply, NULL);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500533 if (r < 0) {
vishwa1eaea4f2016-02-26 11:57:40 -0600534 fprintf(stderr, "Failed to add reset watchdog: %s\n", strerror(-r));
535 rc = -1;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500536 }
537
Sergey Solomineb9b8142016-08-23 09:07:28 -0500538finish:
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500539 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600540 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500541 free(busname);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500542
Chris Austen6caf28b2015-10-13 12:40:40 -0500543 return rc;
544}
545
Nan Li3d0df912016-10-18 19:51:41 +0800546extern struct channel_config_t channel_config;
547
548ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
549 ipmi_request_t request, ipmi_response_t response,
550 ipmi_data_len_t data_len, ipmi_context_t context)
551{
552 ipmi_ret_t rc = IPMI_CC_OK;
553
554 sd_bus *bus = ipmid_get_sd_bus_connection();
555 sd_bus_message *reply = nullptr;
556 sd_bus_error error = SD_BUS_ERROR_NULL;
557 int r = 0;
558 char *app = nullptr;
559 int family = 0;
560 unsigned char prefixlen = 0;
561 char* ipaddr = nullptr;
562 uint32_t mask = 0xFFFFFFFF;
563 char* gateway = nullptr;
564 char tmp_netmask[INET_ADDRSTRLEN];
565
566 // Todo: parse the request data if needed.
567
568 // Using Set Channel cmd to apply changes of Set Lan Cmd.
569
570 r = mapper_get_service(bus, app_obj, &app);
571 if (r < 0) {
572 fprintf(stderr, "Failed to get %s bus name: %s\n",
573 app_obj, strerror(-r));
574 rc = IPMI_CC_UNSPECIFIED_ERROR;
575 goto finish;
576 }
577
578 r = sd_bus_call_method(bus, app, app_obj, app_ifc, "GetAddress4", &error,
579 &reply, "s", app_nwinterface);
580 if (r < 0) {
581 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
582 rc = IPMI_CC_UNSPECIFIED_ERROR;
583 goto finish;
584 }
585
586 r = sd_bus_message_read(reply, "iyss",
587 &family, &prefixlen, &ipaddr, &gateway);
588 if (r < 0) {
589 fprintf(stderr, "Failed to get a response: %s\n", strerror(-r));
590 rc = IPMI_CC_RESPONSE_ERROR;
591 goto finish;
592 }
593
594 printf("N/W data from Cache: %s:%s:%s\n",
595 channel_config.new_ipaddr.c_str(),
596 channel_config.new_netmask.c_str(),
597 channel_config.new_gateway.c_str());
598
599 if(channel_config.new_ipaddr.empty()) {
600 channel_config.new_ipaddr.assign(ipaddr);
601 }
602
603 if(channel_config.new_netmask.empty()) {
604 mask = htonl(mask<<(32-prefixlen));
605 uint8_t* p = (uint8_t*)&mask;
606
607 snprintf(tmp_netmask, INET_ADDRSTRLEN, "%d.%d.%d.%d",
608 *p, *(p+1), *(p+2), *(p+3));
609 channel_config.new_netmask.assign(tmp_netmask);
610 }
611
612 if(channel_config.new_gateway.empty()) {
613 channel_config.new_gateway.assign(gateway);
614 }
615
616 printf("N/W data from HW %s:%d:%s:%s\n",
617 family==AF_INET?"IPv4":"IPv6", prefixlen, ipaddr,gateway);
618 printf("N/W data from Cache: %s:%s:%s\n",
619 channel_config.new_ipaddr.c_str(),
620 channel_config.new_netmask.c_str(),
621 channel_config.new_gateway.c_str());
622
623 r = sd_bus_call_method(bus, // On the System Bus
624 app, // Service to contact
625 app_obj, // Object path
626 app_ifc, // Interface name
627 "SetAddress4", // Method to be called
628 &error, // object to return error
629 &reply, // Response message on success
630 "ssss", // input message (Interface,
631 // IP Address, Netmask, Gateway)
632 app_nwinterface, // eth0
633 channel_config.new_ipaddr.c_str(),
634 channel_config.new_netmask.c_str(),
635 channel_config.new_gateway.c_str());
636 if(r < 0) {
637 fprintf(stderr, "Failed to set network data %s:%s:%s %s\n",
638 channel_config.new_ipaddr.c_str(),
639 channel_config.new_netmask.c_str(),
640 channel_config.new_gateway.c_str(),
641 error.message);
642 rc = IPMI_CC_UNSPECIFIED_ERROR;
643 }
644
645 channel_config.new_ipaddr.clear();
646 channel_config.new_netmask.clear();
647 channel_config.new_gateway.clear();
648
649finish:
650 sd_bus_error_free(&error);
651 reply = sd_bus_message_unref(reply);
652 free(app);
653
654 return rc;
655}
656
Chris Austenc2cd29d2016-02-05 20:02:29 -0600657// ATTENTION: This ipmi function is very hardcoded on purpose
658// OpenBMC does not fully support IPMI. This command is useful
659// to have around because it enables testing of interfaces with
660// the IPMI tool.
661#define GET_CHANNEL_INFO_CHANNEL_OFFSET 0
662// IPMI Table 6-2
663#define IPMI_CHANNEL_TYPE_IPMB 1
664// IPMI Table 6-3
665#define IPMI_CHANNEL_MEDIUM_TYPE_OTHER 6
666
667ipmi_ret_t ipmi_app_channel_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
668 ipmi_request_t request, ipmi_response_t response,
669 ipmi_data_len_t data_len, ipmi_context_t context)
670{
671 ipmi_ret_t rc = IPMI_CC_OK;
672 uint8_t resp[] = {
673 1,
674 IPMI_CHANNEL_MEDIUM_TYPE_OTHER,
675 IPMI_CHANNEL_TYPE_IPMB,
676 1,0x41,0xA7,0x00,0,0};
677 uint8_t *p = (uint8_t*) request;
678
679 printf("IPMI APP GET CHANNEL INFO\n");
680
tomjose7ec0add2016-06-27 07:59:28 -0500681 // The supported channels numbers are 1 and 8.
682 // Channel Number E is used as way to identify the current channel
683 // that the command is being is received from.
tomjose13fb4412016-03-08 14:02:34 -0600684 if (*p == 0xe || *p == 1 || *p == 8) {
Chris Austenc2cd29d2016-02-05 20:02:29 -0600685
686 *data_len = sizeof(resp);
687 memcpy(response, resp, *data_len);
688
689 } else {
690 rc = IPMI_CC_PARM_OUT_OF_RANGE;
691 *data_len = 0;
692 }
693
694 return rc;
695}
696
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500697ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
698 ipmi_request_t request, ipmi_response_t response,
699 ipmi_data_len_t data_len, ipmi_context_t context)
700{
701 ipmi_ret_t rc = IPMI_CC_OK;
702 *data_len = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500703
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500704 // Event and message logging enabled by default so return for now
705 printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
706
707 return rc;
708}
Chris Austen6caf28b2015-10-13 12:40:40 -0500709
710
711
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500712ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
713 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530714 ipmi_data_len_t data_len, ipmi_context_t context)
715{
716 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
717
718 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800719 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530720
721 *data_len = strlen("THIS IS WILDCARD");
722
723 // Now pack actual response
724 memcpy(response, "THIS IS WILDCARD", *data_len);
725
726 return rc;
727}
728
Chris Austen6caf28b2015-10-13 12:40:40 -0500729void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530730{
731 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Chris Austen6caf28b2015-10-13 12:40:40 -0500732 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities);
733
734 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
735 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler);
736
737 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
738 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog);
739
740 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
741 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog);
742
743 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
744 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id);
745
Nan Li41fa24a2016-11-10 20:12:37 +0800746 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS);
747 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL, ipmi_app_get_self_test_results);
748
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500749 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
750 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid);
751
Chris Austen6caf28b2015-10-13 12:40:40 -0500752 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
753 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state);
754
755 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_READ_EVENT);
756 ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL, ipmi_app_read_event);
Adriana Kobylakdfc8d772015-10-20 09:34:48 -0500757
758 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
759 IPMI_CMD_SET_BMC_GLOBAL_ENABLES);
760 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
761 ipmi_app_set_bmc_global_enables);
762
vishwa36993272015-11-20 12:43:49 -0600763 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS);
764 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS, NULL, ipmi_app_get_msg_flags);
765
Nan Li3d0df912016-10-18 19:51:41 +0800766 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
767 IPMI_CMD_SET_CHAN_ACCESS);
768 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
769 ipmi_set_channel_access);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600770
771 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
772 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info);
773
774
775
vishwabmcba0bd5f2015-09-30 16:50:23 +0530776 return;
777}
778
Chris Austen6caf28b2015-10-13 12:40:40 -0500779