blob: 1208f24aa89b7f90b5c01bc849afdf4d6360e705 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 IBM 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 */
Aatir186ce8c2019-10-20 15:13:39 -050016#include "../pel.hpp"
17
18#include <CLI/CLI.hpp>
19#include <iostream>
20#include <string>
21
22using namespace phosphor::logging;
23using namespace openpower::pels;
24
25/**
26 * @brief get data form raw PEL file.
27 * @param[in] std::string Name of file with raw PEL
28 * @return std::vector<uint8_t> char vector read from raw PEL file.
29 */
30std::vector<uint8_t> getFileData(std::string name)
31{
32 std::ifstream file(name, std::ifstream::in);
33 if (file.good())
34 {
35 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
36 std::istreambuf_iterator<char>()};
37 return data;
38 }
39 else
40 {
41 printf("Can't open raw PEL file");
42 return {};
43 }
44}
45
46static void exitWithError(const std::string& help, const char* err)
47{
48 std::cerr << "ERROR: " << err << std::endl << help << std::endl;
49 exit(-1);
50}
51
52int main(int argc, char** argv)
53{
54 CLI::App app{"OpenBMC PEL Tool"};
55 std::string fileName;
56 app.add_option("-f,--file", fileName, "Raw PEL File");
57 CLI11_PARSE(app, argc, argv);
58
59 if (!fileName.empty())
60 {
61 std::vector<uint8_t> data = getFileData(fileName);
62 if (!data.empty())
63 {
64 PEL pel{data};
65 pel.toJSON();
66 }
67 else
68 {
69 exitWithError(app.help("", CLI::AppFormatMode::All),
70 "Raw PEL file can't be read.");
71 }
72 }
73 else
74 {
75 exitWithError(app.help("", CLI::AppFormatMode::All),
76 "Raw PEL file path not specified.");
77 }
78 return 0;
79}