xref: /openbmc/entity-manager/src/overlay.cpp (revision e45d8c71)
1 /*
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 */
16 /// \file overlay.cpp
17 
18 #include "overlay.hpp"
19 
20 #include "utils.hpp"
21 #include "devices.hpp"
22 
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/asio/io_context.hpp>
25 #include <boost/asio/steady_timer.hpp>
26 #include <boost/container/flat_map.hpp>
27 #include <boost/container/flat_set.hpp>
28 #include <boost/process/child.hpp>
29 #include <nlohmann/json.hpp>
30 
31 #include <filesystem>
32 #include <iomanip>
33 #include <iostream>
34 #include <regex>
35 #include <string>
36 
37 constexpr const char* outputDir = "/tmp/overlays";
38 constexpr const char* templateChar = "$";
39 constexpr const char* i2CDevsDir = "/sys/bus/i2c/devices";
40 constexpr const char* muxSymlinkDir = "/dev/i2c-mux";
41 
42 constexpr const bool debug = false;
43 
44 std::regex illegalNameRegex("[^A-Za-z0-9_]");
45 
46 // helper function to make json types into string
47 std::string jsonToString(const nlohmann::json& in)
48 {
49     if (in.type() == nlohmann::json::value_t::string)
50     {
51         return in.get<std::string>();
52     }
53     if (in.type() == nlohmann::json::value_t::array)
54     {
55         // remove brackets and comma from array
56         std::string array = in.dump();
57         array = array.substr(1, array.size() - 2);
58         boost::replace_all(array, ",", " ");
59         return array;
60     }
61     return in.dump();
62 }
63 
64 void linkMux(const std::string& muxName, size_t busIndex, size_t address,
65              const nlohmann::json::array_t& channelNames)
66 {
67     std::error_code ec;
68     std::filesystem::path muxSymlinkDirPath(muxSymlinkDir);
69     std::filesystem::create_directory(muxSymlinkDirPath, ec);
70     // ignore error codes here if the directory already exists
71     ec.clear();
72     std::filesystem::path linkDir = muxSymlinkDirPath / muxName;
73     std::filesystem::create_directory(linkDir, ec);
74 
75     std::ostringstream hexAddress;
76     hexAddress << std::hex << std::setfill('0') << std::setw(4) << address;
77 
78     std::filesystem::path devDir(i2CDevsDir);
79     devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
80 
81     for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
82          channelIndex++)
83     {
84         const std::string* channelName =
85             channelNames[channelIndex].get_ptr<const std::string*>();
86         if (channelName == nullptr)
87         {
88             continue;
89         }
90         if (channelName->empty())
91         {
92             continue;
93         }
94 
95         std::filesystem::path channelPath =
96             devDir / ("channel-" + std::to_string(channelIndex));
97         if (!is_symlink(channelPath))
98         {
99             std::cerr << channelPath << "for mux channel " << *channelName
100                       << " doesn't exist!\n";
101             continue;
102         }
103         std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
104 
105         std::filesystem::path fp("/dev" / bus.filename());
106         std::filesystem::path link(linkDir / *channelName);
107 
108         std::filesystem::create_symlink(fp, link, ec);
109         if (ec)
110         {
111             std::cerr << "Failure creating symlink for " << fp << " to " << link
112                       << "\n";
113         }
114     }
115 }
116 
117 static int deleteDevice(const std::string& devicePath,
118                         const std::shared_ptr<uint64_t>& address,
119                         const std::string& destructor)
120 {
121     if (!address)
122     {
123         return -1;
124     }
125     std::filesystem::path deviceDestructor(devicePath);
126     deviceDestructor /= destructor;
127     std::ofstream deviceFile(deviceDestructor);
128     if (!deviceFile.good())
129     {
130         std::cerr << "Error writing " << deviceDestructor << "\n";
131         return -1;
132     }
133     deviceFile << std::to_string(*address);
134     deviceFile.close();
135     return 0;
136 }
137 
138 static int createDevice(const std::string& devicePath,
139                         const std::string& parameters,
140                         const std::string& constructor)
141 {
142     std::filesystem::path deviceConstructor(devicePath);
143     deviceConstructor /= constructor;
144     std::ofstream deviceFile(deviceConstructor);
145     if (!deviceFile.good())
146     {
147         std::cerr << "Error writing " << deviceConstructor << "\n";
148         return -1;
149     }
150     deviceFile << parameters;
151     deviceFile.close();
152 
153     return 0;
154 }
155 
156 static bool deviceIsCreated(const std::string& devicePath,
157                             const std::shared_ptr<uint64_t>& bus,
158                             const std::shared_ptr<uint64_t>& address,
159                             const bool retrying)
160 {
161     if (!bus || !address)
162     {
163         return false;
164     }
165 
166     std::ostringstream hex;
167     hex << std::hex << std::setw(4) << std::setfill('0') << *address;
168     std::string addressHex = hex.str();
169     std::string busStr = std::to_string(*bus);
170 
171     std::error_code ec;
172     auto path = std::filesystem::recursive_directory_iterator(devicePath, ec);
173     if (ec)
174     {
175         std::cerr << "Unable to open path " << devicePath << "\n";
176         return false;
177     }
178     for (; path != std::filesystem::recursive_directory_iterator(); path++)
179     {
180         if (!std::filesystem::is_directory(*path))
181         {
182             continue;
183         }
184 
185         const std::string directoryName = path->path().filename();
186         std::string name = busStr;
187         name += "-";
188         name += addressHex;
189         if (directoryName == name)
190         {
191             // The first time the BMC boots the kernel has creates a
192             // filesystem enumerating the I2C devices. The I2C device has not
193             // been initialized for use. This requires a call to a device
194             // node, such as "new_device". The first pass through this
195             // function is only confirming the filesystem contains the device
196             // entry of interest (i.e. i2c4-0050).
197             //
198             // An upper level function performs the device creation
199             // action. This action may fail. The device driver (dd) used to
200             // create the I2C filesystem substructure eats any error codes,
201             // and always returns 0. This is by design. It is also possible
202             // for the new_device action to fail because the device is not
203             // actually in the system, i.e. optional equipment.
204             //
205             // The 'retrying' pass of this function is used to confirm the
206             // 'dd' device driver succeeded. Success is measured by finding
207             // the 'hwmon' subdirectory in the filesystem. The first attempt
208             // is delayed by an arbitrary amount, in order to permit the
209             // kernel time to create the filesystem entries. The upper level
210             // function determines the number of times to retry calling this
211             // function.
212             if (retrying)
213             {
214                 std::error_code ec;
215                 std::filesystem::path hwmonDir(devicePath);
216                 hwmonDir /= directoryName;
217                 hwmonDir /= "hwmon";
218                 return std::filesystem::is_directory(hwmonDir, ec);
219             }
220             return true;
221         }
222         path.disable_recursion_pending();
223     }
224     return false;
225 }
226 
227 static int buildDevice(const std::string& devicePath,
228                        const std::string& parameters,
229                        const std::shared_ptr<uint64_t>& bus,
230                        const std::shared_ptr<uint64_t>& address,
231                        const std::string& constructor,
232                        const std::string& destructor, const bool createsHWMon,
233                        const size_t retries = 5)
234 {
235     bool tryAgain = false;
236     if (!retries)
237     {
238         return -1;
239     }
240 
241     if (!deviceIsCreated(devicePath, bus, address, false))
242     {
243         createDevice(devicePath, parameters, constructor);
244         tryAgain = true;
245     }
246     else if (createsHWMon && !deviceIsCreated(devicePath, bus, address, true))
247     {
248         // device is present, hwmon subdir missing
249         deleteDevice(devicePath, address, destructor);
250         tryAgain = true;
251     }
252 
253     if (tryAgain)
254     {
255         std::shared_ptr<boost::asio::steady_timer> createTimer =
256             std::make_shared<boost::asio::steady_timer>(io);
257         createTimer->expires_after(std::chrono::milliseconds(500));
258         createTimer->async_wait([createTimer, devicePath, parameters, bus,
259                                  address, constructor, destructor, createsHWMon,
260                                  retries](const boost::system::error_code& ec) {
261             if (ec)
262             {
263                 std::cerr << "Timer error: " << ec << "\n";
264                 return -2;
265             }
266             return buildDevice(devicePath, parameters, bus, address,
267                                constructor, destructor, createsHWMon,
268                                retries - 1);
269         });
270     }
271     return 0;
272 }
273 
274 void exportDevice(const std::string& type,
275                   const devices::ExportTemplate& exportTemplate,
276                   const nlohmann::json& configuration)
277 {
278 
279     std::string parameters = exportTemplate.parameters;
280     std::string devicePath = exportTemplate.devicePath;
281     std::string constructor = exportTemplate.add;
282     std::string destructor = exportTemplate.remove;
283     bool createsHWMon = exportTemplate.createsHWMon;
284     std::string name = "unknown";
285     std::shared_ptr<uint64_t> bus = nullptr;
286     std::shared_ptr<uint64_t> address = nullptr;
287     const nlohmann::json::array_t* channels = nullptr;
288 
289     for (auto keyPair = configuration.begin(); keyPair != configuration.end();
290          keyPair++)
291     {
292         std::string subsituteString;
293 
294         if (keyPair.key() == "Name" &&
295             keyPair.value().type() == nlohmann::json::value_t::string)
296         {
297             subsituteString = std::regex_replace(
298                 keyPair.value().get<std::string>(), illegalNameRegex, "_");
299             name = subsituteString;
300         }
301         else
302         {
303             subsituteString = jsonToString(keyPair.value());
304         }
305 
306         if (keyPair.key() == "Bus")
307         {
308             bus = std::make_shared<uint64_t>(
309                 *keyPair.value().get_ptr<const uint64_t*>());
310         }
311         else if (keyPair.key() == "Address")
312         {
313             address = std::make_shared<uint64_t>(
314                 *keyPair.value().get_ptr<const uint64_t*>());
315         }
316         else if (keyPair.key() == "ChannelNames")
317         {
318             channels =
319                 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
320         }
321         boost::replace_all(parameters, templateChar + keyPair.key(),
322                            subsituteString);
323         boost::replace_all(devicePath, templateChar + keyPair.key(),
324                            subsituteString);
325     }
326 
327     int err = buildDevice(devicePath, parameters, bus, address, constructor,
328                           destructor, createsHWMon);
329 
330     if (!err && boost::ends_with(type, "Mux") && bus && address && channels)
331     {
332         linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
333                 *channels);
334     }
335 }
336 
337 bool loadOverlays(const nlohmann::json& systemConfiguration)
338 {
339     std::filesystem::create_directory(outputDir);
340     for (auto entity = systemConfiguration.begin();
341          entity != systemConfiguration.end(); entity++)
342     {
343         auto findExposes = entity.value().find("Exposes");
344         if (findExposes == entity.value().end() ||
345             findExposes->type() != nlohmann::json::value_t::array)
346         {
347             continue;
348         }
349 
350         for (auto& configuration : *findExposes)
351         {
352             auto findStatus = configuration.find("Status");
353             // status missing is assumed to be 'okay'
354             if (findStatus != configuration.end() && *findStatus == "disabled")
355             {
356                 continue;
357             }
358             auto findType = configuration.find("Type");
359             if (findType == configuration.end() ||
360                 findType->type() != nlohmann::json::value_t::string)
361             {
362                 continue;
363             }
364             std::string type = findType.value().get<std::string>();
365             auto device = devices::exportTemplates.find(type.c_str());
366             if (device != devices::exportTemplates.end())
367             {
368                 exportDevice(type, device->second, configuration);
369                 continue;
370             }
371 
372             // Because many devices are intentionally not exportable,
373             // this error message is not printed in all situations.
374             // If wondering why your device not appearing, add your type to
375             // the exportTemplates array in the devices.hpp file.
376             if constexpr (debug)
377             {
378                 std::cerr << "Device type " << type
379                           << " not found in export map whitelist\n";
380             }
381         }
382     }
383 
384     return true;
385 }
386