1 #include "sbe_consts.hpp"
2 #include "sbe_dump_collector.hpp"
3 
4 #include <libphal.H>
5 
6 #include <CLI/App.hpp>
7 #include <CLI/Config.hpp>
8 #include <CLI/Formatter.hpp>
9 
10 #include <filesystem>
11 #include <iostream>
12 
13 int main(int argc, char** argv)
14 {
15     using namespace openpower::dump::sbe_chipop;
16     using std::filesystem::path;
17     using namespace openpower::dump::SBE;
18     using namespace openpower::phal::dump;
19 
20     CLI::App app{"Dump Collector Application", "dump-collect"};
21     app.description(
22         "Collects dumps from the Self Boot Engine (SBE) based on "
23         "provided parameters.\nSupports different types of dumps and requires "
24         "specific options based on the dump type.");
25 
26     int type = 0;
27     uint32_t id;
28     std::string pathStr;
29     std::optional<uint64_t> failingUnit;
30 
31     app.add_option("--type, -t", type, "Type of the dump")
32         ->required()
33         ->check(CLI::IsMember({SBE_DUMP_TYPE_HARDWARE, SBE_DUMP_TYPE_HOSTBOOT,
34                                SBE_DUMP_TYPE_SBE}));
35 
36     app.add_option("--id, -i", id, "ID of the dump")->required();
37 
38     app.add_option("--path, -p", pathStr,
39                    "Path to store the collected dump files")
40         ->required();
41 
42     app.add_option("--failingunit, -f", failingUnit, "ID of the failing unit");
43 
44     try
45     {
46         CLI11_PARSE(app, argc, argv);
47     }
48     catch (const CLI::ParseError& e)
49     {
50         return app.exit(e);
51     }
52 
53     if (((type == SBE_DUMP_TYPE_HARDWARE) || (type == SBE_DUMP_TYPE_SBE)) &&
54         !failingUnit.has_value())
55     {
56         std::cerr
57             << "Failing unit ID is required for Hardware and SBE type dumps\n";
58         return EXIT_FAILURE;
59     }
60 
61     // Directory creation should happen here, after successful parsing
62     std::filesystem::path dirPath{pathStr};
63     if (!std::filesystem::exists(dirPath))
64     {
65         std::filesystem::create_directories(dirPath);
66     }
67 
68     SbeDumpCollector dumpCollector;
69 
70     auto failingUnitId = 0xFFFFFF; // Default or unspecified value
71     if (failingUnit.has_value())
72     {
73         failingUnitId = failingUnit.value();
74     }
75 
76     try
77     {
78         if (type == SBE_DUMP_TYPE_SBE)
79         {
80             collectSBEDump(id, failingUnitId, pathStr, type);
81         }
82         else
83         {
84             dumpCollector.collectDump(type, id, failingUnitId, pathStr);
85         }
86     }
87     catch (const std::exception& e)
88     {
89         std::cerr << "Failed to collect dump: " << e.what() << std::endl;
90         std::exit(EXIT_FAILURE);
91     }
92 
93     return 0;
94 }
95