blob: 6d184c359b8ead51fb0180b57062ce9e8aeb578f [file] [log] [blame]
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -08001/*
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
36namespace google
37{
38namespace ipmi
39{
40namespace fs = std::experimental::filesystem;
41
42namespace
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
50using Json = nlohmann::json;
51
52using namespace phosphor::logging;
53using InternalFailure =
54 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
55
56static constexpr auto configFile =
57 "/usr/share/ipmi-entity-association/entity_association_map.json";
58
59static 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
65Json 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
84std::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
108struct GetEntityNameRequest
109{
110 uint8_t subcommand;
111 uint8_t entity_id;
112 uint8_t entity_instance;
113} __attribute__((packed));
114
115struct GetEntityNameReply
116{
117 uint8_t subcommand;
118 uint8_t entity_name_len;
119 uint8_t entity_name[0];
120} __attribute__((packed));
121
122ipmi_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