file.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <linux/efi.h>
  10. #include <linux/fs.h>
  11. #include <linux/slab.h>
  12. #include "internal.h"
  13. static ssize_t efivarfs_file_write(struct file *file,
  14. const char __user *userbuf, size_t count, loff_t *ppos)
  15. {
  16. struct efivar_entry *var = file->private_data;
  17. void *data;
  18. u32 attributes;
  19. struct inode *inode = file->f_mapping->host;
  20. unsigned long datasize = count - sizeof(attributes);
  21. ssize_t bytes = 0;
  22. bool set = false;
  23. if (count < sizeof(attributes))
  24. return -EINVAL;
  25. if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
  26. return -EFAULT;
  27. if (attributes & ~(EFI_VARIABLE_MASK))
  28. return -EINVAL;
  29. data = kmalloc(datasize, GFP_KERNEL);
  30. if (!data)
  31. return -ENOMEM;
  32. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  33. bytes = -EFAULT;
  34. goto out;
  35. }
  36. bytes = efivar_entry_set_get_size(var, attributes, &datasize,
  37. data, &set);
  38. if (!set && bytes)
  39. goto out;
  40. if (bytes == -ENOENT) {
  41. drop_nlink(inode);
  42. d_delete(file->f_dentry);
  43. dput(file->f_dentry);
  44. } else {
  45. mutex_lock(&inode->i_mutex);
  46. i_size_write(inode, datasize + sizeof(attributes));
  47. mutex_unlock(&inode->i_mutex);
  48. }
  49. bytes = count;
  50. out:
  51. kfree(data);
  52. return bytes;
  53. }
  54. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  55. size_t count, loff_t *ppos)
  56. {
  57. struct efivar_entry *var = file->private_data;
  58. unsigned long datasize = 0;
  59. u32 attributes;
  60. void *data;
  61. ssize_t size = 0;
  62. int err;
  63. err = efivar_entry_size(var, &datasize);
  64. if (err)
  65. return err;
  66. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  67. if (!data)
  68. return -ENOMEM;
  69. size = efivar_entry_get(var, &attributes, &datasize,
  70. data + sizeof(attributes));
  71. if (size)
  72. goto out_free;
  73. memcpy(data, &attributes, sizeof(attributes));
  74. size = simple_read_from_buffer(userbuf, count, ppos,
  75. data, datasize + sizeof(attributes));
  76. out_free:
  77. kfree(data);
  78. return size;
  79. }
  80. const struct file_operations efivarfs_file_operations = {
  81. .open = simple_open,
  82. .read = efivarfs_file_read,
  83. .write = efivarfs_file_write,
  84. .llseek = no_llseek,
  85. };