blob: ed6307140efd96417317ef8f5f6f725d29a2be75 [file] [log] [blame]
Lei YU0bf1b782019-08-29 16:02:30 +08001/**
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 */
Lei YUd19df252019-10-25 17:31:52 +080016#include "updater.hpp"
Lei YU0bf1b782019-08-29 16:02:30 +080017#include "version.hpp"
18
Lei YU093b5912019-10-22 15:28:51 +080019#include <CLI/CLI.hpp>
Lei YU0bf1b782019-08-29 16:02:30 +080020#include <phosphor-logging/log.hpp>
21
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060022#include <cassert>
23
Lei YU0bf1b782019-08-29 16:02:30 +080024using namespace phosphor::logging;
25
26int main(int argc, char** argv)
27{
Lei YU093b5912019-10-22 15:28:51 +080028 std::string psuPath;
29 std::vector<std::string> versions;
30 bool rawOutput = false;
Lei YUd19df252019-10-25 17:31:52 +080031 std::vector<std::string> updateArguments;
Lei YU093b5912019-10-22 15:28:51 +080032
33 CLI::App app{"PSU utils app for OpenBMC"};
34 auto action = app.add_option_group("Action");
35 action->add_option("-g,--get-version", psuPath,
36 "Get PSU version from inventory path");
37 action->add_option("-c,--compare", versions,
38 "Compare and get the latest version");
Lei YUd19df252019-10-25 17:31:52 +080039 action
40 ->add_option("-u,--update", updateArguments,
41 "Update PSU firmware, expecting two arguments: "
42 "<PSU inventory path> <image-dir>")
43 ->expected(2);
Lei YU093b5912019-10-22 15:28:51 +080044 action->require_option(1); // Only one option is supported
45 app.add_flag("--raw", rawOutput, "Output raw text without linefeed");
46 CLI11_PARSE(app, argc, argv);
47
48 std::string ret;
49
50 if (!psuPath.empty())
Lei YU0bf1b782019-08-29 16:02:30 +080051 {
Lei YU093b5912019-10-22 15:28:51 +080052 ret = version::getVersion(psuPath);
53 }
54 if (!versions.empty())
55 {
56 ret = version::getLatest(versions);
Lei YU0bf1b782019-08-29 16:02:30 +080057 }
Lei YUd19df252019-10-25 17:31:52 +080058 if (!updateArguments.empty())
59 {
60 assert(updateArguments.size() == 2);
61 if (updater::update(updateArguments[0], updateArguments[1]))
62 {
63 ret = "Update successful";
64 }
65 }
Lei YU0bf1b782019-08-29 16:02:30 +080066
Lei YU093b5912019-10-22 15:28:51 +080067 printf("%s", ret.c_str());
68 if (!rawOutput)
69 {
70 printf("\n");
71 }
72 return ret.empty() ? 1 : 0;
Lei YU0bf1b782019-08-29 16:02:30 +080073}