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/fs_parser.h>
12 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/ucs2_string.h>
15 #include <linux/slab.h>
16 #include <linux/magic.h>
17 #include <linux/statfs.h>
21 LIST_HEAD(efivarfs_list);
23 static void efivarfs_evict_inode(struct inode *inode)
28 static int efivarfs_show_options(struct seq_file *m, struct dentry *root)
30 struct super_block *sb = root->d_sb;
31 struct efivarfs_fs_info *sbi = sb->s_fs_info;
32 struct efivarfs_mount_opts *opts = &sbi->mount_opts;
34 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
35 seq_printf(m, ",uid=%u",
36 from_kuid_munged(&init_user_ns, opts->uid));
37 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
38 seq_printf(m, ",gid=%u",
39 from_kgid_munged(&init_user_ns, opts->gid));
43 static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
45 const u32 attr = EFI_VARIABLE_NON_VOLATILE |
46 EFI_VARIABLE_BOOTSERVICE_ACCESS |
47 EFI_VARIABLE_RUNTIME_ACCESS;
48 u64 storage_space, remaining_space, max_variable_size;
51 /* Some UEFI firmware does not implement QueryVariableInfo() */
52 storage_space = remaining_space = 0;
53 if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
54 status = efivar_query_variable_info(attr, &storage_space,
57 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
58 pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
63 * This is not a normal filesystem, so no point in pretending it has a block
64 * size; we declare f_bsize to 1, so that we can then report the exact value
65 * sent by EFI QueryVariableInfo in f_blocks and f_bfree
68 buf->f_namelen = NAME_MAX;
69 buf->f_blocks = storage_space;
70 buf->f_bfree = remaining_space;
71 buf->f_type = dentry->d_sb->s_magic;
74 * In f_bavail we declare the free space that the kernel will allow writing
75 * when the storage_paranoia x86 quirk is active. To use more, users
76 * should boot the kernel with efi_no_storage_paranoia.
78 if (remaining_space > efivar_reserved_space())
79 buf->f_bavail = remaining_space - efivar_reserved_space();
85 static const struct super_operations efivarfs_ops = {
86 .statfs = efivarfs_statfs,
87 .drop_inode = generic_delete_inode,
88 .evict_inode = efivarfs_evict_inode,
89 .show_options = efivarfs_show_options,
93 * Compare two efivarfs file names.
95 * An efivarfs filename is composed of two parts,
97 * 1. A case-sensitive variable name
98 * 2. A case-insensitive GUID
100 * So we need to perform a case-sensitive match on part 1 and a
101 * case-insensitive match on part 2.
103 static int efivarfs_d_compare(const struct dentry *dentry,
104 unsigned int len, const char *str,
105 const struct qstr *name)
107 int guid = len - EFI_VARIABLE_GUID_LEN;
109 if (name->len != len)
112 /* Case-sensitive compare for the variable name */
113 if (memcmp(str, name->name, guid))
116 /* Case-insensitive compare for the GUID */
117 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
120 static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
122 unsigned long hash = init_name_hash(dentry);
123 const unsigned char *s = qstr->name;
124 unsigned int len = qstr->len;
126 if (!efivarfs_valid_name(s, len))
129 while (len-- > EFI_VARIABLE_GUID_LEN)
130 hash = partial_name_hash(*s++, hash);
132 /* GUID is case-insensitive. */
134 hash = partial_name_hash(tolower(*s++), hash);
136 qstr->hash = end_name_hash(hash);
140 static const struct dentry_operations efivarfs_d_ops = {
141 .d_compare = efivarfs_d_compare,
142 .d_hash = efivarfs_d_hash,
143 .d_delete = always_delete_dentry,
146 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
153 q.len = strlen(name);
155 err = efivarfs_d_hash(parent, &q);
159 d = d_alloc(parent, &q);
163 return ERR_PTR(-ENOMEM);
166 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
167 unsigned long name_size, void *data)
169 struct super_block *sb = (struct super_block *)data;
170 struct efivar_entry *entry;
171 struct inode *inode = NULL;
172 struct dentry *dentry, *root = sb->s_root;
173 unsigned long size = 0;
177 bool is_removable = false;
179 if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
182 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
186 memcpy(entry->var.VariableName, name16, name_size);
187 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
189 len = ucs2_utf8size(entry->var.VariableName);
191 /* name, plus '-', plus GUID, plus NUL*/
192 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
196 ucs2_as_utf8(name, entry->var.VariableName, len);
198 if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
203 efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
205 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
207 /* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
208 strreplace(name, '/', '!');
210 inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
215 dentry = efivarfs_alloc_dentry(root, name);
216 if (IS_ERR(dentry)) {
217 err = PTR_ERR(dentry);
221 __efivar_entry_get(entry, NULL, &size, NULL);
222 __efivar_entry_add(entry, &efivarfs_list);
224 /* copied by the above to local storage in the dentry. */
228 inode->i_private = entry;
229 i_size_write(inode, size + sizeof(entry->var.Attributes));
231 d_add(dentry, inode);
244 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
246 efivar_entry_remove(entry);
255 static const struct fs_parameter_spec efivarfs_parameters[] = {
256 fsparam_u32("uid", Opt_uid),
257 fsparam_u32("gid", Opt_gid),
261 static int efivarfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
263 struct efivarfs_fs_info *sbi = fc->s_fs_info;
264 struct efivarfs_mount_opts *opts = &sbi->mount_opts;
265 struct fs_parse_result result;
268 opt = fs_parse(fc, efivarfs_parameters, param, &result);
274 opts->uid = make_kuid(current_user_ns(), result.uint_32);
275 if (!uid_valid(opts->uid))
279 opts->gid = make_kgid(current_user_ns(), result.uint_32);
280 if (!gid_valid(opts->gid))
290 static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
292 struct inode *inode = NULL;
296 if (!efivar_is_available())
299 sb->s_maxbytes = MAX_LFS_FILESIZE;
300 sb->s_blocksize = PAGE_SIZE;
301 sb->s_blocksize_bits = PAGE_SHIFT;
302 sb->s_magic = EFIVARFS_MAGIC;
303 sb->s_op = &efivarfs_ops;
304 sb->s_d_op = &efivarfs_d_ops;
307 if (!efivar_supports_writes())
308 sb->s_flags |= SB_RDONLY;
310 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
313 inode->i_op = &efivarfs_dir_inode_operations;
315 root = d_make_root(inode);
320 INIT_LIST_HEAD(&efivarfs_list);
322 err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
324 efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
329 static int efivarfs_get_tree(struct fs_context *fc)
331 return get_tree_single(fc, efivarfs_fill_super);
334 static const struct fs_context_operations efivarfs_context_ops = {
335 .get_tree = efivarfs_get_tree,
336 .parse_param = efivarfs_parse_param,
339 static int efivarfs_init_fs_context(struct fs_context *fc)
341 struct efivarfs_fs_info *sfi;
343 sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
347 sfi->mount_opts.uid = GLOBAL_ROOT_UID;
348 sfi->mount_opts.gid = GLOBAL_ROOT_GID;
351 fc->ops = &efivarfs_context_ops;
355 static void efivarfs_kill_sb(struct super_block *sb)
357 kill_litter_super(sb);
359 if (!efivar_is_available())
362 /* Remove all entries and destroy */
363 efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
366 static struct file_system_type efivarfs_type = {
367 .owner = THIS_MODULE,
369 .init_fs_context = efivarfs_init_fs_context,
370 .kill_sb = efivarfs_kill_sb,
371 .parameters = efivarfs_parameters,
374 static __init int efivarfs_init(void)
376 return register_filesystem(&efivarfs_type);
379 static __exit void efivarfs_exit(void)
381 unregister_filesystem(&efivarfs_type);
384 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
385 MODULE_DESCRIPTION("EFI Variable Filesystem");
386 MODULE_LICENSE("GPL");
387 MODULE_ALIAS_FS("efivarfs");
389 module_init(efivarfs_init);
390 module_exit(efivarfs_exit);