blob: f240ecea2ae2d5f02e88825cf786243067e36a4f [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 */
Faisal Awada5dce1a72024-08-19 15:51:44 -050016#include "config.h"
17
Lei YUd19df252019-10-25 17:31:52 +080018#include "updater.hpp"
Lei YU0bf1b782019-08-29 16:02:30 +080019#include "version.hpp"
20
Lei YU093b5912019-10-22 15:28:51 +080021#include <CLI/CLI.hpp>
Lei YU0bf1b782019-08-29 16:02:30 +080022#include <phosphor-logging/log.hpp>
Faisal Awada5dce1a72024-08-19 15:51:44 -050023#include <sdbusplus/bus.hpp>
Lei YU0bf1b782019-08-29 16:02:30 +080024
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060025#include <cassert>
Faisal Awada5dce1a72024-08-19 15:51:44 -050026#include <filesystem>
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060027
Lei YU0bf1b782019-08-29 16:02:30 +080028using namespace phosphor::logging;
29
30int main(int argc, char** argv)
31{
Lei YU093b5912019-10-22 15:28:51 +080032 std::string psuPath;
33 std::vector<std::string> versions;
34 bool rawOutput = false;
Lei YUd19df252019-10-25 17:31:52 +080035 std::vector<std::string> updateArguments;
Lei YU093b5912019-10-22 15:28:51 +080036
37 CLI::App app{"PSU utils app for OpenBMC"};
38 auto action = app.add_option_group("Action");
39 action->add_option("-g,--get-version", psuPath,
40 "Get PSU version from inventory path");
41 action->add_option("-c,--compare", versions,
42 "Compare and get the latest version");
Lei YUd19df252019-10-25 17:31:52 +080043 action
44 ->add_option("-u,--update", updateArguments,
45 "Update PSU firmware, expecting two arguments: "
46 "<PSU inventory path> <image-dir>")
47 ->expected(2);
Lei YU093b5912019-10-22 15:28:51 +080048 action->require_option(1); // Only one option is supported
49 app.add_flag("--raw", rawOutput, "Output raw text without linefeed");
50 CLI11_PARSE(app, argc, argv);
51
52 std::string ret;
53
Faisal Awada5dce1a72024-08-19 15:51:44 -050054 bool useJsonFile = version::utils::checkFileExists(PSU_JSON_PATH);
55 auto bus = sdbusplus::bus::new_default();
Lei YU093b5912019-10-22 15:28:51 +080056 if (!psuPath.empty())
Lei YU0bf1b782019-08-29 16:02:30 +080057 {
Faisal Awada5dce1a72024-08-19 15:51:44 -050058 if (!useJsonFile)
59 {
60 ret = version::getVersion(bus, psuPath);
61 }
62 else
63 {
64 ret = version::getVersion(psuPath);
65 }
Lei YU093b5912019-10-22 15:28:51 +080066 }
67 if (!versions.empty())
68 {
69 ret = version::getLatest(versions);
Lei YU0bf1b782019-08-29 16:02:30 +080070 }
Lei YUd19df252019-10-25 17:31:52 +080071 if (!updateArguments.empty())
72 {
73 assert(updateArguments.size() == 2);
Faisal Awadaec61bbd2024-11-04 08:46:20 -060074 if (updater::update(bus, updateArguments[0], updateArguments[1]))
Lei YUd19df252019-10-25 17:31:52 +080075 {
76 ret = "Update successful";
77 }
78 }
Lei YU0bf1b782019-08-29 16:02:30 +080079
Lei YU093b5912019-10-22 15:28:51 +080080 printf("%s", ret.c_str());
81 if (!rawOutput)
82 {
83 printf("\n");
84 }
85 return ret.empty() ? 1 : 0;
Lei YU0bf1b782019-08-29 16:02:30 +080086}