Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 1 | #include "chassishandler.h" |
Patrick Williams | 37af733 | 2016-09-02 21:21:42 -0500 | [diff] [blame] | 2 | #include "host-ipmid/ipmid-api.h" |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 3 | #include <stdio.h> |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 4 | #include <stdlib.h> |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 5 | #include <stdint.h> |
Brad Bishop | 3551868 | 2016-07-22 08:35:41 -0400 | [diff] [blame] | 6 | #include <mapper.h> |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | #include <netinet/in.h> |
| 9 | #include <limits.h> |
| 10 | #include <string.h> |
| 11 | #include <endian.h> |
| 12 | #include <sstream> |
| 13 | #include <array> |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 14 | #include <phosphor-logging/log.hpp> |
| 15 | #include <xyz/openbmc_project/State/Host/server.hpp> |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 16 | |
| 17 | //Defines |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 18 | #define SET_PARM_VERSION 0x01 |
| 19 | #define SET_PARM_BOOT_FLAGS_PERMANENT 0x40 //boot flags data1 7th bit on |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 20 | #define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME 0x80 //boot flags data1 8th bit on |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 21 | #define SET_PARM_BOOT_FLAGS_VALID_PERMANENT 0xC0 //boot flags data1 7 & 8 bit on |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 22 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 23 | constexpr size_t SIZE_MAC = 18; |
| 24 | constexpr size_t SIZE_BOOT_OPTION = (uint8_t)BootOptionResponseSize:: |
| 25 | OPAL_NETWORK_SETTINGS;//Maximum size of the boot option parametrs |
| 26 | constexpr size_t SIZE_PREFIX = 7; |
| 27 | constexpr size_t MAX_PREFIX_VALUE = 32; |
| 28 | constexpr size_t SIZE_COOKIE = 4; |
| 29 | constexpr size_t SIZE_VERSION = 2; |
| 30 | constexpr auto MAC_ADDRESS_FORMAT = "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"; |
| 31 | constexpr auto IP_ADDRESS_FORMAT = "%d.%d.%d.%d"; |
Matthew Barth | 8b47005 | 2016-09-21 10:02:57 -0500 | [diff] [blame] | 32 | constexpr auto PREFIX_FORMAT = "%hhd"; |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 33 | constexpr auto ADDR_TYPE_FORMAT = "%hhx"; |
| 34 | //PetiBoot-Specific |
| 35 | static constexpr uint8_t net_conf_initial_bytes[] = {0x80,0x21, 0x70 ,0x62 ,0x21, |
| 36 | 0x00 ,0x01 ,0x06 ,0x04}; |
| 37 | |
| 38 | static constexpr size_t COOKIE_OFFSET = 1; |
| 39 | static constexpr size_t VERSION_OFFSET = 5; |
| 40 | static constexpr size_t MAC_OFFSET = 9; |
| 41 | static constexpr size_t ADDRTYPE_OFFSET = 16; |
| 42 | static constexpr size_t IPADDR_OFFSET = 17; |
| 43 | static constexpr size_t PREFIX_OFFSET = 21; |
| 44 | static constexpr size_t GATEWAY_OFFSET = 22; |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 45 | |
| 46 | |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 47 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 48 | |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 49 | void register_netfn_chassis_functions() __attribute__((constructor)); |
| 50 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 51 | // Host settings in dbus |
| 52 | // Service name should be referenced by connection name got via object mapper |
| 53 | const char *settings_object_name = "/org/openbmc/settings/host0"; |
| 54 | const char *settings_intf_name = "org.freedesktop.DBus.Properties"; |
| 55 | const char *host_intf_name = "org.openbmc.settings.Host"; |
| 56 | |
Nan Li | 8d15fb4 | 2016-08-16 22:29:40 +0800 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
| 59 | uint8_t cap_flags; |
| 60 | uint8_t fru_info_dev_addr; |
| 61 | uint8_t sdr_dev_addr; |
| 62 | uint8_t sel_dev_addr; |
| 63 | uint8_t system_management_dev_addr; |
| 64 | uint8_t bridge_dev_addr; |
| 65 | }__attribute__((packed)) ipmi_chassis_cap_t; |
| 66 | |
Nan Li | fdd8ec5 | 2016-08-28 03:57:40 +0800 | [diff] [blame] | 67 | typedef struct |
| 68 | { |
| 69 | uint8_t cur_power_state; |
| 70 | uint8_t last_power_event; |
| 71 | uint8_t misc_power_state; |
| 72 | uint8_t front_panel_button_cap_status; |
| 73 | }__attribute__((packed)) ipmi_get_chassis_status_t; |
| 74 | |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 75 | // Phosphor Host State manager |
| 76 | namespace State = sdbusplus::xyz::openbmc_project::State::server; |
| 77 | |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 78 | int dbus_get_property(const char *name, char **buf) |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 79 | { |
| 80 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 81 | sd_bus_message *m = NULL; |
| 82 | sd_bus *bus = NULL; |
| 83 | char *temp_buf = NULL; |
| 84 | char *connection = NULL; |
| 85 | int r; |
| 86 | |
Brad Bishop | 3551868 | 2016-07-22 08:35:41 -0400 | [diff] [blame] | 87 | // Get the system bus where most system services are provided. |
| 88 | bus = ipmid_get_sd_bus_connection(); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 89 | |
Brad Bishop | 3551868 | 2016-07-22 08:35:41 -0400 | [diff] [blame] | 90 | r = mapper_get_service(bus, settings_object_name, &connection); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 91 | if (r < 0) { |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 92 | fprintf(stderr, "Failed to get %s connection: %s\n", |
| 93 | settings_object_name, strerror(-r)); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 94 | goto finish; |
| 95 | } |
| 96 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 97 | /* |
| 98 | * Bus, service, object path, interface and method are provided to call |
| 99 | * the method. |
| 100 | * Signatures and input arguments are provided by the arguments at the |
| 101 | * end. |
| 102 | */ |
| 103 | r = sd_bus_call_method(bus, |
| 104 | connection, /* service to contact */ |
| 105 | settings_object_name, /* object path */ |
| 106 | settings_intf_name, /* interface name */ |
| 107 | "Get", /* method name */ |
| 108 | &error, /* object to return error in */ |
| 109 | &m, /* return message on success */ |
| 110 | "ss", /* input signature */ |
| 111 | host_intf_name, /* first argument */ |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 112 | name); /* second argument */ |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 113 | |
| 114 | if (r < 0) { |
| 115 | fprintf(stderr, "Failed to issue method call: %s\n", error.message); |
| 116 | goto finish; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * The output should be parsed exactly the same as the output formatting |
| 121 | * specified. |
| 122 | */ |
| 123 | r = sd_bus_message_read(m, "v", "s", &temp_buf); |
| 124 | if (r < 0) { |
| 125 | fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r)); |
| 126 | goto finish; |
| 127 | } |
| 128 | |
Matthew Barth | 5618105 | 2017-01-23 09:36:29 -0600 | [diff] [blame] | 129 | *buf = strdup(temp_buf); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 130 | /* *buf = (char*) malloc(strlen(temp_buf)); |
| 131 | if (*buf) { |
| 132 | strcpy(*buf, temp_buf); |
| 133 | } |
| 134 | */ |
| 135 | printf("IPMID boot option property get: {%s}.\n", (char *) temp_buf); |
| 136 | |
| 137 | finish: |
| 138 | sd_bus_error_free(&error); |
| 139 | sd_bus_message_unref(m); |
| 140 | free(connection); |
| 141 | |
| 142 | return r; |
| 143 | } |
| 144 | |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 145 | int dbus_set_property(const char * name, const char *value) |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 146 | { |
| 147 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 148 | sd_bus_message *m = NULL; |
| 149 | sd_bus *bus = NULL; |
| 150 | char *connection = NULL; |
| 151 | int r; |
| 152 | |
Brad Bishop | 3551868 | 2016-07-22 08:35:41 -0400 | [diff] [blame] | 153 | // Get the system bus where most system services are provided. |
| 154 | bus = ipmid_get_sd_bus_connection(); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 155 | |
Brad Bishop | 3551868 | 2016-07-22 08:35:41 -0400 | [diff] [blame] | 156 | r = mapper_get_service(bus, settings_object_name, &connection); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 157 | if (r < 0) { |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 158 | fprintf(stderr, "Failed to get %s connection: %s\n", |
| 159 | settings_object_name, strerror(-r)); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 160 | goto finish; |
| 161 | } |
| 162 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 163 | /* |
| 164 | * Bus, service, object path, interface and method are provided to call |
| 165 | * the method. |
| 166 | * Signatures and input arguments are provided by the arguments at the |
| 167 | * end. |
| 168 | */ |
| 169 | r = sd_bus_call_method(bus, |
| 170 | connection, /* service to contact */ |
| 171 | settings_object_name, /* object path */ |
| 172 | settings_intf_name, /* interface name */ |
| 173 | "Set", /* method name */ |
| 174 | &error, /* object to return error in */ |
| 175 | &m, /* return message on success */ |
| 176 | "ssv", /* input signature */ |
| 177 | host_intf_name, /* first argument */ |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 178 | name, /* second argument */ |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 179 | "s", /* third argument */ |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 180 | value); /* fourth argument */ |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 181 | |
| 182 | if (r < 0) { |
| 183 | fprintf(stderr, "Failed to issue method call: %s\n", error.message); |
| 184 | goto finish; |
| 185 | } |
| 186 | |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 187 | printf("IPMID boot option property set: {%s}.\n", value); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 188 | |
| 189 | finish: |
| 190 | sd_bus_error_free(&error); |
| 191 | sd_bus_message_unref(m); |
| 192 | free(connection); |
| 193 | |
| 194 | return r; |
| 195 | } |
| 196 | |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 197 | struct get_sys_boot_options_t { |
| 198 | uint8_t parameter; |
| 199 | uint8_t set; |
| 200 | uint8_t block; |
| 201 | } __attribute__ ((packed)); |
| 202 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 203 | struct get_sys_boot_options_response_t { |
| 204 | uint8_t version; |
| 205 | uint8_t parm; |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 206 | uint8_t data[SIZE_BOOT_OPTION]; |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 207 | } __attribute__ ((packed)); |
| 208 | |
| 209 | struct set_sys_boot_options_t { |
| 210 | uint8_t parameter; |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 211 | uint8_t data[SIZE_BOOT_OPTION]; |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 212 | } __attribute__ ((packed)); |
| 213 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 214 | struct host_network_config_t { |
| 215 | std::string ipaddress; |
| 216 | std::string prefix; |
| 217 | std::string gateway; |
| 218 | std::string macaddress; |
| 219 | std::string addrType; |
| 220 | |
| 221 | host_network_config_t()=default; |
| 222 | }; |
| 223 | |
| 224 | void fillNetworkConfig( host_network_config_t & host_config , |
| 225 | const std::string& conf_str ) { |
| 226 | |
| 227 | constexpr auto COMMA_DELIMITER = ","; |
| 228 | constexpr auto EQUAL_DELIMITER = "="; |
| 229 | size_t commaDelimtrPos = 0; |
| 230 | size_t equalDelimtrPos = 0,commaDelimtrPrevPos = 0; |
| 231 | std::string value; |
| 232 | while ( commaDelimtrPos < conf_str.length() ) { |
| 233 | |
| 234 | commaDelimtrPos = conf_str.find(COMMA_DELIMITER,commaDelimtrPos); |
| 235 | //This condition is to extract the last |
| 236 | //Substring as we will not be having the delimeter |
| 237 | //at end. std::string::npos is -1 |
| 238 | |
| 239 | if ( commaDelimtrPos == std::string::npos ) { |
| 240 | commaDelimtrPos = conf_str.length(); |
| 241 | } |
| 242 | |
| 243 | equalDelimtrPos = conf_str.find (EQUAL_DELIMITER,commaDelimtrPrevPos); |
| 244 | |
| 245 | //foo,ipaddress=1234 |
| 246 | if ( equalDelimtrPos == std::string::npos ) { |
| 247 | |
| 248 | commaDelimtrPos++; |
| 249 | commaDelimtrPrevPos= commaDelimtrPos; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | value = conf_str.substr((equalDelimtrPos+1), |
| 254 | commaDelimtrPos-(equalDelimtrPos+1)); |
| 255 | |
| 256 | #ifdef _IPMI_DEBUG_ |
| 257 | printf ("Name=[%s],Value=[%s],commaDelimtrPos=[%d],\ |
| 258 | commaDelimtrPrevPos=[%d],equalDelimtrPos=[%d]\n", |
| 259 | name.c_str(),value.c_str(),commaDelimtrPos, |
| 260 | commaDelimtrPrevPos,equalDelimtrPos); |
| 261 | #endif |
| 262 | |
| 263 | if ( 0 == conf_str.compare(commaDelimtrPrevPos, |
| 264 | equalDelimtrPos-commaDelimtrPrevPos,"ipaddress" )) { |
| 265 | |
| 266 | host_config.ipaddress = std::move(value); |
| 267 | } |
| 268 | else if ( 0 == conf_str.compare(commaDelimtrPrevPos, |
| 269 | equalDelimtrPos-commaDelimtrPrevPos,"prefix" )) { |
| 270 | |
| 271 | host_config.prefix = std::move(value); |
| 272 | } |
| 273 | else if ( 0 == conf_str.compare(commaDelimtrPrevPos, |
| 274 | equalDelimtrPos-commaDelimtrPrevPos, "gateway" )) { |
| 275 | host_config.gateway = std::move(value); |
| 276 | } |
| 277 | else if ( 0 == conf_str.compare(commaDelimtrPrevPos, |
| 278 | equalDelimtrPos-commaDelimtrPrevPos, "mac" )) { |
| 279 | host_config.macaddress = std::move(value); |
| 280 | } |
| 281 | else if ( 0 == conf_str.compare(commaDelimtrPrevPos, |
| 282 | equalDelimtrPos-commaDelimtrPrevPos, "addr_type" )) { |
| 283 | host_config.addrType = std::move(value); |
| 284 | } |
| 285 | |
| 286 | commaDelimtrPos++; |
| 287 | commaDelimtrPrevPos= commaDelimtrPos; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | int getHostNetworkData(get_sys_boot_options_response_t* respptr) |
| 292 | { |
| 293 | |
| 294 | char *prop = nullptr; |
| 295 | int rc = dbus_get_property("network_config",&prop); |
| 296 | |
| 297 | if ( rc < 0 ) { |
| 298 | fprintf(stderr, "Dbus get property(boot_flags) failed\ |
| 299 | for get_sys_boot_options.\n"); |
| 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | std::string conf_str(prop); |
| 304 | |
| 305 | if ( prop ) { |
| 306 | |
| 307 | free(prop); |
| 308 | prop = nullptr; |
| 309 | } |
| 310 | |
| 311 | /* network_config property Value would be in the form of |
| 312 | * ipaddress=1.1.1.1,prefix=16,gateway=2.2.2.2,mac=11:22:33:44:55:66,dhcp=0 |
| 313 | */ |
| 314 | |
| 315 | /* Parsing the string and fill the hostconfig structure with the |
| 316 | * values */ |
| 317 | |
| 318 | printf ("Configuration String[%s]\n ",conf_str.c_str()); |
| 319 | |
| 320 | host_network_config_t host_config; |
| 321 | |
| 322 | // Fill the host_config from the configuration string |
| 323 | fillNetworkConfig(host_config,conf_str); |
| 324 | |
| 325 | //Assigning the index as intialByteLength as it is fixed and prefilled. |
| 326 | printf ("host_config.macaddress.c_str()=[%s]\n",host_config.macaddress.c_str()); |
| 327 | do{ |
| 328 | |
| 329 | rc = sscanf(host_config.macaddress.c_str(),MAC_ADDRESS_FORMAT, |
| 330 | (respptr->data+MAC_OFFSET), (respptr->data+MAC_OFFSET+1), |
| 331 | (respptr->data+MAC_OFFSET+2),(respptr->data+MAC_OFFSET+3), |
| 332 | (respptr->data+MAC_OFFSET+4), (respptr->data+MAC_OFFSET+5)); |
| 333 | |
| 334 | |
| 335 | if ( rc < 6 ){ |
| 336 | fprintf(stderr, "sscanf Failed in extracting mac address.\n"); |
| 337 | rc = -1; |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | //Conevrt the dhcp,ipaddress,mask and gateway as hex number |
| 342 | respptr->data[MAC_OFFSET+6]=0x00; |
| 343 | |
| 344 | rc = sscanf(host_config.addrType.c_str(),ADDR_TYPE_FORMAT, |
| 345 | (respptr->data+ADDRTYPE_OFFSET)); |
| 346 | |
| 347 | if ( rc <= 0 ) { |
| 348 | fprintf(stderr, "sscanf Failed in extracting address type.\n"); |
| 349 | rc = -1; |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | //ipaddress and gateway would be in IPv4 format |
| 354 | rc = inet_pton(AF_INET,host_config.ipaddress.c_str(), |
| 355 | (respptr->data+IPADDR_OFFSET)); |
| 356 | |
| 357 | if ( rc <= 0 ) { |
| 358 | fprintf(stderr, "inet_pton failed during ipaddress coneversion\n"); |
| 359 | rc = -1; |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | rc = sscanf(host_config.prefix.c_str(),PREFIX_FORMAT, |
| 364 | (respptr->data+PREFIX_OFFSET)); |
| 365 | |
| 366 | if ( rc <= 0 ) { |
| 367 | fprintf(stderr, "sscanf failed during prefix extraction.\n"); |
| 368 | rc = -1; |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | rc = inet_pton(AF_INET,host_config.gateway.c_str(), |
| 373 | (respptr->data+GATEWAY_OFFSET)); |
| 374 | |
| 375 | if ( rc <= 0 ) { |
| 376 | fprintf(stderr, "inet_pton failed during gateway conversion.\n"); |
| 377 | rc = -1; |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | }while (0); |
| 382 | |
| 383 | if ( rc ) { |
| 384 | |
| 385 | //PetiBoot-Specific |
| 386 | //If sucess then copy the first 9 bytes to the data |
| 387 | //else set the respptr to 0 |
| 388 | |
| 389 | memcpy(respptr->data,net_conf_initial_bytes, |
| 390 | sizeof(net_conf_initial_bytes)); |
| 391 | |
| 392 | #ifdef _IPMI_DEBUG_ |
| 393 | printf ("\n===Printing the IPMI Formatted Data========\n"); |
| 394 | |
| 395 | for ( uint8_t pos = 0; pos<index; pos++ ) |
| 396 | printf("%02x ", respptr->data[pos]); |
| 397 | #endif |
| 398 | |
| 399 | }else { |
| 400 | |
| 401 | memset(respptr->data,0,SIZE_BOOT_OPTION); |
| 402 | } |
| 403 | |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | int setHostNetworkData(set_sys_boot_options_t * reqptr) |
| 408 | { |
| 409 | std::string host_network_config; |
| 410 | char mac[SIZE_MAC] = {0}; |
| 411 | char ipAddress[INET_ADDRSTRLEN] = {0}; |
| 412 | char gateway[INET_ADDRSTRLEN] = {0}; |
| 413 | char dhcp[SIZE_PREFIX] = {0}; |
| 414 | char prefix[SIZE_PREFIX] = {0}; |
| 415 | int rc = 0; |
| 416 | uint32_t zeroCookie=0; |
| 417 | |
| 418 | //cookie starts from second byte |
| 419 | // version starts from sixth byte |
| 420 | |
| 421 | do { |
| 422 | |
| 423 | // cookie == 0x21 0x70 0x62 0x21 |
| 424 | if ( memcmp(&(reqptr->data[COOKIE_OFFSET]), |
| 425 | (net_conf_initial_bytes+COOKIE_OFFSET), |
| 426 | SIZE_COOKIE) != 0 ) { |
| 427 | //cookie == 0 |
| 428 | if ( memcmp(&(reqptr->data[COOKIE_OFFSET]), |
| 429 | &zeroCookie, |
| 430 | SIZE_COOKIE) == 0 ) { |
| 431 | rc = 0; |
| 432 | break; |
| 433 | } |
| 434 | //Invalid cookie |
| 435 | fprintf(stderr, "Invalid Cookie\n"); |
| 436 | rc = -1; |
| 437 | break; |
| 438 | } |
| 439 | // vesion == 0x00 0x01 |
| 440 | if ( memcmp(&(reqptr->data[VERSION_OFFSET]), |
| 441 | (net_conf_initial_bytes+VERSION_OFFSET), |
| 442 | SIZE_VERSION) != 0 ) { |
| 443 | |
| 444 | fprintf(stderr, "Invalid Version\n"); |
| 445 | rc = -1; |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | snprintf(mac, SIZE_MAC, MAC_ADDRESS_FORMAT, |
| 450 | reqptr->data[MAC_OFFSET], |
| 451 | reqptr->data[MAC_OFFSET+1], |
| 452 | reqptr->data[MAC_OFFSET+2], |
| 453 | reqptr->data[MAC_OFFSET+3], |
| 454 | reqptr->data[MAC_OFFSET+4], |
| 455 | reqptr->data[MAC_OFFSET+5]); |
| 456 | |
| 457 | snprintf(dhcp,SIZE_PREFIX, ADDR_TYPE_FORMAT, reqptr->data[ADDRTYPE_OFFSET]); |
| 458 | //Validating the address type which could be |
| 459 | //either static or dynamic |
| 460 | if( *(reqptr->data+ADDRTYPE_OFFSET) > 1 ) { |
| 461 | |
| 462 | fprintf(stderr, "Invalid Address Type\n"); |
| 463 | rc = -1; |
| 464 | break; |
| 465 | |
| 466 | } |
| 467 | |
| 468 | snprintf(ipAddress, INET_ADDRSTRLEN, IP_ADDRESS_FORMAT, |
| 469 | reqptr->data[IPADDR_OFFSET], reqptr->data[IPADDR_OFFSET+1], |
| 470 | reqptr->data[IPADDR_OFFSET+2], reqptr->data[IPADDR_OFFSET+3]); |
| 471 | |
| 472 | //validating prefix |
| 473 | if ( *(reqptr->data+PREFIX_OFFSET) > (uint8_t)MAX_PREFIX_VALUE ) { |
| 474 | |
| 475 | fprintf(stderr, "Invalid Prefix\n"); |
| 476 | rc = -1; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | snprintf(prefix,SIZE_PREFIX,PREFIX_FORMAT, reqptr->data[PREFIX_OFFSET]); |
| 481 | |
| 482 | snprintf(gateway, INET_ADDRSTRLEN,IP_ADDRESS_FORMAT, |
| 483 | reqptr->data[GATEWAY_OFFSET], reqptr->data[GATEWAY_OFFSET+1], |
| 484 | reqptr->data[GATEWAY_OFFSET+2], reqptr->data[GATEWAY_OFFSET+3]); |
| 485 | |
| 486 | |
| 487 | }while(0); |
| 488 | |
| 489 | if( !rc ) |
| 490 | { |
| 491 | //Cookie == 0 or it is a valid cookie |
| 492 | host_network_config += "ipaddress="+std::string(ipAddress)+",prefix="+ |
| 493 | std::string(prefix)+",gateway="+std::string(gateway)+ |
| 494 | ",mac="+std::string(mac)+",addr_type="+std::string(dhcp); |
| 495 | |
| 496 | printf ("Network configuration changed: %s\n",host_network_config.c_str()); |
| 497 | |
| 498 | rc = dbus_set_property("network_config",host_network_config.c_str()); |
| 499 | |
| 500 | if ( rc < 0 ) { |
| 501 | fprintf(stderr, "Dbus set property(network_config)\ |
| 502 | failed for set_sys_boot_options.\n"); |
| 503 | rc = -1; |
| 504 | } |
| 505 | |
| 506 | } |
| 507 | return rc; |
| 508 | } |
| 509 | |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 510 | ipmi_ret_t ipmi_chassis_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 511 | ipmi_request_t request, ipmi_response_t response, |
| 512 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 513 | { |
| 514 | printf("Handling CHASSIS WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd); |
| 515 | // Status code. |
Nan Li | 70aa8d9 | 2016-08-29 00:11:10 +0800 | [diff] [blame] | 516 | ipmi_ret_t rc = IPMI_CC_INVALID; |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 517 | *data_len = 0; |
| 518 | return rc; |
| 519 | } |
| 520 | |
Nan Li | 8d15fb4 | 2016-08-16 22:29:40 +0800 | [diff] [blame] | 521 | ipmi_ret_t ipmi_get_chassis_cap(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 522 | ipmi_request_t request, ipmi_response_t response, |
| 523 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 524 | { |
| 525 | // sd_bus error |
| 526 | ipmi_ret_t rc = IPMI_CC_OK; |
| 527 | |
| 528 | ipmi_chassis_cap_t chassis_cap{}; |
| 529 | |
| 530 | *data_len = sizeof(ipmi_chassis_cap_t); |
| 531 | |
| 532 | // TODO: need future work. Get those flag from MRW. |
| 533 | |
| 534 | // capabilities flags |
| 535 | // [7..4] - reserved |
| 536 | // [3] – 1b = provides power interlock (IPM 1.5) |
| 537 | // [2] – 1b = provides Diagnostic Interrupt (FP NMI) |
| 538 | // [1] – 1b = provides “Front Panel Lockout” (indicates that the chassis has capabilities |
| 539 | // to lock out external power control and reset button or front panel interfaces |
| 540 | // and/or detect tampering with those interfaces). |
| 541 | // [0] -1b = Chassis provides intrusion (physical security) sensor. |
| 542 | // set to default value 0x0. |
| 543 | chassis_cap.cap_flags = 0x0; |
| 544 | |
| 545 | // Since we do not have a separate SDR Device/SEL Device/ FRU repository. |
| 546 | // The 20h was given as those 5 device addresses. |
| 547 | // Chassis FRU info Device Address |
| 548 | chassis_cap.fru_info_dev_addr = 0x20; |
| 549 | |
| 550 | // Chassis SDR Device Address |
| 551 | chassis_cap.sdr_dev_addr = 0x20; |
| 552 | |
| 553 | // Chassis SEL Device Address |
| 554 | chassis_cap.sel_dev_addr = 0x20; |
| 555 | |
| 556 | // Chassis System Management Device Address |
| 557 | chassis_cap.system_management_dev_addr = 0x20; |
| 558 | |
| 559 | // Chassis Bridge Device Address. |
| 560 | chassis_cap.bridge_dev_addr = 0x20; |
| 561 | |
| 562 | memcpy(response, &chassis_cap, *data_len); |
| 563 | |
| 564 | return rc; |
| 565 | } |
| 566 | |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 567 | //------------------------------------------ |
| 568 | // Calls into Host State Manager Dbus object |
| 569 | //------------------------------------------ |
| 570 | int initiate_state_transition(State::Host::Transition transition) |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 571 | { |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 572 | using namespace phosphor::logging; |
| 573 | |
| 574 | // OpenBMC Host State Manager dbus framework |
| 575 | constexpr auto HOST_STATE_MANAGER_ROOT = "/xyz/openbmc_project/state/host0"; |
| 576 | constexpr auto HOST_STATE_MANAGER_IFACE = "xyz.openbmc_project.State.Host"; |
| 577 | constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 578 | constexpr auto PROPERTY = "RequestedHostTransition"; |
| 579 | |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 580 | // sd_bus error |
| 581 | int rc = 0; |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 582 | char *busname = NULL; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 583 | |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 584 | // SD Bus error report mechanism. |
| 585 | sd_bus_error bus_error = SD_BUS_ERROR_NULL; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 586 | |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 587 | // Gets a hook onto either a SYSTEM or SESSION bus |
| 588 | sd_bus *bus_type = ipmid_get_sd_bus_connection(); |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 589 | rc = mapper_get_service(bus_type, HOST_STATE_MANAGER_ROOT, &busname); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 590 | if (rc < 0) { |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 591 | log<level::ERR>("Failed to get bus name", |
| 592 | entry("ERROR=%s, OBJPATH=%s", |
| 593 | strerror(-rc), HOST_STATE_MANAGER_ROOT)); |
| 594 | return rc; |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 595 | } |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 596 | |
| 597 | // Convert to string equivalent of the passed in transition enum. |
| 598 | auto request = State::convertForMessage(transition); |
| 599 | |
| 600 | rc = sd_bus_call_method(bus_type, // On the system bus |
| 601 | busname, // Service to contact |
| 602 | HOST_STATE_MANAGER_ROOT, // Object path |
| 603 | DBUS_PROPERTY_IFACE, // Interface name |
| 604 | "Set", // Method to be called |
| 605 | &bus_error, // object to return error |
| 606 | nullptr, // Response buffer if any |
| 607 | "ssv", // Takes 3 arguments |
| 608 | HOST_STATE_MANAGER_IFACE, |
| 609 | PROPERTY, |
| 610 | "s", request.c_str()); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 611 | if(rc < 0) |
| 612 | { |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 613 | log<level::ERR>("Failed to initiate transition", |
| 614 | entry("ERROR=%s, REQUEST=%s", |
| 615 | bus_error.message, request.c_str())); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 616 | } |
| 617 | else |
| 618 | { |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 619 | log<level::INFO>("Transition request initiated successfully"); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | sd_bus_error_free(&bus_error); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 623 | free(busname); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 624 | |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 625 | return rc; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 626 | } |
| 627 | |
Nan Li | fdd8ec5 | 2016-08-28 03:57:40 +0800 | [diff] [blame] | 628 | struct hostPowerPolicyTypeMap_t { |
| 629 | uint8_t policyNum; |
| 630 | char policyName[19]; |
| 631 | }; |
| 632 | |
| 633 | hostPowerPolicyTypeMap_t g_hostPowerPolicyTypeMap_t[] = { |
| 634 | |
| 635 | {0x00, "LEAVE_OFF"}, |
| 636 | {0x01, "RESTORE_LAST_STATE"}, |
| 637 | {0x02, "ALWAYS_POWER_ON"}, |
| 638 | {0x03, "UNKNOWN"} |
| 639 | }; |
| 640 | |
| 641 | uint8_t get_host_power_policy(char *p) { |
| 642 | |
| 643 | hostPowerPolicyTypeMap_t *s = g_hostPowerPolicyTypeMap_t; |
| 644 | |
| 645 | while (s->policyNum != 0x03) { |
| 646 | if (!strcmp(s->policyName,p)) |
| 647 | break; |
| 648 | s++; |
| 649 | } |
| 650 | |
| 651 | return s->policyNum; |
| 652 | } |
| 653 | |
| 654 | //---------------------------------------------------------------------- |
| 655 | // Get Chassis Status commands |
| 656 | //---------------------------------------------------------------------- |
| 657 | ipmi_ret_t ipmi_get_chassis_status(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 658 | ipmi_request_t request, ipmi_response_t response, |
| 659 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 660 | { |
| 661 | const char *objname = "/org/openbmc/control/power0"; |
| 662 | const char *intf = "org.openbmc.control.Power"; |
| 663 | |
| 664 | sd_bus *bus = NULL; |
| 665 | sd_bus_message *reply = NULL; |
| 666 | int r = 0; |
| 667 | int pgood = 0; |
| 668 | char *busname = NULL; |
| 669 | ipmi_ret_t rc = IPMI_CC_OK; |
| 670 | ipmi_get_chassis_status_t chassis_status{}; |
| 671 | |
| 672 | char *p = NULL; |
| 673 | uint8_t s = 0; |
| 674 | |
| 675 | // Get the system bus where most system services are provided. |
| 676 | bus = ipmid_get_sd_bus_connection(); |
| 677 | |
| 678 | *data_len = 4; |
| 679 | |
| 680 | r = mapper_get_service(bus, objname, &busname); |
| 681 | if (r < 0) { |
| 682 | fprintf(stderr, "Failed to get bus name, return value: %s.\n", strerror(-r)); |
| 683 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 684 | goto finish; |
| 685 | } |
| 686 | |
| 687 | r = sd_bus_get_property(bus, busname, objname, intf, "pgood", NULL, &reply, "i"); |
| 688 | if (r < 0) { |
| 689 | fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r)); |
| 690 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", |
| 691 | busname, objname, intf); |
| 692 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 693 | goto finish; |
| 694 | } |
| 695 | |
| 696 | r = sd_bus_message_read(reply, "i", &pgood); |
| 697 | if (r < 0) { |
| 698 | fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r)); |
| 699 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 700 | goto finish; |
| 701 | } |
| 702 | |
| 703 | printf("pgood is 0x%02x\n", pgood); |
| 704 | |
| 705 | // Get Power Policy |
| 706 | r = dbus_get_property("power_policy",&p); |
| 707 | |
| 708 | if (r < 0) { |
| 709 | fprintf(stderr, "Dbus get property(power_policy) failed for get_sys_boot_options.\n"); |
| 710 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 711 | } else { |
| 712 | s = get_host_power_policy(p); |
| 713 | } |
| 714 | |
| 715 | if (p) |
| 716 | { |
| 717 | free(p); |
| 718 | p = NULL; |
| 719 | } |
| 720 | |
| 721 | // Current Power State |
| 722 | // [7] reserved |
| 723 | // [6..5] power restore policy |
| 724 | // 00b = chassis stays powered off after AC/mains returns |
| 725 | // 01b = after AC returns, power is restored to the state that was |
| 726 | // in effect when AC/mains was lost. |
| 727 | // 10b = chassis always powers up after AC/mains returns |
| 728 | // 11b = unknow |
| 729 | // Set to 00b, by observing the hardware behavior. |
| 730 | // Do we need to define a dbus property to identify the restore policy? |
| 731 | |
| 732 | // [4] power control fault |
| 733 | // 1b = controller attempted to turn system power on or off, but |
| 734 | // system did not enter desired state. |
| 735 | // Set to 0b, since We don't support it.. |
| 736 | |
| 737 | // [3] power fault |
| 738 | // 1b = fault detected in main power subsystem. |
| 739 | // set to 0b. for we don't support it. |
| 740 | |
| 741 | // [2] 1b = interlock (chassis is presently shut down because a chassis |
| 742 | // panel interlock switch is active). (IPMI 1.5) |
| 743 | // set to 0b, for we don't support it. |
| 744 | |
| 745 | // [1] power overload |
| 746 | // 1b = system shutdown because of power overload condition. |
| 747 | // set to 0b, for we don't support it. |
| 748 | |
| 749 | // [0] power is on |
| 750 | // 1b = system power is on |
| 751 | // 0b = system power is off(soft-off S4/S5, or mechanical off) |
| 752 | |
| 753 | chassis_status.cur_power_state = ((s & 0x3)<<5) | (pgood & 0x1); |
| 754 | |
| 755 | // Last Power Event |
| 756 | // [7..5] – reserved |
| 757 | // [4] – 1b = last ‘Power is on’ state was entered via IPMI command |
| 758 | // [3] – 1b = last power down caused by power fault |
| 759 | // [2] – 1b = last power down caused by a power interlock being activated |
| 760 | // [1] – 1b = last power down caused by a Power overload |
| 761 | // [0] – 1b = AC failed |
| 762 | // set to 0x0, for we don't support these fields. |
| 763 | |
| 764 | chassis_status.last_power_event = 0; |
| 765 | |
| 766 | // Misc. Chassis State |
| 767 | // [7] – reserved |
| 768 | // [6] – 1b = Chassis Identify command and state info supported (Optional) |
| 769 | // 0b = Chassis Identify command support unspecified via this command. |
| 770 | // (The Get Command Support command , if implemented, would still |
| 771 | // indicate support for the Chassis Identify command) |
| 772 | // [5..4] – Chassis Identify State. Mandatory when bit[6] =1b, reserved (return |
| 773 | // as 00b) otherwise. Returns the present chassis identify state. |
| 774 | // Refer to the Chassis Identify command for more info. |
| 775 | // 00b = chassis identify state = Off |
| 776 | // 01b = chassis identify state = Temporary(timed) On |
| 777 | // 10b = chassis identify state = Indefinite On |
| 778 | // 11b = reserved |
| 779 | // [3] – 1b = Cooling/fan fault detected |
| 780 | // [2] – 1b = Drive Fault |
| 781 | // [1] – 1b = Front Panel Lockout active (power off and reset via chassis |
| 782 | // push-buttons disabled.) |
| 783 | // [0] – 1b = Chassis Intrusion active |
| 784 | // set to 0, for we don't support them. |
| 785 | chassis_status.misc_power_state = 0; |
| 786 | |
| 787 | // Front Panel Button Capabilities and disable/enable status(Optional) |
| 788 | // set to 0, for we don't support them. |
| 789 | chassis_status.front_panel_button_cap_status = 0; |
| 790 | |
| 791 | // Pack the actual response |
| 792 | memcpy(response, &chassis_status, *data_len); |
| 793 | |
| 794 | finish: |
| 795 | free(busname); |
| 796 | reply = sd_bus_message_unref(reply); |
| 797 | |
| 798 | return rc; |
| 799 | } |
Chris Austen | 7888c4d | 2015-12-03 15:26:20 -0600 | [diff] [blame] | 800 | |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 801 | //---------------------------------------------------------------------- |
| 802 | // Chassis Control commands |
| 803 | //---------------------------------------------------------------------- |
| 804 | ipmi_ret_t ipmi_chassis_control(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 805 | ipmi_request_t request, ipmi_response_t response, |
| 806 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 807 | { |
| 808 | // Error from power off. |
| 809 | int rc = 0; |
| 810 | |
| 811 | // No response for this command. |
| 812 | *data_len = 0; |
| 813 | |
| 814 | // Catch the actual operaton by peeking into request buffer |
| 815 | uint8_t chassis_ctrl_cmd = *(uint8_t *)request; |
| 816 | printf("Chassis Control Command: Operation:[0x%X]\n",chassis_ctrl_cmd); |
| 817 | |
| 818 | switch(chassis_ctrl_cmd) |
| 819 | { |
Vishwanatha Subbanna | a66239b | 2017-03-02 16:54:32 +0530 | [diff] [blame] | 820 | case CMD_POWER_ON: |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 821 | rc = initiate_state_transition(State::Host::Transition::On); |
Vishwanatha Subbanna | a66239b | 2017-03-02 16:54:32 +0530 | [diff] [blame] | 822 | break; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 823 | case CMD_POWER_OFF: |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 824 | rc = initiate_state_transition(State::Host::Transition::Off); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 825 | break; |
Chris Austen | 7888c4d | 2015-12-03 15:26:20 -0600 | [diff] [blame] | 826 | case CMD_HARD_RESET: |
Vishwanatha Subbanna | bb5190e | 2017-03-01 14:53:29 +0530 | [diff] [blame] | 827 | case CMD_POWER_CYCLE: |
| 828 | // SPEC has a section that says certain implementations can trigger |
| 829 | // PowerOn if power is Off when a command to power cycle is |
| 830 | // requested |
Vishwanatha Subbanna | b12b0c0 | 2017-03-07 18:17:19 +0530 | [diff] [blame] | 831 | rc = initiate_state_transition(State::Host::Transition::Reboot); |
Chris Austen | 7888c4d | 2015-12-03 15:26:20 -0600 | [diff] [blame] | 832 | break; |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 833 | default: |
| 834 | { |
| 835 | fprintf(stderr, "Invalid Chassis Control command:[0x%X] received\n",chassis_ctrl_cmd); |
| 836 | rc = -1; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | return ( (rc < 0) ? IPMI_CC_INVALID : IPMI_CC_OK); |
| 841 | } |
| 842 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 843 | struct bootOptionTypeMap_t { |
| 844 | uint8_t ipmibootflag; |
| 845 | char dbusname[8]; |
| 846 | }; |
| 847 | |
| 848 | #define INVALID_STRING "Invalid" |
| 849 | // dbus supports this list of boot devices. |
| 850 | bootOptionTypeMap_t g_bootOptionTypeMap_t[] = { |
| 851 | |
| 852 | {0x01, "Network"}, |
| 853 | {0x02, "Disk"}, |
| 854 | {0x03, "Safe"}, |
| 855 | {0x05, "CDROM"}, |
| 856 | {0x06, "Setup"}, |
| 857 | {0x00, "Default"}, |
| 858 | {0xFF, INVALID_STRING} |
| 859 | }; |
| 860 | |
| 861 | uint8_t get_ipmi_boot_option(char *p) { |
| 862 | |
| 863 | bootOptionTypeMap_t *s = g_bootOptionTypeMap_t; |
| 864 | |
| 865 | while (s->ipmibootflag != 0xFF) { |
| 866 | if (!strcmp(s->dbusname,p)) |
| 867 | break; |
| 868 | s++; |
| 869 | } |
| 870 | |
| 871 | if (!s->ipmibootflag) |
| 872 | printf("Failed to find Sensor Type %s\n", p); |
| 873 | |
| 874 | return s->ipmibootflag; |
| 875 | } |
| 876 | |
| 877 | char* get_boot_option_by_ipmi(uint8_t p) { |
| 878 | |
| 879 | bootOptionTypeMap_t *s = g_bootOptionTypeMap_t; |
| 880 | |
| 881 | while (s->ipmibootflag != 0xFF) { |
| 882 | |
| 883 | if (s->ipmibootflag == p) |
| 884 | break; |
| 885 | |
| 886 | s++; |
| 887 | } |
| 888 | |
| 889 | |
| 890 | if (!s->ipmibootflag) |
| 891 | printf("Failed to find Sensor Type 0x%x\n", p); |
| 892 | |
| 893 | return s->dbusname; |
| 894 | } |
| 895 | |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 896 | ipmi_ret_t ipmi_chassis_get_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 897 | ipmi_request_t request, ipmi_response_t response, |
| 898 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 899 | { |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 900 | ipmi_ret_t rc = IPMI_CC_PARM_NOT_SUPPORTED; |
| 901 | char *p = NULL; |
| 902 | get_sys_boot_options_response_t *resp = (get_sys_boot_options_response_t *) response; |
| 903 | get_sys_boot_options_t *reqptr = (get_sys_boot_options_t*) request; |
| 904 | uint8_t s; |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 905 | |
| 906 | printf("IPMI GET_SYS_BOOT_OPTIONS\n"); |
| 907 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 908 | memset(resp,0,sizeof(*resp)); |
| 909 | resp->version = SET_PARM_VERSION; |
| 910 | resp->parm = 5; |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 911 | resp->data[0] = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME; |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 912 | |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 913 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 914 | /* |
| 915 | * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc. |
| 916 | * This is the only parameter used by petitboot. |
| 917 | */ |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 918 | if ( reqptr->parameter == static_cast<uint8_t> |
| 919 | ( BootOptionParameter::BOOT_FLAGS )) { |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 920 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 921 | *data_len = static_cast<uint8_t>(BootOptionResponseSize::BOOT_FLAGS); |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 922 | /* Get the boot device */ |
| 923 | int r = dbus_get_property("boot_flags",&p); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 924 | |
| 925 | if (r < 0) { |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 926 | fprintf(stderr, "Dbus get property(boot_flags) failed for get_sys_boot_options.\n"); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 927 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 928 | |
| 929 | } else { |
| 930 | |
| 931 | s = get_ipmi_boot_option(p); |
| 932 | resp->data[1] = (s << 2); |
| 933 | rc = IPMI_CC_OK; |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 934 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 935 | } |
| 936 | |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 937 | if (p) |
| 938 | { |
| 939 | free(p); |
| 940 | p = NULL; |
| 941 | } |
| 942 | |
| 943 | /* Get the boot policy */ |
| 944 | r = dbus_get_property("boot_policy",&p); |
| 945 | |
| 946 | if (r < 0) { |
| 947 | fprintf(stderr, "Dbus get property(boot_policy) failed for get_sys_boot_options.\n"); |
| 948 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 949 | |
| 950 | } else { |
| 951 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 952 | printf("BootPolicy is[%s]", p); |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 953 | resp->data[0] = (strncmp(p,"ONETIME",strlen("ONETIME"))==0) ? |
| 954 | SET_PARM_BOOT_FLAGS_VALID_ONE_TIME: |
| 955 | SET_PARM_BOOT_FLAGS_VALID_PERMANENT; |
| 956 | rc = IPMI_CC_OK; |
| 957 | |
| 958 | } |
| 959 | |
| 960 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 961 | } else if ( reqptr->parameter == static_cast<uint8_t> |
| 962 | ( BootOptionParameter::OPAL_NETWORK_SETTINGS )) { |
| 963 | |
| 964 | *data_len = static_cast<uint8_t>(BootOptionResponseSize::OPAL_NETWORK_SETTINGS); |
| 965 | |
| 966 | resp->parm = static_cast<uint8_t>(BootOptionParameter::OPAL_NETWORK_SETTINGS); |
| 967 | |
| 968 | int ret = getHostNetworkData(resp); |
| 969 | |
| 970 | if (ret < 0) { |
| 971 | |
| 972 | fprintf(stderr, "getHostNetworkData failed for get_sys_boot_options.\n"); |
| 973 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 974 | |
| 975 | }else |
| 976 | rc = IPMI_CC_OK; |
| 977 | } |
| 978 | |
| 979 | else { |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 980 | fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | if (p) |
| 984 | free(p); |
| 985 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 986 | if (rc == IPMI_CC_OK) |
| 987 | { |
| 988 | *data_len += 2; |
| 989 | } |
| 990 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 991 | return rc; |
| 992 | } |
| 993 | |
| 994 | |
| 995 | |
| 996 | ipmi_ret_t ipmi_chassis_set_sys_boot_options(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 997 | ipmi_request_t request, ipmi_response_t response, |
| 998 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 999 | { |
| 1000 | ipmi_ret_t rc = IPMI_CC_OK; |
| 1001 | char *s; |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1002 | set_sys_boot_options_t *reqptr = (set_sys_boot_options_t *) request; |
| 1003 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 1004 | printf("IPMI SET_SYS_BOOT_OPTIONS reqptr->parameter =[%d]\n",reqptr->parameter); |
| 1005 | |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1006 | // This IPMI command does not have any resposne data |
| 1007 | *data_len = 0; |
| 1008 | |
| 1009 | /* 000101 |
| 1010 | * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc. |
| 1011 | * This is the only parameter used by petitboot. |
| 1012 | */ |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 1013 | |
| 1014 | if (reqptr->parameter == (uint8_t)BootOptionParameter::BOOT_FLAGS) { |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1015 | |
| 1016 | s = get_boot_option_by_ipmi(((reqptr->data[1] & 0x3C) >> 2)); |
| 1017 | |
| 1018 | printf("%d: %s\n", __LINE__, s); |
| 1019 | if (!strcmp(s,INVALID_STRING)) { |
| 1020 | |
| 1021 | rc = IPMI_CC_PARM_NOT_SUPPORTED; |
| 1022 | |
| 1023 | } else { |
| 1024 | |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 1025 | int r = dbus_set_property("boot_flags",s); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1026 | |
| 1027 | if (r < 0) { |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 1028 | fprintf(stderr, "Dbus set property(boot_flags) failed for set_sys_boot_options.\n"); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1029 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 1030 | } |
| 1031 | } |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 1032 | |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 1033 | /* setting the boot policy */ |
| 1034 | s = (char *)(((reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) == |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 1035 | SET_PARM_BOOT_FLAGS_PERMANENT) ?"PERMANENT":"ONETIME"); |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 1036 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 1037 | printf ( "\nBoot Policy is %s",s); |
ratagupt | a6f6bff | 2016-04-04 06:20:11 -0500 | [diff] [blame] | 1038 | int r = dbus_set_property("boot_policy",s); |
| 1039 | |
| 1040 | if (r < 0) { |
| 1041 | fprintf(stderr, "Dbus set property(boot_policy) failed for set_sys_boot_options.\n"); |
| 1042 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 1043 | } |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1044 | |
Ratan Gupta | fd28dd7 | 2016-08-01 04:58:01 -0500 | [diff] [blame] | 1045 | } else if (reqptr->parameter == |
| 1046 | (uint8_t)BootOptionParameter::OPAL_NETWORK_SETTINGS) { |
| 1047 | |
| 1048 | int ret = setHostNetworkData(reqptr); |
| 1049 | if (ret < 0) { |
| 1050 | fprintf(stderr, "setHostNetworkData failed for set_sys_boot_options.\n"); |
| 1051 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 1052 | } |
| 1053 | } |
| 1054 | else { |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1055 | fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter); |
| 1056 | rc = IPMI_CC_PARM_NOT_SUPPORTED; |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | return rc; |
| 1060 | } |
| 1061 | |
| 1062 | void register_netfn_chassis_functions() |
| 1063 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1064 | // <Wildcard Command> |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 1065 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_WILDCARD); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1066 | ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_WILDCARD, NULL, ipmi_chassis_wildcard, |
| 1067 | PRIVILEGE_USER); |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 1068 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1069 | // Get Chassis Capabilities |
Nan Li | 8d15fb4 | 2016-08-16 22:29:40 +0800 | [diff] [blame] | 1070 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1071 | ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_CHASSIS_CAP, NULL, ipmi_get_chassis_cap, |
| 1072 | PRIVILEGE_USER); |
Nan Li | 8d15fb4 | 2016-08-16 22:29:40 +0800 | [diff] [blame] | 1073 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1074 | // <Get System Boot Options> |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 1075 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1076 | ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_GET_SYS_BOOT_OPTIONS, NULL, |
| 1077 | ipmi_chassis_get_sys_boot_options, PRIVILEGE_OPERATOR); |
Adriana Kobylak | 40814c6 | 2015-10-27 15:58:44 -0500 | [diff] [blame] | 1078 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1079 | // <Get Chassis Status> |
Nan Li | fdd8ec5 | 2016-08-28 03:57:40 +0800 | [diff] [blame] | 1080 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1081 | ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_STATUS, NULL, ipmi_get_chassis_status, |
| 1082 | PRIVILEGE_USER); |
Nan Li | fdd8ec5 | 2016-08-28 03:57:40 +0800 | [diff] [blame] | 1083 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1084 | // <Chassis Control> |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 1085 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1086 | ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_CHASSIS_CONTROL, NULL, ipmi_chassis_control, |
| 1087 | PRIVILEGE_OPERATOR); |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1088 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1089 | // <Set System Boot Options> |
shgoupf | d84fbbf | 2015-12-17 10:05:51 +0800 | [diff] [blame] | 1090 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1091 | ipmi_register_callback(NETFUN_CHASSIS, IPMI_CMD_SET_SYS_BOOT_OPTIONS, NULL, |
| 1092 | ipmi_chassis_set_sys_boot_options, PRIVILEGE_OPERATOR); |
vishwa | 3699327 | 2015-11-20 12:43:49 -0600 | [diff] [blame] | 1093 | } |