blob: 635a5b81d4c14ede474ad70d011f65fa6e62aced [file] [log] [blame]
Alexander Hansen4e1142d2025-07-25 17:07:27 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
James Feistc95cb142018-02-26 10:41:42 -08003
Brad Bishope45d8c72022-05-25 15:12:53 -04004#include "overlay.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -07005
Patrick Venturea49dc332019-10-26 08:32:02 -07006#include "devices.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +02007#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -07008
Patrick Williamscc34fd72025-06-13 15:04:03 -04009#include <boost/algorithm/string/replace.hpp>
Johnathan Mantey9b867872020-10-13 15:00:51 -070010#include <boost/asio/io_context.hpp>
11#include <boost/asio/steady_timer.hpp>
James Feista465ccc2019-02-08 12:51:01 -080012#include <boost/container/flat_map.hpp>
13#include <boost/container/flat_set.hpp>
James Feist8c505da2020-05-28 10:06:33 -070014#include <nlohmann/json.hpp>
Alexander Hansenc3db2c32024-08-20 15:01:38 +020015#include <phosphor-logging/lg2.hpp>
James Feist8c505da2020-05-28 10:06:33 -070016
James Feist637b3ef2019-04-15 16:35:30 -070017#include <filesystem>
Christopher Meis59ef1e72025-04-16 08:53:25 +020018#include <fstream>
James Feista465ccc2019-02-08 12:51:01 -080019#include <iomanip>
20#include <iostream>
James Feista465ccc2019-02-08 12:51:01 -080021#include <regex>
22#include <string>
James Feistc95cb142018-02-26 10:41:42 -080023
Ed Tanous07d467b2021-02-23 14:48:37 -080024constexpr const char* outputDir = "/tmp/overlays";
25constexpr const char* templateChar = "$";
26constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices";
27constexpr const char* muxSymlinkDir = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080028
Ed Tanousfc171422024-04-04 17:18:16 -070029const std::regex illegalNameRegex("[^A-Za-z0-9_]");
James Feistc95cb142018-02-26 10:41:42 -080030
James Feistc95cb142018-02-26 10:41:42 -080031// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080032std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080033{
34 if (in.type() == nlohmann::json::value_t::string)
35 {
36 return in.get<std::string>();
37 }
Ed Tanous07d467b2021-02-23 14:48:37 -080038 if (in.type() == nlohmann::json::value_t::array)
James Feistc95cb142018-02-26 10:41:42 -080039 {
40 // remove brackets and comma from array
41 std::string array = in.dump();
42 array = array.substr(1, array.size() - 2);
43 boost::replace_all(array, ",", " ");
44 return array;
45 }
46 return in.dump();
47}
48
Zev Weiss9afef3a2022-07-13 16:38:23 -070049static std::string deviceDirName(uint64_t bus, uint64_t address)
50{
51 std::ostringstream name;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -070052 name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
53 << address;
Zev Weiss9afef3a2022-07-13 16:38:23 -070054 return name.str();
55}
56
Jonathan Domand0eb1292023-04-19 11:21:56 -070057void linkMux(const std::string& muxName, uint64_t busIndex, uint64_t address,
Jonathan Doman6af72c92023-04-19 11:55:39 -070058 const std::vector<std::string>& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080059{
James Feist286babc2019-02-07 16:48:28 -080060 std::error_code ec;
Ed Tanous07d467b2021-02-23 14:48:37 -080061 std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
62 std::filesystem::create_directory(muxSymlinkDirPath, ec);
Ed Tanousee3357a2019-02-26 12:44:14 -080063 // ignore error codes here if the directory already exists
64 ec.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -080065 std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
Ed Tanousee3357a2019-02-26 12:44:14 -080066 std::filesystem::create_directory(linkDir, ec);
67
Ed Tanous07d467b2021-02-23 14:48:37 -080068 std::filesystem::path devDir(i2CDevsDir);
Zev Weiss9afef3a2022-07-13 16:38:23 -070069 devDir /= deviceDirName(busIndex, address);
James Feist286babc2019-02-07 16:48:28 -080070
Ed Tanousee3357a2019-02-26 12:44:14 -080071 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
72 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080073 {
Jonathan Doman6af72c92023-04-19 11:55:39 -070074 const std::string& channelName = channelNames[channelIndex];
75 if (channelName.empty())
Ed Tanousee3357a2019-02-26 12:44:14 -080076 {
77 continue;
78 }
79
80 std::filesystem::path channelPath =
81 devDir / ("channel-" + std::to_string(channelIndex));
82 if (!is_symlink(channelPath))
83 {
Jonathan Doman6af72c92023-04-19 11:55:39 -070084 std::cerr << channelPath << " for mux channel " << channelName
Ed Tanousee3357a2019-02-26 12:44:14 -080085 << " doesn't exist!\n";
86 continue;
87 }
88 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
89
90 std::filesystem::path fp("/dev" / bus.filename());
Jonathan Doman6af72c92023-04-19 11:55:39 -070091 std::filesystem::path link(linkDir / channelName);
Ed Tanousee3357a2019-02-26 12:44:14 -080092
93 std::filesystem::create_symlink(fp, link, ec);
94 if (ec)
95 {
96 std::cerr << "Failure creating symlink for " << fp << " to " << link
97 << "\n";
98 }
James Feistc95cb142018-02-26 10:41:42 -080099 }
100}
101
Jonathan Domand0eb1292023-04-19 11:21:56 -0700102static int deleteDevice(const std::string& busPath, uint64_t address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700103 const std::string& destructor)
104{
Zev Weissc11b5da2022-07-12 16:31:37 -0700105 std::filesystem::path deviceDestructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700106 deviceDestructor /= destructor;
107 std::ofstream deviceFile(deviceDestructor);
108 if (!deviceFile.good())
109 {
110 std::cerr << "Error writing " << deviceDestructor << "\n";
111 return -1;
112 }
Jonathan Domand0eb1292023-04-19 11:21:56 -0700113 deviceFile << std::to_string(address);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700114 deviceFile.close();
115 return 0;
116}
117
Zev Weissc11b5da2022-07-12 16:31:37 -0700118static int createDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700119 const std::string& parameters,
120 const std::string& constructor)
121{
Zev Weissc11b5da2022-07-12 16:31:37 -0700122 std::filesystem::path deviceConstructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700123 deviceConstructor /= constructor;
124 std::ofstream deviceFile(deviceConstructor);
125 if (!deviceFile.good())
126 {
127 std::cerr << "Error writing " << deviceConstructor << "\n";
128 return -1;
129 }
130 deviceFile << parameters;
131 deviceFile.close();
132
133 return 0;
134}
135
Jonathan Domand0eb1292023-04-19 11:21:56 -0700136static bool deviceIsCreated(const std::string& busPath, uint64_t bus,
137 uint64_t address,
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700138 const devices::createsHWMon hasHWMonDir)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700139{
Zev Weiss67003d62022-07-15 16:38:19 -0700140 std::filesystem::path dirPath = busPath;
Jonathan Domand0eb1292023-04-19 11:21:56 -0700141 dirPath /= deviceDirName(bus, address);
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700142 if (hasHWMonDir == devices::createsHWMon::hasHWMonDir)
Zev Weiss67003d62022-07-15 16:38:19 -0700143 {
144 dirPath /= "hwmon";
145 }
Johnathan Mantey9b867872020-10-13 15:00:51 -0700146
Ed Tanous37e142b2021-04-30 12:19:26 -0700147 std::error_code ec;
Zev Weiss67003d62022-07-15 16:38:19 -0700148 // Ignore errors; anything but a clean 'true' is just fine as 'false'
149 return std::filesystem::exists(dirPath, ec);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700150}
151
Patrick Williams5a807032025-03-03 11:20:39 -0500152static int buildDevice(
153 const std::string& name, const std::string& busPath,
154 const std::string& parameters, uint64_t bus, uint64_t address,
155 const std::string& constructor, const std::string& destructor,
156 const devices::createsHWMon hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200157 std::vector<std::string> channelNames, boost::asio::io_context& io,
158 const size_t retries = 5)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700159{
Ed Tanous3013fb42022-07-09 08:27:06 -0700160 if (retries == 0U)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700161 {
162 return -1;
163 }
164
Jonathan Doman6af72c92023-04-19 11:55:39 -0700165 // If it's already instantiated, we don't need to create it again.
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700166 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700167 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700168 // Try to create the device
169 createDevice(busPath, parameters, constructor);
Zev Weisscbcd17f2022-07-15 15:39:21 -0700170
Jonathan Doman6af72c92023-04-19 11:55:39 -0700171 // If it didn't work, delete it and try again in 500ms
172 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
173 {
174 deleteDevice(busPath, address, destructor);
175
176 std::shared_ptr<boost::asio::steady_timer> createTimer =
177 std::make_shared<boost::asio::steady_timer>(io);
178 createTimer->expires_after(std::chrono::milliseconds(500));
179 createTimer->async_wait(
180 [createTimer, name, busPath, parameters, bus, address,
181 constructor, destructor, hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200182 channelNames(std::move(channelNames)), retries,
183 &io](const boost::system::error_code& ec) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400184 if (ec)
185 {
186 std::cerr << "Timer error: " << ec << "\n";
187 return -2;
188 }
189 return buildDevice(name, busPath, parameters, bus, address,
190 constructor, destructor, hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200191 std::move(channelNames), io,
192 retries - 1);
Patrick Williamsb7077432024-08-16 15:22:21 -0400193 });
Jonathan Doman6af72c92023-04-19 11:55:39 -0700194 return -1;
195 }
196 }
197
198 // Link the mux channels if needed once the device is created.
199 if (!channelNames.empty())
200 {
201 linkMux(name, bus, address, channelNames);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700202 }
Zev Weisscbcd17f2022-07-15 15:39:21 -0700203
Johnathan Mantey9b867872020-10-13 15:00:51 -0700204 return 0;
205}
206
James Feista465ccc2019-02-08 12:51:01 -0800207void exportDevice(const std::string& type,
208 const devices::ExportTemplate& exportTemplate,
Alexander Hansena555acf2025-06-27 11:59:10 +0200209 const nlohmann::json& configuration,
210 boost::asio::io_context& io)
James Feist053a6642018-10-15 13:17:09 -0700211{
James Feist053a6642018-10-15 13:17:09 -0700212 std::string parameters = exportTemplate.parameters;
Zev Weissc11b5da2022-07-12 16:31:37 -0700213 std::string busPath = exportTemplate.busPath;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700214 std::string constructor = exportTemplate.add;
215 std::string destructor = exportTemplate.remove;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700216 devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir;
James Feist053a6642018-10-15 13:17:09 -0700217 std::string name = "unknown";
Jonathan Domand0eb1292023-04-19 11:21:56 -0700218 std::optional<uint64_t> bus;
219 std::optional<uint64_t> address;
Jonathan Doman6af72c92023-04-19 11:55:39 -0700220 std::vector<std::string> channels;
James Feist053a6642018-10-15 13:17:09 -0700221
222 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
223 keyPair++)
224 {
225 std::string subsituteString;
226
227 if (keyPair.key() == "Name" &&
228 keyPair.value().type() == nlohmann::json::value_t::string)
229 {
230 subsituteString = std::regex_replace(
Ed Tanous07d467b2021-02-23 14:48:37 -0800231 keyPair.value().get<std::string>(), illegalNameRegex, "_");
James Feist053a6642018-10-15 13:17:09 -0700232 name = subsituteString;
233 }
234 else
235 {
236 subsituteString = jsonToString(keyPair.value());
237 }
238
239 if (keyPair.key() == "Bus")
240 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700241 bus = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700242 }
243 else if (keyPair.key() == "Address")
244 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700245 address = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700246 }
Jonathan Doman6af72c92023-04-19 11:55:39 -0700247 else if (keyPair.key() == "ChannelNames" && type.ends_with("Mux"))
James Feist286babc2019-02-07 16:48:28 -0800248 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700249 channels = keyPair.value().get<std::vector<std::string>>();
James Feist286babc2019-02-07 16:48:28 -0800250 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800251 boost::replace_all(parameters, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700252 subsituteString);
Zev Weissc11b5da2022-07-12 16:31:37 -0700253 boost::replace_all(busPath, templateChar + keyPair.key(),
James Feist053a6642018-10-15 13:17:09 -0700254 subsituteString);
255 }
256
Jonathan Domand0eb1292023-04-19 11:21:56 -0700257 if (!bus || !address)
258 {
259 createDevice(busPath, parameters, constructor);
260 return;
261 }
262
Jonathan Doman6af72c92023-04-19 11:55:39 -0700263 buildDevice(name, busPath, parameters, *bus, *address, constructor,
Alexander Hansena555acf2025-06-27 11:59:10 +0200264 destructor, hasHWMonDir, std::move(channels), io);
James Feist053a6642018-10-15 13:17:09 -0700265}
266
Alexander Hansena555acf2025-06-27 11:59:10 +0200267bool loadOverlays(const nlohmann::json& systemConfiguration,
268 boost::asio::io_context& io)
James Feistc95cb142018-02-26 10:41:42 -0800269{
Ed Tanous07d467b2021-02-23 14:48:37 -0800270 std::filesystem::create_directory(outputDir);
James Feistc95cb142018-02-26 10:41:42 -0800271 for (auto entity = systemConfiguration.begin();
272 entity != systemConfiguration.end(); entity++)
273 {
James Feist1e3e6982018-08-03 16:09:28 -0700274 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800275 if (findExposes == entity.value().end() ||
276 findExposes->type() != nlohmann::json::value_t::array)
277 {
278 continue;
279 }
280
Ed Tanous3013fb42022-07-09 08:27:06 -0700281 for (const auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800282 {
James Feist1e3e6982018-08-03 16:09:28 -0700283 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800284 // status missing is assumed to be 'okay'
285 if (findStatus != configuration.end() && *findStatus == "disabled")
286 {
287 continue;
288 }
James Feistd63d18a2018-07-19 15:23:45 -0700289 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800290 if (findType == configuration.end() ||
291 findType->type() != nlohmann::json::value_t::string)
292 {
293 continue;
294 }
295 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700296 auto device = devices::exportTemplates.find(type.c_str());
297 if (device != devices::exportTemplates.end())
298 {
Alexander Hansena555acf2025-06-27 11:59:10 +0200299 exportDevice(type, device->second, configuration, io);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700300 continue;
301 }
302
303 // Because many devices are intentionally not exportable,
304 // this error message is not printed in all situations.
305 // If wondering why your device not appearing, add your type to
306 // the exportTemplates array in the devices.hpp file.
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200307 lg2::debug("Device type {TYPE} not found in export map allowlist",
308 "TYPE", type);
James Feistc95cb142018-02-26 10:41:42 -0800309 }
310 }
311
312 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700313}