/* SPDX-License-Identifier: GPL-2.0-only */

#ifndef _TPM_SVSM_H_
#define _TPM_SVSM_H_

#include <linux/errno.h>
#include <linux/string.h>
#include <linux/types.h>

#define SVSM_VTPM_MAX_BUFFER		4096 


struct svsm_vtpm_request {
	u32 cmd;
};


struct svsm_vtpm_response {
	u32 size;
};


struct svsm_vtpm_cmd_request {
	u32 cmd;
	u8 locality;
	u32 buf_size;
	u8 buf[];
} __packed;


struct svsm_vtpm_cmd_response {
	u32 buf_size;
	u8 buf[];
};


static inline int
svsm_vtpm_cmd_request_fill(struct svsm_vtpm_cmd_request *req, u8 locality,
			   const u8 *buf, size_t len)
{
	if (len > SVSM_VTPM_MAX_BUFFER - sizeof(*req))
		return -EINVAL;

	req->cmd = 8; 
	req->locality = locality;
	req->buf_size = len;

	memcpy(req->buf, buf, len);

	return 0;
}


static inline int
svsm_vtpm_cmd_response_parse(const struct svsm_vtpm_cmd_response *resp, u8 *buf,
			     size_t len)
{
	if (len < resp->buf_size)
		return -E2BIG;

	if (resp->buf_size > SVSM_VTPM_MAX_BUFFER - sizeof(*resp))
		return -EINVAL;  // Invalid response from the platform TPM

	memcpy(buf, resp->buf, resp->buf_size);

	return resp->buf_size;
}

#endif 
