blob: 9bb53e7735c5515e6deae3dfeffaf682fb396a46 [file] [log] [blame]
James Feistc95cb142018-02-26 10:41:42 -08001/*
2// Copyright (c) 2018 Intel Corporation
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*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file overlay.cpp
James Feistc95cb142018-02-26 10:41:42 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "overlay.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070019
Brad Bishope45d8c72022-05-25 15:12:53 -040020#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070021#include "devices.hpp"
22
James Feista465ccc2019-02-08 12:51:01 -080023#include <boost/algorithm/string/predicate.hpp>
Johnathan Mantey9b867872020-10-13 15:00:51 -070024#include <boost/asio/io_context.hpp>
25#include <boost/asio/steady_timer.hpp>
James Feista465ccc2019-02-08 12:51:01 -080026#include <boost/container/flat_map.hpp>
27#include <boost/container/flat_set.hpp>
28#include <boost/process/child.hpp>
James Feist8c505da2020-05-28 10:06:33 -070029#include <nlohmann/json.hpp>
30
James Feist637b3ef2019-04-15 16:35:30 -070031#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080032#include <iomanip>
33#include <iostream>
James Feista465ccc2019-02-08 12:51:01 -080034#include <regex>
35#include <string>
James Feistc95cb142018-02-26 10:41:42 -080036
Ed Tanous07d467b2021-02-23 14:48:37 -080037constexpr const char* outputDir = "/tmp/overlays";
38constexpr const char* templateChar = "$";
39constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices";
40constexpr const char* muxSymlinkDir = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080041
Ed Tanous07d467b2021-02-23 14:48:37 -080042constexpr const bool debug = false;
Josh Lehan96b8a6e2019-10-09 14:39:49 -070043
Ed Tanous07d467b2021-02-23 14:48:37 -080044std::regex illegalNameRegex("[^A-Za-z0-9_]");
James Feistc95cb142018-02-26 10:41:42 -080045
James Feistc95cb142018-02-26 10:41:42 -080046// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080047std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080048{
49 if (in.type() == nlohmann::json::value_t::string)
50 {
51 return in.get<std::string>();
52 }
Ed Tanous07d467b2021-02-23 14:48:37 -080053 if (in.type() == nlohmann::json::value_t::array)
James Feistc95cb142018-02-26 10:41:42 -080054 {
55 // remove brackets and comma from array
56 std::string array = in.dump();
57 array = array.substr(1, array.size() - 2);
58 boost::replace_all(array, ",", " ");
59 return array;
60 }
61 return in.dump();
62}
63
Ed Tanousee3357a2019-02-26 12:44:14 -080064void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -080065 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080066{
James Feist286babc2019-02-07 16:48:28 -080067 std::error_code ec;
Ed Tanous07d467b2021-02-23 14:48:37 -080068 std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
69 std::filesystem::create_directory(muxSymlinkDirPath, ec);
Ed Tanousee3357a2019-02-26 12:44:14 -080070 // ignore error codes here if the directory already exists
71 ec.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -080072 std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
Ed Tanousee3357a2019-02-26 12:44:14 -080073 std::filesystem::create_directory(linkDir, ec);
74
75 std::ostringstream hexAddress;
76 hexAddress << std::hex << std::setfill('0') << std::setw(4) << address;
James Feist286babc2019-02-07 16:48:28 -080077
Ed Tanous07d467b2021-02-23 14:48:37 -080078 std::filesystem::path devDir(i2CDevsDir);
Ed Tanousee3357a2019-02-26 12:44:14 -080079 devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
James Feist286babc2019-02-07 16:48:28 -080080
Ed Tanousee3357a2019-02-26 12:44:14 -080081 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
82 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080083 {
Ed Tanousee3357a2019-02-26 12:44:14 -080084 const std::string* channelName =
85 channelNames[channelIndex].get_ptr<const std::string*>();
86 if (channelName == nullptr)
87 {
88 continue;
89 }
90 if (channelName->empty())
91 {
92 continue;
93 }
94
95 std::filesystem::path channelPath =
96 devDir / ("channel-" + std::to_string(channelIndex));
97 if (!is_symlink(channelPath))
98 {
Zev Weisse7d56e02022-07-15 12:58:30 -070099 std::cerr << channelPath << " for mux channel " << *channelName
Ed Tanousee3357a2019-02-26 12:44:14 -0800100 << " doesn't exist!\n";
101 continue;
102 }
103 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
104
105 std::filesystem::path fp("/dev" / bus.filename());
106 std::filesystem::path link(linkDir / *channelName);
107
108 std::filesystem::create_symlink(fp, link, ec);
109 if (ec)
110 {
111 std::cerr << "Failure creating symlink for " << fp << " to " << link
112 << "\n";
113 }
James Feistc95cb142018-02-26 10:41:42 -0800114 }
115}
116
Zev Weissc11b5da2022-07-12 16:31:37 -0700117static int deleteDevice(const std::string& busPath,
Ed Tanous07d467b2021-02-23 14:48:37 -0800118 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700119 const std::string& destructor)
120{
121 if (!address)
122 {
123 return -1;
124 }
Zev Weissc11b5da2022-07-12 16:31:37 -0700125 std::filesystem::path deviceDestructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700126 deviceDestructor /= destructor;
127 std::ofstream deviceFile(deviceDestructor);
128 if (!deviceFile.good())
129 {
130 std::cerr << "Error writing " << deviceDestructor << "\n";
131 return -1;
132 }
133 deviceFile << std::to_string(*address);
134 deviceFile.close();
135 return 0;
136}
137
Zev Weissc11b5da2022-07-12 16:31:37 -0700138static int createDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700139 const std::string& parameters,
140 const std::string& constructor)
141{
Zev Weissc11b5da2022-07-12 16:31:37 -0700142 std::filesystem::path deviceConstructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700143 deviceConstructor /= constructor;
144 std::ofstream deviceFile(deviceConstructor);
145 if (!deviceFile.good())
146 {
147 std::cerr << "Error writing " << deviceConstructor << "\n";
148 return -1;
149 }
150 deviceFile << parameters;
151 deviceFile.close();
152
153 return 0;
154}
155
Zev Weissc11b5da2022-07-12 16:31:37 -0700156static bool deviceIsCreated(const std::string& busPath,
Ed Tanous07d467b2021-02-23 14:48:37 -0800157 const std::shared_ptr<uint64_t>& bus,
158 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700159 const bool retrying)
160{
161 if (!bus || !address)
162 {
163 return false;
164 }
165
166 std::ostringstream hex;
167 hex << std::hex << std::setw(4) << std::setfill('0') << *address;
168 std::string addressHex = hex.str();
169 std::string busStr = std::to_string(*bus);
170
Ed Tanous37e142b2021-04-30 12:19:26 -0700171 std::error_code ec;
Zev Weissc11b5da2022-07-12 16:31:37 -0700172 auto path = std::filesystem::recursive_directory_iterator(busPath, ec);
Ed Tanous37e142b2021-04-30 12:19:26 -0700173 if (ec)
174 {
Zev Weissc11b5da2022-07-12 16:31:37 -0700175 std::cerr << "Unable to open path " << busPath << "\n";
Ed Tanous37e142b2021-04-30 12:19:26 -0700176 return false;
177 }
178 for (; path != std::filesystem::recursive_directory_iterator(); path++)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700179 {
180 if (!std::filesystem::is_directory(*path))
181 {
182 continue;
183 }
184
185 const std::string directoryName = path->path().filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800186 std::string name = busStr;
187 name += "-";
188 name += addressHex;
189 if (directoryName == name)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700190 {
191 // The first time the BMC boots the kernel has creates a
192 // filesystem enumerating the I2C devices. The I2C device has not
193 // been initialized for use. This requires a call to a device
194 // node, such as "new_device". The first pass through this
195 // function is only confirming the filesystem contains the device
196 // entry of interest (i.e. i2c4-0050).
197 //
198 // An upper level function performs the device creation
199 // action. This action may fail. The device driver (dd) used to
200 // create the I2C filesystem substructure eats any error codes,
201 // and always returns 0. This is by design. It is also possible
202 // for the new_device action to fail because the device is not
203 // actually in the system, i.e. optional equipment.
204 //
205 // The 'retrying' pass of this function is used to confirm the
206 // 'dd' device driver succeeded. Success is measured by finding
207 // the 'hwmon' subdirectory in the filesystem. The first attempt
208 // is delayed by an arbitrary amount, in order to permit the
209 // kernel time to create the filesystem entries. The upper level
210 // function determines the number of times to retry calling this
211 // function.
212 if (retrying)
213 {
214 std::error_code ec;
Zev Weissc11b5da2022-07-12 16:31:37 -0700215 std::filesystem::path hwmonDir(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700216 hwmonDir /= directoryName;
217 hwmonDir /= "hwmon";
218 return std::filesystem::is_directory(hwmonDir, ec);
219 }
220 return true;
221 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800222 path.disable_recursion_pending();
Johnathan Mantey9b867872020-10-13 15:00:51 -0700223 }
224 return false;
225}
226
Zev Weissc11b5da2022-07-12 16:31:37 -0700227static int buildDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700228 const std::string& parameters,
Ed Tanous07d467b2021-02-23 14:48:37 -0800229 const std::shared_ptr<uint64_t>& bus,
230 const std::shared_ptr<uint64_t>& address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700231 const std::string& constructor,
232 const std::string& destructor, const bool createsHWMon,
233 const size_t retries = 5)
234{
235 bool tryAgain = false;
236 if (!retries)
237 {
238 return -1;
239 }
240
Zev Weissc11b5da2022-07-12 16:31:37 -0700241 if (!deviceIsCreated(busPath, bus, address, false))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700242 {
Zev Weissc11b5da2022-07-12 16:31:37 -0700243 createDevice(busPath, parameters, constructor);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700244 tryAgain = true;
245 }
Zev Weissc11b5da2022-07-12 16:31:37 -0700246 else if (createsHWMon && !deviceIsCreated(busPath, bus, address, true))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700247 {
248 // device is present, hwmon subdir missing
Zev Weissc11b5da2022-07-12 16:31:37 -0700249 deleteDevice(busPath, address, destructor);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700250 tryAgain = true;
251 }
252
253 if (tryAgain)
254 {
255 std::shared_ptr<boost::asio::steady_timer> createTimer =
256 std::make_shared<boost::asio::steady_timer>(io);
257 createTimer->expires_after(std::chrono::milliseconds(500));
Zev Weissc11b5da2022-07-12 16:31:37 -0700258 createTimer->async_wait([createTimer, busPath, parameters, bus,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700259 address, constructor, destructor, createsHWMon,
260 retries](const boost::system::error_code& ec) {
261 if (ec)
262 {
263 std::cerr << "Timer error: " << ec << "\n";
264 return -2;
265 }
Zev Weissc11b5da2022-07-12 16:31:37 -0700266 return buildDevice(busPath, parameters, bus, address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700267 constructor, destructor, createsHWMon,
268 retries - 1);
269 });
270 }
271 return 0;
272}
273
James Feista465ccc2019-02-08 12:51:01 -0800274void exportDevice(const std::string& type,
275 const devices::ExportTemplate& exportTemplate,
276 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700277{
278
279 std::string parameters = exportTemplate.parameters;
Zev Weissc11b5da2022-07-12 16:31:37 -0700280 std::string busPath = exportTemplate.busPath;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700281 std::string constructor = exportTemplate.add;
282 std::string destructor = exportTemplate.remove;
283 bool createsHWMon = exportTemplate.createsHWMon;
James Feist053a6642018-10-15 13:17:09 -0700284 std::string name = "unknown";
Johnathan Mantey9b867872020-10-13 15:00:51 -0700285 std::shared_ptr<uint64_t> bus = nullptr;
286 std::shared_ptr<uint64_t> address = nullptr;
James Feista465ccc2019-02-08 12:51:01 -0800287 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700288
289 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
290 keyPair++)
291 {
292 std::string subsituteString;
293
294 if (keyPair.key() == "Name" &&
295 keyPair.value().type() == nlohmann::json::value_t::string)
296 {
297 subsituteString = std::regex_replace(
Ed Tanous07d467b2021-02-23 14:48:37 -0800298 keyPair.value().get<std::string>(), illegalNameRegex, "_");
James Feist053a6642018-10-15 13:17:09 -0700299 name = subsituteString;
300 }
301 else
302 {
303 subsituteString = jsonToString(keyPair.value());
304 }
305
306 if (keyPair.key() == "Bus")
307 {
Johnathan Mantey9b867872020-10-13 15:00:51 -0700308 bus = std::make_shared<uint64_t>(
309 *keyPair.value().get_ptr<const uint64_t*>());
James Feist053a6642018-10-15 13:17:09 -0700310 }
311 else if (keyPair.key() == "Address")
312 {
Johnathan Mantey9b867872020-10-13 15:00:51 -0700313 address = std::make_shared<uint64_t>(
314 *keyPair.value().get_ptr<const uint64_t*>());
James Feist053a6642018-10-15 13:17:09 -0700315 }
James Feist286babc2019-02-07 16:48:28 -0800316 else if (keyPair.key() == "ChannelNames")
317 {
318 channels =
James Feista465ccc2019-02-08 12:51:01 -0800319 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800320 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800321 boost::replace_all(parameters, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700322 subsituteString);
Zev Weissc11b5da2022-07-12 16:31:37 -0700323 boost::replace_all(busPath, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700324 subsituteString);
325 }
326
Zev Weissc11b5da2022-07-12 16:31:37 -0700327 int err = buildDevice(busPath, parameters, bus, address, constructor,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700328 destructor, createsHWMon);
James Feist053a6642018-10-15 13:17:09 -0700329
Johnathan Mantey9b867872020-10-13 15:00:51 -0700330 if (!err && boost::ends_with(type, "Mux") && bus && address && channels)
James Feist286babc2019-02-07 16:48:28 -0800331 {
James Feist98132792019-07-09 13:29:09 -0700332 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
333 *channels);
James Feist286babc2019-02-07 16:48:28 -0800334 }
James Feist053a6642018-10-15 13:17:09 -0700335}
336
James Feista465ccc2019-02-08 12:51:01 -0800337bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800338{
Ed Tanous07d467b2021-02-23 14:48:37 -0800339 std::filesystem::create_directory(outputDir);
James Feistc95cb142018-02-26 10:41:42 -0800340 for (auto entity = systemConfiguration.begin();
341 entity != systemConfiguration.end(); entity++)
342 {
James Feist1e3e6982018-08-03 16:09:28 -0700343 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800344 if (findExposes == entity.value().end() ||
345 findExposes->type() != nlohmann::json::value_t::array)
346 {
347 continue;
348 }
349
James Feista465ccc2019-02-08 12:51:01 -0800350 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800351 {
James Feist1e3e6982018-08-03 16:09:28 -0700352 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800353 // status missing is assumed to be 'okay'
354 if (findStatus != configuration.end() && *findStatus == "disabled")
355 {
356 continue;
357 }
James Feistd63d18a2018-07-19 15:23:45 -0700358 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800359 if (findType == configuration.end() ||
360 findType->type() != nlohmann::json::value_t::string)
361 {
362 continue;
363 }
364 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700365 auto device = devices::exportTemplates.find(type.c_str());
366 if (device != devices::exportTemplates.end())
367 {
James Feist286babc2019-02-07 16:48:28 -0800368 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700369 continue;
370 }
371
372 // Because many devices are intentionally not exportable,
373 // this error message is not printed in all situations.
374 // If wondering why your device not appearing, add your type to
375 // the exportTemplates array in the devices.hpp file.
Ed Tanous07d467b2021-02-23 14:48:37 -0800376 if constexpr (debug)
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700377 {
378 std::cerr << "Device type " << type
379 << " not found in export map whitelist\n";
James Feist053a6642018-10-15 13:17:09 -0700380 }
James Feistc95cb142018-02-26 10:41:42 -0800381 }
382 }
383
384 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700385}