blob: 7b30eddb88edb0dae04a0b520e1c7213bfd29ae0 [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"
Ratan Guptab8e99552017-07-27 07:07:48 +05304#include "types.hpp"
5#include "utils.hpp"
6
vishwabmcba0bd5f2015-09-30 16:50:23 +05307#include <stdio.h>
8#include <string.h>
Chris Austen6caf28b2015-10-13 12:40:40 -05009#include <stdint.h>
Adriana Kobylak3a552e12015-10-19 16:11:00 -050010#include <systemd/sd-bus.h>
Sergey Solomineb9b8142016-08-23 09:07:28 -050011#include <mapper.h>
Nan Liee0cb902016-07-11 15:38:03 +080012#include <array>
Nan Li3d0df912016-10-18 19:51:41 +080013#include <arpa/inet.h>
Ratan Guptab8e99552017-07-27 07:07:48 +053014#include "transporthandler.hpp"
15
16#include <phosphor-logging/log.hpp>
17#include <phosphor-logging/elog-errors.hpp>
18#include "xyz/openbmc_project/Common/error.hpp"
19
Adriana Kobylak3a552e12015-10-19 16:11:00 -050020
21extern sd_bus *bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053022
Nan Li3d0df912016-10-18 19:51:41 +080023constexpr auto app_obj = "/org/openbmc/NetworkManager/Interface";
24constexpr auto app_ifc = "org.openbmc.NetworkManager";
25constexpr auto app_nwinterface = "eth0";
26
Ratan Guptab8e99552017-07-27 07:07:48 +053027constexpr auto ipv4Protocol = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
28
Chris Austen6caf28b2015-10-13 12:40:40 -050029void register_netfn_app_functions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053030
Ratan Guptab8e99552017-07-27 07:07:48 +053031using namespace phosphor::logging;
32using namespace sdbusplus::xyz::openbmc_project::Common::Error;
33
Nan Liee0cb902016-07-11 15:38:03 +080034// Offset in get device id command.
35typedef struct
36{
37 uint8_t id;
38 uint8_t revision;
39 uint8_t fw[2];
40 uint8_t ipmi_ver;
41 uint8_t addn_dev_support;
42 uint8_t manuf_id[3];
43 uint8_t prod_id[2];
44 uint8_t aux[4];
45}__attribute__((packed)) ipmi_device_id_t;
Chris Austen7303bdc2016-04-17 11:50:54 -050046
Adriana Kobylak3a552e12015-10-19 16:11:00 -050047ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
48 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050049 ipmi_data_len_t data_len, ipmi_context_t context)
50{
51 ipmi_ret_t rc = IPMI_CC_OK;
52 *data_len = 0;
53
54 printf("IPMI SET ACPI STATE Ignoring for now\n");
55 return rc;
56}
57
Chris Austen7303bdc2016-04-17 11:50:54 -050058
59typedef struct
60{
61 char major;
62 char minor;
Chris Austen176c9652016-04-30 16:32:17 -050063 uint16_t d[2];
Chris Austen7303bdc2016-04-17 11:50:54 -050064} rev_t;
65
66
67/* Currently only supports the vx.x-x-[-x] format Will return -1 if not in */
68/* the format this routine knows how to parse */
69/* version = v0.6-19-gf363f61-dirty */
70/* ^ ^ ^^ ^ */
71/* | | |----------|-- additional details */
72/* | |---------------- Minor */
73/* |------------------ Major */
74/* Additional details : If the option group exists it will force Auxiliary */
75/* Firmware Revision Information 4th byte to 1 indicating the build was */
76/* derived with additional edits */
77int convert_version(const char *p, rev_t *rev)
78{
79 char *s, *token;
Chris Austen176c9652016-04-30 16:32:17 -050080 uint16_t commits;
Chris Austen7303bdc2016-04-17 11:50:54 -050081
82 if (*p != 'v')
83 return -1;
84 p++;
85
86 s = strdup(p);
87 token = strtok(s,".-");
88
89 rev->major = (int8_t) atoi(token);
90
91 token = strtok(NULL, ".-");
92 rev->minor = (int8_t) atoi(token);
93
94 // Capture the number of commits on top of the minor tag.
95 // I'm using BE format like the ipmi spec asked for
96 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -050097
Chris Austen176c9652016-04-30 16:32:17 -050098 if (token) {
99 commits = (int16_t) atoi(token);
100 rev->d[0] = (commits>>8) | (commits<<8);
Chris Austen7303bdc2016-04-17 11:50:54 -0500101
Chris Austen176c9652016-04-30 16:32:17 -0500102 // commit number we skip
103 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -0500104
Chris Austen176c9652016-04-30 16:32:17 -0500105 } else {
106 rev->d[0] = 0;
107 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500108
109 // Any value of the optional parameter forces it to 1
Chris Austen176c9652016-04-30 16:32:17 -0500110 if (token)
111 token = strtok(NULL,".-");
112
113 rev->d[1] = (token != NULL) ? 1 : 0;
Chris Austen7303bdc2016-04-17 11:50:54 -0500114
115 free(s);
116 return 0;
117}
118
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500119ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
120 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500121 ipmi_data_len_t data_len, ipmi_context_t context)
122{
123 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austen7303bdc2016-04-17 11:50:54 -0500124 const char *objname = "/org/openbmc/inventory/system/chassis/motherboard/bmc";
125 const char *iface = "org.openbmc.InventoryItem";
126 char *ver = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500127 char *busname = NULL;
Chris Austen7303bdc2016-04-17 11:50:54 -0500128 int r;
129 rev_t rev = {0};
Nan Liee0cb902016-07-11 15:38:03 +0800130 ipmi_device_id_t dev_id{};
Chris Austen6caf28b2015-10-13 12:40:40 -0500131
132 // Data length
Chris Austen7303bdc2016-04-17 11:50:54 -0500133 *data_len = sizeof(dev_id);
134
Nan Liee0cb902016-07-11 15:38:03 +0800135 // From IPMI spec, controller that have different application commands, or different
136 // definitions of OEM fields, are expected to have different Device ID values.
137 // Set to 0 now.
138
139 // Device Revision is set to 0 now.
140 // Bit7 identifies if device provide Device SDRs, obmc don't have SDR,we use ipmi to
141 // simulate SDR, hence the value:
142 dev_id.revision = 0x80;
143
144 // Firmware revision is already implemented, so get it from appropriate position.
Sergey Solomineb9b8142016-08-23 09:07:28 -0500145 r = mapper_get_service(bus, objname, &busname);
146 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400147 fprintf(stderr, "Failed to get %s bus name: %s\n",
148 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500149 goto finish;
150 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500151 r = sd_bus_get_property_string(bus,busname,objname,iface,"version", NULL, &ver);
152 if ( r < 0 ) {
153 fprintf(stderr, "Failed to obtain version property: %s\n", strerror(-r));
154 } else {
155 r = convert_version(ver, &rev);
156 if( r >= 0 ) {
Nan Liee0cb902016-07-11 15:38:03 +0800157 // bit7 identifies if the device is available, 0=normal operation,
158 // 1=device firmware, SDR update or self-initialization in progress.
159 // our SDR is normal working condition, so mask:
160 dev_id.fw[0] = 0x7F & rev.major;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500161
162 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Nan Liee0cb902016-07-11 15:38:03 +0800163 dev_id.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
164 memcpy(&dev_id.aux, rev.d, 4);
Chris Austen7303bdc2016-04-17 11:50:54 -0500165 }
166 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500167
Nan Liee0cb902016-07-11 15:38:03 +0800168 // IPMI Spec verison 2.0
169 dev_id.ipmi_ver = 2;
170
171 // Additional device Support.
172 // List the 'logical device' commands and functions that the controller supports
173 // that are in addition to the mandatory IPM and Application commands.
174 // [7] Chassis Device (device functions as chassis device per ICMB spec.)
175 // [6] Bridge (device responds to Bridge NetFn commands)
176 // [5] IPMB Event Generator
177 // [4] IPMB Event Receiver
178 // [3] FRU Inventory Device
179 // [2] SEL Device
180 // [1] SDR Repository Device
181 // [0] Sensor Device
182 // We support FRU/SEL/Sensor now:
183 dev_id.addn_dev_support = 0x8D;
184
185 // This value is the IANA number assigned to "IBM Platform Firmware
186 // Division", which is also used by our service processor. We may want
187 // a different number or at least a different version?
188 dev_id.manuf_id[0] = 0x41;
189 dev_id.manuf_id[1] = 0xA7;
190 dev_id.manuf_id[2] = 0x00;
191
192 // Witherspoon's product ID is hardcoded to 4F42(ASCII 'OB').
193 // TODO: openbmc/openbmc#495
194 dev_id.prod_id[0] = 0x4F;
195 dev_id.prod_id[1] = 0x42;
196
Chris Austen6caf28b2015-10-13 12:40:40 -0500197 // Pack the actual response
Chris Austen7303bdc2016-04-17 11:50:54 -0500198 memcpy(response, &dev_id, *data_len);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500199finish:
200 free(busname);
Xo Wange5536382017-07-20 20:14:00 -0700201 free(ver);
Chris Austen6caf28b2015-10-13 12:40:40 -0500202 return rc;
203}
204
Nan Li41fa24a2016-11-10 20:12:37 +0800205ipmi_ret_t ipmi_app_get_self_test_results(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
206 ipmi_request_t request, ipmi_response_t response,
207 ipmi_data_len_t data_len, ipmi_context_t context)
208{
209 ipmi_ret_t rc = IPMI_CC_OK;
210
211 // Byte 2:
212 // 55h - No error.
213 // 56h - Self Test funciton not implemented in this controller.
214 // 57h - Corrupted or inaccesssible data or devices.
215 // 58h - Fatal hardware error.
216 // FFh - reserved.
217 // all other: Device-specific 'internal failure'.
218 // Byte 3:
219 // For byte 2 = 55h, 56h, FFh: 00h
220 // For byte 2 = 58h, all other: Device-specific
221 // For byte 2 = 57h: self-test error bitfield.
222 // Note: returning 57h does not imply that all test were run.
223 // [7] 1b = Cannot access SEL device.
224 // [6] 1b = Cannot access SDR Repository.
225 // [5] 1b = Cannot access BMC FRU device.
226 // [4] 1b = IPMB signal lines do not respond.
227 // [3] 1b = SDR Repository empty.
228 // [2] 1b = Internal Use Area of BMC FRU corrupted.
229 // [1] 1b = controller update 'boot block' firmware corrupted.
230 // [0] 1b = controller operational firmware corrupted.
231
232 char selftestresults[2] = {0};
233
234 *data_len = 2;
235
236 selftestresults[0] = 0x56;
237 selftestresults[1] = 0;
238
239 memcpy(response, selftestresults, *data_len);
240
241 return rc;
242}
243
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500244ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
245 ipmi_request_t request, ipmi_response_t response,
246 ipmi_data_len_t data_len, ipmi_context_t context)
247{
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500248 const char *objname = "/org/openbmc/control/chassis0";
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600249 const char *iface = "org.freedesktop.DBus.Properties";
250 const char *chassis_iface = "org.openbmc.control.Chassis";
vishwa1eaea4f2016-02-26 11:57:40 -0600251 sd_bus_message *reply = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500252 sd_bus_error error = SD_BUS_ERROR_NULL;
253 int r = 0;
254 char *uuid = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500255 char *busname = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500256
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500257 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
258 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
259 // Ex: 0x2332fc2c40e66298e511f2782395a361
260
261 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
262 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
263 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
264 int i = 0;
265 char *tokptr = NULL;
vishwa1eaea4f2016-02-26 11:57:40 -0600266 char *id_octet = NULL;
267
268 // Status code.
269 ipmi_ret_t rc = IPMI_CC_OK;
270 *data_len = 0;
271
272 printf("IPMI GET DEVICE GUID\n");
273
274 // Call Get properties method with the interface and property name
Sergey Solomineb9b8142016-08-23 09:07:28 -0500275 r = mapper_get_service(bus, objname, &busname);
276 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400277 fprintf(stderr, "Failed to get %s bus name: %s\n",
278 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500279 goto finish;
280 }
vishwa1eaea4f2016-02-26 11:57:40 -0600281 r = sd_bus_call_method(bus,busname,objname,iface,
282 "Get",&error, &reply, "ss",
283 chassis_iface, "uuid");
284 if (r < 0)
285 {
286 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
287 rc = IPMI_CC_UNSPECIFIED_ERROR;
288 goto finish;
289 }
290
291 r = sd_bus_message_read(reply, "v", "s", &uuid);
292 if (r < 0 || uuid == NULL)
293 {
294 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
295 rc = IPMI_CC_RESPONSE_ERROR;
296 goto finish;
297 }
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500298
299 // Traverse the UUID
vishwa1eaea4f2016-02-26 11:57:40 -0600300 id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500301
302 if (id_octet == NULL)
vishwa1eaea4f2016-02-26 11:57:40 -0600303 {
304 // Error
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500305 fprintf(stderr, "Unexpected UUID format: %s", uuid);
vishwa1eaea4f2016-02-26 11:57:40 -0600306 rc = IPMI_CC_RESPONSE_ERROR;
307 goto finish;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500308 }
309
310 while (id_octet != NULL)
311 {
312 // Calculate the octet string size since it varies
313 // Divide it by 2 for the array size since 1 byte is built from 2 chars
314 int tmp_size = strlen(id_octet)/2;
315
316 for(i = 0; i < tmp_size; i++)
317 {
Joel Stanley38310cd2015-11-25 17:32:08 +1030318 char tmp_array[3] = {0}; // Holder of the 2 chars that will become a byte
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500319 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
320
321 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
322 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
323 resp_loc--;
324 id_octet+=2; // Finished with the 2 chars, advance
325 }
326 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
327 }
328
329 // Data length
330 *data_len = resp_size;
331
332 // Pack the actual response
333 memcpy(response, &resp_uuid, *data_len);
334
vishwa1eaea4f2016-02-26 11:57:40 -0600335finish:
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500336 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600337 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500338 free(busname);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500339
340 return rc;
341}
Chris Austen6caf28b2015-10-13 12:40:40 -0500342
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500343ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
344 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530345 ipmi_data_len_t data_len, ipmi_context_t context)
346{
347 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
348
349 // Status code.
350 ipmi_ret_t rc = IPMI_CC_OK;
351
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600352 // Per IPMI 2.0 spec, the input and output buffer size must be the max
353 // buffer size minus one byte to allocate space for the length byte.
354 uint8_t str[] = {0x01, MAX_IPMI_BUFFER-1, MAX_IPMI_BUFFER-1, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530355
356 // Data length
357 *data_len = sizeof(str);
358
359 // Pack the actual response
360 memcpy(response, &str, *data_len);
361
362 return rc;
363}
364
Chris Austen6caf28b2015-10-13 12:40:40 -0500365
366struct set_wd_data_t {
367 uint8_t t_use;
368 uint8_t t_action;
369 uint8_t preset;
370 uint8_t flags;
371 uint8_t ls;
372 uint8_t ms;
373} __attribute__ ((packed));
374
375
376
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500377ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
378 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500379 ipmi_data_len_t data_len, ipmi_context_t context)
380{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530381 const char *objname = "/xyz/openbmc_project/watchdog/host0";
382 const char *iface = "xyz.openbmc_project.State.Watchdog";
383 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600384 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500385 sd_bus_error error = SD_BUS_ERROR_NULL;
386 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500387
388 set_wd_data_t *reqptr = (set_wd_data_t*) request;
389 uint16_t timer = 0;
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530390
391 // Making this uint64_t to match with provider
392 uint64_t timer_ms = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500393 char *busname = NULL;
Chris Austen6caf28b2015-10-13 12:40:40 -0500394 *data_len = 0;
395
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500396 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500397 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500398 // Get timer value in ms
399 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500400
401 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
402
Sergey Solomineb9b8142016-08-23 09:07:28 -0500403 // Get bus name
404 r = mapper_get_service(bus, objname, &busname);
405 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400406 fprintf(stderr, "Failed to get %s bus name: %s\n",
407 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500408 goto finish;
409 }
Adriana Kobylak0896be32015-10-22 13:27:23 -0500410
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530411 // Disable watchdog if running
412 r = sd_bus_call_method(bus, busname, objname, property_iface,
413 "Set", &error, &reply, "ssv",
414 iface, "Enabled", "b", false);
415 if(r < 0) {
416 fprintf(stderr, "Failed to disable Watchdog: %s\n",
417 strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600418 goto finish;
419 }
420
421 if (reqptr->t_use & 0x40)
422 {
423 sd_bus_error_free(&error);
424 reply = sd_bus_message_unref(reply);
425
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530426 // Now Enable Watchdog
427 r = sd_bus_call_method(bus, busname, objname, property_iface,
428 "Set", &error, &reply, "ssv",
429 iface, "Enabled", "b", true);
430 if(r < 0) {
431 fprintf(stderr, "Failed to Enable Watchdog: %s\n",
432 strerror(-r));
433 goto finish;
434 }
435
436 // Set watchdog timer
437 r = sd_bus_call_method(bus, busname, objname, property_iface,
438 "Set", &error, &reply, "ssv",
439 iface, "TimeRemaining", "t", timer_ms);
440 if(r < 0) {
441 fprintf(stderr, "Failed to set new expiration time: %s\n",
442 strerror(-r));
443 goto finish;
vishwa1eaea4f2016-02-26 11:57:40 -0600444 }
445 }
446
447finish:
448 sd_bus_error_free(&error);
449 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500450 free(busname);
vishwa1eaea4f2016-02-26 11:57:40 -0600451
452 return (r < 0) ? -1 : IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -0500453}
454
455
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500456ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
457 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500458 ipmi_data_len_t data_len, ipmi_context_t context)
459{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530460 const char *objname = "/xyz/openbmc_project/watchdog/host0";
461 const char *iface = "xyz.openbmc_project.State.Watchdog";
462 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600463 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500464 sd_bus_error error = SD_BUS_ERROR_NULL;
465 int r = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500466 char *busname = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500467
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530468 // Current time interval that is set in watchdog.
469 uint64_t interval = 0;
470
Chris Austen6caf28b2015-10-13 12:40:40 -0500471 // Status code.
472 ipmi_ret_t rc = IPMI_CC_OK;
473 *data_len = 0;
474
475 printf("WATCHDOG RESET\n");
Sergey Solomineb9b8142016-08-23 09:07:28 -0500476 // Get bus name
477 r = mapper_get_service(bus, objname, &busname);
478 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400479 fprintf(stderr, "Failed to get %s bus name: %s\n",
480 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500481 goto finish;
482 }
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530483
484 // Get the current interval and set it back.
485 r = sd_bus_call_method(bus, busname, objname, property_iface,
486 "Get", &error, &reply, "ss",
487 iface, "Interval");
488
489 if(r < 0) {
490 fprintf(stderr, "Failed to get current Interval msg: %s\n",
491 strerror(-r));
492 goto finish;
493 }
494
495 // Now extract the value
496 r = sd_bus_message_read(reply, "v", "t", &interval);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500497 if (r < 0) {
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530498 fprintf(stderr, "Failed to read current interval: %s\n",
499 strerror(-r));
500 goto finish;
501 }
502
503 sd_bus_error_free(&error);
504 reply = sd_bus_message_unref(reply);
505
506 // Set watchdog timer
507 r = sd_bus_call_method(bus, busname, objname, property_iface,
508 "Set", &error, &reply, "ssv",
509 iface, "TimeRemaining", "t", interval);
510 if(r < 0) {
511 fprintf(stderr, "Failed to refresh the timer: %s\n",
512 strerror(-r));
513 goto finish;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500514 }
515
Sergey Solomineb9b8142016-08-23 09:07:28 -0500516finish:
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500517 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600518 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500519 free(busname);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500520
Chris Austen6caf28b2015-10-13 12:40:40 -0500521 return rc;
522}
523
Ratan Guptab8e99552017-07-27 07:07:48 +0530524extern struct ChannelConfig_t channelConfig;
Nan Li3d0df912016-10-18 19:51:41 +0800525
Ratan Guptab8e99552017-07-27 07:07:48 +0530526ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn,
527 ipmi_cmd_t cmd,
528 ipmi_request_t request,
529 ipmi_response_t response,
530 ipmi_data_len_t data_len,
531 ipmi_context_t context)
Nan Li3d0df912016-10-18 19:51:41 +0800532{
533 ipmi_ret_t rc = IPMI_CC_OK;
534
Ratan Guptab8e99552017-07-27 07:07:48 +0530535 std::string ipaddress;
536 std::string gateway;
537 uint8_t prefix {};
Nan Li3d0df912016-10-18 19:51:41 +0800538
539 // Todo: parse the request data if needed.
540
541 // Using Set Channel cmd to apply changes of Set Lan Cmd.
Ratan Guptab8e99552017-07-27 07:07:48 +0530542 try
543 {
Nan Li3d0df912016-10-18 19:51:41 +0800544
Ratan Guptab8e99552017-07-27 07:07:48 +0530545 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
546
547 log<level::INFO>("Network data from Cache",
548 entry("PREFIX=%s", channelConfig.netmask.c_str()),
549 entry("ADDRESS=%s", channelConfig.ipaddr.c_str()),
550 entry("GATEWAY=%s", channelConfig.gateway.c_str()));
551
552 auto systemObject = ipmi::getDbusObject(bus,
553 ipmi::network::SYSTEMCONFIG_INTERFACE,
554 ipmi::network::ROOT);
555
556 // TODO Currently IPMI supports single interface,need to handle
557 // Multiple interface through
558 // https://github.com/openbmc/openbmc/issues/2138
559
560 auto networkInterfaceObject = ipmi::getDbusObject(
561 bus,
562 ipmi::network::ETHERNET_INTERFACE,
563 ipmi::network::ROOT,
564 ipmi::network::INTERFACE);
565
566
567
568 // check wthether user has given all the data
569 if (!channelConfig.ipaddr.empty() &&
570 !channelConfig.netmask.empty() &&
571 !channelConfig.gateway.empty())
572 {
573 //convert mask into prefix
574 ipaddress = channelConfig.ipaddr;
575 prefix = ipmi::network::toPrefix(AF_INET, channelConfig.netmask);
576 gateway = channelConfig.gateway;
577 }
578 else
579 {
580 // gets the network data from the system as user has
581 // not given all the data then use the data fetched from the
582 // system but it is implementation dependent,IPMI spec doesn't
583 // force it.
584
585 // if system is not having any ip object don't throw error,
586 try
587 {
588
589 auto ipObjectInfo = ipmi::getDbusObject(bus,
590 ipmi::network::IP_INTERFACE,
591 ipmi::network::ROOT,
592 ipmi::network::IP_TYPE);
593
594 auto properties = ipmi::getAllDbusProperties(bus,
595 ipObjectInfo.second,
596 ipObjectInfo.first,
597 ipmi::network::IP_INTERFACE);
598
599 ipaddress = channelConfig.ipaddr.empty() ?
600 std::move(properties["Address"].get<std::string>()) :
601 channelConfig.ipaddr;
602
603 prefix = channelConfig.netmask.empty() ?
604 properties["PrefixLength"].get<uint8_t>() :
605 ipmi::network::toPrefix(AF_INET,
606 channelConfig.netmask);
607
608 }
609 catch (InternalFailure& e)
610 {
611 log<level::INFO>("Failed to get IP object which matches",
612 entry("INTERFACE=%s", ipmi::network::IP_INTERFACE),
613 entry("MATCH=%s", ipmi::network::IP_TYPE));
614 }
615
616 auto systemProperties = ipmi::getAllDbusProperties(
617 bus,
618 systemObject.second,
619 systemObject.first,
620 ipmi::network::SYSTEMCONFIG_INTERFACE);
621
622 gateway = channelConfig.gateway.empty() ?
623 std::move(systemProperties["DefaultGateway"].get<std::string>()) :
624 channelConfig.gateway;
625
626 }
627
628 // Currently network manager doesn't support purging of all the
629 // ip addresses from the parent interface,
630 // TODO once the support is there, will make the change here.
631 // https://github.com/openbmc/openbmc/issues/2141.
632
633 // TODO Currently IPMI supports single interface,need to handle
634 // Multiple interface through
635 // https://github.com/openbmc/openbmc/issues/2138
636
637 //delete all the ipv4 addresses
638
639 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
640 ipmi::network::IP_INTERFACE,
641 ipmi::network::IP_TYPE);
642
643 ipmi::network::createIP(bus, networkInterfaceObject.second,
644 networkInterfaceObject.first,
645 ipv4Protocol, ipaddress, prefix);
646
647 ipmi::setDbusProperty(bus, systemObject.second, systemObject.first,
648 ipmi::network::SYSTEMCONFIG_INTERFACE,
649 "DefaultGateway", gateway);
650
Nan Li3d0df912016-10-18 19:51:41 +0800651 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530652 catch (InternalFailure& e)
653 {
654 log<level::ERR>("Failed to set network data",
655 entry("PREFIX=%d", prefix),
656 entry("ADDRESS=%s", ipaddress),
657 entry("GATEWAY=%s", gateway));
Nan Li3d0df912016-10-18 19:51:41 +0800658
Ratan Guptab8e99552017-07-27 07:07:48 +0530659 commit<InternalFailure>();
Nan Li3d0df912016-10-18 19:51:41 +0800660 rc = IPMI_CC_UNSPECIFIED_ERROR;
661 }
662
Ratan Guptab8e99552017-07-27 07:07:48 +0530663 channelConfig.clear();
Nan Li3d0df912016-10-18 19:51:41 +0800664 return rc;
665}
666
Chris Austenc2cd29d2016-02-05 20:02:29 -0600667// ATTENTION: This ipmi function is very hardcoded on purpose
668// OpenBMC does not fully support IPMI. This command is useful
669// to have around because it enables testing of interfaces with
670// the IPMI tool.
671#define GET_CHANNEL_INFO_CHANNEL_OFFSET 0
672// IPMI Table 6-2
673#define IPMI_CHANNEL_TYPE_IPMB 1
674// IPMI Table 6-3
675#define IPMI_CHANNEL_MEDIUM_TYPE_OTHER 6
676
677ipmi_ret_t ipmi_app_channel_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
678 ipmi_request_t request, ipmi_response_t response,
679 ipmi_data_len_t data_len, ipmi_context_t context)
680{
681 ipmi_ret_t rc = IPMI_CC_OK;
682 uint8_t resp[] = {
683 1,
684 IPMI_CHANNEL_MEDIUM_TYPE_OTHER,
685 IPMI_CHANNEL_TYPE_IPMB,
686 1,0x41,0xA7,0x00,0,0};
687 uint8_t *p = (uint8_t*) request;
688
689 printf("IPMI APP GET CHANNEL INFO\n");
690
tomjose7ec0add2016-06-27 07:59:28 -0500691 // The supported channels numbers are 1 and 8.
692 // Channel Number E is used as way to identify the current channel
693 // that the command is being is received from.
tomjose13fb4412016-03-08 14:02:34 -0600694 if (*p == 0xe || *p == 1 || *p == 8) {
Chris Austenc2cd29d2016-02-05 20:02:29 -0600695
696 *data_len = sizeof(resp);
697 memcpy(response, resp, *data_len);
698
699 } else {
700 rc = IPMI_CC_PARM_OUT_OF_RANGE;
701 *data_len = 0;
702 }
703
704 return rc;
705}
706
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500707ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
708 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530709 ipmi_data_len_t data_len, ipmi_context_t context)
710{
711 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
712
713 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800714 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530715
716 *data_len = strlen("THIS IS WILDCARD");
717
718 // Now pack actual response
719 memcpy(response, "THIS IS WILDCARD", *data_len);
720
721 return rc;
722}
723
Chris Austen6caf28b2015-10-13 12:40:40 -0500724void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530725{
Tom05732372016-09-06 17:21:23 +0530726 // <Get BT Interface Capabilities>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530727 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Tom05732372016-09-06 17:21:23 +0530728 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities,
729 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500730
Tom05732372016-09-06 17:21:23 +0530731 // <Wildcard Command>
Chris Austen6caf28b2015-10-13 12:40:40 -0500732 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
Tom05732372016-09-06 17:21:23 +0530733 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler,
734 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500735
Tom05732372016-09-06 17:21:23 +0530736 // <Reset Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500737 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
Tom05732372016-09-06 17:21:23 +0530738 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog,
739 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500740
Tom05732372016-09-06 17:21:23 +0530741 // <Set Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500742 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
Tom05732372016-09-06 17:21:23 +0530743 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog,
744 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500745
Tom05732372016-09-06 17:21:23 +0530746 // <Get Device ID>
Chris Austen6caf28b2015-10-13 12:40:40 -0500747 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
Tom05732372016-09-06 17:21:23 +0530748 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id,
749 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500750
Tom05732372016-09-06 17:21:23 +0530751 // <Get Self Test Results>
Nan Li41fa24a2016-11-10 20:12:37 +0800752 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS);
Tom05732372016-09-06 17:21:23 +0530753 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL,
754 ipmi_app_get_self_test_results, PRIVILEGE_USER);
Nan Li41fa24a2016-11-10 20:12:37 +0800755
Tom05732372016-09-06 17:21:23 +0530756 // <Get Device GUID>
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500757 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
Tom05732372016-09-06 17:21:23 +0530758 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid,
759 PRIVILEGE_USER);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500760
Tom05732372016-09-06 17:21:23 +0530761 // <Set ACPI Power State>
Chris Austen6caf28b2015-10-13 12:40:40 -0500762 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
Tom05732372016-09-06 17:21:23 +0530763 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state,
764 PRIVILEGE_ADMIN);
Chris Austen6caf28b2015-10-13 12:40:40 -0500765
Tom05732372016-09-06 17:21:23 +0530766 // <Set Channel Access>
Nan Li3d0df912016-10-18 19:51:41 +0800767 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
768 IPMI_CMD_SET_CHAN_ACCESS);
769 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
Tom05732372016-09-06 17:21:23 +0530770 ipmi_set_channel_access, PRIVILEGE_ADMIN);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600771
Tom05732372016-09-06 17:21:23 +0530772 // <Get Channel Info Command>
Chris Austenc2cd29d2016-02-05 20:02:29 -0600773 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
Tom05732372016-09-06 17:21:23 +0530774 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info,
775 PRIVILEGE_USER);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600776
vishwabmcba0bd5f2015-09-30 16:50:23 +0530777 return;
778}
779
Chris Austen6caf28b2015-10-13 12:40:40 -0500780