blob: c90cddbe49f849fd73c378c2e1b537d54cd51bb7 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
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 Feist1df06a42019-04-11 14:23:04 -070018#include <boost/container/flat_map.hpp>
James Feist637b3ef2019-04-15 16:35:30 -070019#include <filesystem>
James Feist1df06a42019-04-11 14:23:04 -070020#include <fstream>
21#include <iostream>
James Feistb4383f42018-08-06 16:54:10 -070022#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070023#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -070024#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080025
James Feist1df06a42019-04-11 14:23:04 -070026constexpr const char* configurationOutDir = "/var/configuration/";
27constexpr const char* versionHashFile = "/var/configuration/version";
28constexpr const char* versionFile = "/etc/os-release";
29
James Feista465ccc2019-02-08 12:51:01 -080030bool findFiles(const std::filesystem::path& dirPath,
31 const std::string& matchString,
32 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070033
James Feista465ccc2019-02-08 12:51:01 -080034bool validateJson(const nlohmann::json& schemaFile,
35 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070036
37bool isPowerOn(void);
38void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070039struct DBusInternalError final : public sdbusplus::exception_t
40{
James Feista465ccc2019-02-08 12:51:01 -080041 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070042 {
43 return "org.freedesktop.DBus.Error.Failed";
44 };
James Feista465ccc2019-02-08 12:51:01 -080045 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070046 {
47 return "internal error";
48 };
James Feista465ccc2019-02-08 12:51:01 -080049 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070050 {
51 return "org.freedesktop.DBus.Error.Failed: "
52 "internal error";
53 };
54};
James Feist1df06a42019-04-11 14:23:04 -070055
56inline bool fwVersionIsSame(void)
57{
58 std::ifstream version(versionFile);
59 if (!version.good())
60 {
61 std::cerr << "Can't read " << versionFile << "\n";
62 return false;
63 }
64
65 std::string versionData;
66 std::string line;
67 while (std::getline(version, line))
68 {
69 versionData += line;
70 }
71
72 std::string expectedHash =
73 std::to_string(std::hash<std::string>{}(versionData));
74
75 std::filesystem::create_directory(configurationOutDir);
76 std::ifstream hashFile(versionHashFile);
77 if (hashFile.good())
78 {
79
80 std::string hashString;
81 hashFile >> hashString;
82
83 if (expectedHash == hashString)
84 {
85 return true;
86 }
87 hashFile.close();
88 }
89
90 std::ofstream output(versionHashFile);
91 output << expectedHash;
92 return false;
93}