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 | */ |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 16 | #include "updater.hpp" |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 17 | #include "version.hpp" |
| 18 | |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 19 | #include <CLI/CLI.hpp> |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 20 | #include <phosphor-logging/log.hpp> |
| 21 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 22 | #include <cassert> |
| 23 | |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 24 | using namespace phosphor::logging; |
| 25 | |
| 26 | int main(int argc, char** argv) |
| 27 | { |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 28 | |
| 29 | std::string psuPath; |
| 30 | std::vector<std::string> versions; |
| 31 | bool rawOutput = false; |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 32 | std::vector<std::string> updateArguments; |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 33 | |
| 34 | CLI::App app{"PSU utils app for OpenBMC"}; |
| 35 | auto action = app.add_option_group("Action"); |
| 36 | action->add_option("-g,--get-version", psuPath, |
| 37 | "Get PSU version from inventory path"); |
| 38 | action->add_option("-c,--compare", versions, |
| 39 | "Compare and get the latest version"); |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 40 | action |
| 41 | ->add_option("-u,--update", updateArguments, |
| 42 | "Update PSU firmware, expecting two arguments: " |
| 43 | "<PSU inventory path> <image-dir>") |
| 44 | ->expected(2); |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 45 | action->require_option(1); // Only one option is supported |
| 46 | app.add_flag("--raw", rawOutput, "Output raw text without linefeed"); |
| 47 | CLI11_PARSE(app, argc, argv); |
| 48 | |
| 49 | std::string ret; |
| 50 | |
| 51 | if (!psuPath.empty()) |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 52 | { |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 53 | ret = version::getVersion(psuPath); |
| 54 | } |
| 55 | if (!versions.empty()) |
| 56 | { |
| 57 | ret = version::getLatest(versions); |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 58 | } |
Lei YU | d19df25 | 2019-10-25 17:31:52 +0800 | [diff] [blame] | 59 | if (!updateArguments.empty()) |
| 60 | { |
| 61 | assert(updateArguments.size() == 2); |
| 62 | if (updater::update(updateArguments[0], updateArguments[1])) |
| 63 | { |
| 64 | ret = "Update successful"; |
| 65 | } |
| 66 | } |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 67 | |
Lei YU | 093b591 | 2019-10-22 15:28:51 +0800 | [diff] [blame] | 68 | printf("%s", ret.c_str()); |
| 69 | if (!rawOutput) |
| 70 | { |
| 71 | printf("\n"); |
| 72 | } |
| 73 | return ret.empty() ? 1 : 0; |
Lei YU | 0bf1b78 | 2019-08-29 16:02:30 +0800 | [diff] [blame] | 74 | } |