1 #include "apphandler.h" 2 #include "app/channel.hpp" 3 #include "app/watchdog.hpp" 4 #include "host-ipmid/ipmid-api.h" 5 #include "ipmid.hpp" 6 #include "types.hpp" 7 #include "utils.hpp" 8 9 #include <stdio.h> 10 #include <string.h> 11 #include <stdint.h> 12 #include <systemd/sd-bus.h> 13 #include <mapper.h> 14 #include <array> 15 #include <vector> 16 #include <experimental/filesystem> 17 18 #include <arpa/inet.h> 19 #include "transporthandler.hpp" 20 21 #include <phosphor-logging/log.hpp> 22 #include <phosphor-logging/elog-errors.hpp> 23 #include "xyz/openbmc_project/Common/error.hpp" 24 25 extern sd_bus *bus; 26 27 constexpr auto app_obj = "/org/openbmc/NetworkManager/Interface"; 28 constexpr auto app_ifc = "org.openbmc.NetworkManager"; 29 constexpr auto app_nwinterface = "eth0"; 30 31 void register_netfn_app_functions() __attribute__((constructor)); 32 33 using namespace phosphor::logging; 34 using namespace sdbusplus::xyz::openbmc_project::Common::Error; 35 namespace fs = std::experimental::filesystem; 36 37 // Offset in get device id command. 38 typedef struct 39 { 40 uint8_t id; 41 uint8_t revision; 42 uint8_t fw[2]; 43 uint8_t ipmi_ver; 44 uint8_t addn_dev_support; 45 uint8_t manuf_id[3]; 46 uint8_t prod_id[2]; 47 uint8_t aux[4]; 48 }__attribute__((packed)) ipmi_device_id_t; 49 50 ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 51 ipmi_request_t request, ipmi_response_t response, 52 ipmi_data_len_t data_len, ipmi_context_t context) 53 { 54 ipmi_ret_t rc = IPMI_CC_OK; 55 *data_len = 0; 56 57 printf("IPMI SET ACPI STATE Ignoring for now\n"); 58 return rc; 59 } 60 61 62 typedef struct 63 { 64 char major; 65 char minor; 66 uint16_t d[2]; 67 } rev_t; 68 69 70 /* Currently only supports the vx.x-x-[-x] format Will return -1 if not in */ 71 /* the format this routine knows how to parse */ 72 /* version = v0.6-19-gf363f61-dirty */ 73 /* ^ ^ ^^ ^ */ 74 /* | | |----------|-- additional details */ 75 /* | |---------------- Minor */ 76 /* |------------------ Major */ 77 /* Additional details : If the option group exists it will force Auxiliary */ 78 /* Firmware Revision Information 4th byte to 1 indicating the build was */ 79 /* derived with additional edits */ 80 int convert_version(const char *p, rev_t *rev) 81 { 82 char *s, *token; 83 uint16_t commits; 84 85 if (*p != 'v') 86 return -1; 87 p++; 88 89 s = strdup(p); 90 token = strtok(s,".-"); 91 92 rev->major = (int8_t) atoi(token); 93 94 token = strtok(NULL, ".-"); 95 rev->minor = (int8_t) atoi(token); 96 97 // Capture the number of commits on top of the minor tag. 98 // I'm using BE format like the ipmi spec asked for 99 token = strtok(NULL,".-"); 100 101 if (token) { 102 commits = (int16_t) atoi(token); 103 rev->d[0] = (commits>>8) | (commits<<8); 104 105 // commit number we skip 106 token = strtok(NULL,".-"); 107 108 } else { 109 rev->d[0] = 0; 110 } 111 112 // Any value of the optional parameter forces it to 1 113 if (token) 114 token = strtok(NULL,".-"); 115 116 rev->d[1] = (token != NULL) ? 1 : 0; 117 118 free(s); 119 return 0; 120 } 121 122 ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 123 ipmi_request_t request, ipmi_response_t response, 124 ipmi_data_len_t data_len, ipmi_context_t context) 125 { 126 ipmi_ret_t rc = IPMI_CC_OK; 127 const char *objname = "/org/openbmc/inventory/system/chassis/motherboard/bmc"; 128 const char *iface = "org.openbmc.InventoryItem"; 129 char *ver = NULL; 130 char *busname = NULL; 131 int r; 132 rev_t rev = {0}; 133 ipmi_device_id_t dev_id{}; 134 135 // Data length 136 *data_len = sizeof(dev_id); 137 138 // From IPMI spec, controller that have different application commands, or different 139 // definitions of OEM fields, are expected to have different Device ID values. 140 // Set to 0 now. 141 142 // Device Revision is set to 0 now. 143 // Bit7 identifies if device provide Device SDRs, obmc don't have SDR,we use ipmi to 144 // simulate SDR, hence the value: 145 dev_id.revision = 0x80; 146 147 // Firmware revision is already implemented, so get it from appropriate position. 148 r = mapper_get_service(bus, objname, &busname); 149 if (r < 0) { 150 fprintf(stderr, "Failed to get %s bus name: %s\n", 151 objname, strerror(-r)); 152 goto finish; 153 } 154 r = sd_bus_get_property_string(bus,busname,objname,iface,"version", NULL, &ver); 155 if ( r < 0 ) { 156 fprintf(stderr, "Failed to obtain version property: %s\n", strerror(-r)); 157 } else { 158 r = convert_version(ver, &rev); 159 if( r >= 0 ) { 160 // bit7 identifies if the device is available, 0=normal operation, 161 // 1=device firmware, SDR update or self-initialization in progress. 162 // our SDR is normal working condition, so mask: 163 dev_id.fw[0] = 0x7F & rev.major; 164 165 rev.minor = (rev.minor > 99 ? 99 : rev.minor); 166 dev_id.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16; 167 memcpy(&dev_id.aux, rev.d, 4); 168 } 169 } 170 171 // IPMI Spec version 2.0 172 dev_id.ipmi_ver = 2; 173 174 // Additional device Support. 175 // List the 'logical device' commands and functions that the controller supports 176 // that are in addition to the mandatory IPM and Application commands. 177 // [7] Chassis Device (device functions as chassis device per ICMB spec.) 178 // [6] Bridge (device responds to Bridge NetFn commands) 179 // [5] IPMB Event Generator 180 // [4] IPMB Event Receiver 181 // [3] FRU Inventory Device 182 // [2] SEL Device 183 // [1] SDR Repository Device 184 // [0] Sensor Device 185 // We support FRU/SEL/Sensor now: 186 dev_id.addn_dev_support = 0x8D; 187 188 // This value is the IANA number assigned to "IBM Platform Firmware 189 // Division", which is also used by our service processor. We may want 190 // a different number or at least a different version? 191 dev_id.manuf_id[0] = 0x41; 192 dev_id.manuf_id[1] = 0xA7; 193 dev_id.manuf_id[2] = 0x00; 194 195 // Witherspoon's product ID is hardcoded to 4F42(ASCII 'OB'). 196 // TODO: openbmc/openbmc#495 197 dev_id.prod_id[0] = 0x4F; 198 dev_id.prod_id[1] = 0x42; 199 200 // Pack the actual response 201 memcpy(response, &dev_id, *data_len); 202 finish: 203 free(busname); 204 free(ver); 205 return rc; 206 } 207 208 ipmi_ret_t ipmi_app_get_self_test_results(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 209 ipmi_request_t request, ipmi_response_t response, 210 ipmi_data_len_t data_len, ipmi_context_t context) 211 { 212 ipmi_ret_t rc = IPMI_CC_OK; 213 214 // Byte 2: 215 // 55h - No error. 216 // 56h - Self Test function not implemented in this controller. 217 // 57h - Corrupted or inaccesssible data or devices. 218 // 58h - Fatal hardware error. 219 // FFh - reserved. 220 // all other: Device-specific 'internal failure'. 221 // Byte 3: 222 // For byte 2 = 55h, 56h, FFh: 00h 223 // For byte 2 = 58h, all other: Device-specific 224 // For byte 2 = 57h: self-test error bitfield. 225 // Note: returning 57h does not imply that all test were run. 226 // [7] 1b = Cannot access SEL device. 227 // [6] 1b = Cannot access SDR Repository. 228 // [5] 1b = Cannot access BMC FRU device. 229 // [4] 1b = IPMB signal lines do not respond. 230 // [3] 1b = SDR Repository empty. 231 // [2] 1b = Internal Use Area of BMC FRU corrupted. 232 // [1] 1b = controller update 'boot block' firmware corrupted. 233 // [0] 1b = controller operational firmware corrupted. 234 235 char selftestresults[2] = {0}; 236 237 *data_len = 2; 238 239 selftestresults[0] = 0x56; 240 selftestresults[1] = 0; 241 242 memcpy(response, selftestresults, *data_len); 243 244 return rc; 245 } 246 247 ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 248 ipmi_request_t request, ipmi_response_t response, 249 ipmi_data_len_t data_len, ipmi_context_t context) 250 { 251 const char *objname = "/org/openbmc/control/chassis0"; 252 const char *iface = "org.freedesktop.DBus.Properties"; 253 const char *chassis_iface = "org.openbmc.control.Chassis"; 254 sd_bus_message *reply = NULL; 255 sd_bus_error error = SD_BUS_ERROR_NULL; 256 int r = 0; 257 char *uuid = NULL; 258 char *busname = NULL; 259 260 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 261 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte order 262 // Ex: 0x2332fc2c40e66298e511f2782395a361 263 264 const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec 265 uint8_t resp_uuid[resp_size]; // Array to hold the formatted response 266 int resp_loc = resp_size-1; // Point resp end of array to save in reverse order 267 int i = 0; 268 char *tokptr = NULL; 269 char *id_octet = NULL; 270 271 // Status code. 272 ipmi_ret_t rc = IPMI_CC_OK; 273 *data_len = 0; 274 275 printf("IPMI GET DEVICE GUID\n"); 276 277 // Call Get properties method with the interface and property name 278 r = mapper_get_service(bus, objname, &busname); 279 if (r < 0) { 280 fprintf(stderr, "Failed to get %s bus name: %s\n", 281 objname, strerror(-r)); 282 goto finish; 283 } 284 r = sd_bus_call_method(bus,busname,objname,iface, 285 "Get",&error, &reply, "ss", 286 chassis_iface, "uuid"); 287 if (r < 0) 288 { 289 fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r)); 290 rc = IPMI_CC_UNSPECIFIED_ERROR; 291 goto finish; 292 } 293 294 r = sd_bus_message_read(reply, "v", "s", &uuid); 295 if (r < 0 || uuid == NULL) 296 { 297 fprintf(stderr, "Failed to get a response: %s", strerror(-r)); 298 rc = IPMI_CC_RESPONSE_ERROR; 299 goto finish; 300 } 301 302 // Traverse the UUID 303 id_octet = strtok_r(uuid, "-", &tokptr); // Get the UUID octects separated by dash 304 305 if (id_octet == NULL) 306 { 307 // Error 308 fprintf(stderr, "Unexpected UUID format: %s", uuid); 309 rc = IPMI_CC_RESPONSE_ERROR; 310 goto finish; 311 } 312 313 while (id_octet != NULL) 314 { 315 // Calculate the octet string size since it varies 316 // Divide it by 2 for the array size since 1 byte is built from 2 chars 317 int tmp_size = strlen(id_octet)/2; 318 319 for(i = 0; i < tmp_size; i++) 320 { 321 char tmp_array[3] = {0}; // Holder of the 2 chars that will become a byte 322 strncpy(tmp_array, id_octet, 2); // 2 chars at a time 323 324 int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte 325 memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); // Copy end to first 326 resp_loc--; 327 id_octet+=2; // Finished with the 2 chars, advance 328 } 329 id_octet=strtok_r(NULL, "-", &tokptr); // Get next octet 330 } 331 332 // Data length 333 *data_len = resp_size; 334 335 // Pack the actual response 336 memcpy(response, &resp_uuid, *data_len); 337 338 finish: 339 sd_bus_error_free(&error); 340 reply = sd_bus_message_unref(reply); 341 free(busname); 342 343 return rc; 344 } 345 346 ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 347 ipmi_request_t request, ipmi_response_t response, 348 ipmi_data_len_t data_len, ipmi_context_t context) 349 { 350 printf("Handling Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd); 351 352 // Status code. 353 ipmi_ret_t rc = IPMI_CC_OK; 354 355 // Per IPMI 2.0 spec, the input and output buffer size must be the max 356 // buffer size minus one byte to allocate space for the length byte. 357 uint8_t str[] = {0x01, MAX_IPMI_BUFFER-1, MAX_IPMI_BUFFER-1, 0x0A, 0x01}; 358 359 // Data length 360 *data_len = sizeof(str); 361 362 // Pack the actual response 363 memcpy(response, &str, *data_len); 364 365 return rc; 366 } 367 368 ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 369 ipmi_request_t request, ipmi_response_t response, 370 ipmi_data_len_t data_len, ipmi_context_t context) 371 { 372 printf("Handling WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd); 373 374 // Status code. 375 ipmi_ret_t rc = IPMI_CC_INVALID; 376 377 *data_len = strlen("THIS IS WILDCARD"); 378 379 // Now pack actual response 380 memcpy(response, "THIS IS WILDCARD", *data_len); 381 382 return rc; 383 } 384 385 void register_netfn_app_functions() 386 { 387 // <Get BT Interface Capabilities> 388 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CAP_BIT); 389 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, ipmi_app_get_bt_capabilities, 390 PRIVILEGE_USER); 391 392 // <Wildcard Command> 393 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WILDCARD); 394 ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, ipmi_app_wildcard_handler, 395 PRIVILEGE_USER); 396 397 // <Reset Watchdog Timer> 398 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_RESET_WD); 399 ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, ipmi_app_reset_watchdog, 400 PRIVILEGE_OPERATOR); 401 402 // <Set Watchdog Timer> 403 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_WD); 404 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, ipmi_app_set_watchdog, 405 PRIVILEGE_OPERATOR); 406 407 // <Get Device ID> 408 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_ID); 409 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, ipmi_app_get_device_id, 410 PRIVILEGE_USER); 411 412 // <Get Self Test Results> 413 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS); 414 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL, 415 ipmi_app_get_self_test_results, PRIVILEGE_USER); 416 417 // <Get Device GUID> 418 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID); 419 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, ipmi_app_get_device_guid, 420 PRIVILEGE_USER); 421 422 // <Set ACPI Power State> 423 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_SET_ACPI); 424 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, ipmi_app_set_acpi_power_state, 425 PRIVILEGE_ADMIN); 426 427 // <Set Channel Access> 428 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, 429 IPMI_CMD_SET_CHAN_ACCESS); 430 ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL, 431 ipmi_set_channel_access, PRIVILEGE_ADMIN); 432 433 // <Get Channel Access> 434 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHANNEL_ACCESS); 435 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHANNEL_ACCESS, NULL, 436 ipmi_get_channel_access, PRIVILEGE_USER); 437 438 // <Get Channel Info Command> 439 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO); 440 ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info, 441 PRIVILEGE_USER); 442 443 return; 444 } 445 446 447