drivers/char/tpm/tpm1-cmd.c

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

File Facts

System
Linux kernel
Corpus path
drivers/char/tpm/tpm1-cmd.c
Extension
.c
Size
18864 bytes
Lines
809
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

struct tpm1_get_random_out {
	__be32 rng_data_len;
	u8 rng_data[TPM_MAX_RNG_DATA];
} __packed;

/**
 * tpm1_get_random() - get random bytes from the TPM's RNG
 * @chip:	a &struct tpm_chip instance
 * @dest:	destination buffer for the random bytes
 * @max:	the maximum number of bytes to write to @dest
 *
 * Return:
 * *  number of bytes read
 * * -errno (positive TPM return codes are masked to -EIO)
 */
int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
{
	struct tpm1_get_random_out *out;
	u32 num_bytes =  min_t(u32, max, TPM_MAX_RNG_DATA);
	struct tpm_buf buf;
	u32 total = 0;
	int retries = 5;
	u32 recd;
	int rc;

	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
	if (rc)
		return rc;

	do {
		tpm_buf_append_u32(&buf, num_bytes);

		rc = tpm_transmit_cmd(chip, &buf, sizeof(out->rng_data_len),
				      "attempting get random");
		if (rc) {
			if (rc > 0)
				rc = -EIO;
			goto out;
		}

		out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];

		recd = be32_to_cpu(out->rng_data_len);
		if (recd > num_bytes) {
			rc = -EFAULT;
			goto out;
		}

		if (tpm_buf_length(&buf) < TPM_HEADER_SIZE +
					   sizeof(out->rng_data_len) + recd) {
			rc = -EFAULT;
			goto out;
		}
		memcpy(dest, out->rng_data, recd);

		dest += recd;
		total += recd;
		num_bytes -= recd;

		tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
	} while (retries-- && total < max);

	rc = total ? (int)total : -EIO;
out:
	tpm_buf_destroy(&buf);
	return rc;
}

#define TPM_ORD_PCRREAD 21
int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
{
	struct tpm_buf buf;
	int rc;

	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCRREAD);
	if (rc)
		return rc;

	tpm_buf_append_u32(&buf, pcr_idx);

	rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE,
			      "attempting to read a pcr value");
	if (rc)
		goto out;

	if (tpm_buf_length(&buf) < TPM_DIGEST_SIZE) {
		rc = -EFAULT;
		goto out;
	}

Annotation

Implementation Notes