Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2019, Linaro Limited |
| 4 | */ |
| 5 | |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_EFI |
| 7 | |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <efi_loader.h> |
| 11 | #include <efi_rng.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 13 | #include <rng.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Sughosh Ganu | 693a4f4 | 2019-12-29 00:01:06 +0530 | [diff] [blame] | 17 | const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID; |
| 18 | |
Heinrich Schuchardt | 0410be2 | 2020-01-09 20:49:44 +0100 | [diff] [blame] | 19 | /** |
| 20 | * platform_get_rng_device() - retrieve random number generator |
| 21 | * |
| 22 | * This function retrieves the udevice implementing a hardware random |
| 23 | * number generator. |
| 24 | * |
| 25 | * This function may be overridden if special initialization is needed. |
| 26 | * |
| 27 | * @dev: udevice |
| 28 | * Return: status code |
| 29 | */ |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 30 | __weak efi_status_t platform_get_rng_device(struct udevice **dev) |
| 31 | { |
| 32 | int ret; |
| 33 | struct udevice *devp; |
| 34 | |
| 35 | ret = uclass_get_device(UCLASS_RNG, 0, &devp); |
| 36 | if (ret) { |
| 37 | debug("Unable to get rng device\n"); |
| 38 | return EFI_DEVICE_ERROR; |
| 39 | } |
| 40 | |
| 41 | *dev = devp; |
| 42 | |
| 43 | return EFI_SUCCESS; |
| 44 | } |
| 45 | |
Heinrich Schuchardt | 0410be2 | 2020-01-09 20:49:44 +0100 | [diff] [blame] | 46 | /** |
| 47 | * rng_getinfo() - get information about random number generation |
| 48 | * |
| 49 | * This function implement the GetInfo() service of the EFI random number |
| 50 | * generator protocol. See the UEFI spec for details. |
| 51 | * |
| 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 |
| 55 | * algorithms |
| 56 | * Return: status code |
| 57 | */ |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 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) |
| 61 | { |
| 62 | efi_status_t ret = EFI_SUCCESS; |
| 63 | efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW; |
| 64 | |
| 65 | EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size, |
| 66 | rng_algorithm_list); |
| 67 | |
| 68 | if (!this || !rng_algorithm_list_size) { |
| 69 | ret = EFI_INVALID_PARAMETER; |
| 70 | goto back; |
| 71 | } |
| 72 | |
| 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; |
| 77 | goto back; |
| 78 | } |
| 79 | |
| 80 | /* |
| 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 |
| 85 | */ |
| 86 | *rng_algorithm_list_size = sizeof(*rng_algorithm_list); |
| 87 | guidcpy(rng_algorithm_list, &rng_algo_guid); |
| 88 | |
| 89 | back: |
| 90 | return EFI_EXIT(ret); |
| 91 | } |
| 92 | |
Heinrich Schuchardt | 0410be2 | 2020-01-09 20:49:44 +0100 | [diff] [blame] | 93 | /** |
| 94 | * rng_getrng() - get random value |
| 95 | * |
| 96 | * This function implement the GetRng() service of the EFI random number |
| 97 | * generator protocol. See the UEFI spec for details. |
| 98 | * |
| 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 |
| 104 | */ |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 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, |
| 108 | uint8_t *rng_value) |
| 109 | { |
| 110 | int ret; |
| 111 | efi_status_t status = EFI_SUCCESS; |
| 112 | struct udevice *dev; |
| 113 | const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW; |
| 114 | |
| 115 | EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length, |
| 116 | rng_value); |
| 117 | |
| 118 | if (!this || !rng_value || !rng_value_length) { |
| 119 | status = EFI_INVALID_PARAMETER; |
| 120 | goto back; |
| 121 | } |
| 122 | |
| 123 | if (rng_algorithm) { |
| 124 | EFI_PRINT("RNG algorithm %pUl\n", rng_algorithm); |
| 125 | if (guidcmp(rng_algorithm, &rng_raw_guid)) { |
| 126 | status = EFI_UNSUPPORTED; |
| 127 | goto back; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | ret = platform_get_rng_device(&dev); |
| 132 | if (ret != EFI_SUCCESS) { |
| 133 | EFI_PRINT("Rng device not found\n"); |
| 134 | status = EFI_UNSUPPORTED; |
| 135 | goto back; |
| 136 | } |
| 137 | |
| 138 | ret = dm_rng_read(dev, rng_value, rng_value_length); |
| 139 | if (ret < 0) { |
| 140 | EFI_PRINT("Rng device read failed\n"); |
| 141 | status = EFI_DEVICE_ERROR; |
| 142 | goto back; |
| 143 | } |
| 144 | |
| 145 | back: |
| 146 | return EFI_EXIT(status); |
| 147 | } |
| 148 | |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 149 | static const struct efi_rng_protocol efi_rng_protocol = { |
Sughosh Ganu | 7064a5d | 2019-12-29 00:01:05 +0530 | [diff] [blame] | 150 | .get_info = rng_getinfo, |
| 151 | .get_rng = getrng, |
| 152 | }; |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * efi_rng_register() - register EFI_RNG_PROTOCOL |
| 156 | * |
| 157 | * If a RNG device is available, the Random Number Generator Protocol is |
| 158 | * registered. |
| 159 | * |
| 160 | * Return: An error status is only returned if adding the protocol fails. |
| 161 | */ |
| 162 | efi_status_t efi_rng_register(void) |
| 163 | { |
| 164 | efi_status_t ret; |
| 165 | struct udevice *dev; |
| 166 | |
| 167 | ret = platform_get_rng_device(&dev); |
| 168 | if (ret != EFI_SUCCESS) { |
Paulo Alcantara | 77794eb | 2020-11-06 13:52:43 -0300 | [diff] [blame] | 169 | log_warning("Missing RNG device for EFI_RNG_PROTOCOL\n"); |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 170 | return EFI_SUCCESS; |
| 171 | } |
| 172 | ret = efi_add_protocol(efi_root, &efi_guid_rng_protocol, |
| 173 | (void *)&efi_rng_protocol); |
| 174 | if (ret != EFI_SUCCESS) |
Paulo Alcantara | 77794eb | 2020-11-06 13:52:43 -0300 | [diff] [blame] | 175 | log_err("Cannot install EFI_RNG_PROTOCOL\n"); |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 176 | |
| 177 | return ret; |
| 178 | } |