xref: /openbmc/entity-manager/src/entity_manager/overlay.cpp (revision 064d8aff8ea9c499be27679cb5c79c6ae25d261b)
14e1142d6SAlexander Hansen // SPDX-License-Identifier: Apache-2.0
24e1142d6SAlexander Hansen // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
3fc9e7fdaSChristopher Meis 
4fc9e7fdaSChristopher Meis #include "overlay.hpp"
5fc9e7fdaSChristopher Meis 
65a61ec86SGeorge Liu #include "../utils.hpp"
7fc9e7fdaSChristopher Meis #include "devices.hpp"
859ef1e72SChristopher Meis #include "utils.hpp"
9fc9e7fdaSChristopher Meis 
10fc9e7fdaSChristopher Meis #include <boost/asio/io_context.hpp>
11fc9e7fdaSChristopher Meis #include <boost/asio/steady_timer.hpp>
12fc9e7fdaSChristopher Meis #include <boost/container/flat_set.hpp>
13fc9e7fdaSChristopher Meis #include <nlohmann/json.hpp>
14fc9e7fdaSChristopher Meis #include <phosphor-logging/lg2.hpp>
15fc9e7fdaSChristopher Meis 
16fc9e7fdaSChristopher Meis #include <filesystem>
17dbf95b2cSEd Tanous #include <flat_map>
1859ef1e72SChristopher Meis #include <fstream>
19fc9e7fdaSChristopher Meis #include <iomanip>
20fc9e7fdaSChristopher Meis #include <regex>
21fc9e7fdaSChristopher Meis #include <string>
22fc9e7fdaSChristopher Meis 
23fc9e7fdaSChristopher Meis constexpr const char* outputDir = "/tmp/overlays";
24fc9e7fdaSChristopher Meis constexpr const char* templateChar = "$";
25fc9e7fdaSChristopher Meis constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices";
26fc9e7fdaSChristopher Meis constexpr const char* muxSymlinkDir = "/dev/i2c-mux";
27fc9e7fdaSChristopher Meis 
28fc9e7fdaSChristopher Meis const std::regex illegalNameRegex("[^A-Za-z0-9_]");
29fc9e7fdaSChristopher Meis 
30fc9e7fdaSChristopher Meis // helper function to make json types into string
jsonToString(const nlohmann::json & in)31fc9e7fdaSChristopher Meis std::string jsonToString(const nlohmann::json& in)
32fc9e7fdaSChristopher Meis {
33fc9e7fdaSChristopher Meis     if (in.type() == nlohmann::json::value_t::string)
34fc9e7fdaSChristopher Meis     {
35fc9e7fdaSChristopher Meis         return in.get<std::string>();
36fc9e7fdaSChristopher Meis     }
37fc9e7fdaSChristopher Meis     if (in.type() == nlohmann::json::value_t::array)
38fc9e7fdaSChristopher Meis     {
39fc9e7fdaSChristopher Meis         // remove brackets and comma from array
40fc9e7fdaSChristopher Meis         std::string array = in.dump();
41fc9e7fdaSChristopher Meis         array = array.substr(1, array.size() - 2);
425a61ec86SGeorge Liu         std::ranges::replace(array, ',', ' ');
43fc9e7fdaSChristopher Meis         return array;
44fc9e7fdaSChristopher Meis     }
45fc9e7fdaSChristopher Meis     return in.dump();
46fc9e7fdaSChristopher Meis }
47fc9e7fdaSChristopher Meis 
deviceDirName(uint64_t bus,uint64_t address)48fc9e7fdaSChristopher Meis static std::string deviceDirName(uint64_t bus, uint64_t address)
49fc9e7fdaSChristopher Meis {
50fc9e7fdaSChristopher Meis     std::ostringstream name;
51fc9e7fdaSChristopher Meis     name << bus << "-" << std::hex << std::setw(4) << std::setfill('0')
52fc9e7fdaSChristopher Meis          << address;
53fc9e7fdaSChristopher Meis     return name.str();
54fc9e7fdaSChristopher Meis }
55fc9e7fdaSChristopher Meis 
linkMux(std::string_view muxName,uint64_t busIndex,uint64_t address,const std::vector<std::string> & channelNames)56250432b3SEd Tanous void linkMux(std::string_view muxName, uint64_t busIndex, uint64_t address,
57fc9e7fdaSChristopher Meis              const std::vector<std::string>& channelNames)
58fc9e7fdaSChristopher Meis {
59fc9e7fdaSChristopher Meis     std::error_code ec;
60fc9e7fdaSChristopher Meis     std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
61fc9e7fdaSChristopher Meis     std::filesystem::create_directory(muxSymlinkDirPath, ec);
62fc9e7fdaSChristopher Meis     // ignore error codes here if the directory already exists
63fc9e7fdaSChristopher Meis     ec.clear();
64fc9e7fdaSChristopher Meis     std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
65fc9e7fdaSChristopher Meis     std::filesystem::create_directory(linkDir, ec);
66fc9e7fdaSChristopher Meis 
67fc9e7fdaSChristopher Meis     std::filesystem::path devDir(i2CDevsDir);
68fc9e7fdaSChristopher Meis     devDir /= deviceDirName(busIndex, address);
69fc9e7fdaSChristopher Meis 
70fc9e7fdaSChristopher Meis     for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
71fc9e7fdaSChristopher Meis          channelIndex++)
72fc9e7fdaSChristopher Meis     {
73fc9e7fdaSChristopher Meis         const std::string& channelName = channelNames[channelIndex];
74fc9e7fdaSChristopher Meis         if (channelName.empty())
75fc9e7fdaSChristopher Meis         {
76fc9e7fdaSChristopher Meis             continue;
77fc9e7fdaSChristopher Meis         }
78fc9e7fdaSChristopher Meis 
79fc9e7fdaSChristopher Meis         std::filesystem::path channelPath =
80fc9e7fdaSChristopher Meis             devDir / ("channel-" + std::to_string(channelIndex));
81fc9e7fdaSChristopher Meis         if (!is_symlink(channelPath))
82fc9e7fdaSChristopher Meis         {
838feb0454SAlexander Hansen             lg2::error("{PATH} for mux channel {CHANNEL} doesn't exist!",
848feb0454SAlexander Hansen                        "PATH", channelPath.string(), "CHANNEL", channelName);
85fc9e7fdaSChristopher Meis             continue;
86fc9e7fdaSChristopher Meis         }
87fc9e7fdaSChristopher Meis         std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
88fc9e7fdaSChristopher Meis 
89fc9e7fdaSChristopher Meis         std::filesystem::path fp("/dev" / bus.filename());
90fc9e7fdaSChristopher Meis         std::filesystem::path link(linkDir / channelName);
91fc9e7fdaSChristopher Meis 
92fc9e7fdaSChristopher Meis         std::filesystem::create_symlink(fp, link, ec);
93fc9e7fdaSChristopher Meis         if (ec)
94fc9e7fdaSChristopher Meis         {
958feb0454SAlexander Hansen             lg2::error("Failure creating symlink for {PATH} to {LINK}", "PATH",
968feb0454SAlexander Hansen                        fp.string(), "LINK", link.string());
97fc9e7fdaSChristopher Meis         }
98fc9e7fdaSChristopher Meis     }
99fc9e7fdaSChristopher Meis }
100fc9e7fdaSChristopher Meis 
deleteDevice(std::string_view busPath,uint64_t address,std::string_view destructor)101250432b3SEd Tanous static int deleteDevice(std::string_view busPath, uint64_t address,
102250432b3SEd Tanous                         std::string_view destructor)
103fc9e7fdaSChristopher Meis {
104fc9e7fdaSChristopher Meis     std::filesystem::path deviceDestructor(busPath);
105fc9e7fdaSChristopher Meis     deviceDestructor /= destructor;
106fc9e7fdaSChristopher Meis     std::ofstream deviceFile(deviceDestructor);
107fc9e7fdaSChristopher Meis     if (!deviceFile.good())
108fc9e7fdaSChristopher Meis     {
1098feb0454SAlexander Hansen         lg2::error("Error writing {PATH}", "PATH", deviceDestructor.string());
110fc9e7fdaSChristopher Meis         return -1;
111fc9e7fdaSChristopher Meis     }
112fc9e7fdaSChristopher Meis     deviceFile << std::to_string(address);
113fc9e7fdaSChristopher Meis     deviceFile.close();
114fc9e7fdaSChristopher Meis     return 0;
115fc9e7fdaSChristopher Meis }
116fc9e7fdaSChristopher Meis 
createDevice(std::string_view busPath,std::string_view parameters,std::string_view constructor)117250432b3SEd Tanous static int createDevice(std::string_view busPath, std::string_view parameters,
118250432b3SEd Tanous                         std::string_view constructor)
119fc9e7fdaSChristopher Meis {
120fc9e7fdaSChristopher Meis     std::filesystem::path deviceConstructor(busPath);
121fc9e7fdaSChristopher Meis     deviceConstructor /= constructor;
122fc9e7fdaSChristopher Meis     std::ofstream deviceFile(deviceConstructor);
123fc9e7fdaSChristopher Meis     if (!deviceFile.good())
124fc9e7fdaSChristopher Meis     {
1258feb0454SAlexander Hansen         lg2::error("Error writing {PATH}", "PATH", deviceConstructor.string());
126fc9e7fdaSChristopher Meis         return -1;
127fc9e7fdaSChristopher Meis     }
128fc9e7fdaSChristopher Meis     deviceFile << parameters;
129fc9e7fdaSChristopher Meis     deviceFile.close();
130fc9e7fdaSChristopher Meis 
131fc9e7fdaSChristopher Meis     return 0;
132fc9e7fdaSChristopher Meis }
133fc9e7fdaSChristopher Meis 
deviceIsCreated(std::string_view busPath,uint64_t bus,uint64_t address,const devices::createsHWMon hasHWMonDir)134250432b3SEd Tanous static bool deviceIsCreated(std::string_view busPath, uint64_t bus,
135fc9e7fdaSChristopher Meis                             uint64_t address,
136fc9e7fdaSChristopher Meis                             const devices::createsHWMon hasHWMonDir)
137fc9e7fdaSChristopher Meis {
138fc9e7fdaSChristopher Meis     std::filesystem::path dirPath = busPath;
139fc9e7fdaSChristopher Meis     dirPath /= deviceDirName(bus, address);
140fc9e7fdaSChristopher Meis     if (hasHWMonDir == devices::createsHWMon::hasHWMonDir)
141fc9e7fdaSChristopher Meis     {
142fc9e7fdaSChristopher Meis         dirPath /= "hwmon";
143fc9e7fdaSChristopher Meis     }
144fc9e7fdaSChristopher Meis 
145fc9e7fdaSChristopher Meis     std::error_code ec;
146fc9e7fdaSChristopher Meis     // Ignore errors; anything but a clean 'true' is just fine as 'false'
147fc9e7fdaSChristopher Meis     return std::filesystem::exists(dirPath, ec);
148fc9e7fdaSChristopher Meis }
149fc9e7fdaSChristopher Meis 
buildDevice(std::string_view name,std::string_view busPath,std::string_view parameters,uint64_t bus,uint64_t address,std::string_view constructor,std::string_view destructor,const devices::createsHWMon hasHWMonDir,std::vector<std::string> channelNames,boost::asio::io_context & io,const size_t retries=5)150fc9e7fdaSChristopher Meis static int buildDevice(
151250432b3SEd Tanous     std::string_view name, std::string_view busPath,
152250432b3SEd Tanous     std::string_view parameters, uint64_t bus, uint64_t address,
153250432b3SEd Tanous     std::string_view constructor, std::string_view destructor,
154fc9e7fdaSChristopher Meis     const devices::createsHWMon hasHWMonDir,
155a555acf0SAlexander Hansen     std::vector<std::string> channelNames, boost::asio::io_context& io,
156a555acf0SAlexander Hansen     const size_t retries = 5)
157fc9e7fdaSChristopher Meis {
158fc9e7fdaSChristopher Meis     if (retries == 0U)
159fc9e7fdaSChristopher Meis     {
160fc9e7fdaSChristopher Meis         return -1;
161fc9e7fdaSChristopher Meis     }
162fc9e7fdaSChristopher Meis 
163*064d8affSAlexander Hansen     lg2::debug("try to build device {NAME} at bus={BUS}, address={ADDR}",
164*064d8affSAlexander Hansen                "NAME", name, "BUS", bus, "ADDR", address);
165*064d8affSAlexander Hansen 
166fc9e7fdaSChristopher Meis     // If it's already instantiated, we don't need to create it again.
167fc9e7fdaSChristopher Meis     if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
168fc9e7fdaSChristopher Meis     {
169fc9e7fdaSChristopher Meis         // Try to create the device
170fc9e7fdaSChristopher Meis         createDevice(busPath, parameters, constructor);
171fc9e7fdaSChristopher Meis 
172fc9e7fdaSChristopher Meis         // If it didn't work, delete it and try again in 500ms
173fc9e7fdaSChristopher Meis         if (!deviceIsCreated(busPath, bus, address, hasHWMonDir))
174fc9e7fdaSChristopher Meis         {
175fc9e7fdaSChristopher Meis             deleteDevice(busPath, address, destructor);
176fc9e7fdaSChristopher Meis 
177fc9e7fdaSChristopher Meis             std::shared_ptr<boost::asio::steady_timer> createTimer =
178fc9e7fdaSChristopher Meis                 std::make_shared<boost::asio::steady_timer>(io);
179fc9e7fdaSChristopher Meis             createTimer->expires_after(std::chrono::milliseconds(500));
180fc9e7fdaSChristopher Meis             createTimer->async_wait(
181fc9e7fdaSChristopher Meis                 [createTimer, name, busPath, parameters, bus, address,
182fc9e7fdaSChristopher Meis                  constructor, destructor, hasHWMonDir,
183a555acf0SAlexander Hansen                  channelNames(std::move(channelNames)), retries,
184a555acf0SAlexander Hansen                  &io](const boost::system::error_code& ec) mutable {
185fc9e7fdaSChristopher Meis                     if (ec)
186fc9e7fdaSChristopher Meis                     {
1878feb0454SAlexander Hansen                         lg2::error("Timer error: {ERR}", "ERR", ec.message());
188fc9e7fdaSChristopher Meis                         return -2;
189fc9e7fdaSChristopher Meis                     }
190fc9e7fdaSChristopher Meis                     return buildDevice(name, busPath, parameters, bus, address,
191fc9e7fdaSChristopher Meis                                        constructor, destructor, hasHWMonDir,
192a555acf0SAlexander Hansen                                        std::move(channelNames), io,
193a555acf0SAlexander Hansen                                        retries - 1);
194fc9e7fdaSChristopher Meis                 });
195fc9e7fdaSChristopher Meis             return -1;
196fc9e7fdaSChristopher Meis         }
197fc9e7fdaSChristopher Meis     }
198fc9e7fdaSChristopher Meis 
199fc9e7fdaSChristopher Meis     // Link the mux channels if needed once the device is created.
200fc9e7fdaSChristopher Meis     if (!channelNames.empty())
201fc9e7fdaSChristopher Meis     {
202fc9e7fdaSChristopher Meis         linkMux(name, bus, address, channelNames);
203fc9e7fdaSChristopher Meis     }
204fc9e7fdaSChristopher Meis 
205fc9e7fdaSChristopher Meis     return 0;
206fc9e7fdaSChristopher Meis }
207fc9e7fdaSChristopher Meis 
exportDevice(const devices::ExportTemplate & exportTemplate,const nlohmann::json & configuration,boost::asio::io_context & io)208250432b3SEd Tanous void exportDevice(const devices::ExportTemplate& exportTemplate,
209a555acf0SAlexander Hansen                   const nlohmann::json& configuration,
210a555acf0SAlexander Hansen                   boost::asio::io_context& io)
211fc9e7fdaSChristopher Meis {
212250432b3SEd Tanous     std::string_view type = exportTemplate.type;
213250432b3SEd Tanous     std::string parameters(exportTemplate.parameters);
214250432b3SEd Tanous     std::string busPath(exportTemplate.busPath);
215250432b3SEd Tanous     std::string_view constructor = exportTemplate.add;
216250432b3SEd Tanous     std::string_view destructor = exportTemplate.remove;
217fc9e7fdaSChristopher Meis     devices::createsHWMon hasHWMonDir = exportTemplate.hasHWMonDir;
218fc9e7fdaSChristopher Meis     std::string name = "unknown";
219fc9e7fdaSChristopher Meis     std::optional<uint64_t> bus;
220fc9e7fdaSChristopher Meis     std::optional<uint64_t> address;
221fc9e7fdaSChristopher Meis     std::vector<std::string> channels;
222fc9e7fdaSChristopher Meis 
223fc9e7fdaSChristopher Meis     for (auto keyPair = configuration.begin(); keyPair != configuration.end();
224fc9e7fdaSChristopher Meis          keyPair++)
225fc9e7fdaSChristopher Meis     {
226fc9e7fdaSChristopher Meis         std::string subsituteString;
227fc9e7fdaSChristopher Meis 
228fc9e7fdaSChristopher Meis         if (keyPair.key() == "Name" &&
229fc9e7fdaSChristopher Meis             keyPair.value().type() == nlohmann::json::value_t::string)
230fc9e7fdaSChristopher Meis         {
231fc9e7fdaSChristopher Meis             subsituteString = std::regex_replace(
232fc9e7fdaSChristopher Meis                 keyPair.value().get<std::string>(), illegalNameRegex, "_");
233fc9e7fdaSChristopher Meis             name = subsituteString;
234fc9e7fdaSChristopher Meis         }
235fc9e7fdaSChristopher Meis         else
236fc9e7fdaSChristopher Meis         {
237fc9e7fdaSChristopher Meis             subsituteString = jsonToString(keyPair.value());
238fc9e7fdaSChristopher Meis         }
239fc9e7fdaSChristopher Meis 
240fc9e7fdaSChristopher Meis         if (keyPair.key() == "Bus")
241fc9e7fdaSChristopher Meis         {
242fc9e7fdaSChristopher Meis             bus = keyPair.value().get<uint64_t>();
243fc9e7fdaSChristopher Meis         }
244fc9e7fdaSChristopher Meis         else if (keyPair.key() == "Address")
245fc9e7fdaSChristopher Meis         {
246fc9e7fdaSChristopher Meis             address = keyPair.value().get<uint64_t>();
247fc9e7fdaSChristopher Meis         }
248fc9e7fdaSChristopher Meis         else if (keyPair.key() == "ChannelNames" && type.ends_with("Mux"))
249fc9e7fdaSChristopher Meis         {
250fc9e7fdaSChristopher Meis             channels = keyPair.value().get<std::vector<std::string>>();
251fc9e7fdaSChristopher Meis         }
2525a61ec86SGeorge Liu         replaceAll(parameters, templateChar + keyPair.key(), subsituteString);
2535a61ec86SGeorge Liu         replaceAll(busPath, templateChar + keyPair.key(), subsituteString);
254fc9e7fdaSChristopher Meis     }
255fc9e7fdaSChristopher Meis 
256fc9e7fdaSChristopher Meis     if (!bus || !address)
257fc9e7fdaSChristopher Meis     {
258fc9e7fdaSChristopher Meis         createDevice(busPath, parameters, constructor);
259fc9e7fdaSChristopher Meis         return;
260fc9e7fdaSChristopher Meis     }
261fc9e7fdaSChristopher Meis 
262fc9e7fdaSChristopher Meis     buildDevice(name, busPath, parameters, *bus, *address, constructor,
263a555acf0SAlexander Hansen                 destructor, hasHWMonDir, std::move(channels), io);
264fc9e7fdaSChristopher Meis }
265fc9e7fdaSChristopher Meis 
loadOverlays(const nlohmann::json & systemConfiguration,boost::asio::io_context & io)266a555acf0SAlexander Hansen bool loadOverlays(const nlohmann::json& systemConfiguration,
267a555acf0SAlexander Hansen                   boost::asio::io_context& io)
268fc9e7fdaSChristopher Meis {
269*064d8affSAlexander Hansen     lg2::debug("start loading device overlays");
270*064d8affSAlexander Hansen 
271fc9e7fdaSChristopher Meis     std::filesystem::create_directory(outputDir);
272fc9e7fdaSChristopher Meis     for (auto entity = systemConfiguration.begin();
273fc9e7fdaSChristopher Meis          entity != systemConfiguration.end(); entity++)
274fc9e7fdaSChristopher Meis     {
275fc9e7fdaSChristopher Meis         auto findExposes = entity.value().find("Exposes");
276fc9e7fdaSChristopher Meis         if (findExposes == entity.value().end() ||
277fc9e7fdaSChristopher Meis             findExposes->type() != nlohmann::json::value_t::array)
278fc9e7fdaSChristopher Meis         {
279fc9e7fdaSChristopher Meis             continue;
280fc9e7fdaSChristopher Meis         }
281fc9e7fdaSChristopher Meis 
282fc9e7fdaSChristopher Meis         for (const auto& configuration : *findExposes)
283fc9e7fdaSChristopher Meis         {
284fc9e7fdaSChristopher Meis             auto findStatus = configuration.find("Status");
285fc9e7fdaSChristopher Meis             // status missing is assumed to be 'okay'
286fc9e7fdaSChristopher Meis             if (findStatus != configuration.end() && *findStatus == "disabled")
287fc9e7fdaSChristopher Meis             {
288fc9e7fdaSChristopher Meis                 continue;
289fc9e7fdaSChristopher Meis             }
290fc9e7fdaSChristopher Meis             auto findType = configuration.find("Type");
291fc9e7fdaSChristopher Meis             if (findType == configuration.end() ||
292fc9e7fdaSChristopher Meis                 findType->type() != nlohmann::json::value_t::string)
293fc9e7fdaSChristopher Meis             {
294fc9e7fdaSChristopher Meis                 continue;
295fc9e7fdaSChristopher Meis             }
296250432b3SEd Tanous             const std::string& type = findType.value().get<std::string>();
297250432b3SEd Tanous             const auto* device = std::ranges::find_if(
298250432b3SEd Tanous                 devices::exportTemplates,
299250432b3SEd Tanous                 [&type](const auto& tmp) { return tmp.type == type; });
300fc9e7fdaSChristopher Meis             if (device != devices::exportTemplates.end())
301fc9e7fdaSChristopher Meis             {
302250432b3SEd Tanous                 exportDevice(*device, configuration, io);
303fc9e7fdaSChristopher Meis                 continue;
304fc9e7fdaSChristopher Meis             }
305fc9e7fdaSChristopher Meis 
306fc9e7fdaSChristopher Meis             // Because many devices are intentionally not exportable,
307fc9e7fdaSChristopher Meis             // this error message is not printed in all situations.
308fc9e7fdaSChristopher Meis             // If wondering why your device not appearing, add your type to
309fc9e7fdaSChristopher Meis             // the exportTemplates array in the devices.hpp file.
310fc9e7fdaSChristopher Meis             lg2::debug("Device type {TYPE} not found in export map allowlist",
311fc9e7fdaSChristopher Meis                        "TYPE", type);
312fc9e7fdaSChristopher Meis         }
313fc9e7fdaSChristopher Meis     }
314fc9e7fdaSChristopher Meis 
315*064d8affSAlexander Hansen     lg2::debug("finish loading device overlays");
316*064d8affSAlexander Hansen 
317fc9e7fdaSChristopher Meis     return true;
318fc9e7fdaSChristopher Meis }
319