blob: 8dd524f322847b346e119c6d5c5b3693de6fb8bc [file] [log] [blame]
Matt Flemingd68772b2013-02-08 16:27:24 +00001/*
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>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070012#include <linux/slab.h>
Matt Flemingd68772b2013-02-08 16:27:24 +000013
14#include "internal.h"
15
Matt Flemingd68772b2013-02-08 16:27:24 +000016static ssize_t efivarfs_file_write(struct file *file,
17 const char __user *userbuf, size_t count, loff_t *ppos)
18{
19 struct efivar_entry *var = file->private_data;
20 void *data;
21 u32 attributes;
22 struct inode *inode = file->f_mapping->host;
23 unsigned long datasize = count - sizeof(attributes);
24 ssize_t bytes = 0;
25 bool set = false;
26
27 if (count < sizeof(attributes))
28 return -EINVAL;
29
30 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
31 return -EFAULT;
32
33 if (attributes & ~(EFI_VARIABLE_MASK))
34 return -EINVAL;
35
36 data = kmalloc(datasize, GFP_KERNEL);
37 if (!data)
38 return -ENOMEM;
39
40 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
41 bytes = -EFAULT;
42 goto out;
43 }
44
45 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
46 data, &set);
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080047 if (!set && bytes) {
48 if (bytes == -ENOENT)
49 bytes = -EIO;
Matt Flemingd68772b2013-02-08 16:27:24 +000050 goto out;
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080051 }
Matt Flemingd68772b2013-02-08 16:27:24 +000052
53 if (bytes == -ENOENT) {
54 drop_nlink(inode);
55 d_delete(file->f_dentry);
56 dput(file->f_dentry);
57 } else {
58 mutex_lock(&inode->i_mutex);
59 i_size_write(inode, datasize + sizeof(attributes));
60 mutex_unlock(&inode->i_mutex);
61 }
62
63 bytes = count;
64
65out:
66 kfree(data);
67
68 return bytes;
69}
70
71static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
72 size_t count, loff_t *ppos)
73{
74 struct efivar_entry *var = file->private_data;
75 unsigned long datasize = 0;
76 u32 attributes;
77 void *data;
78 ssize_t size = 0;
79 int err;
80
81 err = efivar_entry_size(var, &datasize);
Lingzhu Xiang3fab70c2013-05-10 18:29:21 +080082
83 /*
84 * efivarfs represents uncommitted variables with
85 * zero-length files. Reading them should return EOF.
86 */
87 if (err == -ENOENT)
88 return 0;
89 else if (err)
Matt Flemingd68772b2013-02-08 16:27:24 +000090 return err;
91
92 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
93
94 if (!data)
95 return -ENOMEM;
96
97 size = efivar_entry_get(var, &attributes, &datasize,
98 data + sizeof(attributes));
99 if (size)
100 goto out_free;
101
102 memcpy(data, &attributes, sizeof(attributes));
103 size = simple_read_from_buffer(userbuf, count, ppos,
104 data, datasize + sizeof(attributes));
105out_free:
106 kfree(data);
107
108 return size;
109}
110
111const struct file_operations efivarfs_file_operations = {
H. Peter Anvinf53f2922013-04-20 09:16:44 -0700112 .open = simple_open,
Matt Flemingd68772b2013-02-08 16:27:24 +0000113 .read = efivarfs_file_read,
114 .write = efivarfs_file_write,
115 .llseek = no_llseek,
116};