From be9c253f21321dfd2b445a75fdbc94895c487097 Mon Sep 17 00:00:00 2001 From: Laura Abbott Date: Fri, 9 Aug 2013 18:17:06 -0700 Subject: [PATCH] msm: Add support for early random numbers Currently, the software random number generator is not initialized until relatively late in the boot process. Software that relies on random numbers early will not be reliable. Entropy sources are available early but not early enough for some use cases which means that moving the software random number generation earlier is not an option. Instead we initialize the random pool with values from a HW RNG accessed through a call to the secure environment. Change-Id: Id756a8740df7ec938984c3e7de22681e0270bb5b Signed-off-by: Laura Abbott [ohaugan@codeaurora.org: Removed unsupported scm api] Signed-off-by: Olav Haugan --- drivers/soc/qcom/Kconfig | 8 +++++ drivers/soc/qcom/Makefile | 1 + drivers/soc/qcom/early_random.c | 56 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 drivers/soc/qcom/early_random.c diff --git a/drivers/soc/qcom/Kconfig b/drivers/soc/qcom/Kconfig index 15c389254cac..0a72ebe62c2e 100644 --- a/drivers/soc/qcom/Kconfig +++ b/drivers/soc/qcom/Kconfig @@ -349,6 +349,14 @@ config QCOM_COMMAND_DB Command DB queries shared memory by key string for shared system resources +config QCOM_EARLY_RANDOM + bool "Initialize random pool very early" + help + The standard random pool may not initialize until late in the boot + process which means that any calls to get random numbers before then + may not be truly random. Select this option to make an early call + to get some random data to put in the pool. If unsure, say N. + config QTI_RPMH_API bool "QTI RPMH (h/w accelerators) Communication API" select MAILBOX diff --git a/drivers/soc/qcom/Makefile b/drivers/soc/qcom/Makefile index 0974960f3c4c..5d47461e1f68 100644 --- a/drivers/soc/qcom/Makefile +++ b/drivers/soc/qcom/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_QCOM_SMSM) += smsm.o obj-$(CONFIG_QCOM_WCNSS_CTRL) += wcnss_ctrl.o CFLAGS_scm.o :=$(call as-instr,.arch_extension sec,-DREQUIRES_SEC=1) obj-$(CONFIG_QCOM_SCM) += scm.o +obj-$(CONFIG_QCOM_EARLY_RANDOM) += early_random.o obj-$(CONFIG_SOC_BUS) += socinfo.o obj-$(CONFIG_MSM_BOOT_STATS) += boot_stats.o obj-$(CONFIG_MSM_CORE_HANG_DETECT) += core_hang_detect.o diff --git a/drivers/soc/qcom/early_random.c b/drivers/soc/qcom/early_random.c new file mode 100644 index 000000000000..1033d11dae79 --- /dev/null +++ b/drivers/soc/qcom/early_random.c @@ -0,0 +1,56 @@ +/* Copyright (c) 2013-2014, 2016-2017, The Linux Foundation. All rights + * reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include + +#include + +#include + +#define TZ_SVC_CRYPTO 10 +#define PRNG_CMD_ID 0x01 + +struct tz_prng_data { + uint8_t *out_buf; + uint32_t out_buf_sz; +} __packed; + +DEFINE_SCM_BUFFER(common_scm_buf); +#define RANDOM_BUFFER_SIZE PAGE_SIZE +char random_buffer[RANDOM_BUFFER_SIZE] __aligned(PAGE_SIZE); + +void __init init_random_pool(void) +{ + struct tz_prng_data data; + int ret; + struct scm_desc desc; + + data.out_buf = (uint8_t *) virt_to_phys(random_buffer); + desc.args[0] = (unsigned long) data.out_buf; + desc.args[1] = data.out_buf_sz = SZ_512; + desc.arginfo = SCM_ARGS(2, SCM_RW, SCM_VAL); + + dmac_flush_range(random_buffer, random_buffer + RANDOM_BUFFER_SIZE); + + ret = scm_call2(SCM_SIP_FNID(TZ_SVC_CRYPTO, PRNG_CMD_ID), &desc); + + if (!ret) { + dmac_inv_range(random_buffer, random_buffer + + RANDOM_BUFFER_SIZE); + add_device_randomness(random_buffer, SZ_512); + } +} +