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  */
16 #include "model.hpp"
17 #include "updater.hpp"
18 #include "version.hpp"
19 
20 #include <CLI/CLI.hpp>
21 #include <phosphor-logging/log.hpp>
22 #include <sdbusplus/bus.hpp>
23 
24 #include <cassert>
25 #include <filesystem>
26 
27 using namespace phosphor::logging;
28 
main(int argc,char ** argv)29 int main(int argc, char** argv)
30 {
31     std::string psuPathVersion, psuPathModel;
32     std::vector<std::string> versions;
33     bool rawOutput = false;
34     std::vector<std::string> updateArguments;
35 
36     CLI::App app{"PSU utils app for OpenBMC"};
37     auto action = app.add_option_group("Action");
38     action->add_option("-g,--get-version", psuPathVersion,
39                        "Get PSU version from inventory path");
40     action->add_option("-m,--get-model", psuPathModel,
41                        "Get PSU model from inventory path");
42     action->add_option("-c,--compare", versions,
43                        "Compare and get the latest version");
44     action
45         ->add_option("-u,--update", updateArguments,
46                      "Update PSU firmware, expecting two arguments: "
47                      "<PSU inventory path> <image-dir>")
48         ->expected(2);
49     action->require_option(1); // Only one option is supported
50     app.add_flag("--raw", rawOutput, "Output raw text without linefeed");
51     CLI11_PARSE(app, argc, argv);
52 
53     std::string ret;
54 
55     auto bus = sdbusplus::bus::new_default();
56     if (!psuPathVersion.empty())
57     {
58         ret = version::getVersion(bus, psuPathVersion);
59     }
60     if (!psuPathModel.empty())
61     {
62         ret = model::getModel(bus, psuPathModel);
63     }
64     if (!versions.empty())
65     {
66         ret = version::getLatest(versions);
67     }
68     if (!updateArguments.empty())
69     {
70         assert(updateArguments.size() == 2);
71         if (updater::update(bus, updateArguments[0], updateArguments[1]))
72         {
73             ret = "Update successful";
74         }
75     }
76 
77     printf("%s", ret.c_str());
78     if (!rawOutput)
79     {
80         printf("\n");
81     }
82     return ret.empty() ? 1 : 0;
83 }
84