Skip to content

Commit 412eb58

Browse files
author
Jarkko Sakkinen
committed
tpm: use tpm_buf in tpm_transmit_cmd() as the IO parameter
Since we pass an initialized struct tpm_buf instance in every call site now, it is cleaner to pass that directly to the tpm_transmit_cmd() as the TPM command/response buffer. Fine-tune a little bit tpm_transmit() and tpm_transmit_cmd() comments while doing this. Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com> Tested-by: Stefan Berger <stefanb@linux.ibm.com> Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com> Reviewed-by: James Bottomley <James.Bottomley@HansenPartnership.com> Tested-by: Alexander Steffen <Alexander.Steffen@infineon.com>
1 parent 36ce089 commit 412eb58

File tree

7 files changed

+64
-80
lines changed

7 files changed

+64
-80
lines changed

drivers/char/tpm/tpm-interface.c

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,22 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip,
307307

308308
/**
309309
* tpm_transmit - Internal kernel interface to transmit TPM commands.
310+
* @chip: a TPM chip to use
311+
* @space: a TPM space
312+
* @buf: a TPM command buffer
313+
* @bufsiz: length of the TPM command buffer
314+
* @flags: TPM transmit flags
310315
*
311-
* @chip: TPM chip to use
312-
* @space: tpm space
313-
* @buf: TPM command buffer
314-
* @bufsiz: length of the TPM command buffer
315-
* @flags: tpm transmit flags - bitmap
316+
* A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
317+
* the TPM and retransmits the command after a delay up to a maximum wait of
318+
* TPM2_DURATION_LONG.
316319
*
317-
* A wrapper around tpm_try_transmit that handles TPM2_RC_RETRY
318-
* returns from the TPM and retransmits the command after a delay up
319-
* to a maximum wait of TPM2_DURATION_LONG.
320-
*
321-
* Note: TPM1 never returns TPM2_RC_RETRY so the retry logic is TPM2
322-
* only
320+
* Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
321+
* only.
323322
*
324323
* Return:
325-
* the length of the return when the operation is successful.
326-
* A negative number for system errors (errno).
324+
* * The response length - OK
325+
* * -errno - A system error
327326
*/
328327
ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
329328
u8 *buf, size_t bufsiz, unsigned int flags)
@@ -374,33 +373,31 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
374373
}
375374
return ret;
376375
}
376+
377377
/**
378378
* tpm_transmit_cmd - send a tpm command to the device
379-
* The function extracts tpm out header return code
380-
*
381-
* @chip: TPM chip to use
382-
* @space: tpm space
383-
* @buf: TPM command buffer
384-
* @bufsiz: length of the buffer
385-
* @min_rsp_body_length: minimum expected length of response body
386-
* @flags: tpm transmit flags - bitmap
387-
* @desc: command description used in the error message
379+
* @chip: a TPM chip to use
380+
* @space: a TPM space
381+
* @buf: a TPM command buffer
382+
* @min_rsp_body_length: minimum expected length of response body
383+
* @flags: TPM transmit flags
384+
* @desc: command description used in the error message
388385
*
389386
* Return:
390-
* 0 when the operation is successful.
391-
* A negative number for system errors (errno).
392-
* A positive number for a TPM error.
387+
* * 0 - OK
388+
* * -errno - A system error
389+
* * TPM_RC - A TPM error
393390
*/
394391
ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
395-
void *buf, size_t bufsiz,
396-
size_t min_rsp_body_length, unsigned int flags,
397-
const char *desc)
392+
struct tpm_buf *buf, size_t min_rsp_body_length,
393+
unsigned int flags, const char *desc)
398394
{
399-
const struct tpm_output_header *header = buf;
395+
const struct tpm_output_header *header =
396+
(struct tpm_output_header *)buf->data;
400397
int err;
401398
ssize_t len;
402399

403-
len = tpm_transmit(chip, space, buf, bufsiz, flags);
400+
len = tpm_transmit(chip, space, buf->data, PAGE_SIZE, flags);
404401
if (len < 0)
405402
return len;
406403

@@ -537,14 +534,22 @@ EXPORT_SYMBOL_GPL(tpm_pcr_extend);
537534
*/
538535
int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
539536
{
537+
struct tpm_buf buf;
540538
int rc;
541539

542540
chip = tpm_find_get_ops(chip);
543541
if (!chip)
544542
return -ENODEV;
545543

546-
rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0,
544+
rc = tpm_buf_init(&buf, 0, 0);
545+
if (rc)
546+
goto out;
547+
548+
memcpy(buf.data, cmd, buflen);
549+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
547550
"attempting to a send a command");
551+
tpm_buf_destroy(&buf);
552+
out:
548553
tpm_put_ops(chip);
549554
return rc;
550555
}

drivers/char/tpm/tpm-sysfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
5353

5454
tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
5555

