aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/firmware
diff options
context:
space:
mode:
authorMatt Fleming <matt.fleming@intel.com>2013-02-08 11:27:24 -0500
committerMatt Fleming <matt.fleming@intel.com>2013-04-17 08:25:09 -0400
commitd68772b7c83f4b518be15ae96f4827c8ed02f684 (patch)
treeb3408a750976edf6e0b62bfce3e26d1c9685a907 /drivers/firmware
parent048517722cde2595a7366d0c3c72b8b1ec142a9c (diff)
efivarfs: Move to fs/efivarfs
Now that efivarfs uses the efivar API, move it out of efivars.c and into fs/efivarfs where it belongs. This move will eventually allow us to enable the efivarfs code without having to also enable CONFIG_EFI_VARS built, and vice versa. Furthermore, things like, mount -t efivarfs none /sys/firmware/efi/efivars will now work if efivarfs is built as a module without requiring the use of MODULE_ALIAS(), which would have been necessary when the efivarfs code was part of efivars.c. Cc: Matthew Garrett <matthew.garrett@nebula.com> Cc: Jeremy Kerr <jk@ozlabs.org> Reviewed-by: Tom Gundersen <teg@jklm.no> Tested-by: Tom Gundersen <teg@jklm.no> Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Diffstat (limited to 'drivers/firmware')
-rw-r--r--drivers/firmware/efivars.c496
1 files changed, 0 insertions, 496 deletions
diff --git a/drivers/firmware/efivars.c b/drivers/firmware/efivars.c
index 5e7c3b1acde9..af396758482f 100644
--- a/drivers/firmware/efivars.c
+++ b/drivers/firmware/efivars.c
@@ -94,7 +94,6 @@ MODULE_DESCRIPTION("sysfs interface to EFI Variables");
94MODULE_LICENSE("GPL"); 94MODULE_LICENSE("GPL");
95MODULE_VERSION(EFIVARS_VERSION); 95MODULE_VERSION(EFIVARS_VERSION);
96 96
97static LIST_HEAD(efivarfs_list);
98LIST_HEAD(efivar_sysfs_list); 97LIST_HEAD(efivar_sysfs_list);
99EXPORT_SYMBOL_GPL(efivar_sysfs_list); 98EXPORT_SYMBOL_GPL(efivar_sysfs_list);
100 99
@@ -558,12 +557,6 @@ static struct kobj_type efivar_ktype = {
558 .default_attrs = def_attrs, 557 .default_attrs = def_attrs,
559}; 558};
560 559
561static int efivarfs_file_open(struct inode *inode, struct file *file)
562{
563 file->private_data = inode->i_private;
564 return 0;
565}
566
567static int efi_status_to_err(efi_status_t status) 560static int efi_status_to_err(efi_status_t status)
568{ 561{
569 int err; 562 int err;
@@ -597,493 +590,6 @@ static int efi_status_to_err(efi_status_t status)
597 return err; 590 return err;
598} 591}
599 592
600static ssize_t efivarfs_file_write(struct file *file,
601 const char __user *userbuf, size_t count, loff_t *ppos)
602{
603 struct efivar_entry *var = file->private_data;
604 void *data;
605 u32 attributes;
606 struct inode *inode = file->f_mapping->host;
607 unsigned long datasize = count - sizeof(attributes);
608 ssize_t bytes = 0;
609 bool set = false;
610
611 if (count < sizeof(attributes))
612 return -EINVAL;
613
614 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
615 return -EFAULT;
616
617 if (attributes & ~(EFI_VARIABLE_MASK))
618 return -EINVAL;
619
620 data = kmalloc(datasize, GFP_KERNEL);
621 if (!data)
622 return -ENOMEM;
623
624 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
625 bytes = -EFAULT;
626 goto out;
627 }
628
629 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
630 data, &set);
631 if (!set && bytes)
632 goto out;
633
634 if (!bytes) {
635 mutex_lock(&inode->i_mutex);
636 i_size_write(inode, datasize + sizeof(attributes));
637 mutex_unlock(&inode->i_mutex);
638 } else if (bytes == -ENOENT) {
639 drop_nlink(inode);
640 d_delete(file->f_dentry);
641 dput(file->f_dentry);
642 } else
643 pr_warn("efivarfs: inconsistent EFI variable implementation? "
644 "status=%zu\n", bytes);
645
646 bytes = count;
647
648out:
649 kfree(data);
650
651 return bytes;
652}
653
654static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
655 size_t count, loff_t *ppos)
656{
657 struct efivar_entry *var = file->private_data;
658 unsigned long datasize = 0;
659 u32 attributes;
660 void *data;
661 ssize_t size = 0;
662 int err;
663
664 err = efivar_entry_size(var, &datasize);
665 if (err)
666 return err;
667
668 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
669
670 if (!data)
671 return -ENOMEM;
672
673 size = efivar_entry_get(var, &attributes, &datasize,
674 data + sizeof(attributes));
675 if (size)
676 goto out_free;
677
678 memcpy(data, &attributes, sizeof(attributes));
679 size = simple_read_from_buffer(userbuf, count, ppos,
680 data, datasize + sizeof(attributes));
681out_free:
682 kfree(data);
683
684 return size;
685}
686
687static void efivarfs_evict_inode(struct inode *inode)
688{
689 clear_inode(inode);
690}
691
692static const struct super_operations efivarfs_ops = {
693 .statfs = simple_statfs,
694 .drop_inode = generic_delete_inode,
695 .evict_inode = efivarfs_evict_inode,
696 .show_options = generic_show_options,
697};
698
699static struct super_block *efivarfs_sb;
700
701static const struct inode_operations efivarfs_dir_inode_operations;
702
703static const struct file_operations efivarfs_file_operations = {
704 .open = efivarfs_file_open,
705 .read = efivarfs_file_read,
706 .write = efivarfs_file_write,
707 .llseek = no_llseek,
708};
709
710static struct inode *efivarfs_get_inode(struct super_block *sb,
711 const struct inode *dir, int mode, dev_t dev)
712{
713 struct inode *inode = new_inode(sb);
714
715 if (inode) {
716 inode->i_ino = get_next_ino();
717 inode->i_mode = mode;
718 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
719 switch (mode & S_IFMT) {
720 case S_IFREG:
721 inode->i_fop = &efivarfs_file_operations;
722 break;
723 case S_IFDIR:
724 inode->i_op = &efivarfs_dir_inode_operations;
725 inode->i_fop = &simple_dir_operations;
726 inc_nlink(inode);
727 break;
728 }
729 }
730 return inode;
731}
732
733/*
734 * Return true if 'str' is a valid efivarfs filename of the form,
735 *
736 * VariableName-12345678-1234-1234-1234-1234567891bc
737 */
738static bool efivarfs_valid_name(const char *str, int len)
739{
740 static const char dashes[EFI_VARIABLE_GUID_LEN] = {
741 [8] = 1, [13] = 1, [18] = 1, [23] = 1
742 };
743 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
744 int i;
745
746 /*
747 * We need a GUID, plus at least one letter for the variable name,
748 * plus the '-' separator
749 */
750 if (len < EFI_VARIABLE_GUID_LEN + 2)
751 return false;
752
753 /* GUID must be preceded by a '-' */
754 if (*(s - 1) != '-')
755 return false;
756
757 /*
758 * Validate that 's' is of the correct format, e.g.
759 *
760 * 12345678-1234-1234-1234-123456789abc
761 */
762 for (i = 0; i < EFI_VARIABLE_GUID_LEN; i++) {
763 if (dashes[i]) {
764 if (*s++ != '-')
765 return false;
766 } else {
767 if (!isxdigit(*s++))
768 return false;
769 }
770 }
771
772 return true;
773}
774
775static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
776{
777 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
778 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
779 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
780 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
781 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
782 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
783 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
784 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
785 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
786 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
787 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
788 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
789 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
790 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
791 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
792 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
793}
794
795static int efivarfs_create(struct inode *dir, struct dentry *dentry,
796 umode_t mode, bool excl)
797{
798 struct inode *inode;
799 struct efivar_entry *var;
800 int namelen, i = 0, err = 0;
801
802 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
803 return -EINVAL;
804
805 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
806 if (!inode)
807 return -ENOMEM;
808
809 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
810 if (!var) {
811 err = -ENOMEM;
812 goto out;
813 }
814
815 /* length of the variable name itself: remove GUID and separator */
816 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
817
818 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
819 &var->var.VendorGuid);
820
821 for (i = 0; i < namelen; i++)
822 var->var.VariableName[i] = dentry->d_name.name[i];
823
824 var->var.VariableName[i] = '\0';
825
826 inode->i_private = var;
827
828 efivar_entry_add(var, &efivarfs_list);
829 d_instantiate(dentry, inode);
830 dget(dentry);
831out:
832 if (err) {
833 kfree(var);
834 iput(inode);
835 }
836 return err;
837}
838
839static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
840{
841 struct efivar_entry *var = dentry->d_inode->i_private;
842
843 if (efivar_entry_delete(var))
844 return -EINVAL;
845
846 drop_nlink(dentry->d_inode);
847 dput(dentry);
848 return 0;
849};
850
851/*
852 * Compare two efivarfs file names.
853 *
854 * An efivarfs filename is composed of two parts,
855 *
856 * 1. A case-sensitive variable name
857 * 2. A case-insensitive GUID
858 *
859 * So we need to perform a case-sensitive match on part 1 and a
860 * case-insensitive match on part 2.
861 */
862static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
863 const struct dentry *dentry, const struct inode *inode,
864 unsigned int len, const char *str,
865 const struct qstr *name)
866{
867 int guid = len - EFI_VARIABLE_GUID_LEN;
868
869 if (name->len != len)
870 return 1;
871
872 /* Case-sensitive compare for the variable name */
873 if (memcmp(str, name->name, guid))
874 return 1;
875
876 /* Case-insensitive compare for the GUID */
877 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
878}
879
880static int efivarfs_d_hash(const struct dentry *dentry,
881 const struct inode *inode, struct qstr *qstr)
882{
883 unsigned long hash = init_name_hash();
884 const unsigned char *s = qstr->name;
885 unsigned int len = qstr->len;
886
887 if (!efivarfs_valid_name(s, len))
888 return -EINVAL;
889
890 while (len-- > EFI_VARIABLE_GUID_LEN)
891 hash = partial_name_hash(*s++, hash);
892
893 /* GUID is case-insensitive. */
894 while (len--)
895 hash = partial_name_hash(tolower(*s++), hash);
896
897 qstr->hash = end_name_hash(hash);
898 return 0;
899}
900
901/*
902 * Retaining negative dentries for an in-memory filesystem just wastes
903 * memory and lookup time: arrange for them to be deleted immediately.
904 */
905static int efivarfs_delete_dentry(const struct dentry *dentry)
906{
907 return 1;
908}
909
910static struct dentry_operations efivarfs_d_ops = {
911 .d_compare = efivarfs_d_compare,
912 .d_hash = efivarfs_d_hash,
913 .d_delete = efivarfs_delete_dentry,
914};
915
916static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
917{
918 struct dentry *d;
919 struct qstr q;
920 int err;
921
922 q.name = name;
923 q.len = strlen(name);
924
925 err = efivarfs_d_hash(NULL, NULL, &q);
926 if (err)
927 return ERR_PTR(err);
928
929 d = d_alloc(parent, &q);
930 if (d)
931 return d;
932
933 return ERR_PTR(-ENOMEM);
934}
935
936static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
937 unsigned long name_size, void *data)
938{
939 struct super_block *sb = (struct super_block *)data;
940 struct efivar_entry *entry;
941 struct inode *inode = NULL;
942 struct dentry *dentry, *root = sb->s_root;
943 unsigned long size = 0;
944 char *name;
945 int len, i;
946 int err = -ENOMEM;
947
948 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
949 if (!entry)
950 return err;
951
952 memcpy(entry->var.VariableName, name16, name_size);
953 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
954
955 len = utf16_strlen(entry->var.VariableName);
956
957 /* name, plus '-', plus GUID, plus NUL*/
958 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
959 if (!name)
960 goto fail;
961
962 for (i = 0; i < len; i++)
963 name[i] = entry->var.VariableName[i] & 0xFF;
964
965 name[len] = '-';
966
967 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
968
969 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
970
971 inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
972 if (!inode)
973 goto fail_name;
974
975 dentry = efivarfs_alloc_dentry(root, name);
976 if (IS_ERR(dentry)) {
977 err = PTR_ERR(dentry);
978 goto fail_inode;
979 }
980
981 /* copied by the above to local storage in the dentry. */
982 kfree(name);
983
984 efivar_entry_size(entry, &size);
985 efivar_entry_add(entry, &efivarfs_list);
986
987 mutex_lock(&inode->i_mutex);
988 inode->i_private = entry;
989 i_size_write(inode, size + sizeof(entry->var.Attributes));
990 mutex_unlock(&inode->i_mutex);
991 d_add(dentry, inode);
992
993 return 0;
994
995fail_inode:
996 iput(inode);
997fail_name:
998 kfree(name);
999fail:
1000 kfree(entry);
1001 return err;
1002}
1003
1004static int efivarfs_destroy(struct efivar_entry *entry, void *data)
1005{
1006 efivar_entry_remove(entry);
1007 kfree(entry);
1008 return 0;
1009}
1010
1011static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
1012{
1013 struct inode *inode = NULL;
1014 struct dentry *root;
1015 int err;
1016
1017 efivarfs_sb = sb;
1018
1019 sb->s_maxbytes = MAX_LFS_FILESIZE;
1020 sb->s_blocksize = PAGE_CACHE_SIZE;
1021 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1022 sb->s_magic = EFIVARFS_MAGIC;
1023 sb->s_op = &efivarfs_ops;
1024 sb->s_d_op = &efivarfs_d_ops;
1025 sb->s_time_gran = 1;
1026
1027 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
1028 if (!inode)
1029 return -ENOMEM;
1030 inode->i_op = &efivarfs_dir_inode_operations;
1031
1032 root = d_make_root(inode);
1033 sb->s_root = root;
1034 if (!root)
1035 return -ENOMEM;
1036
1037 INIT_LIST_HEAD(&efivarfs_list);
1038
1039 err = efivar_init(efivarfs_callback, (void *)sb, false,
1040 true, &efivarfs_list);
1041 if (err)
1042 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
1043
1044 return err;
1045}
1046
1047static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1048 int flags, const char *dev_name, void *data)
1049{
1050 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1051}
1052
1053static void efivarfs_kill_sb(struct super_block *sb)
1054{
1055 kill_litter_super(sb);
1056 efivarfs_sb = NULL;
1057
1058 /* Remove all entries and destroy */
1059 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
1060}
1061
1062static struct file_system_type efivarfs_type = {
1063 .name = "efivarfs",
1064 .mount = efivarfs_mount,
1065 .kill_sb = efivarfs_kill_sb,
1066};
1067MODULE_ALIAS_FS("efivarfs");
1068
1069/*
1070 * Handle negative dentry.
1071 */
1072static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1073 unsigned int flags)
1074{
1075 if (dentry->d_name.len > NAME_MAX)
1076 return ERR_PTR(-ENAMETOOLONG);
1077 d_add(dentry, NULL);
1078 return NULL;
1079}
1080
1081static const struct inode_operations efivarfs_dir_inode_operations = {
1082 .lookup = efivarfs_lookup,
1083 .unlink = efivarfs_unlink,
1084 .create = efivarfs_create,
1085};
1086
1087static ssize_t efivar_create(struct file *filp, struct kobject *kobj, 593static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
1088 struct bin_attribute *bin_attr, 594 struct bin_attribute *bin_attr,
1089 char *buf, loff_t pos, size_t count) 595 char *buf, loff_t pos, size_t count)
@@ -2164,8 +1670,6 @@ int efivars_register(struct efivars *efivars,
2164 1670
2165 __efivars = efivars; 1671 __efivars = efivars;
2166 1672
2167 register_filesystem(&efivarfs_type);
2168
2169 return 0; 1673 return 0;
2170} 1674}
2171EXPORT_SYMBOL_GPL(efivars_register); 1675EXPORT_SYMBOL_GPL(efivars_register);