xref: /openbmc/phosphor-power/tools/power-utils/main.cpp (revision 23dee383ca3a45889fbb4f7bbae65fe3325c5805)
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*0fbc2f6aSShawn McCarney #include "model.hpp"
17d19df255SLei YU #include "updater.hpp"
180bf1b782SLei YU #include "version.hpp"
190bf1b782SLei YU 
20093b5917SLei YU #include <CLI/CLI.hpp>
210bf1b782SLei YU #include <phosphor-logging/log.hpp>
225dce1a74SFaisal Awada #include <sdbusplus/bus.hpp>
230bf1b782SLei YU 
24d1bc4cecSBrandon Wyman #include <cassert>
255dce1a74SFaisal Awada #include <filesystem>
26d1bc4cecSBrandon Wyman 
270bf1b782SLei YU using namespace phosphor::logging;
280bf1b782SLei YU 
main(int argc,char ** argv)290bf1b782SLei YU int main(int argc, char** argv)
300bf1b782SLei YU {
31*0fbc2f6aSShawn McCarney     std::string psuPathVersion, psuPathModel;
32093b5917SLei YU     std::vector<std::string> versions;
33093b5917SLei YU     bool rawOutput = false;
34d19df255SLei YU     std::vector<std::string> updateArguments;
35093b5917SLei YU 
36093b5917SLei YU     CLI::App app{"PSU utils app for OpenBMC"};
37093b5917SLei YU     auto action = app.add_option_group("Action");
38*0fbc2f6aSShawn McCarney     action->add_option("-g,--get-version", psuPathVersion,
39093b5917SLei YU                        "Get PSU version from inventory path");
40*0fbc2f6aSShawn McCarney     action->add_option("-m,--get-model", psuPathModel,
41*0fbc2f6aSShawn McCarney                        "Get PSU model from inventory path");
42093b5917SLei YU     action->add_option("-c,--compare", versions,
43093b5917SLei YU                        "Compare and get the latest version");
44d19df255SLei YU     action
45d19df255SLei YU         ->add_option("-u,--update", updateArguments,
46d19df255SLei YU                      "Update PSU firmware, expecting two arguments: "
47d19df255SLei YU                      "<PSU inventory path> <image-dir>")
48d19df255SLei YU         ->expected(2);
49093b5917SLei YU     action->require_option(1); // Only one option is supported
50093b5917SLei YU     app.add_flag("--raw", rawOutput, "Output raw text without linefeed");
51093b5917SLei YU     CLI11_PARSE(app, argc, argv);
52093b5917SLei YU 
53093b5917SLei YU     std::string ret;
54093b5917SLei YU 
555dce1a74SFaisal Awada     auto bus = sdbusplus::bus::new_default();
56*0fbc2f6aSShawn McCarney     if (!psuPathVersion.empty())
570bf1b782SLei YU     {
58*0fbc2f6aSShawn McCarney         ret = version::getVersion(bus, psuPathVersion);
595dce1a74SFaisal Awada     }
60*0fbc2f6aSShawn McCarney     if (!psuPathModel.empty())
61*0fbc2f6aSShawn McCarney     {
62*0fbc2f6aSShawn McCarney         ret = model::getModel(bus, psuPathModel);
63*0fbc2f6aSShawn McCarney     }
64093b5917SLei YU     if (!versions.empty())
65093b5917SLei YU     {
66093b5917SLei YU         ret = version::getLatest(versions);
670bf1b782SLei YU     }
68d19df255SLei YU     if (!updateArguments.empty())
69d19df255SLei YU     {
70d19df255SLei YU         assert(updateArguments.size() == 2);
71ec61bbd7SFaisal Awada         if (updater::update(bus, updateArguments[0], updateArguments[1]))
72d19df255SLei YU         {
73d19df255SLei YU             ret = "Update successful";
74d19df255SLei YU         }
75d19df255SLei YU     }
760bf1b782SLei YU 
77093b5917SLei YU     printf("%s", ret.c_str());
78093b5917SLei YU     if (!rawOutput)
79093b5917SLei YU     {
80093b5917SLei YU         printf("\n");
81093b5917SLei YU     }
82093b5917SLei YU     return ret.empty() ? 1 : 0;
830bf1b782SLei YU }
84