56-
rc = tpm_transmit_cmd(chip, NULL, tpm_buf.data, PAGE_SIZE,
56+
rc = tpm_transmit_cmd(chip, NULL, &tpm_buf,
5757
READ_PUBEK_RESULT_MIN_BODY_SIZE, 0,
5858
"attempting to read the PUBEK");
5959
if (rc) {

drivers/char/tpm/tpm.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,8 @@ enum tpm_transmit_flags {
503503
ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
504504
u8 *buf, size_t bufsiz, unsigned int flags);
505505
ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
506-
void *buf, size_t bufsiz,
507-
size_t min_rsp_body_length, unsigned int flags,
508-
const char *desc);
506+
struct tpm_buf *buf, size_t min_rsp_body_length,
507+
unsigned int flags, const char *desc);
509508
int tpm_get_timeouts(struct tpm_chip *);
510509
int tpm_auto_startup(struct tpm_chip *chip);
511510

drivers/char/tpm/tpm1-cmd.c

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,9 @@ static int tpm1_startup(struct tpm_chip *chip)
334334

335335
tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
336336

337-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
337+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
338338
"attempting to start the TPM");
339-
340339
tpm_buf_destroy(&buf);
341-
342340
return rc;
343341
}
344342

@@ -461,9 +459,7 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
461459
tpm_buf_append_u32(&buf, pcr_idx);
462460
tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
463461

464-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
465-
TPM_DIGEST_SIZE, 0, log_msg);
466-
462+
rc = tpm_transmit_cmd(chip, NULL, &buf, TPM_DIGEST_SIZE, 0, log_msg);
467463
tpm_buf_destroy(&buf);
468464
return rc;
469465
}
@@ -493,11 +489,9 @@ ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
493489
tpm_buf_append_u32(&buf, 4);
494490
tpm_buf_append_u32(&buf, subcap_id);
495491
}
496-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
497-
min_cap_length, 0, desc);
492+
rc = tpm_transmit_cmd(chip, NULL, &buf, min_cap_length, 0, desc);
498493
if (!rc)
499494
*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
500-
501495
tpm_buf_destroy(&buf);
502496
return rc;
503497
}
@@ -536,7 +530,7 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
536530
do {
537531
tpm_buf_append_u32(&buf, num_bytes);
538532

539-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
533+
rc = tpm_transmit_cmd(chip, NULL, &buf,
540534
sizeof(out->rng_data_len), 0,
541535
"attempting get random");
542536
if (rc)
@@ -582,8 +576,7 @@ int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
582576

583577
tpm_buf_append_u32(&buf, pcr_idx);
584578

585-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
586-
TPM_DIGEST_SIZE, 0,
579+
rc = tpm_transmit_cmd(chip, NULL, &buf, TPM_DIGEST_SIZE, 0,
587580
"attempting to read a pcr value");
588581
if (rc)
589582
goto out;
@@ -617,11 +610,8 @@ static int tpm1_continue_selftest(struct tpm_chip *chip)
617610
if (rc)
618611
return rc;
619612

620-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
621-
0, 0, "continue selftest");
622-
613+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0, "continue selftest");
623614
tpm_buf_destroy(&buf);
624-
625615
return rc;
626616
}
627617

@@ -746,9 +736,7 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
746736
return rc;
747737
/* now do the actual savestate */
748738
for (try = 0; try < TPM_RETRY; try++) {
749-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
750-
0, 0, NULL);
751-
739+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0, NULL);
752740
/*
753741
* If the TPM indicates that it is too busy to respond to
754742
* this command then retry before giving up. It can take

drivers/char/tpm/tpm2-cmd.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
197197
tpm_buf_append(&buf, (const unsigned char *)pcr_select,
198198
sizeof(pcr_select));
199199

200-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
201-
res_buf ? "attempting to read a pcr value" : NULL);
200+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0, res_buf ?
201+
"attempting to read a pcr value" : NULL);
202202
if (rc == 0 && res_buf) {
203203
out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
204204
memcpy(res_buf, out->digest, SHA1_DIGEST_SIZE);
@@ -264,7 +264,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
264264
}
265265
}
266266

267-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
267+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
268268
"attempting extend a PCR value");
269269

270270
tpm_buf_destroy(&buf);
@@ -309,7 +309,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
309309
do {
310310
tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
311311
tpm_buf_append_u16(&buf, num_bytes);
312-
err = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
312+
err = tpm_transmit_cmd(chip, NULL, &buf,
313313
offsetof(struct tpm2_get_random_out,
314314
buffer),
315315
0, "attempting get random");
@@ -362,9 +362,7 @@ void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
362362

363363
tpm_buf_append_u32(&buf, handle);
364364

365-
(void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
366-
"flushing context");
367-
365+
tpm_transmit_cmd(chip, NULL, &buf, 0, flags, "flushing context");
368366
tpm_buf_destroy(&buf);
369367
}
370368

@@ -478,8 +476,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
478476
goto out;
479477
}
480478

481-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
482-
"sealing data");
479+
rc = tpm_transmit_cmd(chip, NULL, &buf, 4, 0, "sealing data");
483480
if (rc)
484481
goto out;
485482

@@ -561,8 +558,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
561558
goto out;
562559
}
563560

564-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
565-
"loading blob");
561+
rc = tpm_transmit_cmd(chip, NULL, &buf, 4, flags, "loading blob");
566562
if (!rc)
567563
*blob_handle = be32_to_cpup(
568564
(__be32 *) &buf.data[TPM_HEADER_SIZE]);
@@ -612,8 +608,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
612608
options->blobauth /* hmac */,
613609
TPM_DIGEST_SIZE);
614610

