blob: 4bc733fb0a5b4a46568fc8e560938119f826846f [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
Ed Tanous072e25d2018-12-16 21:45:20 -080017#include "filesystem.hpp"
James Feista465ccc2019-02-08 12:51:01 -080018
James Feistc95cb142018-02-26 10:41:42 -080019#include <Overlay.hpp>
20#include <Utils.hpp>
James Feista465ccc2019-02-08 12:51:01 -080021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/container/flat_map.hpp>
23#include <boost/container/flat_set.hpp>
24#include <boost/process/child.hpp>
25#include <devices.hpp>
26#include <iomanip>
27#include <iostream>
28#include <nlohmann/json.hpp>
29#include <regex>
30#include <string>
James Feistc95cb142018-02-26 10:41:42 -080031
James Feista465ccc2019-02-08 12:51:01 -080032constexpr const char* DT_OVERLAY = "/usr/bin/dtoverlay";
33constexpr const char* DTC = "/usr/bin/dtc";
34constexpr const char* OUTPUT_DIR = "/tmp/overlays";
35constexpr const char* TEMPLATE_DIR = PACKAGE_DIR "overlay_templates";
36constexpr 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
42// some drivers need to be unbind / bind to load device tree changes
43static const boost::container::flat_map<std::string, std::string> FORCE_PROBES =
44 {{"IntelFanConnector", "/sys/bus/platform/drivers/aspeed_pwm_tacho"}};
45
James Feistc95cb142018-02-26 10:41:42 -080046std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
47
James Feista465ccc2019-02-08 12:51:01 -080048void createOverlay(const std::string& templatePath,
49 const nlohmann::json& configuration);
James Feistc95cb142018-02-26 10:41:42 -080050
51void unloadAllOverlays(void)
52{
53 boost::process::child c(DT_OVERLAY, "-d", OUTPUT_DIR, "-R");
54 c.wait();
55}
56
57// this is hopefully temporary, but some drivers can't detect changes
58// without an unbind and bind so this function must exist for now
James Feista465ccc2019-02-08 12:51:01 -080059void forceProbe(const std::string& driver)
James Feistc95cb142018-02-26 10:41:42 -080060{
61 std::ofstream driverUnbind(driver + "/unbind");
62 std::ofstream driverBind(driver + "/bind");
63 if (!driverUnbind.is_open())
64 {
65 std::cerr << "force probe error opening " << driver << "\n";
66 return;
67 }
68 if (!driverBind.is_open())
69 {
70 driverUnbind.close();
71 std::cerr << "force probe error opening " << driver << "\n";
72 return;
73 }
74
Ed Tanous072e25d2018-12-16 21:45:20 -080075 std::filesystem::path pathObj(driver);
James Feista465ccc2019-02-08 12:51:01 -080076 for (auto& p : std::filesystem::directory_iterator(pathObj))
James Feistc95cb142018-02-26 10:41:42 -080077 {
78 // symlinks are object names
79 if (is_symlink(p))
80 {
81 std::string driverName = p.path().filename();
82 driverUnbind << driverName;
83 driverBind << driverName;
84 break;
85 }
86 }
87 driverUnbind.close();
88 driverBind.close();
89}
90
91// helper function to make json types into string
James Feista465ccc2019-02-08 12:51:01 -080092std::string jsonToString(const nlohmann::json& in)
James Feistc95cb142018-02-26 10:41:42 -080093{
94 if (in.type() == nlohmann::json::value_t::string)
95 {
96 return in.get<std::string>();
97 }
98 else if (in.type() == nlohmann::json::value_t::array)
99 {
100 // remove brackets and comma from array
101 std::string array = in.dump();
102 array = array.substr(1, array.size() - 2);
103 boost::replace_all(array, ",", " ");
104 return array;
105 }
106 return in.dump();
107}
108
James Feista465ccc2019-02-08 12:51:01 -0800109void linkMux(const std::string& muxName, size_t bus, size_t address,
110 const nlohmann::json::array_t& channelNames)
James Feistc95cb142018-02-26 10:41:42 -0800111{
James Feist286babc2019-02-07 16:48:28 -0800112 // create directory first time
113 static bool createDir = false;
114 std::error_code ec;
115 if (!createDir)
James Feistc95cb142018-02-26 10:41:42 -0800116 {
James Feist286babc2019-02-07 16:48:28 -0800117 std::filesystem::create_directory(MUX_SYMLINK_DIR, ec);
118 createDir = true;
119 }
120 std::ostringstream hex;
121 hex << std::hex << std::setfill('0') << std::setw(4) << address;
James Feista465ccc2019-02-08 12:51:01 -0800122 const std::string& addressHex = hex.str();
James Feist286babc2019-02-07 16:48:28 -0800123
124 std::filesystem::path devDir(I2C_DEVS_DIR);
125 devDir /= std::to_string(bus) + "-" + addressHex;
126
127 size_t channel = 0;
128 std::string channelName;
129 std::filesystem::path channelPath = devDir / "channel-0";
130 while (is_symlink(channelPath))
131 {
132 if (channel > channelNames.size())
133 {
134 channelName = std::to_string(channel);
135 }
136 else
137 {
James Feista465ccc2019-02-08 12:51:01 -0800138 const std::string* ptr =
139 channelNames[channel].get_ptr<const std::string*>();
James Feist286babc2019-02-07 16:48:28 -0800140 if (ptr == nullptr)
141 {
142 channelName = channelNames[channel].dump();
143 }
144 else
145 {
146 channelName = *ptr;
147 }
148 }
149 // if configuration has name empty, don't put it in dev
150 if (channelName.empty())
James Feistc95cb142018-02-26 10:41:42 -0800151 {
152 continue;
153 }
James Feist286babc2019-02-07 16:48:28 -0800154
155 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
James Feista465ccc2019-02-08 12:51:01 -0800156 const std::string& busName = bus.filename();
James Feist286babc2019-02-07 16:48:28 -0800157
158 std::string linkDir = MUX_SYMLINK_DIR + std::string("/") + muxName;
159 if (channel == 0)
160 {
161 std::filesystem::create_directory(linkDir, ec);
162 }
163 std::filesystem::create_symlink(
164 "/dev/" + busName, linkDir + std::string("/") + channelName, ec);
165
166 if (ec)
167 {
168 std::cerr << "Failure creating symlink for " << busName << "\n";
169 return;
170 }
171
172 channel++;
173 channelPath = devDir / ("channel-" + std::to_string(channel));
174 }
175 if (channel == 0)
176 {
177 std::cerr << "Mux missing channels " << devDir << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800178 }
179}
180
James Feista465ccc2019-02-08 12:51:01 -0800181void exportDevice(const std::string& type,
182 const devices::ExportTemplate& exportTemplate,
183 const nlohmann::json& configuration)
James Feist053a6642018-10-15 13:17:09 -0700184{
185
186 std::string parameters = exportTemplate.parameters;
187 std::string device = exportTemplate.device;
188 std::string name = "unknown";
James Feista465ccc2019-02-08 12:51:01 -0800189 const uint64_t* bus = nullptr;
190 const uint64_t* address = nullptr;
191 const nlohmann::json::array_t* channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700192
193 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
194 keyPair++)
195 {
196 std::string subsituteString;
197
198 if (keyPair.key() == "Name" &&
199 keyPair.value().type() == nlohmann::json::value_t::string)
200 {
201 subsituteString = std::regex_replace(
202 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
203 name = subsituteString;
204 }
205 else
206 {
207 subsituteString = jsonToString(keyPair.value());
208 }
209
210 if (keyPair.key() == "Bus")
211 {
James Feista465ccc2019-02-08 12:51:01 -0800212 bus = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700213 }
214 else if (keyPair.key() == "Address")
215 {
James Feista465ccc2019-02-08 12:51:01 -0800216 address = keyPair.value().get_ptr<const uint64_t*>();
James Feist053a6642018-10-15 13:17:09 -0700217 }
James Feist286babc2019-02-07 16:48:28 -0800218 else if (keyPair.key() == "ChannelNames")
219 {
220 channels =
James Feista465ccc2019-02-08 12:51:01 -0800221 keyPair.value().get_ptr<const nlohmann::json::array_t*>();
James Feist286babc2019-02-07 16:48:28 -0800222 }
James Feist053a6642018-10-15 13:17:09 -0700223 boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(),
224 subsituteString);
225 boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(),
226 subsituteString);
227 }
228
229 // if we found bus and address we can attempt to prevent errors
230 if (bus != nullptr && address != nullptr)
231 {
232 std::ostringstream hex;
233 hex << std::hex << *address;
James Feista465ccc2019-02-08 12:51:01 -0800234 const std::string& addressHex = hex.str();
James Feist053a6642018-10-15 13:17:09 -0700235 std::string busStr = std::to_string(*bus);
236
Ed Tanous072e25d2018-12-16 21:45:20 -0800237 std::filesystem::path devicePath(device);
James Feist58b9c262019-02-12 13:38:45 -0800238 std::filesystem::path parentPath = devicePath.parent_path();
239 if (std::filesystem::is_directory(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700240 {
James Feist58b9c262019-02-12 13:38:45 -0800241 for (const auto& path :
242 std::filesystem::directory_iterator(parentPath))
James Feist053a6642018-10-15 13:17:09 -0700243 {
James Feist58b9c262019-02-12 13:38:45 -0800244 if (!std::filesystem::is_directory(path))
245 {
246 continue;
247 }
James Feist053a6642018-10-15 13:17:09 -0700248
James Feist58b9c262019-02-12 13:38:45 -0800249 const std::string& directoryName = path.path().filename();
250 if (boost::starts_with(directoryName, busStr) &&
251 boost::ends_with(directoryName, addressHex))
252 {
253 return; // already exported
254 }
James Feist053a6642018-10-15 13:17:09 -0700255 }
256 }
257 }
258
259 std::ofstream deviceFile(device);
260 if (!deviceFile.good())
261 {
262 std::cerr << "Error writing " << device << "\n";
263 return;
264 }
265 deviceFile << parameters;
266 deviceFile.close();
James Feist286babc2019-02-07 16:48:28 -0800267 if (boost::ends_with(type, "Mux") && bus && address && channels)
268 {
269 linkMux(name, *bus, *address, *channels);
270 }
James Feist053a6642018-10-15 13:17:09 -0700271}
272
273// this is now deprecated
James Feista465ccc2019-02-08 12:51:01 -0800274void createOverlay(const std::string& templatePath,
275 const nlohmann::json& configuration)
James Feistc95cb142018-02-26 10:41:42 -0800276{
277 std::ifstream templateFile(templatePath);
278
279 if (!templateFile.is_open())
280 {
281 std::cerr << "createOverlay error opening " << templatePath << "\n";
282 return;
283 }
284 std::stringstream buff;
285 buff << templateFile.rdbuf();
286 std::string templateStr = buff.str();
287 std::string name = "unknown";
James Feistd63d18a2018-07-19 15:23:45 -0700288 std::string type = configuration["Type"];
James Feistc95cb142018-02-26 10:41:42 -0800289 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
290 keyPair++)
291 {
292 std::string subsituteString;
293
294 // device tree symbols are in decimal
James Feist1e3e6982018-08-03 16:09:28 -0700295 if (keyPair.key() == "Bus" &&
James Feistc95cb142018-02-26 10:41:42 -0800296 keyPair.value().type() == nlohmann::json::value_t::string)
297 {
298 unsigned int dec =
299 std::stoul(keyPair.value().get<std::string>(), nullptr, 16);
300 subsituteString = std::to_string(dec);
301 }
James Feistd63d18a2018-07-19 15:23:45 -0700302 else if (keyPair.key() == "Name" &&
James Feistc95cb142018-02-26 10:41:42 -0800303 keyPair.value().type() == nlohmann::json::value_t::string)
304 {
305 subsituteString = std::regex_replace(
306 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
307 name = subsituteString;
308 }
James Feist1e3e6982018-08-03 16:09:28 -0700309 else if (keyPair.key() == "Address")
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700310 {
311 if (keyPair.value().type() == nlohmann::json::value_t::string)
312 {
313 subsituteString = keyPair.value().get<std::string>();
314 subsituteString.erase(
315 0, subsituteString.find_first_not_of(HEX_FORMAT_STR));
316 }
317 else
318 {
319 std::ostringstream hex;
320 hex << std::hex << keyPair.value().get<unsigned int>();
321 subsituteString = hex.str();
322 }
323 }
James Feistc95cb142018-02-26 10:41:42 -0800324 else
325 {
326 subsituteString = jsonToString(keyPair.value());
327 }
328 boost::replace_all(templateStr, TEMPLATE_CHAR + keyPair.key(),
329 subsituteString);
330 }
331 // todo: this is a lame way to fill in platform, but we only
332 // care about ast2500 right now
James Feist1e3e6982018-08-03 16:09:28 -0700333 boost::replace_all(templateStr, TEMPLATE_CHAR + std::string("Platform"),
James Feistc95cb142018-02-26 10:41:42 -0800334 PLATFORM);
335 std::string dtsFilename =
336 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dts";
337 std::string dtboFilename =
338 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dtbo";
339
340 std::ofstream out(dtsFilename);
341 if (!out.is_open())
342 {
343 std::cerr << "createOverlay error opening " << dtsFilename << "\n";
344 return;
345 }
346 out << templateStr;
347 out.close();
348
James Feistc95cb142018-02-26 10:41:42 -0800349 // compile dtbo and load overlay
350 boost::process::child c1(DTC, "-@", "-q", "-I", "dts", "-O", "dtb", "-o",
351 dtboFilename, dtsFilename);
352 c1.wait();
353 if (c1.exit_code())
354 {
355 std::cerr << "DTC error with file " << dtsFilename << "\n";
356 return;
357 }
358 boost::process::child c2(DT_OVERLAY, "-d", OUTPUT_DIR, name + "_" + type);
359 c2.wait();
360 if (c2.exit_code())
361 {
362 std::cerr << "DTOverlay error with file " << dtboFilename << "\n";
363 return;
364 }
365 auto findForceProbe = FORCE_PROBES.find(type);
366 if (findForceProbe != FORCE_PROBES.end())
367 {
368 forceProbe(findForceProbe->second);
369 }
James Feistc95cb142018-02-26 10:41:42 -0800370}
371
James Feista465ccc2019-02-08 12:51:01 -0800372bool loadOverlays(const nlohmann::json& systemConfiguration)
James Feistc95cb142018-02-26 10:41:42 -0800373{
374
Ed Tanous072e25d2018-12-16 21:45:20 -0800375 std::vector<std::filesystem::path> paths;
376 if (!findFiles(std::filesystem::path(TEMPLATE_DIR),
James Feista3c180a2018-08-09 16:06:04 -0700377 R"(.*\.template)", paths))
James Feistc95cb142018-02-26 10:41:42 -0800378 {
379 std::cerr << "Unable to find any tempate files in " << TEMPLATE_DIR
380 << "\n";
381 return false;
382 }
383
Ed Tanous072e25d2018-12-16 21:45:20 -0800384 std::filesystem::create_directory(OUTPUT_DIR);
James Feistc95cb142018-02-26 10:41:42 -0800385 for (auto entity = systemConfiguration.begin();
386 entity != systemConfiguration.end(); entity++)
387 {
James Feist1e3e6982018-08-03 16:09:28 -0700388 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800389 if (findExposes == entity.value().end() ||
390 findExposes->type() != nlohmann::json::value_t::array)
391 {
392 continue;
393 }
394
James Feista465ccc2019-02-08 12:51:01 -0800395 for (auto& configuration : *findExposes)
James Feistc95cb142018-02-26 10:41:42 -0800396 {
James Feist1e3e6982018-08-03 16:09:28 -0700397 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800398 // status missing is assumed to be 'okay'
399 if (findStatus != configuration.end() && *findStatus == "disabled")
400 {
401 continue;
402 }
James Feistd63d18a2018-07-19 15:23:45 -0700403 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800404 if (findType == configuration.end() ||
405 findType->type() != nlohmann::json::value_t::string)
406 {
407 continue;
408 }
409 std::string type = findType.value().get<std::string>();
James Feistce4367c2018-10-16 09:19:57 -0700410#if OVERLAYS
411
James Feistc95cb142018-02-26 10:41:42 -0800412 std::string typeFile = type + std::string(".template");
James Feista465ccc2019-02-08 12:51:01 -0800413 for (const auto& path : paths)
James Feistc95cb142018-02-26 10:41:42 -0800414 {
415 if (path.filename() != typeFile)
416 {
417 continue;
418 }
419 createOverlay(path.string(), configuration);
420 break;
421 }
James Feistce4367c2018-10-16 09:19:57 -0700422#endif
James Feist053a6642018-10-15 13:17:09 -0700423 auto device = devices::exportTemplates.find(type.c_str());
424 if (device != devices::exportTemplates.end())
425 {
James Feist286babc2019-02-07 16:48:28 -0800426 exportDevice(type, device->second, configuration);
James Feist053a6642018-10-15 13:17:09 -0700427 }
James Feistc95cb142018-02-26 10:41:42 -0800428 }
429 }
430
431 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700432}