drivers/char/tpm/tpm-buf.c

Source file repositories/reference/linux-study-clean/drivers/char/tpm/tpm-buf.c

File Facts

System
Linux kernel
Corpus path
drivers/char/tpm/tpm-buf.c
Extension
.c
Size
5707 bytes
Lines
247
Domain
Driver Families
Bucket
drivers/char
Inferred role
Driver Families: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/*
 * Handling of TPM command and other buffers.
 */

#include <linux/tpm_command.h>
#include <linux/module.h>
#include <linux/tpm.h>

/**
 * tpm_buf_init() - Allocate and initialize a TPM command
 * @buf:	A &tpm_buf
 * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
 * @ordinal:	A command ordinal
 *
 * Return: 0 or -ENOMEM
 */
int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
{
	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
	if (!buf->data)
		return -ENOMEM;

	tpm_buf_reset(buf, tag, ordinal);
	return 0;
}
EXPORT_SYMBOL_GPL(tpm_buf_init);

/**
 * tpm_buf_reset() - Initialize a TPM command
 * @buf:	A &tpm_buf
 * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
 * @ordinal:	A command ordinal
 */
void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
{
	struct tpm_header *head = (struct tpm_header *)buf->data;

	WARN_ON(tag != TPM_TAG_RQU_COMMAND && tag != TPM2_ST_NO_SESSIONS &&
		tag != TPM2_ST_SESSIONS && tag != 0);

	buf->flags = 0;
	buf->length = sizeof(*head);
	head->tag = cpu_to_be16(tag);
	head->length = cpu_to_be32(sizeof(*head));
	head->ordinal = cpu_to_be32(ordinal);
	buf->handles = 0;
}
EXPORT_SYMBOL_GPL(tpm_buf_reset);

/**
 * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer
 * @buf:	A @tpm_buf
 *
 * Return: 0 or -ENOMEM
 */
int tpm_buf_init_sized(struct tpm_buf *buf)
{
	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
	if (!buf->data)
		return -ENOMEM;

	tpm_buf_reset_sized(buf);
	return 0;
}
EXPORT_SYMBOL_GPL(tpm_buf_init_sized);

/**
 * tpm_buf_reset_sized() - Initialize a sized buffer
 * @buf:	A &tpm_buf
 */
void tpm_buf_reset_sized(struct tpm_buf *buf)
{
	buf->flags = TPM_BUF_TPM2B;
	buf->length = 2;
	buf->data[0] = 0;
	buf->data[1] = 0;
}
EXPORT_SYMBOL_GPL(tpm_buf_reset_sized);

void tpm_buf_destroy(struct tpm_buf *buf)
{
	free_page((unsigned long)buf->data);
}
EXPORT_SYMBOL_GPL(tpm_buf_destroy);

/**
 * tpm_buf_length() - Return the number of bytes consumed by the data
 * @buf:	A &tpm_buf
 *

Annotation

Implementation Notes