1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
4 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
7 #include <linux/ctype.h>
10 #include <linux/fs_context.h>
11 #include <linux/module.h>
12 #include <linux/pagemap.h>
13 #include <linux/ucs2_string.h>
14 #include <linux/slab.h>
15 #include <linux/magic.h>
16 #include <linux/statfs.h>
17 #include <linux/printk.h>
21 LIST_HEAD(efivarfs_list);
23 static void efivarfs_evict_inode(struct inode *inode)
28 static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
30 const u32 attr = EFI_VARIABLE_NON_VOLATILE |
31 EFI_VARIABLE_BOOTSERVICE_ACCESS |
32 EFI_VARIABLE_RUNTIME_ACCESS;
33 u64 storage_space, remaining_space, max_variable_size;
36 /* Some UEFI firmware does not implement QueryVariableInfo() */
37 storage_space = remaining_space = 0;
38 if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
39 status = efivar_query_variable_info(attr, &storage_space,
42 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
43 pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
48 * This is not a normal filesystem, so no point in pretending it has a block
49 * size; we declare f_bsize to 1, so that we can then report the exact value
50 * sent by EFI QueryVariableInfo in f_blocks and f_bfree
53 buf->f_namelen = NAME_MAX;
54 buf->f_blocks = storage_space;
55 buf->f_bfree = remaining_space;
56 buf->f_type = dentry->d_sb->s_magic;
59 * In f_bavail we declare the free space that the kernel will allow writing
60 * when the storage_paranoia x86 quirk is active. To use more, users
61 * should boot the kernel with efi_no_storage_paranoia.
63 if (remaining_space > efivar_reserved_space())
64 buf->f_bavail = remaining_space - efivar_reserved_space();
70 static const struct super_operations efivarfs_ops = {
71 .statfs = efivarfs_statfs,
72 .drop_inode = generic_delete_inode,
73 .evict_inode = efivarfs_evict_inode,
77 * Compare two efivarfs file names.
79 * An efivarfs filename is composed of two parts,
81 * 1. A case-sensitive variable name
82 * 2. A case-insensitive GUID
84 * So we need to perform a case-sensitive match on part 1 and a
85 * case-insensitive match on part 2.
87 static int efivarfs_d_compare(const struct dentry *dentry,
88 unsigned int len, const char *str,
89 const struct qstr *name)
91 int guid = len - EFI_VARIABLE_GUID_LEN;
96 /* Case-sensitive compare for the variable name */
97 if (memcmp(str, name->name, guid))
100 /* Case-insensitive compare for the GUID */
101 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
104 static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
106 unsigned long hash = init_name_hash(dentry);
107 const unsigned char *s = qstr->name;
108 unsigned int len = qstr->len;
110 if (!efivarfs_valid_name(s, len))
113 while (len-- > EFI_VARIABLE_GUID_LEN)
114 hash = partial_name_hash(*s++, hash);
116 /* GUID is case-insensitive. */
118 hash = partial_name_hash(tolower(*s++), hash);
120 qstr->hash = end_name_hash(hash);
124 static const struct dentry_operations efivarfs_d_ops = {
125 .d_compare = efivarfs_d_compare,
126 .d_hash = efivarfs_d_hash,
127 .d_delete = always_delete_dentry,
130 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
137 q.len = strlen(name);
139 err = efivarfs_d_hash(parent, &q);
143 d = d_alloc(parent, &q);
147 return ERR_PTR(-ENOMEM);
150 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
151 unsigned long name_size, void *data)
153 struct super_block *sb = (struct super_block *)data;
154 struct efivar_entry *entry;
155 struct inode *inode = NULL;
156 struct dentry *dentry, *root = sb->s_root;
157 unsigned long size = 0;
161 bool is_removable = false;
163 if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
166 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
170 memcpy(entry->var.VariableName, name16, name_size);
171 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
173 len = ucs2_utf8size(entry->var.VariableName);
175 /* name, plus '-', plus GUID, plus NUL*/
176 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
180 ucs2_as_utf8(name, entry->var.VariableName, len);
182 if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
187 efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
189 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
191 /* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
192 strreplace(name, '/', '!');
194 inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
199 dentry = efivarfs_alloc_dentry(root, name);
200 if (IS_ERR(dentry)) {
201 err = PTR_ERR(dentry);
205 __efivar_entry_get(entry, NULL, &size, NULL);
206 __efivar_entry_add(entry, &efivarfs_list);
208 /* copied by the above to local storage in the dentry. */
212 inode->i_private = entry;
213 i_size_write(inode, size + sizeof(entry->var.Attributes));
215 d_add(dentry, inode);
228 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
230 efivar_entry_remove(entry);
235 static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
237 struct inode *inode = NULL;
241 if (!efivar_is_available())
244 sb->s_maxbytes = MAX_LFS_FILESIZE;
245 sb->s_blocksize = PAGE_SIZE;
246 sb->s_blocksize_bits = PAGE_SHIFT;
247 sb->s_magic = EFIVARFS_MAGIC;
248 sb->s_op = &efivarfs_ops;
249 sb->s_d_op = &efivarfs_d_ops;
252 if (!efivar_supports_writes())
253 sb->s_flags |= SB_RDONLY;
255 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
258 inode->i_op = &efivarfs_dir_inode_operations;
260 root = d_make_root(inode);
265 INIT_LIST_HEAD(&efivarfs_list);
267 err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
269 efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
274 static int efivarfs_get_tree(struct fs_context *fc)
276 return get_tree_single(fc, efivarfs_fill_super);
279 static int efivarfs_reconfigure(struct fs_context *fc)
281 if (!efivar_supports_writes() && !(fc->sb_flags & SB_RDONLY)) {
282 pr_err("Firmware does not support SetVariableRT. Can not remount with rw\n");
289 static const struct fs_context_operations efivarfs_context_ops = {
290 .get_tree = efivarfs_get_tree,
291 .reconfigure = efivarfs_reconfigure,
294 static int efivarfs_init_fs_context(struct fs_context *fc)
296 fc->ops = &efivarfs_context_ops;
300 static void efivarfs_kill_sb(struct super_block *sb)
302 struct efivarfs_fs_info *sfi = sb->s_fs_info;
304 kill_litter_super(sb);
306 if (!efivar_is_available())
309 /* Remove all entries and destroy */
310 efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
314 static struct file_system_type efivarfs_type = {
315 .owner = THIS_MODULE,
317 .init_fs_context = efivarfs_init_fs_context,
318 .kill_sb = efivarfs_kill_sb,
321 static __init int efivarfs_init(void)
323 return register_filesystem(&efivarfs_type);
326 static __exit void efivarfs_exit(void)
328 unregister_filesystem(&efivarfs_type);
331 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
332 MODULE_DESCRIPTION("EFI Variable Filesystem");
333 MODULE_LICENSE("GPL");
334 MODULE_ALIAS_FS("efivarfs");
336 module_init(efivarfs_init);
337 module_exit(efivarfs_exit);