[v2,2/2] tpm: Add reserved memory event log

Message ID 20230113161017.1079299-3-eajames@linux.ibm.com
State New
Headers
Series tpm: Add reserved memory event log |

Commit Message

Eddie James Jan. 13, 2023, 4:10 p.m. UTC
  Some platforms may desire to pass the event log up to linux in the
form of a reserved memory region. Add support for this in the TPM
core to find the reserved memory region and map it.

Signed-off-by: Eddie James <eajames@linux.ibm.com>
---
 drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
  

Comments

Stefan Berger Jan. 18, 2023, 1:27 p.m. UTC | #1
On 1/13/23 11:10, Eddie James wrote:
> Some platforms may desire to pass the event log up to linux in the

Which platforms are these that work like this?

    Stefan

> form of a reserved memory region. Add support for this in the TPM
> core to find the reserved memory region and map it.
> 
> Signed-off-by: Eddie James <eajames@linux.ibm.com>
> ---
>   drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>   1 file changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
> index 741ab2204b11..c815cadf00a4 100644
> --- a/drivers/char/tpm/eventlog/of.c
> +++ b/drivers/char/tpm/eventlog/of.c
> @@ -12,12 +12,48 @@
>   
>   #include <linux/device.h>
>   #include <linux/slab.h>
> +#include <linux/io.h>
> +#include <linux/ioport.h>
>   #include <linux/of.h>
> +#include <linux/of_address.h>
> +#include <linux/of_reserved_mem.h>
>   #include <linux/tpm_eventlog.h>
>   
>   #include "../tpm.h"
>   #include "common.h"
>   
> +static int tpm_read_log_memory_region(struct tpm_chip *chip)
> +{
> +	struct device_node *node;
> +	struct resource res;
> +	int rc;
> +
> +	node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
> +	if (!node) {
> +		dev_info(&chip->dev, "no phandle\n");
> +		return -ENODEV;
> +	}
> +
> +	rc = of_address_to_resource(node, 0, &res);
> +	of_node_put(node);
> +	if (rc) {
> +		dev_info(&chip->dev, "no mem\n");
> +		return rc;
> +	}
> +
> +	chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
> +						 MEMREMAP_WB);
> +	if (!chip->log.bios_event_log) {
> +		dev_info(&chip->dev, "err memremap\n");
> +		return -ENOMEM;
> +	}
> +
> +	chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
> +
> +	return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
> +		EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
> +}
> +
>   int tpm_read_log_of(struct tpm_chip *chip)
>   {
>   	struct device_node *np;
> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>   	sizep = of_get_property(np, "linux,sml-size", NULL);
>   	basep = of_get_property(np, "linux,sml-base", NULL);
>   	if (sizep == NULL && basep == NULL)
> -		return -ENODEV;
> +		return tpm_read_log_memory_region(chip);
>   	if (sizep == NULL || basep == NULL)
>   		return -EIO;
>
  
Eddie James Jan. 18, 2023, 2:49 p.m. UTC | #2
On 1/18/23 07:27, Stefan Berger wrote:
>
>
> On 1/13/23 11:10, Eddie James wrote:
>> Some platforms may desire to pass the event log up to linux in the
>
> Which platforms are these that work like this?


Platforms booting from U-Boot without EFI. So at the moment, IBM's 
OpenBMC systems hope to use this.

Thanks,

Eddie


>
>    Stefan
>
>> form of a reserved memory region. Add support for this in the TPM
>> core to find the reserved memory region and map it.
>>
>> Signed-off-by: Eddie James <eajames@linux.ibm.com>
>> ---
>>   drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>>   1 file changed, 37 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/char/tpm/eventlog/of.c 
>> b/drivers/char/tpm/eventlog/of.c
>> index 741ab2204b11..c815cadf00a4 100644
>> --- a/drivers/char/tpm/eventlog/of.c
>> +++ b/drivers/char/tpm/eventlog/of.c
>> @@ -12,12 +12,48 @@
>>     #include <linux/device.h>
>>   #include <linux/slab.h>
>> +#include <linux/io.h>
>> +#include <linux/ioport.h>
>>   #include <linux/of.h>
>> +#include <linux/of_address.h>
>> +#include <linux/of_reserved_mem.h>
>>   #include <linux/tpm_eventlog.h>
>>     #include "../tpm.h"
>>   #include "common.h"
>>   +static int tpm_read_log_memory_region(struct tpm_chip *chip)
>> +{
>> +    struct device_node *node;
>> +    struct resource res;
>> +    int rc;
>> +
>> +    node = of_parse_phandle(chip->dev.parent->of_node, 
>> "memory-region", 0);
>> +    if (!node) {
>> +        dev_info(&chip->dev, "no phandle\n");
>> +        return -ENODEV;
>> +    }
>> +
>> +    rc = of_address_to_resource(node, 0, &res);
>> +    of_node_put(node);
>> +    if (rc) {
>> +        dev_info(&chip->dev, "no mem\n");
>> +        return rc;
>> +    }
>> +
>> +    chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, 
>> resource_size(&res),
>> +                         MEMREMAP_WB);
>> +    if (!chip->log.bios_event_log) {
>> +        dev_info(&chip->dev, "err memremap\n");
>> +        return -ENOMEM;
>> +    }
>> +
>> +    chip->log.bios_event_log_end = chip->log.bios_event_log + 
>> resource_size(&res);
>> +
>> +    return chip->flags & TPM_CHIP_FLAG_TPM2 ? 
>> EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
>> +        EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
>> +}
>> +
>>   int tpm_read_log_of(struct tpm_chip *chip)
>>   {
>>       struct device_node *np;
>> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>>       sizep = of_get_property(np, "linux,sml-size", NULL);
>>       basep = of_get_property(np, "linux,sml-base", NULL);
>>       if (sizep == NULL && basep == NULL)
>> -        return -ENODEV;
>> +        return tpm_read_log_memory_region(chip);
>>       if (sizep == NULL || basep == NULL)
>>           return -EIO;
  
Stefan Berger Jan. 18, 2023, 3:01 p.m. UTC | #3
On 1/18/23 09:49, Eddie James wrote:
> 
> On 1/18/23 07:27, Stefan Berger wrote:
>>
>>
>> On 1/13/23 11:10, Eddie James wrote:
>>> Some platforms may desire to pass the event log up to linux in the
>>
>> Which platforms are these that work like this?
> 
> 
> Platforms booting from U-Boot without EFI. So at the moment, IBM's OpenBMC systems hope to use this.

It may be worth to mention this in the patch description in case someone wonders which
systems would take that path.

    Stefan
> 
> Thanks,
> 
> Eddie
> 
> 
>>
>>    Stefan
>>
>>> form of a reserved memory region. Add support for this in the TPM
>>> core to find the reserved memory region and map it.
>>>
>>> Signed-off-by: Eddie James <eajames@linux.ibm.com>
>>> ---
>>>   drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>>>   1 file changed, 37 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
>>> index 741ab2204b11..c815cadf00a4 100644
>>> --- a/drivers/char/tpm/eventlog/of.c
>>> +++ b/drivers/char/tpm/eventlog/of.c
>>> @@ -12,12 +12,48 @@
>>>     #include <linux/device.h>
>>>   #include <linux/slab.h>
>>> +#include <linux/io.h>
>>> +#include <linux/ioport.h>
>>>   #include <linux/of.h>
>>> +#include <linux/of_address.h>
>>> +#include <linux/of_reserved_mem.h>
>>>   #include <linux/tpm_eventlog.h>
>>>     #include "../tpm.h"
>>>   #include "common.h"
>>>   +static int tpm_read_log_memory_region(struct tpm_chip *chip)
>>> +{
>>> +    struct device_node *node;
>>> +    struct resource res;
>>> +    int rc;
>>> +
>>> +    node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
>>> +    if (!node) {
>>> +        dev_info(&chip->dev, "no phandle\n");
>>> +        return -ENODEV;
>>> +    }
>>> +
>>> +    rc = of_address_to_resource(node, 0, &res);
>>> +    of_node_put(node);
>>> +    if (rc) {
>>> +        dev_info(&chip->dev, "no mem\n");
>>> +        return rc;
>>> +    }
>>> +
>>> +    chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
>>> +                         MEMREMAP_WB);
>>> +    if (!chip->log.bios_event_log) {
>>> +        dev_info(&chip->dev, "err memremap\n");
>>> +        return -ENOMEM;
>>> +    }
>>> +
>>> +    chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
>>> +
>>> +    return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
>>> +        EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
>>> +}
>>> +
>>>   int tpm_read_log_of(struct tpm_chip *chip)
>>>   {
>>>       struct device_node *np;
>>> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>>>       sizep = of_get_property(np, "linux,sml-size", NULL);
>>>       basep = of_get_property(np, "linux,sml-base", NULL);
>>>       if (sizep == NULL && basep == NULL)
>>> -        return -ENODEV;
>>> +        return tpm_read_log_memory_region(chip);
>>>       if (sizep == NULL || basep == NULL)
>>>           return -EIO;
  
Jarkko Sakkinen Jan. 21, 2023, 12:13 a.m. UTC | #4
On Fri, Jan 13, 2023 at 10:10:17AM -0600, Eddie James wrote:
> Some platforms may desire to pass the event log up to linux in the
> form of a reserved memory region. Add support for this in the TPM
> core to find the reserved memory region and map it.
> 
> Signed-off-by: Eddie James <eajames@linux.ibm.com>
> ---
>  drivers/char/tpm/eventlog/of.c | 38 +++++++++++++++++++++++++++++++++-
>  1 file changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
> index 741ab2204b11..c815cadf00a4 100644
> --- a/drivers/char/tpm/eventlog/of.c
> +++ b/drivers/char/tpm/eventlog/of.c
> @@ -12,12 +12,48 @@
>  
>  #include <linux/device.h>
>  #include <linux/slab.h>
> +#include <linux/io.h>
> +#include <linux/ioport.h>
>  #include <linux/of.h>
> +#include <linux/of_address.h>
> +#include <linux/of_reserved_mem.h>
>  #include <linux/tpm_eventlog.h>
>  
>  #include "../tpm.h"
>  #include "common.h"
>  
> +static int tpm_read_log_memory_region(struct tpm_chip *chip)
> +{
> +	struct device_node *node;
> +	struct resource res;
> +	int rc;
> +
> +	node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
> +	if (!node) {
> +		dev_info(&chip->dev, "no phandle\n");
> +		return -ENODEV;
> +	}
> +
> +	rc = of_address_to_resource(node, 0, &res);
> +	of_node_put(node);
> +	if (rc) {
> +		dev_info(&chip->dev, "no mem\n");
> +		return rc;
> +	}
> +
> +	chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
> +						 MEMREMAP_WB);
> +	if (!chip->log.bios_event_log) {
> +		dev_info(&chip->dev, "err memremap\n");
> +		return -ENOMEM;
> +	}
> +
> +	chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
> +
> +	return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
> +		EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
> +}
> +
>  int tpm_read_log_of(struct tpm_chip *chip)
>  {
>  	struct device_node *np;
> @@ -39,7 +75,7 @@ int tpm_read_log_of(struct tpm_chip *chip)
>  	sizep = of_get_property(np, "linux,sml-size", NULL);
>  	basep = of_get_property(np, "linux,sml-base", NULL);
>  	if (sizep == NULL && basep == NULL)
> -		return -ENODEV;
> +		return tpm_read_log_memory_region(chip);
>  	if (sizep == NULL || basep == NULL)
>  		return -EIO;
>  
> -- 
> 2.31.1
> 

I would CC this to linux-devicetree.

BR, Jarkko
  
Jarkko Sakkinen Jan. 21, 2023, 3:58 a.m. UTC | #5
On Wed, Jan 18, 2023 at 10:01:16AM -0500, Stefan Berger wrote:
> 
> 
> On 1/18/23 09:49, Eddie James wrote:
> > 
> > On 1/18/23 07:27, Stefan Berger wrote:
> > > 
> > > 
> > > On 1/13/23 11:10, Eddie James wrote:
> > > > Some platforms may desire to pass the event log up to linux in the
> > > 
> > > Which platforms are these that work like this?
> > 
> > 
> > Platforms booting from U-Boot without EFI. So at the moment, IBM's OpenBMC systems hope to use this.
> 
> It may be worth to mention this in the patch description in case someone wonders which
> systems would take that path.

+1

BR, Jarkko
  

Patch

diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
index 741ab2204b11..c815cadf00a4 100644
--- a/drivers/char/tpm/eventlog/of.c
+++ b/drivers/char/tpm/eventlog/of.c
@@ -12,12 +12,48 @@ 
 
 #include <linux/device.h>
 #include <linux/slab.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_reserved_mem.h>
 #include <linux/tpm_eventlog.h>
 
 #include "../tpm.h"
 #include "common.h"
 
+static int tpm_read_log_memory_region(struct tpm_chip *chip)
+{
+	struct device_node *node;
+	struct resource res;
+	int rc;
+
+	node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
+	if (!node) {
+		dev_info(&chip->dev, "no phandle\n");
+		return -ENODEV;
+	}
+
+	rc = of_address_to_resource(node, 0, &res);
+	of_node_put(node);
+	if (rc) {
+		dev_info(&chip->dev, "no mem\n");
+		return rc;
+	}
+
+	chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
+						 MEMREMAP_WB);
+	if (!chip->log.bios_event_log) {
+		dev_info(&chip->dev, "err memremap\n");
+		return -ENOMEM;
+	}
+
+	chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
+
+	return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
+		EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
+}
+
 int tpm_read_log_of(struct tpm_chip *chip)
 {
 	struct device_node *np;
@@ -39,7 +75,7 @@  int tpm_read_log_of(struct tpm_chip *chip)
 	sizep = of_get_property(np, "linux,sml-size", NULL);
 	basep = of_get_property(np, "linux,sml-base", NULL);
 	if (sizep == NULL && basep == NULL)
-		return -ENODEV;
+		return tpm_read_log_memory_region(chip);
 	if (sizep == NULL || basep == NULL)
 		return -EIO;