James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 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 | #pragma once |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 18 | #include <boost/container/flat_map.hpp> |
James Feist | 637b3ef | 2019-04-15 16:35:30 -0700 | [diff] [blame] | 19 | #include <filesystem> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <iostream> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 22 | #include <nlohmann/json.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 23 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 24 | #include <sdbusplus/exception.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 25 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 26 | constexpr const char* configurationOutDir = "/var/configuration/"; |
| 27 | constexpr const char* versionHashFile = "/var/configuration/version"; |
| 28 | constexpr const char* versionFile = "/etc/os-release"; |
| 29 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 30 | bool findFiles(const std::filesystem::path& dirPath, |
| 31 | const std::string& matchString, |
| 32 | std::vector<std::filesystem::path>& foundPaths); |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 33 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 34 | bool getI2cDevicePaths( |
| 35 | const std::filesystem::path& dirPath, |
| 36 | boost::container::flat_map<size_t, std::filesystem::path>& busPaths); |
| 37 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 38 | bool validateJson(const nlohmann::json& schemaFile, |
| 39 | const nlohmann::json& input); |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 40 | |
| 41 | bool isPowerOn(void); |
| 42 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 43 | struct DBusInternalError final : public sdbusplus::exception_t |
| 44 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 45 | const char* name() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 46 | { |
| 47 | return "org.freedesktop.DBus.Error.Failed"; |
| 48 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 49 | const char* description() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 50 | { |
| 51 | return "internal error"; |
| 52 | }; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 53 | const char* what() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 54 | { |
| 55 | return "org.freedesktop.DBus.Error.Failed: " |
| 56 | "internal error"; |
| 57 | }; |
| 58 | }; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 59 | |
| 60 | inline bool fwVersionIsSame(void) |
| 61 | { |
| 62 | std::ifstream version(versionFile); |
| 63 | if (!version.good()) |
| 64 | { |
| 65 | std::cerr << "Can't read " << versionFile << "\n"; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | std::string versionData; |
| 70 | std::string line; |
| 71 | while (std::getline(version, line)) |
| 72 | { |
| 73 | versionData += line; |
| 74 | } |
| 75 | |
| 76 | std::string expectedHash = |
| 77 | std::to_string(std::hash<std::string>{}(versionData)); |
| 78 | |
| 79 | std::filesystem::create_directory(configurationOutDir); |
| 80 | std::ifstream hashFile(versionHashFile); |
| 81 | if (hashFile.good()) |
| 82 | { |
| 83 | |
| 84 | std::string hashString; |
| 85 | hashFile >> hashString; |
| 86 | |
| 87 | if (expectedHash == hashString) |
| 88 | { |
| 89 | return true; |
| 90 | } |
| 91 | hashFile.close(); |
| 92 | } |
| 93 | |
| 94 | std::ofstream output(versionHashFile); |
| 95 | output << expectedHash; |
| 96 | return false; |
| 97 | } |