1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019, Linaro Limited
6 #define LOG_CATEGORY LOGC_EFI
9 #include <efi_loader.h>
13 #include <asm/global_data.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
20 * platform_get_rng_device() - retrieve random number generator
22 * This function retrieves the udevice implementing a hardware random
25 * This function may be overridden if special initialization is needed.
30 __weak efi_status_t platform_get_rng_device(struct udevice **dev)
35 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
37 debug("Unable to get rng device\n");
38 return EFI_DEVICE_ERROR;
47 * rng_getinfo() - get information about random number generation
49 * This function implement the GetInfo() service of the EFI random number
50 * generator protocol. See the UEFI spec for details.
52 * @this: random number generator protocol instance
53 * @rng_algorithm_list_size: number of random number generation algorithms
54 * @rng_algorithm_list: descriptions of random number generation
58 static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
59 efi_uintn_t *rng_algorithm_list_size,
60 efi_guid_t *rng_algorithm_list)
62 efi_status_t ret = EFI_SUCCESS;
63 efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
65 EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
68 if (!this || !rng_algorithm_list_size) {
69 ret = EFI_INVALID_PARAMETER;
73 if (!rng_algorithm_list ||
74 *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
75 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
76 ret = EFI_BUFFER_TOO_SMALL;
81 * For now, use EFI_RNG_ALGORITHM_RAW as the default
82 * algorithm. If a new algorithm gets added in the
83 * future through a Kconfig, rng_algo_guid will be set
84 * based on that Kconfig option
86 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
87 guidcpy(rng_algorithm_list, &rng_algo_guid);
94 * getrng() - get random value
96 * This function implement the GetRng() service of the EFI random number
97 * generator protocol. See the UEFI spec for details.
99 * @this: random number generator protocol instance
100 * @rng_algorithm: random number generation algorithm
101 * @rng_value_length: number of random bytes to generate, buffer length
102 * @rng_value: buffer to receive random bytes
103 * Return: status code
105 static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
106 efi_guid_t *rng_algorithm,
107 efi_uintn_t rng_value_length,
111 efi_status_t status = EFI_SUCCESS;
113 const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
115 EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
118 if (!this || !rng_value || !rng_value_length) {
119 status = EFI_INVALID_PARAMETER;
124 EFI_PRINT("RNG algorithm %pUs\n", rng_algorithm);
125 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
126 status = EFI_UNSUPPORTED;
131 ret = platform_get_rng_device(&dev);
132 if (ret != EFI_SUCCESS) {
133 EFI_PRINT("Rng device not found\n");
134 status = EFI_UNSUPPORTED;
138 ret = dm_rng_read(dev, rng_value, rng_value_length);
140 EFI_PRINT("Rng device read failed\n");
141 status = EFI_DEVICE_ERROR;
146 return EFI_EXIT(status);
149 static const struct efi_rng_protocol efi_rng_protocol = {
150 .get_info = rng_getinfo,
155 * efi_rng_register() - register EFI_RNG_PROTOCOL
157 * If a RNG device is available, the Random Number Generator Protocol is
160 * Return: An error status is only returned if adding the protocol fails.
162 efi_status_t efi_rng_register(void)
167 ret = platform_get_rng_device(&dev);
168 if (ret != EFI_SUCCESS) {
169 log_warning("Missing RNG device for EFI_RNG_PROTOCOL\n");
172 ret = efi_add_protocol(efi_root, &efi_guid_rng_protocol,
173 (void *)&efi_rng_protocol);
174 if (ret != EFI_SUCCESS)
175 log_err("Cannot install EFI_RNG_PROTOCOL\n");