Jaghathiswari Rankappagounder Natarajan | fd0f1cf | 2019-01-18 15:31:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "entity_name.hpp" |
| 18 | |
| 19 | #include "main.hpp" |
| 20 | |
| 21 | #include <cstdint> |
| 22 | #include <cstring> |
| 23 | #include <experimental/filesystem> |
| 24 | #include <fstream> |
| 25 | #include <nlohmann/json.hpp> |
| 26 | #include <phosphor-logging/elog-errors.hpp> |
| 27 | #include <phosphor-logging/log.hpp> |
| 28 | #include <regex> |
| 29 | #include <sstream> |
| 30 | #include <string> |
| 31 | #include <system_error> |
| 32 | #include <unordered_map> |
| 33 | #include <vector> |
| 34 | #include <xyz/openbmc_project/Common/error.hpp> |
| 35 | |
| 36 | namespace google |
| 37 | { |
| 38 | namespace ipmi |
| 39 | { |
| 40 | namespace fs = std::experimental::filesystem; |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | |
| 45 | // TODO (jaghu) : Add a call to get getChannelMaxTransferSize. |
| 46 | #ifndef MAX_IPMI_BUFFER |
| 47 | #define MAX_IPMI_BUFFER 64 |
| 48 | #endif |
| 49 | |
| 50 | using Json = nlohmann::json; |
| 51 | |
| 52 | using namespace phosphor::logging; |
| 53 | using InternalFailure = |
| 54 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 55 | |
| 56 | static constexpr auto configFile = |
| 57 | "/usr/share/ipmi-entity-association/entity_association_map.json"; |
| 58 | |
| 59 | static const std::map<uint8_t, std::string> entityIdToName{ |
| 60 | {0x03, "cpu"}, |
| 61 | {0x04, "storage_device"}, |
| 62 | {0x0B, "add_in_card"}, |
| 63 | {0x20, "memory_module"}}; |
| 64 | |
| 65 | Json parse_config() |
| 66 | { |
| 67 | std::ifstream jsonFile(configFile); |
| 68 | if (!jsonFile.is_open()) |
| 69 | { |
| 70 | log<level::ERR>("Entity association JSON file not found"); |
| 71 | elog<InternalFailure>(); |
| 72 | } |
| 73 | |
| 74 | auto data = Json::parse(jsonFile, nullptr, false); |
| 75 | if (data.is_discarded()) |
| 76 | { |
| 77 | log<level::ERR>("Entity association JSON parser failure"); |
| 78 | elog<InternalFailure>(); |
| 79 | } |
| 80 | |
| 81 | return data; |
| 82 | } |
| 83 | |
| 84 | std::string read(const std::string& type, uint8_t instance, const Json& config) |
| 85 | { |
| 86 | static const std::vector<Json> empty{}; |
| 87 | std::vector<Json> readings = config.value(type, empty); |
| 88 | std::string name = ""; |
| 89 | for (const auto& j : readings) |
| 90 | { |
| 91 | uint8_t instanceNum = j.value("instance", 0); |
| 92 | // Not the instance we're interested in |
| 93 | if (instanceNum != instance) |
| 94 | { |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | // Found the instance we're interested in |
| 99 | name = j.value("name", ""); |
| 100 | |
| 101 | break; |
| 102 | } |
| 103 | return name; |
| 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 | |
| 108 | struct GetEntityNameRequest |
| 109 | { |
| 110 | uint8_t subcommand; |
| 111 | uint8_t entity_id; |
| 112 | uint8_t entity_instance; |
| 113 | } __attribute__((packed)); |
| 114 | |
| 115 | struct GetEntityNameReply |
| 116 | { |
| 117 | uint8_t subcommand; |
| 118 | uint8_t entity_name_len; |
| 119 | uint8_t entity_name[0]; |
| 120 | } __attribute__((packed)); |
| 121 | |
| 122 | ipmi_ret_t GetEntityName(const uint8_t* reqBuf, uint8_t* replyBuf, |
| 123 | size_t* dataLen) |
| 124 | { |
| 125 | struct GetEntityNameRequest request; |
| 126 | |
| 127 | if ((*dataLen) < sizeof(request)) |
| 128 | { |
| 129 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 130 | static_cast<uint32_t>(*dataLen)); |
| 131 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 132 | } |
| 133 | |
| 134 | std::memcpy(&request, &reqBuf[0], sizeof(request)); |
| 135 | |
| 136 | // Check if we support this Entity ID. |
| 137 | auto it = entityIdToName.find(request.entity_id); |
| 138 | if (it == entityIdToName.end()) |
| 139 | { |
| 140 | log<level::ERR>("Unknown Entity ID", |
| 141 | entry("ENTITY_ID=%d", request.entity_id)); |
| 142 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 143 | } |
| 144 | |
| 145 | static Json config{}; |
| 146 | static bool parsed = false; |
| 147 | |
| 148 | std::string entityName; |
| 149 | try |
| 150 | { |
| 151 | // Parse the JSON config file. |
| 152 | if (!parsed) |
| 153 | { |
| 154 | config = parse_config(); |
| 155 | parsed = true; |
| 156 | } |
| 157 | |
| 158 | // Find the "entity id:entity instance" mapping to entity name. |
| 159 | entityName = read(it->second, request.entity_instance, config); |
| 160 | if (entityName.empty()) |
| 161 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 162 | } |
| 163 | catch (InternalFailure& e) |
| 164 | { |
| 165 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 166 | } |
| 167 | |
| 168 | int length = sizeof(struct GetEntityNameReply) + entityName.length(); |
| 169 | |
| 170 | // TODO (jaghu) : Add a call to get getChannelMaxTransferSize. |
| 171 | if (length > MAX_IPMI_BUFFER) |
| 172 | { |
| 173 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
| 174 | return IPMI_CC_INVALID; |
| 175 | } |
| 176 | |
| 177 | auto reply = reinterpret_cast<struct GetEntityNameReply*>(&replyBuf[0]); |
| 178 | reply->subcommand = SysEntityName; |
| 179 | reply->entity_name_len = entityName.length(); |
| 180 | std::memcpy(reply->entity_name, entityName.c_str(), entityName.length()); |
| 181 | |
| 182 | (*dataLen) = length; |
| 183 | return IPMI_CC_OK; |
| 184 | } |
| 185 | } // namespace ipmi |
| 186 | } // namespace google |