Skip to content

Commit

Permalink
Merge tag 'tpm-master-27052024' of https://source.denx.de/u-boot/cust…
Browse files Browse the repository at this point in the history
  • Loading branch information
trini committed May 27, 2024
2 parents 6c012d6 + 89aa846 commit 46ff00b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 60 deletions.
49 changes: 35 additions & 14 deletions cmd/tpm-v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,19 @@ static int do_tpm2_pcr_extend(struct cmd_tbl *cmdtp, int flag, int argc,
struct tpm_chip_priv *priv;
u32 index = simple_strtoul(argv[1], NULL, 0);
void *digest = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
int algo = TPM2_ALG_SHA256;
int algo_len;
int ret;
u32 rc;

if (argc != 3)
if (argc < 3 || argc > 4)
return CMD_RET_USAGE;
if (argc == 4) {
algo = tpm2_name_to_algorithm(argv[3]);
if (algo < 0)
return CMD_RET_FAILURE;
}
algo_len = tpm2_algorithm_to_len(algo);

ret = get_tpm(&dev);
if (ret)
Expand All @@ -116,8 +124,12 @@ static int do_tpm2_pcr_extend(struct cmd_tbl *cmdtp, int flag, int argc,
if (index >= priv->pcr_count)
return -EINVAL;

rc = tpm2_pcr_extend(dev, index, TPM2_ALG_SHA256, digest,
TPM2_DIGEST_LEN);
rc = tpm2_pcr_extend(dev, index, algo, digest, algo_len);
if (!rc) {
printf("PCR #%u extended with %d byte %s digest\n", index,
algo_len, tpm2_algorithm_name(algo));
print_byte_string(digest, algo_len);
}

unmap_sysmem(digest);

Expand All @@ -127,15 +139,23 @@ static int do_tpm2_pcr_extend(struct cmd_tbl *cmdtp, int flag, int argc,
static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
enum tpm2_algorithms algo = TPM2_ALG_SHA256;
struct udevice *dev;
struct tpm_chip_priv *priv;
u32 index, rc;
int algo_len;
unsigned int updates;
void *data;
int ret;

if (argc != 3)
if (argc < 3 || argc > 4)
return CMD_RET_USAGE;
if (argc == 4) {
algo = tpm2_name_to_algorithm(argv[3]);
if (algo < 0)
return CMD_RET_FAILURE;
}
algo_len = tpm2_algorithm_to_len(algo);

ret = get_tpm(&dev);
if (ret)
Expand All @@ -151,11 +171,12 @@ static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,

data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);

rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, TPM2_ALG_SHA256,
data, TPM2_DIGEST_LEN, &updates);
rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, algo,
data, algo_len, &updates);
if (!rc) {
printf("PCR #%u content (%u known updates):\n", index, updates);
print_byte_string(data, TPM2_DIGEST_LEN);
printf("PCR #%u %s %d byte content (%u known updates):\n", index,
tpm2_algorithm_name(algo), algo_len, updates);
print_byte_string(data, algo_len);
}

unmap_sysmem(data);
Expand Down Expand Up @@ -415,14 +436,14 @@ U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
" <hierarchy> is one of:\n"
" * TPM2_RH_LOCKOUT\n"
" * TPM2_RH_PLATFORM\n"
"pcr_extend <pcr> <digest_addr>\n"
" Extend PCR #<pcr> with digest at <digest_addr>.\n"
"pcr_extend <pcr> <digest_addr> [<digest_algo>]\n"
" Extend PCR #<pcr> with digest at <digest_addr> with digest_algo.\n"
" <pcr>: index of the PCR\n"
" <digest_addr>: address of a 32-byte SHA256 digest\n"
"pcr_read <pcr> <digest_addr>\n"
" Read PCR #<pcr> to memory address <digest_addr>.\n"
" <digest_addr>: address of digest of digest_algo type (defaults to SHA256)\n"
"pcr_read <pcr> <digest_addr> [<digest_algo>]\n"
" Read PCR #<pcr> to memory address <digest_addr> with <digest_algo>.\n"
" <pcr>: index of the PCR\n"
" <digest_addr>: address to store the a 32-byte SHA256 digest\n"
" <digest_addr>: address of digest of digest_algo type (defaults to SHA256)\n"
"get_capability <capability> <property> <addr> <count>\n"
" Read and display <count> entries indexed by <capability>/<property>.\n"
" Values are 4 bytes long and are written at <addr>.\n"
Expand Down
21 changes: 12 additions & 9 deletions drivers/tpm/tpm2_tis_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,22 @@ static int tpm_tis_spi_probe(struct udevice *dev)
/* legacy reset */
ret = gpio_request_by_name(dev, "gpio-reset", 0,
&reset_gpio, GPIOD_IS_OUT);
if (ret) {
if (!ret) {
log(LOGC_NONE, LOGL_NOTICE,
"%s: missing reset GPIO\n", __func__);
goto init;
"%s: gpio-reset is deprecated\n", __func__);
}
log(LOGC_NONE, LOGL_NOTICE,
"%s: gpio-reset is deprecated\n", __func__);
}
dm_gpio_set_value(&reset_gpio, 1);
mdelay(1);
dm_gpio_set_value(&reset_gpio, 0);

