Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 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 | */ |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 16 | #include "../pel.hpp" |
| 17 | |
| 18 | #include <CLI/CLI.hpp> |
| 19 | #include <iostream> |
| 20 | #include <string> |
| 21 | |
| 22 | using namespace phosphor::logging; |
| 23 | using 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 | */ |
| 30 | std::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 | |
| 46 | static 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 | |
| 52 | int 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 | } |