| // Copyright 2024 Google LLC | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| #include "version.hpp" | |
| #include "google3/ec_commands.h" | |
| #include "message_util.hpp" | |
| #include <sdbusplus/exception.hpp> | |
| #include <stdplus/print.hpp> | |
| #include <stdplus/raw.hpp> | |
| #include <xyz/openbmc_project/Control/Hoth/error.hpp> | |
| #include <cerrno> | |
| #include <span> | |
| #include <stdexcept> | |
| #include <vector> | |
| namespace google | |
| { | |
| namespace hoth | |
| { | |
| using internal::EC_RES_SUCCESS; | |
| using internal::HostCommand; | |
| using internal::RspHeader; | |
| using sdbusplus::error::xyz::openbmc_project::control::hoth::ResponseFailure; | |
| static ec_response_get_version getHothVersion(HostCommand* hostCmd) | |
| { | |
| uint8_t request = 0; | |
| std::vector<uint8_t> buf = | |
| hostCmd->sendCommand(EC_CMD_GET_VERSION, 0, &request, sizeof(request)); | |
| // Extract the response header out when returning the span | |
| std::span<const uint8_t> output = buf; | |
| const auto& rsp = stdplus::raw::extractRef<RspHeader>(output); | |
| if (rsp.result != EC_RES_SUCCESS) | |
| { | |
| stdplus::print( | |
| stderr, "Version command received a bad response from Hoth {:#x}\n", | |
| static_cast<uint16_t>(rsp.result)); | |
| throw ResponseFailure(); | |
| } | |
| return stdplus::raw::copyFrom<ec_response_get_version>(output); | |
| } | |
| std::string RoVersion::version() const | |
| { | |
| ec_response_get_version resp = getHothVersion(RoVersion::hostCmd); | |
| return resp.version_string_ro; | |
| } | |
| std::string RwVersion::version() const | |
| { | |
| ec_response_get_version resp = getHothVersion(RwVersion::hostCmd); | |
| return resp.version_string_rw; | |
| } | |
| std::string PayloadVersion::version() const | |
| { | |
| payload_status_response resp; | |
| try | |
| { | |
| resp = ecUtil->getPayloadStatus(); | |
| } | |
| catch (const std::exception& e) | |
| { | |
| stdplus::print(stderr, "Failed to get payload status: {}\n", e.what()); | |
| throw sdbusplus::exception::SdBusError(EIO, | |
| "Failed to get payload status"); | |
| } | |
| uint8_t active_half = resp.header.active_half; | |
| if (active_half >= resp.header.region_count || | |
| active_half >= PAYLOAD_STATUS_MAX_REGION_STATES) | |
| { | |
| stdplus::print( | |
| stderr, | |
| "Invalid active_half ({}) or region_count ({}) in payload status\n", | |
| active_half, resp.header.region_count); | |
| throw sdbusplus::exception::SdBusError( | |
| EBADMSG, "Invalid payload status response from hoth"); | |
| } | |
| return internal::payloadVersionString(resp.states[active_half]); | |
| } | |
| } // namespace hoth | |
| } // namespace google |