blob: 236f0d65162ebf505c6568c79f29715c55e1cb81 [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 */
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060016#include "model.hpp"
Lei YUd19df252019-10-25 17:31:52 +080017#include "updater.hpp"
Lei YU0bf1b782019-08-29 16:02:30 +080018#include "version.hpp"
19
Lei YU093b5912019-10-22 15:28:51 +080020#include <CLI/CLI.hpp>
Lei YU0bf1b782019-08-29 16:02:30 +080021#include <phosphor-logging/log.hpp>
Faisal Awada5dce1a72024-08-19 15:51:44 -050022#include <sdbusplus/bus.hpp>
Lei YU0bf1b782019-08-29 16:02:30 +080023
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060024#include <cassert>
Faisal Awada5dce1a72024-08-19 15:51:44 -050025#include <filesystem>
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060026
Lei YU0bf1b782019-08-29 16:02:30 +080027using namespace phosphor::logging;
28
29int main(int argc, char** argv)
30{
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060031 std::string psuPathVersion, psuPathModel;
Lei YU093b5912019-10-22 15:28:51 +080032 std::vector<std::string> versions;
33 bool rawOutput = false;
Lei YUd19df252019-10-25 17:31:52 +080034 std::vector<std::string> updateArguments;
Lei YU093b5912019-10-22 15:28:51 +080035
36 CLI::App app{"PSU utils app for OpenBMC"};
37 auto action = app.add_option_group("Action");
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060038 action->add_option("-g,--get-version", psuPathVersion,
Lei YU093b5912019-10-22 15:28:51 +080039 "Get PSU version from inventory path");
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060040 action->add_option("-m,--get-model", psuPathModel,
41 "Get PSU model from inventory path");
Lei YU093b5912019-10-22 15:28:51 +080042 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
Faisal Awada5dce1a72024-08-19 15:51:44 -050055 auto bus = sdbusplus::bus::new_default();
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060056 if (!psuPathVersion.empty())
Lei YU0bf1b782019-08-29 16:02:30 +080057 {
Shawn McCarney23dee382024-11-11 18:41:49 -060058 ret = version::getVersion(bus, psuPathVersion);
Lei YU093b5912019-10-22 15:28:51 +080059 }
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060060 if (!psuPathModel.empty())
61 {
62 ret = model::getModel(bus, psuPathModel);
63 }
Lei YU093b5912019-10-22 15:28:51 +080064 if (!versions.empty())
65 {
66 ret = version::getLatest(versions);
Lei YU0bf1b782019-08-29 16:02:30 +080067 }
Lei YUd19df252019-10-25 17:31:52 +080068 if (!updateArguments.empty())
69 {
70 assert(updateArguments.size() == 2);
Faisal Awadaec61bbd2024-11-04 08:46:20 -060071 if (updater::update(bus, updateArguments[0], updateArguments[1]))
Lei YUd19df252019-10-25 17:31:52 +080072 {
73 ret = "Update successful";
74 }
75 }
Lei YU0bf1b782019-08-29 16:02:30 +080076
Lei YU093b5912019-10-22 15:28:51 +080077 printf("%s", ret.c_str());
78 if (!rawOutput)
79 {
80 printf("\n");
81 }
82 return ret.empty() ? 1 : 0;
Lei YU0bf1b782019-08-29 16:02:30 +080083}