diff --git a/drivers/firmware/efi/tpm.c b/drivers/firmware/efi/tpm.c index fe48150f06d1..2c912ea08166 100644 --- a/drivers/firmware/efi/tpm.c +++ b/drivers/firmware/efi/tpm.c @@ -28,6 +28,7 @@ static int tpm2_calc_event_log_size(void *data, int count, void *size_info) if (event_size == 0) return -1; size += event_size; + count--; } return size; @@ -41,6 +42,7 @@ int __init efi_tpm_eventlog_init(void) struct linux_efi_tpm_eventlog *log_tbl; struct efi_tcg2_final_events_table *final_tbl; unsigned int tbl_size; + int ret = 0; if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) { /* @@ -60,10 +62,9 @@ int __init efi_tpm_eventlog_init(void) tbl_size = sizeof(*log_tbl) + log_tbl->size; memblock_reserve(efi.tpm_log, tbl_size); - early_memunmap(log_tbl, sizeof(*log_tbl)); if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) - return 0; + goto out; final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl)); @@ -71,17 +72,22 @@ int __init efi_tpm_eventlog_init(void) pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n", efi.tpm_final_log); efi.tpm_final_log = EFI_INVALID_TABLE_ADDR; - return -ENOMEM; + ret = -ENOMEM; + goto out; } - tbl_size = tpm2_calc_event_log_size(final_tbl->events, + tbl_size = tpm2_calc_event_log_size(efi.tpm_final_log + + sizeof(final_tbl->version) + + sizeof(final_tbl->nr_events), final_tbl->nr_events, - (void *)efi.tpm_log); + log_tbl->log); memblock_reserve((unsigned long)final_tbl, tbl_size + sizeof(*final_tbl)); early_memunmap(final_tbl, sizeof(*final_tbl)); efi_tpm_final_log_size = tbl_size; - return 0; +out: + early_memunmap(log_tbl, sizeof(*log_tbl)); + return ret; } diff --git a/include/linux/tpm_eventlog.h b/include/linux/tpm_eventlog.h index 0ca27bc053af..63238c84dc0b 100644 --- a/include/linux/tpm_eventlog.h +++ b/include/linux/tpm_eventlog.h @@ -185,8 +185,12 @@ static inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event, size = 0; goto out; } + } else { + mapping = marker_start; } + event = (struct tcg_pcr_event2_head *)mapping; + efispecid = (struct tcg_efi_specid_event_head *)event_header->event; /* Check if event is malformed. */ @@ -201,34 +205,24 @@ static inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event, /* Map the digest's algorithm identifier */ if (do_mapping) { TPM_MEMUNMAP(mapping, mapping_size); - mapping_size = marker - marker_start + halg_size; - mapping = TPM_MEMREMAP((unsigned long)marker_start, - mapping_size); + mapping_size = halg_size; + mapping = TPM_MEMREMAP((unsigned long)marker, + mapping_size); if (!mapping) { size = 0; goto out; } + } else { + mapping = marker; } - memcpy(&halg, marker, halg_size); + memcpy(&halg, mapping, halg_size); marker = marker + halg_size; for (j = 0; j < efispecid->num_algs; j++) { if (halg == efispecid->digest_sizes[j].alg_id) { marker += efispecid->digest_sizes[j].digest_size; - - /* Map the digest content itself */ - if (do_mapping) { - TPM_MEMUNMAP(mapping, mapping_size); - mapping_size = marker - marker_start; - mapping = TPM_MEMREMAP((unsigned long)marker_start, - mapping_size); - if (!mapping) { - size = 0; - goto out; - } - } break; } } @@ -239,23 +233,25 @@ static inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event, } } - event_field = (struct tcg_event_field *)marker; - /* * Map the event size - we don't read from the event itself, so * we don't need to map it */ if (do_mapping) { - TPM_MEMUNMAP(marker_start, mapping_size); + TPM_MEMUNMAP(mapping, mapping_size); mapping_size += sizeof(event_field->event_size); - mapping = TPM_MEMREMAP((unsigned long)marker_start, + mapping = TPM_MEMREMAP((unsigned long)marker, mapping_size); if (!mapping) { size = 0; goto out; } + } else { + mapping = marker; } + event_field = (struct tcg_event_field *)mapping; + marker = marker + sizeof(event_field->event_size) + event_field->event_size; size = marker - marker_start;