10bf1b782SLei YU /** 20bf1b782SLei YU * Copyright © 2019 IBM Corporation 30bf1b782SLei YU * 40bf1b782SLei YU * Licensed under the Apache License, Version 2.0 (the "License"); 50bf1b782SLei YU * you may not use this file except in compliance with the License. 60bf1b782SLei YU * You may obtain a copy of the License at 70bf1b782SLei YU * 80bf1b782SLei YU * http://www.apache.org/licenses/LICENSE-2.0 90bf1b782SLei YU * 100bf1b782SLei YU * Unless required by applicable law or agreed to in writing, software 110bf1b782SLei YU * distributed under the License is distributed on an "AS IS" BASIS, 120bf1b782SLei YU * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130bf1b782SLei YU * See the License for the specific language governing permissions and 140bf1b782SLei YU * limitations under the License. 150bf1b782SLei YU */ 16*5dce1a74SFaisal Awada #include "config.h" 17*5dce1a74SFaisal Awada 18d19df255SLei YU #include "updater.hpp" 190bf1b782SLei YU #include "version.hpp" 200bf1b782SLei YU 21093b5917SLei YU #include <CLI/CLI.hpp> 220bf1b782SLei YU #include <phosphor-logging/log.hpp> 23*5dce1a74SFaisal Awada #include <sdbusplus/bus.hpp> 240bf1b782SLei YU 25d1bc4cecSBrandon Wyman #include <cassert> 26*5dce1a74SFaisal Awada #include <filesystem> 27d1bc4cecSBrandon Wyman 280bf1b782SLei YU using namespace phosphor::logging; 290bf1b782SLei YU 300bf1b782SLei YU int main(int argc, char** argv) 310bf1b782SLei YU { 32093b5917SLei YU std::string psuPath; 33093b5917SLei YU std::vector<std::string> versions; 34093b5917SLei YU bool rawOutput = false; 35d19df255SLei YU std::vector<std::string> updateArguments; 36093b5917SLei YU 37093b5917SLei YU CLI::App app{"PSU utils app for OpenBMC"}; 38093b5917SLei YU auto action = app.add_option_group("Action"); 39093b5917SLei YU action->add_option("-g,--get-version", psuPath, 40093b5917SLei YU "Get PSU version from inventory path"); 41093b5917SLei YU action->add_option("-c,--compare", versions, 42093b5917SLei YU "Compare and get the latest version"); 43d19df255SLei YU action 44d19df255SLei YU ->add_option("-u,--update", updateArguments, 45d19df255SLei YU "Update PSU firmware, expecting two arguments: " 46d19df255SLei YU "<PSU inventory path> <image-dir>") 47d19df255SLei YU ->expected(2); 48093b5917SLei YU action->require_option(1); // Only one option is supported 49093b5917SLei YU app.add_flag("--raw", rawOutput, "Output raw text without linefeed"); 50093b5917SLei YU CLI11_PARSE(app, argc, argv); 51093b5917SLei YU 52093b5917SLei YU std::string ret; 53093b5917SLei YU 54*5dce1a74SFaisal Awada bool useJsonFile = version::utils::checkFileExists(PSU_JSON_PATH); 55*5dce1a74SFaisal Awada auto bus = sdbusplus::bus::new_default(); 56093b5917SLei YU if (!psuPath.empty()) 570bf1b782SLei YU { 58*5dce1a74SFaisal Awada if (!useJsonFile) 59*5dce1a74SFaisal Awada { 60*5dce1a74SFaisal Awada ret = version::getVersion(bus, psuPath); 61*5dce1a74SFaisal Awada } 62*5dce1a74SFaisal Awada else 63*5dce1a74SFaisal Awada { 64093b5917SLei YU ret = version::getVersion(psuPath); 65093b5917SLei YU } 66*5dce1a74SFaisal Awada } 67093b5917SLei YU if (!versions.empty()) 68093b5917SLei YU { 69093b5917SLei YU ret = version::getLatest(versions); 700bf1b782SLei YU } 71d19df255SLei YU if (!updateArguments.empty()) 72d19df255SLei YU { 73d19df255SLei YU assert(updateArguments.size() == 2); 74d19df255SLei YU if (updater::update(updateArguments[0], updateArguments[1])) 75d19df255SLei YU { 76d19df255SLei YU ret = "Update successful"; 77d19df255SLei YU } 78d19df255SLei YU } 790bf1b782SLei YU 80093b5917SLei YU printf("%s", ret.c_str()); 81093b5917SLei YU if (!rawOutput) 82093b5917SLei YU { 83093b5917SLei YU printf("\n"); 84093b5917SLei YU } 85093b5917SLei YU return ret.empty() ? 1 : 0; 860bf1b782SLei YU } 87