blob: 8fae3a1fe15aaced9319894616a86cc0aea38252 [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"
Shawn McCarney14572cf2024-11-06 12:17:57 -060019#include "utils.hpp"
Lei YU0bf1b782019-08-29 16:02:30 +080020#include "version.hpp"
21
Lei YU093b5912019-10-22 15:28:51 +080022#include <CLI/CLI.hpp>
Lei YU0bf1b782019-08-29 16:02:30 +080023#include <phosphor-logging/log.hpp>
Faisal Awada5dce1a72024-08-19 15:51:44 -050024#include <sdbusplus/bus.hpp>
Lei YU0bf1b782019-08-29 16:02:30 +080025
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060026#include <cassert>
Faisal Awada5dce1a72024-08-19 15:51:44 -050027#include <filesystem>
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060028
Lei YU0bf1b782019-08-29 16:02:30 +080029using namespace phosphor::logging;
30
31int main(int argc, char** argv)
32{
Lei YU093b5912019-10-22 15:28:51 +080033 std::string psuPath;
34 std::vector<std::string> versions;
35 bool rawOutput = false;
Lei YUd19df252019-10-25 17:31:52 +080036 std::vector<std::string> updateArguments;
Lei YU093b5912019-10-22 15:28:51 +080037
38 CLI::App app{"PSU utils app for OpenBMC"};
39 auto action = app.add_option_group("Action");
40 action->add_option("-g,--get-version", psuPath,
41 "Get PSU version from inventory path");
42 action->add_option("-c,--compare", versions,
43 "Compare and get the latest version");
Lei YUd19df252019-10-25 17:31:52 +080044 action
45 ->add_option("-u,--update", updateArguments,
46 "Update PSU firmware, expecting two arguments: "
47 "<PSU inventory path> <image-dir>")
48 ->expected(2);
Lei YU093b5912019-10-22 15:28:51 +080049 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
Shawn McCarney14572cf2024-11-06 12:17:57 -060055 bool useJsonFile = utils::checkFileExists(PSU_JSON_PATH);
Faisal Awada5dce1a72024-08-19 15:51:44 -050056 auto bus = sdbusplus::bus::new_default();
Lei YU093b5912019-10-22 15:28:51 +080057 if (!psuPath.empty())
Lei YU0bf1b782019-08-29 16:02:30 +080058 {
Faisal Awada5dce1a72024-08-19 15:51:44 -050059 if (!useJsonFile)
60 {
61 ret = version::getVersion(bus, psuPath);
62 }
63 else
64 {
65 ret = version::getVersion(psuPath);
66 }
Lei YU093b5912019-10-22 15:28:51 +080067 }
68 if (!versions.empty())
69 {
70 ret = version::getLatest(versions);
Lei YU0bf1b782019-08-29 16:02:30 +080071 }
Lei YUd19df252019-10-25 17:31:52 +080072 if (!updateArguments.empty())
73 {
74 assert(updateArguments.size() == 2);
Faisal Awadaec61bbd2024-11-04 08:46:20 -060075 if (updater::update(bus, updateArguments[0], updateArguments[1]))
Lei YUd19df252019-10-25 17:31:52 +080076 {
77 ret = "Update successful";
78 }
79 }
Lei YU0bf1b782019-08-29 16:02:30 +080080
Lei YU093b5912019-10-22 15:28:51 +080081 printf("%s", ret.c_str());
82 if (!rawOutput)
83 {
84 printf("\n");
85 }
86 return ret.empty() ? 1 : 0;
Lei YU0bf1b782019-08-29 16:02:30 +080087}