Debian Codegraph @

drivers/platform/x86/intel/wmi/sbl-fw-update.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Slim Bootloader(SBL) firmware update signaling driver
4 *
5 * Slim Bootloader is a small, open-source, non UEFI compliant, boot firmware
6 * optimized for running on certain Intel platforms.
7 *
8 * SBL exposes an ACPI-WMI device via /sys/bus/wmi/devices/<INTEL_WMI_SBL_GUID>.
9 * This driver further adds "firmware_update_request" device attribute.
10 * This attribute normally has a value of 0 and userspace can signal SBL
11 * to update firmware, on next reboot, by writing a value of 1.
12 *
13 * More details of SBL firmware update process is available at:
14 * https://slimbootloader.github.io/security/firmware-update.html
15 */
16
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/sysfs.h>
21 #include <linux/wmi.h>
22
23 #define INTEL_WMI_SBL_GUID "44FADEB1-B204-40F2-8581-394BBDC1B651"
24
25 static int get_fwu_request(struct device *dev, u32 *out)
26 {
27 struct wmi_buffer buffer;
28 __le32 *result;
29 int ret;
30
31 ret = wmidev_query_block(to_wmi_device(dev), 0, &buffer);
32 if (ret < 0)
33 return ret;
34
35 if (buffer.length < sizeof(*result)) {
36 kfree(buffer.data);
37 return -ENODATA;
38 }
39
40 result = buffer.data;
41 *out = le32_to_cpu(*result);
42 kfree(result);
43
44 return 0;
45 }
46
47 static int set_fwu_request(struct device *dev, u32 in)
48 {
49 __le32 value = cpu_to_le32(in);
50 struct wmi_buffer buffer = {
51 .length = sizeof(value),
52 .data = &value,
53 };
54
55 return wmidev_set_block(to_wmi_device(dev), 0, &buffer);
56 }
57
58 static ssize_t firmware_update_request_show(struct device *dev,
59 struct device_attribute *attr,
60 char *buf)
61 {
62 u32 val;
63 int ret;
64
65 ret = get_fwu_request(dev, &val);
66 if (ret)
67 return ret;
68
69 return sprintf(buf, "%d\n", val);
70 }
71
72 static ssize_t firmware_update_request_store(struct device *dev,
73 struct device_attribute *attr,
74 const char *buf, size_t count)
75 {
76 unsigned int val;
77 int ret;
78
79 ret = kstrtouint(buf, 0, &val);
80 if (ret)
81 return ret;
82
83 /* May later be extended to support values other than 0 and 1 */
84 if (val > 1)
85 return -ERANGE;
86
87 ret = set_fwu_request(dev, val);
88 if (ret)
89 return ret;
90
91 return count;
92 }
93 static DEVICE_ATTR_RW(firmware_update_request);
94
95 static struct attribute *firmware_update_attrs[] = {
96 &dev_attr_firmware_update_request.attr,
97 NULL
98 };
99 ATTRIBUTE_GROUPS(firmware_update);
100
101 static int intel_wmi_sbl_fw_update_probe(struct wmi_device *wdev,
102 const void *context)
103 {
104 dev_info(&wdev->dev, "Slim Bootloader signaling driver attached\n");
105 return 0;
106 }
107
108 static void intel_wmi_sbl_fw_update_remove(struct wmi_device *wdev)
109 {
110 dev_info(&wdev->dev, "Slim Bootloader signaling driver removed\n");
111 }
112
113 static const struct wmi_device_id intel_wmi_sbl_id_table[] = {
114 { .guid_string = INTEL_WMI_SBL_GUID },
115 {}
116 };
117 MODULE_DEVICE_TABLE(wmi, intel_wmi_sbl_id_table);
118
119 static struct wmi_driver intel_wmi_sbl_fw_update_driver = {
120 .driver = {
121 .name = "intel-wmi-sbl-fw-update",
122 .dev_groups = firmware_update_groups,
123 },
124 .probe = intel_wmi_sbl_fw_update_probe,
125 .remove = intel_wmi_sbl_fw_update_remove,
126 .id_table = intel_wmi_sbl_id_table,
127 .no_singleton = true,
128 };
129 module_wmi_driver(intel_wmi_sbl_fw_update_driver);
130
131 MODULE_AUTHOR("Jithu Joseph <[email protected]>");
132 MODULE_DESCRIPTION("Slim Bootloader firmware update signaling driver");
133 MODULE_LICENSE("GPL v2");