Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Support for extracting embedded firmware for peripherals from EFI code,
4 *
5 * Copyright (c) 2018 Hans de Goede <hdegoede@redhat.com>
6 */
7
8#include <linux/dmi.h>
9#include <linux/efi.h>
10#include <linux/efi_embedded_fw.h>
11#include <linux/io.h>
12#include <linux/slab.h>
13#include <linux/types.h>
14#include <linux/vmalloc.h>
15#include <crypto/sha.h>
16
17/* Exported for use by lib/test_firmware.c only */
18LIST_HEAD(efi_embedded_fw_list);
19EXPORT_SYMBOL_GPL(efi_embedded_fw_list);
20
21static bool checked_for_fw;
22
23static const struct dmi_system_id * const embedded_fw_table[] = {
24#ifdef CONFIG_TOUCHSCREEN_DMI
25 touchscreen_dmi_table,
26#endif
27 NULL
28};
29
30/*
31 * Note the efi_check_for_embedded_firmwares() code currently makes the
32 * following 2 assumptions. This may needs to be revisited if embedded firmware
33 * is found where this is not true:
34 * 1) The firmware is only found in EFI_BOOT_SERVICES_CODE memory segments
35 * 2) The firmware always starts at an offset which is a multiple of 8 bytes
36 */
37static int __init efi_check_md_for_embedded_firmware(
38 efi_memory_desc_t *md, const struct efi_embedded_fw_desc *desc)
39{
40 struct sha256_state sctx;
41 struct efi_embedded_fw *fw;
42 u8 sha256[32];
43 u64 i, size;
44 u8 *map;
45
46 size = md->num_pages << EFI_PAGE_SHIFT;
47 map = memremap(md->phys_addr, size, MEMREMAP_WB);
48 if (!map) {
49 pr_err("Error mapping EFI mem at %#llx\n", md->phys_addr);
50 return -ENOMEM;
51 }
52
53 for (i = 0; (i + desc->length) <= size; i += 8) {
54 if (memcmp(map + i, desc->prefix, EFI_EMBEDDED_FW_PREFIX_LEN))
55 continue;
56
57 sha256_init(&sctx);
58 sha256_update(&sctx, map + i, desc->length);
59 sha256_final(&sctx, sha256);
60 if (memcmp(sha256, desc->sha256, 32) == 0)
61 break;
62 }
63 if ((i + desc->length) > size) {
64 memunmap(map);
65 return -ENOENT;
66 }
67
68 pr_info("Found EFI embedded fw '%s'\n", desc->name);
69
70 fw = kmalloc(sizeof(*fw), GFP_KERNEL);
71 if (!fw) {
72 memunmap(map);
73 return -ENOMEM;
74 }
75
76 fw->data = kmemdup(map + i, desc->length, GFP_KERNEL);
77 memunmap(map);
78 if (!fw->data) {
79 kfree(fw);
80 return -ENOMEM;
81 }
82
83 fw->name = desc->name;
84 fw->length = desc->length;
85 list_add(&fw->list, &efi_embedded_fw_list);
86
87 return 0;
88}
89
90void __init efi_check_for_embedded_firmwares(void)
91{
92 const struct efi_embedded_fw_desc *fw_desc;
93 const struct dmi_system_id *dmi_id;
94 efi_memory_desc_t *md;
95 int i, r;
96
97 for (i = 0; embedded_fw_table[i]; i++) {
98 dmi_id = dmi_first_match(embedded_fw_table[i]);
99 if (!dmi_id)
100 continue;
101
102 fw_desc = dmi_id->driver_data;
103
104 /*
105 * In some drivers the struct driver_data contains may contain
106 * other driver specific data after the fw_desc struct; and
107 * the fw_desc struct itself may be empty, skip these.
108 */
109 if (!fw_desc->name)
110 continue;
111
112 for_each_efi_memory_desc(md) {
113 if (md->type != EFI_BOOT_SERVICES_CODE)
114 continue;
115
116 r = efi_check_md_for_embedded_firmware(md, fw_desc);
117 if (r == 0)
118 break;
119 }
120 }
121
122 checked_for_fw = true;
123}
124
125int efi_get_embedded_fw(const char *name, const u8 **data, size_t *size)
126{
127 struct efi_embedded_fw *iter, *fw = NULL;
128
129 if (!checked_for_fw) {
130 pr_warn("Warning %s called while we did not check for embedded fw\n",
131 __func__);
132 return -ENOENT;
133 }
134
135 list_for_each_entry(iter, &efi_embedded_fw_list, list) {
136 if (strcmp(name, iter->name) == 0) {
137 fw = iter;
138 break;
139 }
140 }
141
142 if (!fw)
143 return -ENOENT;
144
145 *data = fw->data;
146 *size = fw->length;
147
148 return 0;
149}
150EXPORT_SYMBOL_GPL(efi_get_embedded_fw);