blob: aa06bebb1196392719e7494a1d3a4a620dd82a78 [file] [log] [blame]
James Feistc95cb142018-02-26 10:41:42 -08001/*
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
James Feistc95cb142018-02-26 10:41:42 -080017#include <Overlay.hpp>
18#include <Utils.hpp>
James Feista465ccc2019-02-08 12:51:01 -080019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/container/flat_map.hpp>
21#include <boost/container/flat_set.hpp>
22#include <boost/process/child.hpp>
23#include <devices.hpp>
James Feist637b3ef2019-04-15 16:35:30 -070024#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080025#include <iomanip>
26#include <iostream>
27#include <nlohmann/json.hpp>
28#include <regex>
29#include <string>
James Feistc95cb142018-02-26 10:41:42 -080030
James Feista465ccc2019-02-08 12:51:01 -080031constexpr const char* DT_OVERLAY = "/usr/bin/dtoverlay";
32constexpr const char* DTC = "/usr/bin/dtc";
33constexpr const char* OUTPUT_DIR = "/tmp/overlays";
34constexpr const char* TEMPLATE_DIR = PACKAGE_DIR "overlay_templates";
35constexpr const char* TEMPLATE_CHAR = "$";
36constexpr const char* HEX_FORMAT_STR = "0x";
37constexpr const char* PLATFORM = "aspeed,ast2500";
38constexpr const char* I2C_DEVS_DIR = "/sys/bus/i2c/devices";
39constexpr const char* MUX_SYMLINK_DIR = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080040
Josh Lehan96b8a6e2019-10-09 14:39:49 -070041constexpr const bool DEBUG = false;
42
James Feistc95cb142018-02-26 10:41:42 -080043// some drivers need to be unbind / bind to load device tree changes
44static const boost::container::flat_map<std::string, std::string> FORCE_PROBES =
45 {{"IntelFanConnector", "/sys/bus/platform/drivers/aspeed_pwm_tacho"}};
46
James Feistc95cb142018-02-26 10:41:42 -080047std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
48
James Feista465ccc2019-02-08 12:51:01 -080049void createOverlay(const std::string& templatePath,
50 const nlohmann::json& configuration);
James Feistc95cb142018-02-26 10:41:42 -080051
52void unloadAllOverlays(void)
53{
54 boost::process::child c(DT_OVERLAY, "-d", OUTPUT_DIR, "-R");
55 c.wait();
56}
57
58// this is hopefully temporary, but some drivers can't detect changes
59// without an unbind and bind so this function must exist for now
James Feista465ccc2019-02-08 12:51:01 -080060void forceProbe(const std::string& driver)
James Feistc95cb142018-02-26 10:41:42 -080061{
62 std::ofstream driverUnbind(driver + "/unbind");
63 std::ofstream driverBind(driver + "/bind");
64 if (!driverUnbind.is_open())
65 {
66 std::cerr << "force probe error opening " << driver << "\n";
67 return;
68 }
69 if (!driverBind.is_open())
70 {
71 driverUnbind.close();
72 std::cerr << "force probe error opening " << driver << "\n";
73 return;
74 }
75
Ed Tanous072e25d2018-12-16 21:45:20 -080076 std::filesystem::path pathObj(driver);
James Feista465ccc2019-02-08 12:51:01 -080077 for (auto& p : std::filesystem::directory_iterator(pathObj))
James Feistc95cb142018-02-26 10:41:42 -080078 {
79 // symlinks are object names
80 if (is_symlink(p))
81 {
82 std::string driverName = p.path().filename();
83 driverUnbind << driverName;
84 driverBind << driverName;
85 break;
86 }
87 }
88 driverUnbind.close();
89 driverBind.close();
90}
91
92// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080093std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080094{
95 if (in.type() == nlohmann::json::value_t::string)
96 {
97 return in.get<std::string>();
98 }
99 else if (in.type() == nlohmann::json::value_t::array)
100 {
101 // remove brackets and comma from array
102 std::string array = in.dump();
103 array = array.substr(1, array.size() - 2);
104 boost::replace_all(array, ",", " ");
105 return array;
106 }
107 return in.dump();
108}
109
Ed Tanousee3357a2019-02-26 12:44:14 -0800110void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -0800111 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -0800112{
James Feist286babc2019-02-07 16:48:28 -0800113 std::error_code ec;
Ed Tanousee3357a2019-02-26 12:44:14 -0800114 std::filesystem::path muxSymlinkDir(MUX_SYMLINK_DIR);
115 std::filesystem::create_directory(muxSymlinkDir, ec);
116 // ignore error codes here if the directory already exists
117 ec.clear();
118 std::filesystem::path linkDir = muxSymlinkDir / muxName;
119 std::filesystem::create_directory(linkDir, ec);
120
121 std::ostringstream hexAddress;
122 hexAddress << std::hex << std::setfill('0') << std::setw(4) << address;
James Feist286babc2019-02-07 16:48:28 -0800123
124 std::filesystem::path devDir(I2C_DEVS_DIR);
Ed Tanousee3357a2019-02-26 12:44:14 -0800125 devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
James Feist286babc2019-02-07 16:48:28 -0800126
Ed Tanousee3357a2019-02-26 12:44:14 -0800127 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
128 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -0800129 {
Ed Tanousee3357a2019-02-26 12:44:14 -0800130 const std::string* channelName =
131 channelNames[channelIndex].get_ptr<const std::string*>();
132 if (channelName == nullptr)
133 {
134 continue;
135 }
136 if (channelName->empty())
137 {
138 continue;
139 }
140
141 std::filesystem::path channelPath =
142 devDir / ("channel-" + std::to_string(channelIndex));
143 if (!is_symlink(channelPath))
144 {
145 std::cerr << channelPath << "for mux channel " << *channelName
146 << " doesn't exist!\n";
147 continue;
148 }
149 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
150
151 std::filesystem::path fp("/dev" / bus.filename());
152 std::filesystem::path link(linkDir / *channelName);
153
154 std::filesystem::create_symlink(fp, link, ec);
155 if (ec)
156 {
157 std::cerr << "Failure creating symlink for " << fp << " to " << link
158 << "\n";
159 }
James Feistc95cb142018-02-26 10:41:42 -0800160 }
161}
162
James Feista465ccc2019-02-08 12:51:01 -0800163void exportDevice(const std::string& type,
164 const devices::ExportTemplate& exportTemplate,
165 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700166{
167
168 std::string parameters = exportTemplate.parameters;
169 std::string device = exportTemplate.device;
170 std::string name = "unknown";
James Feista465ccc2019-02-08 12:51:01 -0800171 const uint64_t* bus = nullptr;
172 const uint64_t* address = nullptr;
173 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700174
175 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
176 keyPair++)
177 {
178 std::string subsituteString;
179
180 if (keyPair.key() == "Name" &&
181 keyPair.value().type() == nlohmann::json::value_t::string)
182 {
183 subsituteString = std::regex_replace(
184 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
185 name = subsituteString;
186 }
187 else
188 {
189 subsituteString = jsonToString(keyPair.value());
190 }
191
192 if (keyPair.key() == "Bus")
193 {
James Feista465ccc2019-02-08 12:51:01 -0800194 bus = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700195 }
196 else if (keyPair.key() == "Address")
197 {
James Feista465ccc2019-02-08 12:51:01 -0800198 address = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700199 }
James Feist286babc2019-02-07 16:48:28 -0800200 else if (keyPair.key() == "ChannelNames")
201 {
202 channels =
James Feista465ccc2019-02-08 12:51:01 -0800203 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800204 }
James Feist053a6642018-10-15 13:17:09 -0700205 boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(),
206 subsituteString);
207 boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(),
208 subsituteString);
209 }
210
211 // if we found bus and address we can attempt to prevent errors
212 if (bus != nullptr && address != nullptr)
213 {
214 std::ostringstream hex;
215 hex << std::hex << *address;
James Feista465ccc2019-02-08 12:51:01 -0800216 const std::string& addressHex = hex.str();
James Feist053a6642018-10-15 13:17:09 -0700217 std::string busStr = std::to_string(*bus);
218
Ed Tanous072e25d2018-12-16 21:45:20 -0800219 std::filesystem::path devicePath(device);
James Feist58b9c262019-02-12 13:38:45 -0800220 std::filesystem::path parentPath = devicePath.parent_path();
221 if (std::filesystem::is_directory(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700222 {
James Feist58b9c262019-02-12 13:38:45 -0800223 for (const auto& path :
224 std::filesystem::directory_iterator(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700225 {
James Feist58b9c262019-02-12 13:38:45 -0800226 if (!std::filesystem::is_directory(path))
227 {
228 continue;
229 }
James Feist053a6642018-10-15 13:17:09 -0700230
James Feist58b9c262019-02-12 13:38:45 -0800231 const std::string& directoryName = path.path().filename();
232 if (boost::starts_with(directoryName, busStr) &&
233 boost::ends_with(directoryName, addressHex))
234 {
235 return; // already exported
236 }
James Feist053a6642018-10-15 13:17:09 -0700237 }
238 }
239 }
240
241 std::ofstream deviceFile(device);
242 if (!deviceFile.good())
243 {
244 std::cerr << "Error writing " << device << "\n";
245 return;
246 }
247 deviceFile << parameters;
248 deviceFile.close();
James Feist286babc2019-02-07 16:48:28 -0800249 if (boost::ends_with(type, "Mux") && bus && address && channels)
250 {
James Feist98132792019-07-09 13:29:09 -0700251 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
252 *channels);
James Feist286babc2019-02-07 16:48:28 -0800253 }
James Feist053a6642018-10-15 13:17:09 -0700254}
255
256// this is now deprecated
James Feista465ccc2019-02-08 12:51:01 -0800257void createOverlay(const std::string& templatePath,
258 const nlohmann::json& configuration)
James Feistc95cb142018-02-26 10:41:42 -0800259{
260 std::ifstream templateFile(templatePath);
261
262 if (!templateFile.is_open())
263 {
264 std::cerr << "createOverlay error opening " << templatePath << "\n";
265 return;
266 }
267 std::stringstream buff;
268 buff << templateFile.rdbuf();
269 std::string templateStr = buff.str();
270 std::string name = "unknown";
James Feistd63d18a2018-07-19 15:23:45 -0700271 std::string type = configuration["Type"];
James Feistc95cb142018-02-26 10:41:42 -0800272 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
273 keyPair++)
274 {
275 std::string subsituteString;
276
277 // device tree symbols are in decimal
James Feist1e3e6982018-08-03 16:09:28 -0700278 if (keyPair.key() == "Bus" &&
James Feistc95cb142018-02-26 10:41:42 -0800279 keyPair.value().type() == nlohmann::json::value_t::string)
280 {
James Feist98132792019-07-09 13:29:09 -0700281 long unsigned int dec =
James Feistc95cb142018-02-26 10:41:42 -0800282 std::stoul(keyPair.value().get<std::string>(), nullptr, 16);
283 subsituteString = std::to_string(dec);
284 }
James Feistd63d18a2018-07-19 15:23:45 -0700285 else if (keyPair.key() == "Name" &&
James Feistc95cb142018-02-26 10:41:42 -0800286 keyPair.value().type() == nlohmann::json::value_t::string)
287 {
288 subsituteString = std::regex_replace(
289 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
290 name = subsituteString;
291 }
James Feist1e3e6982018-08-03 16:09:28 -0700292 else if (keyPair.key() == "Address")
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700293 {
294 if (keyPair.value().type() == nlohmann::json::value_t::string)
295 {
296 subsituteString = keyPair.value().get<std::string>();
297 subsituteString.erase(
298 0, subsituteString.find_first_not_of(HEX_FORMAT_STR));
299 }
300 else
301 {
302 std::ostringstream hex;
303 hex << std::hex << keyPair.value().get<unsigned int>();
304 subsituteString = hex.str();
305 }
306 }
James Feistc95cb142018-02-26 10:41:42 -0800307 else
308 {
309 subsituteString = jsonToString(keyPair.value());
310 }
311 boost::replace_all(templateStr, TEMPLATE_CHAR + keyPair.key(),
312 subsituteString);
313 }
314 // todo: this is a lame way to fill in platform, but we only
315 // care about ast2500 right now
James Feist1e3e6982018-08-03 16:09:28 -0700316 boost::replace_all(templateStr, TEMPLATE_CHAR + std::string("Platform"),
James Feistc95cb142018-02-26 10:41:42 -0800317 PLATFORM);
318 std::string dtsFilename =
319 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dts";
320 std::string dtboFilename =
321 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dtbo";
322
323 std::ofstream out(dtsFilename);
324 if (!out.is_open())
325 {
326 std::cerr << "createOverlay error opening " << dtsFilename << "\n";
327 return;
328 }
329 out << templateStr;
330 out.close();
331
James Feistc95cb142018-02-26 10:41:42 -0800332 // compile dtbo and load overlay
333 boost::process::child c1(DTC, "-@", "-q", "-I", "dts", "-O", "dtb", "-o",
334 dtboFilename, dtsFilename);
335 c1.wait();
336 if (c1.exit_code())
337 {
338 std::cerr << "DTC error with file " << dtsFilename << "\n";
339 return;
340 }
341 boost::process::child c2(DT_OVERLAY, "-d", OUTPUT_DIR, name + "_" + type);
342 c2.wait();
343 if (c2.exit_code())
344 {
345 std::cerr << "DTOverlay error with file " << dtboFilename << "\n";
346 return;
347 }
348 auto findForceProbe = FORCE_PROBES.find(type);
349 if (findForceProbe != FORCE_PROBES.end())
350 {
351 forceProbe(findForceProbe->second);
352 }
James Feistc95cb142018-02-26 10:41:42 -0800353}
354
James Feista465ccc2019-02-08 12:51:01 -0800355bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800356{
357
Ed Tanous072e25d2018-12-16 21:45:20 -0800358 std::vector<std::filesystem::path> paths;
James Feistf861da82019-06-27 12:05:16 -0700359 if (!findFiles(std::filesystem::path(TEMPLATE_DIR), R"(.*\.template)",
360 paths))
James Feistc95cb142018-02-26 10:41:42 -0800361 {
362 std::cerr << "Unable to find any tempate files in " << TEMPLATE_DIR
363 << "\n";
364 return false;
365 }
366
Ed Tanous072e25d2018-12-16 21:45:20 -0800367 std::filesystem::create_directory(OUTPUT_DIR);
James Feistc95cb142018-02-26 10:41:42 -0800368 for (auto entity = systemConfiguration.begin();
369 entity != systemConfiguration.end(); entity++)
370 {
James Feist1e3e6982018-08-03 16:09:28 -0700371 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800372 if (findExposes == entity.value().end() ||
373 findExposes->type() != nlohmann::json::value_t::array)
374 {
375 continue;
376 }
377
James Feista465ccc2019-02-08 12:51:01 -0800378 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800379 {
James Feist1e3e6982018-08-03 16:09:28 -0700380 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800381 // status missing is assumed to be 'okay'
382 if (findStatus != configuration.end() && *findStatus == "disabled")
383 {
384 continue;
385 }
James Feistd63d18a2018-07-19 15:23:45 -0700386 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800387 if (findType == configuration.end() ||
388 findType->type() != nlohmann::json::value_t::string)
389 {
390 continue;
391 }
392 std::string type = findType.value().get<std::string>();
James Feistce4367c2018-10-16 09:19:57 -0700393#if OVERLAYS
394
James Feistc95cb142018-02-26 10:41:42 -0800395 std::string typeFile = type + std::string(".template");
James Feista465ccc2019-02-08 12:51:01 -0800396 for (const auto& path : paths)
James Feistc95cb142018-02-26 10:41:42 -0800397 {
398 if (path.filename() != typeFile)
399 {
400 continue;
401 }
402 createOverlay(path.string(), configuration);
403 break;
404 }
James Feistce4367c2018-10-16 09:19:57 -0700405#endif
James Feist053a6642018-10-15 13:17:09 -0700406 auto device = devices::exportTemplates.find(type.c_str());
407 if (device != devices::exportTemplates.end())
408 {
James Feist286babc2019-02-07 16:48:28 -0800409 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700410 continue;
411 }
412
413 // Because many devices are intentionally not exportable,
414 // this error message is not printed in all situations.
415 // If wondering why your device not appearing, add your type to
416 // the exportTemplates array in the devices.hpp file.
417 if constexpr (DEBUG)
418 {
419 std::cerr << "Device type " << type
420 << " not found in export map whitelist\n";
James Feist053a6642018-10-15 13:17:09 -0700421 }
James Feistc95cb142018-02-26 10:41:42 -0800422 }
423 }
424
425 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700426}