From b484296f6fda23ab2c996892826ebcc12cbd2303 Mon Sep 17 00:00:00 2001 From: AKASHI Takahiro Date: Wed, 11 Mar 2020 15:18:18 +0900 Subject: [PATCH 1/6] efi_loader: get_memory_map: return parameters whenever possible Currently, if GetMemoryMap API returns EFI_BUFFER_TOO_SMALL, it doesn't set valid values to other parameters, descriptor_size and descriptor_version, except memory_map_size. Some efi applications, however, may use those value; in particular, xen uses descriptor_size to calculate a size of buffer to be allocated. While UEFI specification is ambiguous in this point, it would be better to address this issue proactively to maximize the compatibility with existing efi applications. With this patch, for example, xen.efi (and hence linux kernel) can be started via bootefi without modification. Signed-off-by: AKASHI Takahiro Reviewed-by: Heinrich Schuchardt --- lib/efi_loader/efi_memory.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c index 89adf20310..97d90f069a 100644 --- a/lib/efi_loader/efi_memory.c +++ b/lib/efi_loader/efi_memory.c @@ -627,18 +627,18 @@ efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size, *memory_map_size = map_size; - if (provided_map_size < map_size) - return EFI_BUFFER_TOO_SMALL; - - if (!memory_map) - return EFI_INVALID_PARAMETER; - if (descriptor_size) *descriptor_size = sizeof(struct efi_mem_desc); if (descriptor_version) *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION; + if (provided_map_size < map_size) + return EFI_BUFFER_TOO_SMALL; + + if (!memory_map) + return EFI_INVALID_PARAMETER; + /* Copy list into array */ /* Return the list in ascending order */ memory_map = &memory_map[map_entries - 1]; From e8bced62b6be344dbc922d88b1b278e316e1585f Mon Sep 17 00:00:00 2001 From: AKASHI Takahiro Date: Fri, 28 Feb 2020 09:05:04 +0900 Subject: [PATCH 2/6] cmd: efidebug: fix a failure of "boot rm" sub-command There is a wrong usage of utf8_utf16_strncpy() in "boot rm" command, and then it will end up with a failure of this command due to a wrong value of an interim variable ("var_name16"). Signed-off-by: AKASHI Takahiro Reviewed-by: Heinrich Schuchardt --- cmd/efidebug.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/efidebug.c b/cmd/efidebug.c index 21dfd44fcc..056ebb1083 100644 --- a/cmd/efidebug.c +++ b/cmd/efidebug.c @@ -649,7 +649,7 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag, int id, i; char *endp; char var_name[9]; - u16 var_name16[9]; + u16 var_name16[9], *p; efi_status_t ret; if (argc == 1) @@ -662,7 +662,8 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag, return CMD_RET_FAILURE; sprintf(var_name, "Boot%04X", id); - utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9); + p = var_name16; + utf8_utf16_strncpy(&p, var_name, 9); ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL)); if (ret) { From 30efb5dd43fdeb6173c61a55b537fcfba2c45f3f Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Mon, 2 Mar 2020 20:13:10 +0100 Subject: [PATCH 3/6] cmd: efidebug: correct error message Add the missing line feed at the error message if the variable referred to by 'efidebug boot rm' does not exist. Shorten the format string by using the variable name instead of the number of the boot variable. Signed-off-by: Heinrich Schuchardt --- cmd/efidebug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/efidebug.c b/cmd/efidebug.c index 056ebb1083..8c3681c37d 100644 --- a/cmd/efidebug.c +++ b/cmd/efidebug.c @@ -667,7 +667,7 @@ static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag, ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL)); if (ret) { - printf("Cannot remove Boot%04X", id); + printf("Cannot remove %ls\n", var_name16); return CMD_RET_FAILURE; } } From 2b8568f461188e8bd14ebe9b6968c56baca7b142 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Fri, 6 Mar 2020 21:56:10 +0100 Subject: [PATCH 4/6] efi_loader: unnecessary assignment in efi_queue_event The assigned value NULL is never used. Signed-off-by: Heinrich Schuchardt --- lib/efi_loader/efi_boottime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index 1f598b357a..e533a185f8 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -208,7 +208,7 @@ static void efi_process_event_queue(void) */ static void efi_queue_event(struct efi_event *event) { - struct efi_event *item = NULL; + struct efi_event *item; if (!event->notify_function) return; From c312c4b4dcd0574b81ff2c0d543f42295305ffaa Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Sun, 2 Feb 2020 19:45:35 +0100 Subject: [PATCH 5/6] drivers/rng: simplify Kconfig For all sandbox systems with DM_RNG we enable RNG_SANDBOX. So we can simply set the default to yes. All rng drivers depend on DM_RNG. Use a single 'if' instead of individual dependencies. Now 'make menuconfig' shows the individual drivers neatly indented under the DM_RNG entry. Signed-off-by: Heinrich Schuchardt Reviewed-by: Sughosh Ganu --- configs/sandbox64_defconfig | 1 - configs/sandbox_defconfig | 1 - drivers/rng/Kconfig | 9 +++++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig index d1c94b65a1..71a4d7fccb 100644 --- a/configs/sandbox64_defconfig +++ b/configs/sandbox64_defconfig @@ -169,7 +169,6 @@ CONFIG_REMOTEPROC_SANDBOX=y CONFIG_DM_RESET=y CONFIG_SANDBOX_RESET=y CONFIG_DM_RNG=y -CONFIG_RNG_SANDBOX=y CONFIG_DM_RTC=y CONFIG_RTC_RV8803=y CONFIG_SANDBOX_SERIAL=y diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index 45b5475b79..f96891ecae 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -189,7 +189,6 @@ CONFIG_REMOTEPROC_SANDBOX=y CONFIG_DM_RESET=y CONFIG_SANDBOX_RESET=y CONFIG_DM_RNG=y -CONFIG_RNG_SANDBOX=y CONFIG_DM_RTC=y CONFIG_RTC_RV8803=y CONFIG_DEBUG_UART_SANDBOX=y diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index 893b89d49b..c1aa43b823 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -6,16 +6,21 @@ config DM_RNG This interface is used to initialise the rng device and to read the random seed from the device. +if DM_RNG + config RNG_SANDBOX bool "Sandbox random number generator" - depends on SANDBOX && DM_RNG + depends on SANDBOX + default y help Enable random number generator for sandbox. This is an emulation of a rng device. config RNG_STM32MP1 bool "Enable random number generator for STM32MP1" - depends on ARCH_STM32MP && DM_RNG + depends on ARCH_STM32MP default n help Enable STM32MP1 rng driver. + +endif From bc40eb278b0c14b990556eab34a57b2b8ed598f4 Mon Sep 17 00:00:00 2001 From: Heinrich Schuchardt Date: Sun, 2 Feb 2020 19:52:04 +0100 Subject: [PATCH 6/6] drivers/rng: add Amlogic hardware RNG driver Add support for the hardware random number generator of Amlogic SOCs. Signed-off-by: Heinrich Schuchardt Reviewed-by: Neil Armstrong Reviewed-by: Sughosh Ganu --- drivers/rng/Kconfig | 8 +++ drivers/rng/Makefile | 1 + drivers/rng/meson-rng.c | 120 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 drivers/rng/meson-rng.c diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index c1aa43b823..edb6152bb9 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -8,6 +8,14 @@ config DM_RNG if DM_RNG +config RNG_MESON + bool "Amlogic Meson Random Number Generator support" + depends on ARCH_MESON + default y + help + Enable support for hardware random number generator + of Amlogic Meson SoCs. + config RNG_SANDBOX bool "Sandbox random number generator" depends on SANDBOX diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile index 3517005541..6a8a66779b 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -4,5 +4,6 @@ # obj-$(CONFIG_DM_RNG) += rng-uclass.o +obj-$(CONFIG_RNG_MESON) += meson-rng.o obj-$(CONFIG_RNG_SANDBOX) += sandbox_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o diff --git a/drivers/rng/meson-rng.c b/drivers/rng/meson-rng.c new file mode 100644 index 0000000000..4b81a62353 --- /dev/null +++ b/drivers/rng/meson-rng.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright 2020, Heinrich Schuchardt + * + * Driver for Amlogic hardware random number generator + */ + +#include +#include +#include +#include +#include + +struct meson_rng_platdata { + fdt_addr_t base; + struct clk clk; +}; + +/** + * meson_rng_read() - fill buffer with random bytes + * + * @buffer: buffer to receive data + * @size: size of buffer + * + * Return: 0 + */ +static int meson_rng_read(struct udevice *dev, void *data, size_t len) +{ + struct meson_rng_platdata *pdata = dev_get_platdata(dev); + char *buffer = (char *)data; + + while (len) { + u32 rand = readl(pdata->base); + size_t step; + + if (len >= 4) + step = 4; + else + step = len; + memcpy(buffer, &rand, step); + buffer += step; + len -= step; + } + return 0; +} + +/** + * meson_rng_probe() - probe rng device + * + * @dev: device + * Return: 0 if ok + */ +static int meson_rng_probe(struct udevice *dev) +{ + struct meson_rng_platdata *pdata = dev_get_platdata(dev); + int err; + + err = clk_enable(&pdata->clk); + if (err) + return err; + + return 0; +} + +/** + * meson_rng_remove() - deinitialize rng device + * + * @dev: device + * Return: 0 if ok + */ +static int meson_rng_remove(struct udevice *dev) +{ + struct meson_rng_platdata *pdata = dev_get_platdata(dev); + + return clk_disable(&pdata->clk); +} + +/** + * meson_rng_ofdata_to_platdata() - transfer device tree data to plaform data + * + * @dev: device + * Return: 0 if ok + */ +static int meson_rng_ofdata_to_platdata(struct udevice *dev) +{ + struct meson_rng_platdata *pdata = dev_get_platdata(dev); + int err; + + pdata->base = dev_read_addr(dev); + if (!pdata->base) + return -ENODEV; + + err = clk_get_by_name(dev, "core", &pdata->clk); + if (err) + return err; + + return 0; +} + +static const struct dm_rng_ops meson_rng_ops = { + .read = meson_rng_read, +}; + +static const struct udevice_id meson_rng_match[] = { + { + .compatible = "amlogic,meson-rng", + }, + {}, +}; + +U_BOOT_DRIVER(meson_rng) = { + .name = "meson-rng", + .id = UCLASS_RNG, + .of_match = meson_rng_match, + .ops = &meson_rng_ops, + .probe = meson_rng_probe, + .remove = meson_rng_remove, + .platdata_auto_alloc_size = sizeof(struct meson_rng_platdata), + .ofdata_to_platdata = meson_rng_ofdata_to_platdata, +};