super.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Red Hat, Inc.
  4. * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
  5. */
  6. #include <linux/ctype.h>
  7. #include <linux/efi.h>
  8. #include <linux/fs.h>
  9. #include <linux/fs_context.h>
  10. #include <linux/module.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/ucs2_string.h>
  13. #include <linux/slab.h>
  14. #include <linux/magic.h>
  15. #include "internal.h"
  16. LIST_HEAD(efivarfs_list);
  17. static void efivarfs_evict_inode(struct inode *inode)
  18. {
  19. clear_inode(inode);
  20. }
  21. static const struct super_operations efivarfs_ops = {
  22. .statfs = simple_statfs,
  23. .drop_inode = generic_delete_inode,
  24. .evict_inode = efivarfs_evict_inode,
  25. };
  26. /*
  27. * Compare two efivarfs file names.
  28. *
  29. * An efivarfs filename is composed of two parts,
  30. *
  31. * 1. A case-sensitive variable name
  32. * 2. A case-insensitive GUID
  33. *
  34. * So we need to perform a case-sensitive match on part 1 and a
  35. * case-insensitive match on part 2.
  36. */
  37. static int efivarfs_d_compare(const struct dentry *dentry,
  38. unsigned int len, const char *str,
  39. const struct qstr *name)
  40. {
  41. int guid = len - EFI_VARIABLE_GUID_LEN;
  42. if (name->len != len)
  43. return 1;
  44. /* Case-sensitive compare for the variable name */
  45. if (memcmp(str, name->name, guid))
  46. return 1;
  47. /* Case-insensitive compare for the GUID */
  48. return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
  49. }
  50. static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
  51. {
  52. unsigned long hash = init_name_hash(dentry);
  53. const unsigned char *s = qstr->name;
  54. unsigned int len = qstr->len;
  55. if (!efivarfs_valid_name(s, len))
  56. return -EINVAL;
  57. while (len-- > EFI_VARIABLE_GUID_LEN)
  58. hash = partial_name_hash(*s++, hash);
  59. /* GUID is case-insensitive. */
  60. while (len--)
  61. hash = partial_name_hash(tolower(*s++), hash);
  62. qstr->hash = end_name_hash(hash);
  63. return 0;
  64. }
  65. static const struct dentry_operations efivarfs_d_ops = {
  66. .d_compare = efivarfs_d_compare,
  67. .d_hash = efivarfs_d_hash,
  68. .d_delete = always_delete_dentry,
  69. };
  70. static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
  71. {
  72. struct dentry *d;
  73. struct qstr q;
  74. int err;
  75. q.name = name;
  76. q.len = strlen(name);
  77. err = efivarfs_d_hash(parent, &q);
  78. if (err)
  79. return ERR_PTR(err);
  80. d = d_alloc(parent, &q);
  81. if (d)
  82. return d;
  83. return ERR_PTR(-ENOMEM);
  84. }
  85. static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
  86. unsigned long name_size, void *data)
  87. {
  88. struct super_block *sb = (struct super_block *)data;
  89. struct efivar_entry *entry;
  90. struct inode *inode = NULL;
  91. struct dentry *dentry, *root = sb->s_root;
  92. unsigned long size = 0;
  93. char *name;
  94. int len;
  95. int err = -ENOMEM;
  96. bool is_removable = false;
  97. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  98. if (!entry)
  99. return err;
  100. memcpy(entry->var.VariableName, name16, name_size);
  101. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  102. len = ucs2_utf8size(entry->var.VariableName);
  103. /* name, plus '-', plus GUID, plus NUL*/
  104. name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
  105. if (!name)
  106. goto fail;
  107. ucs2_as_utf8(name, entry->var.VariableName, len);
  108. if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
  109. is_removable = true;
  110. name[len] = '-';
  111. efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
  112. name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
  113. /* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
  114. strreplace(name, '/', '!');
  115. inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
  116. is_removable);
  117. if (!inode)
  118. goto fail_name;
  119. dentry = efivarfs_alloc_dentry(root, name);
  120. if (IS_ERR(dentry)) {
  121. err = PTR_ERR(dentry);
  122. goto fail_inode;
  123. }
  124. efivar_entry_size(entry, &size);
  125. err = efivar_entry_add(entry, &efivarfs_list);
  126. if (err)
  127. goto fail_inode;
  128. /* copied by the above to local storage in the dentry. */
  129. kfree(name);
  130. inode_lock(inode);
  131. inode->i_private = entry;
  132. i_size_write(inode, size + sizeof(entry->var.Attributes));
  133. inode_unlock(inode);
  134. d_add(dentry, inode);
  135. return 0;
  136. fail_inode:
  137. iput(inode);
  138. fail_name:
  139. kfree(name);
  140. fail:
  141. kfree(entry);
  142. return err;
  143. }
  144. static int efivarfs_destroy(struct efivar_entry *entry, void *data)
  145. {
  146. int err = efivar_entry_remove(entry);
  147. if (err)
  148. return err;
  149. kfree(entry);
  150. return 0;
  151. }
  152. static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
  153. {
  154. struct inode *inode = NULL;
  155. struct dentry *root;
  156. int err;
  157. sb->s_maxbytes = MAX_LFS_FILESIZE;
  158. sb->s_blocksize = PAGE_SIZE;
  159. sb->s_blocksize_bits = PAGE_SHIFT;
  160. sb->s_magic = EFIVARFS_MAGIC;
  161. sb->s_op = &efivarfs_ops;
  162. sb->s_d_op = &efivarfs_d_ops;
  163. sb->s_time_gran = 1;
  164. if (!efivar_supports_writes())
  165. sb->s_flags |= SB_RDONLY;
  166. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
  167. if (!inode)
  168. return -ENOMEM;
  169. inode->i_op = &efivarfs_dir_inode_operations;
  170. root = d_make_root(inode);
  171. sb->s_root = root;
  172. if (!root)
  173. return -ENOMEM;
  174. INIT_LIST_HEAD(&efivarfs_list);
  175. err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
  176. if (err)
  177. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  178. return err;
  179. }
  180. static int efivarfs_get_tree(struct fs_context *fc)
  181. {
  182. return get_tree_single(fc, efivarfs_fill_super);
  183. }
  184. static const struct fs_context_operations efivarfs_context_ops = {
  185. .get_tree = efivarfs_get_tree,
  186. };
  187. static int efivarfs_init_fs_context(struct fs_context *fc)
  188. {
  189. fc->ops = &efivarfs_context_ops;
  190. return 0;
  191. }
  192. static void efivarfs_kill_sb(struct super_block *sb)
  193. {
  194. kill_litter_super(sb);
  195. /* Remove all entries and destroy */
  196. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  197. }
  198. static struct file_system_type efivarfs_type = {
  199. .owner = THIS_MODULE,
  200. .name = "efivarfs",
  201. .init_fs_context = efivarfs_init_fs_context,
  202. .kill_sb = efivarfs_kill_sb,
  203. };
  204. static __init int efivarfs_init(void)
  205. {
  206. if (!efivars_kobject())
  207. return -ENODEV;
  208. return register_filesystem(&efivarfs_type);
  209. }
  210. static __exit void efivarfs_exit(void)
  211. {
  212. unregister_filesystem(&efivarfs_type);
  213. }
  214. MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
  215. MODULE_DESCRIPTION("EFI Variable Filesystem");
  216. MODULE_LICENSE("GPL");
  217. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
  218. MODULE_ALIAS_FS("efivarfs");
  219. module_init(efivarfs_init);
  220. module_exit(efivarfs_exit);