xref: /linux/drivers/firmware/efi/libstub/random.c (revision e4fbf4767440472f9d23b0f25a2b905e1c63b6a8)
1*e4fbf476SArd Biesheuvel /*
2*e4fbf476SArd Biesheuvel  * Copyright (C) 2016 Linaro Ltd;  <ard.biesheuvel@linaro.org>
3*e4fbf476SArd Biesheuvel  *
4*e4fbf476SArd Biesheuvel  * This program is free software; you can redistribute it and/or modify
5*e4fbf476SArd Biesheuvel  * it under the terms of the GNU General Public License version 2 as
6*e4fbf476SArd Biesheuvel  * published by the Free Software Foundation.
7*e4fbf476SArd Biesheuvel  *
8*e4fbf476SArd Biesheuvel  */
9*e4fbf476SArd Biesheuvel 
10*e4fbf476SArd Biesheuvel #include <linux/efi.h>
11*e4fbf476SArd Biesheuvel #include <asm/efi.h>
12*e4fbf476SArd Biesheuvel 
13*e4fbf476SArd Biesheuvel #include "efistub.h"
14*e4fbf476SArd Biesheuvel 
15*e4fbf476SArd Biesheuvel struct efi_rng_protocol {
16*e4fbf476SArd Biesheuvel 	efi_status_t (*get_info)(struct efi_rng_protocol *,
17*e4fbf476SArd Biesheuvel 				 unsigned long *, efi_guid_t *);
18*e4fbf476SArd Biesheuvel 	efi_status_t (*get_rng)(struct efi_rng_protocol *,
19*e4fbf476SArd Biesheuvel 				efi_guid_t *, unsigned long, u8 *out);
20*e4fbf476SArd Biesheuvel };
21*e4fbf476SArd Biesheuvel 
22*e4fbf476SArd Biesheuvel efi_status_t efi_get_random_bytes(efi_system_table_t *sys_table_arg,
23*e4fbf476SArd Biesheuvel 				  unsigned long size, u8 *out)
24*e4fbf476SArd Biesheuvel {
25*e4fbf476SArd Biesheuvel 	efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
26*e4fbf476SArd Biesheuvel 	efi_status_t status;
27*e4fbf476SArd Biesheuvel 	struct efi_rng_protocol *rng;
28*e4fbf476SArd Biesheuvel 
29*e4fbf476SArd Biesheuvel 	status = efi_call_early(locate_protocol, &rng_proto, NULL,
30*e4fbf476SArd Biesheuvel 				(void **)&rng);
31*e4fbf476SArd Biesheuvel 	if (status != EFI_SUCCESS)
32*e4fbf476SArd Biesheuvel 		return status;
33*e4fbf476SArd Biesheuvel 
34*e4fbf476SArd Biesheuvel 	return rng->get_rng(rng, NULL, size, out);
35*e4fbf476SArd Biesheuvel }
36