summaryrefslogtreecommitdiffstats
path: root/drivers/hw_random/efi-rng.c
blob: 61cb01caf6479af1f5caaeef43ae6cc085238ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-License-Identifier: GPL-2.0
#include <common.h>
#include <driver.h>
#include <init.h>
#include <linux/hw_random.h>
#include <efi.h>
#include <efi/efi-device.h>

struct efi_rng_priv {
	struct efi_rng_protocol *protocol;
	struct hwrng hwrng;
};

static inline struct efi_rng_priv *to_efi_rng(struct hwrng *hwrng)
{
	return container_of(hwrng, struct efi_rng_priv, hwrng);
}

static int efi_rng_read(struct hwrng *hwrng, void *data, size_t len, bool wait)
{
	struct efi_rng_protocol *protocol = to_efi_rng(hwrng)->protocol;
	efi_status_t efiret;

	efiret = protocol->get_rng(protocol, NULL, len, data);

	return -efi_errno(efiret) ?: len;
}

static int efi_rng_probe(struct efi_device *efidev)
{
	struct efi_rng_priv *priv;

	priv = xzalloc(sizeof(*priv));

	BS->handle_protocol(efidev->handle, &efi_rng_protocol_guid,
			(void **)&priv->protocol);
	if (!priv->protocol)
		return -ENODEV;

	priv->hwrng.name = dev_name(&efidev->dev);
	priv->hwrng.read = efi_rng_read;

	return hwrng_register(&efidev->dev, &priv->hwrng);
}

static struct efi_driver efi_rng_driver = {
        .driver = {
		.name  = "efi-rng",
	},
        .probe = efi_rng_probe,
	.guid = EFI_RNG_PROTOCOL_GUID,
};
device_efi_driver(efi_rng_driver);