| // 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. |
| |
| #pragma once |
| |
| #include <cstddef> |
| #include <cstdint> |
| |
| #include "ec_util_interface.hpp" |
| #include "google3/host_commands.h" |
| #include "host_command.hpp" |
| |
| namespace google { |
| namespace hoth { |
| namespace internal { |
| |
| class CommandNotSupportedException : public std::exception { |
| public: |
| explicit CommandNotSupportedException(uint16_t command) : command(command) { |
| std::stringstream ss; |
| ss << "EC Command 0x" << std::hex << command << " not supported"; |
| this->message = ss.str(); |
| } |
| const char* what() const noexcept override { return message.c_str(); } |
| |
| uint16_t get_command() const { return command; } |
| |
| private: |
| uint16_t command; |
| std::string message; |
| }; |
| |
| class CommandRunException : public std::exception { |
| public: |
| explicit CommandRunException(uint16_t command, uint16_t result) |
| : command(command), result(result) { |
| std::stringstream ss; |
| ss << "EC Command 0x" << std::hex << command << " returned error 0x" |
| << std::hex << result; |
| this->message = ss.str(); |
| } |
| const char* what() const noexcept override { return message.c_str(); } |
| |
| uint16_t get_command() const { return command; } |
| uint16_t get_result() const { return result; } |
| |
| private: |
| uint16_t command; |
| uint16_t result; |
| std::string message; |
| }; |
| |
| class EcUtilImpl : public EcUtil { |
| public: |
| explicit EcUtilImpl(HostCommand* hostCmd) : hostCmd(hostCmd) {} |
| |
| // Check wether a command is supported and authorized. |
| bool isHostCommandSupported(uint16_t cmd) const override; |
| |
| // Get the Hoth statistics by issuing an EC_PRV_CMD_HOTH_GET_STATISTICS |
| // Command. |
| ec_response_statistics getHothStatistics() const override; |
| |
| // Get the hardware information from Hoth chip by issuing an |
| // EC_PRV_CMD_HOTH_CHIP_INFO command. |
| ec_response_chip_info getHothChipInfo() const override; |
| |
| // Get the Hoth persistent panic info by issuing a series of |
| // EC_PRV_CMD_HOTH_PERSISTENT_PANIC_INFO commands. |
| std::optional<ec_response_persistent_panic_info> getHothPersistentPanicInfo() |
| const; |
| |
| // Check the presence of Hoth peresistent panic info by issuing a single |
| // EC_PRV_CMD_HOTH_PERSISTENT_PANIC_INFO command. |
| bool checkHothPersistentPanicInfo() const override; |
| |
| // Get the Hoth auth record. |
| // Only supports record id = 0, which is the only one currently used. |
| ec_authz_record_get_response getHothAuthRecord() const override; |
| |
| ec_response_key_rotation_status getHothKeyRotationStatus() const override; |
| |
| private: |
| [[nodiscard]] std::vector<uint8_t> sendCommand(uint16_t command, |
| uint8_t* request_ptr, |
| size_t request_size) const; |
| |
| /** @brief Connection to Hoth for sending and receiving host commands */ |
| HostCommand* hostCmd; |
| }; |
| |
| } // namespace internal |
| |
| } // namespace hoth |
| |
| } // namespace google |