blob: 8340922e385c790c79eae8dce178f27777530f97 [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>
Tom Joseph69fabfe2017-08-04 10:15:01 +053013#include <vector>
Nan Li3d0df912016-10-18 19:51:41 +080014#include <arpa/inet.h>
Ratan Guptab8e99552017-07-27 07:07:48 +053015#include "transporthandler.hpp"
16
17#include <phosphor-logging/log.hpp>
18#include <phosphor-logging/elog-errors.hpp>
19#include "xyz/openbmc_project/Common/error.hpp"
20
Adriana Kobylak3a552e12015-10-19 16:11:00 -050021extern 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
Patrick Venture6c50ad92017-07-29 22:38:28 -0700421 /*
422 * If the action is 0, it means, do nothing. Multiple actions on timer
423 * expiration aren't supported by phosphor-watchdog yet, so when the
424 * action set is "none", we should just leave the timer disabled.
425 */
426 if (0 == reqptr->t_action)
427 {
428 goto finish;
429 }
430
vishwa1eaea4f2016-02-26 11:57:40 -0600431 if (reqptr->t_use & 0x40)
432 {
433 sd_bus_error_free(&error);
434 reply = sd_bus_message_unref(reply);
435
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530436 // Now Enable Watchdog
437 r = sd_bus_call_method(bus, busname, objname, property_iface,
438 "Set", &error, &reply, "ssv",
439 iface, "Enabled", "b", true);
440 if(r < 0) {
441 fprintf(stderr, "Failed to Enable Watchdog: %s\n",
442 strerror(-r));
443 goto finish;
444 }
445
446 // Set watchdog timer
447 r = sd_bus_call_method(bus, busname, objname, property_iface,
448 "Set", &error, &reply, "ssv",
449 iface, "TimeRemaining", "t", timer_ms);
450 if(r < 0) {
451 fprintf(stderr, "Failed to set new expiration time: %s\n",
452 strerror(-r));
453 goto finish;
vishwa1eaea4f2016-02-26 11:57:40 -0600454 }
455 }
456
457finish:
458 sd_bus_error_free(&error);
459 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500460 free(busname);
vishwa1eaea4f2016-02-26 11:57:40 -0600461
462 return (r < 0) ? -1 : IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -0500463}
464
465
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500466ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
467 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500468 ipmi_data_len_t data_len, ipmi_context_t context)
469{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530470 const char *objname = "/xyz/openbmc_project/watchdog/host0";
471 const char *iface = "xyz.openbmc_project.State.Watchdog";
472 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600473 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500474 sd_bus_error error = SD_BUS_ERROR_NULL;
475 int r = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500476 char *busname = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500477
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530478 // Current time interval that is set in watchdog.
479 uint64_t interval = 0;
480
Chris Austen6caf28b2015-10-13 12:40:40 -0500481 // Status code.
482 ipmi_ret_t rc = IPMI_CC_OK;
483 *data_len = 0;
484
485 printf("WATCHDOG RESET\n");
Sergey Solomineb9b8142016-08-23 09:07:28 -0500486 // Get bus name
487 r = mapper_get_service(bus, objname, &busname);
488 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400489 fprintf(stderr, "Failed to get %s bus name: %s\n",
490 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500491 goto finish;
492 }
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530493
494 // Get the current interval and set it back.
495 r = sd_bus_call_method(bus, busname, objname, property_iface,
496 "Get", &error, &reply, "ss",
497 iface, "Interval");
498
499 if(r < 0) {
500 fprintf(stderr, "Failed to get current Interval msg: %s\n",
501 strerror(-r));
502 goto finish;
503 }
504
505 // Now extract the value
506 r = sd_bus_message_read(reply, "v", "t", &interval);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500507 if (r < 0) {
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530508 fprintf(stderr, "Failed to read current interval: %s\n",
509 strerror(-r));
510 goto finish;
511 }
512
513 sd_bus_error_free(&error);
514 reply = sd_bus_message_unref(reply);
515
516 // Set watchdog timer
517 r = sd_bus_call_method(bus, busname, objname, property_iface,
518 "Set", &error, &reply, "ssv",
519 iface, "TimeRemaining", "t", interval);
520 if(r < 0) {
521 fprintf(stderr, "Failed to refresh the timer: %s\n",
522 strerror(-r));
523 goto finish;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500524 }
525
Sergey Solomineb9b8142016-08-23 09:07:28 -0500526finish:
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500527 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600528 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500529 free(busname);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500530
Chris Austen6caf28b2015-10-13 12:40:40 -0500531 return rc;
532}
533
Ratan Guptab8e99552017-07-27 07:07:48 +0530534extern struct ChannelConfig_t channelConfig;
Nan Li3d0df912016-10-18 19:51:41 +0800535
Ratan Guptab8e99552017-07-27 07:07:48 +0530536ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn,
537 ipmi_cmd_t cmd,
538 ipmi_request_t request,
539 ipmi_response_t response,
540 ipmi_data_len_t data_len,
541 ipmi_context_t context)
Nan Li3d0df912016-10-18 19:51:41 +0800542{
543 ipmi_ret_t rc = IPMI_CC_OK;
544
Ratan Guptab8e99552017-07-27 07:07:48 +0530545 std::string ipaddress;
546 std::string gateway;
547 uint8_t prefix {};
Ratan Gupta533d03b2017-07-30 10:39:22 +0530548 uint32_t vlanID {};
Nan Li3d0df912016-10-18 19:51:41 +0800549
550 // Todo: parse the request data if needed.
551
552 // Using Set Channel cmd to apply changes of Set Lan Cmd.
Ratan Guptab8e99552017-07-27 07:07:48 +0530553 try
554 {
Nan Li3d0df912016-10-18 19:51:41 +0800555
Ratan Guptab8e99552017-07-27 07:07:48 +0530556 sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
557
558 log<level::INFO>("Network data from Cache",
559 entry("PREFIX=%s", channelConfig.netmask.c_str()),
560 entry("ADDRESS=%s", channelConfig.ipaddr.c_str()),
561 entry("GATEWAY=%s", channelConfig.gateway.c_str()));
562
563 auto systemObject = ipmi::getDbusObject(bus,
564 ipmi::network::SYSTEMCONFIG_INTERFACE,
565 ipmi::network::ROOT);
566
567 // TODO Currently IPMI supports single interface,need to handle
568 // Multiple interface through
569 // https://github.com/openbmc/openbmc/issues/2138
570
571 auto networkInterfaceObject = ipmi::getDbusObject(
572 bus,
573 ipmi::network::ETHERNET_INTERFACE,
574 ipmi::network::ROOT,
575 ipmi::network::INTERFACE);
576
577
578
579 // check wthether user has given all the data
580 if (!channelConfig.ipaddr.empty() &&
581 !channelConfig.netmask.empty() &&
582 !channelConfig.gateway.empty())
583 {
584 //convert mask into prefix
585 ipaddress = channelConfig.ipaddr;
586 prefix = ipmi::network::toPrefix(AF_INET, channelConfig.netmask);
587 gateway = channelConfig.gateway;
Ratan Gupta533d03b2017-07-30 10:39:22 +0530588
589 if (channelConfig.vlanID != ipmi::network::VLAN_ID_MASK)
590 {
591 //get the first twelve bits which is vlan id
592 //not interested in rest of the bits.
593 vlanID = channelConfig.vlanID & ipmi::network::VLAN_ID_MASK;
594 channelConfig.vlanID = le32toh(channelConfig.vlanID);
595 }
596
Ratan Guptab8e99552017-07-27 07:07:48 +0530597 }
598 else
599 {
Ratan Gupta533d03b2017-07-30 10:39:22 +0530600 // We have partial filled cache so get the remaning
601 // info from the system.
602
Ratan Guptab8e99552017-07-27 07:07:48 +0530603 // gets the network data from the system as user has
604 // not given all the data then use the data fetched from the
605 // system but it is implementation dependent,IPMI spec doesn't
606 // force it.
607
608 // if system is not having any ip object don't throw error,
609 try
610 {
Ratan Guptab8e99552017-07-27 07:07:48 +0530611 auto ipObjectInfo = ipmi::getDbusObject(bus,
612 ipmi::network::IP_INTERFACE,
613 ipmi::network::ROOT,
614 ipmi::network::IP_TYPE);
615
616 auto properties = ipmi::getAllDbusProperties(bus,
617 ipObjectInfo.second,
618 ipObjectInfo.first,
619 ipmi::network::IP_INTERFACE);
620
621 ipaddress = channelConfig.ipaddr.empty() ?
622 std::move(properties["Address"].get<std::string>()) :
623 channelConfig.ipaddr;
624
625 prefix = channelConfig.netmask.empty() ?
626 properties["PrefixLength"].get<uint8_t>() :
627 ipmi::network::toPrefix(AF_INET,
628 channelConfig.netmask);
629
Ratan Gupta533d03b2017-07-30 10:39:22 +0530630 if (channelConfig.vlanID != ipmi::network::VLAN_ID_MASK)
631 {
632 //get the first twelve bits which is vlan id
633 //not interested in rest of the bits.
634 vlanID = channelConfig.vlanID & ipmi::network::VLAN_ID_MASK;
635 channelConfig.vlanID = le32toh(channelConfig.vlanID);
636 }
637 else
638 {
639 vlanID = ipmi::network::getVLAN(ipObjectInfo.first);
640 }
641
Ratan Guptab8e99552017-07-27 07:07:48 +0530642 }
643 catch (InternalFailure& e)
644 {
645 log<level::INFO>("Failed to get IP object which matches",
646 entry("INTERFACE=%s", ipmi::network::IP_INTERFACE),
647 entry("MATCH=%s", ipmi::network::IP_TYPE));
648 }
649
650 auto systemProperties = ipmi::getAllDbusProperties(
651 bus,
652 systemObject.second,
653 systemObject.first,
654 ipmi::network::SYSTEMCONFIG_INTERFACE);
655
656 gateway = channelConfig.gateway.empty() ?
657 std::move(systemProperties["DefaultGateway"].get<std::string>()) :
658 channelConfig.gateway;
659
660 }
661
662 // Currently network manager doesn't support purging of all the
Ratan Gupta533d03b2017-07-30 10:39:22 +0530663 // ip addresses and the vlan interfaces from the parent interface,
Ratan Guptab8e99552017-07-27 07:07:48 +0530664 // TODO once the support is there, will make the change here.
665 // https://github.com/openbmc/openbmc/issues/2141.
666
667 // TODO Currently IPMI supports single interface,need to handle
668 // Multiple interface through
669 // https://github.com/openbmc/openbmc/issues/2138
670
Ratan Gupta533d03b2017-07-30 10:39:22 +0530671 //delete all the vlan interfaces
672 //delete all the ipv4 addresses
673 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
674 ipmi::network::VLAN_INTERFACE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530675 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
676 ipmi::network::IP_INTERFACE,
677 ipmi::network::IP_TYPE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530678 if (vlanID)
679 {
680 ipmi::network::createVLAN(bus, ipmi::network::SERVICE,
681 ipmi::network::ROOT,
682 ipmi::network::INTERFACE, vlanID);
683
684 networkInterfaceObject = ipmi::getDbusObject(
685 bus,
686 ipmi::network::VLAN_INTERFACE,
687 ipmi::network::ROOT);
688 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530689
690 ipmi::network::createIP(bus, networkInterfaceObject.second,
691 networkInterfaceObject.first,
692 ipv4Protocol, ipaddress, prefix);
693
694 ipmi::setDbusProperty(bus, systemObject.second, systemObject.first,
695 ipmi::network::SYSTEMCONFIG_INTERFACE,
696 "DefaultGateway", gateway);
697
Nan Li3d0df912016-10-18 19:51:41 +0800698 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530699 catch (InternalFailure& e)
700 {
701 log<level::ERR>("Failed to set network data",
702 entry("PREFIX=%d", prefix),
703 entry("ADDRESS=%s", ipaddress),
704 entry("GATEWAY=%s", gateway));
Nan Li3d0df912016-10-18 19:51:41 +0800705
Ratan Guptab8e99552017-07-27 07:07:48 +0530706 commit<InternalFailure>();
Nan Li3d0df912016-10-18 19:51:41 +0800707 rc = IPMI_CC_UNSPECIFIED_ERROR;
708 }
709
Ratan Guptab8e99552017-07-27 07:07:48 +0530710 channelConfig.clear();
Nan Li3d0df912016-10-18 19:51:41 +0800711 return rc;
712}
713
Tom Joseph69fabfe2017-08-04 10:15:01 +0530714ipmi_ret_t getChannelAccess(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
715 ipmi_request_t request, ipmi_response_t response,
716 ipmi_data_len_t data_len, ipmi_context_t context)
717{
718 auto requestData = reinterpret_cast<const GetChannelAccessRequest*>
719 (request);
720 std::vector<uint8_t> outPayload(sizeof(GetChannelAccessResponse));
721 auto responseData = reinterpret_cast<GetChannelAccessResponse*>
722 (outPayload.data());
723
724 // Channel 1 is arbitrarily assigned to ETH0 channel
725 constexpr auto channelOne = 0x01;
726
727 /*
728 * The value Eh is used as a way to identify the current channel that
729 * the command is being received from.
730 */
731 constexpr auto channelE = 0x0E;
732
733 if (requestData->channelNumber != channelOne &&
734 requestData->channelNumber != channelE)
735 {
736 *data_len = 0;
737 return IPMI_CC_INVALID_FIELD_REQUEST;
738 }
739
740 /*
741 * [7:6] - reserved
742 * [5] - 1b = Alerting disabled
743 * [4] - 1b = per message authentication disabled
744 * [3] - 0b = User level authentication enabled
745 * [2:0] - 2h = always available
746 */
747 constexpr auto channelSetting = 0x32;
748
749 responseData->settings = channelSetting;
750 //Defaulting the channel privilege to administrator level.
751 responseData->privilegeLimit = PRIVILEGE_ADMIN;
752
753 *data_len = outPayload.size();
754 memcpy(response, outPayload.data(), *data_len);
755
756 return IPMI_CC_OK;
757}
Ratan Gupta533d03b2017-07-30 10:39:22 +0530758
Chris Austenc2cd29d2016-02-05 20:02:29 -0600759// ATTENTION: This ipmi function is very hardcoded on purpose
760// OpenBMC does not fully support IPMI. This command is useful
761// to have around because it enables testing of interfaces with
762// the IPMI tool.
763#define GET_CHANNEL_INFO_CHANNEL_OFFSET 0
764// IPMI Table 6-2
765#define IPMI_CHANNEL_TYPE_IPMB 1
766// IPMI Table 6-3
767#define IPMI_CHANNEL_MEDIUM_TYPE_OTHER 6
768
769ipmi_ret_t ipmi_app_channel_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
770 ipmi_request_t request, ipmi_response_t response,
771 ipmi_data_len_t data_len, ipmi_context_t context)
772{
773 ipmi_ret_t rc = IPMI_CC_OK;
774 uint8_t resp[] = {
775 1,
776 IPMI_CHANNEL_MEDIUM_TYPE_OTHER,
777 IPMI_CHANNEL_TYPE_IPMB,
778 1,0x41,0xA7,0x00,0,0};
779 uint8_t *p = (uint8_t*) request;
780
781 printf("IPMI APP GET CHANNEL INFO\n");
782
tomjose7ec0add2016-06-27 07:59:28 -0500783 // The supported channels numbers are 1 and 8.
784 // Channel Number E is used as way to identify the current channel
785 // that the command is being is received from.
tomjose13fb4412016-03-08 14:02:34 -0600786 if (*p == 0xe || *p == 1 || *p == 8) {
Chris Austenc2cd29d2016-02-05 20:02:29 -0600787
788 *data_len = sizeof(resp);
789 memcpy(response, resp, *data_len);
790
791 } else {
792 rc = IPMI_CC_PARM_OUT_OF_RANGE;
793 *data_len = 0;
794 }
795
796 return rc;
797}
798
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500799ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
800 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530801 ipmi_data_len_t data_len, ipmi_context_t context)
802{
803 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
804
805 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800806 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530807
808 *data_len = strlen("THIS IS WILDCARD");
809
810 // Now pack actual response
811 memcpy(response, "THIS IS WILDCARD", *data_len);
812
813 return rc;
814}
815
Chris Austen6caf28b2015-10-13 12:40:40 -0500816void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530817{
Tom05732372016-09-06 17:21:23 +0530818 // <Get BT Interface Capabilities>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530819 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Tom05732372016-09-06 17:21:23 +0530820 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities,
821 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500822
Tom05732372016-09-06 17:21:23 +0530823 // <Wildcard Command>
Chris Austen6caf28b2015-10-13 12:40:40 -0500824 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
Tom05732372016-09-06 17:21:23 +0530825 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler,
826 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500827
Tom05732372016-09-06 17:21:23 +0530828 // <Reset Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500829 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
Tom05732372016-09-06 17:21:23 +0530830 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog,
831 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500832
Tom05732372016-09-06 17:21:23 +0530833 // <Set Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500834 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
Tom05732372016-09-06 17:21:23 +0530835 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog,
836 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500837
Tom05732372016-09-06 17:21:23 +0530838 // <Get Device ID>
Chris Austen6caf28b2015-10-13 12:40:40 -0500839 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
Tom05732372016-09-06 17:21:23 +0530840 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id,
841 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500842
Tom05732372016-09-06 17:21:23 +0530843 // <Get Self Test Results>
Nan Li41fa24a2016-11-10 20:12:37 +0800844 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS);
Tom05732372016-09-06 17:21:23 +0530845 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL,
846 ipmi_app_get_self_test_results, PRIVILEGE_USER);
Nan Li41fa24a2016-11-10 20:12:37 +0800847
Tom05732372016-09-06 17:21:23 +0530848 // <Get Device GUID>
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500849 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
Tom05732372016-09-06 17:21:23 +0530850 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid,
851 PRIVILEGE_USER);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500852
Tom05732372016-09-06 17:21:23 +0530853 // <Set ACPI Power State>
Chris Austen6caf28b2015-10-13 12:40:40 -0500854 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
Tom05732372016-09-06 17:21:23 +0530855 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state,
856 PRIVILEGE_ADMIN);
Chris Austen6caf28b2015-10-13 12:40:40 -0500857
Tom05732372016-09-06 17:21:23 +0530858 // <Set Channel Access>
Nan Li3d0df912016-10-18 19:51:41 +0800859 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
860 IPMI_CMD_SET_CHAN_ACCESS);
861 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
Tom05732372016-09-06 17:21:23 +0530862 ipmi_set_channel_access, PRIVILEGE_ADMIN);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600863
Tom Joseph69fabfe2017-08-04 10:15:01 +0530864 // <Get Channel Access>
865 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHANNEL_ACCESS);
866 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHANNEL_ACCESS, NULL,
867 getChannelAccess, PRIVILEGE_USER);
868
Tom05732372016-09-06 17:21:23 +0530869 // <Get Channel Info Command>
Chris Austenc2cd29d2016-02-05 20:02:29 -0600870 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
Tom05732372016-09-06 17:21:23 +0530871 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info,
872 PRIVILEGE_USER);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600873
vishwabmcba0bd5f2015-09-30 16:50:23 +0530874 return;
875}
876
Chris Austen6caf28b2015-10-13 12:40:40 -0500877