]> Pileus Git - ~andy/linux/blob - fs/efivarfs/inode.c
Merge branch 'x86-efi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~andy/linux] / fs / efivarfs / inode.c
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
16 struct 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  */
44 bool 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
81 static 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
101 static 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);
137 out:
138         if (err) {
139                 kfree(var);
140                 iput(inode);
141         }
142         return err;
143 }
144
145 static 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  */
160 static 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
169 const struct inode_operations efivarfs_dir_inode_operations = {
170         .lookup = efivarfs_lookup,
171         .unlink = efivarfs_unlink,
172         .create = efivarfs_create,
173 };