blob: 07b5e609445323589bc5e8b1ba08d9f8021e5772 [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
17#include <string>
18#include <iostream>
19#include <regex>
James Feist286babc2019-02-07 16:48:28 -080020#include <iomanip>
James Feistc95cb142018-02-26 10:41:42 -080021#include <boost/process/child.hpp>
James Feist053a6642018-10-15 13:17:09 -070022#include <boost/algorithm/string/predicate.hpp>
Ed Tanous072e25d2018-12-16 21:45:20 -080023#include "filesystem.hpp"
James Feistc95cb142018-02-26 10:41:42 -080024#include <boost/container/flat_map.hpp>
25#include <boost/container/flat_set.hpp>
26#include <nlohmann/json.hpp>
James Feist053a6642018-10-15 13:17:09 -070027#include <devices.hpp>
James Feistc95cb142018-02-26 10:41:42 -080028#include <Overlay.hpp>
29#include <Utils.hpp>
30
31constexpr const char *DT_OVERLAY = "/usr/bin/dtoverlay";
32constexpr const char *DTC = "/usr/bin/dtc";
33constexpr const char *OUTPUT_DIR = "/tmp/overlays";
James Feistce4367c2018-10-16 09:19:57 -070034constexpr const char *TEMPLATE_DIR = PACKAGE_DIR "overlay_templates";
James Feistc95cb142018-02-26 10:41:42 -080035constexpr const char *TEMPLATE_CHAR = "$";
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -070036constexpr const char *HEX_FORMAT_STR = "0x";
James Feistc95cb142018-02-26 10:41:42 -080037constexpr const char *PLATFORM = "aspeed,ast2500";
James Feist286babc2019-02-07 16:48:28 -080038constexpr 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
41// some drivers need to be unbind / bind to load device tree changes
42static const boost::container::flat_map<std::string, std::string> FORCE_PROBES =
43 {{"IntelFanConnector", "/sys/bus/platform/drivers/aspeed_pwm_tacho"}};
44
James Feistc95cb142018-02-26 10:41:42 -080045std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
46
47void createOverlay(const std::string &templatePath,
48 const nlohmann::json &configuration);
49
50void unloadAllOverlays(void)
51{
52 boost::process::child c(DT_OVERLAY, "-d", OUTPUT_DIR, "-R");
53 c.wait();
54}
55
56// this is hopefully temporary, but some drivers can't detect changes
57// without an unbind and bind so this function must exist for now
58void forceProbe(const std::string &driver)
59{
60 std::ofstream driverUnbind(driver + "/unbind");
61 std::ofstream driverBind(driver + "/bind");
62 if (!driverUnbind.is_open())
63 {
64 std::cerr << "force probe error opening " << driver << "\n";
65 return;
66 }
67 if (!driverBind.is_open())
68 {
69 driverUnbind.close();
70 std::cerr << "force probe error opening " << driver << "\n";
71 return;
72 }
73
Ed Tanous072e25d2018-12-16 21:45:20 -080074 std::filesystem::path pathObj(driver);
75 for (auto &p : std::filesystem::directory_iterator(pathObj))
James Feistc95cb142018-02-26 10:41:42 -080076 {
77 // symlinks are object names
78 if (is_symlink(p))
79 {
80 std::string driverName = p.path().filename();
81 driverUnbind << driverName;
82 driverBind << driverName;
83 break;
84 }
85 }
86 driverUnbind.close();
87 driverBind.close();
88}
89
90// helper function to make json types into string
91std::string jsonToString(const nlohmann::json &in)
92{
93 if (in.type() == nlohmann::json::value_t::string)
94 {
95 return in.get<std::string>();
96 }
97 else if (in.type() == nlohmann::json::value_t::array)
98 {
99 // remove brackets and comma from array
100 std::string array = in.dump();
101 array = array.substr(1, array.size() - 2);
102 boost::replace_all(array, ",", " ");
103 return array;
104 }
105 return in.dump();
106}
107
James Feist286babc2019-02-07 16:48:28 -0800108void linkMux(const std::string &muxName, size_t bus, size_t address,
109 const nlohmann::json::array_t &channelNames)
James Feistc95cb142018-02-26 10:41:42 -0800110{
James Feist286babc2019-02-07 16:48:28 -0800111 // create directory first time
112 static bool createDir = false;
113 std::error_code ec;
114 if (!createDir)
James Feistc95cb142018-02-26 10:41:42 -0800115 {
James Feist286babc2019-02-07 16:48:28 -0800116 std::filesystem::create_directory(MUX_SYMLINK_DIR, ec);
117 createDir = true;
118 }
119 std::ostringstream hex;
120 hex << std::hex << std::setfill('0') << std::setw(4) << address;
121 const std::string &addressHex = hex.str();
122
123 std::filesystem::path devDir(I2C_DEVS_DIR);
124 devDir /= std::to_string(bus) + "-" + addressHex;
125
126 size_t channel = 0;
127 std::string channelName;
128 std::filesystem::path channelPath = devDir / "channel-0";
129 while (is_symlink(channelPath))
130 {
131 if (channel > channelNames.size())
132 {
133 channelName = std::to_string(channel);
134 }
135 else
136 {
137 const std::string *ptr =
138 channelNames[channel].get_ptr<const std::string *>();
139 if (ptr == nullptr)
140 {
141 channelName = channelNames[channel].dump();
142 }
143 else
144 {
145 channelName = *ptr;
146 }
147 }
148 // if configuration has name empty, don't put it in dev
149 if (channelName.empty())
James Feistc95cb142018-02-26 10:41:42 -0800150 {
151 continue;
152 }
James Feist286babc2019-02-07 16:48:28 -0800153
154 std::filesystem::path bus = std::filesystem::read_symlink(channelPath);
155 const std::string &busName = bus.filename();
156
157 std::string linkDir = MUX_SYMLINK_DIR + std::string("/") + muxName;
158 if (channel == 0)
159 {
160 std::filesystem::create_directory(linkDir, ec);
161 }
162 std::filesystem::create_symlink(
163 "/dev/" + busName, linkDir + std::string("/") + channelName, ec);
164
165 if (ec)
166 {
167 std::cerr << "Failure creating symlink for " << busName << "\n";
168 return;
169 }
170
171 channel++;
172 channelPath = devDir / ("channel-" + std::to_string(channel));
173 }
174 if (channel == 0)
175 {
176 std::cerr << "Mux missing channels " << devDir << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800177 }
178}
179
James Feist286babc2019-02-07 16:48:28 -0800180void exportDevice(const std::string &type,
181 const devices::ExportTemplate &exportTemplate,
James Feist053a6642018-10-15 13:17:09 -0700182 const nlohmann::json &configuration)
183{
184
185 std::string parameters = exportTemplate.parameters;
186 std::string device = exportTemplate.device;
187 std::string name = "unknown";
188 const uint64_t *bus = nullptr;
189 const uint64_t *address = nullptr;
James Feist286babc2019-02-07 16:48:28 -0800190 const nlohmann::json::array_t *channels = nullptr;
James Feist053a6642018-10-15 13:17:09 -0700191
192 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
193 keyPair++)
194 {
195 std::string subsituteString;
196
197 if (keyPair.key() == "Name" &&
198 keyPair.value().type() == nlohmann::json::value_t::string)
199 {
200 subsituteString = std::regex_replace(
201 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
202 name = subsituteString;
203 }
204 else
205 {
206 subsituteString = jsonToString(keyPair.value());
207 }
208
209 if (keyPair.key() == "Bus")
210 {
211 bus = keyPair.value().get_ptr<const uint64_t *>();
212 }
213 else if (keyPair.key() == "Address")
214 {
215 address = keyPair.value().get_ptr<const uint64_t *>();
216 }
James Feist286babc2019-02-07 16:48:28 -0800217 else if (keyPair.key() == "ChannelNames")
218 {
219 channels =
220 keyPair.value().get_ptr<const nlohmann::json::array_t *>();
221 }
James Feist053a6642018-10-15 13:17:09 -0700222 boost::replace_all(parameters, TEMPLATE_CHAR + keyPair.key(),
223 subsituteString);
224 boost::replace_all(device, TEMPLATE_CHAR + keyPair.key(),
225 subsituteString);
226 }
227
228 // if we found bus and address we can attempt to prevent errors
229 if (bus != nullptr && address != nullptr)
230 {
231 std::ostringstream hex;
232 hex << std::hex << *address;
233 const std::string &addressHex = hex.str();
234 std::string busStr = std::to_string(*bus);
235
Ed Tanous072e25d2018-12-16 21:45:20 -0800236 std::filesystem::path devicePath(device);
James Feist053a6642018-10-15 13:17:09 -0700237 const std::string &dir = devicePath.parent_path().string();
Ed Tanous072e25d2018-12-16 21:45:20 -0800238 for (const auto &path : std::filesystem::directory_iterator(dir))
James Feist053a6642018-10-15 13:17:09 -0700239 {
Ed Tanous072e25d2018-12-16 21:45:20 -0800240 if (!std::filesystem::is_directory(path))
James Feist053a6642018-10-15 13:17:09 -0700241 {
242 continue;
243 }
244
245 const std::string &directoryName = path.path().filename();
246 if (boost::starts_with(directoryName, busStr) &&
247 boost::ends_with(directoryName, addressHex))
248 {
249 return; // already exported
250 }
251 }
252 }
253
254 std::ofstream deviceFile(device);
255 if (!deviceFile.good())
256 {
257 std::cerr << "Error writing " << device << "\n";
258 return;
259 }
260 deviceFile << parameters;
261 deviceFile.close();
James Feist286babc2019-02-07 16:48:28 -0800262 if (boost::ends_with(type, "Mux") && bus && address && channels)
263 {
264 linkMux(name, *bus, *address, *channels);
265 }
James Feist053a6642018-10-15 13:17:09 -0700266}
267
268// this is now deprecated
James Feistc95cb142018-02-26 10:41:42 -0800269void createOverlay(const std::string &templatePath,
270 const nlohmann::json &configuration)
271{
272 std::ifstream templateFile(templatePath);
273
274 if (!templateFile.is_open())
275 {
276 std::cerr << "createOverlay error opening " << templatePath << "\n";
277 return;
278 }
279 std::stringstream buff;
280 buff << templateFile.rdbuf();
281 std::string templateStr = buff.str();
282 std::string name = "unknown";
James Feistd63d18a2018-07-19 15:23:45 -0700283 std::string type = configuration["Type"];
James Feistc95cb142018-02-26 10:41:42 -0800284 for (auto keyPair = configuration.begin(); keyPair != configuration.end();
285 keyPair++)
286 {
287 std::string subsituteString;
288
289 // device tree symbols are in decimal
James Feist1e3e6982018-08-03 16:09:28 -0700290 if (keyPair.key() == "Bus" &&
James Feistc95cb142018-02-26 10:41:42 -0800291 keyPair.value().type() == nlohmann::json::value_t::string)
292 {
293 unsigned int dec =
294 std::stoul(keyPair.value().get<std::string>(), nullptr, 16);
295 subsituteString = std::to_string(dec);
296 }
James Feistd63d18a2018-07-19 15:23:45 -0700297 else if (keyPair.key() == "Name" &&
James Feistc95cb142018-02-26 10:41:42 -0800298 keyPair.value().type() == nlohmann::json::value_t::string)
299 {
300 subsituteString = std::regex_replace(
301 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
302 name = subsituteString;
303 }
James Feist1e3e6982018-08-03 16:09:28 -0700304 else if (keyPair.key() == "Address")
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700305 {
306 if (keyPair.value().type() == nlohmann::json::value_t::string)
307 {
308 subsituteString = keyPair.value().get<std::string>();
309 subsituteString.erase(
310 0, subsituteString.find_first_not_of(HEX_FORMAT_STR));
311 }
312 else
313 {
314 std::ostringstream hex;
315 hex << std::hex << keyPair.value().get<unsigned int>();
316 subsituteString = hex.str();
317 }
318 }
James Feistc95cb142018-02-26 10:41:42 -0800319 else
320 {
321 subsituteString = jsonToString(keyPair.value());
322 }
323 boost::replace_all(templateStr, TEMPLATE_CHAR + keyPair.key(),
324 subsituteString);
325 }
326 // todo: this is a lame way to fill in platform, but we only
327 // care about ast2500 right now
James Feist1e3e6982018-08-03 16:09:28 -0700328 boost::replace_all(templateStr, TEMPLATE_CHAR + std::string("Platform"),
James Feistc95cb142018-02-26 10:41:42 -0800329 PLATFORM);
330 std::string dtsFilename =
331 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dts";
332 std::string dtboFilename =
333 std::string(OUTPUT_DIR) + "/" + name + "_" + type + ".dtbo";
334
335 std::ofstream out(dtsFilename);
336 if (!out.is_open())
337 {
338 std::cerr << "createOverlay error opening " << dtsFilename << "\n";
339 return;
340 }
341 out << templateStr;
342 out.close();
343
James Feistc95cb142018-02-26 10:41:42 -0800344 // compile dtbo and load overlay
345 boost::process::child c1(DTC, "-@", "-q", "-I", "dts", "-O", "dtb", "-o",
346 dtboFilename, dtsFilename);
347 c1.wait();
348 if (c1.exit_code())
349 {
350 std::cerr << "DTC error with file " << dtsFilename << "\n";
351 return;
352 }
353 boost::process::child c2(DT_OVERLAY, "-d", OUTPUT_DIR, name + "_" + type);
354 c2.wait();
355 if (c2.exit_code())
356 {
357 std::cerr << "DTOverlay error with file " << dtboFilename << "\n";
358 return;
359 }
360 auto findForceProbe = FORCE_PROBES.find(type);
361 if (findForceProbe != FORCE_PROBES.end())
362 {
363 forceProbe(findForceProbe->second);
364 }
James Feistc95cb142018-02-26 10:41:42 -0800365}
366
367bool loadOverlays(const nlohmann::json &systemConfiguration)
368{
369
Ed Tanous072e25d2018-12-16 21:45:20 -0800370 std::vector<std::filesystem::path> paths;
371 if (!findFiles(std::filesystem::path(TEMPLATE_DIR),
James Feista3c180a2018-08-09 16:06:04 -0700372 R"(.*\.template)", paths))
James Feistc95cb142018-02-26 10:41:42 -0800373 {
374 std::cerr << "Unable to find any tempate files in " << TEMPLATE_DIR
375 << "\n";
376 return false;
377 }
378
Ed Tanous072e25d2018-12-16 21:45:20 -0800379 std::filesystem::create_directory(OUTPUT_DIR);
James Feistc95cb142018-02-26 10:41:42 -0800380 for (auto entity = systemConfiguration.begin();
381 entity != systemConfiguration.end(); entity++)
382 {
James Feist1e3e6982018-08-03 16:09:28 -0700383 auto findExposes = entity.value().find("Exposes");
James Feistc95cb142018-02-26 10:41:42 -0800384 if (findExposes == entity.value().end() ||
385 findExposes->type() != nlohmann::json::value_t::array)
386 {
387 continue;
388 }
389
390 for (auto &configuration : *findExposes)
391 {
James Feist1e3e6982018-08-03 16:09:28 -0700392 auto findStatus = configuration.find("Status");
James Feistc95cb142018-02-26 10:41:42 -0800393 // status missing is assumed to be 'okay'
394 if (findStatus != configuration.end() && *findStatus == "disabled")
395 {
396 continue;
397 }
James Feistd63d18a2018-07-19 15:23:45 -0700398 auto findType = configuration.find("Type");
James Feistc95cb142018-02-26 10:41:42 -0800399 if (findType == configuration.end() ||
400 findType->type() != nlohmann::json::value_t::string)
401 {
402 continue;
403 }
404 std::string type = findType.value().get<std::string>();
James Feistce4367c2018-10-16 09:19:57 -0700405#if OVERLAYS
406
James Feistc95cb142018-02-26 10:41:42 -0800407 std::string typeFile = type + std::string(".template");
James Feist053a6642018-10-15 13:17:09 -0700408 for (const auto &path : paths)
James Feistc95cb142018-02-26 10:41:42 -0800409 {
410 if (path.filename() != typeFile)
411 {
412 continue;
413 }
414 createOverlay(path.string(), configuration);
415 break;
416 }
James Feistce4367c2018-10-16 09:19:57 -0700417#endif
James Feist053a6642018-10-15 13:17:09 -0700418 auto device = devices::exportTemplates.find(type.c_str());
419 if (device != devices::exportTemplates.end())
420 {
James Feist286babc2019-02-07 16:48:28 -0800421 exportDevice(type, device->second, configuration);
James Feist053a6642018-10-15 13:17:09 -0700422 }
James Feistc95cb142018-02-26 10:41:42 -0800423 }
424 }
425
426 return true;
Jae Hyun Yoo6d1d0142018-07-25 10:07:43 -0700427}