// SPDX-License-Identifier: GPL-2.0-only/* * This module provides an interface to trigger and test firmware loading. * * It is designed to be used for basic evaluation of the firmware loading * subsystem (for example when validating firmware verification). It lacks * any extra dependencies, and will not normally be loaded by the system * unless explicitly requested by name. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/init.h>#include <linux/module.h>#include <linux/printk.h>#include <linux/completion.h>#include <linux/firmware.h>#include <linux/device.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/sizes.h>#include <linux/slab.h>#include <linux/uaccess.h>#include <linux/delay.h>#include <linux/kstrtox.h>#include <linux/kthread.h>#include <linux/vmalloc.h>#include <linux/efi_embedded_fw.h>#include <linux/string_choices.h> MODULE_IMPORT_NS("TEST_FIRMWARE"); #define TEST_FIRMWARE_NAME "test-firmware.bin"#define TEST_FIRMWARE_NUM_REQS 4#define TEST_FIRMWARE_BUF_SIZE SZ_1K#define TEST_UPLOAD_MAX_SIZE SZ_2K#define TEST_UPLOAD_BLK_SIZE 37 /* Avoid powers of two in testing */ static DEFINE_MUTEX(test_fw_mutex);static const struct firmware *test_firmware;static LIST_HEAD(test_upload_list); struct test_batched_req { u8 idx; int rc; bool sent; const struct firmware *fw; const char *name; const char *fw_buf; struct completion completion; struct task_struct *task; struct device *dev;}; /** * struct test_config - represents configuration for the test for different triggers * * @name: the name of the firmware file to look for * @into_buf: when the into_buf is used if this is true * request_firmware_into_buf() will be used instead. * @buf_size: size of buf to allocate when into_buf is true * @file_offset: file offset to request when calling request_firmware_into_buf * @partial: partial read opt when calling request_firmware_into_buf * @sync_direct: when the sync trigger is used if this is true * request_firmware_direct() will be used instead. * @send_uevent: whether or not to send a uevent for async requests * @num_requests: number of requests to try per test case. This is trigger * specific. * @reqs: stores all requests information * @read_fw_idx: index of thread from which we want to read firmware results * from through the read_fw trigger. * @upload_name: firmware name to be used with upload_read sysfs node * @test_result: a test may use this to collect the result from the call * of the request_firmware*() calls used in their tests. In order of * priority we always keep first any setup error. If no setup errors were * found then we move on to the first error encountered while running the * API. Note that for async calls this typically will be a successful * result (0) unless of course you've used bogus parameters, or the system * is out of memory. In the async case the callback is expected to do a * bit more homework to figure out what happened, unfortunately the only * information passed today on error is the fact that no firmware was * found so we can only assume -ENOENT on async calls if the firmware is * NULL. * * Errors you can expect: * * API specific: * * 0: success for sync, for async it means request was sent * -EINVAL: invalid parameters or request * -ENOENT: files not found * * System environment: * * -ENOMEM: memory pressure on system * -ENODEV: out of number of devices to test * -EINVAL: an unexpected error has occurred * @req_firmware: if @sync_direct is true this is set to * request_firmware_direct(), otherwise request_firmware() */struct test_config { char *name; bool into_buf; size_t buf_size; size_t file_offset; bool partial; bool sync_direct; bool send_uevent; u8 num_requests; u8 read_fw_idx; char *upload_name; /* * These below don't belong her but we'll move them once we create * a struct fw_test_device and stuff the misc_dev under there later. */ struct test_batched_req *reqs; int test_result; int (*req_firmware)(const struct firmware **fw, const char *name, struct device *device);}; struct upload_inject_err { const char *prog; enum fw_upload_err err_code;}; struct test_firmware_upload { char *name; struct list_head node; char *buf; size_t size; bool cancel_request; struct upload_inject_err inject; struct fw_upload *fwl;}; static struct test_config *test_fw_config; static struct test_firmware_upload *upload_lookup_name(const char *name){ struct test_firmware_upload *tst; list_for_each_entry(tst, &test_upload_list, node) if (strncmp(name, tst->name, strlen(tst->name)) == 0) return tst; return NULL;} static ssize_t test_fw_misc_read(struct file *f, char __user *buf, size_t size, loff_t *offset){ ssize_t rc = 0; mutex_lock(&test_fw_mutex); if (test_firmware) rc = simple_read_from_buffer(buf, size, offset, test_firmware->data, test_firmware->size); mutex_unlock(&test_fw_mutex); return rc;} static const struct file_operations test_fw_fops = { .owner = THIS_MODULE, .read = test_fw_misc_read,}; static void __test_release_all_firmware(void){ struct test_batched_req *req; u8 i; if (!test_fw_config->reqs) return; for (i = 0; i < test_fw_config->num_requests; i++) { req = &test_fw_config->reqs[i]; if (req->fw) { if (req->fw_buf) { kfree_const(req->fw_buf); req->fw_buf = NULL; } release_firmware(req->fw); req->fw = NULL; } } vfree(test_fw_config->reqs); test_fw_config->reqs = NULL;} static void test_release_all_firmware(void){ mutex_lock(&test_fw_mutex); __test_release_all_firmware(); mutex_unlock(&test_fw_mutex);} static void __test_firmware_config_free(void){ __test_release_all_firmware(); kfree_const(test_fw_config->name); test_fw_config->name = NULL;} /* * XXX: move to kstrncpy() once merged. * * Users should use kfree_const() when freeing these. */static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp){ *dst = kstrndup(name, count, gfp); if (!*dst) return -ENOMEM; return count;} static int __test_firmware_config_init(void){ int ret; ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME, strlen(TEST_FIRMWARE_NAME), GFP_KERNEL); if (ret < 0) goto out; test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS; test_fw_config->send_uevent = true; test_fw_config->into_buf = false; test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE; test_fw_config->file_offset = 0; test_fw_config->partial = false; test_fw_config->sync_direct = false; test_fw_config->req_firmware = request_firmware; test_fw_config->test_result = 0; test_fw_config->reqs = NULL; test_fw_config->upload_name = NULL; return 0; out: __test_firmware_config_free(); return ret;} static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ int ret; mutex_lock(&test_fw_mutex); __test_firmware_config_free(); ret = __test_firmware_config_init(); if (ret < 0) { ret = -ENOMEM; pr_err("could not alloc settings for config trigger: %d\n", ret); goto out; } pr_info("reset\n"); ret = count; out: mutex_unlock(&test_fw_mutex); return ret;}static DEVICE_ATTR_WO(reset); static ssize_t config_show(struct device *dev, struct device_attribute *attr, char *buf){ int len = 0; mutex_lock(&test_fw_mutex); len += scnprintf(buf, PAGE_SIZE - len, "Custom trigger configuration for: %s\n", dev_name(dev)); if (test_fw_config->name) len += scnprintf(buf + len, PAGE_SIZE - len, "name:\t%s\n", test_fw_config->name); else len += scnprintf(buf + len, PAGE_SIZE - len, "name:\tEMPTY\n"); len += scnprintf(buf + len, PAGE_SIZE - len, "num_requests:\t%u\n", test_fw_config->num_requests); len += scnprintf(buf + len, PAGE_SIZE - len, "send_uevent:\t\t%s\n", test_fw_config->send_uevent ? "FW_ACTION_UEVENT" : "FW_ACTION_NOUEVENT"); len += scnprintf(buf + len, PAGE_SIZE - len, "into_buf:\t\t%s\n", str_true_false(test_fw_config->into_buf)); len += scnprintf(buf + len, PAGE_SIZE - len, "buf_size:\t%zu\n", test_fw_config->buf_size); len += scnprintf(buf + len, PAGE_SIZE - len, "file_offset:\t%zu\n", test_fw_config->file_offset); len += scnprintf(buf + len, PAGE_SIZE - len, "partial:\t\t%s\n", str_true_false(test_fw_config->partial)); len += scnprintf(buf + len, PAGE_SIZE - len, "sync_direct:\t\t%s\n", str_true_false(test_fw_config->sync_direct)); len += scnprintf(buf + len, PAGE_SIZE - len, "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx); if (test_fw_config->upload_name) len += scnprintf(buf + len, PAGE_SIZE - len, "upload_name:\t%s\n", test_fw_config->upload_name); else len += scnprintf(buf + len, PAGE_SIZE - len, "upload_name:\tEMPTY\n"); mutex_unlock(&test_fw_mutex); return len;}static DEVICE_ATTR_RO(config); static ssize_t config_name_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ int ret; mutex_lock(&test_fw_mutex); kfree_const(test_fw_config->name); ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL); mutex_unlock(&test_fw_mutex); return ret;} /* * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE. */static ssize_t config_test_show_str(char *dst, char *src){ int len; mutex_lock(&test_fw_mutex); len = snprintf(dst, PAGE_SIZE, "%s\n", src); mutex_unlock(&test_fw_mutex); return len;} static inline int __test_dev_config_update_bool(const char *buf, size_t size, bool *cfg){ int ret; if (kstrtobool(buf, cfg) < 0) ret = -EINVAL; else ret = size; return ret;} static int test_dev_config_update_bool(const char *buf, size_t size, bool *cfg){ int ret; mutex_lock(&test_fw_mutex); ret = __test_dev_config_update_bool(buf, size, cfg); mutex_unlock(&test_fw_mutex); return ret;} static ssize_t test_dev_config_show_bool(char *buf, bool val){ return snprintf(buf, PAGE_SIZE, "%d\n", val);} static int __test_dev_config_update_size_t( const char *buf, size_t size, size_t *cfg){ int ret; long new; ret = kstrtol(buf, 10, &new); if (ret) return ret; *(size_t *)cfg = new; /* Always return full write size even if we didn't consume all */ return size;} static ssize_t test_dev_config_show_size_t(char *buf, size_t val){ return snprintf(buf, PAGE_SIZE, "%zu\n", val);} static ssize_t test_dev_config_show_int(char *buf, int val){ return snprintf(buf, PAGE_SIZE, "%d\n", val);} static int __test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg){ u8 val; int ret; ret = kstrtou8(buf, 10, &val); if (ret) return ret; *(u8 *)cfg = val; /* Always return full write size even if we didn't consume all */ return size;} static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg){ int ret; mutex_lock(&test_fw_mutex); ret = __test_dev_config_update_u8(buf, size, cfg); mutex_unlock(&test_fw_mutex); return ret;} static ssize_t test_dev_config_show_u8(char *buf, u8 val){ return snprintf(buf, PAGE_SIZE, "%u\n", val);} static ssize_t config_name_show(struct device *dev, struct device_attribute *attr, char *buf){ return config_test_show_str(buf, test_fw_config->name);}static DEVICE_ATTR_RW(config_name); static ssize_t config_upload_name_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ struct test_firmware_upload *tst; int ret = count; mutex_lock(&test_fw_mutex); tst = upload_lookup_name(buf); if (tst) test_fw_config->upload_name = tst->name; else ret = -EINVAL; mutex_unlock(&test_fw_mutex); return ret;} static ssize_t config_upload_name_show(struct device *dev, struct device_attribute *attr, char *buf){ return config_test_show_str(buf, test_fw_config->upload_name);}static DEVICE_ATTR_RW(config_upload_name); static ssize_t config_num_requests_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ int rc; mutex_lock(&test_fw_mutex); if (test_fw_config->reqs) { pr_err("Must call release_all_firmware prior to changing config\n"); rc = -EINVAL; mutex_unlock(&test_fw_mutex); goto out; } Lines 1–500 of 1572. Load more
// SPDX-License-Identifier: GPL-2.0-only /* * This module provides an interface to trigger and test firmware loading. * * It is designed to be used for basic evaluation of the firmware loading * subsystem (for example when validating firmware verification). It lacks * any extra dependencies, and will not normally be loaded by the system * unless explicitly requested by name. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/init.h> #include <linux/module.h> #include <linux/printk.h> #include <linux/completion.h> #include <linux/firmware.h> #include <linux/device.h> #include <linux/fs.h> #include <linux/miscdevice.h> #include <linux/sizes.h> #include <linux/slab.h> #include <linux/uaccess.h> #include <linux/delay.h> #include <linux/kstrtox.h> #include <linux/kthread.h> #include <linux/vmalloc.h> #include <linux/efi_embedded_fw.h> #include <linux/string_choices.h> MODULE_IMPORT_NS("TEST_FIRMWARE"); #define TEST_FIRMWARE_NAME "test-firmware.bin" #define TEST_FIRMWARE_NUM_REQS 4 #define TEST_FIRMWARE_BUF_SIZE SZ_1K #define TEST_UPLOAD_MAX_SIZE SZ_2K #define TEST_UPLOAD_BLK_SIZE 37 /* Avoid powers of two in testing */ static DEFINE_MUTEX(test_fw_mutex); static const struct firmware *test_firmware; static LIST_HEAD(test_upload_list); struct test_batched_req { u8 idx; int rc; bool sent; const struct firmware *fw; const char *name; const char *fw_buf; struct completion completion; struct task_struct *task; struct device *dev; }; /** * struct test_config - represents configuration for the test for different triggers * * @name: the name of the firmware file to look for * @into_buf: when the into_buf is used if this is true * request_firmware_into_buf() will be used instead. * @buf_size: size of buf to allocate when into_buf is true * @file_offset: file offset to request when calling request_firmware_into_buf * @partial: partial read opt when calling request_firmware_into_buf * @sync_direct: when the sync trigger is used if this is true * request_firmware_direct() will be used instead. * @send_uevent: whether or not to send a uevent for async requests * @num_requests: number of requests to try per test case. This is trigger * specific. * @reqs: stores all requests information * @read_fw_idx: index of thread from which we want to read firmware results * from through the read_fw trigger. * @upload_name: firmware name to be used with upload_read sysfs node * @test_result: a test may use this to collect the result from the call * of the request_firmware*() calls used in their tests. In order of * priority we always keep first any setup error. If no setup errors were * found then we move on to the first error encountered while running the * API. Note that for async calls this typically will be a successful * result (0) unless of course you've used bogus parameters, or the system * is out of memory. In the async case the callback is expected to do a * bit more homework to figure out what happened, unfortunately the only * information passed today on error is the fact that no firmware was * found so we can only assume -ENOENT on async calls if the firmware is * NULL. * * Errors you can expect: * * API specific: * * 0: success for sync, for async it means request was sent * -EINVAL: invalid parameters or request * -ENOENT: files not found * * System environment: * * -ENOMEM: memory pressure on system * -ENODEV: out of number of devices to test * -EINVAL: an unexpected error has occurred * @req_firmware: if @sync_direct is true this is set to * request_firmware_direct(), otherwise request_firmware() */ struct test_config { char *name; bool into_buf; size_t buf_size; size_t file_offset; bool partial; bool sync_direct; bool send_uevent; u8 num_requests; u8 read_fw_idx; char *upload_name; /* * These below don't belong her but we'll move them once we create * a struct fw_test_device and stuff the misc_dev under there later. */ struct test_batched_req *reqs; int test_result; int (*req_firmware)(const struct firmware **fw, const char *name, struct device *device); }; struct upload_inject_err { const char *prog; enum fw_upload_err err_code; }; struct test_firmware_upload { char *name; struct list_head node; char *buf; size_t size; bool cancel_request; struct upload_inject_err inject; struct fw_upload *fwl; }; static struct test_config *test_fw_config; static struct test_firmware_upload *upload_lookup_name(const char *name) { struct test_firmware_upload *tst; list_for_each_entry(tst, &test_upload_list, node) if (strncmp(name, tst->name, strlen(tst->name)) == 0) return tst; return NULL; } static ssize_t test_fw_misc_read(struct file *f, char __user *buf, size_t size, loff_t *offset) { ssize_t rc = 0; mutex_lock(&test_fw_mutex); if (test_firmware) rc = simple_read_from_buffer(buf, size, offset, test_firmware->data, test_firmware->size); mutex_unlock(&test_fw_mutex); return rc; } static const struct file_operations test_fw_fops = { .owner = THIS_MODULE, .read = test_fw_misc_read, }; static void __test_release_all_firmware(void) { struct test_batched_req *req; u8 i; if (!test_fw_config->reqs) return; for (i = 0; i < test_fw_config->num_requests; i++) { req = &test_fw_config->reqs[i]; if (req->fw) { if (req->fw_buf) { kfree_const(req->fw_buf); req->fw_buf = NULL; } release_firmware(req->fw); req->fw = NULL; } } vfree(test_fw_config->reqs); test_fw_config->reqs = NULL; } static void test_release_all_firmware(void) { mutex_lock(&test_fw_mutex); __test_release_all_firmware(); mutex_unlock(&test_fw_mutex); } static void __test_firmware_config_free(void) { __test_release_all_firmware(); kfree_const(test_fw_config->name); test_fw_config->name = NULL; } /* * XXX: move to kstrncpy() once merged. * * Users should use kfree_const() when freeing these. */ static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp) { *dst = kstrndup(name, count, gfp); if (!*dst) return -ENOMEM; return count; } static int __test_firmware_config_init(void) { int ret; ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME, strlen(TEST_FIRMWARE_NAME), GFP_KERNEL); if (ret < 0) goto out; test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS; test_fw_config->send_uevent = true; test_fw_config->into_buf = false; test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE; test_fw_config->file_offset = 0; test_fw_config->partial = false; test_fw_config->sync_direct = false; test_fw_config->req_firmware = request_firmware; test_fw_config->test_result = 0; test_fw_config->reqs = NULL; test_fw_config->upload_name = NULL; return 0; out: __test_firmware_config_free(); return ret; } static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int ret; mutex_lock(&test_fw_mutex); __test_firmware_config_free(); ret = __test_firmware_config_init(); if (ret < 0) { ret = -ENOMEM; pr_err("could not alloc settings for config trigger: %d\n", ret); goto out; } pr_info("reset\n"); ret = count; out: mutex_unlock(&test_fw_mutex); return ret; } static DEVICE_ATTR_WO(reset); static ssize_t config_show(struct device *dev, struct device_attribute *attr, char *buf) { int len = 0; mutex_lock(&test_fw_mutex); len += scnprintf(buf, PAGE_SIZE - len, "Custom trigger configuration for: %s\n", dev_name(dev)); if (test_fw_config->name) len += scnprintf(buf + len, PAGE_SIZE - len, "name:\t%s\n", test_fw_config->name); else len += scnprintf(buf + len, PAGE_SIZE - len, "name:\tEMPTY\n"); len += scnprintf(buf + len, PAGE_SIZE - len, "num_requests:\t%u\n", test_fw_config->num_requests); len += scnprintf(buf + len, PAGE_SIZE - len, "send_uevent:\t\t%s\n", test_fw_config->send_uevent ? "FW_ACTION_UEVENT" : "FW_ACTION_NOUEVENT"); len += scnprintf(buf + len, PAGE_SIZE - len, "into_buf:\t\t%s\n", str_true_false(test_fw_config->into_buf)); len += scnprintf(buf + len, PAGE_SIZE - len, "buf_size:\t%zu\n", test_fw_config->buf_size); len += scnprintf(buf + len, PAGE_SIZE - len, "file_offset:\t%zu\n", test_fw_config->file_offset); len += scnprintf(buf + len, PAGE_SIZE - len, "partial:\t\t%s\n", str_true_false(test_fw_config->partial)); len += scnprintf(buf + len, PAGE_SIZE - len, "sync_direct:\t\t%s\n", str_true_false(test_fw_config->sync_direct)); len += scnprintf(buf + len, PAGE_SIZE - len, "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx); if (test_fw_config->upload_name) len += scnprintf(buf + len, PAGE_SIZE - len, "upload_name:\t%s\n", test_fw_config->upload_name); else len += scnprintf(buf + len, PAGE_SIZE - len, "upload_name:\tEMPTY\n"); mutex_unlock(&test_fw_mutex); return len; } static DEVICE_ATTR_RO(config); static ssize_t config_name_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int ret; mutex_lock(&test_fw_mutex); kfree_const(test_fw_config->name); ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL); mutex_unlock(&test_fw_mutex); return ret; } /* * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE. */ static ssize_t config_test_show_str(char *dst, char *src) { int len; mutex_lock(&test_fw_mutex); len = snprintf(dst, PAGE_SIZE, "%s\n", src); mutex_unlock(&test_fw_mutex); return len; } static inline int __test_dev_config_update_bool(const char *buf, size_t size, bool *cfg) { int ret; if (kstrtobool(buf, cfg) < 0) ret = -EINVAL; else ret = size; return ret; } static int test_dev_config_update_bool(const char *buf, size_t size, bool *cfg) { int ret; mutex_lock(&test_fw_mutex); ret = __test_dev_config_update_bool(buf, size, cfg); mutex_unlock(&test_fw_mutex); return ret; } static ssize_t test_dev_config_show_bool(char *buf, bool val) { return snprintf(buf, PAGE_SIZE, "%d\n", val); } static int __test_dev_config_update_size_t( const char *buf, size_t size, size_t *cfg) { int ret; long new; ret = kstrtol(buf, 10, &new); if (ret) return ret; *(size_t *)cfg = new; /* Always return full write size even if we didn't consume all */ return size; } static ssize_t test_dev_config_show_size_t(char *buf, size_t val) { return snprintf(buf, PAGE_SIZE, "%zu\n", val); } static ssize_t test_dev_config_show_int(char *buf, int val) { return snprintf(buf, PAGE_SIZE, "%d\n", val); } static int __test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg) { u8 val; int ret; ret = kstrtou8(buf, 10, &val); if (ret) return ret; *(u8 *)cfg = val; /* Always return full write size even if we didn't consume all */ return size; } static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg) { int ret; mutex_lock(&test_fw_mutex); ret = __test_dev_config_update_u8(buf, size, cfg); mutex_unlock(&test_fw_mutex); return ret; } static ssize_t test_dev_config_show_u8(char *buf, u8 val) { return snprintf(buf, PAGE_SIZE, "%u\n", val); } static ssize_t config_name_show(struct device *dev, struct device_attribute *attr, char *buf) { return config_test_show_str(buf, test_fw_config->name); } static DEVICE_ATTR_RW(config_name); static ssize_t config_upload_name_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct test_firmware_upload *tst; int ret = count; mutex_lock(&test_fw_mutex); tst = upload_lookup_name(buf); if (tst) test_fw_config->upload_name = tst->name; else ret = -EINVAL; mutex_unlock(&test_fw_mutex); return ret; } static ssize_t config_upload_name_show(struct device *dev, struct device_attribute *attr, char *buf) { return config_test_show_str(buf, test_fw_config->upload_name); } static DEVICE_ATTR_RW(config_upload_name); static ssize_t config_num_requests_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int rc; mutex_lock(&test_fw_mutex); if (test_fw_config->reqs) { pr_err("Must call release_all_firmware prior to changing config\n"); rc = -EINVAL; mutex_unlock(&test_fw_mutex); goto out; }