Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 1 | #include <cstdio> |
| 2 | #include <string> |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 3 | #include <arpa/inet.h> |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 4 | #include <systemd/sd-bus.h> |
| 5 | #include <mapper.h> |
| 6 | #include <chrono> |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame^] | 7 | #include "selutility.hpp" |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 8 | #include "storagehandler.h" |
| 9 | #include "storageaddsel.h" |
Patrick Williams | 37af733 | 2016-09-02 21:21:42 -0500 | [diff] [blame] | 10 | #include "host-ipmid/ipmid-api.h" |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame^] | 11 | #include <phosphor-logging/log.hpp> |
| 12 | #include <sdbusplus/server.hpp> |
| 13 | #include "xyz/openbmc_project/Common/error.hpp" |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 14 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 15 | void register_netfn_storage_functions() __attribute__((constructor)); |
| 16 | |
| 17 | |
| 18 | unsigned int g_sel_time = 0xFFFFFFFF; |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 19 | extern unsigned short g_sel_reserve; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 20 | |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 21 | constexpr auto time_manager_intf = "org.openbmc.TimeManager"; |
| 22 | constexpr auto time_manager_obj = "/org/openbmc/TimeManager"; |
| 23 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame^] | 24 | namespace cache |
| 25 | { |
| 26 | /* |
| 27 | * This cache contains the object paths of the logging entries sorted in the |
| 28 | * order of the filename(numeric order). The cache is initialized by |
| 29 | * invoking readLoggingObjectPaths with the cache as the parameter. The |
| 30 | * cache is invoked in the execution of the Get SEL info and Delete SEL |
| 31 | * entry command. The Get SEL Info command is typically invoked before the |
| 32 | * Get SEL entry command, so the cache is utilized for responding to Get SEL |
| 33 | * entry command. The cache is invalidated by clearing after Delete SEL |
| 34 | * entry and Clear SEL command. |
| 35 | */ |
| 36 | ipmi::sel::ObjectPaths paths; |
| 37 | |
| 38 | } // namespace objectPathsCache |
| 39 | |
| 40 | using InternalFailure = |
| 41 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 42 | using namespace phosphor::logging; |
| 43 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 44 | ipmi_ret_t ipmi_storage_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 45 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 46 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 47 | { |
| 48 | printf("Handling STORAGE WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd); |
| 49 | // Status code. |
Nan Li | 70aa8d9 | 2016-08-29 00:11:10 +0800 | [diff] [blame] | 50 | ipmi_ret_t rc = IPMI_CC_INVALID; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 51 | *data_len = 0; |
| 52 | return rc; |
| 53 | } |
| 54 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame^] | 55 | ipmi_ret_t getSELInfo(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 56 | ipmi_request_t request, ipmi_response_t response, |
| 57 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 58 | { |
| 59 | std::vector<uint8_t> outPayload(sizeof(ipmi::sel::GetSELInfoResponse)); |
| 60 | auto responseData = reinterpret_cast<ipmi::sel::GetSELInfoResponse*> |
| 61 | (outPayload.data()); |
| 62 | |
| 63 | responseData->selVersion = ipmi::sel::selVersion; |
| 64 | // Last erase timestamp is not available from log manager. |
| 65 | responseData->eraseTimeStamp = ipmi::sel::invalidTimeStamp; |
| 66 | responseData->operationSupport = ipmi::sel::operationSupport; |
| 67 | |
| 68 | ipmi::sel::readLoggingObjectPaths(cache::paths); |
| 69 | responseData->entries = 0; |
| 70 | responseData->addTimeStamp = ipmi::sel::invalidTimeStamp; |
| 71 | |
| 72 | if (!cache::paths.empty()) |
| 73 | { |
| 74 | responseData->entries = static_cast<uint16_t>(cache::paths.size()); |
| 75 | |
| 76 | try |
| 77 | { |
| 78 | responseData->addTimeStamp = static_cast<uint32_t>( |
| 79 | (ipmi::sel::getEntryTimeStamp(cache::paths.back()) |
| 80 | .count())); |
| 81 | } |
| 82 | catch (InternalFailure& e) |
| 83 | { |
| 84 | } |
| 85 | catch (const std::runtime_error& e) |
| 86 | { |
| 87 | log<level::ERR>(e.what()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | memcpy(response, outPayload.data(), outPayload.size()); |
| 92 | *data_len = outPayload.size(); |
| 93 | |
| 94 | return IPMI_CC_OK; |
| 95 | } |
| 96 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 97 | ipmi_ret_t ipmi_storage_get_sel_time(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 98 | ipmi_request_t request, ipmi_response_t response, |
| 99 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 100 | { |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 101 | using namespace std::chrono; |
| 102 | |
| 103 | char *time_provider = nullptr; |
| 104 | const char* time_in_str = nullptr; |
| 105 | uint64_t host_time_usec = 0; |
| 106 | uint32_t resp = 0; |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 107 | ipmi_ret_t rc = IPMI_CC_OK; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 108 | |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 109 | sd_bus_message *reply = nullptr; |
| 110 | sd_bus_error bus_error = SD_BUS_ERROR_NULL; |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 111 | |
| 112 | printf("IPMI Handling GET-SEL-TIME\n"); |
| 113 | |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 114 | auto bus = ipmid_get_sd_bus_connection(); |
| 115 | |
| 116 | auto rct = mapper_get_service(bus, time_manager_obj, &time_provider); |
| 117 | if (rct < 0) { |
| 118 | printf("Error [%s] getting bus name for time provider\n", |
| 119 | strerror(-rct)); |
| 120 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 121 | goto finish; |
| 122 | } |
| 123 | |
| 124 | rct = sd_bus_call_method(bus, |
| 125 | time_provider, |
| 126 | time_manager_obj, |
| 127 | time_manager_intf, |
| 128 | "GetTime", |
| 129 | &bus_error, |
| 130 | &reply, |
| 131 | "s", |
| 132 | "host"); |
| 133 | if (rct < 0) { |
| 134 | printf("Error [%s] getting time\n", strerror(-rct)); |
| 135 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 136 | goto finish; |
| 137 | } |
| 138 | |
| 139 | rct = sd_bus_message_read(reply, "sx", &time_in_str, &host_time_usec); |
| 140 | if (rct < 0) { |
| 141 | fprintf(stderr, "Error [%s] parsing get-time response\n", |
| 142 | strerror(-rct)); |
| 143 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 144 | goto finish; |
| 145 | } |
| 146 | |
| 147 | // Time is really long int but IPMI wants just uint32. This works okay until |
| 148 | // the number of seconds since 1970 overflows uint32 size.. Still a whole |
| 149 | // lot of time here to even think about that. |
| 150 | resp = duration_cast<seconds>(microseconds(host_time_usec)).count(); |
| 151 | resp = htole32(resp); |
| 152 | printf("Host Time read:[%s] :: [%d]\n", time_in_str, resp); |
| 153 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 154 | // From the IPMI Spec 2.0, response should be a 32-bit value |
| 155 | *data_len = sizeof(resp); |
| 156 | |
| 157 | // Pack the actual response |
| 158 | memcpy(response, &resp, *data_len); |
| 159 | |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 160 | finish: |
| 161 | sd_bus_error_free(&bus_error); |
| 162 | reply = sd_bus_message_unref(reply); |
| 163 | free(time_provider); |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 164 | return rc; |
| 165 | } |
| 166 | |
| 167 | ipmi_ret_t ipmi_storage_set_sel_time(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 168 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 169 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 170 | { |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 171 | char *time_provider = nullptr; |
| 172 | int time_rc = 0; |
| 173 | ipmi_ret_t rc = IPMI_CC_OK; |
| 174 | |
| 175 | sd_bus_message *reply = nullptr; |
| 176 | sd_bus_error bus_error = SD_BUS_ERROR_NULL; |
| 177 | |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 178 | uint32_t* secs = (uint32_t*)request; |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 179 | *data_len = 0; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 180 | |
| 181 | printf("Handling Set-SEL-Time:[0x%X], Cmd:[0x%X]\n",netfn, cmd); |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 182 | printf("Data: 0x%X]\n",*secs); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 183 | |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 184 | auto bus = ipmid_get_sd_bus_connection(); |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 185 | |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 186 | auto rct = mapper_get_service(bus, time_manager_obj, &time_provider); |
| 187 | if (rct < 0) { |
| 188 | printf("Error [%s] getting bus name for time provider\n", |
| 189 | strerror(-rct)); |
| 190 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 191 | goto finish; |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 192 | } |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 193 | |
| 194 | rct = sd_bus_call_method(bus, |
| 195 | time_provider, |
| 196 | time_manager_obj, |
| 197 | time_manager_intf, |
| 198 | "SetTime", |
| 199 | &bus_error, |
| 200 | &reply, |
| 201 | "ss", |
| 202 | "host", |
| 203 | std::to_string(le32toh(*secs)).c_str()); |
| 204 | |
| 205 | if (rct < 0) { |
| 206 | printf("Error [%s] setting time\n", strerror(-rct)); |
| 207 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 208 | goto finish; |
| 209 | } |
| 210 | |
| 211 | rct = sd_bus_message_read(reply, "i", &time_rc); |
| 212 | if (rct < 0) { |
| 213 | fprintf(stderr, "Error [%s] parsing set-time response\n", |
| 214 | strerror(-rct)); |
| 215 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 216 | goto finish; |
| 217 | } |
| 218 | |
| 219 | if (time_rc < 0) { |
| 220 | printf("Error setting time."); |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 221 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 222 | } |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 223 | |
| 224 | finish: |
| 225 | sd_bus_error_free(&bus_error); |
| 226 | reply = sd_bus_message_unref(reply); |
| 227 | free(time_provider); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 228 | return rc; |
| 229 | } |
| 230 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 231 | ipmi_ret_t ipmi_storage_reserve_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 232 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 233 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 234 | { |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 235 | ipmi_ret_t rc = IPMI_CC_OK; |
| 236 | |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 237 | // IPMI spec, Reservation ID, the value simply increases against each execution of reserve_sel command. |
| 238 | if( ++g_sel_reserve == 0) |
| 239 | g_sel_reserve = 1; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 240 | |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 241 | printf("IPMI Handling RESERVE-SEL 0x%04x\n", g_sel_reserve); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 242 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 243 | *data_len = sizeof(g_sel_reserve); |
| 244 | |
| 245 | // Pack the actual response |
| 246 | memcpy(response, &g_sel_reserve, *data_len); |
| 247 | |
| 248 | return rc; |
| 249 | } |
| 250 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 251 | ipmi_ret_t ipmi_storage_add_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 252 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 253 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 254 | { |
| 255 | |
| 256 | ipmi_ret_t rc = IPMI_CC_OK; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 257 | ipmi_add_sel_request_t *p = (ipmi_add_sel_request_t*) request; |
| 258 | uint16_t recordid; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 259 | |
Chris Austen | 313d95b | 2015-10-31 12:55:30 -0500 | [diff] [blame] | 260 | recordid = ((uint16_t)p->eventdata[1] << 8) | p->eventdata[2]; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 261 | |
| 262 | printf("IPMI Handling ADD-SEL for record 0x%04x\n", recordid); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 263 | |
| 264 | *data_len = sizeof(g_sel_reserve); |
| 265 | |
| 266 | // Pack the actual response |
Chris Austen | 7cc3332 | 2015-11-11 00:20:22 -0600 | [diff] [blame] | 267 | memcpy(response, &p->eventdata[1], 2); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 268 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 269 | send_esel(recordid); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 270 | |
| 271 | return rc; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | |
| 276 | void register_netfn_storage_functions() |
| 277 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 278 | // <Wildcard Command> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 279 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_WILDCARD); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 280 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WILDCARD, NULL, ipmi_storage_wildcard, |
| 281 | PRIVILEGE_USER); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 282 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame^] | 283 | // <Get SEL Info> |
| 284 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_GET_SEL_INFO); |
| 285 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_INFO, NULL, getSELInfo, |
| 286 | PRIVILEGE_USER); |
| 287 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 288 | // <Get SEL Time> |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 289 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_GET_SEL_TIME); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 290 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_TIME, NULL, ipmi_storage_get_sel_time, |
| 291 | PRIVILEGE_USER); |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 292 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 293 | // <Set SEL Time> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 294 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_SET_SEL_TIME); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 295 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_SET_SEL_TIME, NULL, ipmi_storage_set_sel_time, |
| 296 | PRIVILEGE_OPERATOR); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 297 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 298 | // <Reserve SEL> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 299 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_RESERVE_SEL); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 300 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_RESERVE_SEL, NULL, ipmi_storage_reserve_sel, |
| 301 | PRIVILEGE_USER); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 302 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 303 | // <Add SEL Entry> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 304 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_ADD_SEL); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 305 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_ADD_SEL, NULL, ipmi_storage_add_sel, |
| 306 | PRIVILEGE_OPERATOR); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 307 | return; |
| 308 | } |
| 309 | |