xref: /openbmc/phosphor-power/tools/power-utils/main.cpp (revision d19df255c75ed3df986b2e41ca2bc4a6ba88941c)
10bf1b782SLei YU /**
20bf1b782SLei YU  * Copyright © 2019 IBM Corporation
30bf1b782SLei YU  *
40bf1b782SLei YU  * Licensed under the Apache License, Version 2.0 (the "License");
50bf1b782SLei YU  * you may not use this file except in compliance with the License.
60bf1b782SLei YU  * You may obtain a copy of the License at
70bf1b782SLei YU  *
80bf1b782SLei YU  *     http://www.apache.org/licenses/LICENSE-2.0
90bf1b782SLei YU  *
100bf1b782SLei YU  * Unless required by applicable law or agreed to in writing, software
110bf1b782SLei YU  * distributed under the License is distributed on an "AS IS" BASIS,
120bf1b782SLei YU  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130bf1b782SLei YU  * See the License for the specific language governing permissions and
140bf1b782SLei YU  * limitations under the License.
150bf1b782SLei YU  */
16*d19df255SLei YU #include "updater.hpp"
170bf1b782SLei YU #include "version.hpp"
180bf1b782SLei YU 
19093b5917SLei YU #include <CLI/CLI.hpp>
20*d19df255SLei YU #include <cassert>
210bf1b782SLei YU #include <phosphor-logging/log.hpp>
220bf1b782SLei YU 
230bf1b782SLei YU using namespace phosphor::logging;
240bf1b782SLei YU 
250bf1b782SLei YU int main(int argc, char** argv)
260bf1b782SLei YU {
27093b5917SLei YU 
28093b5917SLei YU     std::string psuPath;
29093b5917SLei YU     std::vector<std::string> versions;
30093b5917SLei YU     bool rawOutput = false;
31*d19df255SLei YU     std::vector<std::string> updateArguments;
32093b5917SLei YU 
33093b5917SLei YU     CLI::App app{"PSU utils app for OpenBMC"};
34093b5917SLei YU     auto action = app.add_option_group("Action");
35093b5917SLei YU     action->add_option("-g,--get-version", psuPath,
36093b5917SLei YU                        "Get PSU version from inventory path");
37093b5917SLei YU     action->add_option("-c,--compare", versions,
38093b5917SLei YU                        "Compare and get the latest version");
39*d19df255SLei YU     action
40*d19df255SLei YU         ->add_option("-u,--update", updateArguments,
41*d19df255SLei YU                      "Update PSU firmware, expecting two arguments: "
42*d19df255SLei YU                      "<PSU inventory path> <image-dir>")
43*d19df255SLei YU         ->expected(2);
44093b5917SLei YU     action->require_option(1); // Only one option is supported
45093b5917SLei YU     app.add_flag("--raw", rawOutput, "Output raw text without linefeed");
46093b5917SLei YU     CLI11_PARSE(app, argc, argv);
47093b5917SLei YU 
48093b5917SLei YU     std::string ret;
49093b5917SLei YU 
50093b5917SLei YU     if (!psuPath.empty())
510bf1b782SLei YU     {
52093b5917SLei YU         ret = version::getVersion(psuPath);
53093b5917SLei YU     }
54093b5917SLei YU     if (!versions.empty())
55093b5917SLei YU     {
56093b5917SLei YU         ret = version::getLatest(versions);
570bf1b782SLei YU     }
58*d19df255SLei YU     if (!updateArguments.empty())
59*d19df255SLei YU     {
60*d19df255SLei YU         assert(updateArguments.size() == 2);
61*d19df255SLei YU         if (updater::update(updateArguments[0], updateArguments[1]))
62*d19df255SLei YU         {
63*d19df255SLei YU             ret = "Update successful";
64*d19df255SLei YU         }
65*d19df255SLei YU     }
660bf1b782SLei YU 
67093b5917SLei YU     printf("%s", ret.c_str());
68093b5917SLei YU     if (!rawOutput)
69093b5917SLei YU     {
70093b5917SLei YU         printf("\n");
71093b5917SLei YU     }
72093b5917SLei YU     return ret.empty() ? 1 : 0;
730bf1b782SLei YU }
74