^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) * Originally from efivars.c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This code takes all variables accessible from EFI runtime and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * exports them via sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/ucs2_string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define EFIVARS_VERSION "0.08"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define EFIVARS_DATE "2004-May-17"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) MODULE_DESCRIPTION("sysfs interface to EFI Variables");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) MODULE_VERSION(EFIVARS_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static LIST_HEAD(efivar_sysfs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct kset *efivars_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct bin_attribute *efivars_new_var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static struct bin_attribute *efivars_del_var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct compat_efi_variable {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) efi_guid_t VendorGuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __u32 DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __u8 Data[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __u32 Status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __u32 Attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct efivar_attribute {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct attribute attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ssize_t (*show) (struct efivar_entry *entry, char *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define EFIVAR_ATTR(_name, _mode, _show, _store) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct efivar_attribute efivar_attr_##_name = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .attr = {.name = __stringify(_name), .mode = _mode}, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .show = _show, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .store = _store, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Prototype for sysfs creation function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) efivar_create_sysfs_entry(struct efivar_entry *new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) efivar_guid_read(struct efivar_entry *entry, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct efi_variable *var = &entry->var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!entry || !buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) efi_guid_to_str(&var->VendorGuid, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) str += strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) str += sprintf(str, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return str - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) efivar_attr_read(struct efivar_entry *entry, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct efi_variable *var = &entry->var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned long size = sizeof(var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!entry || !buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) var->DataSize = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) str += sprintf(str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (var->Attributes &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) str += sprintf(str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return str - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) efivar_size_read(struct efivar_entry *entry, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct efi_variable *var = &entry->var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) unsigned long size = sizeof(var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!entry || !buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) var->DataSize = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) str += sprintf(str, "0x%lx\n", var->DataSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return str - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) efivar_data_read(struct efivar_entry *entry, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct efi_variable *var = &entry->var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned long size = sizeof(var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!entry || !buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) var->DataSize = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) memcpy(buf, var->Data, var->DataSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return var->DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long size, u32 attributes, u8 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * If only updating the variable data, then the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * and guid should remain the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (memcmp(name, var->VariableName, sizeof(var->VariableName)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) efi_guidcmp(vendor, var->VendorGuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if ((size <= 0) || (attributes == 0)){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) efivar_validate(vendor, name, data, size) == false) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) printk(KERN_ERR "efivars: Malformed variable content\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) memcpy(dst->Data, src->Data, sizeof(src->Data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dst->VendorGuid = src->VendorGuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) dst->DataSize = src->DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dst->Attributes = src->Attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * We allow each variable to be edited via rewriting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * entire efi variable structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct efi_variable *new_var, *var = &entry->var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) efi_char16_t *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) efi_guid_t vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) u32 attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!entry || !buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (in_compat_syscall()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct compat_efi_variable *compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (count != sizeof(*compat))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) compat = (struct compat_efi_variable *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) attributes = compat->Attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) vendor = compat->VendorGuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) name = compat->VariableName;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) size = compat->DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) data = compat->Data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) err = sanity_check(var, name, vendor, size, attributes, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) copy_out_compat(&entry->var, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (count != sizeof(struct efi_variable))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) new_var = (struct efi_variable *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) attributes = new_var->Attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) vendor = new_var->VendorGuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) name = new_var->VariableName;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) size = new_var->DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) data = new_var->Data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) err = sanity_check(var, name, vendor, size, attributes, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) memcpy(&entry->var, new_var, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) err = efivar_entry_set(entry, attributes, size, data, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) efivar_show_raw(struct efivar_entry *entry, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct efi_variable *var = &entry->var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct compat_efi_variable *compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) unsigned long datasize = sizeof(var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (!entry || !buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) var->DataSize = datasize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (in_compat_syscall()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) compat = (struct compat_efi_variable *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) size = sizeof(*compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) memcpy(compat->VariableName, var->VariableName,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) EFI_VAR_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) memcpy(compat->Data, var->Data, sizeof(compat->Data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) compat->VendorGuid = var->VendorGuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) compat->DataSize = var->DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) compat->Attributes = var->Attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) size = sizeof(*var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) memcpy(buf, var, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Generic read/write functions that call the specific functions of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * the attributes...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct efivar_entry *var = to_efivar_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (efivar_attr->show) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ret = efivar_attr->show(var, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct efivar_entry *var = to_efivar_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct efivar_attribute *efivar_attr = to_efivar_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (efivar_attr->store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) ret = efivar_attr->store(var, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static const struct sysfs_ops efivar_attr_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) .show = efivar_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) .store = efivar_attr_store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static void efivar_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct efivar_entry *var = to_efivar_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) kfree(var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static struct attribute *def_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) &efivar_attr_guid.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) &efivar_attr_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) &efivar_attr_attributes.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) &efivar_attr_data.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) &efivar_attr_raw_var.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static struct kobj_type efivar_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) .release = efivar_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) .sysfs_ops = &efivar_attr_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) .default_attrs = def_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) char *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct compat_efi_variable *compat = (struct compat_efi_variable *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct efi_variable *new_var = (struct efi_variable *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct efivar_entry *new_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) bool need_compat = in_compat_syscall();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) efi_char16_t *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) u32 attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (need_compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (count != sizeof(*compat))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) attributes = compat->Attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) name = compat->VariableName;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) size = compat->DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) data = compat->Data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (count != sizeof(*new_var))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) attributes = new_var->Attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) name = new_var->VariableName;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) size = new_var->DataSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) data = new_var->Data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if ((attributes & ~EFI_VARIABLE_MASK) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) efivar_validate(new_var->VendorGuid, name, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) size) == false) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) printk(KERN_ERR "efivars: Malformed variable content\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (!new_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (need_compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) copy_out_compat(&new_entry->var, compat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) memcpy(&new_entry->var, new_var, sizeof(*new_var));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) err = efivar_entry_set(new_entry, attributes, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) data, &efivar_sysfs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (err == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (efivar_create_sysfs_entry(new_entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) printk(KERN_WARNING "efivars: failed to create sysfs entry.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) kfree(new_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) kfree(new_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) char *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct efi_variable *del_var = (struct efi_variable *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct compat_efi_variable *compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) struct efivar_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) efi_char16_t *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) efi_guid_t vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (in_compat_syscall()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (count != sizeof(*compat))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) compat = (struct compat_efi_variable *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) name = compat->VariableName;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) vendor = compat->VendorGuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (count != sizeof(*del_var))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) name = del_var->VariableName;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) vendor = del_var->VendorGuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (efivar_entry_iter_begin())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) else if (__efivar_entry_delete(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) efivar_entry_iter_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (!entry->scanning) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) efivar_entry_iter_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) efivar_unregister(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) efivar_entry_iter_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* It's dead Jim.... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * efivar_create_sysfs_entry - create a new entry in sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * @new_var: efivar entry to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * Returns 0 on success, negative error code on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) efivar_create_sysfs_entry(struct efivar_entry *new_var)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int short_name_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) char *short_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) unsigned long utf8_name_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) efi_char16_t *variable_name = new_var->var.VariableName;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * Length of the variable bytes in UTF8, plus the '-' separator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * plus the GUID, plus trailing NUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) utf8_name_size = ucs2_utf8size(variable_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) short_name = kmalloc(short_name_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (!short_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) ucs2_as_utf8(short_name, variable_name, short_name_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /* This is ugly, but necessary to separate one vendor's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) private variables from another's. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) short_name[utf8_name_size] = '-';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) efi_guid_to_str(&new_var->var.VendorGuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) short_name + utf8_name_size + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) new_var->kobj.kset = efivars_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) NULL, "%s", short_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) kfree(short_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) kobject_put(&new_var->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) kobject_uevent(&new_var->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (efivar_entry_add(new_var, &efivar_sysfs_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) efivar_unregister(new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) create_efivars_bin_attributes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) struct bin_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /* new_var */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) attr = kzalloc(sizeof(*attr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (!attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) attr->attr.name = "new_var";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) attr->attr.mode = 0200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) attr->write = efivar_create;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) efivars_new_var = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /* del_var */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) attr = kzalloc(sizeof(*attr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (!attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) attr->attr.name = "del_var";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) attr->attr.mode = 0200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) attr->write = efivar_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) efivars_del_var = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) sysfs_bin_attr_init(efivars_new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) sysfs_bin_attr_init(efivars_del_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /* Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) printk(KERN_ERR "efivars: unable to create new_var sysfs file"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) " due to error %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) printk(KERN_ERR "efivars: unable to create del_var sysfs file"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) " due to error %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) kfree(efivars_del_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) efivars_del_var = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) kfree(efivars_new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) efivars_new_var = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) unsigned long name_size, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct efivar_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) entry = kzalloc(sizeof(*entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) memcpy(entry->var.VariableName, name, name_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) efivar_create_sysfs_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) int err = efivar_entry_remove(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) efivar_unregister(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) static void efivars_sysfs_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /* Remove all entries and destroy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) pr_err("efivars: Failed to destroy sysfs entries\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (efivars_new_var)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (efivars_del_var)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) kfree(efivars_new_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) kfree(efivars_del_var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) kset_unregister(efivars_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) static int efivars_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) struct kobject *parent_kobj = efivars_kobject();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) /* No efivars has been registered yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (!parent_kobj || !efivar_supports_writes())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) EFIVARS_DATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) efivars_kset = kset_create_and_add("vars", NULL, parent_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (!efivars_kset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) printk(KERN_ERR "efivars: Subsystem registration failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) error = create_efivars_bin_attributes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) efivars_sysfs_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) module_init(efivars_sysfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) module_exit(efivars_sysfs_exit);