blob: 133e5a4634e0c3b17215386ff766e5a4f9b27b7d [file] [log] [blame]
Gunnar Mills392f2942017-04-12 11:04:37 -05001#include <iostream>
2#include <string>
3#include <sstream>
4#include <fstream>
5#include <stdexcept>
6#include <phosphor-logging/log.hpp>
Eddie James6d873712017-09-01 11:29:07 -05007#include "config.h"
Gunnar Mills392f2942017-04-12 11:04:37 -05008#include "version.hpp"
Saqib Khan26a960d2017-09-19 14:23:28 -05009#include <openssl/sha.h>
Gunnar Mills392f2942017-04-12 11:04:37 -050010
11namespace phosphor
12{
13namespace software
14{
15namespace manager
16{
17
18using namespace phosphor::logging;
19
Gunnar Millscebd1022017-04-17 16:10:15 -050020std::string Version::getValue(const std::string& manifestFilePath,
21 std::string key)
Gunnar Mills392f2942017-04-12 11:04:37 -050022{
Gunnar Millscebd1022017-04-17 16:10:15 -050023 key = key + "=";
24 auto keySize = key.length();
Gunnar Mills392f2942017-04-12 11:04:37 -050025
26 if (manifestFilePath.empty())
27 {
28 log<level::ERR>("Error MANIFESTFilePath is empty");
29 throw std::runtime_error("MANIFESTFilePath is empty");
30 }
31
Gunnar Millscebd1022017-04-17 16:10:15 -050032 std::string value{};
Gunnar Mills392f2942017-04-12 11:04:37 -050033 std::ifstream efile;
34 std::string line;
35 efile.exceptions(std::ifstream::failbit
36 | std::ifstream::badbit
37 | std::ifstream::eofbit);
38
39 // Too many GCC bugs (53984, 66145) to do this the right way...
40 try
41 {
42 efile.open(manifestFilePath);
43 while (getline(efile, line))
44 {
Gunnar Millscebd1022017-04-17 16:10:15 -050045 if (line.compare(0, keySize, key) == 0)
Gunnar Mills392f2942017-04-12 11:04:37 -050046 {
Gunnar Millscebd1022017-04-17 16:10:15 -050047 value = line.substr(keySize);
Gunnar Mills392f2942017-04-12 11:04:37 -050048 break;
49 }
50 }
51 efile.close();
52 }
53 catch (const std::exception& e)
54 {
Gunnar Millscebd1022017-04-17 16:10:15 -050055 log<level::ERR>("Error in reading MANIFEST file");
Gunnar Mills392f2942017-04-12 11:04:37 -050056 }
57
Gunnar Millscebd1022017-04-17 16:10:15 -050058 return value;
Gunnar Mills392f2942017-04-12 11:04:37 -050059}
60
61std::string Version::getId(const std::string& version)
62{
Gunnar Mills392f2942017-04-12 11:04:37 -050063
64 if (version.empty())
65 {
Gunnar Millscebd1022017-04-17 16:10:15 -050066 log<level::ERR>("Error version is empty");
67 throw std::runtime_error("Version is empty");
Gunnar Mills392f2942017-04-12 11:04:37 -050068 }
69
Saqib Khan26a960d2017-09-19 14:23:28 -050070 unsigned char digest[SHA512_DIGEST_LENGTH];
71 SHA512_CTX ctx;
72 SHA512_Init(&ctx);
73 SHA512_Update(&ctx, version.c_str(), strlen(version.c_str()));
74 SHA512_Final(digest, &ctx);
75 char mdString[SHA512_DIGEST_LENGTH*2+1];
76 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
77 {
78 snprintf(&mdString[i*2], 3, "%02x", (unsigned int)digest[i]);
79 }
80
81 // Only need 8 hex digits.
82 std::string hexId = std::string(mdString);
83 return (hexId.substr(0, 8));
Gunnar Mills392f2942017-04-12 11:04:37 -050084}
85
Saqib Khan1eef62d2017-08-10 15:29:34 -050086std::string Version::getBMCVersion(const std::string& releaseFilePath)
Saqib Khanba239882017-05-26 08:41:54 -050087{
88 std::string versionKey = "VERSION_ID=";
89 std::string version{};
90 std::ifstream efile;
91 std::string line;
Saqib Khan1eef62d2017-08-10 15:29:34 -050092 efile.open(releaseFilePath);
Saqib Khanba239882017-05-26 08:41:54 -050093
94 while (getline(efile, line))
95 {
96 if (line.substr(0, versionKey.size()).find(versionKey) !=
97 std::string::npos)
98 {
99 std::size_t pos = line.find_first_of('"') + 1;
100 version = line.substr(pos, line.find_last_of('"') - pos);
101 break;
102 }
103 }
104 efile.close();
105
106 if (version.empty())
107 {
108 log<level::ERR>("Error BMC current version is empty");
109 throw std::runtime_error("BMC current version is empty");
110 }
111
112 return version;
113}
114
Eddie James6d873712017-09-01 11:29:07 -0500115bool Version::isFunctional()
116{
117 return versionStr == getBMCVersion(OS_RELEASE_FILE);
118}
119
Gunnar Mills392f2942017-04-12 11:04:37 -0500120} // namespace manager
121} // namespace software
122} // namepsace phosphor