blob: b4603cbc21313dff4afc33da9deb0316059353a5 [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
Ed Tanous072e25d2018-12-16 21:45:20 -080018#include "filesystem.hpp"
James Feista465ccc2019-02-08 12:51:01 -080019
James Feist1df06a42019-04-11 14:23:04 -070020#include <boost/container/flat_map.hpp>
21#include <fstream>
22#include <iostream>
James Feistb4383f42018-08-06 16:54:10 -070023#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070024#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -070025#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080026
James Feist1df06a42019-04-11 14:23:04 -070027constexpr const char* configurationOutDir = "/var/configuration/";
28constexpr const char* versionHashFile = "/var/configuration/version";
29constexpr const char* versionFile = "/etc/os-release";
30
James Feista465ccc2019-02-08 12:51:01 -080031bool findFiles(const std::filesystem::path& dirPath,
32 const std::string& matchString,
33 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070034
James Feista465ccc2019-02-08 12:51:01 -080035bool validateJson(const nlohmann::json& schemaFile,
36 const nlohmann::json& input);
James Feist1df06a42019-04-11 14:23:04 -070037
38bool isPowerOn(void);
39void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist68500ff2018-08-08 15:40:42 -070040struct DBusInternalError final : public sdbusplus::exception_t
41{
James Feista465ccc2019-02-08 12:51:01 -080042 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070043 {
44 return "org.freedesktop.DBus.Error.Failed";
45 };
James Feista465ccc2019-02-08 12:51:01 -080046 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070047 {
48 return "internal error";
49 };
James Feista465ccc2019-02-08 12:51:01 -080050 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070051 {
52 return "org.freedesktop.DBus.Error.Failed: "
53 "internal error";
54 };
55};
James Feist1df06a42019-04-11 14:23:04 -070056
57inline bool fwVersionIsSame(void)
58{
59 std::ifstream version(versionFile);
60 if (!version.good())
61 {
62 std::cerr << "Can't read " << versionFile << "\n";
63 return false;
64 }
65
66 std::string versionData;
67 std::string line;
68 while (std::getline(version, line))
69 {
70 versionData += line;
71 }
72
73 std::string expectedHash =
74 std::to_string(std::hash<std::string>{}(versionData));
75
76 std::filesystem::create_directory(configurationOutDir);
77 std::ifstream hashFile(versionHashFile);
78 if (hashFile.good())
79 {
80
81 std::string hashString;
82 hashFile >> hashString;
83
84 if (expectedHash == hashString)
85 {
86 return true;
87 }
88 hashFile.close();
89 }
90
91 std::ofstream output(versionHashFile);
92 output << expectedHash;
93 return false;
94}