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