1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/efi_embedded_fw.h>
4 #include <linux/property.h>
5 #include <linux/security.h>
6 #include <linux/vmalloc.h>
7 
8 #include "fallback.h"
9 #include "firmware.h"
10 
11 int firmware_fallback_platform(struct fw_priv *fw_priv, u32 opt_flags)
12 {
13 	const u8 *data;
14 	size_t size;
15 	int rc;
16 
17 	if (!(opt_flags & FW_OPT_FALLBACK_PLATFORM))
18 		return -ENOENT;
19 
20 	rc = security_kernel_load_data(LOADING_FIRMWARE_EFI_EMBEDDED);
21 	if (rc)
22 		return rc;
23 
24 	rc = efi_get_embedded_fw(fw_priv->fw_name, &data, &size);
25 	if (rc)
26 		return rc; /* rc == -ENOENT when the fw was not found */
27 
28 	if (fw_priv->data && size > fw_priv->allocated_size)
29 		return -ENOMEM;
30 	if (!fw_priv->data)
31 		fw_priv->data = vmalloc(size);
32 	if (!fw_priv->data)
33 		return -ENOMEM;
34 
35 	memcpy(fw_priv->data, data, size);
36 	fw_priv->size = size;
37 	fw_state_done(fw_priv);
38 	return 0;
39 }
40