aboutsummaryrefslogtreecommitdiffstats
path: root/fs/efivarfs
diff options
context:
space:
mode:
authorMatt Fleming <matt.fleming@intel.com>2013-02-08 11:27:24 -0500
committerMatt Fleming <matt.fleming@intel.com>2013-04-17 08:25:09 -0400
commitd68772b7c83f4b518be15ae96f4827c8ed02f684 (patch)
treeb3408a750976edf6e0b62bfce3e26d1c9685a907 /fs/efivarfs
parent048517722cde2595a7366d0c3c72b8b1ec142a9c (diff)
efivarfs: Move to fs/efivarfs
Now that efivarfs uses the efivar API, move it out of efivars.c and into fs/efivarfs where it belongs. This move will eventually allow us to enable the efivarfs code without having to also enable CONFIG_EFI_VARS built, and vice versa. Furthermore, things like, mount -t efivarfs none /sys/firmware/efi/efivars will now work if efivarfs is built as a module without requiring the use of MODULE_ALIAS(), which would have been necessary when the efivarfs code was part of efivars.c. Cc: Matthew Garrett <matthew.garrett@nebula.com> Cc: Jeremy Kerr <jk@ozlabs.org> Reviewed-by: Tom Gundersen <teg@jklm.no> Tested-by: Tom Gundersen <teg@jklm.no> Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Diffstat (limited to 'fs/efivarfs')
-rw-r--r--fs/efivarfs/Kconfig12
-rw-r--r--fs/efivarfs/Makefile7
-rw-r--r--fs/efivarfs/file.c111
-rw-r--r--fs/efivarfs/inode.c173
-rw-r--r--fs/efivarfs/internal.h22
-rw-r--r--fs/efivarfs/super.c267
6 files changed, 592 insertions, 0 deletions
diff --git a/fs/efivarfs/Kconfig b/fs/efivarfs/Kconfig
new file mode 100644
index 000000000000..1fb2b7f849f3
--- /dev/null
+++ b/fs/efivarfs/Kconfig
@@ -0,0 +1,12 @@
1config EFIVAR_FS
2 tristate "EFI Variable filesystem"
3 depends on EFI_VARS
4 help
5 efivarfs is a replacement filesystem for the old EFI
6 variable support via sysfs, as it doesn't suffer from the
7 same 1024-byte variable size limit.
8
9 To compile this file system support as a module, choose M
10 here. The module will be called efivarfs.
11
12 If unsure, say N.
diff --git a/fs/efivarfs/Makefile b/fs/efivarfs/Makefile
new file mode 100644
index 000000000000..955d478177d5
--- /dev/null
+++ b/fs/efivarfs/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the efivarfs filesystem
3#
4
5obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
6
7efivarfs-objs := inode.o file.o super.o
diff --git a/fs/efivarfs/file.c b/fs/efivarfs/file.c
new file mode 100644
index 000000000000..aeb0368dace2
--- /dev/null
+++ b/fs/efivarfs/file.c
@@ -0,0 +1,111 @@
1/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/efi.h>
11#include <linux/fs.h>
12
13#include "internal.h"
14
15static int efivarfs_file_open(struct inode *inode, struct file *file)
16{
17 file->private_data = inode->i_private;
18 return 0;
19}
20
21static ssize_t efivarfs_file_write(struct file *file,
22 const char __user *userbuf, size_t count, loff_t *ppos)
23{
24 struct efivar_entry *var = file->private_data;
25 void *data;
26 u32 attributes;
27 struct inode *inode = file->f_mapping->host;
28 unsigned long datasize = count - sizeof(attributes);
29 ssize_t bytes = 0;
30 bool set = false;
31
32 if (count < sizeof(attributes))
33 return -EINVAL;
34
35 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
36 return -EFAULT;
37
38 if (attributes & ~(EFI_VARIABLE_MASK))
39 return -EINVAL;
40
41 data = kmalloc(datasize, GFP_KERNEL);
42 if (!data)
43 return -ENOMEM;
44
45 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
46 bytes = -EFAULT;
47 goto out;
48 }
49
50 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
51 data, &set);
52 if (!set && bytes)
53 goto out;
54
55 if (bytes == -ENOENT) {
56 drop_nlink(inode);
57 d_delete(file->f_dentry);
58 dput(file->f_dentry);
59 } else {
60 mutex_lock(&inode->i_mutex);
61 i_size_write(inode, datasize + sizeof(attributes));
62 mutex_unlock(&inode->i_mutex);
63 }
64
65 bytes = count;
66
67out:
68 kfree(data);
69
70 return bytes;
71}
72
73static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
74 size_t count, loff_t *ppos)
75{
76 struct efivar_entry *var = file->private_data;
77 unsigned long datasize = 0;
78 u32 attributes;
79 void *data;
80 ssize_t size = 0;
81 int err;
82
83 err = efivar_entry_size(var, &datasize);
84 if (err)
85 return err;
86
87 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
88
89 if (!data)
90 return -ENOMEM;
91
92 size = efivar_entry_get(var, &attributes, &datasize,
93 data + sizeof(attributes));
94 if (size)
95 goto out_free;
96
97 memcpy(data, &attributes, sizeof(attributes));
98 size = simple_read_from_buffer(userbuf, count, ppos,
99 data, datasize + sizeof(attributes));
100out_free:
101 kfree(data);
102
103 return size;
104}
105
106const struct file_operations efivarfs_file_operations = {
107 .open = efivarfs_file_open,
108 .read = efivarfs_file_read,
109 .write = efivarfs_file_write,
110 .llseek = no_llseek,
111};
diff --git a/fs/efivarfs/inode.c b/fs/efivarfs/inode.c
new file mode 100644
index 000000000000..640e289d522e
--- /dev/null
+++ b/fs/efivarfs/inode.c
@@ -0,0 +1,173 @@
1/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/efi.h>
11#include <linux/fs.h>
12#include <linux/ctype.h>
13
14#include "internal.h"
15
16struct inode *efivarfs_get_inode(struct super_block *sb,
17 const struct inode *dir, int mode, dev_t dev)
18{
19 struct inode *inode = new_inode(sb);
20
21 if (inode) {
22 inode->i_ino = get_next_ino();
23 inode->i_mode = mode;
24 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
25 switch (mode & S_IFMT) {
26 case S_IFREG:
27 inode->i_fop = &efivarfs_file_operations;
28 break;
29 case S_IFDIR:
30 inode->i_op = &efivarfs_dir_inode_operations;
31 inode->i_fop = &simple_dir_operations;
32 inc_nlink(inode);
33 break;
34 }
35 }
36 return inode;
37}
38
39/*
40 * Return true if 'str' is a valid efivarfs filename of the form,
41 *
42 * VariableName-12345678-1234-1234-1234-1234567891bc
43 */
44bool efivarfs_valid_name(const char *str, int len)
45{
46 static const char dashes[EFI_VARIABLE_GUID_LEN] = {
47 [8] = 1, [13] = 1, [18] = 1, [23] = 1
48 };
49 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
50 int i;
51
52 /*
53 * We need a GUID, plus at least one letter for the variable name,
54 * plus the '-' separator
55 */
56 if (len < EFI_VARIABLE_GUID_LEN + 2)
57 return false;
58
59 /* GUID must be preceded by a '-' */
60 if (*(s - 1) != '-')
61 return false;
62
63 /*
64 * Validate that 's' is of the correct format, e.g.
65 *
66 * 12345678-1234-1234-1234-123456789abc
67 */
68 for (i = 0; i < EFI_VARIABLE_GUID_LEN; i++) {
69 if (dashes[i]) {
70 if (*s++ != '-')
71 return false;
72 } else {
73 if (!isxdigit(*s++))
74 return false;
75 }
76 }
77
78 return true;
79}
80
81static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
82{
83 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
84 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
85 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
86 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
87 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
88 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
89 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
90 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
91 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
92 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
93 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
94 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
95 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
96 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
97 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
98 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
99}
100
101static int efivarfs_create(struct inode *dir, struct dentry *dentry,
102 umode_t mode, bool excl)
103{
104 struct inode *inode;
105 struct efivar_entry *var;
106 int namelen, i = 0, err = 0;
107
108 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
109 return -EINVAL;
110
111 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
112 if (!inode)
113 return -ENOMEM;
114
115 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
116 if (!var) {
117 err = -ENOMEM;
118 goto out;
119 }
120
121 /* length of the variable name itself: remove GUID and separator */
122 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
123
124 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
125 &var->var.VendorGuid);
126
127 for (i = 0; i < namelen; i++)
128 var->var.VariableName[i] = dentry->d_name.name[i];
129
130 var->var.VariableName[i] = '\0';
131
132 inode->i_private = var;
133
134 efivar_entry_add(var, &efivarfs_list);
135 d_instantiate(dentry, inode);
136 dget(dentry);
137out:
138 if (err) {
139 kfree(var);
140 iput(inode);
141 }
142 return err;
143}
144
145static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
146{
147 struct efivar_entry *var = dentry->d_inode->i_private;
148
149 if (efivar_entry_delete(var))
150 return -EINVAL;
151
152 drop_nlink(dentry->d_inode);
153 dput(dentry);
154 return 0;
155};
156
157/*
158 * Handle negative dentry.
159 */
160static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
161 unsigned int flags)
162{
163 if (dentry->d_name.len > NAME_MAX)
164 return ERR_PTR(-ENAMETOOLONG);
165 d_add(dentry, NULL);
166 return NULL;
167}
168
169const struct inode_operations efivarfs_dir_inode_operations = {
170 .lookup = efivarfs_lookup,
171 .unlink = efivarfs_unlink,
172 .create = efivarfs_create,
173};
diff --git a/fs/efivarfs/internal.h b/fs/efivarfs/internal.h
new file mode 100644
index 000000000000..b5ff16addb7c
--- /dev/null
+++ b/fs/efivarfs/internal.h
@@ -0,0 +1,22 @@
1/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#ifndef EFIVAR_FS_INTERNAL_H
10#define EFIVAR_FS_INTERNAL_H
11
12#include <linux/list.h>
13
14extern const struct file_operations efivarfs_file_operations;
15extern const struct inode_operations efivarfs_dir_inode_operations;
16extern bool efivarfs_valid_name(const char *str, int len);
17extern struct inode *efivarfs_get_inode(struct super_block *sb,
18 const struct inode *dir, int mode, dev_t dev);
19
20extern struct list_head efivarfs_list;
21
22#endif /* EFIVAR_FS_INTERNAL_H */
diff --git a/fs/efivarfs/super.c b/fs/efivarfs/super.c
new file mode 100644
index 000000000000..34c48f1fcdbc
--- /dev/null
+++ b/fs/efivarfs/super.c
@@ -0,0 +1,267 @@
1/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/ctype.h>
11#include <linux/efi.h>
12#include <linux/fs.h>
13#include <linux/module.h>
14#include <linux/pagemap.h>
15
16#include "internal.h"
17
18LIST_HEAD(efivarfs_list);
19
20static void efivarfs_evict_inode(struct inode *inode)
21{
22 clear_inode(inode);
23}
24
25static const struct super_operations efivarfs_ops = {
26 .statfs = simple_statfs,
27 .drop_inode = generic_delete_inode,
28 .evict_inode = efivarfs_evict_inode,
29 .show_options = generic_show_options,
30};
31
32static struct super_block *efivarfs_sb;
33
34/*
35 * Compare two efivarfs file names.
36 *
37 * An efivarfs filename is composed of two parts,
38 *
39 * 1. A case-sensitive variable name
40 * 2. A case-insensitive GUID
41 *
42 * So we need to perform a case-sensitive match on part 1 and a
43 * case-insensitive match on part 2.
44 */
45static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
46 const struct dentry *dentry, const struct inode *inode,
47 unsigned int len, const char *str,
48 const struct qstr *name)
49{
50 int guid = len - EFI_VARIABLE_GUID_LEN;
51
52 if (name->len != len)
53 return 1;
54
55 /* Case-sensitive compare for the variable name */
56 if (memcmp(str, name->name, guid))
57 return 1;
58
59 /* Case-insensitive compare for the GUID */
60 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
61}
62
63static int efivarfs_d_hash(const struct dentry *dentry,
64 const struct inode *inode, struct qstr *qstr)
65{
66 unsigned long hash = init_name_hash();
67 const unsigned char *s = qstr->name;
68 unsigned int len = qstr->len;
69
70 if (!efivarfs_valid_name(s, len))
71 return -EINVAL;
72
73 while (len-- > EFI_VARIABLE_GUID_LEN)
74 hash = partial_name_hash(*s++, hash);
75
76 /* GUID is case-insensitive. */
77 while (len--)
78 hash = partial_name_hash(tolower(*s++), hash);
79
80 qstr->hash = end_name_hash(hash);
81 return 0;
82}
83
84/*
85 * Retaining negative dentries for an in-memory filesystem just wastes
86 * memory and lookup time: arrange for them to be deleted immediately.
87 */
88static int efivarfs_delete_dentry(const struct dentry *dentry)
89{
90 return 1;
91}
92
93static struct dentry_operations efivarfs_d_ops = {
94 .d_compare = efivarfs_d_compare,
95 .d_hash = efivarfs_d_hash,
96 .d_delete = efivarfs_delete_dentry,
97};
98
99static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
100{
101 struct dentry *d;
102 struct qstr q;
103 int err;
104
105 q.name = name;
106 q.len = strlen(name);
107
108 err = efivarfs_d_hash(NULL, NULL, &q);
109 if (err)
110 return ERR_PTR(err);
111
112 d = d_alloc(parent, &q);
113 if (d)
114 return d;
115
116 return ERR_PTR(-ENOMEM);
117}
118
119static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
120 unsigned long name_size, void *data)
121{
122 struct super_block *sb = (struct super_block *)data;
123 struct efivar_entry *entry;
124 struct inode *inode = NULL;
125 struct dentry *dentry, *root = sb->s_root;
126 unsigned long size = 0;
127 char *name;
128 int len, i;
129 int err = -ENOMEM;
130
131 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
132 if (!entry)
133 return err;
134
135 memcpy(entry->var.VariableName, name16, name_size);
136 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
137
138 len = utf16_strlen(entry->var.VariableName);
139
140 /* name, plus '-', plus GUID, plus NUL*/
141 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
142 if (!name)
143 goto fail;
144
145 for (i = 0; i < len; i++)
146 name[i] = entry->var.VariableName[i] & 0xFF;
147
148 name[len] = '-';
149
150 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
151
152 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
153
154 inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
155 if (!inode)
156 goto fail_name;
157
158 dentry = efivarfs_alloc_dentry(root, name);
159 if (IS_ERR(dentry)) {
160 err = PTR_ERR(dentry);
161 goto fail_inode;
162 }
163
164 /* copied by the above to local storage in the dentry. */
165 kfree(name);
166
167 efivar_entry_size(entry, &size);
168 efivar_entry_add(entry, &efivarfs_list);
169
170 mutex_lock(&inode->i_mutex);
171 inode->i_private = entry;
172 i_size_write(inode, size + sizeof(entry->var.Attributes));
173 mutex_unlock(&inode->i_mutex);
174 d_add(dentry, inode);
175
176 return 0;
177
178fail_inode:
179 iput(inode);
180fail_name:
181 kfree(name);
182fail:
183 kfree(entry);
184 return err;
185}
186
187static int efivarfs_destroy(struct efivar_entry *entry, void *data)
188{
189 efivar_entry_remove(entry);
190 kfree(entry);
191 return 0;
192}
193
194static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
195{
196 struct inode *inode = NULL;
197 struct dentry *root;
198 int err;
199
200 efivarfs_sb = sb;
201
202 sb->s_maxbytes = MAX_LFS_FILESIZE;
203 sb->s_blocksize = PAGE_CACHE_SIZE;
204 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
205 sb->s_magic = EFIVARFS_MAGIC;
206 sb->s_op = &efivarfs_ops;
207 sb->s_d_op = &efivarfs_d_ops;
208 sb->s_time_gran = 1;
209
210 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
211 if (!inode)
212 return -ENOMEM;
213 inode->i_op = &efivarfs_dir_inode_operations;
214
215 root = d_make_root(inode);
216 sb->s_root = root;
217 if (!root)
218 return -ENOMEM;
219
220 INIT_LIST_HEAD(&efivarfs_list);
221
222 err = efivar_init(efivarfs_callback, (void *)sb, false,
223 true, &efivarfs_list);
224 if (err)
225 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
226
227 return err;
228}
229
230static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
231 int flags, const char *dev_name, void *data)
232{
233 return mount_single(fs_type, flags, data, efivarfs_fill_super);
234}
235
236static void efivarfs_kill_sb(struct super_block *sb)
237{
238 kill_litter_super(sb);
239 efivarfs_sb = NULL;
240
241 /* Remove all entries and destroy */
242 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
243}
244
245static struct file_system_type efivarfs_type = {
246 .name = "efivarfs",
247 .mount = efivarfs_mount,
248 .kill_sb = efivarfs_kill_sb,
249};
250
251static __init int efivarfs_init(void)
252{
253 if (!efi_enabled(EFI_RUNTIME_SERVICES))
254 return 0;
255
256 if (!efivars_kobject())
257 return 0;
258
259 return register_filesystem(&efivarfs_type);
260}
261
262MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
263MODULE_DESCRIPTION("EFI Variable Filesystem");
264MODULE_LICENSE("GPL");
265MODULE_ALIAS_FS("efivarfs");
266
267module_init(efivarfs_init);