aboutsummaryrefslogtreecommitdiffstats
path: root/fs/efivarfs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-05-01 18:51:46 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-05-01 18:51:46 -0400
commitb9394d8a657cd3c064fa432aa0905c1b58b38fe9 (patch)
tree5a0c98370f313fa11693ab72eea0dc809fd6567d /fs/efivarfs
parent126de6b20bfb82cc19012d5048f11f339ae5a021 (diff)
parent7b2dd6d2c4db3912771bfcfd7ac7264011a3c831 (diff)
Merge branch 'x86-efi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86/efi changes from Peter Anvin: "The bulk of these changes are cleaning up the efivars handling and breaking it up into a tree of files. There are a number of fixes as well. The entire changeset is pretty big, but most of it is code movement. Several of these commits are quite new; the history got very messed up due to a mismerge with the urgent changes for rc8 which completely broke IA64, and so Ingo requested that we rebase it to straighten it out." * 'x86-efi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: efi: remove "kfree(NULL)" efi: locking fix in efivar_entry_set_safe() efi, pstore: Read data from variable store before memcpy() efi, pstore: Remove entry from list when erasing efi, pstore: Initialise 'entry' before iterating efi: split efisubsystem from efivars efivarfs: Move to fs/efivarfs efivars: Move pstore code into the new EFI directory efivars: efivar_entry API efivars: Keep a private global pointer to efivars efi: move utf16 string functions to efi.h x86, efi: Make efi_memblock_x86_reserve_range more readable efivarfs: convert to use simple_open()
Diffstat (limited to 'fs/efivarfs')
-rw-r--r--fs/efivarfs/Kconfig12
-rw-r--r--fs/efivarfs/Makefile7
-rw-r--r--fs/efivarfs/file.c105
-rw-r--r--fs/efivarfs/inode.c173
-rw-r--r--fs/efivarfs/internal.h22
-rw-r--r--fs/efivarfs/super.c268
6 files changed, 587 insertions, 0 deletions
diff --git a/fs/efivarfs/Kconfig b/fs/efivarfs/Kconfig
new file mode 100644
index 000000000000..367bbb10c543
--- /dev/null
+++ b/fs/efivarfs/Kconfig
@@ -0,0 +1,12 @@
1config EFIVAR_FS
2 tristate "EFI Variable filesystem"
3 depends on EFI
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..ede07fc7309f
--- /dev/null
+++ b/fs/efivarfs/file.c
@@ -0,0 +1,105 @@
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 ssize_t efivarfs_file_write(struct file *file,
16 const char __user *userbuf, size_t count, loff_t *ppos)
17{
18 struct efivar_entry *var = file->private_data;
19 void *data;
20 u32 attributes;
21 struct inode *inode = file->f_mapping->host;
22 unsigned long datasize = count - sizeof(attributes);
23 ssize_t bytes = 0;
24 bool set = false;
25
26 if (count < sizeof(attributes))
27 return -EINVAL;
28
29 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
30 return -EFAULT;
31
32 if (attributes & ~(EFI_VARIABLE_MASK))
33 return -EINVAL;
34
35 data = kmalloc(datasize, GFP_KERNEL);
36 if (!data)
37 return -ENOMEM;
38
39 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
40 bytes = -EFAULT;
41 goto out;
42 }
43
44 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
45 data, &set);
46 if (!set && bytes)
47 goto out;
48
49 if (bytes == -ENOENT) {
50 drop_nlink(inode);
51 d_delete(file->f_dentry);
52 dput(file->f_dentry);
53 } else {
54 mutex_lock(&inode->i_mutex);
55 i_size_write(inode, datasize + sizeof(attributes));
56 mutex_unlock(&inode->i_mutex);
57 }
58
59 bytes = count;
60
61out:
62 kfree(data);
63
64 return bytes;
65}
66
67static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
68 size_t count, loff_t *ppos)
69{
70 struct efivar_entry *var = file->private_data;
71 unsigned long datasize = 0;
72 u32 attributes;
73 void *data;
74 ssize_t size = 0;
75 int err;
76
77 err = efivar_entry_size(var, &datasize);
78 if (err)
79 return err;
80
81 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
82
83 if (!data)
84 return -ENOMEM;
85
86 size = efivar_entry_get(var, &attributes, &datasize,
87 data + sizeof(attributes));
88 if (size)
89 goto out_free;
90
91 memcpy(data, &attributes, sizeof(attributes));
92 size = simple_read_from_buffer(userbuf, count, ppos,
93 data, datasize + sizeof(attributes));
94out_free:
95 kfree(data);
96
97 return size;
98}
99
100const struct file_operations efivarfs_file_operations = {
101 .open = simple_open,
102 .read = efivarfs_file_read,
103 .write = efivarfs_file_write,
104 .llseek = no_llseek,
105};
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..525a2a1ac16c
--- /dev/null
+++ b/fs/efivarfs/super.c
@@ -0,0 +1,268 @@
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#include <linux/ucs2_string.h>
16
17#include "internal.h"
18
19LIST_HEAD(efivarfs_list);
20
21static void efivarfs_evict_inode(struct inode *inode)
22{
23 clear_inode(inode);
24}
25
26static const struct super_operations efivarfs_ops = {
27 .statfs = simple_statfs,
28 .drop_inode = generic_delete_inode,
29 .evict_inode = efivarfs_evict_inode,
30 .show_options = generic_show_options,
31};
32
33static struct super_block *efivarfs_sb;
34
35/*
36 * Compare two efivarfs file names.
37 *
38 * An efivarfs filename is composed of two parts,
39 *
40 * 1. A case-sensitive variable name
41 * 2. A case-insensitive GUID
42 *
43 * So we need to perform a case-sensitive match on part 1 and a
44 * case-insensitive match on part 2.
45 */
46static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
47 const struct dentry *dentry, const struct inode *inode,
48 unsigned int len, const char *str,
49 const struct qstr *name)
50{
51 int guid = len - EFI_VARIABLE_GUID_LEN;
52
53 if (name->len != len)
54 return 1;
55
56 /* Case-sensitive compare for the variable name */
57 if (memcmp(str, name->name, guid))
58 return 1;
59
60 /* Case-insensitive compare for the GUID */
61 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
62}
63
64static int efivarfs_d_hash(const struct dentry *dentry,
65 const struct inode *inode, struct qstr *qstr)
66{
67 unsigned long hash = init_name_hash();
68 const unsigned char *s = qstr->name;
69 unsigned int len = qstr->len;
70
71 if (!efivarfs_valid_name(s, len))
72 return -EINVAL;
73
74 while (len-- > EFI_VARIABLE_GUID_LEN)
75 hash = partial_name_hash(*s++, hash);
76
77 /* GUID is case-insensitive. */
78 while (len--)
79 hash = partial_name_hash(tolower(*s++), hash);
80
81 qstr->hash = end_name_hash(hash);
82 return 0;
83}
84
85/*
86 * Retaining negative dentries for an in-memory filesystem just wastes
87 * memory and lookup time: arrange for them to be deleted immediately.
88 */
89static int efivarfs_delete_dentry(const struct dentry *dentry)
90{
91 return 1;
92}
93
94static struct dentry_operations efivarfs_d_ops = {
95 .d_compare = efivarfs_d_compare,
96 .d_hash = efivarfs_d_hash,
97 .d_delete = efivarfs_delete_dentry,
98};
99
100static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
101{
102 struct dentry *d;
103 struct qstr q;
104 int err;
105
106 q.name = name;
107 q.len = strlen(name);
108
109 err = efivarfs_d_hash(NULL, NULL, &q);
110 if (err)
111 return ERR_PTR(err);
112
113 d = d_alloc(parent, &q);
114 if (d)
115 return d;
116
117 return ERR_PTR(-ENOMEM);
118}
119
120static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
121 unsigned long name_size, void *data)
122{
123 struct super_block *sb = (struct super_block *)data;
124 struct efivar_entry *entry;
125 struct inode *inode = NULL;
126 struct dentry *dentry, *root = sb->s_root;
127 unsigned long size = 0;
128 char *name;
129 int len, i;
130 int err = -ENOMEM;
131
132 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
133 if (!entry)
134 return err;
135
136 memcpy(entry->var.VariableName, name16, name_size);
137 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
138
139 len = ucs2_strlen(entry->var.VariableName);
140
141 /* name, plus '-', plus GUID, plus NUL*/
142 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
143 if (!name)
144 goto fail;
145
146 for (i = 0; i < len; i++)
147 name[i] = entry->var.VariableName[i] & 0xFF;
148
149 name[len] = '-';
150
151 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
152
153 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
154
155 inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
156 if (!inode)
157 goto fail_name;
158
159 dentry = efivarfs_alloc_dentry(root, name);
160 if (IS_ERR(dentry)) {
161 err = PTR_ERR(dentry);
162 goto fail_inode;
163 }
164
165 /* copied by the above to local storage in the dentry. */
166 kfree(name);
167
168 efivar_entry_size(entry, &size);
169 efivar_entry_add(entry, &efivarfs_list);
170
171 mutex_lock(&inode->i_mutex);
172 inode->i_private = entry;
173 i_size_write(inode, size + sizeof(entry->var.Attributes));
174 mutex_unlock(&inode->i_mutex);
175 d_add(dentry, inode);
176
177 return 0;
178
179fail_inode:
180 iput(inode);
181fail_name:
182 kfree(name);
183fail:
184 kfree(entry);
185 return err;
186}
187
188static int efivarfs_destroy(struct efivar_entry *entry, void *data)
189{
190 efivar_entry_remove(entry);
191 kfree(entry);
192 return 0;
193}
194
195static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
196{
197 struct inode *inode = NULL;
198 struct dentry *root;
199 int err;
200
201 efivarfs_sb = sb;
202
203 sb->s_maxbytes = MAX_LFS_FILESIZE;
204 sb->s_blocksize = PAGE_CACHE_SIZE;
205 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
206 sb->s_magic = EFIVARFS_MAGIC;
207 sb->s_op = &efivarfs_ops;
208 sb->s_d_op = &efivarfs_d_ops;
209 sb->s_time_gran = 1;
210
211 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
212 if (!inode)
213 return -ENOMEM;
214 inode->i_op = &efivarfs_dir_inode_operations;
215
216 root = d_make_root(inode);
217 sb->s_root = root;
218 if (!root)
219 return -ENOMEM;
220
221 INIT_LIST_HEAD(&efivarfs_list);
222
223 err = efivar_init(efivarfs_callback, (void *)sb, false,
224 true, &efivarfs_list);
225 if (err)
226 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
227
228 return err;
229}
230
231static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
232 int flags, const char *dev_name, void *data)
233{
234 return mount_single(fs_type, flags, data, efivarfs_fill_super);
235}
236
237static void efivarfs_kill_sb(struct super_block *sb)
238{
239 kill_litter_super(sb);
240 efivarfs_sb = NULL;
241
242 /* Remove all entries and destroy */
243 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
244}
245
246static struct file_system_type efivarfs_type = {
247 .name = "efivarfs",
248 .mount = efivarfs_mount,
249 .kill_sb = efivarfs_kill_sb,
250};
251
252static __init int efivarfs_init(void)
253{
254 if (!efi_enabled(EFI_RUNTIME_SERVICES))
255 return 0;
256
257 if (!efivars_kobject())
258 return 0;
259
260 return register_filesystem(&efivarfs_type);
261}
262
263MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
264MODULE_DESCRIPTION("EFI Variable Filesystem");
265MODULE_LICENSE("GPL");
266MODULE_ALIAS_FS("efivarfs");
267
268module_init(efivarfs_init);