blob: 30b8c28ed1ea55555b427da0a906517fac422ef0 [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>
Patrick Venture07886992019-03-08 12:53:57 -080023#include <filesystem>
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080024#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{
Patrick Venture07886992019-03-08 12:53:57 -080040namespace fs = std::filesystem;
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080041
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"},
Jaghathiswari Rankappagounder Natarajana289fa22019-02-15 13:49:45 -080062 {0x06, "system_management_module"},
63 {0x08, "memory_module"},
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080064 {0x0B, "add_in_card"},
Jaghathiswari Rankappagounder Natarajana289fa22019-02-15 13:49:45 -080065 {0x17, "system_chassis"},
66 {0x20, "memory_device"}};
Jaghathiswari Rankappagounder Natarajanfd0f1cf2019-01-18 15:31:07 -080067
68Json parse_config()
69{
70 std::ifstream jsonFile(configFile);
71 if (!jsonFile.is_open())
72 {
73 log<level::ERR>("Entity association JSON file not found");
74 elog<InternalFailure>();
75 }
76
77 auto data = Json::parse(jsonFile, nullptr, false);
78 if (data.is_discarded())
79 {
80 log<level::ERR>("Entity association JSON parser failure");
81 elog<InternalFailure>();
82 }
83
84 return data;
85}
86
87std::string read(const std::string& type, uint8_t instance, const Json& config)
88{
89 static const std::vector<Json> empty{};
90 std::vector<Json> readings = config.value(type, empty);
91 std::string name = "";
92 for (const auto& j : readings)
93 {
94 uint8_t instanceNum = j.value("instance", 0);
95 // Not the instance we're interested in
96 if (instanceNum != instance)
97 {
98 continue;
99 }
100
101 // Found the instance we're interested in
102 name = j.value("name", "");
103
104 break;
105 }
106 return name;
107}
108
109} // namespace
110
111struct GetEntityNameRequest
112{
113 uint8_t subcommand;
114 uint8_t entity_id;
115 uint8_t entity_instance;
116} __attribute__((packed));
117
118struct GetEntityNameReply
119{
120 uint8_t subcommand;
121 uint8_t entity_name_len;
122 uint8_t entity_name[0];
123} __attribute__((packed));
124
125ipmi_ret_t GetEntityName(const uint8_t* reqBuf, uint8_t* replyBuf,
126 size_t* dataLen)
127{
128 struct GetEntityNameRequest request;
129
130 if ((*dataLen) < sizeof(request))
131 {
132 std::fprintf(stderr, "Invalid command length: %u\n",
133 static_cast<uint32_t>(*dataLen));
134 return IPMI_CC_REQ_DATA_LEN_INVALID;
135 }
136
137 std::memcpy(&request, &reqBuf[0], sizeof(request));
138
139 // Check if we support this Entity ID.
140 auto it = entityIdToName.find(request.entity_id);
141 if (it == entityIdToName.end())
142 {
143 log<level::ERR>("Unknown Entity ID",
144 entry("ENTITY_ID=%d", request.entity_id));
145 return IPMI_CC_INVALID_FIELD_REQUEST;
146 }
147
148 static Json config{};
149 static bool parsed = false;
150
151 std::string entityName;
152 try
153 {
154 // Parse the JSON config file.
155 if (!parsed)
156 {
157 config = parse_config();
158 parsed = true;
159 }
160
161 // Find the "entity id:entity instance" mapping to entity name.
162 entityName = read(it->second, request.entity_instance, config);
163 if (entityName.empty())
164 return IPMI_CC_INVALID_FIELD_REQUEST;
165 }
166 catch (InternalFailure& e)
167 {
168 return IPMI_CC_UNSPECIFIED_ERROR;
169 }
170
171 int length = sizeof(struct GetEntityNameReply) + entityName.length();
172
173 // TODO (jaghu) : Add a call to get getChannelMaxTransferSize.
174 if (length > MAX_IPMI_BUFFER)
175 {
176 std::fprintf(stderr, "Response would overflow response buffer\n");
177 return IPMI_CC_INVALID;
178 }
179
180 auto reply = reinterpret_cast<struct GetEntityNameReply*>(&replyBuf[0]);
181 reply->subcommand = SysEntityName;
182 reply->entity_name_len = entityName.length();
183 std::memcpy(reply->entity_name, entityName.c_str(), entityName.length());
184
185 (*dataLen) = length;
186 return IPMI_CC_OK;
187}
188} // namespace ipmi
189} // namespace google