blob: 6027049ce292eb4d3ab4920bc882d8c56ec5dd1e [file]
// 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 "google3/host_commands.h"
#include "ec_util_interface.hpp"
#include "host_command.hpp"
#include <cstddef>
#include <cstdint>
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;
// 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;
secure_boot_enforcement_state getSecureBootEnforcementState()
const override;
// Sets the TPM locality to `locality` via EC_PRV_CMD_HOTH_TPM_CONTROL.
void setTpmLocality(uint8_t locality) const override;
// Gets the TPM locality via EC_PRV_CMD_HOTH_TPM_CONTROL.
uint8_t getTpmLocality() const override;
payload_status_response getPayloadStatus() 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