blob: ce41bca74dc570bb212f243c242bc098587ed967 [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_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>
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
Jonathan Domand0eb1292023-04-19 11:21:56 -070056void linkMux(const std::string& 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
Jonathan Domand0eb1292023-04-19 11:21:56 -0700101static int deleteDevice(const std::string& busPath, uint64_t address,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700102 const std::string& destructor)
103{
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
Zev Weissc11b5da2022-07-12 16:31:37 -0700117static int createDevice(const std::string& busPath,
Johnathan Mantey9b867872020-10-13 15:00:51 -0700118 const std::string& parameters,
119 const std::string& constructor)
120{
Zev Weissc11b5da2022-07-12 16:31:37 -0700121 std::filesystem::path deviceConstructor(busPath);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700122 deviceConstructor /= constructor;
123 std::ofstream deviceFile(deviceConstructor);
124 if (!deviceFile.good())
125 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200126 lg2::error("Error writing {PATH}", "PATH", deviceConstructor.string());
Johnathan Mantey9b867872020-10-13 15:00:51 -0700127 return -1;
128 }
129 deviceFile << parameters;
130 deviceFile.close();
131
132 return 0;
133}
134
Jonathan Domand0eb1292023-04-19 11:21:56 -0700135static bool deviceIsCreated(const std::string& busPath, uint64_t bus,
136 uint64_t address,
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700137 const devices::createsHWMon hasHWMonDir)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700138{
Zev Weiss67003d62022-07-15 16:38:19 -0700139 std::filesystem::path dirPath = busPath;
Jonathan Domand0eb1292023-04-19 11:21:56 -0700140 dirPath /= deviceDirName(bus, address);
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700141 if (hasHWMonDir == devices::createsHWMon::hasHWMonDir)
Zev Weiss67003d62022-07-15 16:38:19 -0700142 {
143 dirPath /= "hwmon";
144 }
Johnathan Mantey9b867872020-10-13 15:00:51 -0700145
Ed Tanous37e142b2021-04-30 12:19:26 -0700146 std::error_code ec;
Zev Weiss67003d62022-07-15 16:38:19 -0700147 // Ignore errors; anything but a clean 'true' is just fine as 'false'
148 return std::filesystem::exists(dirPath, ec);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700149}
150
Patrick Williams5a807032025-03-03 11:20:39 -0500151static int buildDevice(
152 const std::string& name, const std::string& busPath,
153 const std::string& parameters, uint64_t bus, uint64_t address,
154 const std::string& constructor, const std::string& destructor,
155 const devices::createsHWMon hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200156 std::vector<std::string> channelNames, boost::asio::io_context& io,
157 const size_t retries = 5)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700158{
Ed Tanous3013fb42022-07-09 08:27:06 -0700159 if (retries == 0U)
Johnathan Mantey9b867872020-10-13 15:00:51 -0700160 {
161 return -1;
162 }
163
Jonathan Doman6af72c92023-04-19 11:55:39 -0700164 // If it's already instantiated, we don't need to create it again.
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700165 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
Johnathan Mantey9b867872020-10-13 15:00:51 -0700166 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700167 // Try to create the device
168 createDevice(busPath, parameters, constructor);
Zev Weisscbcd17f2022-07-15 15:39:21 -0700169
Jonathan Doman6af72c92023-04-19 11:55:39 -0700170 // If it didn't work, delete it and try again in 500ms
171 if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
172 {
173 deleteDevice(busPath, address, destructor);
174
175 std::shared_ptr<boost::asio::steady_timer> createTimer =
176 std::make_shared<boost::asio::steady_timer>(io);
177 createTimer->expires_after(std::chrono::milliseconds(500));
178 createTimer->async_wait(
179 [createTimer, name, busPath, parameters, bus, address,
180 constructor, destructor, hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200181 channelNames(std::move(channelNames)), retries,
182 &io](const boost::system::error_code& ec) mutable {
Patrick Williamsb7077432024-08-16 15:22:21 -0400183 if (ec)
184 {
Alexander Hansen8feb0452025-09-15 14:29:20 +0200185 lg2::error("Timer error: {ERR}", "ERR", ec.message());
Patrick Williamsb7077432024-08-16 15:22:21 -0400186 return -2;
187 }
188 return buildDevice(name, busPath, parameters, bus, address,
189 constructor, destructor, hasHWMonDir,
Alexander Hansena555acf2025-06-27 11:59:10 +0200190 std::move(channelNames), io,
191 retries - 1);
Patrick Williamsb7077432024-08-16 15:22:21 -0400192 });
Jonathan Doman6af72c92023-04-19 11:55:39 -0700193 return -1;
194 }
195 }
196
197 // Link the mux channels if needed once the device is created.
198 if (!channelNames.empty())
199 {
200 linkMux(name, bus, address, channelNames);
Johnathan Mantey9b867872020-10-13 15:00:51 -0700201 }
Zev Weisscbcd17f2022-07-15 15:39:21 -0700202
Johnathan Mantey9b867872020-10-13 15:00:51 -0700203 return 0;
204}
205
James Feista465ccc2019-02-08 12:51:01 -0800206void exportDevice(const std::string& type,
207 const devices::ExportTemplate& exportTemplate,
Alexander Hansena555acf2025-06-27 11:59:10 +0200208 const nlohmann::json& configuration,
209 boost::asio::io_context& io)
James Feist053a6642018-10-15 13:17:09 -0700210{
James Feist053a6642018-10-15 13:17:09 -0700211 std::string parameters = exportTemplate.parameters;
Zev Weissc11b5da2022-07-12 16:31:37 -0700212 std::string busPath = exportTemplate.busPath;
Johnathan Mantey9b867872020-10-13 15:00:51 -0700213 std::string constructor = exportTemplate.add;
214 std::string destructor = exportTemplate.remove;
Johnathan Mantey7b21ef22022-08-08 12:57:55 -0700215 devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir;
James Feist053a6642018-10-15 13:17:09 -0700216 std::string name = "unknown";
Jonathan Domand0eb1292023-04-19 11:21:56 -0700217 std::optional<uint64_t> bus;
218 std::optional<uint64_t> address;
Jonathan Doman6af72c92023-04-19 11:55:39 -0700219 std::vector<std::string> channels;
James Feist053a6642018-10-15 13:17:09 -0700220
221 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
222 keyPair++)
223 {
224 std::string subsituteString;
225
226 if (keyPair.key() == "Name" &&
227 keyPair.value().type() == nlohmann::json::value_t::string)
228 {
229 subsituteString = std::regex_replace(
Ed Tanous07d467b2021-02-23 14:48:37 -0800230 keyPair.value().get<std::string>(), illegalNameRegex, "_");
James Feist053a6642018-10-15 13:17:09 -0700231 name = subsituteString;
232 }
233 else
234 {
235 subsituteString = jsonToString(keyPair.value());
236 }
237
238 if (keyPair.key() == "Bus")
239 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700240 bus = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700241 }
242 else if (keyPair.key() == "Address")
243 {
Jonathan Domand0eb1292023-04-19 11:21:56 -0700244 address = keyPair.value().get<uint64_t>();
James Feist053a6642018-10-15 13:17:09 -0700245 }
Jonathan Doman6af72c92023-04-19 11:55:39 -0700246 else if (keyPair.key() == "ChannelNames" && type.ends_with("Mux"))
James Feist286babc2019-02-07 16:48:28 -0800247 {
Jonathan Doman6af72c92023-04-19 11:55:39 -0700248 channels = keyPair.value().get<std::vector<std::string>>();
James Feist286babc2019-02-07 16:48:28 -0800249 }
George Liu5a61ec82025-08-25 11:16:44 +0800250 replaceAll(parameters, templateChar + keyPair.key(), subsituteString);
251 replaceAll(busPath, templateChar + keyPair.key(), subsituteString);
James Feist053a6642018-10-15 13:17:09 -0700252 }
253
Jonathan Domand0eb1292023-04-19 11:21:56 -0700254 if (!bus || !address)
255 {
256 createDevice(busPath, parameters, constructor);
257 return;
258 }
259
Jonathan Doman6af72c92023-04-19 11:55:39 -0700260 buildDevice(name, busPath, parameters, *bus, *address, constructor,
Alexander Hansena555acf2025-06-27 11:59:10 +0200261 destructor, hasHWMonDir, std::move(channels), io);
James Feist053a6642018-10-15 13:17:09 -0700262}
263
Alexander Hansena555acf2025-06-27 11:59:10 +0200264bool loadOverlays(const nlohmann::json& systemConfiguration,
265 boost::asio::io_context& io)
James Feistc95cb142018-02-26 10:41:42 -0800266{
Ed Tanous07d467b2021-02-23 14:48:37 -0800267 std::filesystem::create_directory(outputDir);
James Feistc95cb142018-02-26 10:41:42 -0800268 for (auto entity = systemConfiguration.begin();
269 entity != systemConfiguration.end(); entity++)
270 {
James Feist1e3e6982018-08-03 16:09:28 -0700271 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800272 if (findExposes == entity.value().end() ||
273 findExposes->type() != nlohmann::json::value_t::array)
274 {
275 continue;
276 }
277
Ed Tanous3013fb42022-07-09 08:27:06 -0700278 for (const auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800279 {
James Feist1e3e6982018-08-03 16:09:28 -0700280 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800281 // status missing is assumed to be 'okay'
282 if (findStatus != configuration.end() && *findStatus == "disabled")
283 {
284 continue;
285 }
James Feistd63d18a2018-07-19 15:23:45 -0700286 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800287 if (findType == configuration.end() ||
288 findType->type() != nlohmann::json::value_t::string)
289 {
290 continue;
291 }
292 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700293 auto device = devices::exportTemplates.find(type.c_str());
294 if (device != devices::exportTemplates.end())
295 {
Alexander Hansena555acf2025-06-27 11:59:10 +0200296 exportDevice(type, device->second, configuration, io);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700297 continue;
298 }
299
300 // Because many devices are intentionally not exportable,
301 // this error message is not printed in all situations.
302 // If wondering why your device not appearing, add your type to
303 // the exportTemplates array in the devices.hpp file.
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200304 lg2::debug("Device type {TYPE} not found in export map allowlist",
305 "TYPE", type);
James Feistc95cb142018-02-26 10:41:42 -0800306 }
307 }
308
309 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700310}