1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019, Linaro Limited
8 #include <efi_loader.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
18 * platform_get_rng_device() - retrieve random number generator
20 * This function retrieves the udevice implementing a hardware random
23 * This function may be overridden if special initialization is needed.
28 __weak efi_status_t platform_get_rng_device(struct udevice **dev)
33 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
35 debug("Unable to get rng device\n");
36 return EFI_DEVICE_ERROR;
45 * rng_getinfo() - get information about random number generation
47 * This function implement the GetInfo() service of the EFI random number
48 * generator protocol. See the UEFI spec for details.
50 * @this: random number generator protocol instance
51 * @rng_algorithm_list_size: number of random number generation algorithms
52 * @rng_algorithm_list: descriptions of random number generation
56 static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
57 efi_uintn_t *rng_algorithm_list_size,
58 efi_guid_t *rng_algorithm_list)
60 efi_status_t ret = EFI_SUCCESS;
61 efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
63 EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
66 if (!this || !rng_algorithm_list_size) {
67 ret = EFI_INVALID_PARAMETER;
71 if (!rng_algorithm_list ||
72 *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
73 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
74 ret = EFI_BUFFER_TOO_SMALL;
79 * For now, use EFI_RNG_ALGORITHM_RAW as the default
80 * algorithm. If a new algorithm gets added in the
81 * future through a Kconfig, rng_algo_guid will be set
82 * based on that Kconfig option
84 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
85 guidcpy(rng_algorithm_list, &rng_algo_guid);
92 * rng_getrng() - get random value
94 * This function implement the GetRng() service of the EFI random number
95 * generator protocol. See the UEFI spec for details.
97 * @this: random number generator protocol instance
98 * @rng_algorithm: random number generation algorithm
99 * @rng_value_length: number of random bytes to generate, buffer length
100 * @rng_value: buffer to receive random bytes
101 * Return: status code
103 static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
104 efi_guid_t *rng_algorithm,
105 efi_uintn_t rng_value_length,
109 efi_status_t status = EFI_SUCCESS;
111 const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
113 EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
116 if (!this || !rng_value || !rng_value_length) {
117 status = EFI_INVALID_PARAMETER;
122 EFI_PRINT("RNG algorithm %pUl\n", rng_algorithm);
123 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
124 status = EFI_UNSUPPORTED;
129 ret = platform_get_rng_device(&dev);
130 if (ret != EFI_SUCCESS) {
131 EFI_PRINT("Rng device not found\n");
132 status = EFI_UNSUPPORTED;
136 ret = dm_rng_read(dev, rng_value, rng_value_length);
138 EFI_PRINT("Rng device read failed\n");
139 status = EFI_DEVICE_ERROR;
144 return EFI_EXIT(status);
147 const struct efi_rng_protocol efi_rng_protocol = {
148 .get_info = rng_getinfo,