blob: bb11d8d0e0cb34acfa8a0fe2a44c24d385f1827b [file] [log] [blame]
Sughosh Ganuf552fa42019-12-29 00:01:05 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019, Linaro Limited
4 */
5
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +02006#define LOG_CATEGORY LOGC_EFI
7
Sughosh Ganuf552fa42019-12-29 00:01:05 +05308#include <common.h>
9#include <dm.h>
10#include <efi_loader.h>
11#include <efi_rng.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Sughosh Ganuf552fa42019-12-29 00:01:05 +053013#include <rng.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Sughosh Ganuf552fa42019-12-29 00:01:05 +053015
16DECLARE_GLOBAL_DATA_PTR;
17
Sughosh Ganu33c37d92019-12-29 00:01:06 +053018const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
19
Heinrich Schuchardtd417b942020-01-09 20:49:44 +010020/**
21 * platform_get_rng_device() - retrieve random number generator
22 *
23 * This function retrieves the udevice implementing a hardware random
24 * number generator.
25 *
26 * This function may be overridden if special initialization is needed.
27 *
28 * @dev: udevice
29 * Return: status code
30 */
Sughosh Ganuf552fa42019-12-29 00:01:05 +053031__weak efi_status_t platform_get_rng_device(struct udevice **dev)
32{
33 int ret;
34 struct udevice *devp;
35
36 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
37 if (ret) {
38 debug("Unable to get rng device\n");
39 return EFI_DEVICE_ERROR;
40 }
41
42 *dev = devp;
43
44 return EFI_SUCCESS;
45}
46
Heinrich Schuchardtd417b942020-01-09 20:49:44 +010047/**
48 * rng_getinfo() - get information about random number generation
49 *
50 * This function implement the GetInfo() service of the EFI random number
51 * generator protocol. See the UEFI spec for details.
52 *
53 * @this: random number generator protocol instance
54 * @rng_algorithm_list_size: number of random number generation algorithms
55 * @rng_algorithm_list: descriptions of random number generation
56 * algorithms
57 * Return: status code
58 */
Sughosh Ganuf552fa42019-12-29 00:01:05 +053059static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
60 efi_uintn_t *rng_algorithm_list_size,
61 efi_guid_t *rng_algorithm_list)
62{
63 efi_status_t ret = EFI_SUCCESS;
64 efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
65
66 EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
67 rng_algorithm_list);
68
69 if (!this || !rng_algorithm_list_size) {
70 ret = EFI_INVALID_PARAMETER;
71 goto back;
72 }
73
74 if (!rng_algorithm_list ||
75 *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
76 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
77 ret = EFI_BUFFER_TOO_SMALL;
78 goto back;
79 }
80
81 /*
82 * For now, use EFI_RNG_ALGORITHM_RAW as the default
83 * algorithm. If a new algorithm gets added in the
84 * future through a Kconfig, rng_algo_guid will be set
85 * based on that Kconfig option
86 */
87 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
88 guidcpy(rng_algorithm_list, &rng_algo_guid);
89
90back:
91 return EFI_EXIT(ret);
92}
93
Heinrich Schuchardtd417b942020-01-09 20:49:44 +010094/**
95 * rng_getrng() - get random value
96 *
97 * This function implement the GetRng() service of the EFI random number
98 * generator protocol. See the UEFI spec for details.
99 *
100 * @this: random number generator protocol instance
101 * @rng_algorithm: random number generation algorithm
102 * @rng_value_length: number of random bytes to generate, buffer length
103 * @rng_value: buffer to receive random bytes
104 * Return: status code
105 */
Sughosh Ganuf552fa42019-12-29 00:01:05 +0530106static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
107 efi_guid_t *rng_algorithm,
108 efi_uintn_t rng_value_length,
109 uint8_t *rng_value)
110{
111 int ret;
112 efi_status_t status = EFI_SUCCESS;
113 struct udevice *dev;
114 const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
115
116 EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
117 rng_value);
118
119 if (!this || !rng_value || !rng_value_length) {
120 status = EFI_INVALID_PARAMETER;
121 goto back;
122 }
123
124 if (rng_algorithm) {
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100125 EFI_PRINT("RNG algorithm %pUs\n", rng_algorithm);
Sughosh Ganuf552fa42019-12-29 00:01:05 +0530126 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
127 status = EFI_UNSUPPORTED;
128 goto back;
129 }
130 }
131
132 ret = platform_get_rng_device(&dev);
133 if (ret != EFI_SUCCESS) {
134 EFI_PRINT("Rng device not found\n");
135 status = EFI_UNSUPPORTED;
136 goto back;
137 }
138
139 ret = dm_rng_read(dev, rng_value, rng_value_length);
140 if (ret < 0) {
141 EFI_PRINT("Rng device read failed\n");
142 status = EFI_DEVICE_ERROR;
143 goto back;
144 }
145
146back:
147 return EFI_EXIT(status);
148}
149
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200150static const struct efi_rng_protocol efi_rng_protocol = {
Sughosh Ganuf552fa42019-12-29 00:01:05 +0530151 .get_info = rng_getinfo,
152 .get_rng = getrng,
153};
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200154
155/**
156 * efi_rng_register() - register EFI_RNG_PROTOCOL
157 *
158 * If a RNG device is available, the Random Number Generator Protocol is
159 * registered.
160 *
161 * Return: An error status is only returned if adding the protocol fails.
162 */
163efi_status_t efi_rng_register(void)
164{
165 efi_status_t ret;
166 struct udevice *dev;
167
168 ret = platform_get_rng_device(&dev);
169 if (ret != EFI_SUCCESS) {
Paulo Alcantara337c97d2020-11-06 13:52:43 -0300170 log_warning("Missing RNG device for EFI_RNG_PROTOCOL\n");
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200171 return EFI_SUCCESS;
172 }
173 ret = efi_add_protocol(efi_root, &efi_guid_rng_protocol,
174 (void *)&efi_rng_protocol);
175 if (ret != EFI_SUCCESS)
Paulo Alcantara337c97d2020-11-06 13:52:43 -0300176 log_err("Cannot install EFI_RNG_PROTOCOL\n");
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200177
178 return ret;
179}