blob: 5adbb1bd1e3e08c3b777ca9b445b2bf5b344c5a6 [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"
Faisal Awadaf9b426b2025-01-31 11:44:48 -060018#include "utility.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>
Faisal Awadaf9b426b2025-01-31 11:44:48 -060022#include <phosphor-logging/lg2.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{
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060032 std::string psuPathVersion, psuPathModel;
Lei YU093b5912019-10-22 15:28:51 +080033 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");
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060039 action->add_option("-g,--get-version", psuPathVersion,
Lei YU093b5912019-10-22 15:28:51 +080040 "Get PSU version from inventory path");
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060041 action->add_option("-m,--get-model", psuPathModel,
42 "Get PSU model from inventory path");
Lei YU093b5912019-10-22 15:28:51 +080043 action->add_option("-c,--compare", versions,
44 "Compare and get the latest version");
Lei YUd19df252019-10-25 17:31:52 +080045 action
46 ->add_option("-u,--update", updateArguments,
47 "Update PSU firmware, expecting two arguments: "
48 "<PSU inventory path> <image-dir>")
49 ->expected(2);
Lei YU093b5912019-10-22 15:28:51 +080050 action->require_option(1); // Only one option is supported
51 app.add_flag("--raw", rawOutput, "Output raw text without linefeed");
52 CLI11_PARSE(app, argc, argv);
53
54 std::string ret;
55
Faisal Awada5dce1a72024-08-19 15:51:44 -050056 auto bus = sdbusplus::bus::new_default();
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060057 if (!psuPathVersion.empty())
Lei YU0bf1b782019-08-29 16:02:30 +080058 {
Shawn McCarney23dee382024-11-11 18:41:49 -060059 ret = version::getVersion(bus, psuPathVersion);
Lei YU093b5912019-10-22 15:28:51 +080060 }
Shawn McCarney0fbc2f62024-11-05 17:01:53 -060061 if (!psuPathModel.empty())
62 {
63 ret = model::getModel(bus, psuPathModel);
64 }
Lei YU093b5912019-10-22 15:28:51 +080065 if (!versions.empty())
66 {
67 ret = version::getLatest(versions);
Lei YU0bf1b782019-08-29 16:02:30 +080068 }
Lei YUd19df252019-10-25 17:31:52 +080069 if (!updateArguments.empty())
70 {
71 assert(updateArguments.size() == 2);
Faisal Awadaec61bbd2024-11-04 08:46:20 -060072 if (updater::update(bus, updateArguments[0], updateArguments[1]))
Lei YUd19df252019-10-25 17:31:52 +080073 {
74 ret = "Update successful";
Faisal Awadaf9b426b2025-01-31 11:44:48 -060075 lg2::info("Successful update to PSU: {PSU}", "PSU",
76 updateArguments[0]);
77 }
78 else
79 {
80 lg2::error("Failed to update PSU: {PSU}", "PSU",
81 updateArguments[0]);
Lei YUd19df252019-10-25 17:31:52 +080082 }
83 }
Lei YU0bf1b782019-08-29 16:02:30 +080084
Lei YU093b5912019-10-22 15:28:51 +080085 printf("%s", ret.c_str());
86 if (!rawOutput)
87 {
88 printf("\n");
89 }
90 return ret.empty() ? 1 : 0;
Lei YU0bf1b782019-08-29 16:02:30 +080091}