File: /usr/src/linux/arch/ia64/kernel/efivars.c
1 /*
2 * EFI Variables - efivars.c
3 *
4 * Copyright (C) 2001 Dell Computer Corporation <Matt_Domsch@dell.com>
5 *
6 * This code takes all variables accessible from EFI runtime and
7 * exports them via /proc
8 *
9 * Reads to /proc/efi/vars/varname return an efi_variable_t structure.
10 * Writes to /proc/efi/vars/varname must be an efi_variable_t structure.
11 * Writes with DataSize = 0 or Attributes = 0 deletes the variable.
12 * Writes with a new value in VariableName+VendorGuid creates
13 * a new variable.
14 *
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * Changelog:
31 *
32 * 20 April 2001 - Matt Domsch <Matt_Domsch@dell.com>
33 * Moved vars from /proc/efi to /proc/efi/vars, and made
34 * efi.c own the /proc/efi directory.
35 * v0.03 release to linux-ia64@linuxia64.org
36 *
37 * 26 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
38 * At the request of Stephane, moved ownership of /proc/efi
39 * to efi.c, and now efivars lives under /proc/efi/vars.
40 *
41 * 12 March 2001 - Matt Domsch <Matt_Domsch@dell.com>
42 * Feedback received from Stephane Eranian incorporated.
43 * efivar_write() checks copy_from_user() return value.
44 * efivar_read/write() returns proper errno.
45 * v0.02 release to linux-ia64@linuxia64.org
46 *
47 * 26 February 2001 - Matt Domsch <Matt_Domsch@dell.com>
48 * v0.01 release to linux-ia64@linuxia64.org
49 */
50
51 #include <linux/config.h>
52 #include <linux/types.h>
53 #include <linux/errno.h>
54 #include <linux/init.h>
55 #include <linux/proc_fs.h>
56 #include <linux/sched.h> /* for capable() */
57 #include <linux/mm.h>
58 #include <linux/module.h>
59
60 #include <asm/efi.h>
61 #include <asm/uaccess.h>
62 #ifdef CONFIG_SMP
63 #include <linux/smp.h>
64 #endif
65
66 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
67 MODULE_DESCRIPTION("/proc interface to EFI Variables");
68
69 #define EFIVARS_VERSION "0.03 2001-Apr-20"
70
71 static int
72 efivar_read(char *page, char **start, off_t off,
73 int count, int *eof, void *data);
74 static int
75 efivar_write(struct file *file, const char *buffer,
76 unsigned long count, void *data);
77
78
79 /*
80 * The maximum size of VariableName + Data = 1024
81 * Therefore, it's reasonable to save that much
82 * space in each part of the structure,
83 * and we use a page for reading/writing.
84 */
85
86 typedef struct _efi_variable_t {
87 efi_char16_t VariableName[1024/sizeof(efi_char16_t)];
88 efi_guid_t VendorGuid;
89 unsigned long DataSize;
90 __u8 Data[1024];
91 efi_status_t Status;
92 __u32 Attributes;
93 } __attribute__((packed)) efi_variable_t;
94
95
96 typedef struct _efivar_entry_t {
97 efi_variable_t var;
98 struct proc_dir_entry *entry;
99 struct list_head list;
100 } efivar_entry_t;
101
102 spinlock_t efivars_lock = SPIN_LOCK_UNLOCKED;
103 static LIST_HEAD(efivar_list);
104 static struct proc_dir_entry *efi_vars_dir = NULL;
105
106 #define efivar_entry(n) list_entry(n, efivar_entry_t, list)
107
108 /* Return the number of unicode characters in data */
109 static unsigned long
110 utf8_strlen(efi_char16_t *data, unsigned long maxlength)
111 {
112 unsigned long length = 0;
113 while (*data++ != 0 && length < maxlength)
114 length++;
115 return length;
116 }
117
118 /* Return the number of bytes is the length of this string */
119 /* Note: this is NOT the same as the number of unicode characters */
120 static inline unsigned long
121 utf8_strsize(efi_char16_t *data, unsigned long maxlength)
122 {
123 return utf8_strlen(data, maxlength/sizeof(efi_char16_t)) *
124 sizeof(efi_char16_t);
125 }
126
127
128 static int
129 proc_calc_metrics(char *page, char **start, off_t off,
130 int count, int *eof, int len)
131 {
132 if (len <= off+count) *eof = 1;
133 *start = page + off;
134 len -= off;
135 if (len>count) len = count;
136 if (len<0) len = 0;
137 return len;
138 }
139
140
141 static void
142 uuid_unparse(efi_guid_t *guid, char *out)
143 {
144 sprintf(out, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
145 guid->data1, guid->data2, guid->data3,
146 guid->data4[0], guid->data4[1], guid->data4[2], guid->data4[3],
147 guid->data4[4], guid->data4[5], guid->data4[6], guid->data4[7]);
148 }
149
150
151
152
153
154 /*
155 * efivar_create_proc_entry()
156 * Requires:
157 * variable_name_size = number of bytes required to hold
158 * variable_name (not counting the NULL
159 * character at the end.
160 * Returns 1 on failure, 0 on success
161 */
162 static int
163 efivar_create_proc_entry(unsigned long variable_name_size,
164 efi_char16_t *variable_name,
165 efi_guid_t *vendor_guid)
166 {
167
168 int i, short_name_size = variable_name_size /
169 sizeof(efi_char16_t) + 38;
170 char *short_name = kmalloc(short_name_size+1,
171 GFP_KERNEL);
172 efivar_entry_t *new_efivar = kmalloc(sizeof(efivar_entry_t),
173 GFP_KERNEL);
174 if (!short_name || !new_efivar) {
175 if (short_name) kfree(short_name);
176 if (new_efivar) kfree(new_efivar);
177 return 1;
178 }
179 memset(short_name, 0, short_name_size+1);
180 memset(new_efivar, 0, sizeof(efivar_entry_t));
181
182 memcpy(new_efivar->var.VariableName, variable_name,
183 variable_name_size);
184 memcpy(&(new_efivar->var.VendorGuid), vendor_guid, sizeof(efi_guid_t));
185
186 /* Convert Unicode to normal chars (assume top bits are 0),
187 ala UTF-8 */
188 for (i=0; i<variable_name_size / sizeof(efi_char16_t); i++) {
189 short_name[i] = variable_name[i] & 0xFF;
190 }
191
192 /* This is ugly, but necessary to separate one vendor's
193 private variables from another's. */
194
195 *(short_name + strlen(short_name)) = '-';
196 uuid_unparse(vendor_guid, short_name + strlen(short_name));
197
198
199 /* Create the entry in proc */
200 new_efivar->entry = create_proc_entry(short_name, 0600, efi_vars_dir);
201 kfree(short_name); short_name = NULL;
202 if (!new_efivar->entry) return 1;
203
204
205 new_efivar->entry->data = new_efivar;
206 new_efivar->entry->read_proc = efivar_read;
207 new_efivar->entry->write_proc = efivar_write;
208
209 list_add(&new_efivar->list, &efivar_list);
210
211
212 return 0;
213 }
214
215
216
217 /***********************************************************
218 * efivar_read()
219 * Requires:
220 * Modifies: page
221 * Returns: number of bytes written, or -EINVAL on failure
222 ***********************************************************/
223
224 static int
225 efivar_read(char *page, char **start, off_t off, int count, int *eof, void *data)
226 {
227 int len = sizeof(efi_variable_t);
228 efivar_entry_t *efi_var = data;
229 efi_variable_t *var_data = (efi_variable_t *)page;
230
231 if (!page || !data) return -EINVAL;
232
233 spin_lock(&efivars_lock);
234 MOD_INC_USE_COUNT;
235
236 memcpy(var_data, &efi_var->var, len);
237
238 var_data->DataSize = 1024;
239 var_data->Status = efi.get_variable(var_data->VariableName,
240 &var_data->VendorGuid,
241 &var_data->Attributes,
242 &var_data->DataSize,
243 var_data->Data);
244
245 MOD_DEC_USE_COUNT;
246 spin_unlock(&efivars_lock);
247
248 return proc_calc_metrics(page, start, off, count, eof, len);
249 }
250
251 /***********************************************************
252 * efivar_write()
253 * Requires: data is an efi_setvariable_t data type,
254 * properly filled in, possibly by a call
255 * first to efivar_read().
256 * Caller must have CAP_SYS_ADMIN
257 * Modifies: NVRAM
258 * Returns: var_data->DataSize on success, errno on failure
259 *
260 ***********************************************************/
261 static int
262 efivar_write(struct file *file, const char *buffer,
263 unsigned long count, void *data)
264 {
265 unsigned long strsize1, strsize2;
266 int found=0;
267 struct list_head *pos;
268 unsigned long size = sizeof(efi_variable_t);
269 efi_status_t status;
270 efivar_entry_t *efivar = data, *search_efivar = NULL;
271 efi_variable_t *var_data;
272 if (!data || count != size) {
273 printk(KERN_WARNING "efivars: improper struct of size 0x%lx passed.\n", count);
274 return -EINVAL;
275 }
276 if (!capable(CAP_SYS_ADMIN))
277 return -EACCES;
278
279 spin_lock(&efivars_lock);
280 MOD_INC_USE_COUNT;
281
282 var_data = kmalloc(size, GFP_KERNEL);
283 if (!var_data) {
284 MOD_DEC_USE_COUNT;
285 spin_unlock(&efivars_lock);
286 return -ENOMEM;
287 }
288 if (copy_from_user(var_data, buffer, size)) {
289 MOD_DEC_USE_COUNT;
290 spin_unlock(&efivars_lock);
291 return -EFAULT;
292 }
293
294
295 /* Since the data ptr we've currently got is probably for
296 a different variable find the right variable.
297 This allows any properly formatted data structure to
298 be written to any of the files in /proc/efi/vars and it will work.
299 */
300 list_for_each(pos, &efivar_list) {
301 search_efivar = efivar_entry(pos);
302 strsize1 = utf8_strsize(search_efivar->var.VariableName, 1024);
303 strsize2 = utf8_strsize(var_data->VariableName, 1024);
304 if ( strsize1 == strsize2 &&
305 !memcmp(&(search_efivar->var.VariableName),
306 var_data->VariableName, strsize1) &&
307 !efi_guidcmp(search_efivar->var.VendorGuid,
308 var_data->VendorGuid)) {
309 found = 1;
310 break;
311 }
312 }
313 if (found) efivar = search_efivar;
314
315 status = efi.set_variable(var_data->VariableName,
316 &var_data->VendorGuid,
317 var_data->Attributes,
318 var_data->DataSize,
319 var_data->Data);
320
321 if (status != EFI_SUCCESS) {
322 printk(KERN_WARNING "set_variable() failed: status=%lx\n", status);
323 kfree(var_data);
324 MOD_DEC_USE_COUNT;
325 spin_unlock(&efivars_lock);
326 return -EIO;
327 }
328
329
330 if (!var_data->DataSize || !var_data->Attributes) {
331 /* We just deleted the NVRAM variable */
332 remove_proc_entry(efivar->entry->name, efi_vars_dir);
333 list_del(&efivar->list);
334 kfree(efivar);
335 }
336
337 /* If this is a new variable, set up the proc entry for it. */
338 if (!found) {
339 efivar_create_proc_entry(utf8_strsize(var_data->VariableName,
340 1024),
341 var_data->VariableName,
342 &var_data->VendorGuid);
343 }
344
345 kfree(var_data);
346 MOD_DEC_USE_COUNT;
347 spin_unlock(&efivars_lock);
348 return size;
349 }
350
351
352
353 static int __init
354 efivars_init(void)
355 {
356
357 efi_status_t status;
358 efi_guid_t vendor_guid;
359 efi_char16_t *variable_name = kmalloc(1024, GFP_KERNEL);
360 unsigned long variable_name_size = 1024;
361
362 spin_lock(&efivars_lock);
363
364 printk(KERN_INFO "EFI Variables Facility v%s\n", EFIVARS_VERSION);
365
366 /* Since efi.c happens before procfs is available,
367 we create the directory here if it doesn't
368 already exist. There's probably a better way
369 to do this.
370 */
371 if (!efi_dir)
372 efi_dir = proc_mkdir("efi", NULL);
373
374 efi_vars_dir = proc_mkdir("vars", efi_dir);
375
376
377
378 /* Per EFI spec, the maximum storage allocated for both
379 the variable name and variable data is 1024 bytes.
380 */
381
382 memset(variable_name, 0, 1024);
383
384 do {
385 variable_name_size=1024;
386
387 status = efi.get_next_variable(&variable_name_size,
388 variable_name,
389 &vendor_guid);
390
391
392 switch (status) {
393 case EFI_SUCCESS:
394 efivar_create_proc_entry(variable_name_size,
395 variable_name,
396 &vendor_guid);
397 break;
398 case EFI_NOT_FOUND:
399 break;
400 default:
401 printk(KERN_WARNING "get_next_variable: status=%lx\n", status);
402 status = EFI_NOT_FOUND;
403 break;
404 }
405
406 } while (status != EFI_NOT_FOUND);
407
408 kfree(variable_name);
409 spin_unlock(&efivars_lock);
410 return 0;
411 }
412
413 static void __exit
414 efivars_exit(void)
415 {
416 struct list_head *pos;
417 efivar_entry_t *efivar;
418
419 spin_lock(&efivars_lock);
420
421 list_for_each(pos, &efivar_list) {
422 efivar = efivar_entry(pos);
423 remove_proc_entry(efivar->entry->name, efi_vars_dir);
424 list_del(&efivar->list);
425 kfree(efivar);
426 }
427 remove_proc_entry(efi_vars_dir->name, efi_dir);
428 spin_unlock(&efivars_lock);
429
430 }
431
432 module_init(efivars_init);
433 module_exit(efivars_exit);
434
435 /*
436 * Overrides for Emacs so that we follow Linus's tabbing style.
437 * Emacs will notice this stuff at the end of the file and automatically
438 * adjust the settings for this buffer only. This must remain at the end
439 * of the file.
440 * ---------------------------------------------------------------------------
441 * Local variables:
442 * c-indent-level: 4
443 * c-brace-imaginary-offset: 0
444 * c-brace-offset: -4
445 * c-argdecl-indent: 4
446 * c-label-offset: -4
447 * c-continued-statement-offset: 4
448 * c-continued-brace-offset: 0
449 * indent-tabs-mode: nil
450 * tab-width: 8
451 * End:
452 */
453