Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 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> |
Linus Torvalds | 20b4fb4 | 2013-05-01 17:51:54 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 13 | |
| 14 | #include "internal.h" |
| 15 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 16 | static 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); |
Geyslan G. Bem | aca32b5 | 2013-10-30 15:57:41 -0300 | [diff] [blame^] | 24 | ssize_t bytes; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 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 | |
Geyslan G. Bem | aca32b5 | 2013-10-30 15:57:41 -0300 | [diff] [blame^] | 36 | data = memdup_user(userbuf + sizeof(attributes), datasize); |
| 37 | if (IS_ERR(data)) |
| 38 | return PTR_ERR(data); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 39 | |
| 40 | bytes = efivar_entry_set_get_size(var, attributes, &datasize, |
| 41 | data, &set); |
Lingzhu Xiang | 3fab70c | 2013-05-10 18:29:21 +0800 | [diff] [blame] | 42 | if (!set && bytes) { |
| 43 | if (bytes == -ENOENT) |
| 44 | bytes = -EIO; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 45 | goto out; |
Lingzhu Xiang | 3fab70c | 2013-05-10 18:29:21 +0800 | [diff] [blame] | 46 | } |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 47 | |
| 48 | if (bytes == -ENOENT) { |
| 49 | drop_nlink(inode); |
| 50 | d_delete(file->f_dentry); |
| 51 | dput(file->f_dentry); |
| 52 | } else { |
| 53 | mutex_lock(&inode->i_mutex); |
| 54 | i_size_write(inode, datasize + sizeof(attributes)); |
| 55 | mutex_unlock(&inode->i_mutex); |
| 56 | } |
| 57 | |
| 58 | bytes = count; |
| 59 | |
| 60 | out: |
| 61 | kfree(data); |
| 62 | |
| 63 | return bytes; |
| 64 | } |
| 65 | |
| 66 | static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf, |
| 67 | size_t count, loff_t *ppos) |
| 68 | { |
| 69 | struct efivar_entry *var = file->private_data; |
| 70 | unsigned long datasize = 0; |
| 71 | u32 attributes; |
| 72 | void *data; |
| 73 | ssize_t size = 0; |
| 74 | int err; |
| 75 | |
| 76 | err = efivar_entry_size(var, &datasize); |
Lingzhu Xiang | 3fab70c | 2013-05-10 18:29:21 +0800 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * efivarfs represents uncommitted variables with |
| 80 | * zero-length files. Reading them should return EOF. |
| 81 | */ |
| 82 | if (err == -ENOENT) |
| 83 | return 0; |
| 84 | else if (err) |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 85 | return err; |
| 86 | |
| 87 | data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL); |
| 88 | |
| 89 | if (!data) |
| 90 | return -ENOMEM; |
| 91 | |
| 92 | size = efivar_entry_get(var, &attributes, &datasize, |
| 93 | data + sizeof(attributes)); |
| 94 | if (size) |
| 95 | goto out_free; |
| 96 | |
| 97 | memcpy(data, &attributes, sizeof(attributes)); |
| 98 | size = simple_read_from_buffer(userbuf, count, ppos, |
| 99 | data, datasize + sizeof(attributes)); |
| 100 | out_free: |
| 101 | kfree(data); |
| 102 | |
| 103 | return size; |
| 104 | } |
| 105 | |
| 106 | const struct file_operations efivarfs_file_operations = { |
H. Peter Anvin | f53f292 | 2013-04-20 09:16:44 -0700 | [diff] [blame] | 107 | .open = simple_open, |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 108 | .read = efivarfs_file_read, |
| 109 | .write = efivarfs_file_write, |
| 110 | .llseek = no_llseek, |
| 111 | }; |