blob: e54d7bf93b3ff52dd20edd6b3019dcb75de0aa33 [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
Patrick Venturea49dc332019-10-26 08:32:02 -070017#include "Overlay.hpp"
18
19#include "Utils.hpp"
20#include "devices.hpp"
21
James Feista465ccc2019-02-08 12:51:01 -080022#include <boost/algorithm/string/predicate.hpp>
23#include <boost/container/flat_map.hpp>
24#include <boost/container/flat_set.hpp>
25#include <boost/process/child.hpp>
James Feist637b3ef2019-04-15 16:35:30 -070026#include <filesystem>
James Feista465ccc2019-02-08 12:51:01 -080027#include <iomanip>
28#include <iostream>
29#include <nlohmann/json.hpp>
30#include <regex>
31#include <string>
James Feistc95cb142018-02-26 10:41:42 -080032
James Feista465ccc2019-02-08 12:51:01 -080033constexpr const char* DT_OVERLAY = "/usr/bin/dtoverlay";
34constexpr const char* DTC = "/usr/bin/dtc";
35constexpr const char* OUTPUT_DIR = "/tmp/overlays";
James Feista465ccc2019-02-08 12:51:01 -080036constexpr const char* TEMPLATE_CHAR = "$";
37constexpr const char* HEX_FORMAT_STR = "0x";
38constexpr const char* PLATFORM = "aspeed,ast2500";
39constexpr const char* I2C_DEVS_DIR = "/sys/bus/i2c/devices";
40constexpr const char* MUX_SYMLINK_DIR = "/dev/i2c-mux";
James Feistc95cb142018-02-26 10:41:42 -080041
Josh Lehan96b8a6e2019-10-09 14:39:49 -070042constexpr const bool DEBUG = false;
43
James Feistc95cb142018-02-26 10:41:42 -080044// some drivers need to be unbind / bind to load device tree changes
45static const boost::container::flat_map<std::string, std::string> FORCE_PROBES =
46 {{"IntelFanConnector", "/sys/bus/platform/drivers/aspeed_pwm_tacho"}};
47
James Feistc95cb142018-02-26 10:41:42 -080048std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
49
James Feista465ccc2019-02-08 12:51:01 -080050void createOverlay(const std::string& templatePath,
51 const nlohmann::json& configuration);
James Feistc95cb142018-02-26 10:41:42 -080052
53void unloadAllOverlays(void)
54{
55 boost::process::child c(DT_OVERLAY, "-d", OUTPUT_DIR, "-R");
56 c.wait();
57}
58
59// this is hopefully temporary, but some drivers can't detect changes
60// without an unbind and bind so this function must exist for now
James Feista465ccc2019-02-08 12:51:01 -080061void forceProbe(const std::string& driver)
James Feistc95cb142018-02-26 10:41:42 -080062{
63 std::ofstream driverUnbind(driver + "/unbind");
64 std::ofstream driverBind(driver + "/bind");
65 if (!driverUnbind.is_open())
66 {
67 std::cerr << "force probe error opening " << driver << "\n";
68 return;
69 }
70 if (!driverBind.is_open())
71 {
72 driverUnbind.close();
73 std::cerr << "force probe error opening " << driver << "\n";
74 return;
75 }
76
Ed Tanous072e25d2018-12-16 21:45:20 -080077 std::filesystem::path pathObj(driver);
James Feista465ccc2019-02-08 12:51:01 -080078 for (auto& p : std::filesystem::directory_iterator(pathObj))
James Feistc95cb142018-02-26 10:41:42 -080079 {
80 // symlinks are object names
81 if (is_symlink(p))
82 {
83 std::string driverName = p.path().filename();
84 driverUnbind << driverName;
85 driverBind << driverName;
86 break;
87 }
88 }
89 driverUnbind.close();
90 driverBind.close();
91}
92
93// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080094std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080095{
96 if (in.type() == nlohmann::json::value_t::string)
97 {
98 return in.get<std::string>();
99 }
100 else if (in.type() == nlohmann::json::value_t::array)
101 {
102 // remove brackets and comma from array
103 std::string array = in.dump();
104 array = array.substr(1, array.size() - 2);
105 boost::replace_all(array, ",", " ");
106 return array;
107 }
108 return in.dump();
109}
110
Ed Tanousee3357a2019-02-26 12:44:14 -0800111void linkMux(const std::string& muxName, size_t busIndex, size_t address,
James Feista465ccc2019-02-08 12:51:01 -0800112 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -0800113{
James Feist286babc2019-02-07 16:48:28 -0800114 std::error_code ec;
Ed Tanousee3357a2019-02-26 12:44:14 -0800115 std::filesystem::path muxSymlinkDir(MUX_SYMLINK_DIR);
116 std::filesystem::create_directory(muxSymlinkDir, ec);
117 // ignore error codes here if the directory already exists
118 ec.clear();
119 std::filesystem::path linkDir = muxSymlinkDir / muxName;
120 std::filesystem::create_directory(linkDir, ec);
121
122 std::ostringstream hexAddress;
123 hexAddress << std::hex << std::setfill('0') << std::setw(4) << address;
James Feist286babc2019-02-07 16:48:28 -0800124
125 std::filesystem::path devDir(I2C_DEVS_DIR);
Ed Tanousee3357a2019-02-26 12:44:14 -0800126 devDir /= std::to_string(busIndex) + "-" + hexAddress.str();
James Feist286babc2019-02-07 16:48:28 -0800127
Ed Tanousee3357a2019-02-26 12:44:14 -0800128 for (std::size_t channelIndex = 0; channelIndex < channelNames.size();
129 channelIndex++)
James Feist286babc2019-02-07 16:48:28 -0800130 {
Ed Tanousee3357a2019-02-26 12:44:14 -0800131 const std::string* channelName =
132 channelNames[channelIndex].get_ptr<const std::string*>();
133 if (channelName == nullptr)
134 {
135 continue;
136 }
137 if (channelName->empty())
138 {
139 continue;
140 }
141
142 std::filesystem::path channelPath =
143 devDir / ("channel-" + std::to_string(channelIndex));
144 if (!is_symlink(channelPath))
145 {
146 std::cerr << channelPath << "for mux channel " << *channelName
147 << " doesn't exist!\n";
148 continue;
149 }
150 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
151
152 std::filesystem::path fp("/dev" / bus.filename());
153 std::filesystem::path link(linkDir / *channelName);
154
155 std::filesystem::create_symlink(fp, link, ec);
156 if (ec)
157 {
158 std::cerr << "Failure creating symlink for " << fp << " to " << link
159 << "\n";
160 }
James Feistc95cb142018-02-26 10:41:42 -0800161 }
162}
163
James Feista465ccc2019-02-08 12:51:01 -0800164void exportDevice(const std::string& type,
165 const devices::ExportTemplate& exportTemplate,
166 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700167{
168
169 std::string parameters = exportTemplate.parameters;
170 std::string device = exportTemplate.device;
171 std::string name = "unknown";
James Feista465ccc2019-02-08 12:51:01 -0800172 const uint64_t* bus = nullptr;
173 const uint64_t* address = nullptr;
174 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700175
176 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
177 keyPair++)
178 {
179 std::string subsituteString;
180
181 if (keyPair.key() == "Name" &&
182 keyPair.value().type() == nlohmann::json::value_t::string)
183 {
184 subsituteString = std::regex_replace(
185 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
186 name = subsituteString;
187 }
188 else
189 {
190 subsituteString = jsonToString(keyPair.value());
191 }
192
193 if (keyPair.key() == "Bus")
194 {
James Feista465ccc2019-02-08 12:51:01 -0800195 bus = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700196 }
197 else if (keyPair.key() == "Address")
198 {
James Feista465ccc2019-02-08 12:51:01 -0800199 address = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700200 }
James Feist286babc2019-02-07 16:48:28 -0800201 else if (keyPair.key() == "ChannelNames")
202 {
203 channels =
James Feista465ccc2019-02-08 12:51:01 -0800204 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800205 }
James Feist053a6642018-10-15 13:17:09 -0700206 boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(),
207 subsituteString);
208 boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(),
209 subsituteString);
210 }
211
212 // if we found bus and address we can attempt to prevent errors
213 if (bus != nullptr && address != nullptr)
214 {
215 std::ostringstream hex;
216 hex << std::hex << *address;
James Feista465ccc2019-02-08 12:51:01 -0800217 const std::string& addressHex = hex.str();
James Feist053a6642018-10-15 13:17:09 -0700218 std::string busStr = std::to_string(*bus);
219
Ed Tanous072e25d2018-12-16 21:45:20 -0800220 std::filesystem::path devicePath(device);
James Feist58b9c262019-02-12 13:38:45 -0800221 std::filesystem::path parentPath = devicePath.parent_path();
222 if (std::filesystem::is_directory(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700223 {
James Feist58b9c262019-02-12 13:38:45 -0800224 for (const auto& path :
225 std::filesystem::directory_iterator(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700226 {
James Feist58b9c262019-02-12 13:38:45 -0800227 if (!std::filesystem::is_directory(path))
228 {
229 continue;
230 }
James Feist053a6642018-10-15 13:17:09 -0700231
James Feist58b9c262019-02-12 13:38:45 -0800232 const std::string& directoryName = path.path().filename();
233 if (boost::starts_with(directoryName, busStr) &&
234 boost::ends_with(directoryName, addressHex))
235 {
236 return; // already exported
237 }
James Feist053a6642018-10-15 13:17:09 -0700238 }
239 }
240 }
241
242 std::ofstream deviceFile(device);
243 if (!deviceFile.good())
244 {
245 std::cerr << "Error writing " << device << "\n";
246 return;
247 }
248 deviceFile << parameters;
249 deviceFile.close();
James Feist286babc2019-02-07 16:48:28 -0800250 if (boost::ends_with(type, "Mux") && bus && address && channels)
251 {
James Feist98132792019-07-09 13:29:09 -0700252 linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
253 *channels);
James Feist286babc2019-02-07 16:48:28 -0800254 }
James Feist053a6642018-10-15 13:17:09 -0700255}
256
257// this is now deprecated
James Feista465ccc2019-02-08 12:51:01 -0800258void createOverlay(const std::string& templatePath,
259 const nlohmann::json& configuration)
James Feistc95cb142018-02-26 10:41:42 -0800260{
261 std::ifstream templateFile(templatePath);
262
263 if (!templateFile.is_open())
264 {
265 std::cerr << "createOverlay error opening " << templatePath << "\n";
266 return;
267 }
268 std::stringstream buff;
269 buff << templateFile.rdbuf();
270 std::string templateStr = buff.str();
271 std::string name = "unknown";
James Feistd63d18a2018-07-19 15:23:45 -0700272 std::string type = configuration["Type"];
James Feistc95cb142018-02-26 10:41:42 -0800273 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
274 keyPair++)
275 {
276 std::string subsituteString;
277
278 // device tree symbols are in decimal
James Feist1e3e6982018-08-03 16:09:28 -0700279 if (keyPair.key() == "Bus" &&
James Feistc95cb142018-02-26 10:41:42 -0800280 keyPair.value().type() == nlohmann::json::value_t::string)
281 {
James Feist98132792019-07-09 13:29:09 -0700282 long unsigned int dec =
James Feistc95cb142018-02-26 10:41:42 -0800283 std::stoul(keyPair.value().get<std::string>(), nullptr, 16);
284 subsituteString = std::to_string(dec);
285 }
James Feistd63d18a2018-07-19 15:23:45 -0700286 else if (keyPair.key() == "Name" &&
James Feistc95cb142018-02-26 10:41:42 -0800287 keyPair.value().type() == nlohmann::json::value_t::string)
288 {
289 subsituteString = std::regex_replace(
290 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
291 name = subsituteString;
292 }
James Feist1e3e6982018-08-03 16:09:28 -0700293 else if (keyPair.key() == "Address")
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700294 {
295 if (keyPair.value().type() == nlohmann::json::value_t::string)
296 {
297 subsituteString = keyPair.value().get<std::string>();
298 subsituteString.erase(
299 0, subsituteString.find_first_not_of(HEX_FORMAT_STR));
300 }
301 else
302 {
303 std::ostringstream hex;
304 hex << std::hex << keyPair.value().get<unsigned int>();
305 subsituteString = hex.str();
306 }
307 }
James Feistc95cb142018-02-26 10:41:42 -0800308 else
309 {
310 subsituteString = jsonToString(keyPair.value());
311 }
312 boost::replace_all(templateStr, TEMPLATE_CHAR + keyPair.key(),
313 subsituteString);
314 }
315 // todo: this is a lame way to fill in platform, but we only
316 // care about ast2500 right now
James Feist1e3e6982018-08-03 16:09:28 -0700317 boost::replace_all(templateStr, TEMPLATE_CHAR + std::string("Platform"),
James Feistc95cb142018-02-26 10:41:42 -0800318 PLATFORM);
319 std::string dtsFilename =
320 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dts";
321 std::string dtboFilename =
322 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dtbo";
323
324 std::ofstream out(dtsFilename);
325 if (!out.is_open())
326 {
327 std::cerr << "createOverlay error opening " << dtsFilename << "\n";
328 return;
329 }
330 out << templateStr;
331 out.close();
332
James Feistc95cb142018-02-26 10:41:42 -0800333 // compile dtbo and load overlay
334 boost::process::child c1(DTC, "-@", "-q", "-I", "dts", "-O", "dtb", "-o",
335 dtboFilename, dtsFilename);
336 c1.wait();
337 if (c1.exit_code())
338 {
339 std::cerr << "DTC error with file " << dtsFilename << "\n";
340 return;
341 }
342 boost::process::child c2(DT_OVERLAY, "-d", OUTPUT_DIR, name + "_" + type);
343 c2.wait();
344 if (c2.exit_code())
345 {
346 std::cerr << "DTOverlay error with file " << dtboFilename << "\n";
347 return;
348 }
349 auto findForceProbe = FORCE_PROBES.find(type);
350 if (findForceProbe != FORCE_PROBES.end())
351 {
352 forceProbe(findForceProbe->second);
353 }
James Feistc95cb142018-02-26 10:41:42 -0800354}
355
James Feista465ccc2019-02-08 12:51:01 -0800356bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800357{
Ed Tanous072e25d2018-12-16 21:45:20 -0800358 std::filesystem::create_directory(OUTPUT_DIR);
James Feistc95cb142018-02-26 10:41:42 -0800359 for (auto entity = systemConfiguration.begin();
360 entity != systemConfiguration.end(); entity++)
361 {
James Feist1e3e6982018-08-03 16:09:28 -0700362 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800363 if (findExposes == entity.value().end() ||
364 findExposes->type() != nlohmann::json::value_t::array)
365 {
366 continue;
367 }
368
James Feista465ccc2019-02-08 12:51:01 -0800369 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800370 {
James Feist1e3e6982018-08-03 16:09:28 -0700371 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800372 // status missing is assumed to be 'okay'
373 if (findStatus != configuration.end() && *findStatus == "disabled")
374 {
375 continue;
376 }
James Feistd63d18a2018-07-19 15:23:45 -0700377 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800378 if (findType == configuration.end() ||
379 findType->type() != nlohmann::json::value_t::string)
380 {
381 continue;
382 }
383 std::string type = findType.value().get<std::string>();
James Feist053a6642018-10-15 13:17:09 -0700384 auto device = devices::exportTemplates.find(type.c_str());
385 if (device != devices::exportTemplates.end())
386 {
James Feist286babc2019-02-07 16:48:28 -0800387 exportDevice(type, device->second, configuration);
Josh Lehan96b8a6e2019-10-09 14:39:49 -0700388 continue;
389 }
390
391 // Because many devices are intentionally not exportable,
392 // this error message is not printed in all situations.
393 // If wondering why your device not appearing, add your type to
394 // the exportTemplates array in the devices.hpp file.
395 if constexpr (DEBUG)
396 {
397 std::cerr << "Device type " << type
398 << " not found in export map whitelist\n";
James Feist053a6642018-10-15 13:17:09 -0700399 }
James Feistc95cb142018-02-26 10:41:42 -0800400 }
401 }
402
403 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700404}