blob: 2251e8cb1ca7e9ec4ded3290bfe2a4670ab6f870 [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
420 if (reqptr->t_use & 0x40)
421 {
422 sd_bus_error_free(&error);
423 reply = sd_bus_message_unref(reply);
424
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530425 // Now Enable Watchdog
426 r = sd_bus_call_method(bus, busname, objname, property_iface,
427 "Set", &error, &reply, "ssv",
428 iface, "Enabled", "b", true);
429 if(r < 0) {
430 fprintf(stderr, "Failed to Enable Watchdog: %s\n",
431 strerror(-r));
432 goto finish;
433 }
434
435 // Set watchdog timer
436 r = sd_bus_call_method(bus, busname, objname, property_iface,
437 "Set", &error, &reply, "ssv",
438 iface, "TimeRemaining", "t", timer_ms);
439 if(r < 0) {
440 fprintf(stderr, "Failed to set new expiration time: %s\n",
441 strerror(-r));
442 goto finish;
vishwa1eaea4f2016-02-26 11:57:40 -0600443 }
444 }
445
446finish:
447 sd_bus_error_free(&error);
448 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500449 free(busname);
vishwa1eaea4f2016-02-26 11:57:40 -0600450
451 return (r < 0) ? -1 : IPMI_CC_OK;
Chris Austen6caf28b2015-10-13 12:40:40 -0500452}
453
454
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500455ipmi_ret_t ipmi_app_reset_watchdog(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
456 ipmi_request_t request, ipmi_response_t response,
Chris Austen6caf28b2015-10-13 12:40:40 -0500457 ipmi_data_len_t data_len, ipmi_context_t context)
458{
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530459 const char *objname = "/xyz/openbmc_project/watchdog/host0";
460 const char *iface = "xyz.openbmc_project.State.Watchdog";
461 const char *property_iface = "org.freedesktop.DBus.Properties";
vishwa1eaea4f2016-02-26 11:57:40 -0600462 sd_bus_message *reply = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500463 sd_bus_error error = SD_BUS_ERROR_NULL;
464 int r = 0;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500465 char *busname = NULL;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500466
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530467 // Current time interval that is set in watchdog.
468 uint64_t interval = 0;
469
Chris Austen6caf28b2015-10-13 12:40:40 -0500470 // Status code.
471 ipmi_ret_t rc = IPMI_CC_OK;
472 *data_len = 0;
473
474 printf("WATCHDOG RESET\n");
Sergey Solomineb9b8142016-08-23 09:07:28 -0500475 // Get bus name
476 r = mapper_get_service(bus, objname, &busname);
477 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400478 fprintf(stderr, "Failed to get %s bus name: %s\n",
479 objname, strerror(-r));
Sergey Solomineb9b8142016-08-23 09:07:28 -0500480 goto finish;
481 }
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530482
483 // Get the current interval and set it back.
484 r = sd_bus_call_method(bus, busname, objname, property_iface,
485 "Get", &error, &reply, "ss",
486 iface, "Interval");
487
488 if(r < 0) {
489 fprintf(stderr, "Failed to get current Interval msg: %s\n",
490 strerror(-r));
491 goto finish;
492 }
493
494 // Now extract the value
495 r = sd_bus_message_read(reply, "v", "t", &interval);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500496 if (r < 0) {
Vishwanatha Subbanna8fc09882017-06-12 19:57:37 +0530497 fprintf(stderr, "Failed to read current interval: %s\n",
498 strerror(-r));
499 goto finish;
500 }
501
502 sd_bus_error_free(&error);
503 reply = sd_bus_message_unref(reply);
504
505 // Set watchdog timer
506 r = sd_bus_call_method(bus, busname, objname, property_iface,
507 "Set", &error, &reply, "ssv",
508 iface, "TimeRemaining", "t", interval);
509 if(r < 0) {
510 fprintf(stderr, "Failed to refresh the timer: %s\n",
511 strerror(-r));
512 goto finish;
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500513 }
514
Sergey Solomineb9b8142016-08-23 09:07:28 -0500515finish:
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500516 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600517 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500518 free(busname);
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500519
Chris Austen6caf28b2015-10-13 12:40:40 -0500520 return rc;
521}
522
Ratan Guptab8e99552017-07-27 07:07:48 +0530523extern struct ChannelConfig_t channelConfig;
Nan Li3d0df912016-10-18 19:51:41 +0800524
Ratan Guptab8e99552017-07-27 07:07:48 +0530525ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn,
526 ipmi_cmd_t cmd,
527 ipmi_request_t request,
528 ipmi_response_t response,
529 ipmi_data_len_t data_len,
530 ipmi_context_t context)
Nan Li3d0df912016-10-18 19:51:41 +0800531{
532 ipmi_ret_t rc = IPMI_CC_OK;
533
Ratan Guptab8e99552017-07-27 07:07:48 +0530534 std::string ipaddress;
535 std::string gateway;
536 uint8_t prefix {};
Ratan Gupta533d03b2017-07-30 10:39:22 +0530537 uint32_t vlanID {};
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;
Ratan Gupta533d03b2017-07-30 10:39:22 +0530577
578 if (channelConfig.vlanID != ipmi::network::VLAN_ID_MASK)
579 {
580 //get the first twelve bits which is vlan id
581 //not interested in rest of the bits.
582 vlanID = channelConfig.vlanID & ipmi::network::VLAN_ID_MASK;
583 channelConfig.vlanID = le32toh(channelConfig.vlanID);
584 }
585
Ratan Guptab8e99552017-07-27 07:07:48 +0530586 }
587 else
588 {
Ratan Gupta533d03b2017-07-30 10:39:22 +0530589 // We have partial filled cache so get the remaning
590 // info from the system.
591
Ratan Guptab8e99552017-07-27 07:07:48 +0530592 // gets the network data from the system as user has
593 // not given all the data then use the data fetched from the
594 // system but it is implementation dependent,IPMI spec doesn't
595 // force it.
596
597 // if system is not having any ip object don't throw error,
598 try
599 {
Ratan Guptab8e99552017-07-27 07:07:48 +0530600 auto ipObjectInfo = ipmi::getDbusObject(bus,
601 ipmi::network::IP_INTERFACE,
602 ipmi::network::ROOT,
603 ipmi::network::IP_TYPE);
604
605 auto properties = ipmi::getAllDbusProperties(bus,
606 ipObjectInfo.second,
607 ipObjectInfo.first,
608 ipmi::network::IP_INTERFACE);
609
610 ipaddress = channelConfig.ipaddr.empty() ?
611 std::move(properties["Address"].get<std::string>()) :
612 channelConfig.ipaddr;
613
614 prefix = channelConfig.netmask.empty() ?
615 properties["PrefixLength"].get<uint8_t>() :
616 ipmi::network::toPrefix(AF_INET,
617 channelConfig.netmask);
618
Ratan Gupta533d03b2017-07-30 10:39:22 +0530619 if (channelConfig.vlanID != ipmi::network::VLAN_ID_MASK)
620 {
621 //get the first twelve bits which is vlan id
622 //not interested in rest of the bits.
623 vlanID = channelConfig.vlanID & ipmi::network::VLAN_ID_MASK;
624 channelConfig.vlanID = le32toh(channelConfig.vlanID);
625 }
626 else
627 {
628 vlanID = ipmi::network::getVLAN(ipObjectInfo.first);
629 }
630
Ratan Guptab8e99552017-07-27 07:07:48 +0530631 }
632 catch (InternalFailure& e)
633 {
634 log<level::INFO>("Failed to get IP object which matches",
635 entry("INTERFACE=%s", ipmi::network::IP_INTERFACE),
636 entry("MATCH=%s", ipmi::network::IP_TYPE));
637 }
638
639 auto systemProperties = ipmi::getAllDbusProperties(
640 bus,
641 systemObject.second,
642 systemObject.first,
643 ipmi::network::SYSTEMCONFIG_INTERFACE);
644
645 gateway = channelConfig.gateway.empty() ?
646 std::move(systemProperties["DefaultGateway"].get<std::string>()) :
647 channelConfig.gateway;
648
649 }
650
651 // Currently network manager doesn't support purging of all the
Ratan Gupta533d03b2017-07-30 10:39:22 +0530652 // ip addresses and the vlan interfaces from the parent interface,
Ratan Guptab8e99552017-07-27 07:07:48 +0530653 // TODO once the support is there, will make the change here.
654 // https://github.com/openbmc/openbmc/issues/2141.
655
656 // TODO Currently IPMI supports single interface,need to handle
657 // Multiple interface through
658 // https://github.com/openbmc/openbmc/issues/2138
659
Ratan Gupta533d03b2017-07-30 10:39:22 +0530660 //delete all the vlan interfaces
661 //delete all the ipv4 addresses
662 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
663 ipmi::network::VLAN_INTERFACE);
Ratan Guptab8e99552017-07-27 07:07:48 +0530664 ipmi::deleteAllDbusObjects(bus, ipmi::network::ROOT,
665 ipmi::network::IP_INTERFACE,
666 ipmi::network::IP_TYPE);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530667 if (vlanID)
668 {
669 ipmi::network::createVLAN(bus, ipmi::network::SERVICE,
670 ipmi::network::ROOT,
671 ipmi::network::INTERFACE, vlanID);
672
673 networkInterfaceObject = ipmi::getDbusObject(
674 bus,
675 ipmi::network::VLAN_INTERFACE,
676 ipmi::network::ROOT);
677 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530678
679 ipmi::network::createIP(bus, networkInterfaceObject.second,
680 networkInterfaceObject.first,
681 ipv4Protocol, ipaddress, prefix);
682
683 ipmi::setDbusProperty(bus, systemObject.second, systemObject.first,
684 ipmi::network::SYSTEMCONFIG_INTERFACE,
685 "DefaultGateway", gateway);
686
Nan Li3d0df912016-10-18 19:51:41 +0800687 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530688 catch (InternalFailure& e)
689 {
690 log<level::ERR>("Failed to set network data",
691 entry("PREFIX=%d", prefix),
692 entry("ADDRESS=%s", ipaddress),
693 entry("GATEWAY=%s", gateway));
Nan Li3d0df912016-10-18 19:51:41 +0800694
Ratan Guptab8e99552017-07-27 07:07:48 +0530695 commit<InternalFailure>();
Nan Li3d0df912016-10-18 19:51:41 +0800696 rc = IPMI_CC_UNSPECIFIED_ERROR;
697 }
698
Ratan Guptab8e99552017-07-27 07:07:48 +0530699 channelConfig.clear();
Nan Li3d0df912016-10-18 19:51:41 +0800700 return rc;
701}
702
Ratan Gupta533d03b2017-07-30 10:39:22 +0530703
Chris Austenc2cd29d2016-02-05 20:02:29 -0600704// ATTENTION: This ipmi function is very hardcoded on purpose
705// OpenBMC does not fully support IPMI. This command is useful
706// to have around because it enables testing of interfaces with
707// the IPMI tool.
708#define GET_CHANNEL_INFO_CHANNEL_OFFSET 0
709// IPMI Table 6-2
710#define IPMI_CHANNEL_TYPE_IPMB 1
711// IPMI Table 6-3
712#define IPMI_CHANNEL_MEDIUM_TYPE_OTHER 6
713
714ipmi_ret_t ipmi_app_channel_info(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 ipmi_ret_t rc = IPMI_CC_OK;
719 uint8_t resp[] = {
720 1,
721 IPMI_CHANNEL_MEDIUM_TYPE_OTHER,
722 IPMI_CHANNEL_TYPE_IPMB,
723 1,0x41,0xA7,0x00,0,0};
724 uint8_t *p = (uint8_t*) request;
725
726 printf("IPMI APP GET CHANNEL INFO\n");
727
tomjose7ec0add2016-06-27 07:59:28 -0500728 // The supported channels numbers are 1 and 8.
729 // Channel Number E is used as way to identify the current channel
730 // that the command is being is received from.
tomjose13fb4412016-03-08 14:02:34 -0600731 if (*p == 0xe || *p == 1 || *p == 8) {
Chris Austenc2cd29d2016-02-05 20:02:29 -0600732
733 *data_len = sizeof(resp);
734 memcpy(response, resp, *data_len);
735
736 } else {
737 rc = IPMI_CC_PARM_OUT_OF_RANGE;
738 *data_len = 0;
739 }
740
741 return rc;
742}
743
Adriana Kobylak3a552e12015-10-19 16:11:00 -0500744ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
745 ipmi_request_t request, ipmi_response_t response,
vishwabmcba0bd5f2015-09-30 16:50:23 +0530746 ipmi_data_len_t data_len, ipmi_context_t context)
747{
748 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
749
750 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +0800751 ipmi_ret_t rc = IPMI_CC_INVALID;
vishwabmcba0bd5f2015-09-30 16:50:23 +0530752
753 *data_len = strlen("THIS IS WILDCARD");
754
755 // Now pack actual response
756 memcpy(response, "THIS IS WILDCARD", *data_len);
757
758 return rc;
759}
760
Chris Austen6caf28b2015-10-13 12:40:40 -0500761void register_netfn_app_functions()
vishwabmcba0bd5f2015-09-30 16:50:23 +0530762{
Tom05732372016-09-06 17:21:23 +0530763 // <Get BT Interface Capabilities>
vishwabmcba0bd5f2015-09-30 16:50:23 +0530764 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT);
Tom05732372016-09-06 17:21:23 +0530765 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities,
766 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500767
Tom05732372016-09-06 17:21:23 +0530768 // <Wildcard Command>
Chris Austen6caf28b2015-10-13 12:40:40 -0500769 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD);
Tom05732372016-09-06 17:21:23 +0530770 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler,
771 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500772
Tom05732372016-09-06 17:21:23 +0530773 // <Reset Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500774 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD);
Tom05732372016-09-06 17:21:23 +0530775 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog,
776 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500777
Tom05732372016-09-06 17:21:23 +0530778 // <Set Watchdog Timer>
Chris Austen6caf28b2015-10-13 12:40:40 -0500779 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD);
Tom05732372016-09-06 17:21:23 +0530780 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog,
781 PRIVILEGE_OPERATOR);
Chris Austen6caf28b2015-10-13 12:40:40 -0500782
Tom05732372016-09-06 17:21:23 +0530783 // <Get Device ID>
Chris Austen6caf28b2015-10-13 12:40:40 -0500784 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID);
Tom05732372016-09-06 17:21:23 +0530785 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id,
786 PRIVILEGE_USER);
Chris Austen6caf28b2015-10-13 12:40:40 -0500787
Tom05732372016-09-06 17:21:23 +0530788 // <Get Self Test Results>
Nan Li41fa24a2016-11-10 20:12:37 +0800789 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS);
Tom05732372016-09-06 17:21:23 +0530790 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL,
791 ipmi_app_get_self_test_results, PRIVILEGE_USER);
Nan Li41fa24a2016-11-10 20:12:37 +0800792
Tom05732372016-09-06 17:21:23 +0530793 // <Get Device GUID>
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500794 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID);
Tom05732372016-09-06 17:21:23 +0530795 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid,
796 PRIVILEGE_USER);
Adriana Kobylakd100ee52015-10-20 17:02:37 -0500797
Tom05732372016-09-06 17:21:23 +0530798 // <Set ACPI Power State>
Chris Austen6caf28b2015-10-13 12:40:40 -0500799 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI);
Tom05732372016-09-06 17:21:23 +0530800 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state,
801 PRIVILEGE_ADMIN);
Chris Austen6caf28b2015-10-13 12:40:40 -0500802
Tom05732372016-09-06 17:21:23 +0530803 // <Set Channel Access>
Nan Li3d0df912016-10-18 19:51:41 +0800804 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
805 IPMI_CMD_SET_CHAN_ACCESS);
806 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
Tom05732372016-09-06 17:21:23 +0530807 ipmi_set_channel_access, PRIVILEGE_ADMIN);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600808
Tom05732372016-09-06 17:21:23 +0530809 // <Get Channel Info Command>
Chris Austenc2cd29d2016-02-05 20:02:29 -0600810 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
Tom05732372016-09-06 17:21:23 +0530811 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info,
812 PRIVILEGE_USER);
Chris Austenc2cd29d2016-02-05 20:02:29 -0600813
vishwabmcba0bd5f2015-09-30 16:50:23 +0530814 return;
815}
816
Chris Austen6caf28b2015-10-13 12:40:40 -0500817