615-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
616-
"unsealing");
611+
rc = tpm_transmit_cmd(chip, NULL, &buf, 6, flags, "unsealing");
617612
if (rc > 0)
618613
rc = -EPERM;
619614

@@ -703,7 +698,7 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
703698
tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
704699
tpm_buf_append_u32(&buf, property_id);
705700
tpm_buf_append_u32(&buf, 1);
706-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
701+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0, NULL);
707702
if (!rc) {
708703
out = (struct tpm2_get_cap_out *)
709704
&buf.data[TPM_HEADER_SIZE];
@@ -733,8 +728,7 @@ void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
733728
if (rc)
734729
return;
735730
tpm_buf_append_u16(&buf, shutdown_type);
736-
tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
737-
"stopping the TPM");
731+
tpm_transmit_cmd(chip, NULL, &buf, 0, 0, "stopping the TPM");
738732
tpm_buf_destroy(&buf);
739733
}
740734

@@ -763,7 +757,7 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
763757
return rc;
764758

765759
tpm_buf_append_u8(&buf, full);
766-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
760+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
767761
"attempting the self test");
768762
tpm_buf_destroy(&buf);
769763

@@ -800,7 +794,7 @@ int tpm2_probe(struct tpm_chip *chip)
800794
tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
801795
tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
802796
tpm_buf_append_u32(&buf, 1);
803-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
797+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0, NULL);
804798
/* We ignore TPM return codes on purpose. */
805799
if (rc >= 0) {
806800
out = (struct tpm_output_header *)buf.data;
@@ -839,7 +833,7 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
839833
tpm_buf_append_u32(&buf, 0);
840834
tpm_buf_append_u32(&buf, 1);
841835

842-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
836+
rc = tpm_transmit_cmd(chip, NULL, &buf, 9, 0,
843837
"get tpm pcr allocation");
844838
if (rc)
845839
goto out;
@@ -911,8 +905,7 @@ static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
911905
tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
912906
tpm_buf_append_u32(&buf, nr_commands);
913907

914-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
915-
9 + 4 * nr_commands, 0, NULL);
908+
rc = tpm_transmit_cmd(chip, NULL, &buf, 9 + 4 * nr_commands, 0, NULL);
916909
if (rc) {
917910
tpm_buf_destroy(&buf);
918911
goto out;
@@ -969,7 +962,7 @@ static int tpm2_startup(struct tpm_chip *chip)
969962
return rc;
970963

971964
tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
972-
rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
965+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
973966
"attempting to start the TPM");
974967
tpm_buf_destroy(&buf);
975968

drivers/char/tpm/tpm2-space.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
8383
body_size = sizeof(*ctx) + be16_to_cpu(ctx->blob_size);
8484
tpm_buf_append(&tbuf, &buf[*offset], body_size);
8585

86-
rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 4,
86+
rc = tpm_transmit_cmd(chip, NULL, &tbuf, 4,
8787
TPM_TRANSMIT_NESTED, NULL);
8888
if (rc < 0) {
8989
dev_warn(&chip->dev, "%s: failed with a system error %d\n",
@@ -132,7 +132,7 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
132132

133133
tpm_buf_append_u32(&tbuf, handle);
134134

135-
rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 0,
135+
rc = tpm_transmit_cmd(chip, NULL, &tbuf, 0,
136136
TPM_TRANSMIT_NESTED, NULL);
137137
if (rc < 0) {
138138
dev_warn(&chip->dev, "%s: failed with a system error %d\n",

drivers/char/tpm/tpm_vtpm_proxy.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
416416

417417
proxy_dev->state |= STATE_DRIVER_COMMAND;
418418

419-
rc = tpm_transmit_cmd(chip, NULL, buf.data, tpm_buf_length(&buf), 0,
420-
TPM_TRANSMIT_NESTED,
419+
rc = tpm_transmit_cmd(chip, NULL, &buf, 0, TPM_TRANSMIT_NESTED,
421420
"attempting to set locality");
422421

423422
proxy_dev->state &= ~STATE_DRIVER_COMMAND;

0 commit comments

Comments
 (0)