xref: /linux/fs/efivarfs/super.c (revision afca12e35e711ae8f97e835a3704cc305592eac9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.
4  * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
5  */
6 
7 #include <linux/ctype.h>
8 #include <linux/efi.h>
9 #include <linux/fs.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 
18 #include "internal.h"
19 
20 LIST_HEAD(efivarfs_list);
21 
22 static void efivarfs_evict_inode(struct inode *inode)
23 {
24 	clear_inode(inode);
25 }
26 
27 static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
28 {
29 	const u32 attr = EFI_VARIABLE_NON_VOLATILE |
30 			 EFI_VARIABLE_BOOTSERVICE_ACCESS |
31 			 EFI_VARIABLE_RUNTIME_ACCESS;
32 	u64 storage_space, remaining_space, max_variable_size;
33 	efi_status_t status;
34 
35 	/* Some UEFI firmware does not implement QueryVariableInfo() */
36 	storage_space = remaining_space = 0;
37 	if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
38 		status = efivar_query_variable_info(attr, &storage_space,
39 						    &remaining_space,
40 						    &max_variable_size);
41 		if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
42 			pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
43 					    status);
44 	}
45 
46 	/*
47 	 * This is not a normal filesystem, so no point in pretending it has a block
48 	 * size; we declare f_bsize to 1, so that we can then report the exact value
49 	 * sent by EFI QueryVariableInfo in f_blocks and f_bfree
50 	 */
51 	buf->f_bsize	= 1;
52 	buf->f_namelen	= NAME_MAX;
53 	buf->f_blocks	= storage_space;
54 	buf->f_bfree	= remaining_space;
55 	buf->f_type	= dentry->d_sb->s_magic;
56 
57 	/*
58 	 * In f_bavail we declare the free space that the kernel will allow writing
59 	 * when the storage_paranoia x86 quirk is active. To use more, users
60 	 * should boot the kernel with efi_no_storage_paranoia.
61 	 */
62 	if (remaining_space > efivar_reserved_space())
63 		buf->f_bavail = remaining_space - efivar_reserved_space();
64 	else
65 		buf->f_bavail = 0;
66 
67 	return 0;
68 }
69 static const struct super_operations efivarfs_ops = {
70 	.statfs = efivarfs_statfs,
71 	.drop_inode = generic_delete_inode,
72 	.evict_inode = efivarfs_evict_inode,
73 };
74 
75 /*
76  * Compare two efivarfs file names.
77  *
78  * An efivarfs filename is composed of two parts,
79  *
80  *	1. A case-sensitive variable name
81  *	2. A case-insensitive GUID
82  *
83  * So we need to perform a case-sensitive match on part 1 and a
84  * case-insensitive match on part 2.
85  */
86 static int efivarfs_d_compare(const struct dentry *dentry,
87 			      unsigned int len, const char *str,
88 			      const struct qstr *name)
89 {
90 	int guid = len - EFI_VARIABLE_GUID_LEN;
91 
92 	if (name->len != len)
93 		return 1;
94 
95 	/* Case-sensitive compare for the variable name */
96 	if (memcmp(str, name->name, guid))
97 		return 1;
98 
99 	/* Case-insensitive compare for the GUID */
100 	return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
101 }
102 
103 static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
104 {
105 	unsigned long hash = init_name_hash(dentry);
106 	const unsigned char *s = qstr->name;
107 	unsigned int len = qstr->len;
108 
109 	if (!efivarfs_valid_name(s, len))
110 		return -EINVAL;
111 
112 	while (len-- > EFI_VARIABLE_GUID_LEN)
113 		hash = partial_name_hash(*s++, hash);
114 
115 	/* GUID is case-insensitive. */
116 	while (len--)
117 		hash = partial_name_hash(tolower(*s++), hash);
118 
119 	qstr->hash = end_name_hash(hash);
120 	return 0;
121 }
122 
123 static const struct dentry_operations efivarfs_d_ops = {
124 	.d_compare = efivarfs_d_compare,
125 	.d_hash = efivarfs_d_hash,
126 	.d_delete = always_delete_dentry,
127 };
128 
129 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
130 {
131 	struct dentry *d;
132 	struct qstr q;
133 	int err;
134 
135 	q.name = name;
136 	q.len = strlen(name);
137 
138 	err = efivarfs_d_hash(parent, &q);
139 	if (err)
140 		return ERR_PTR(err);
141 
142 	d = d_alloc(parent, &q);
143 	if (d)
144 		return d;
145 
146 	return ERR_PTR(-ENOMEM);
147 }
148 
149 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
150 			     unsigned long name_size, void *data)
151 {
152 	struct super_block *sb = (struct super_block *)data;
153 	struct efivar_entry *entry;
154 	struct inode *inode = NULL;
155 	struct dentry *dentry, *root = sb->s_root;
156 	unsigned long size = 0;
157 	char *name;
158 	int len;
159 	int err = -ENOMEM;
160 	bool is_removable = false;
161 
162 	if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
163 		return 0;
164 
165 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
166 	if (!entry)
167 		return err;
168 
169 	memcpy(entry->var.VariableName, name16, name_size);
170 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
171 
172 	len = ucs2_utf8size(entry->var.VariableName);
173 
174 	/* name, plus '-', plus GUID, plus NUL*/
175 	name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
176 	if (!name)
177 		goto fail;
178 
179 	ucs2_as_utf8(name, entry->var.VariableName, len);
180 
181 	if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
182 		is_removable = true;
183 
184 	name[len] = '-';
185 
186 	efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
187 
188 	name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
189 
190 	/* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
191 	strreplace(name, '/', '!');
192 
193 	inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
194 				   is_removable);
195 	if (!inode)
196 		goto fail_name;
197 
198 	dentry = efivarfs_alloc_dentry(root, name);
199 	if (IS_ERR(dentry)) {
200 		err = PTR_ERR(dentry);
201 		goto fail_inode;
202 	}
203 
204 	__efivar_entry_get(entry, NULL, &size, NULL);
205 	__efivar_entry_add(entry, &efivarfs_list);
206 
207 	/* copied by the above to local storage in the dentry. */
208 	kfree(name);
209 
210 	inode_lock(inode);
211 	inode->i_private = entry;
212 	i_size_write(inode, size + sizeof(entry->var.Attributes));
213 	inode_unlock(inode);
214 	d_add(dentry, inode);
215 
216 	return 0;
217 
218 fail_inode:
219 	iput(inode);
220 fail_name:
221 	kfree(name);
222 fail:
223 	kfree(entry);
224 	return err;
225 }
226 
227 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
228 {
229 	efivar_entry_remove(entry);
230 	kfree(entry);
231 	return 0;
232 }
233 
234 static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
235 {
236 	struct inode *inode = NULL;
237 	struct dentry *root;
238 	int err;
239 
240 	if (!efivar_is_available())
241 		return -EOPNOTSUPP;
242 
243 	sb->s_maxbytes          = MAX_LFS_FILESIZE;
244 	sb->s_blocksize         = PAGE_SIZE;
245 	sb->s_blocksize_bits    = PAGE_SHIFT;
246 	sb->s_magic             = EFIVARFS_MAGIC;
247 	sb->s_op                = &efivarfs_ops;
248 	sb->s_d_op		= &efivarfs_d_ops;
249 	sb->s_time_gran         = 1;
250 
251 	if (!efivar_supports_writes())
252 		sb->s_flags |= SB_RDONLY;
253 
254 	inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
255 	if (!inode)
256 		return -ENOMEM;
257 	inode->i_op = &efivarfs_dir_inode_operations;
258 
259 	root = d_make_root(inode);
260 	sb->s_root = root;
261 	if (!root)
262 		return -ENOMEM;
263 
264 	INIT_LIST_HEAD(&efivarfs_list);
265 
266 	err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
267 	if (err)
268 		efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
269 
270 	return err;
271 }
272 
273 static int efivarfs_get_tree(struct fs_context *fc)
274 {
275 	return get_tree_single(fc, efivarfs_fill_super);
276 }
277 
278 static const struct fs_context_operations efivarfs_context_ops = {
279 	.get_tree	= efivarfs_get_tree,
280 };
281 
282 static int efivarfs_init_fs_context(struct fs_context *fc)
283 {
284 	fc->ops = &efivarfs_context_ops;
285 	return 0;
286 }
287 
288 static void efivarfs_kill_sb(struct super_block *sb)
289 {
290 	kill_litter_super(sb);
291 
292 	if (!efivar_is_available())
293 		return;
294 
295 	/* Remove all entries and destroy */
296 	efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
297 }
298 
299 static struct file_system_type efivarfs_type = {
300 	.owner   = THIS_MODULE,
301 	.name    = "efivarfs",
302 	.init_fs_context = efivarfs_init_fs_context,
303 	.kill_sb = efivarfs_kill_sb,
304 };
305 
306 static __init int efivarfs_init(void)
307 {
308 	return register_filesystem(&efivarfs_type);
309 }
310 
311 static __exit void efivarfs_exit(void)
312 {
313 	unregister_filesystem(&efivarfs_type);
314 }
315 
316 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
317 MODULE_DESCRIPTION("EFI Variable Filesystem");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS_FS("efivarfs");
320 
321 module_init(efivarfs_init);
322 module_exit(efivarfs_exit);
323