Merge branch 'pm-cpufreq-fixes'
[deliverable/linux.git] / 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 #include <linux/slab.h>
14
15 #include "internal.h"
16
17 struct inode *efivarfs_get_inode(struct super_block *sb,
18 const struct inode *dir, int mode,
19 dev_t dev, bool is_removable)
20 {
21 struct inode *inode = new_inode(sb);
22
23 if (inode) {
24 inode->i_ino = get_next_ino();
25 inode->i_mode = mode;
26 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
27 inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
28 switch (mode & S_IFMT) {
29 case S_IFREG:
30 inode->i_fop = &efivarfs_file_operations;
31 break;
32 case S_IFDIR:
33 inode->i_op = &efivarfs_dir_inode_operations;
34 inode->i_fop = &simple_dir_operations;
35 inc_nlink(inode);
36 break;
37 }
38 }
39 return inode;
40 }
41
42 /*
43 * Return true if 'str' is a valid efivarfs filename of the form,
44 *
45 * VariableName-12345678-1234-1234-1234-1234567891bc
46 */
47 bool efivarfs_valid_name(const char *str, int len)
48 {
49 static const char dashes[EFI_VARIABLE_GUID_LEN] = {
50 [8] = 1, [13] = 1, [18] = 1, [23] = 1
51 };
52 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
53 int i;
54
55 /*
56 * We need a GUID, plus at least one letter for the variable name,
57 * plus the '-' separator
58 */
59 if (len < EFI_VARIABLE_GUID_LEN + 2)
60 return false;
61
62 /* GUID must be preceded by a '-' */
63 if (*(s - 1) != '-')
64 return false;
65
66 /*
67 * Validate that 's' is of the correct format, e.g.
68 *
69 * 12345678-1234-1234-1234-123456789abc
70 */
71 for (i = 0; i < EFI_VARIABLE_GUID_LEN; i++) {
72 if (dashes[i]) {
73 if (*s++ != '-')
74 return false;
75 } else {
76 if (!isxdigit(*s++))
77 return false;
78 }
79 }
80
81 return true;
82 }
83
84 static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
85 {
86 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
87 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
88 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
89 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
90 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
91 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
92 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
93 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
94 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
95 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
96 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
97 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
98 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
99 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
100 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
101 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
102 }
103
104 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
105 umode_t mode, bool excl)
106 {
107 struct inode *inode = NULL;
108 struct efivar_entry *var;
109 int namelen, i = 0, err = 0;
110 bool is_removable = false;
111
112 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
113 return -EINVAL;
114
115 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
116 if (!var)
117 return -ENOMEM;
118
119 /* length of the variable name itself: remove GUID and separator */
120 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
121
122 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
123 &var->var.VendorGuid);
124
125 if (efivar_variable_is_removable(var->var.VendorGuid,
126 dentry->d_name.name, namelen))
127 is_removable = true;
128
129 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
130 if (!inode) {
131 err = -ENOMEM;
132 goto out;
133 }
134
135 for (i = 0; i < namelen; i++)
136 var->var.VariableName[i] = dentry->d_name.name[i];
137
138 var->var.VariableName[i] = '\0';
139
140 inode->i_private = var;
141
142 efivar_entry_add(var, &efivarfs_list);
143 d_instantiate(dentry, inode);
144 dget(dentry);
145 out:
146 if (err) {
147 kfree(var);
148 if (inode)
149 iput(inode);
150 }
151 return err;
152 }
153
154 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
155 {
156 struct efivar_entry *var = d_inode(dentry)->i_private;
157
158 if (efivar_entry_delete(var))
159 return -EINVAL;
160
161 drop_nlink(d_inode(dentry));
162 dput(dentry);
163 return 0;
164 };
165
166 const struct inode_operations efivarfs_dir_inode_operations = {
167 .lookup = simple_lookup,
168 .unlink = efivarfs_unlink,
169 .create = efivarfs_create,
170 };
This page took 0.040489 seconds and 5 git commands to generate.