// SPDX-License-Identifier: GPL-2.0+ /* * Originally from efivars.c * * Copyright (C) 2001,2003,2004 Dell <[email protected]> * Copyright (C) 2004 Intel Corporation <[email protected]> */ #include <linux/capability.h> #include <linux/types.h> #include <linux/errno.h> #include <linux/init.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/string.h> #include <linux/smp.h> #include <linux/efi.h> #include <linux/device.h> #include <linux/slab.h> #include <linux/ctype.h> #include <linux/ucs2_string.h> #include "internal.h" MODULE_IMPORT_NS(…); static bool validate_device_path(efi_char16_t *var_name, int match, u8 *buffer, unsigned long len) { … } static bool validate_boot_order(efi_char16_t *var_name, int match, u8 *buffer, unsigned long len) { … } static bool validate_load_option(efi_char16_t *var_name, int match, u8 *buffer, unsigned long len) { … } static bool validate_uint16(efi_char16_t *var_name, int match, u8 *buffer, unsigned long len) { … } static bool validate_ascii_string(efi_char16_t *var_name, int match, u8 *buffer, unsigned long len) { … } struct variable_validate { … }; /* * This is the list of variables we need to validate, as well as the * whitelist for what we think is safe not to default to immutable. * * If it has a validate() method that's not NULL, it'll go into the * validation routine. If not, it is assumed valid, but still used for * whitelisting. * * Note that it's sorted by {vendor,name}, but globbed names must come after * any other name with the same prefix. */ static const struct variable_validate variable_validate[] = …; /* * Check if @var_name matches the pattern given in @match_name. * * @var_name: an array of @len non-NUL characters. * @match_name: a NUL-terminated pattern string, optionally ending in "*". A * final "*" character matches any trailing characters @var_name, * including the case when there are none left in @var_name. * @match: on output, the number of non-wildcard characters in @match_name * that @var_name matches, regardless of the return value. * @return: whether @var_name fully matches @match_name. */ static bool variable_matches(const char *var_name, size_t len, const char *match_name, int *match) { … } bool efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data, unsigned long data_size) { … } bool efivar_variable_is_removable(efi_guid_t vendor, const char *var_name, size_t len) { … } static bool variable_is_present(efi_char16_t *variable_name, efi_guid_t *vendor, struct list_head *head) { … } /* * Returns the size of variable_name, in bytes, including the * terminating NULL character, or variable_name_size if no NULL * character is found among the first variable_name_size bytes. */ static unsigned long var_name_strnsize(efi_char16_t *variable_name, unsigned long variable_name_size) { … } /* * Print a warning when duplicate EFI variables are encountered and * disable the sysfs workqueue since the firmware is buggy. */ static void dup_variable_bug(efi_char16_t *str16, efi_guid_t *vendor_guid, unsigned long len16) { … } /** * efivar_init - build the initial list of EFI variables * @func: callback function to invoke for every variable * @data: function-specific data to pass to @func * @head: initialised head of variable list * * Get every EFI variable from the firmware and invoke @func. @func * should call efivar_entry_add() to build the list of variables. * * Returns 0 on success, or a kernel error code on failure. */ int efivar_init(int (*func)(efi_char16_t *, efi_guid_t, unsigned long, void *, struct list_head *), void *data, struct list_head *head) { … } /** * efivar_entry_add - add entry to variable list * @entry: entry to add to list * @head: list head * * Returns 0 on success, or a kernel error code on failure. */ int efivar_entry_add(struct efivar_entry *entry, struct list_head *head) { … } /** * __efivar_entry_add - add entry to variable list * @entry: entry to add to list * @head: list head */ void __efivar_entry_add(struct efivar_entry *entry, struct list_head *head) { … } /** * efivar_entry_remove - remove entry from variable list * @entry: entry to remove from list * * Returns 0 on success, or a kernel error code on failure. */ void efivar_entry_remove(struct efivar_entry *entry) { … } /* * efivar_entry_list_del_unlock - remove entry from variable list * @entry: entry to remove * * Remove @entry from the variable list and release the list lock. * * NOTE: slightly weird locking semantics here - we expect to be * called with the efivars lock already held, and we release it before * returning. This is because this function is usually called after * set_variable() while the lock is still held. */ static void efivar_entry_list_del_unlock(struct efivar_entry *entry) { … } /** * efivar_entry_delete - delete variable and remove entry from list * @entry: entry containing variable to delete * * Delete the variable from the firmware and remove @entry from the * variable list. It is the caller's responsibility to free @entry * once we return. * * Returns 0 on success, -EINTR if we can't grab the semaphore, * converted EFI status code if set_variable() fails. */ int efivar_entry_delete(struct efivar_entry *entry) { … } /** * efivar_entry_size - obtain the size of a variable * @entry: entry for this variable * @size: location to store the variable's size */ int efivar_entry_size(struct efivar_entry *entry, unsigned long *size) { … } /** * __efivar_entry_get - call get_variable() * @entry: read data for this variable * @attributes: variable attributes * @size: size of @data buffer * @data: buffer to store variable data * * The caller MUST call efivar_entry_iter_begin() and * efivar_entry_iter_end() before and after the invocation of this * function, respectively. */ int __efivar_entry_get(struct efivar_entry *entry, u32 *attributes, unsigned long *size, void *data) { … } /** * efivar_entry_get - call get_variable() * @entry: read data for this variable * @attributes: variable attributes * @size: size of @data buffer * @data: buffer to store variable data */ int efivar_entry_get(struct efivar_entry *entry, u32 *attributes, unsigned long *size, void *data) { … } /** * efivar_entry_set_get_size - call set_variable() and get new size (atomic) * @entry: entry containing variable to set and get * @attributes: attributes of variable to be written * @size: size of data buffer * @data: buffer containing data to write * @set: did the set_variable() call succeed? * * This is a pretty special (complex) function. See efivarfs_file_write(). * * Atomically call set_variable() for @entry and if the call is * successful, return the new size of the variable from get_variable() * in @size. The success of set_variable() is indicated by @set. * * Returns 0 on success, -EINVAL if the variable data is invalid, * -ENOSPC if the firmware does not have enough available space, or a * converted EFI status code if either of set_variable() or * get_variable() fail. * * If the EFI variable does not exist when calling set_variable() * (EFI_NOT_FOUND), @entry is removed from the variable list. */ int efivar_entry_set_get_size(struct efivar_entry *entry, u32 attributes, unsigned long *size, void *data, bool *set) { … } /** * efivar_entry_iter - iterate over variable list * @func: callback function * @head: head of variable list * @data: function-specific data to pass to callback * * Iterate over the list of EFI variables and call @func with every * entry on the list. It is safe for @func to remove entries in the * list via efivar_entry_delete() while iterating. * * Some notes for the callback function: * - a non-zero return value indicates an error and terminates the loop * - @func is called from atomic context */ int efivar_entry_iter(int (*func)(struct efivar_entry *, void *), struct list_head *head, void *data) { … }