• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2012 IBM Corporation
4  *
5  * Author: Ashley Lai <ashleydlai@gmail.com>
6  *         Nayna Jain <nayna@linux.vnet.ibm.com>
7  *
8  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9  *
10  * Read the event log created by the firmware on PPC64
11  */
12 
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/of.h>
16 #include <linux/tpm_eventlog.h>
17 
18 #include "../tpm.h"
19 #include "common.h"
20 
tpm_read_log_of(struct tpm_chip * chip)21 int tpm_read_log_of(struct tpm_chip *chip)
22 {
23 	struct device_node *np;
24 	const u32 *sizep;
25 	const u64 *basep;
26 	struct tpm_bios_log *log;
27 	u32 size;
28 	u64 base;
29 
30 	log = &chip->log;
31 	if (chip->dev.parent && chip->dev.parent->of_node)
32 		np = chip->dev.parent->of_node;
33 	else
34 		return -ENODEV;
35 
36 	if (of_property_read_bool(np, "powered-while-suspended"))
37 		chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
38 
39 	sizep = of_get_property(np, "linux,sml-size", NULL);
40 	basep = of_get_property(np, "linux,sml-base", NULL);
41 	if (sizep == NULL && basep == NULL)
42 		return -ENODEV;
43 	if (sizep == NULL || basep == NULL)
44 		return -EIO;
45 
46 	/*
47 	 * For both vtpm/tpm, firmware has log addr and log size in big
48 	 * endian format. But in case of vtpm, there is a method called
49 	 * sml-handover which is run during kernel init even before
50 	 * device tree is setup. This sml-handover function takes care
51 	 * of endianness and writes to sml-base and sml-size in little
52 	 * endian format. For this reason, vtpm doesn't need conversion
53 	 * but physical tpm needs the conversion.
54 	 */
55 	if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
56 	    of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
57 		size = be32_to_cpup((__force __be32 *)sizep);
58 		base = be64_to_cpup((__force __be64 *)basep);
59 	} else {
60 		size = *sizep;
61 		base = *basep;
62 	}
63 
64 	if (size == 0) {
65 		dev_warn(&chip->dev, "%s: Event log area empty\n", __func__);
66 		return -EIO;
67 	}
68 
69 	log->bios_event_log = devm_kmemdup(&chip->dev, __va(base), size, GFP_KERNEL);
70 	if (!log->bios_event_log)
71 		return -ENOMEM;
72 
73 	log->bios_event_log_end = log->bios_event_log + size;
74 
75 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
76 		return EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
77 	return EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
78 }
79