/* * firmware_class.c - Multi purpose firmware loading support * * Copyright (c) 2003 Manuel Estrada Sainz * * Please see Documentation/firmware_class/ for more information. * */ #include <linux/capability.h>#include <linux/device.h>#include <linux/module.h>#include <linux/init.h>#include <linux/timer.h>#include <linux/vmalloc.h>#include <linux/interrupt.h>#include <linux/bitops.h>#include <linux/mutex.h>#include <linux/workqueue.h>#include <linux/highmem.h>#include <linux/firmware.h>#include <linux/slab.h>#include <linux/sched.h>#include <linux/file.h>#include <linux/list.h>#include <linux/async.h>#include <linux/pm.h>#include <linux/suspend.h>#include <linux/syscore_ops.h>#include <linux/reboot.h>#include <linux/security.h> #include <generated/utsrelease.h> #include "base.h" MODULE_AUTHOR("Manuel Estrada Sainz");MODULE_DESCRIPTION("Multi purpose firmware loading support");MODULE_LICENSE("GPL"); /* Builtin firmware support */ #ifdef CONFIG_FW_LOADER extern struct builtin_fw __start_builtin_fw[];extern struct builtin_fw __end_builtin_fw[]; static bool fw_get_builtin_firmware(struct firmware *fw, const char *name){ struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { if (strcmp(name, b_fw->name) == 0) { fw->size = b_fw->size; fw->data = b_fw->data; return true; } } return false;} static bool fw_is_builtin_firmware(const struct firmware *fw){ struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) if (fw->data == b_fw->data) return true; return false;} #else /* Module case - no builtin firmware support */ static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name){ return false;} static inline bool fw_is_builtin_firmware(const struct firmware *fw){ return false;}#endif enum { FW_STATUS_LOADING, FW_STATUS_DONE, FW_STATUS_ABORT,}; static int loading_timeout = 60; /* In seconds */ static inline long firmware_loading_timeout(void){ return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET;} /* firmware behavior options */#define FW_OPT_UEVENT (1U << 0)#define FW_OPT_NOWAIT (1U << 1)#ifdef CONFIG_FW_LOADER_USER_HELPER#define FW_OPT_USERHELPER (1U << 2)#else#define FW_OPT_USERHELPER 0#endif#ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK#define FW_OPT_FALLBACK FW_OPT_USERHELPER#else#define FW_OPT_FALLBACK 0#endif#define FW_OPT_NO_WARN (1U << 3) struct firmware_cache { /* firmware_buf instance will be added into the below list */ spinlock_t lock; struct list_head head; int state; #ifdef CONFIG_PM_SLEEP /* * Names of firmware images which have been cached successfully * will be added into the below list so that device uncache * helper can trace which firmware images have been cached * before. */ spinlock_t name_lock; struct list_head fw_names; struct delayed_work work; struct notifier_block pm_notify;#endif}; struct firmware_buf { struct kref ref; struct list_head list; struct completion completion; struct firmware_cache *fwc; unsigned long status; void *data; size_t size;#ifdef CONFIG_FW_LOADER_USER_HELPER bool is_paged_buf; bool need_uevent; struct page **pages; int nr_pages; int page_array_size; struct list_head pending_list;#endif const char *fw_id;}; struct fw_cache_entry { struct list_head list; const char *name;}; struct fw_name_devm { unsigned long magic; const char *name;}; #define to_fwbuf(d) container_of(d, struct firmware_buf, ref) #define FW_LOADER_NO_CACHE 0#define FW_LOADER_START_CACHE 1 static int fw_cache_piggyback_on_request(const char *name); /* fw_lock could be moved to 'struct firmware_priv' but since it is just * guarding for corner cases a global lock should be OK */static DEFINE_MUTEX(fw_lock); static struct firmware_cache fw_cache; static struct firmware_buf *__allocate_fw_buf(const char *fw_name, struct firmware_cache *fwc){ struct firmware_buf *buf; buf = kzalloc(sizeof(*buf), GFP_ATOMIC); if (!buf) return NULL; buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC); if (!buf->fw_id) { kfree(buf); return NULL; } kref_init(&buf->ref); buf->fwc = fwc; init_completion(&buf->completion);#ifdef CONFIG_FW_LOADER_USER_HELPER INIT_LIST_HEAD(&buf->pending_list);#endif pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf); return buf;} static struct firmware_buf *__fw_lookup_buf(const char *fw_name){ struct firmware_buf *tmp; struct firmware_cache *fwc = &fw_cache; list_for_each_entry(tmp, &fwc->head, list) if (!strcmp(tmp->fw_id, fw_name)) return tmp; return NULL;} static int fw_lookup_and_allocate_buf(const char *fw_name, struct firmware_cache *fwc, struct firmware_buf **buf){ struct firmware_buf *tmp; spin_lock(&fwc->lock); tmp = __fw_lookup_buf(fw_name); if (tmp) { kref_get(&tmp->ref); spin_unlock(&fwc->lock); *buf = tmp; return 1; } tmp = __allocate_fw_buf(fw_name, fwc); if (tmp) list_add(&tmp->list, &fwc->head); spin_unlock(&fwc->lock); *buf = tmp; return tmp ? 0 : -ENOMEM;} static void __fw_free_buf(struct kref *ref) __releases(&fwc->lock){ struct firmware_buf *buf = to_fwbuf(ref); struct firmware_cache *fwc = buf->fwc; pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", __func__, buf->fw_id, buf, buf->data, (unsigned int)buf->size); list_del(&buf->list); spin_unlock(&fwc->lock); #ifdef CONFIG_FW_LOADER_USER_HELPER if (buf->is_paged_buf) { int i; vunmap(buf->data); for (i = 0; i < buf->nr_pages; i++) __free_page(buf->pages[i]); kfree(buf->pages); } else#endif vfree(buf->data); kfree_const(buf->fw_id); kfree(buf);} static void fw_free_buf(struct firmware_buf *buf){ struct firmware_cache *fwc = buf->fwc; spin_lock(&fwc->lock); if (!kref_put(&buf->ref, __fw_free_buf)) spin_unlock(&fwc->lock);} /* direct firmware loading support */static char fw_path_para[256];static const char * const fw_path[] = { fw_path_para, "/lib/firmware/updates/" UTS_RELEASE, "/lib/firmware/updates", "/lib/firmware/" UTS_RELEASE, "/lib/firmware"}; /* * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH' * from kernel command line because firmware_class is generally built in * kernel instead of module. */module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644);MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path"); static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf){ int size; char *buf; int rc; if (!S_ISREG(file_inode(file)->i_mode)) return -EINVAL; size = i_size_read(file_inode(file)); if (size <= 0) return -EINVAL; buf = vmalloc(size); if (!buf) return -ENOMEM; rc = kernel_read(file, 0, buf, size); if (rc != size) { if (rc > 0) rc = -EIO; goto fail; } rc = security_kernel_fw_from_file(file, buf, size); if (rc) goto fail; fw_buf->data = buf; fw_buf->size = size; return 0;fail: vfree(buf); return rc;} static int fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf){ int i, len; int rc = -ENOENT; char *path; path = __getname(); if (!path) return -ENOMEM; for (i = 0; i < ARRAY_SIZE(fw_path); i++) { struct file *file; /* skip the unset customized path */ if (!fw_path[i][0]) continue; len = snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); if (len >= PATH_MAX) { rc = -ENAMETOOLONG; break; } file = filp_open(path, O_RDONLY, 0); if (IS_ERR(file)) continue; rc = fw_read_file_contents(file, buf); fput(file); if (rc) dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n", path, rc); else break; } __putname(path); if (!rc) { dev_dbg(device, "firmware: direct-loading firmware %s\n", buf->fw_id); mutex_lock(&fw_lock); set_bit(FW_STATUS_DONE, &buf->status); complete_all(&buf->completion); mutex_unlock(&fw_lock); } return rc;} /* firmware holds the ownership of pages */static void firmware_free_data(const struct firmware *fw){ /* Loaded directly? */ if (!fw->priv) { vfree(fw->data); return; } fw_free_buf(fw->priv);} /* store the pages buffer info firmware from buf */static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw){ fw->priv = buf;#ifdef CONFIG_FW_LOADER_USER_HELPER fw->pages = buf->pages;#endif fw->size = buf->size; fw->data = buf->data; pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", __func__, buf->fw_id, buf, buf->data, (unsigned int)buf->size);} #ifdef CONFIG_PM_SLEEPstatic void fw_name_devm_release(struct device *dev, void *res){ struct fw_name_devm *fwn = res; if (fwn->magic == (unsigned long)&fw_cache) pr_debug("%s: fw_name-%s devm-%p released\n", __func__, fwn->name, res); kfree_const(fwn->name);} static int fw_devm_match(struct device *dev, void *res, void *match_data){ struct fw_name_devm *fwn = res; return (fwn->magic == (unsigned long)&fw_cache) && !strcmp(fwn->name, match_data);} static struct fw_name_devm *fw_find_devm_name(struct device *dev, const char *name){ struct fw_name_devm *fwn; fwn = devres_find(dev, fw_name_devm_release, fw_devm_match, (void *)name); return fwn;} /* add firmware name into devres list */static int fw_add_devm_name(struct device *dev, const char *name){ struct fw_name_devm *fwn; fwn = fw_find_devm_name(dev, name); if (fwn) return 1; fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm), GFP_KERNEL); if (!fwn) return -ENOMEM; fwn->name = kstrdup_const(name, GFP_KERNEL); if (!fwn->name) { kfree(fwn); return -ENOMEM; } fwn->magic = (unsigned long)&fw_cache; devres_add(dev, fwn); return 0;}#elsestatic int fw_add_devm_name(struct device *dev, const char *name){ return 0;}#endif /* * user-mode helper code */#ifdef CONFIG_FW_LOADER_USER_HELPERstruct firmware_priv { bool nowait; struct device dev; struct firmware_buf *buf; struct firmware *fw;}; static struct firmware_priv *to_firmware_priv(struct device *dev){ return container_of(dev, struct firmware_priv, dev);} static void __fw_load_abort(struct firmware_buf *buf){ /* * There is a small window in which user can write to 'loading' * between loading done and disappearance of 'loading' */ if (test_bit(FW_STATUS_DONE, &buf->status)) return; list_del_init(&buf->pending_list); set_bit(FW_STATUS_ABORT, &buf->status); complete_all(&buf->completion);} static void fw_load_abort(struct firmware_priv *fw_priv){ struct firmware_buf *buf = fw_priv->buf; __fw_load_abort(buf); /* avoid user action after loading abort */ fw_priv->buf = NULL;Lines 1–500 of 1727. Load more
/* * firmware_class.c - Multi purpose firmware loading support * * Copyright (c) 2003 Manuel Estrada Sainz * * Please see Documentation/firmware_class/ for more information. * */ #include <linux/capability.h> #include <linux/device.h> #include <linux/module.h> #include <linux/init.h> #include <linux/timer.h> #include <linux/vmalloc.h> #include <linux/interrupt.h> #include <linux/bitops.h> #include <linux/mutex.h> #include <linux/workqueue.h> #include <linux/highmem.h> #include <linux/firmware.h> #include <linux/slab.h> #include <linux/sched.h> #include <linux/file.h> #include <linux/list.h> #include <linux/async.h> #include <linux/pm.h> #include <linux/suspend.h> #include <linux/syscore_ops.h> #include <linux/reboot.h> #include <linux/security.h> #include <generated/utsrelease.h> #include "base.h" MODULE_AUTHOR("Manuel Estrada Sainz"); MODULE_DESCRIPTION("Multi purpose firmware loading support"); MODULE_LICENSE("GPL"); /* Builtin firmware support */ #ifdef CONFIG_FW_LOADER extern struct builtin_fw __start_builtin_fw[]; extern struct builtin_fw __end_builtin_fw[]; static bool fw_get_builtin_firmware(struct firmware *fw, const char *name) { struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { if (strcmp(name, b_fw->name) == 0) { fw->size = b_fw->size; fw->data = b_fw->data; return true; } } return false; } static bool fw_is_builtin_firmware(const struct firmware *fw) { struct builtin_fw *b_fw; for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) if (fw->data == b_fw->data) return true; return false; } #else /* Module case - no builtin firmware support */ static inline bool fw_get_builtin_firmware(struct firmware *fw, const char *name) { return false; } static inline bool fw_is_builtin_firmware(const struct firmware *fw) { return false; } #endif enum { FW_STATUS_LOADING, FW_STATUS_DONE, FW_STATUS_ABORT, }; static int loading_timeout = 60; /* In seconds */ static inline long firmware_loading_timeout(void) { return loading_timeout > 0 ? loading_timeout * HZ : MAX_JIFFY_OFFSET; } /* firmware behavior options */ #define FW_OPT_UEVENT (1U << 0) #define FW_OPT_NOWAIT (1U << 1) #ifdef CONFIG_FW_LOADER_USER_HELPER #define FW_OPT_USERHELPER (1U << 2) #else #define FW_OPT_USERHELPER 0 #endif #ifdef CONFIG_FW_LOADER_USER_HELPER_FALLBACK #define FW_OPT_FALLBACK FW_OPT_USERHELPER #else #define FW_OPT_FALLBACK 0 #endif #define FW_OPT_NO_WARN (1U << 3) struct firmware_cache { /* firmware_buf instance will be added into the below list */ spinlock_t lock; struct list_head head; int state; #ifdef CONFIG_PM_SLEEP /* * Names of firmware images which have been cached successfully * will be added into the below list so that device uncache * helper can trace which firmware images have been cached * before. */ spinlock_t name_lock; struct list_head fw_names; struct delayed_work work; struct notifier_block pm_notify; #endif }; struct firmware_buf { struct kref ref; struct list_head list; struct completion completion; struct firmware_cache *fwc; unsigned long status; void *data; size_t size; #ifdef CONFIG_FW_LOADER_USER_HELPER bool is_paged_buf; bool need_uevent; struct page **pages; int nr_pages; int page_array_size; struct list_head pending_list; #endif const char *fw_id; }; struct fw_cache_entry { struct list_head list; const char *name; }; struct fw_name_devm { unsigned long magic; const char *name; }; #define to_fwbuf(d) container_of(d, struct firmware_buf, ref) #define FW_LOADER_NO_CACHE 0 #define FW_LOADER_START_CACHE 1 static int fw_cache_piggyback_on_request(const char *name); /* fw_lock could be moved to 'struct firmware_priv' but since it is just * guarding for corner cases a global lock should be OK */ static DEFINE_MUTEX(fw_lock); static struct firmware_cache fw_cache; static struct firmware_buf *__allocate_fw_buf(const char *fw_name, struct firmware_cache *fwc) { struct firmware_buf *buf; buf = kzalloc(sizeof(*buf), GFP_ATOMIC); if (!buf) return NULL; buf->fw_id = kstrdup_const(fw_name, GFP_ATOMIC); if (!buf->fw_id) { kfree(buf); return NULL; } kref_init(&buf->ref); buf->fwc = fwc; init_completion(&buf->completion); #ifdef CONFIG_FW_LOADER_USER_HELPER INIT_LIST_HEAD(&buf->pending_list); #endif pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf); return buf; } static struct firmware_buf *__fw_lookup_buf(const char *fw_name) { struct firmware_buf *tmp; struct firmware_cache *fwc = &fw_cache; list_for_each_entry(tmp, &fwc->head, list) if (!strcmp(tmp->fw_id, fw_name)) return tmp; return NULL; } static int fw_lookup_and_allocate_buf(const char *fw_name, struct firmware_cache *fwc, struct firmware_buf **buf) { struct firmware_buf *tmp; spin_lock(&fwc->lock); tmp = __fw_lookup_buf(fw_name); if (tmp) { kref_get(&tmp->ref); spin_unlock(&fwc->lock); *buf = tmp; return 1; } tmp = __allocate_fw_buf(fw_name, fwc); if (tmp) list_add(&tmp->list, &fwc->head); spin_unlock(&fwc->lock); *buf = tmp; return tmp ? 0 : -ENOMEM; } static void __fw_free_buf(struct kref *ref) __releases(&fwc->lock) { struct firmware_buf *buf = to_fwbuf(ref); struct firmware_cache *fwc = buf->fwc; pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", __func__, buf->fw_id, buf, buf->data, (unsigned int)buf->size); list_del(&buf->list); spin_unlock(&fwc->lock); #ifdef CONFIG_FW_LOADER_USER_HELPER if (buf->is_paged_buf) { int i; vunmap(buf->data); for (i = 0; i < buf->nr_pages; i++) __free_page(buf->pages[i]); kfree(buf->pages); } else #endif vfree(buf->data); kfree_const(buf->fw_id); kfree(buf); } static void fw_free_buf(struct firmware_buf *buf) { struct firmware_cache *fwc = buf->fwc; spin_lock(&fwc->lock); if (!kref_put(&buf->ref, __fw_free_buf)) spin_unlock(&fwc->lock); } /* direct firmware loading support */ static char fw_path_para[256]; static const char * const fw_path[] = { fw_path_para, "/lib/firmware/updates/" UTS_RELEASE, "/lib/firmware/updates", "/lib/firmware/" UTS_RELEASE, "/lib/firmware" }; /* * Typical usage is that passing 'firmware_class.path=$CUSTOMIZED_PATH' * from kernel command line because firmware_class is generally built in * kernel instead of module. */ module_param_string(path, fw_path_para, sizeof(fw_path_para), 0644); MODULE_PARM_DESC(path, "customized firmware image search path with a higher priority than default path"); static int fw_read_file_contents(struct file *file, struct firmware_buf *fw_buf) { int size; char *buf; int rc; if (!S_ISREG(file_inode(file)->i_mode)) return -EINVAL; size = i_size_read(file_inode(file)); if (size <= 0) return -EINVAL; buf = vmalloc(size); if (!buf) return -ENOMEM; rc = kernel_read(file, 0, buf, size); if (rc != size) { if (rc > 0) rc = -EIO; goto fail; } rc = security_kernel_fw_from_file(file, buf, size); if (rc) goto fail; fw_buf->data = buf; fw_buf->size = size; return 0; fail: vfree(buf); return rc; } static int fw_get_filesystem_firmware(struct device *device, struct firmware_buf *buf) { int i, len; int rc = -ENOENT; char *path; path = __getname(); if (!path) return -ENOMEM; for (i = 0; i < ARRAY_SIZE(fw_path); i++) { struct file *file; /* skip the unset customized path */ if (!fw_path[i][0]) continue; len = snprintf(path, PATH_MAX, "%s/%s", fw_path[i], buf->fw_id); if (len >= PATH_MAX) { rc = -ENAMETOOLONG; break; } file = filp_open(path, O_RDONLY, 0); if (IS_ERR(file)) continue; rc = fw_read_file_contents(file, buf); fput(file); if (rc) dev_warn(device, "firmware, attempted to load %s, but failed with error %d\n", path, rc); else break; } __putname(path); if (!rc) { dev_dbg(device, "firmware: direct-loading firmware %s\n", buf->fw_id); mutex_lock(&fw_lock); set_bit(FW_STATUS_DONE, &buf->status); complete_all(&buf->completion); mutex_unlock(&fw_lock); } return rc; } /* firmware holds the ownership of pages */ static void firmware_free_data(const struct firmware *fw) { /* Loaded directly? */ if (!fw->priv) { vfree(fw->data); return; } fw_free_buf(fw->priv); } /* store the pages buffer info firmware from buf */ static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw) { fw->priv = buf; #ifdef CONFIG_FW_LOADER_USER_HELPER fw->pages = buf->pages; #endif fw->size = buf->size; fw->data = buf->data; pr_debug("%s: fw-%s buf=%p data=%p size=%u\n", __func__, buf->fw_id, buf, buf->data, (unsigned int)buf->size); } #ifdef CONFIG_PM_SLEEP static void fw_name_devm_release(struct device *dev, void *res) { struct fw_name_devm *fwn = res; if (fwn->magic == (unsigned long)&fw_cache) pr_debug("%s: fw_name-%s devm-%p released\n", __func__, fwn->name, res); kfree_const(fwn->name); } static int fw_devm_match(struct device *dev, void *res, void *match_data) { struct fw_name_devm *fwn = res; return (fwn->magic == (unsigned long)&fw_cache) && !strcmp(fwn->name, match_data); } static struct fw_name_devm *fw_find_devm_name(struct device *dev, const char *name) { struct fw_name_devm *fwn; fwn = devres_find(dev, fw_name_devm_release, fw_devm_match, (void *)name); return fwn; } /* add firmware name into devres list */ static int fw_add_devm_name(struct device *dev, const char *name) { struct fw_name_devm *fwn; fwn = fw_find_devm_name(dev, name); if (fwn) return 1; fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm), GFP_KERNEL); if (!fwn) return -ENOMEM; fwn->name = kstrdup_const(name, GFP_KERNEL); if (!fwn->name) { kfree(fwn); return -ENOMEM; } fwn->magic = (unsigned long)&fw_cache; devres_add(dev, fwn); return 0; } #else static int fw_add_devm_name(struct device *dev, const char *name) { return 0; } #endif /* * user-mode helper code */ #ifdef CONFIG_FW_LOADER_USER_HELPER struct firmware_priv { bool nowait; struct device dev; struct firmware_buf *buf; struct firmware *fw; }; static struct firmware_priv *to_firmware_priv(struct device *dev) { return container_of(dev, struct firmware_priv, dev); } static void __fw_load_abort(struct firmware_buf *buf) { /* * There is a small window in which user can write to 'loading' * between loading done and disappearance of 'loading' */ if (test_bit(FW_STATUS_DONE, &buf->status)) return; list_del_init(&buf->pending_list); set_bit(FW_STATUS_ABORT, &buf->status); complete_all(&buf->completion); } static void fw_load_abort(struct firmware_priv *fw_priv) { struct firmware_buf *buf = fw_priv->buf; __fw_load_abort(buf); /* avoid user action after loading abort */ fw_priv->buf = NULL;