blob: 5dbab3508d140e8260395c5338a3445733347877 [file] [log] [blame]
Saqib Khan167601b2017-06-18 23:33:46 -05001#include <iostream>
2#include <string>
3#include <sstream>
4#include <fstream>
5#include <stdexcept>
6#include <phosphor-logging/log.hpp>
7#include "version.hpp"
8#include <phosphor-logging/elog-errors.hpp>
9#include "xyz/openbmc_project/Common/error.hpp"
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -050010#include "item_updater.hpp"
Saqib Khan2308b8b2017-09-19 15:33:06 -050011#include <openssl/sha.h>
Saqib Khan167601b2017-06-18 23:33:46 -050012
13namespace openpower
14{
15namespace software
16{
17namespace updater
18{
19
20using namespace sdbusplus::xyz::openbmc_project::Common::Error;
21using namespace phosphor::logging;
Gunnar Millsa93a07b2017-09-21 15:40:09 -050022using Argument = xyz::openbmc_project::Common::InvalidArgument;
Saqib Khan167601b2017-06-18 23:33:46 -050023
24std::string Version::getId(const std::string& version)
25{
Saqib Khan167601b2017-06-18 23:33:46 -050026
27 if (version.empty())
28 {
29 log<level::ERR>("Error version is empty");
Gunnar Millsa93a07b2017-09-21 15:40:09 -050030 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"),
31 Argument::ARGUMENT_VALUE(version.c_str()));
Saqib Khan167601b2017-06-18 23:33:46 -050032 }
33
Saqib Khan2308b8b2017-09-19 15:33:06 -050034 unsigned char digest[SHA512_DIGEST_LENGTH];
35 SHA512_CTX ctx;
36 SHA512_Init(&ctx);
37 SHA512_Update(&ctx, version.c_str(), strlen(version.c_str()));
38 SHA512_Final(digest, &ctx);
39 char mdString[SHA512_DIGEST_LENGTH*2+1];
40 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
41 {
42 snprintf(&mdString[i*2], 3, "%02x", (unsigned int)digest[i]);
43 }
44
45 // Only need 8 hex digits.
46 std::string hexId = std::string(mdString);
47 return (hexId.substr(0, 8));
Saqib Khan167601b2017-06-18 23:33:46 -050048}
49
50std::map<std::string, std::string> Version::getValue(
51 const std::string& filePath, std::map<std::string, std::string> keys)
52{
Saqib Khan167601b2017-06-18 23:33:46 -050053 if (filePath.empty())
54 {
55 log<level::ERR>("Error filePath is empty");
Gunnar Millsa93a07b2017-09-21 15:40:09 -050056 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"),
57 Argument::ARGUMENT_VALUE(filePath.c_str()));
Saqib Khan167601b2017-06-18 23:33:46 -050058 }
59
60 std::ifstream efile;
61 std::string line;
Gunnar Millsa93a07b2017-09-21 15:40:09 -050062 efile.exceptions(std::ifstream::failbit |
63 std::ifstream::badbit |
64 std::ifstream::eofbit);
Saqib Khan167601b2017-06-18 23:33:46 -050065
66 try
67 {
68 efile.open(filePath);
69 while (getline(efile, line))
70 {
71 for(auto& key : keys)
72 {
73 auto value = key.first + "=";
74 auto keySize = value.length();
75 if (line.compare(0, keySize, value) == 0)
76 {
77 key.second = line.substr(keySize);
78 break;
79 }
80 }
81 }
82 efile.close();
83 }
84 catch (const std::exception& e)
85 {
Saqib Khane53222d2017-08-19 16:57:24 -050086 if (!efile.eof())
87 {
88 log<level::ERR>("Error in reading file");
89 }
90 efile.close();
Saqib Khan167601b2017-06-18 23:33:46 -050091 }
92
93 return keys;
94}
95
Saqib Khan167601b2017-06-18 23:33:46 -050096} // namespace updater
97} // namespace software
98} // namespace openpower