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