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" |
Faisal Awada | f9b426b | 2025-01-31 11:44:48 -0600 | [diff] [blame] | 18 | #include "utility.hpp" |
Faisal Awada | fe5b5c6 | 2025-03-22 10:50:01 -0500 | [diff] [blame] | 19 | #include "validator.hpp" |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 20 | #include "version.hpp" |
| 21 | |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 22 | #include <CLI/CLI.hpp> |
Faisal Awada | f9b426b | 2025-01-31 11:44:48 -0600 | [diff] [blame] | 23 | #include <phosphor-logging/lg2.hpp> |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 24 | #include <sdbusplus/bus.hpp> |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 25 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 26 | #include <cassert> |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 27 | #include <filesystem> |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 28 | |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 29 | using namespace phosphor::logging; |
| 30 | |
| 31 | int main(int argc, char** argv) |
| 32 | { |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 33 | std::string psuPathVersion, psuPathModel; |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 34 | std::vector<std::string> versions; |
Faisal Awada | fe5b5c6 | 2025-03-22 10:50:01 -0500 | [diff] [blame] | 35 | bool validateUpdate = false; |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 36 | bool rawOutput = false; |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 37 | std::vector<std::string> updateArguments; |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 38 | |
| 39 | CLI::App app{"PSU utils app for OpenBMC"}; |
| 40 | auto action = app.add_option_group("Action"); |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 41 | action->add_option("-g,--get-version", psuPathVersion, |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 42 | "Get PSU version from inventory path"); |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 43 | action->add_option("-m,--get-model", psuPathModel, |
| 44 | "Get PSU model from inventory path"); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 45 | action->add_option("-c,--compare", versions, |
| 46 | "Compare and get the latest version"); |
Faisal Awada | fe5b5c6 | 2025-03-22 10:50:01 -0500 | [diff] [blame] | 47 | auto updateOpt = |
| 48 | action |
| 49 | ->add_option("-u,--update", updateArguments, |
| 50 | "Update PSU firmware, expecting two arguments: " |
| 51 | "<PSU inventory path> <image-dir>") |
| 52 | ->expected(2) |
| 53 | ->type_name("PSU_PATH IMAGE_DIR"); |
| 54 | app.add_flag( |
| 55 | "--validate", validateUpdate, |
| 56 | "Validate number of present PSU vs number of required PSUs and all PSUs have same model before updating firmware") |
| 57 | ->needs(updateOpt); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 58 | action->require_option(1); // Only one option is supported |
| 59 | app.add_flag("--raw", rawOutput, "Output raw text without linefeed"); |
| 60 | CLI11_PARSE(app, argc, argv); |
| 61 | |
| 62 | std::string ret; |
| 63 | |
Faisal Awada | 5dce1a7 | 2024-08-19 15:51:44 -0500 | [diff] [blame] | 64 | auto bus = sdbusplus::bus::new_default(); |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 65 | if (!psuPathVersion.empty()) |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 66 | { |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 67 | ret = version::getVersion(bus, psuPathVersion); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 68 | } |
Shawn McCarney | 0fbc2f6 | 2024-11-05 17:01:53 -0600 | [diff] [blame] | 69 | if (!psuPathModel.empty()) |
| 70 | { |
| 71 | ret = model::getModel(bus, psuPathModel); |
| 72 | } |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 73 | if (!versions.empty()) |
| 74 | { |
| 75 | ret = version::getLatest(versions); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 76 | } |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 77 | if (!updateArguments.empty()) |
| 78 | { |
| 79 | assert(updateArguments.size() == 2); |
Faisal Awada | fe5b5c6 | 2025-03-22 10:50:01 -0500 | [diff] [blame] | 80 | bool updateStatus = false; |
| 81 | if (validateUpdate) |
| 82 | { |
| 83 | updateStatus = updater::validateAndUpdate(bus, updateArguments[0], |
| 84 | updateArguments[1]); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | updateStatus = |
| 89 | updater::update(bus, updateArguments[0], updateArguments[1]); |
| 90 | } |
| 91 | if (updateStatus) |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 92 | { |
| 93 | ret = "Update successful"; |
Faisal Awada | f9b426b | 2025-01-31 11:44:48 -0600 | [diff] [blame] | 94 | lg2::info("Successful update to PSU: {PSU}", "PSU", |
| 95 | updateArguments[0]); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | lg2::error("Failed to update PSU: {PSU}", "PSU", |
| 100 | updateArguments[0]); |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 101 | } |
| 102 | } |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 103 | |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 104 | printf("%s", ret.c_str()); |
| 105 | if (!rawOutput) |
| 106 | { |
| 107 | printf("\n"); |
| 108 | } |
| 109 | return ret.empty() ? 1 : 0; |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 110 | } |