From 2d307fb9ed2065cc1596a3c4263e55d1cae6799d Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Tue, 3 Oct 2023 03:09:01 +0200 Subject: input: avoid NULL dereference Before using the result of env_get("stdin") we must check if it is NULL. Avoid #if. This resolves the -Wunused-but-set-variable issue and we don't need a dummy assignment in the else branch. Anyway this warning is disabled in the Makefile. For sake of readability use an early return after the configuration check. Checking CONFIG_SPL_BUILD is incorrect as env_get() is only defined if CONFIG_$(SPL_TPL)ENV_SUPPORT=y. Fixes: 985ca3945fa3 ("spl: input: Allow input in SPL and TPL") Signed-off-by: Heinrich Schuchardt Reviewed-by: Simon Glass Reviewed-by: Tom Rini --- drivers/input/input.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'drivers') diff --git a/drivers/input/input.c b/drivers/input/input.c index a4341e8c7ce..8a6506e7c6f 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -669,17 +669,22 @@ int input_stdio_register(struct stdio_dev *dev) int error; error = stdio_register(dev); -#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT) - /* check if this is the standard input device */ - if (!error && strcmp(env_get("stdin"), dev->name) == 0) { - /* reassign the console */ - if (OVERWRITE_CONSOLE || - console_assign(stdin, dev->name)) - return -1; + + if (!CONFIG_IS_ENABLED(ENV_SUPPORT)) + return 0; + + if (!error) { + const char *cstdin; + + /* check if this is the standard input device */ + cstdin = env_get("stdin"); + if (cstdin && !strcmp(cstdin, dev->name)) { + /* reassign the console */ + if (OVERWRITE_CONSOLE || + console_assign(stdin, dev->name)) + return -1; + } } -#else - error = error; -#endif return 0; } -- cgit v1.3.1 From 538b97dd5d6be8206b532664cf616c567434368d Mon Sep 17 00:00:00 2001 From: Nicolò Veronese Date: Wed, 4 Oct 2023 00:14:26 +0200 Subject: spi: mtk_spim: prevent global pll clock override MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With commit 793e62301180 ("spi: mtk_spim: get spi clk rate only once") a new system to calculate the SPI clocks has been added. Unfortunately, the do_div macro overrides the global priv->pll_clk_rate field. This will cause to have a reduced clock rate on each subsequent SPI call. Signed-off-by: Valerio 'ftp21' Mancini Signed-off-by: Nicolò Veronese --- drivers/spi/mtk_spim.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/spi/mtk_spim.c b/drivers/spi/mtk_spim.c index 418e586b91d..90f4c3cecb9 100644 --- a/drivers/spi/mtk_spim.c +++ b/drivers/spi/mtk_spim.c @@ -409,7 +409,7 @@ static int mtk_spim_transfer_wait(struct spi_slave *slave, { struct udevice *bus = dev_get_parent(slave->dev); struct mtk_spim_priv *priv = dev_get_priv(bus); - u32 sck_l, sck_h, clk_count, reg; + u32 pll_clk, sck_l, sck_h, clk_count, reg; ulong us = 1; int ret = 0; @@ -418,11 +418,12 @@ static int mtk_spim_transfer_wait(struct spi_slave *slave, else clk_count = op->data.nbytes; + pll_clk = priv->pll_clk_rate; sck_l = readl(priv->base + SPI_CFG2_REG) >> SPI_CFG2_SCK_LOW_OFFSET; sck_h = readl(priv->base + SPI_CFG2_REG) & SPI_CFG2_SCK_HIGH_MASK; - do_div(priv->pll_clk_rate, sck_l + sck_h + 2); + do_div(pll_clk, sck_l + sck_h + 2); - us = CLK_TO_US(priv->pll_clk_rate, clk_count * 8); + us = CLK_TO_US(pll_clk, clk_count * 8); us += 1000 * 1000; /* 1s tolerance */ if (us > UINT_MAX) -- cgit v1.3.1 From 31565bb0aa2d76b6941e96bcdbd204bae49ca828 Mon Sep 17 00:00:00 2001 From: Andre Przywara Date: Wed, 30 Aug 2023 12:32:30 +0100 Subject: driver: rng: Add DM_RNG interface for ARMv8.5 RNDR registers The ARMv8.5 architecture extension defines architectural RNDR/RNDRRS system registers, that provide 64 bits worth of randomness on every read. Since it's an extension, and implementing it is optional, there is a field in the ID_AA64ISAR0_EL1 ID register to query the availability of those registers. Add a UCLASS_RNG driver that returns entropy via repeated reads from those system registers, if the extension is implemented. The driver always binds, but checks the availability in the probe() routine. This helps systems which suffer from low boot entropy, since U-Boot can provide entropy via the generic UEFI entropy gathering protocol to the OS, at an early stage. Signed-off-by: Andre Przywara Reviewed-by: Simon Glass --- arch/arm/include/asm/system.h | 1 + drivers/rng/Kconfig | 6 ++++ drivers/rng/Makefile | 1 + drivers/rng/arm_rndr.c | 82 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 drivers/rng/arm_rndr.c (limited to 'drivers') diff --git a/arch/arm/include/asm/system.h b/arch/arm/include/asm/system.h index 87d1c77e8b1..0eae857e73a 100644 --- a/arch/arm/include/asm/system.h +++ b/arch/arm/include/asm/system.h @@ -84,6 +84,7 @@ #define HCR_EL2_HCD_DIS (1 << 29) /* Hypervisor Call disabled */ #define HCR_EL2_AMO_EL2 (1 << 5) /* Route SErrors to EL2 */ +#define ID_AA64ISAR0_EL1_RNDR (0xFUL << 60) /* RNDR random registers */ /* * ID_AA64ISAR1_EL1 bits definitions */ diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index 24666bff987..994cc35b274 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -76,6 +76,12 @@ config RNG_SMCCC_TRNG Enable random number generator for platforms that support Arm SMCCC TRNG interface. +config RNG_ARM_RNDR + bool "Generic ARMv8.5 RNDR register" + depends on DM_RNG && ARM64 + help + Use the ARMv8.5 RNDR register to provide random numbers. + config TPM_RNG bool "Enable random number generator on TPM device" depends on TPM diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile index 192f911e155..47b323e61ee 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -13,4 +13,5 @@ obj-$(CONFIG_RNG_STM32) += stm32_rng.o obj-$(CONFIG_RNG_ROCKCHIP) += rockchip_rng.o obj-$(CONFIG_RNG_IPROC200) += iproc_rng200.o obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.o +obj-$(CONFIG_RNG_ARM_RNDR) += arm_rndr.o obj-$(CONFIG_TPM_RNG) += tpm_rng.o diff --git a/drivers/rng/arm_rndr.c b/drivers/rng/arm_rndr.c new file mode 100644 index 00000000000..55989743eae --- /dev/null +++ b/drivers/rng/arm_rndr.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2023, Arm Ltd. + * + * Use the (optional) ARMv8.5 RNDR register to provide random numbers to + * U-Boot's UCLASS_RNG users. + * Detection is done at runtime using the CPU ID registers. + */ + +#define LOG_CATEGORY UCLASS_RNG + +#include +#include +#include +#include +#include + +#define DRIVER_NAME "arm-rndr" + +static bool cpu_has_rndr(void) +{ + uint64_t reg; + + __asm__ volatile("mrs %0, ID_AA64ISAR0_EL1\n" : "=r" (reg)); + return !!(reg & ID_AA64ISAR0_EL1_RNDR); +} + +/* + * The system register name is RNDR, but this isn't widely known among older + * toolchains, and also triggers errors because of it being an architecture + * extension. Since we check the availability of the register before, it's + * fine to use here, though. + */ +#define RNDR "S3_3_C2_C4_0" + +static uint64_t read_rndr(void) +{ + uint64_t reg; + + __asm__ volatile("mrs %0, " RNDR "\n" : "=r" (reg)); + + return reg; +} + +static int arm_rndr_read(struct udevice *dev, void *data, size_t len) +{ + uint64_t random; + + while (len) { + int tocopy = min(sizeof(uint64_t), len); + + random = read_rndr(); + memcpy(data, &random, tocopy); + len -= tocopy; + data += tocopy; + } + + return 0; +} + +static const struct dm_rng_ops arm_rndr_ops = { + .read = arm_rndr_read, +}; + +static int arm_rndr_probe(struct udevice *dev) +{ + if (!cpu_has_rndr()) + return -ENODEV; + + return 0; +} + +U_BOOT_DRIVER(arm_rndr) = { + .name = DRIVER_NAME, + .id = UCLASS_RNG, + .ops = &arm_rndr_ops, + .probe = arm_rndr_probe, +}; + +U_BOOT_DRVINFO(cpu_arm_rndr) = { + .name = DRIVER_NAME, +}; -- cgit v1.3.1 From 89cfa35bfc4c5bcabdcf31c8e4631e0dddd508c3 Mon Sep 17 00:00:00 2001 From: Sean Anderson Date: Sat, 30 Sep 2023 16:45:46 -0400 Subject: misc: fs_loader: Fix alignment of fs_loader driver DM_DRIVER_GET will redeclare the fs_loader driver without the correct alignment. This causes GCC to use the default section alignment of 32 bytes. This in turn creates a gap in the linker list due to the padding required to achieve the correct alignment, corrupting all further entries. Use DM_DRIVER_REF instead, which doesn't redeclare anything. Fixes: 0998a20cfc6 ("misc: fs_loader: Add function to get the chosen loader") Signed-off-by: Sean Anderson --- drivers/misc/fs_loader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/misc/fs_loader.c b/drivers/misc/fs_loader.c index ccf5c7a8037..1ffc199ba1e 100644 --- a/drivers/misc/fs_loader.c +++ b/drivers/misc/fs_loader.c @@ -316,7 +316,7 @@ int get_fs_loader(struct udevice **dev) return ret; /* Just create a new device */ - ret = device_bind(dm_root(), DM_DRIVER_GET(fs_loader), "default-loader", + ret = device_bind(dm_root(), DM_DRIVER_REF(fs_loader), "default-loader", &default_plat, ofnode_null(), dev); if (ret) return ret; -- cgit v1.3.1