blob: ddbddb487e9236a45195a08c567f0622c21a3f2a [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
Patrick Venturea49dc332019-10-26 08:32:02 -070020#include "devices.hpp"
Ed Tanous3013fb42022-07-09 08:27:06 -070021#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070022
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>
Alexander Hansenc3db2c32024-08-20 15:01:38 +020030#include <phosphor-logging/lg2.hpp>
James Feist8c505da2020-05-28 10:06:33 -070031
James Feist637b3ef2019-04-15 16:35:30 -070032#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080033#include <iomanip>
34#include <iostream>
James Feista465ccc2019-02-08 12:51:01 -080035#include <regex>
36#include <string>
James Feistc95cb142018-02-26 10:41:42 -080037
Ed Tanous07d467b2021-02-23 14:48:37 -080038constexpr const char* outputDir = "/tmp/overlays";
39constexpr const char* templateChar = "$";
40constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices";
41constexpr const char* muxSymlinkDir = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080042
Ed Tanousfc171422024-04-04 17:18:16 -070043const std::regex illegalNameRegex("[^A-Za-z0-9_]");
James Feistc95cb142018-02-26 10:41:42 -080044
James Feistc95cb142018-02-26 10:41:42 -080045// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080046std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080047{
48 if (in.type() == nlohmann::json::value_t::string)
49 {
50 return in.get<std::string>();
51 }
Ed Tanous07d467b2021-02-23 14:48:37 -080052 if (in.type() == nlohmann::json::value_t::array)
James Feistc95cb142018-02-26 10:41:42 -080053 {
54 // remove brackets and comma from array
55 std::string array = in.dump();
56 array = array.substr(1, array.size() - 2);
57 boost::replace_all(array, ",", " ");
58 return array;
59 }
60 return in.dump();
61}
62
Zev Weiss9afef3a2022-07-13 16:38:23 -070063static std::string deviceDirName(uint64_t bus, uint64_t address)
64{
65 std::ostringstream name;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -070066 name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
67 << address;
Zev Weiss9afef3a2022-07-13 16:38:23 -070068 return name.str();
69}
70
Jonathan Domand0eb1292023-04-19 11:21:56 -070071void linkMux(const std::string& muxName, uint64_t busIndex, uint64_t address,
Jonathan Doman6af72c92023-04-19 11:55:39 -070072 const std::vector<std::string>& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080073{
James Feist286babc2019-02-07 16:48:28 -080074 std::error_code ec;
Ed Tanous07d467b2021-02-23 14:48:37 -080075 std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
76 std::filesystem::create_directory(muxSymlinkDirPath, ec);
Ed Tanousee3357a2019-02-26 12:44:14 -080077 // ignore error codes here if the directory already exists
78 ec.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -080079 std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
Ed Tanousee3357a2019-02-26 12:44:14 -080080 std::filesystem::create_directory(linkDir, ec);
81
Ed Tanous07d467b2021-02-23 14:48:37 -080082 std::filesystem::path devDir(i2CDevsDir);
Zev Weiss9afef3a2022-07-13 16:38:23 -070083 devDir /= deviceDirName(busIndex, address);
James Feist286babc2019-02-07 16:48:28 -080084
Ed Tanousee3357a2019-02-26 12:44:14 -080085 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
86 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080087 {
Jonathan Doman6af72c92023-04-19 11:55:39 -070088 const std::string& channelName = channelNames[channelIndex];
89 if (channelName.empty())
Ed Tanousee3357a2019-02-26 12:44:14 -080090 {
91 continue;
92 }
93
94 std::filesystem::path channelPath =
95 devDir / ("channel-" + std::to_string(channelIndex));
96 if (!is_symlink(channelPath))
97 {
Jonathan Doman6af72c92023-04-19 11:55:39 -070098 std::cerr << channelPath << " for mux channel " << channelName
Ed Tanousee3357a2019-02-26 12:44:14 -080099 << " doesn't exist!\n";
100 continue;
101 }
102 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
103
104 std::filesystem::path fp("/dev" / bus.filename());
Jonathan Doman6af72c92023-04-19 11:55:39 -0700105 std::filesystem::path link(linkDir / channelName);
Ed Tanousee3357a2019-02-26 12:44:14 -0800106
107 std::filesystem::create_symlink(fp, link, ec);
108 if (ec)
109 {
110 std::cerr << "Failure creating symlink for " << fp << " to " << link
111 << "\n";
112 }
James Feistc95cb142018-02-26 10:41:42 -0800113 }
114}
115
Jonathan Domand0eb1292023-04-19 11:21:56 -0700116static int deleteDevice(const std::string& busPath, uint64_t address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700117 const std::string& destructor)
118{
Zev Weissc11b5da2022-07-12 16:31:37 -0700119 std::filesystem::path deviceDestructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700120 deviceDestructor /= destructor;
121 std::ofstream deviceFile(deviceDestructor);
122 if (!deviceFile.good())
123 {
124 std::cerr << "Error writing " << deviceDestructor << "\n";
125 return -1;
126 }
Jonathan Domand0eb1292023-04-19 11:21:56 -0700127 deviceFile << std::to_string(address);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700128 deviceFile.close();
129 return 0;
130}
131
Zev Weissc11b5da2022-07-12 16:31:37 -0700132static int createDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700133 const std::string& parameters,
134 const std::string& constructor)
135{
Zev Weissc11b5da2022-07-12 16:31:37 -0700136 std::filesystem::path deviceConstructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700137 deviceConstructor /= constructor;
138 std::ofstream deviceFile(deviceConstructor);
139 if (!deviceFile.good())
140 {
141 std::cerr << "Error writing " << deviceConstructor << "\n";
142 return -1;
143 }
144 deviceFile << parameters;
145 deviceFile.close();
146
147 return 0;
148}
149
Jonathan Domand0eb1292023-04-19 11:21:56 -0700150static bool deviceIsCreated(const std::string& busPath, uint64_t bus,
151 uint64_t address,
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700152 const devices::createsHWMon hasHWMonDir)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700153{
Zev Weiss67003d62022-07-15 16:38:19 -0700154 std::filesystem::path dirPath = busPath;
Jonathan Domand0eb1292023-04-19 11:21:56 -0700155 dirPath /= deviceDirName(bus, address);
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700156 if (hasHWMonDir == devices::createsHWMon::hasHWMonDir)
Zev Weiss67003d62022-07-15 16:38:19 -0700157 {
158 dirPath /= "hwmon";
159 }
Johnathan Mantey9b867872020-10-13 15:00:51 -0700160
Ed Tanous37e142b2021-04-30 12:19:26 -0700161 std::error_code ec;
Zev Weiss67003d62022-07-15 16:38:19 -0700162 // Ignore errors; anything but a clean 'true' is just fine as 'false'
163 return std::filesystem::exists(dirPath, ec);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700164}
165
Patrick Williamsb7077432024-08-16 15:22:21 -0400166static int
167 buildDevice(const std::string& name, const std::string& busPath,
168 const std::string& parameters, uint64_t bus, uint64_t address,
169 const std::string& constructor, const std::string& destructor,
170 const devices::createsHWMon hasHWMonDir,
171 std::vector<std::string> channelNames, const size_t retries = 5)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700172{
Ed Tanous3013fb42022-07-09 08:27:06 -0700173 if (retries == 0U)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700174 {
175 return -1;
176 }
177
Jonathan Doman6af72c92023-04-19 11:55:39 -0700178 // If it's already instantiated, we don't need to create it again.
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700179 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700180 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700181 // Try to create the device
182 createDevice(busPath, parameters, constructor);
Zev Weisscbcd17f2022-07-15 15:39:21 -0700183
Jonathan Doman6af72c92023-04-19 11:55:39 -0700184 // If it didn't work, delete it and try again in 500ms
185 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
186 {
187 deleteDevice(busPath, address, destructor);
188
189 std::shared_ptr<boost::asio::steady_timer> createTimer =
190 std::make_shared<boost::asio::steady_timer>(io);
191 createTimer->expires_after(std::chrono::milliseconds(500));
192 createTimer->async_wait(
193 [createTimer, name, busPath, parameters, bus, address,
194 constructor, destructor, hasHWMonDir,
195 channelNames(std::move(channelNames)),
196 retries](const boost::system::error_code& ec) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400197 if (ec)
198 {
199 std::cerr << "Timer error: " << ec << "\n";
200 return -2;
201 }
202 return buildDevice(name, busPath, parameters, bus, address,
203 constructor, destructor, hasHWMonDir,
204 std::move(channelNames), retries - 1);
205 });
Jonathan Doman6af72c92023-04-19 11:55:39 -0700206 return -1;
207 }
208 }
209
210 // Link the mux channels if needed once the device is created.
211 if (!channelNames.empty())
212 {
213 linkMux(name, bus, address, channelNames);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700214 }
Zev Weisscbcd17f2022-07-15 15:39:21 -0700215
Johnathan Mantey9b867872020-10-13 15:00:51 -0700216 return 0;
217}
218
James Feista465ccc2019-02-08 12:51:01 -0800219void exportDevice(const std::string& type,
220 const devices::ExportTemplate& exportTemplate,
221 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700222{
James Feist053a6642018-10-15 13:17:09 -0700223 std::string parameters = exportTemplate.parameters;
Zev Weissc11b5da2022-07-12 16:31:37 -0700224 std::string busPath = exportTemplate.busPath;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700225 std::string constructor = exportTemplate.add;
226 std::string destructor = exportTemplate.remove;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700227 devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir;
James Feist053a6642018-10-15 13:17:09 -0700228 std::string name = "unknown";
Jonathan Domand0eb1292023-04-19 11:21:56 -0700229 std::optional<uint64_t> bus;
230 std::optional<uint64_t> address;
Jonathan Doman6af72c92023-04-19 11:55:39 -0700231 std::vector<std::string> channels;
James Feist053a6642018-10-15 13:17:09 -0700232
233 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
234 keyPair++)
235 {
236 std::string subsituteString;
237
238 if (keyPair.key() == "Name" &&
239 keyPair.value().type() == nlohmann::json::value_t::string)
240 {
241 subsituteString = std::regex_replace(
Ed Tanous07d467b2021-02-23 14:48:37 -0800242 keyPair.value().get<std::string>(), illegalNameRegex, "_");
James Feist053a6642018-10-15 13:17:09 -0700243 name = subsituteString;
244 }
245 else
246 {
247 subsituteString = jsonToString(keyPair.value());
248 }
249
250 if (keyPair.key() == "Bus")
251 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700252 bus = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700253 }
254 else if (keyPair.key() == "Address")
255 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700256 address = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700257 }
Jonathan Doman6af72c92023-04-19 11:55:39 -0700258 else if (keyPair.key() == "ChannelNames" && type.ends_with("Mux"))
James Feist286babc2019-02-07 16:48:28 -0800259 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700260 channels = keyPair.value().get<std::vector<std::string>>();
James Feist286babc2019-02-07 16:48:28 -0800261 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800262 boost::replace_all(parameters, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700263 subsituteString);
Zev Weissc11b5da2022-07-12 16:31:37 -0700264 boost::replace_all(busPath, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700265 subsituteString);
266 }
267
Jonathan Domand0eb1292023-04-19 11:21:56 -0700268 if (!bus || !address)
269 {
270 createDevice(busPath, parameters, constructor);
271 return;
272 }
273
Jonathan Doman6af72c92023-04-19 11:55:39 -0700274 buildDevice(name, busPath, parameters, *bus, *address, constructor,
275 destructor, hasHWMonDir, std::move(channels));
James Feist053a6642018-10-15 13:17:09 -0700276}
277
James Feista465ccc2019-02-08 12:51:01 -0800278bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800279{
Ed Tanous07d467b2021-02-23 14:48:37 -0800280 std::filesystem::create_directory(outputDir);
James Feistc95cb142018-02-26 10:41:42 -0800281 for (auto entity = systemConfiguration.begin();
282 entity != systemConfiguration.end(); entity++)
283 {
James Feist1e3e6982018-08-03 16:09:28 -0700284 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800285 if (findExposes == entity.value().end() ||
286 findExposes->type() != nlohmann::json::value_t::array)
287 {
288 continue;
289 }
290
Ed Tanous3013fb42022-07-09 08:27:06 -0700291 for (const auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800292 {
James Feist1e3e6982018-08-03 16:09:28 -0700293 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800294 // status missing is assumed to be 'okay'
295 if (findStatus != configuration.end() && *findStatus == "disabled")
296 {
297 continue;
298 }
James Feistd63d18a2018-07-19 15:23:45 -0700299 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800300 if (findType == configuration.end() ||
301 findType->type() != nlohmann::json::value_t::string)
302 {
303 continue;
304 }
305 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700306 auto device = devices::exportTemplates.find(type.c_str());
307 if (device != devices::exportTemplates.end())
308 {
James Feist286babc2019-02-07 16:48:28 -0800309 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700310 continue;
311 }
312
313 // Because many devices are intentionally not exportable,
314 // this error message is not printed in all situations.
315 // If wondering why your device not appearing, add your type to
316 // the exportTemplates array in the devices.hpp file.
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200317 lg2::debug("Device type {TYPE} not found in export map allowlist",
318 "TYPE", type);
James Feistc95cb142018-02-26 10:41:42 -0800319 }
320 }
321
322 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700323}