PEL: PELTool Application

PELTooL application would be used to interact with PELs. This commit has
the first functionality, where a PEL file is passed and all PEL sections
are hexdumped in a JSON object.

Signed-off-by: Aatir <aatrapps@gmail.com>
Change-Id: I155d75bb58cbd14a297b094314f7fd1f271f4f37
diff --git a/extensions/openpower-pels/tools/peltool.cpp b/extensions/openpower-pels/tools/peltool.cpp
new file mode 100644
index 0000000..f682a42
--- /dev/null
+++ b/extensions/openpower-pels/tools/peltool.cpp
@@ -0,0 +1,64 @@
+#include "../pel.hpp"
+
+#include <CLI/CLI.hpp>
+#include <iostream>
+#include <string>
+
+using namespace phosphor::logging;
+using namespace openpower::pels;
+
+/**
+ * @brief get data form raw PEL file.
+ * @param[in] std::string Name of file with raw PEL
+ * @return std::vector<uint8_t> char vector read from raw PEL file.
+ */
+std::vector<uint8_t> getFileData(std::string name)
+{
+    std::ifstream file(name, std::ifstream::in);
+    if (file.good())
+    {
+        std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
+                                  std::istreambuf_iterator<char>()};
+        return data;
+    }
+    else
+    {
+        printf("Can't open raw PEL file");
+        return {};
+    }
+}
+
+static void exitWithError(const std::string& help, const char* err)
+{
+    std::cerr << "ERROR: " << err << std::endl << help << std::endl;
+    exit(-1);
+}
+
+int main(int argc, char** argv)
+{
+    CLI::App app{"OpenBMC PEL Tool"};
+    std::string fileName;
+    app.add_option("-f,--file", fileName, "Raw PEL File");
+    CLI11_PARSE(app, argc, argv);
+
+    if (!fileName.empty())
+    {
+        std::vector<uint8_t> data = getFileData(fileName);
+        if (!data.empty())
+        {
+            PEL pel{data};
+            pel.toJSON();
+        }
+        else
+        {
+            exitWithError(app.help("", CLI::AppFormatMode::All),
+                          "Raw PEL file can't be read.");
+        }
+    }
+    else
+    {
+        exitWithError(app.help("", CLI::AppFormatMode::All),
+                      "Raw PEL file path not specified.");
+    }
+    return 0;
+}