From d5abc7c1050ab2b9556a4bf21626cd74e83cd086 Mon Sep 17 00:00:00 2001 From: Matt Fleming Date: Sun, 3 Feb 2013 19:53:46 +0000 Subject: efi: move utf16 string functions to efi.h There are currently two implementations of the utf16 string functions. Somewhat confusingly, they've got different names. Centralise the functions in efi.h. Reviewed-by: Tom Gundersen Tested-by: Tom Gundersen Reviewed-by: Mike Waychison Signed-off-by: Matt Fleming --- include/linux/efi.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'include') diff --git a/include/linux/efi.h b/include/linux/efi.h index 9bf2f1fcae2..d1d782a6d34 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -719,6 +719,23 @@ static inline void memrange_efi_to_native(u64 *addr, u64 *npages) *addr &= PAGE_MASK; } +/* Return the number of unicode characters in data */ +static inline unsigned long +utf16_strnlen(efi_char16_t *s, size_t maxlength) +{ + unsigned long length = 0; + + while (*s++ != 0 && length < maxlength) + length++; + return length; +} + +static inline unsigned long +utf16_strlen(efi_char16_t *s) +{ + return utf16_strnlen(s, ~0UL); +} + #if defined(CONFIG_EFI_VARS) || defined(CONFIG_EFI_VARS_MODULE) /* * EFI Variable support. -- cgit v1.2.3-18-g5258 From e14ab23dde12b80db4c94b684a2e485b72b16af3 Mon Sep 17 00:00:00 2001 From: Matt Fleming Date: Sun, 3 Feb 2013 20:16:40 +0000 Subject: efivars: efivar_entry API There isn't really a formal interface for dealing with EFI variables or struct efivar_entry. Historically, this has led to various bits of code directly accessing the generic EFI variable ops, which inherently ties it to specific EFI variable operations instead of indirectly using whatever ops were registered with register_efivars(). This lead to the efivarfs code only working with the generic EFI variable ops and not CONFIG_GOOGLE_SMI. Encapsulate everything that needs to access '__efivars' inside an efivar_entry_* API and use the new API in the pstore, sysfs and efivarfs code. Much of the efivars code had to be rewritten to use this new API. For instance, it is now up to the users of the API to build the initial list of EFI variables in their efivar_init() callback function. The variable list needs to be passed to efivar_init() which allows us to keep work arounds for things like implementation bugs in GetNextVariable() in a central location. Allowing users of the API to use a callback function to build the list greatly benefits the efivarfs code which needs to allocate inodes and dentries for every variable. It previously did this in a racy way because the code ran without holding the variable spinlock. Both the sysfs and efivarfs code maintain their own lists which means the two interfaces can be running simultaneously without interference, though it should be noted that because no synchronisation is performed it is very easy to create inconsistencies. efibootmgr doesn't currently use efivarfs and users are likely to also require the old sysfs interface, so it makes sense to allow both to be built. Reviewed-by: Tom Gundersen Tested-by: Tom Gundersen Cc: Seiji Aguchi Cc: Matthew Garrett Cc: Jeremy Kerr Cc: Tony Luck Cc: Mike Waychison Signed-off-by: Matt Fleming --- include/linux/efi.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/linux/efi.h b/include/linux/efi.h index d1d782a6d34..cd561b301e6 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -662,6 +662,12 @@ static inline int efi_enabled(int facility) EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \ EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | \ EFI_VARIABLE_APPEND_WRITE) +/* + * Length of a GUID string (strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")) + * not including trailing NUL + */ +#define EFI_VARIABLE_GUID_LEN 36 + /* * The type of search to perform when calling boottime->locate_handle */ @@ -762,19 +768,75 @@ struct efivars { * which is protected by the BKL, so that path is safe. */ spinlock_t lock; - struct list_head list; struct kset *kset; struct kobject *kobject; - struct bin_attribute *new_var, *del_var; const struct efivar_operations *ops; - struct efivar_entry *walk_entry; - struct pstore_info efi_pstore_info; }; -int register_efivars(struct efivars *efivars, +/* + * The maximum size of VariableName + Data = 1024 + * Therefore, it's reasonable to save that much + * space in each part of the structure, + * and we use a page for reading/writing. + */ + +struct efi_variable { + efi_char16_t VariableName[1024/sizeof(efi_char16_t)]; + efi_guid_t VendorGuid; + unsigned long DataSize; + __u8 Data[1024]; + efi_status_t Status; + __u32 Attributes; +} __attribute__((packed)); + +struct efivar_entry { + struct efi_variable var; + struct list_head list; + struct kobject kobj; +}; + +int efivars_register(struct efivars *efivars, const struct efivar_operations *ops, - struct kobject *parent_kobj); -void unregister_efivars(struct efivars *efivars); + struct kobject *kobject); +int efivars_unregister(struct efivars *efivars); +struct kobject *efivars_kobject(void); + +int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *), + void *data, bool atomic, bool duplicates, + struct list_head *head); + +void efivar_entry_add(struct efivar_entry *entry, struct list_head *head); +void efivar_entry_remove(struct efivar_entry *entry); + +int __efivar_entry_delete(struct efivar_entry *entry); +int efivar_entry_delete(struct efivar_entry *entry); + +int __efivar_entry_size(struct efivar_entry *entry, unsigned long *size); +int efivar_entry_size(struct efivar_entry *entry, unsigned long *size); +int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, + unsigned long *size, void *data); +int efivar_entry_set(struct efivar_entry *entry, u32 attributes, + unsigned long size, void *data, struct list_head *head); +int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, + unsigned long *size, void *data, bool *set); +int efivar_entry_set_safe(efi_char16_t *name, efi_guid_t vendor, u32 attributes, + bool block, unsigned long size, void *data); + +void efivar_entry_iter_begin(void); +void efivar_entry_iter_end(void); + +int __efivar_entry_iter(int (*func)(struct efivar_entry *, void *), + struct list_head *head, void *data, + struct efivar_entry **prev); +int efivar_entry_iter(int (*func)(struct efivar_entry *, void *), + struct list_head *head, void *data); + +struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, + struct list_head *head, bool remove); + +bool efivar_validate(struct efi_variable *var, u8 *data, unsigned long len); + +int efivars_sysfs_init(void); #endif /* CONFIG_EFI_VARS */ -- cgit v1.2.3-18-g5258 From 048517722cde2595a7366d0c3c72b8b1ec142a9c Mon Sep 17 00:00:00 2001 From: Matt Fleming Date: Fri, 8 Feb 2013 15:48:51 +0000 Subject: efivars: Move pstore code into the new EFI directory efivars.c has grown far too large and needs to be divided up. Create a new directory and move the persistence storage code to efi-pstore.c now that it uses the new efivar API. This helps us to greatly reduce the size of efivars.c and paves the way for moving other code out of efivars.c. Note that because CONFIG_EFI_VARS can be built as a module efi-pstore must also include support for building as a module. Reviewed-by: Tom Gundersen Tested-by: Tom Gundersen Cc: Seiji Aguchi Cc: Anton Vorontsov Cc: Colin Cross Cc: Kees Cook Cc: Matthew Garrett Cc: Tony Luck Signed-off-by: Matt Fleming --- include/linux/efi.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'include') diff --git a/include/linux/efi.h b/include/linux/efi.h index cd561b301e6..8ff6ec1ac04 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -743,6 +743,34 @@ utf16_strlen(efi_char16_t *s) } #if defined(CONFIG_EFI_VARS) || defined(CONFIG_EFI_VARS_MODULE) +/* + * Return the number of bytes is the length of this string + * Note: this is NOT the same as the number of unicode characters + */ +static inline unsigned long +utf16_strsize(efi_char16_t *data, unsigned long maxlength) +{ + return utf16_strnlen(data, maxlength/sizeof(efi_char16_t)) * sizeof(efi_char16_t); +} + +static inline int +utf16_strncmp(const efi_char16_t *a, const efi_char16_t *b, size_t len) +{ + while (1) { + if (len == 0) + return 0; + if (*a < *b) + return -1; + if (*a > *b) + return 1; + if (*a == 0) /* implies *b == 0 */ + return 0; + a++; + b++; + len--; + } +} + /* * EFI Variable support. * @@ -795,6 +823,14 @@ struct efivar_entry { struct kobject kobj; }; +extern struct list_head efivar_sysfs_list; + +static inline void +efivar_unregister(struct efivar_entry *var) +{ + kobject_put(&var->kobj); +} + int efivars_register(struct efivars *efivars, const struct efivar_operations *ops, struct kobject *kobject); @@ -836,6 +872,8 @@ struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, bool efivar_validate(struct efi_variable *var, u8 *data, unsigned long len); +void efivar_run_worker(void); + int efivars_sysfs_init(void); #endif /* CONFIG_EFI_VARS */ -- cgit v1.2.3-18-g5258 From a9499fa7cd3fd4824a7202d00c766b269fa3bda6 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Fri, 8 Feb 2013 15:37:06 +0000 Subject: efi: split efisubsystem from efivars This registers /sys/firmware/efi/{,systab,efivars/} whenever EFI is enabled and the system is booted with EFI. This allows *) userspace to check for the existence of /sys/firmware/efi as a way to determine whether or it is running on an EFI system. *) 'mount -t efivarfs none /sys/firmware/efi/efivars' without manually loading any modules. [ Also, move the efivar API into vars.c and unconditionally compile it. This allows us to move efivars.c, which now only contains the sysfs variable code, into the firmware/efi directory. Note that the efivars.c filename is kept to maintain backwards compatability with the old efivars.ko module. With this patch it is now possible for efivarfs to be built without CONFIG_EFI_VARS - Matt ] Cc: Seiji Aguchi Cc: Tony Luck Cc: Mike Waychison Cc: Kay Sievers Cc: Jeremy Kerr Cc: Matthew Garrett Cc: Chun-Yi Lee Cc: Andy Whitcroft Cc: Tobias Powalowski Signed-off-by: Tom Gundersen Signed-off-by: Matt Fleming --- include/linux/efi.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/linux/efi.h b/include/linux/efi.h index 8ff6ec1ac04..2fc81668271 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -742,7 +742,6 @@ utf16_strlen(efi_char16_t *s) return utf16_strnlen(s, ~0UL); } -#if defined(CONFIG_EFI_VARS) || defined(CONFIG_EFI_VARS_MODULE) /* * Return the number of bytes is the length of this string * Note: this is NOT the same as the number of unicode characters @@ -872,8 +871,10 @@ struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, bool efivar_validate(struct efi_variable *var, u8 *data, unsigned long len); +extern struct work_struct efivar_work; void efivar_run_worker(void); +#if defined(CONFIG_EFI_VARS) || defined(CONFIG_EFI_VARS_MODULE) int efivars_sysfs_init(void); #endif /* CONFIG_EFI_VARS */ -- cgit v1.2.3-18-g5258 From 8a415b8c05f261a52f45f2271b6c4731376fd5b5 Mon Sep 17 00:00:00 2001 From: Matt Fleming Date: Mon, 29 Apr 2013 20:08:02 +0100 Subject: efi, pstore: Read data from variable store before memcpy() Seiji reported getting empty dmesg-* files, because the data was never actually read in efi_pstore_read_func(), and so the memcpy() was copying garbage data. This patch necessitated adding __efivar_entry_get() which is callable between efivar_entry_iter_{begin,end}(). We can also delete __efivar_entry_size() because efi_pstore_read_func() was the only caller. Reported-by: Seiji Aguchi Tested-by: Seiji Aguchi Cc: Tony Luck Cc: Matthew Garrett Signed-off-by: Matt Fleming --- include/linux/efi.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/linux/efi.h b/include/linux/efi.h index 3f7257f1f5e..2bc0ad78d05 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -808,8 +808,9 @@ void efivar_entry_remove(struct efivar_entry *entry); int __efivar_entry_delete(struct efivar_entry *entry); int efivar_entry_delete(struct efivar_entry *entry); -int __efivar_entry_size(struct efivar_entry *entry, unsigned long *size); int efivar_entry_size(struct efivar_entry *entry, unsigned long *size); +int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes, + unsigned long *size, void *data); int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, unsigned long *size, void *data); int efivar_entry_set(struct efivar_entry *entry, u32 attributes, -- cgit v1.2.3-18-g5258