blob: 28a3c65d21929852e14c57d062b160d970654be8 [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
Adriana Kobylak3a552e12015-10-19 16:11:00 -050034ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
35 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050036 ipmi_data_len_t data_len, ipmi_context_t context)
37{
38 ipmi_ret_t rc = IPMI_CC_OK;
39 *data_len = 0;
40
41 printf("IPMI SET ACPI STATE Ignoring for now\n");
42 return rc;
43}
44
Chris Austen7303bdc2016-04-17 11:50:54 -050045
46typedef struct
47{
48 char major;
49 char minor;
Chris Austen176c9652016-04-30 16:32:17 -050050 uint16_t d[2];
Chris Austen7303bdc2016-04-17 11:50:54 -050051} rev_t;
52
53
54/* Currently only supports the vx.x-x-[-x] format Will return -1 if not in */
55/* the format this routine knows how to parse */
56/* version = v0.6-19-gf363f61-dirty */
57/* ^ ^ ^^ ^ */
58/* | | |----------|-- additional details */
59/* | |---------------- Minor */
60/* |------------------ Major */
61/* Additional details : If the option group exists it will force Auxiliary */
62/* Firmware Revision Information 4th byte to 1 indicating the build was */
63/* derived with additional edits */
64int convert_version(const char *p, rev_t *rev)
65{
66 char *s, *token;
Chris Austen176c9652016-04-30 16:32:17 -050067 uint16_t commits;
Chris Austen7303bdc2016-04-17 11:50:54 -050068
69 if (*p != 'v')
70 return -1;
71 p++;
72
73 s = strdup(p);
74 token = strtok(s,".-");
75
76 rev->major = (int8_t) atoi(token);
77
78 token = strtok(NULL, ".-");
79 rev->minor = (int8_t) atoi(token);
80
81 // Capture the number of commits on top of the minor tag.
82 // I'm using BE format like the ipmi spec asked for
83 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -050084
Chris Austen176c9652016-04-30 16:32:17 -050085 if (token) {
86 commits = (int16_t) atoi(token);
87 rev->d[0] = (commits>>8) | (commits<<8);
Chris Austen7303bdc2016-04-17 11:50:54 -050088
Chris Austen176c9652016-04-30 16:32:17 -050089 // commit number we skip
90 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -050091
Chris Austen176c9652016-04-30 16:32:17 -050092 } else {
93 rev->d[0] = 0;
94 }
Chris Austen7303bdc2016-04-17 11:50:54 -050095
96 // Any value of the optional parameter forces it to 1
Chris Austen176c9652016-04-30 16:32:17 -050097 if (token)
98 token = strtok(NULL,".-");
99
100 rev->d[1] = (token != NULL) ? 1 : 0;
Chris Austen7303bdc2016-04-17 11:50:54 -0500101
102 free(s);
103 return 0;
104}
105
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500106ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
107 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500108 ipmi_data_len_t data_len, ipmi_context_t context)
109{
110 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austen7303bdc2016-04-17 11:50:54 -0500111 const char *objname = "/org/openbmc/inventory/system/chassis/motherboard/bmc";
112 const char *iface = "org.openbmc.InventoryItem";
113 char *ver = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500114 char *busname = NULL;
Chris Austen7303bdc2016-04-17 11:50:54 -0500115 int r;
116 rev_t rev = {0};
Nan Liee0cb902016-07-11 15:38:03 +0800117 ipmi_device_id_t dev_id{};
Chris Austen6caf28b2015-10-13 12:40:40 -0500118
119 // Data length
Chris Austen7303bdc2016-04-17 11:50:54 -0500120 *data_len = sizeof(dev_id);
121
Nan Liee0cb902016-07-11 15:38:03 +0800122 // From IPMI spec, controller that have different application commands, or different
123 // definitions of OEM fields, are expected to have different Device ID values.
124 // Set to 0 now.
125
126 // Device Revision is set to 0 now.
127 // Bit7 identifies if device provide Device SDRs, obmc don't have SDR,we use ipmi to
128 // simulate SDR, hence the value:
129 dev_id.revision = 0x80;
130
131 // Firmware revision is already implemented, so get it from appropriate position.
Sergey Solomineb9b8142016-08-23 09:07:28 -0500132 r = mapper_get_service(bus, objname, &busname);
133 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400134 fprintf(stderr, "Failed to get %s bus name: %s\n",
135 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500136 goto finish;
137 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500138 r = sd_bus_get_property_string(bus,busname,objname,iface,"version", NULL, &ver);
139 if ( r < 0 ) {
140 fprintf(stderr, "Failed to obtain version property: %s\n", strerror(-r));
141 } else {
142 r = convert_version(ver, &rev);
143 if( r >= 0 ) {
Nan Liee0cb902016-07-11 15:38:03 +0800144 // bit7 identifies if the device is available, 0=normal operation,
145 // 1=device firmware, SDR update or self-initialization in progress.
146 // our SDR is normal working condition, so mask:
147 dev_id.fw[0] = 0x7F & rev.major;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500148
149 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Nan Liee0cb902016-07-11 15:38:03 +0800150 dev_id.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
151 memcpy(&dev_id.aux, rev.d, 4);
Chris Austen7303bdc2016-04-17 11:50:54 -0500152 }
153 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500154
Nan Liee0cb902016-07-11 15:38:03 +0800155 // IPMI Spec verison 2.0
156 dev_id.ipmi_ver = 2;
157
158 // Additional device Support.
159 // List the 'logical device' commands and functions that the controller supports
160 // that are in addition to the mandatory IPM and Application commands.
161 // [7] Chassis Device (device functions as chassis device per ICMB spec.)
162 // [6] Bridge (device responds to Bridge NetFn commands)
163 // [5] IPMB Event Generator
164 // [4] IPMB Event Receiver
165 // [3] FRU Inventory Device
166 // [2] SEL Device
167 // [1] SDR Repository Device
168 // [0] Sensor Device
169 // We support FRU/SEL/Sensor now:
170 dev_id.addn_dev_support = 0x8D;
171
172 // This value is the IANA number assigned to "IBM Platform Firmware
173 // Division", which is also used by our service processor. We may want
174 // a different number or at least a different version?
175 dev_id.manuf_id[0] = 0x41;
176 dev_id.manuf_id[1] = 0xA7;
177 dev_id.manuf_id[2] = 0x00;
178
179 // Witherspoon's product ID is hardcoded to 4F42(ASCII 'OB').
180 // TODO: openbmc/openbmc#495
181 dev_id.prod_id[0] = 0x4F;
182 dev_id.prod_id[1] = 0x42;
183
Chris Austen6caf28b2015-10-13 12:40:40 -0500184 // Pack the actual response
Chris Austen7303bdc2016-04-17 11:50:54 -0500185 memcpy(response, &dev_id, *data_len);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500186finish:
187 free(busname);
Chris Austen6caf28b2015-10-13 12:40:40 -0500188 return rc;
189}
190
Nan Li41fa24a2016-11-10 20:12:37 +0800191ipmi_ret_t ipmi_app_get_self_test_results(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
192 ipmi_request_t request, ipmi_response_t response,
193 ipmi_data_len_t data_len, ipmi_context_t context)
194{
195 ipmi_ret_t rc = IPMI_CC_OK;
196
197 // Byte 2:
198 // 55h - No error.
199 // 56h - Self Test funciton not implemented in this controller.
200 // 57h - Corrupted or inaccesssible data or devices.
201 // 58h - Fatal hardware error.
202 // FFh - reserved.
203 // all other: Device-specific 'internal failure'.
204 // Byte 3:
205 // For byte 2 = 55h, 56h, FFh: 00h
206 // For byte 2 = 58h, all other: Device-specific
207 // For byte 2 = 57h: self-test error bitfield.
208 // Note: returning 57h does not imply that all test were run.
209 // [7] 1b = Cannot access SEL device.
210 // [6] 1b = Cannot access SDR Repository.
211 // [5] 1b = Cannot access BMC FRU device.
212 // [4] 1b = IPMB signal lines do not respond.
213 // [3] 1b = SDR Repository empty.
214 // [2] 1b = Internal Use Area of BMC FRU corrupted.
215 // [1] 1b = controller update 'boot block' firmware corrupted.
216 // [0] 1b = controller operational firmware corrupted.
217
218 char selftestresults[2] = {0};
219
220 *data_len = 2;
221
222 selftestresults[0] = 0x56;
223 selftestresults[1] = 0;
224
225 memcpy(response, selftestresults, *data_len);
226
227 return rc;
228}
229
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500230ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
231 ipmi_request_t request, ipmi_response_t response,
232 ipmi_data_len_t data_len, ipmi_context_t context)
233{
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500234 const char *objname = "/org/openbmc/control/chassis0";
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600235 const char *iface = "org.freedesktop.DBus.Properties";
236 const char *chassis_iface = "org.openbmc.control.Chassis";
vishwa1eaea4f2016-02-26 11:57:40 -0600237 sd_bus_message *reply = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500238 sd_bus_error error = SD_BUS_ERROR_NULL;
239 int r = 0;
240 char *uuid = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500241 char *busname = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500242
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500243 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
244 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
245 // Ex: 0x2332fc2c40e66298e511f2782395a361
246
247 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
248 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
249 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
250 int i = 0;
251 char *tokptr = NULL;
vishwa1eaea4f2016-02-26 11:57:40 -0600252 char *id_octet = NULL;
253
254 // Status code.
255 ipmi_ret_t rc = IPMI_CC_OK;
256 *data_len = 0;
257
258 printf("IPMI GET DEVICE GUID\n");
259
260 // Call Get properties method with the interface and property name
Sergey Solomineb9b8142016-08-23 09:07:28 -0500261 r = mapper_get_service(bus, objname, &busname);
262 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400263 fprintf(stderr, "Failed to get %s bus name: %s\n",
264 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500265 goto finish;
266 }
vishwa1eaea4f2016-02-26 11:57:40 -0600267 r = sd_bus_call_method(bus,busname,objname,iface,
268 "Get",&error, &reply, "ss",
269 chassis_iface, "uuid");
270 if (r < 0)
271 {
272 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
273 rc = IPMI_CC_UNSPECIFIED_ERROR;
274 goto finish;
275 }
276
277 r = sd_bus_message_read(reply, "v", "s", &uuid);
278 if (r < 0 || uuid == NULL)
279 {
280 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
281 rc = IPMI_CC_RESPONSE_ERROR;
282 goto finish;
283 }
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500284
285 // Traverse the UUID
vishwa1eaea4f2016-02-26 11:57:40 -0600286 id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500287
288 if (id_octet == NULL)
vishwa1eaea4f2016-02-26 11:57:40 -0600289 {
290 // Error
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500291 fprintf(stderr, "Unexpected UUID format: %s", uuid);
vishwa1eaea4f2016-02-26 11:57:40 -0600292 rc = IPMI_CC_RESPONSE_ERROR;
293 goto finish;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500294 }
295
296 while (id_octet != NULL)
297 {
298 // Calculate the octet string size since it varies
299 // Divide it by 2 for the array size since 1 byte is built from 2 chars
300 int tmp_size = strlen(id_octet)/2;
301
302 for(i = 0; i < tmp_size; i++)
303 {
Joel Stanley38310cd2015-11-25 17:32:08 +1030304 char tmp_array[3] = {0}; // Holder of the 2 chars that will become a byte
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500305 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
306
307 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
308 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
309 resp_loc--;
310 id_octet+=2; // Finished with the 2 chars, advance
311 }
312 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
313 }
314
315 // Data length
316 *data_len = resp_size;
317
318 // Pack the actual response
319 memcpy(response, &resp_uuid, *data_len);
320
vishwa1eaea4f2016-02-26 11:57:40 -0600321finish:
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500322 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600323 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500324 free(busname);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500325
326 return rc;
327}
Chris Austen6caf28b2015-10-13 12:40:40 -0500328
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500329ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
330 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530331 ipmi_data_len_t data_len, ipmi_context_t context)
332{
333 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
334
335 // Status code.
336 ipmi_ret_t rc = IPMI_CC_OK;
337
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600338 // Per IPMI 2.0 spec, the input and output buffer size must be the max
339 // buffer size minus one byte to allocate space for the length byte.
340 uint8_t str[] = {0x01, MAX_IPMI_BUFFER-1, MAX_IPMI_BUFFER-1, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530341
342 // Data length
343 *data_len = sizeof(str);
344
345 // Pack the actual response
346 memcpy(response, &str, *data_len);
347
348 return rc;
349}
350
Chris Austen6caf28b2015-10-13 12:40:40 -0500351
352struct set_wd_data_t {
353 uint8_t t_use;
354 uint8_t t_action;
355 uint8_t preset;
356 uint8_t flags;
357 uint8_t ls;
358 uint8_t ms;
359} __attribute__ ((packed));
360
361
362
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500363ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
364 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500365 ipmi_data_len_t data_len, ipmi_context_t context)
366{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530367 const char *objname = "/xyz/openbmc_project/watchdog/host0";
368 const char *iface = "xyz.openbmc_project.State.Watchdog";
369 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600370 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500371 sd_bus_error error = SD_BUS_ERROR_NULL;
372 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500373
374 set_wd_data_t *reqptr = (set_wd_data_t*) request;
375 uint16_t timer = 0;
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530376
377 // Making this uint64_t to match with provider
378 uint64_t timer_ms = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500379 char *busname = NULL;
Chris Austen6caf28b2015-10-13 12:40:40 -0500380 *data_len = 0;
381
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500382 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500383 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500384 // Get timer value in ms
385 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500386
387 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
388
Sergey Solomineb9b8142016-08-23 09:07:28 -0500389 // Get bus name
390 r = mapper_get_service(bus, objname, &busname);
391 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400392 fprintf(stderr, "Failed to get %s bus name: %s\n",
393 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500394 goto finish;
395 }
Adriana Kobylak0896be32015-10-22 13:27:23 -0500396
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530397 // Disable watchdog if running
398 r = sd_bus_call_method(bus, busname, objname, property_iface,
399 "Set", &error, &reply, "ssv",
400 iface, "Enabled", "b", false);
401 if(r < 0) {
402 fprintf(stderr, "Failed to disable Watchdog: %s\n",
403 strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600404 goto finish;
405 }
406
407 if (reqptr->t_use & 0x40)
408 {
409 sd_bus_error_free(&error);
410 reply = sd_bus_message_unref(reply);
411
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530412 // Now Enable Watchdog
413 r = sd_bus_call_method(bus, busname, objname, property_iface,
414 "Set", &error, &reply, "ssv",
415 iface, "Enabled", "b", true);
416 if(r < 0) {
417 fprintf(stderr, "Failed to Enable Watchdog: %s\n",
418 strerror(-r));
419 goto finish;
420 }
421
422 // Set watchdog timer
423 r = sd_bus_call_method(bus, busname, objname, property_iface,
424 "Set", &error, &reply, "ssv",
425 iface, "TimeRemaining", "t", timer_ms);
426 if(r < 0) {
427 fprintf(stderr, "Failed to set new expiration time: %s\n",
428 strerror(-r));
429 goto finish;
vishwa1eaea4f2016-02-26 11:57:40 -0600430 }
431 }
432
433finish:
434 sd_bus_error_free(&error);
435 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500436 free(busname);
vishwa1eaea4f2016-02-26 11:57:40 -0600437
438 return (r < 0) ? -1 : IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -0500439}
440
441
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500442ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
443 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500444 ipmi_data_len_t data_len, ipmi_context_t context)
445{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530446 const char *objname = "/xyz/openbmc_project/watchdog/host0";
447 const char *iface = "xyz.openbmc_project.State.Watchdog";
448 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600449 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500450 sd_bus_error error = SD_BUS_ERROR_NULL;
451 int r = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500452 char *busname = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500453
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530454 // Current time interval that is set in watchdog.
455 uint64_t interval = 0;
456
Chris Austen6caf28b2015-10-13 12:40:40 -0500457 // Status code.
458 ipmi_ret_t rc = IPMI_CC_OK;
459 *data_len = 0;
460
461 printf("WATCHDOG RESET\n");
Sergey Solomineb9b8142016-08-23 09:07:28 -0500462 // Get bus name
463 r = mapper_get_service(bus, objname, &busname);
464 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400465 fprintf(stderr, "Failed to get %s bus name: %s\n",
466 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500467 goto finish;
468 }
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530469
470 // Get the current interval and set it back.
471 r = sd_bus_call_method(bus, busname, objname, property_iface,
472 "Get", &error, &reply, "ss",
473 iface, "Interval");
474
475 if(r < 0) {
476 fprintf(stderr, "Failed to get current Interval msg: %s\n",
477 strerror(-r));
478 goto finish;
479 }
480
481 // Now extract the value
482 r = sd_bus_message_read(reply, "v", "t", &interval);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500483 if (r < 0) {
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530484 fprintf(stderr, "Failed to read current interval: %s\n",
485 strerror(-r));
486 goto finish;
487 }
488
489 sd_bus_error_free(&error);
490 reply = sd_bus_message_unref(reply);
491
492 // Set watchdog timer
493 r = sd_bus_call_method(bus, busname, objname, property_iface,
494 "Set", &error, &reply, "ssv",
495 iface, "TimeRemaining", "t", interval);
496 if(r < 0) {
497 fprintf(stderr, "Failed to refresh the timer: %s\n",
498 strerror(-r));
499 goto finish;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500500 }
501
Sergey Solomineb9b8142016-08-23 09:07:28 -0500502finish:
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500503 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600504 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500505 free(busname);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500506
Chris Austen6caf28b2015-10-13 12:40:40 -0500507 return rc;
508}
509
Nan Li3d0df912016-10-18 19:51:41 +0800510extern struct channel_config_t channel_config;
511
512ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
513 ipmi_request_t request, ipmi_response_t response,
514 ipmi_data_len_t data_len, ipmi_context_t context)
515{
516 ipmi_ret_t rc = IPMI_CC_OK;
517
518 sd_bus *bus = ipmid_get_sd_bus_connection();
519 sd_bus_message *reply = nullptr;
520 sd_bus_error error = SD_BUS_ERROR_NULL;
521 int r = 0;
522 char *app = nullptr;
523 int family = 0;
524 unsigned char prefixlen = 0;
525 char* ipaddr = nullptr;
526 uint32_t mask = 0xFFFFFFFF;
527 char* gateway = nullptr;
528 char tmp_netmask[INET_ADDRSTRLEN];
529
530 // Todo: parse the request data if needed.
531
532 // Using Set Channel cmd to apply changes of Set Lan Cmd.
533
534 r = mapper_get_service(bus, app_obj, &app);
535 if (r < 0) {
536 fprintf(stderr, "Failed to get %s bus name: %s\n",
537 app_obj, strerror(-r));
538 rc = IPMI_CC_UNSPECIFIED_ERROR;
539 goto finish;
540 }
541
542 r = sd_bus_call_method(bus, app, app_obj, app_ifc, "GetAddress4", &error,
543 &reply, "s", app_nwinterface);
544 if (r < 0) {
545 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
546 rc = IPMI_CC_UNSPECIFIED_ERROR;
547 goto finish;
548 }
549
550 r = sd_bus_message_read(reply, "iyss",
551 &family, &prefixlen, &ipaddr, &gateway);
552 if (r < 0) {
553 fprintf(stderr, "Failed to get a response: %s\n", strerror(-r));
554 rc = IPMI_CC_RESPONSE_ERROR;
555 goto finish;
556 }
557
558 printf("N/W data from Cache: %s:%s:%s\n",
559 channel_config.new_ipaddr.c_str(),
560 channel_config.new_netmask.c_str(),
561 channel_config.new_gateway.c_str());
562
563 if(channel_config.new_ipaddr.empty()) {
564 channel_config.new_ipaddr.assign(ipaddr);
565 }
566
567 if(channel_config.new_netmask.empty()) {
568 mask = htonl(mask<<(32-prefixlen));
569 uint8_t* p = (uint8_t*)&mask;
570
571 snprintf(tmp_netmask, INET_ADDRSTRLEN, "%d.%d.%d.%d",
572 *p, *(p+1), *(p+2), *(p+3));
573 channel_config.new_netmask.assign(tmp_netmask);
574 }
575
576 if(channel_config.new_gateway.empty()) {
577 channel_config.new_gateway.assign(gateway);
578 }
579
580 printf("N/W data from HW %s:%d:%s:%s\n",
581 family==AF_INET?"IPv4":"IPv6", prefixlen, ipaddr,gateway);
582 printf("N/W data from Cache: %s:%s:%s\n",
583 channel_config.new_ipaddr.c_str(),
584 channel_config.new_netmask.c_str(),
585 channel_config.new_gateway.c_str());
586
587 r = sd_bus_call_method(bus, // On the System Bus
588 app, // Service to contact
589 app_obj, // Object path
590 app_ifc, // Interface name
591 "SetAddress4", // Method to be called
592 &error, // object to return error
593 &reply, // Response message on success
594 "ssss", // input message (Interface,
595 // IP Address, Netmask, Gateway)
596 app_nwinterface, // eth0
597 channel_config.new_ipaddr.c_str(),
598 channel_config.new_netmask.c_str(),
599 channel_config.new_gateway.c_str());
600 if(r < 0) {
601 fprintf(stderr, "Failed to set network data %s:%s:%s %s\n",
602 channel_config.new_ipaddr.c_str(),
603 channel_config.new_netmask.c_str(),
604 channel_config.new_gateway.c_str(),
605 error.message);
606 rc = IPMI_CC_UNSPECIFIED_ERROR;
607 }
608
609 channel_config.new_ipaddr.clear();
610 channel_config.new_netmask.clear();
611 channel_config.new_gateway.clear();
612
613finish:
614 sd_bus_error_free(&error);
615 reply = sd_bus_message_unref(reply);
616 free(app);
617
618 return rc;
619}
620
Chris Austenc2cd29d2016-02-05 20:02:29 -0600621// ATTENTION: This ipmi function is very hardcoded on purpose
622// OpenBMC does not fully support IPMI. This command is useful
623// to have around because it enables testing of interfaces with
624// the IPMI tool.
625#define GET_CHANNEL_INFO_CHANNEL_OFFSET 0
626// IPMI Table 6-2
627#define IPMI_CHANNEL_TYPE_IPMB 1
628// IPMI Table 6-3
629#define IPMI_CHANNEL_MEDIUM_TYPE_OTHER 6
630
631ipmi_ret_t ipmi_app_channel_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
632 ipmi_request_t request, ipmi_response_t response,
633 ipmi_data_len_t data_len, ipmi_context_t context)
634{
635 ipmi_ret_t rc = IPMI_CC_OK;
636 uint8_t resp[] = {
637 1,
638 IPMI_CHANNEL_MEDIUM_TYPE_OTHER,
639 IPMI_CHANNEL_TYPE_IPMB,
640 1,0x41,0xA7,0x00,0,0};
641 uint8_t *p = (uint8_t*) request;
642
643 printf("IPMI APP GET CHANNEL INFO\n");
644
tomjose7ec0add2016-06-27 07:59:28 -0500645 // The supported channels numbers are 1 and 8.
646 // Channel Number E is used as way to identify the current channel
647 // that the command is being is received from.
tomjose13fb4412016-03-08 14:02:34 -0600648 if (*p == 0xe || *p == 1 || *p == 8) {
Chris Austenc2cd29d2016-02-05 20:02:29 -0600649
650 *data_len = sizeof(resp);
651 memcpy(response, resp, *data_len);
652
653 } else {
654 rc = IPMI_CC_PARM_OUT_OF_RANGE;
655 *data_len = 0;
656 }
657
658 return rc;
659}
660
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500661ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
662 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530663 ipmi_data_len_t data_len, ipmi_context_t context)
664{
665 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
666
667 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800668 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530669
670 *data_len = strlen("THIS IS WILDCARD");
671
672 // Now pack actual response
673 memcpy(response, "THIS IS WILDCARD", *data_len);
674
675 return rc;
676}
677
Chris Austen6caf28b2015-10-13 12:40:40 -0500678void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530679{
Tom05732372016-09-06 17:21:23 +0530680 // <Get BT Interface Capabilities>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530681 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Tom05732372016-09-06 17:21:23 +0530682 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities,
683 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500684
Tom05732372016-09-06 17:21:23 +0530685 // <Wildcard Command>
Chris Austen6caf28b2015-10-13 12:40:40 -0500686 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
Tom05732372016-09-06 17:21:23 +0530687 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler,
688 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500689
Tom05732372016-09-06 17:21:23 +0530690 // <Reset Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500691 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
Tom05732372016-09-06 17:21:23 +0530692 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog,
693 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500694
Tom05732372016-09-06 17:21:23 +0530695 // <Set Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500696 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
Tom05732372016-09-06 17:21:23 +0530697 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog,
698 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500699
Tom05732372016-09-06 17:21:23 +0530700 // <Get Device ID>
Chris Austen6caf28b2015-10-13 12:40:40 -0500701 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
Tom05732372016-09-06 17:21:23 +0530702 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id,
703 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500704
Tom05732372016-09-06 17:21:23 +0530705 // <Get Self Test Results>
Nan Li41fa24a2016-11-10 20:12:37 +0800706 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS);
Tom05732372016-09-06 17:21:23 +0530707 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL,
708 ipmi_app_get_self_test_results, PRIVILEGE_USER);
Nan Li41fa24a2016-11-10 20:12:37 +0800709
Tom05732372016-09-06 17:21:23 +0530710 // <Get Device GUID>
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500711 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
Tom05732372016-09-06 17:21:23 +0530712 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid,
713 PRIVILEGE_USER);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500714
Tom05732372016-09-06 17:21:23 +0530715 // <Set ACPI Power State>
Chris Austen6caf28b2015-10-13 12:40:40 -0500716 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
Tom05732372016-09-06 17:21:23 +0530717 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state,
718 PRIVILEGE_ADMIN);
Chris Austen6caf28b2015-10-13 12:40:40 -0500719
Tom05732372016-09-06 17:21:23 +0530720 // <Set Channel Access>
Nan Li3d0df912016-10-18 19:51:41 +0800721 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
722 IPMI_CMD_SET_CHAN_ACCESS);
723 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
Tom05732372016-09-06 17:21:23 +0530724 ipmi_set_channel_access, PRIVILEGE_ADMIN);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600725
Tom05732372016-09-06 17:21:23 +0530726 // <Get Channel Info Command>
Chris Austenc2cd29d2016-02-05 20:02:29 -0600727 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
Tom05732372016-09-06 17:21:23 +0530728 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info,
729 PRIVILEGE_USER);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600730
vishwabmcba0bd5f2015-09-30 16:50:23 +0530731 return;
732}
733
Chris Austen6caf28b2015-10-13 12:40:40 -0500734