if (!ret) {
log(LOGC_NONE, LOGL_WARNING,
"%s: TPM gpio reset should not be used on secure production devices\n",
dev->name);
dm_gpio_set_value(&reset_gpio, 1);
mdelay(1);
dm_gpio_set_value(&reset_gpio, 0);
}
}
init:

/* Ensure a minimum amount of time elapsed since reset of the TPM */
mdelay(drv_data->time_before_first_cmd_ms);

Expand Down
77 changes: 74 additions & 3 deletions include/tpm-v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,54 @@ enum tpm2_algorithms {
TPM2_ALG_SM3_256 = 0x12,
};

extern const enum tpm2_algorithms tpm2_supported_algorithms[4];
/**
* struct digest_info - details of supported digests
*
* @hash_name: hash name
* @hash_alg: hash algorithm id
* @hash_mask: hash registry mask
* @hash_len: hash digest length
*/
struct digest_info {
const char *hash_name;
u16 hash_alg;
u32 hash_mask;
u16 hash_len;
};

/* Algorithm Registry */
#define TCG2_BOOT_HASH_ALG_SHA1 0x00000001
#define TCG2_BOOT_HASH_ALG_SHA256 0x00000002
#define TCG2_BOOT_HASH_ALG_SHA384 0x00000004
#define TCG2_BOOT_HASH_ALG_SHA512 0x00000008
#define TCG2_BOOT_HASH_ALG_SM3_256 0x00000010

static const struct digest_info hash_algo_list[] = {
{
"sha1",
TPM2_ALG_SHA1,
TCG2_BOOT_HASH_ALG_SHA1,
TPM2_SHA1_DIGEST_SIZE,
},
{
"sha256",
TPM2_ALG_SHA256,
TCG2_BOOT_HASH_ALG_SHA256,
TPM2_SHA256_DIGEST_SIZE,
},
{
"sha384",
TPM2_ALG_SHA384,
TCG2_BOOT_HASH_ALG_SHA384,
TPM2_SHA384_DIGEST_SIZE,
},
{
"sha512",
TPM2_ALG_SHA512,
TCG2_BOOT_HASH_ALG_SHA512,
TPM2_SHA512_DIGEST_SIZE,
},
};

static inline u16 tpm2_algorithm_to_len(enum tpm2_algorithms a)
{
Expand All @@ -404,8 +451,6 @@ static inline u16 tpm2_algorithm_to_len(enum tpm2_algorithms a)
}
}

#define tpm2_algorithm_to_mask(a) (1 << (a))

/* NV index attributes */
enum tpm_index_attrs {
TPMA_NV_PPWRITE = 1UL << 0,
Expand Down Expand Up @@ -965,4 +1010,30 @@ u32 tpm2_enable_nvcommits(struct udevice *dev, uint vendor_cmd,
*/
u32 tpm2_auto_start(struct udevice *dev);

/**
* tpm2_name_to_algorithm() - Return an algorithm id given a supported
* algorithm name
*
* @name: algorithm name
* Return: enum tpm2_algorithms or -EINVAL
*/
enum tpm2_algorithms tpm2_name_to_algorithm(const char *name);

/**
* tpm2_algorithm_name() - Return an algorithm name string for a
* supported algorithm id
*
* @algorithm_id: algorithm defined in enum tpm2_algorithms
* Return: algorithm name string or ""
*/
const char *tpm2_algorithm_name(enum tpm2_algorithms);

/**
* tpm2_algorithm_to_mask() - Get a TCG hash mask for algorithm
*
* @hash_alg: TCG defined algorithm
* Return: TCG hashing algorithm bitmaps (or 0 if algo not supported)
*/
u32 tpm2_algorithm_to_mask(enum tpm2_algorithms);

#endif /* __TPM_V2_H */
6 changes: 3 additions & 3 deletions lib/efi_loader/efi_tcg2.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
}

digest_list->count = 0;
for (i = 0; i < ARRAY_SIZE(tpm2_supported_algorithms); i++) {
u16 hash_alg = tpm2_supported_algorithms[i];
for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
u16 hash_alg = hash_algo_list[i].hash_alg;

if (!(active & tpm2_algorithm_to_mask(hash_alg)))
if (!(active & hash_algo_list[i].hash_mask))
continue;
switch (hash_alg) {
case TPM2_ALG_SHA1:
Expand Down
Loading

0 comments on commit 46ff00b

Please sign in to comment.