[PATCH v1] nvmem: bsec.c: Use SMC_READ_OTP in stm32_bsec_read_mac()

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Nov 26 08:09:22 PST 2021


Hello Oleksij, David,

On 26.11.21 14:31, Oleksij Rempel wrote:
> From: David Jander <david at protonic.nl>
> 
> TF-A (version >= v2.4?) apparently does not copy all of OTP to shadow
> registers so reading the MAC address area will read all-zeroes.
> Make sure to read the _real_ OTP register in this case.

Thanks for the fix. Linux reads the lowest 32 words of OTP directly from shadow
registers and calls into TF-A for reading shadow memory beyond that, which should
include MAC address (offset 0x39, didn't test though).

It never does READ_OTP, so it seems the TF-A change would affect Linux as well?
It's for sure something we'll need to address in TF-A eventually, but as a bug
fix, this is ok:

Reviewed-by: Ahmad Fatoum <a.fatoum at pengutronix.de>


> Signed-off-by: David Jander <david at protonic.nl>
> Signed-off-by: Oleksij Rempel <o.rempel at pengutronix.de>
> ---
>  drivers/nvmem/bsec.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/nvmem/bsec.c b/drivers/nvmem/bsec.c
> index d9b38c8414..a31eff7358 100644
> --- a/drivers/nvmem/bsec.c
> +++ b/drivers/nvmem/bsec.c
> @@ -82,20 +82,27 @@ static void stm32_bsec_set_unique_machine_id(struct regmap *map)
>  	machine_id_set_hashable(unique_id, sizeof(unique_id));
>  }
>  
> -static int stm32_bsec_read_mac(struct regmap *map, int offset, u8 *mac)
> +static int stm32_bsec_read_mac(struct bsec_priv *priv, int offset, u8 *mac)
>  {
> -	u8 res[8];
> +	u32 val[2];
>  	int ret;
>  
> -	ret = regmap_bulk_read(map, offset * 4, res, 8);
> +	/* Some TF-A does not copy all of OTP into shadow registers, so make
> +	 * sure we read the _real_ OTP bits here.
> +	 */
> +	ret = bsec_smc(priv, BSEC_SMC_READ_OTP, offset * 4, 0, &val[0]);
> +	if (ret)
> +		return ret;
> +	ret = bsec_smc(priv, BSEC_SMC_READ_OTP, offset * 4 + 4, 0, &val[1]);
>  	if (ret)
>  		return ret;
>  
> -	memcpy(mac, res, ETH_ALEN);
> +	memcpy(mac, val, ETH_ALEN);
>  	return 0;
>  }
>  
> -static void stm32_bsec_init_dt(struct device_d *dev, struct regmap *map)
> +static void stm32_bsec_init_dt(struct bsec_priv *priv, struct device_d *dev,
> +			       struct regmap *map)
>  {
>  	struct device_node *node = dev->device_node;
>  	struct device_node *rnode;
> @@ -118,7 +125,7 @@ static void stm32_bsec_init_dt(struct device_d *dev, struct regmap *map)
>  	rnode = of_find_node_by_phandle(phandle);
>  	offset = be32_to_cpup(prop++);
>  
> -	ret = stm32_bsec_read_mac(map, offset, mac);
> +	ret = stm32_bsec_read_mac(priv, offset, mac);
>  	if (ret) {
>  		dev_warn(dev, "error setting MAC address: %s\n", strerror(-ret));
>  		return;
> @@ -159,7 +166,7 @@ static int stm32_bsec_probe(struct device_d *dev)
>  	if (IS_ENABLED(CONFIG_MACHINE_ID))
>  		stm32_bsec_set_unique_machine_id(map);
>  
> -	stm32_bsec_init_dt(dev, map);
> +	stm32_bsec_init_dt(priv, dev, map);
>  
>  	return 0;
>  }
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list