GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / char / tpm / eventlog / efi.c
1 /*
2  * Copyright (C) 2017 Google
3  *
4  * Authors:
5  *      Thiebaud Weksteen <tweek@google.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  *
12  */
13
14 #include <linux/efi.h>
15 #include <linux/tpm_eventlog.h>
16
17 #include "../tpm.h"
18 #include "common.h"
19
20 /* read binary bios log from EFI configuration table */
21 int tpm_read_log_efi(struct tpm_chip *chip)
22 {
23
24         struct linux_efi_tpm_eventlog *log_tbl;
25         struct tpm_bios_log *log;
26         u32 log_size;
27         u8 tpm_log_version;
28
29         if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
30                 return -ENODEV;
31
32         if (efi.tpm_log == EFI_INVALID_TABLE_ADDR)
33                 return -ENODEV;
34
35         log = &chip->log;
36
37         log_tbl = memremap(efi.tpm_log, sizeof(*log_tbl), MEMREMAP_WB);
38         if (!log_tbl) {
39                 pr_err("Could not map UEFI TPM log table !\n");
40                 return -ENOMEM;
41         }
42
43         log_size = log_tbl->size;
44         memunmap(log_tbl);
45
46         if (!log_size) {
47                 pr_warn("UEFI TPM log area empty\n");
48                 return -EIO;
49         }
50
51         log_tbl = memremap(efi.tpm_log, sizeof(*log_tbl) + log_size,
52                            MEMREMAP_WB);
53         if (!log_tbl) {
54                 pr_err("Could not map UEFI TPM log table payload!\n");
55                 return -ENOMEM;
56         }
57
58         /* malloc EventLog space */
59         log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
60         if (!log->bios_event_log)
61                 goto err_memunmap;
62         log->bios_event_log_end = log->bios_event_log + log_size;
63
64         tpm_log_version = log_tbl->version;
65         memunmap(log_tbl);
66         return tpm_log_version;
67
68 err_memunmap:
69         memunmap(log_tbl);
70         return -ENOMEM;
71 }