Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 1 | /** |
| 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 McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 16 | #include "model.hpp" |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 17 | #include "updater.hpp" |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 18 | #include "version.hpp" |
| 19 | |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 20 | #include <CLI/CLI.hpp> |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 22 | #include <sdbusplus/bus.hpp> |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 23 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 24 | #include <cassert> |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 25 | #include <filesystem> |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 26 | |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 27 | using namespace phosphor::logging; |
| 28 | |
| 29 | int main(int argc, char** argv) |
| 30 | { |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 31 | std::string psuPathVersion, psuPathModel; |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 32 | std::vector<std::string> versions; |
| 33 | bool rawOutput = false; |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 34 | std::vector<std::string> updateArguments; |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 35 | |
| 36 | CLI::App app{"PSU utils app for OpenBMC"}; |
| 37 | auto action = app.add_option_group("Action"); |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 38 | action->add_option("-g,--get-version", psuPathVersion, |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 39 | "Get PSU version from inventory path"); |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 40 | action->add_option("-m,--get-model", psuPathModel, |
| 41 | "Get PSU model from inventory path"); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 42 | action->add_option("-c,--compare", versions, |
| 43 | "Compare and get the latest version"); |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 44 | action |
| 45 | ->add_option("-u,--update", updateArguments, |
| 46 | "Update PSU firmware, expecting two arguments: " |
| 47 | "<PSU inventory path> <image-dir>") |
| 48 | ->expected(2); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 49 | 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 Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 55 | auto bus = sdbusplus::bus::new_default(); |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 56 | if (!psuPathVersion.empty()) |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 57 | { |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 58 | ret = version::getVersion(bus, psuPathVersion); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 59 | } |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 60 | if (!psuPathModel.empty()) |
| 61 | { |
| 62 | ret = model::getModel(bus, psuPathModel); |
| 63 | } |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 64 | if (!versions.empty()) |
| 65 | { |
| 66 | ret = version::getLatest(versions); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 67 | } |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 68 | if (!updateArguments.empty()) |
| 69 | { |
| 70 | assert(updateArguments.size() == 2); |
Faisal Awada | ec61bbd | 2024-11-04 08:46:20 -0600 | [diff] [blame] | 71 | if (updater::update(bus, updateArguments[0], updateArguments[1])) |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 72 | { |
| 73 | ret = "Update successful"; |
| 74 | } |
| 75 | } |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 76 | |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 77 | printf("%s", ret.c_str()); |
| 78 | if (!rawOutput) |
| 79 | { |
| 80 | printf("\n"); |
| 81 | } |
| 82 | return ret.empty() ? 1 : 0; |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 83 | } |