blob: 60e9af654d975259a56c2058e81257c179b084e8 [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
George Liu5a61ec82025-08-25 11:16:44 +08006#include "../utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -07007#include "devices.hpp"
Christopher Meis59ef1e72025-04-16 08:53:25 +02008#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -07009
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_set.hpp>
James Feist8c505da2020-05-28 10:06:33 -070013#include <nlohmann/json.hpp>
Alexander Hansenc3db2c32024-08-20 15:01:38 +020014#include <phosphor-logging/lg2.hpp>
James Feist8c505da2020-05-28 10:06:33 -070015
James Feist637b3ef2019-04-15 16:35:30 -070016#include <filesystem>
Ed Tanousdbf95b22025-10-13 11:38:35 -070017#include <flat_map>
Christopher Meis59ef1e72025-04-16 08:53:25 +020018#include <fstream>
James Feista465ccc2019-02-08 12:51:01 -080019#include <iomanip>
James Feista465ccc2019-02-08 12:51:01 -080020#include <regex>
21#include <string>
James Feistc95cb142018-02-26 10:41:42 -080022
Ed Tanous07d467b2021-02-23 14:48:37 -080023constexpr const char* outputDir = "/tmp/overlays";
24constexpr const char* templateChar = "$";
25constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices";
26constexpr const char* muxSymlinkDir = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080027
Ed Tanousfc171422024-04-04 17:18:16 -070028const std::regex illegalNameRegex("[^A-Za-z0-9_]");
James Feistc95cb142018-02-26 10:41:42 -080029
James Feistc95cb142018-02-26 10:41:42 -080030// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080031std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080032{
33 if (in.type() == nlohmann::json::value_t::string)
34 {
35 return in.get<std::string>();
36 }
Ed Tanous07d467b2021-02-23 14:48:37 -080037 if (in.type() == nlohmann::json::value_t::array)
James Feistc95cb142018-02-26 10:41:42 -080038 {
39 // remove brackets and comma from array
40 std::string array = in.dump();
41 array = array.substr(1, array.size() - 2);
George Liu5a61ec82025-08-25 11:16:44 +080042 std::ranges::replace(array, ',', ' ');
James Feistc95cb142018-02-26 10:41:42 -080043 return array;
44 }
45 return in.dump();
46}
47
Zev Weiss9afef3a2022-07-13 16:38:23 -070048static std::string deviceDirName(uint64_t bus, uint64_t address)
49{
50 std::ostringstream name;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -070051 name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
52 << address;
Zev Weiss9afef3a2022-07-13 16:38:23 -070053 return name.str();
54}
55
Ed Tanous250432b2025-10-13 13:28:38 -070056void linkMux(std::string_view muxName, uint64_t busIndex, uint64_t address,
Jonathan Doman6af72c92023-04-19 11:55:39 -070057 const std::vector<std::string>& channelNames)
James Feistc95cb142018-02-26 10:41:42 -080058{
James Feist286babc2019-02-07 16:48:28 -080059 std::error_code ec;
Ed Tanous07d467b2021-02-23 14:48:37 -080060 std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
61 std::filesystem::create_directory(muxSymlinkDirPath, ec);
Ed Tanousee3357a2019-02-26 12:44:14 -080062 // ignore error codes here if the directory already exists
63 ec.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -080064 std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
Ed Tanousee3357a2019-02-26 12:44:14 -080065 std::filesystem::create_directory(linkDir, ec);
66
Ed Tanous07d467b2021-02-23 14:48:37 -080067 std::filesystem::path devDir(i2CDevsDir);
Zev Weiss9afef3a2022-07-13 16:38:23 -070068 devDir /= deviceDirName(busIndex, address);
James Feist286babc2019-02-07 16:48:28 -080069
Ed Tanousee3357a2019-02-26 12:44:14 -080070 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
71 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -080072 {
Jonathan Doman6af72c92023-04-19 11:55:39 -070073 const std::string& channelName = channelNames[channelIndex];
74 if (channelName.empty())
Ed Tanousee3357a2019-02-26 12:44:14 -080075 {
76 continue;
77 }
78
79 std::filesystem::path channelPath =
80 devDir / ("channel-" + std::to_string(channelIndex));
81 if (!is_symlink(channelPath))
82 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020083 lg2::error("{PATH} for mux channel {CHANNEL} doesn't exist!",
84 "PATH", channelPath.string(), "CHANNEL", channelName);
Ed Tanousee3357a2019-02-26 12:44:14 -080085 continue;
86 }
87 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
88
89 std::filesystem::path fp("/dev" / bus.filename());
Jonathan Doman6af72c92023-04-19 11:55:39 -070090 std::filesystem::path link(linkDir / channelName);
Ed Tanousee3357a2019-02-26 12:44:14 -080091
92 std::filesystem::create_symlink(fp, link, ec);
93 if (ec)
94 {
Alexander Hansen8feb0452025-09-15 14:29:20 +020095 lg2::error("Failure creating symlink for {PATH} to {LINK}", "PATH",
96 fp.string(), "LINK", link.string());
Ed Tanousee3357a2019-02-26 12:44:14 -080097 }
James Feistc95cb142018-02-26 10:41:42 -080098 }
99}
100
Ed Tanous250432b2025-10-13 13:28:38 -0700101static int deleteDevice(std::string_view busPath, uint64_t address,
102 std::string_view destructor)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700103{
Zev Weissc11b5da2022-07-12 16:31:37 -0700104 std::filesystem::path deviceDestructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700105 deviceDestructor /= destructor;
106 std::ofstream deviceFile(deviceDestructor);
107 if (!deviceFile.good())
108 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200109 lg2::error("Error writing {PATH}", "PATH", deviceDestructor.string());
Johnathan Mantey9b867872020-10-13 15:00:51 -0700110 return -1;
111 }
Jonathan Domand0eb1292023-04-19 11:21:56 -0700112 deviceFile << std::to_string(address);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700113 deviceFile.close();
114 return 0;
115}
116
Ed Tanous250432b2025-10-13 13:28:38 -0700117static int createDevice(std::string_view busPath, std::string_view parameters,
118 std::string_view constructor)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700119{
Zev Weissc11b5da2022-07-12 16:31:37 -0700120 std::filesystem::path deviceConstructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700121 deviceConstructor /= constructor;
122 std::ofstream deviceFile(deviceConstructor);
123 if (!deviceFile.good())
124 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200125 lg2::error("Error writing {PATH}", "PATH", deviceConstructor.string());
Johnathan Mantey9b867872020-10-13 15:00:51 -0700126 return -1;
127 }
128 deviceFile << parameters;
129 deviceFile.close();
130
131 return 0;
132}
133
Ed Tanous250432b2025-10-13 13:28:38 -0700134static bool deviceIsCreated(std::string_view busPath, uint64_t bus,
Jonathan Domand0eb1292023-04-19 11:21:56 -0700135 uint64_t address,
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700136 const devices::createsHWMon hasHWMonDir)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700137{
Zev Weiss67003d62022-07-15 16:38:19 -0700138 std::filesystem::path dirPath = busPath;
Jonathan Domand0eb1292023-04-19 11:21:56 -0700139 dirPath /= deviceDirName(bus, address);
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700140 if (hasHWMonDir == devices::createsHWMon::hasHWMonDir)
Zev Weiss67003d62022-07-15 16:38:19 -0700141 {
142 dirPath /= "hwmon";
143 }
Johnathan Mantey9b867872020-10-13 15:00:51 -0700144
Ed Tanous37e142b2021-04-30 12:19:26 -0700145 std::error_code ec;
Zev Weiss67003d62022-07-15 16:38:19 -0700146 // Ignore errors; anything but a clean 'true' is just fine as 'false'
147 return std::filesystem::exists(dirPath, ec);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700148}
149
Patrick Williams5a807032025-03-03 11:20:39 -0500150static int buildDevice(
Ed Tanous250432b2025-10-13 13:28:38 -0700151 std::string_view name, std::string_view busPath,
152 std::string_view parameters, uint64_t bus, uint64_t address,
153 std::string_view constructor, std::string_view destructor,
Patrick Williams5a807032025-03-03 11:20:39 -0500154 const devices::createsHWMon hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200155 std::vector<std::string> channelNames, boost::asio::io_context& io,
156 const size_t retries = 5)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700157{
Ed Tanous3013fb42022-07-09 08:27:06 -0700158 if (retries == 0U)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700159 {
160 return -1;
161 }
162
Jonathan Doman6af72c92023-04-19 11:55:39 -0700163 // If it's already instantiated, we don't need to create it again.
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700164 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700165 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700166 // Try to create the device
167 createDevice(busPath, parameters, constructor);
Zev Weisscbcd17f2022-07-15 15:39:21 -0700168
Jonathan Doman6af72c92023-04-19 11:55:39 -0700169 // If it didn't work, delete it and try again in 500ms
170 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
171 {
172 deleteDevice(busPath, address, destructor);
173
174 std::shared_ptr<boost::asio::steady_timer> createTimer =
175 std::make_shared<boost::asio::steady_timer>(io);
176 createTimer->expires_after(std::chrono::milliseconds(500));
177 createTimer->async_wait(
178 [createTimer, name, busPath, parameters, bus, address,
179 constructor, destructor, hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200180 channelNames(std::move(channelNames)), retries,
181 &io](const boost::system::error_code& ec) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400182 if (ec)
183 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200184 lg2::error("Timer error: {ERR}", "ERR", ec.message());
Patrick Williamsb7077432024-08-16 15:22:21 -0400185 return -2;
186 }
187 return buildDevice(name, busPath, parameters, bus, address,
188 constructor, destructor, hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200189 std::move(channelNames), io,
190 retries - 1);
Patrick Williamsb7077432024-08-16 15:22:21 -0400191 });
Jonathan Doman6af72c92023-04-19 11:55:39 -0700192 return -1;
193 }
194 }
195
196 // Link the mux channels if needed once the device is created.
197 if (!channelNames.empty())
198 {
199 linkMux(name, bus, address, channelNames);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700200 }
Zev Weisscbcd17f2022-07-15 15:39:21 -0700201
Johnathan Mantey9b867872020-10-13 15:00:51 -0700202 return 0;
203}
204
Ed Tanous250432b2025-10-13 13:28:38 -0700205void exportDevice(const devices::ExportTemplate& exportTemplate,
Alexander Hansena555acf2025-06-27 11:59:10 +0200206 const nlohmann::json& configuration,
207 boost::asio::io_context& io)
James Feist053a6642018-10-15 13:17:09 -0700208{
Ed Tanous250432b2025-10-13 13:28:38 -0700209 std::string_view type = exportTemplate.type;
210 std::string parameters(exportTemplate.parameters);
211 std::string busPath(exportTemplate.busPath);
212 std::string_view constructor = exportTemplate.add;
213 std::string_view destructor = exportTemplate.remove;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700214 devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir;
James Feist053a6642018-10-15 13:17:09 -0700215 std::string name = "unknown";
Jonathan Domand0eb1292023-04-19 11:21:56 -0700216 std::optional<uint64_t> bus;
217 std::optional<uint64_t> address;
Jonathan Doman6af72c92023-04-19 11:55:39 -0700218 std::vector<std::string> channels;
James Feist053a6642018-10-15 13:17:09 -0700219
220 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
221 keyPair++)
222 {
223 std::string subsituteString;
224
225 if (keyPair.key() == "Name" &&
226 keyPair.value().type() == nlohmann::json::value_t::string)
227 {
228 subsituteString = std::regex_replace(
Ed Tanous07d467b2021-02-23 14:48:37 -0800229 keyPair.value().get<std::string>(), illegalNameRegex, "_");
James Feist053a6642018-10-15 13:17:09 -0700230 name = subsituteString;
231 }
232 else
233 {
234 subsituteString = jsonToString(keyPair.value());
235 }
236
237 if (keyPair.key() == "Bus")
238 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700239 bus = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700240 }
241 else if (keyPair.key() == "Address")
242 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700243 address = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700244 }
Jonathan Doman6af72c92023-04-19 11:55:39 -0700245 else if (keyPair.key() == "ChannelNames" && type.ends_with("Mux"))
James Feist286babc2019-02-07 16:48:28 -0800246 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700247 channels = keyPair.value().get<std::vector<std::string>>();
James Feist286babc2019-02-07 16:48:28 -0800248 }
George Liu5a61ec82025-08-25 11:16:44 +0800249 replaceAll(parameters, templateChar + keyPair.key(), subsituteString);
250 replaceAll(busPath, templateChar + keyPair.key(), subsituteString);
James Feist053a6642018-10-15 13:17:09 -0700251 }
252
Jonathan Domand0eb1292023-04-19 11:21:56 -0700253 if (!bus || !address)
254 {
255 createDevice(busPath, parameters, constructor);
256 return;
257 }
258
Jonathan Doman6af72c92023-04-19 11:55:39 -0700259 buildDevice(name, busPath, parameters, *bus, *address, constructor,
Alexander Hansena555acf2025-06-27 11:59:10 +0200260 destructor, hasHWMonDir, std::move(channels), io);
James Feist053a6642018-10-15 13:17:09 -0700261}
262
Alexander Hansena555acf2025-06-27 11:59:10 +0200263bool loadOverlays(const nlohmann::json& systemConfiguration,
264 boost::asio::io_context& io)
James Feistc95cb142018-02-26 10:41:42 -0800265{
Ed Tanous07d467b2021-02-23 14:48:37 -0800266 std::filesystem::create_directory(outputDir);
James Feistc95cb142018-02-26 10:41:42 -0800267 for (auto entity = systemConfiguration.begin();
268 entity != systemConfiguration.end(); entity++)
269 {
James Feist1e3e6982018-08-03 16:09:28 -0700270 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800271 if (findExposes == entity.value().end() ||
272 findExposes->type() != nlohmann::json::value_t::array)
273 {
274 continue;
275 }
276
Ed Tanous3013fb42022-07-09 08:27:06 -0700277 for (const auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800278 {
James Feist1e3e6982018-08-03 16:09:28 -0700279 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800280 // status missing is assumed to be 'okay'
281 if (findStatus != configuration.end() && *findStatus == "disabled")
282 {
283 continue;
284 }
James Feistd63d18a2018-07-19 15:23:45 -0700285 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800286 if (findType == configuration.end() ||
287 findType->type() != nlohmann::json::value_t::string)
288 {
289 continue;
290 }
Ed Tanous250432b2025-10-13 13:28:38 -0700291 const std::string& type = findType.value().get<std::string>();
292 const auto* device = std::ranges::find_if(
293 devices::exportTemplates,
294 [&type](const auto& tmp) { return tmp.type == type; });
James Feist053a6642018-10-15 13:17:09 -0700295 if (device != devices::exportTemplates.end())
296 {
Ed Tanous250432b2025-10-13 13:28:38 -0700297 exportDevice(*device, configuration, io);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700298 continue;
299 }
300
301 // Because many devices are intentionally not exportable,
302 // this error message is not printed in all situations.
303 // If wondering why your device not appearing, add your type to
304 // the exportTemplates array in the devices.hpp file.
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200305 lg2::debug("Device type {TYPE} not found in export map allowlist",
306 "TYPE", type);
James Feistc95cb142018-02-26 10:41:42 -0800307 }
308 }
309
310 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700311}