blob: 5145a0a99af0a339e2c2c109cdf0a715367aa78b [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 -050020extern sd_bus *bus;
vishwabmcba0bd5f2015-09-30 16:50:23 +053021
Nan Li3d0df912016-10-18 19:51:41 +080022constexpr auto app_obj = "/org/openbmc/NetworkManager/Interface";
23constexpr auto app_ifc = "org.openbmc.NetworkManager";
24constexpr auto app_nwinterface = "eth0";
25
Ratan Guptab8e99552017-07-27 07:07:48 +053026constexpr auto ipv4Protocol = "xyz.openbmc_project.Network.IP.Protocol.IPv4";
27
Chris Austen6caf28b2015-10-13 12:40:40 -050028void register_netfn_app_functions() __attribute__((constructor));
vishwabmcba0bd5f2015-09-30 16:50:23 +053029
Ratan Guptab8e99552017-07-27 07:07:48 +053030using namespace phosphor::logging;
31using namespace sdbusplus::xyz::openbmc_project::Common::Error;
32
Nan Liee0cb902016-07-11 15:38:03 +080033// Offset in get device id command.
34typedef struct
35{
36 uint8_t id;
37 uint8_t revision;
38 uint8_t fw[2];
39 uint8_t ipmi_ver;
40 uint8_t addn_dev_support;
41 uint8_t manuf_id[3];
42 uint8_t prod_id[2];
43 uint8_t aux[4];
44}__attribute__((packed)) ipmi_device_id_t;
Chris Austen7303bdc2016-04-17 11:50:54 -050045
Adriana Kobylak3a552e12015-10-19 16:11:00 -050046ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
47 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -050048 ipmi_data_len_t data_len, ipmi_context_t context)
49{
50 ipmi_ret_t rc = IPMI_CC_OK;
51 *data_len = 0;
52
53 printf("IPMI SET ACPI STATE Ignoring for now\n");
54 return rc;
55}
56
Chris Austen7303bdc2016-04-17 11:50:54 -050057
58typedef struct
59{
60 char major;
61 char minor;
Chris Austen176c9652016-04-30 16:32:17 -050062 uint16_t d[2];
Chris Austen7303bdc2016-04-17 11:50:54 -050063} rev_t;
64
65
66/* Currently only supports the vx.x-x-[-x] format Will return -1 if not in */
67/* the format this routine knows how to parse */
68/* version = v0.6-19-gf363f61-dirty */
69/* ^ ^ ^^ ^ */
70/* | | |----------|-- additional details */
71/* | |---------------- Minor */
72/* |------------------ Major */
73/* Additional details : If the option group exists it will force Auxiliary */
74/* Firmware Revision Information 4th byte to 1 indicating the build was */
75/* derived with additional edits */
76int convert_version(const char *p, rev_t *rev)
77{
78 char *s, *token;
Chris Austen176c9652016-04-30 16:32:17 -050079 uint16_t commits;
Chris Austen7303bdc2016-04-17 11:50:54 -050080
81 if (*p != 'v')
82 return -1;
83 p++;
84
85 s = strdup(p);
86 token = strtok(s,".-");
87
88 rev->major = (int8_t) atoi(token);
89
90 token = strtok(NULL, ".-");
91 rev->minor = (int8_t) atoi(token);
92
93 // Capture the number of commits on top of the minor tag.
94 // I'm using BE format like the ipmi spec asked for
95 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -050096
Chris Austen176c9652016-04-30 16:32:17 -050097 if (token) {
98 commits = (int16_t) atoi(token);
99 rev->d[0] = (commits>>8) | (commits<<8);
Chris Austen7303bdc2016-04-17 11:50:54 -0500100
Chris Austen176c9652016-04-30 16:32:17 -0500101 // commit number we skip
102 token = strtok(NULL,".-");
Chris Austen7303bdc2016-04-17 11:50:54 -0500103
Chris Austen176c9652016-04-30 16:32:17 -0500104 } else {
105 rev->d[0] = 0;
106 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500107
108 // Any value of the optional parameter forces it to 1
Chris Austen176c9652016-04-30 16:32:17 -0500109 if (token)
110 token = strtok(NULL,".-");
111
112 rev->d[1] = (token != NULL) ? 1 : 0;
Chris Austen7303bdc2016-04-17 11:50:54 -0500113
114 free(s);
115 return 0;
116}
117
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500118ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
119 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500120 ipmi_data_len_t data_len, ipmi_context_t context)
121{
122 ipmi_ret_t rc = IPMI_CC_OK;
Chris Austen7303bdc2016-04-17 11:50:54 -0500123 const char *objname = "/org/openbmc/inventory/system/chassis/motherboard/bmc";
124 const char *iface = "org.openbmc.InventoryItem";
125 char *ver = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500126 char *busname = NULL;
Chris Austen7303bdc2016-04-17 11:50:54 -0500127 int r;
128 rev_t rev = {0};
Nan Liee0cb902016-07-11 15:38:03 +0800129 ipmi_device_id_t dev_id{};
Chris Austen6caf28b2015-10-13 12:40:40 -0500130
131 // Data length
Chris Austen7303bdc2016-04-17 11:50:54 -0500132 *data_len = sizeof(dev_id);
133
Nan Liee0cb902016-07-11 15:38:03 +0800134 // From IPMI spec, controller that have different application commands, or different
135 // definitions of OEM fields, are expected to have different Device ID values.
136 // Set to 0 now.
137
138 // Device Revision is set to 0 now.
139 // Bit7 identifies if device provide Device SDRs, obmc don't have SDR,we use ipmi to
140 // simulate SDR, hence the value:
141 dev_id.revision = 0x80;
142
143 // Firmware revision is already implemented, so get it from appropriate position.
Sergey Solomineb9b8142016-08-23 09:07:28 -0500144 r = mapper_get_service(bus, objname, &busname);
145 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400146 fprintf(stderr, "Failed to get %s bus name: %s\n",
147 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500148 goto finish;
149 }
Chris Austen7303bdc2016-04-17 11:50:54 -0500150 r = sd_bus_get_property_string(bus,busname,objname,iface,"version", NULL, &ver);
151 if ( r < 0 ) {
152 fprintf(stderr, "Failed to obtain version property: %s\n", strerror(-r));
153 } else {
154 r = convert_version(ver, &rev);
155 if( r >= 0 ) {
Nan Liee0cb902016-07-11 15:38:03 +0800156 // bit7 identifies if the device is available, 0=normal operation,
157 // 1=device firmware, SDR update or self-initialization in progress.
158 // our SDR is normal working condition, so mask:
159 dev_id.fw[0] = 0x7F & rev.major;
Adriana Kobylak0e912642016-06-22 16:54:39 -0500160
161 rev.minor = (rev.minor > 99 ? 99 : rev.minor);
Nan Liee0cb902016-07-11 15:38:03 +0800162 dev_id.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16;
163 memcpy(&dev_id.aux, rev.d, 4);
Chris Austen7303bdc2016-04-17 11:50:54 -0500164 }
165 }
Chris Austen6caf28b2015-10-13 12:40:40 -0500166
Nan Liee0cb902016-07-11 15:38:03 +0800167 // IPMI Spec verison 2.0
168 dev_id.ipmi_ver = 2;
169
170 // Additional device Support.
171 // List the 'logical device' commands and functions that the controller supports
172 // that are in addition to the mandatory IPM and Application commands.
173 // [7] Chassis Device (device functions as chassis device per ICMB spec.)
174 // [6] Bridge (device responds to Bridge NetFn commands)
175 // [5] IPMB Event Generator
176 // [4] IPMB Event Receiver
177 // [3] FRU Inventory Device
178 // [2] SEL Device
179 // [1] SDR Repository Device
180 // [0] Sensor Device
181 // We support FRU/SEL/Sensor now:
182 dev_id.addn_dev_support = 0x8D;
183
184 // This value is the IANA number assigned to "IBM Platform Firmware
185 // Division", which is also used by our service processor. We may want
186 // a different number or at least a different version?
187 dev_id.manuf_id[0] = 0x41;
188 dev_id.manuf_id[1] = 0xA7;
189 dev_id.manuf_id[2] = 0x00;
190
191 // Witherspoon's product ID is hardcoded to 4F42(ASCII 'OB').
192 // TODO: openbmc/openbmc#495
193 dev_id.prod_id[0] = 0x4F;
194 dev_id.prod_id[1] = 0x42;
195
Chris Austen6caf28b2015-10-13 12:40:40 -0500196 // Pack the actual response
Chris Austen7303bdc2016-04-17 11:50:54 -0500197 memcpy(response, &dev_id, *data_len);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500198finish:
199 free(busname);
Xo Wange5536382017-07-20 20:14:00 -0700200 free(ver);
Chris Austen6caf28b2015-10-13 12:40:40 -0500201 return rc;
202}
203
Nan Li41fa24a2016-11-10 20:12:37 +0800204ipmi_ret_t ipmi_app_get_self_test_results(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
205 ipmi_request_t request, ipmi_response_t response,
206 ipmi_data_len_t data_len, ipmi_context_t context)
207{
208 ipmi_ret_t rc = IPMI_CC_OK;
209
210 // Byte 2:
211 // 55h - No error.
212 // 56h - Self Test funciton not implemented in this controller.
213 // 57h - Corrupted or inaccesssible data or devices.
214 // 58h - Fatal hardware error.
215 // FFh - reserved.
216 // all other: Device-specific 'internal failure'.
217 // Byte 3:
218 // For byte 2 = 55h, 56h, FFh: 00h
219 // For byte 2 = 58h, all other: Device-specific
220 // For byte 2 = 57h: self-test error bitfield.
221 // Note: returning 57h does not imply that all test were run.
222 // [7] 1b = Cannot access SEL device.
223 // [6] 1b = Cannot access SDR Repository.
224 // [5] 1b = Cannot access BMC FRU device.
225 // [4] 1b = IPMB signal lines do not respond.
226 // [3] 1b = SDR Repository empty.
227 // [2] 1b = Internal Use Area of BMC FRU corrupted.
228 // [1] 1b = controller update 'boot block' firmware corrupted.
229 // [0] 1b = controller operational firmware corrupted.
230
231 char selftestresults[2] = {0};
232
233 *data_len = 2;
234
235 selftestresults[0] = 0x56;
236 selftestresults[1] = 0;
237
238 memcpy(response, selftestresults, *data_len);
239
240 return rc;
241}
242
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500243ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
244 ipmi_request_t request, ipmi_response_t response,
245 ipmi_data_len_t data_len, ipmi_context_t context)
246{
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500247 const char *objname = "/org/openbmc/control/chassis0";
Adriana Kobylak31bccae2015-11-05 13:31:06 -0600248 const char *iface = "org.freedesktop.DBus.Properties";
249 const char *chassis_iface = "org.openbmc.control.Chassis";
vishwa1eaea4f2016-02-26 11:57:40 -0600250 sd_bus_message *reply = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500251 sd_bus_error error = SD_BUS_ERROR_NULL;
252 int r = 0;
253 char *uuid = NULL;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500254 char *busname = NULL;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500255
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500256 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
257 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order
258 // Ex: 0x2332fc2c40e66298e511f2782395a361
259
260 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec
261 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response
262 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order
263 int i = 0;
264 char *tokptr = NULL;
vishwa1eaea4f2016-02-26 11:57:40 -0600265 char *id_octet = NULL;
266
267 // Status code.
268 ipmi_ret_t rc = IPMI_CC_OK;
269 *data_len = 0;
270
271 printf("IPMI GET DEVICE GUID\n");
272
273 // Call Get properties method with the interface and property name
Sergey Solomineb9b8142016-08-23 09:07:28 -0500274 r = mapper_get_service(bus, objname, &busname);
275 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400276 fprintf(stderr, "Failed to get %s bus name: %s\n",
277 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500278 goto finish;
279 }
vishwa1eaea4f2016-02-26 11:57:40 -0600280 r = sd_bus_call_method(bus,busname,objname,iface,
281 "Get",&error, &reply, "ss",
282 chassis_iface, "uuid");
283 if (r < 0)
284 {
285 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
286 rc = IPMI_CC_UNSPECIFIED_ERROR;
287 goto finish;
288 }
289
290 r = sd_bus_message_read(reply, "v", "s", &uuid);
291 if (r < 0 || uuid == NULL)
292 {
293 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
294 rc = IPMI_CC_RESPONSE_ERROR;
295 goto finish;
296 }
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500297
298 // Traverse the UUID
vishwa1eaea4f2016-02-26 11:57:40 -0600299 id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500300
301 if (id_octet == NULL)
vishwa1eaea4f2016-02-26 11:57:40 -0600302 {
303 // Error
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500304 fprintf(stderr, "Unexpected UUID format: %s", uuid);
vishwa1eaea4f2016-02-26 11:57:40 -0600305 rc = IPMI_CC_RESPONSE_ERROR;
306 goto finish;
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500307 }
308
309 while (id_octet != NULL)
310 {
311 // Calculate the octet string size since it varies
312 // Divide it by 2 for the array size since 1 byte is built from 2 chars
313 int tmp_size = strlen(id_octet)/2;
314
315 for(i = 0; i < tmp_size; i++)
316 {
Joel Stanley38310cd2015-11-25 17:32:08 +1030317 char tmp_array[3] = {0}; // Holder of the 2 chars that will become a byte
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500318 strncpy(tmp_array, id_octet, 2); // 2 chars at a time
319
320 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte
321 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first
322 resp_loc--;
323 id_octet+=2; // Finished with the 2 chars, advance
324 }
325 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet
326 }
327
328 // Data length
329 *data_len = resp_size;
330
331 // Pack the actual response
332 memcpy(response, &resp_uuid, *data_len);
333
vishwa1eaea4f2016-02-26 11:57:40 -0600334finish:
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500335 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600336 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500337 free(busname);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500338
339 return rc;
340}
Chris Austen6caf28b2015-10-13 12:40:40 -0500341
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500342ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
343 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530344 ipmi_data_len_t data_len, ipmi_context_t context)
345{
346 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
347
348 // Status code.
349 ipmi_ret_t rc = IPMI_CC_OK;
350
Adriana Kobylak88ad8152016-12-13 10:09:08 -0600351 // Per IPMI 2.0 spec, the input and output buffer size must be the max
352 // buffer size minus one byte to allocate space for the length byte.
353 uint8_t str[] = {0x01, MAX_IPMI_BUFFER-1, MAX_IPMI_BUFFER-1, 0x0A, 0x01};
vishwabmcba0bd5f2015-09-30 16:50:23 +0530354
355 // Data length
356 *data_len = sizeof(str);
357
358 // Pack the actual response
359 memcpy(response, &str, *data_len);
360
361 return rc;
362}
363
Chris Austen6caf28b2015-10-13 12:40:40 -0500364
365struct set_wd_data_t {
366 uint8_t t_use;
367 uint8_t t_action;
368 uint8_t preset;
369 uint8_t flags;
370 uint8_t ls;
371 uint8_t ms;
372} __attribute__ ((packed));
373
374
375
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500376ipmi_ret_t ipmi_app_set_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
377 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500378 ipmi_data_len_t data_len, ipmi_context_t context)
379{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530380 const char *objname = "/xyz/openbmc_project/watchdog/host0";
381 const char *iface = "xyz.openbmc_project.State.Watchdog";
382 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600383 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500384 sd_bus_error error = SD_BUS_ERROR_NULL;
385 int r = 0;
Chris Austen6caf28b2015-10-13 12:40:40 -0500386
387 set_wd_data_t *reqptr = (set_wd_data_t*) request;
388 uint16_t timer = 0;
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530389
390 // Making this uint64_t to match with provider
391 uint64_t timer_ms = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500392 char *busname = NULL;
Chris Austen6caf28b2015-10-13 12:40:40 -0500393 *data_len = 0;
394
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500395 // Get number of 100ms intervals
Chris Austen6caf28b2015-10-13 12:40:40 -0500396 timer = (((uint16_t)reqptr->ms) << 8) + reqptr->ls;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500397 // Get timer value in ms
398 timer_ms = timer * 100;
Chris Austen6caf28b2015-10-13 12:40:40 -0500399
400 printf("WATCHDOG SET Timer:[0x%X] 100ms intervals\n",timer);
401
Sergey Solomineb9b8142016-08-23 09:07:28 -0500402 // Get bus name
403 r = mapper_get_service(bus, objname, &busname);
404 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400405 fprintf(stderr, "Failed to get %s bus name: %s\n",
406 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500407 goto finish;
408 }
Adriana Kobylak0896be32015-10-22 13:27:23 -0500409
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530410 // Disable watchdog if running
411 r = sd_bus_call_method(bus, busname, objname, property_iface,
412 "Set", &error, &reply, "ssv",
413 iface, "Enabled", "b", false);
414 if(r < 0) {
415 fprintf(stderr, "Failed to disable Watchdog: %s\n",
416 strerror(-r));
vishwa1eaea4f2016-02-26 11:57:40 -0600417 goto finish;
418 }
419
Patrick Venture6c50ad92017-07-29 22:38:28 -0700420 /*
421 * If the action is 0, it means, do nothing. Multiple actions on timer
422 * expiration aren't supported by phosphor-watchdog yet, so when the
423 * action set is "none", we should just leave the timer disabled.
424 */
425 if (0 == reqptr->t_action)
426 {
427 goto finish;
428 }
429
vishwa1eaea4f2016-02-26 11:57:40 -0600430 if (reqptr->t_use & 0x40)
431 {
432 sd_bus_error_free(&error);
433 reply = sd_bus_message_unref(reply);
434
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530435 // Now Enable Watchdog
436 r = sd_bus_call_method(bus, busname, objname, property_iface,
437 "Set", &error, &reply, "ssv",
438 iface, "Enabled", "b", true);
439 if(r < 0) {
440 fprintf(stderr, "Failed to Enable Watchdog: %s\n",
441 strerror(-r));
442 goto finish;
443 }
444
445 // Set watchdog timer
446 r = sd_bus_call_method(bus, busname, objname, property_iface,
447 "Set", &error, &reply, "ssv",
448 iface, "TimeRemaining", "t", timer_ms);
449 if(r < 0) {
450 fprintf(stderr, "Failed to set new expiration time: %s\n",
451 strerror(-r));
452 goto finish;
vishwa1eaea4f2016-02-26 11:57:40 -0600453 }
454 }
455
456finish:
457 sd_bus_error_free(&error);
458 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500459 free(busname);
vishwa1eaea4f2016-02-26 11:57:40 -0600460
461 return (r < 0) ? -1 : IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -0500462}
463
464
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500465ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
466 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500467 ipmi_data_len_t data_len, ipmi_context_t context)
468{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530469 const char *objname = "/xyz/openbmc_project/watchdog/host0";
470 const char *iface = "xyz.openbmc_project.State.Watchdog";
471 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600472 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500473 sd_bus_error error = SD_BUS_ERROR_NULL;
474 int r = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500475 char *busname = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500476
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530477 // Current time interval that is set in watchdog.
478 uint64_t interval = 0;
479
Chris Austen6caf28b2015-10-13 12:40:40 -0500480 // Status code.
481 ipmi_ret_t rc = IPMI_CC_OK;
482 *data_len = 0;
483
484 printf("WATCHDOG RESET\n");
Sergey Solomineb9b8142016-08-23 09:07:28 -0500485 // Get bus name
486 r = mapper_get_service(bus, objname, &busname);
487 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400488 fprintf(stderr, "Failed to get %s bus name: %s\n",
489 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500490 goto finish;
491 }
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530492
493 // Get the current interval and set it back.
494 r = sd_bus_call_method(bus, busname, objname, property_iface,
495 "Get", &error, &reply, "ss",
496 iface, "Interval");
497
498 if(r < 0) {
499 fprintf(stderr, "Failed to get current Interval msg: %s\n",
500 strerror(-r));
501 goto finish;
502 }
503
504 // Now extract the value
505 r = sd_bus_message_read(reply, "v", "t", &interval);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500506 if (r < 0) {
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530507 fprintf(stderr, "Failed to read current interval: %s\n",
508 strerror(-r));
509 goto finish;
510 }
511
512 sd_bus_error_free(&error);
513 reply = sd_bus_message_unref(reply);
514
515 // Set watchdog timer
516 r = sd_bus_call_method(bus, busname, objname, property_iface,
517 "Set", &error, &reply, "ssv",
518 iface, "TimeRemaining", "t", interval);
519 if(r < 0) {
520 fprintf(stderr, "Failed to refresh the timer: %s\n",
521 strerror(-r));
522 goto finish;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500523 }
524
Sergey Solomineb9b8142016-08-23 09:07:28 -0500525finish:
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500526 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600527 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500528 free(busname);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500529
Chris Austen6caf28b2015-10-13 12:40:40 -0500530 return rc;
531}
532
Ratan Guptab8e99552017-07-27 07:07:48 +0530533extern struct ChannelConfig_t channelConfig;
Nan Li3d0df912016-10-18 19:51:41 +0800534
Ratan Guptab8e99552017-07-27 07:07:48 +0530535ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn,
536 ipmi_cmd_t cmd,
537 ipmi_request_t request,
538 ipmi_response_t response,
539 ipmi_data_len_t data_len,
540 ipmi_context_t context)
Nan Li3d0df912016-10-18 19:51:41 +0800541{
542 ipmi_ret_t rc = IPMI_CC_OK;
543
Ratan Guptab8e99552017-07-27 07:07:48 +0530544 std::string ipaddress;
545 std::string gateway;
546 uint8_t prefix {};
Ratan Gupta533d03b2017-07-30 10:39:22 +0530547 uint32_t vlanID {};
Nan Li3d0df912016-10-18 19:51:41 +0800548
549 // Todo: parse the request data if needed.
550
551 // Using Set Channel cmd to apply changes of Set Lan Cmd.
Ratan Guptab8e99552017-07-27 07:07:48 +0530552 try
553 {
Nan Li3d0df912016-10-18 19:51:41 +0800554
Ratan Guptab8e99552017-07-27 07:07:48 +0530555 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
556
557 log<level::INFO>("Network data from Cache",
558 entry("PREFIX=%s", channelConfig.netmask.c_str()),
559 entry("ADDRESS=%s", channelConfig.ipaddr.c_str()),
560 entry("GATEWAY=%s", channelConfig.gateway.c_str()));
561
562 auto systemObject = ipmi::getDbusObject(bus,
563 ipmi::network::SYSTEMCONFIG_INTERFACE,
564 ipmi::network::ROOT);
565
566 // TODO Currently IPMI supports single interface,need to handle
567 // Multiple interface through
568 // https://github.com/openbmc/openbmc/issues/2138
569
570 auto networkInterfaceObject = ipmi::getDbusObject(
571 bus,
572 ipmi::network::ETHERNET_INTERFACE,
573 ipmi::network::ROOT,
574 ipmi::network::INTERFACE);
575
576
577
578 // check wthether user has given all the data
579 if (!channelConfig.ipaddr.empty() &&
580 !channelConfig.netmask.empty() &&
581 !channelConfig.gateway.empty())
582 {
583 //convert mask into prefix
584 ipaddress = channelConfig.ipaddr;
585 prefix = ipmi::network::toPrefix(AF_INET, channelConfig.netmask);
586 gateway = channelConfig.gateway;
Ratan Gupta533d03b2017-07-30 10:39:22 +0530587
588 if (channelConfig.vlanID != ipmi::network::VLAN_ID_MASK)
589 {
590 //get the first twelve bits which is vlan id
591 //not interested in rest of the bits.
592 vlanID = channelConfig.vlanID & ipmi::network::VLAN_ID_MASK;
593 channelConfig.vlanID = le32toh(channelConfig.vlanID);
594 }
595
Ratan Guptab8e99552017-07-27 07:07:48 +0530596 }
597 else
598 {
Ratan Gupta533d03b2017-07-30 10:39:22 +0530599 // We have partial filled cache so get the remaning
600 // info from the system.
601
Ratan Guptab8e99552017-07-27 07:07:48 +0530602 // gets the network data from the system as user has
603 // not given all the data then use the data fetched from the
604 // system but it is implementation dependent,IPMI spec doesn't
605 // force it.
606
607 // if system is not having any ip object don't throw error,
608 try
609 {
Ratan Guptab8e99552017-07-27 07:07:48 +0530610 auto ipObjectInfo = ipmi::getDbusObject(bus,
611 ipmi::network::IP_INTERFACE,
612 ipmi::network::ROOT,
613 ipmi::network::IP_TYPE);
614
615 auto properties = ipmi::getAllDbusProperties(bus,
616 ipObjectInfo.second,
617 ipObjectInfo.first,
618 ipmi::network::IP_INTERFACE);
619
620 ipaddress = channelConfig.ipaddr.empty() ?
621 std::move(properties["Address"].get<std::string>()) :
622 channelConfig.ipaddr;
623
624 prefix = channelConfig.netmask.empty() ?
625 properties["PrefixLength"].get<uint8_t>() :
626 ipmi::network::toPrefix(AF_INET,
627 channelConfig.netmask);
628
Ratan Gupta533d03b2017-07-30 10:39:22 +0530629 if (channelConfig.vlanID != ipmi::network::VLAN_ID_MASK)
630 {
631 //get the first twelve bits which is vlan id
632 //not interested in rest of the bits.
633 vlanID = channelConfig.vlanID & ipmi::network::VLAN_ID_MASK;
634 channelConfig.vlanID = le32toh(channelConfig.vlanID);
635 }
636 else
637 {
638 vlanID = ipmi::network::getVLAN(ipObjectInfo.first);
639 }
640
Ratan Guptab8e99552017-07-27 07:07:48 +0530641 }
642 catch (InternalFailure& e)
643 {
644 log<level::INFO>("Failed to get IP object which matches",
645 entry("INTERFACE=%s", ipmi::network::IP_INTERFACE),
646 entry("MATCH=%s", ipmi::network::IP_TYPE));
647 }
648
649 auto systemProperties = ipmi::getAllDbusProperties(
650 bus,
651 systemObject.second,
652 systemObject.first,
653 ipmi::network::SYSTEMCONFIG_INTERFACE);
654
655 gateway = channelConfig.gateway.empty() ?
656 std::move(systemProperties["DefaultGateway"].get<std::string>()) :
657 channelConfig.gateway;
658
659 }
660
661 // Currently network manager doesn't support purging of all the
Ratan Gupta533d03b2017-07-30 10:39:22 +0530662 // ip addresses and the vlan interfaces from the parent interface,
Ratan Guptab8e99552017-07-27 07:07:48 +0530663 // TODO once the support is there, will make the change here.
664 // https://github.com/openbmc/openbmc/issues/2141.
665
666 // TODO Currently IPMI supports single interface,need to handle
667 // Multiple interface through
668 // https://github.com/openbmc/openbmc/issues/2138
669
Ratan Gupta533d03b2017-07-30 10:39:22 +0530670 //delete all the vlan interfaces
671 //delete all the ipv4 addresses
672 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
673 ipmi::network::VLAN_INTERFACE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530674 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
675 ipmi::network::IP_INTERFACE,
676 ipmi::network::IP_TYPE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530677 if (vlanID)
678 {
679 ipmi::network::createVLAN(bus, ipmi::network::SERVICE,
680 ipmi::network::ROOT,
681 ipmi::network::INTERFACE, vlanID);
682
683 networkInterfaceObject = ipmi::getDbusObject(
684 bus,
685 ipmi::network::VLAN_INTERFACE,
686 ipmi::network::ROOT);
687 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530688
689 ipmi::network::createIP(bus, networkInterfaceObject.second,
690 networkInterfaceObject.first,
691 ipv4Protocol, ipaddress, prefix);
692
693 ipmi::setDbusProperty(bus, systemObject.second, systemObject.first,
694 ipmi::network::SYSTEMCONFIG_INTERFACE,
695 "DefaultGateway", gateway);
696
Nan Li3d0df912016-10-18 19:51:41 +0800697 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530698 catch (InternalFailure& e)
699 {
700 log<level::ERR>("Failed to set network data",
701 entry("PREFIX=%d", prefix),
702 entry("ADDRESS=%s", ipaddress),
703 entry("GATEWAY=%s", gateway));
Nan Li3d0df912016-10-18 19:51:41 +0800704
Ratan Guptab8e99552017-07-27 07:07:48 +0530705 commit<InternalFailure>();
Nan Li3d0df912016-10-18 19:51:41 +0800706 rc = IPMI_CC_UNSPECIFIED_ERROR;
707 }
708
Ratan Guptab8e99552017-07-27 07:07:48 +0530709 channelConfig.clear();
Nan Li3d0df912016-10-18 19:51:41 +0800710 return rc;
711}
712
Ratan Gupta533d03b2017-07-30 10:39:22 +0530713
Chris Austenc2cd29d2016-02-05 20:02:29 -0600714// ATTENTION: This ipmi function is very hardcoded on purpose
715// OpenBMC does not fully support IPMI. This command is useful
716// to have around because it enables testing of interfaces with
717// the IPMI tool.
718#define GET_CHANNEL_INFO_CHANNEL_OFFSET 0
719// IPMI Table 6-2
720#define IPMI_CHANNEL_TYPE_IPMB 1
721// IPMI Table 6-3
722#define IPMI_CHANNEL_MEDIUM_TYPE_OTHER 6
723
724ipmi_ret_t ipmi_app_channel_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
725 ipmi_request_t request, ipmi_response_t response,
726 ipmi_data_len_t data_len, ipmi_context_t context)
727{
728 ipmi_ret_t rc = IPMI_CC_OK;
729 uint8_t resp[] = {
730 1,
731 IPMI_CHANNEL_MEDIUM_TYPE_OTHER,
732 IPMI_CHANNEL_TYPE_IPMB,
733 1,0x41,0xA7,0x00,0,0};
734 uint8_t *p = (uint8_t*) request;
735
736 printf("IPMI APP GET CHANNEL INFO\n");
737
tomjose7ec0add2016-06-27 07:59:28 -0500738 // The supported channels numbers are 1 and 8.
739 // Channel Number E is used as way to identify the current channel
740 // that the command is being is received from.
tomjose13fb4412016-03-08 14:02:34 -0600741 if (*p == 0xe || *p == 1 || *p == 8) {
Chris Austenc2cd29d2016-02-05 20:02:29 -0600742
743 *data_len = sizeof(resp);
744 memcpy(response, resp, *data_len);
745
746 } else {
747 rc = IPMI_CC_PARM_OUT_OF_RANGE;
748 *data_len = 0;
749 }
750
751 return rc;
752}
753
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500754ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
755 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530756 ipmi_data_len_t data_len, ipmi_context_t context)
757{
758 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
759
760 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800761 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530762
763 *data_len = strlen("THIS IS WILDCARD");
764
765 // Now pack actual response
766 memcpy(response, "THIS IS WILDCARD", *data_len);
767
768 return rc;
769}
770
Chris Austen6caf28b2015-10-13 12:40:40 -0500771void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530772{
Tom05732372016-09-06 17:21:23 +0530773 // <Get BT Interface Capabilities>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530774 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Tom05732372016-09-06 17:21:23 +0530775 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities,
776 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500777
Tom05732372016-09-06 17:21:23 +0530778 // <Wildcard Command>
Chris Austen6caf28b2015-10-13 12:40:40 -0500779 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
Tom05732372016-09-06 17:21:23 +0530780 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler,
781 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500782
Tom05732372016-09-06 17:21:23 +0530783 // <Reset Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500784 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
Tom05732372016-09-06 17:21:23 +0530785 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog,
786 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500787
Tom05732372016-09-06 17:21:23 +0530788 // <Set Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500789 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
Tom05732372016-09-06 17:21:23 +0530790 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog,
791 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500792
Tom05732372016-09-06 17:21:23 +0530793 // <Get Device ID>
Chris Austen6caf28b2015-10-13 12:40:40 -0500794 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
Tom05732372016-09-06 17:21:23 +0530795 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id,
796 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500797
Tom05732372016-09-06 17:21:23 +0530798 // <Get Self Test Results>
Nan Li41fa24a2016-11-10 20:12:37 +0800799 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS);
Tom05732372016-09-06 17:21:23 +0530800 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL,
801 ipmi_app_get_self_test_results, PRIVILEGE_USER);
Nan Li41fa24a2016-11-10 20:12:37 +0800802
Tom05732372016-09-06 17:21:23 +0530803 // <Get Device GUID>
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500804 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
Tom05732372016-09-06 17:21:23 +0530805 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid,
806 PRIVILEGE_USER);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500807
Tom05732372016-09-06 17:21:23 +0530808 // <Set ACPI Power State>
Chris Austen6caf28b2015-10-13 12:40:40 -0500809 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
Tom05732372016-09-06 17:21:23 +0530810 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state,
811 PRIVILEGE_ADMIN);
Chris Austen6caf28b2015-10-13 12:40:40 -0500812
Tom05732372016-09-06 17:21:23 +0530813 // <Set Channel Access>
Nan Li3d0df912016-10-18 19:51:41 +0800814 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
815 IPMI_CMD_SET_CHAN_ACCESS);
816 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
Tom05732372016-09-06 17:21:23 +0530817 ipmi_set_channel_access, PRIVILEGE_ADMIN);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600818
Tom05732372016-09-06 17:21:23 +0530819 // <Get Channel Info Command>
Chris Austenc2cd29d2016-02-05 20:02:29 -0600820 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
Tom05732372016-09-06 17:21:23 +0530821 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info,
822 PRIVILEGE_USER);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600823
vishwabmcba0bd5f2015-09-30 16:50:23 +0530824 return;
825}
826
Chris Austen6caf28b2015-10-13 12:40:40 -0500827