| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * This module provides an interface to trigger and test firmware loading. |
| 4 | * |
| 5 | * It is designed to be used for basic evaluation of the firmware loading |
| 6 | * subsystem (for example when validating firmware verification). It lacks |
| 7 | * any extra dependencies, and will not normally be loaded by the system |
| 8 | * unless explicitly requested by name. |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/printk.h> |
| 16 | #include <linux/completion.h> |
| 17 | #include <linux/firmware.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/miscdevice.h> |
| 21 | #include <linux/sizes.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/kstrtox.h> |
| 26 | #include <linux/kthread.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/efi_embedded_fw.h> |
| 29 | #include <linux/string_choices.h> |
| 30 | |
| 31 | MODULE_IMPORT_NS("TEST_FIRMWARE" ); |
| 32 | |
| 33 | #define TEST_FIRMWARE_NAME "test-firmware.bin" |
| 34 | #define TEST_FIRMWARE_NUM_REQS 4 |
| 35 | #define TEST_FIRMWARE_BUF_SIZE SZ_1K |
| 36 | #define TEST_UPLOAD_MAX_SIZE SZ_2K |
| 37 | #define TEST_UPLOAD_BLK_SIZE 37 /* Avoid powers of two in testing */ |
| 38 | |
| 39 | static DEFINE_MUTEX(test_fw_mutex); |
| 40 | static const struct firmware *test_firmware; |
| 41 | static LIST_HEAD(test_upload_list); |
| 42 | |
| 43 | struct test_batched_req { |
| 44 | u8 idx; |
| 45 | int rc; |
| 46 | bool sent; |
| 47 | const struct firmware *fw; |
| 48 | const char *name; |
| 49 | const char *fw_buf; |
| 50 | struct completion completion; |
| 51 | struct task_struct *task; |
| 52 | struct device *dev; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * struct test_config - represents configuration for the test for different triggers |
| 57 | * |
| 58 | * @name: the name of the firmware file to look for |
| 59 | * @into_buf: when the into_buf is used if this is true |
| 60 | * request_firmware_into_buf() will be used instead. |
| 61 | * @buf_size: size of buf to allocate when into_buf is true |
| 62 | * @file_offset: file offset to request when calling request_firmware_into_buf |
| 63 | * @partial: partial read opt when calling request_firmware_into_buf |
| 64 | * @sync_direct: when the sync trigger is used if this is true |
| 65 | * request_firmware_direct() will be used instead. |
| 66 | * @send_uevent: whether or not to send a uevent for async requests |
| 67 | * @num_requests: number of requests to try per test case. This is trigger |
| 68 | * specific. |
| 69 | * @reqs: stores all requests information |
| 70 | * @read_fw_idx: index of thread from which we want to read firmware results |
| 71 | * from through the read_fw trigger. |
| 72 | * @upload_name: firmware name to be used with upload_read sysfs node |
| 73 | * @test_result: a test may use this to collect the result from the call |
| 74 | * of the request_firmware*() calls used in their tests. In order of |
| 75 | * priority we always keep first any setup error. If no setup errors were |
| 76 | * found then we move on to the first error encountered while running the |
| 77 | * API. Note that for async calls this typically will be a successful |
| 78 | * result (0) unless of course you've used bogus parameters, or the system |
| 79 | * is out of memory. In the async case the callback is expected to do a |
| 80 | * bit more homework to figure out what happened, unfortunately the only |
| 81 | * information passed today on error is the fact that no firmware was |
| 82 | * found so we can only assume -ENOENT on async calls if the firmware is |
| 83 | * NULL. |
| 84 | * |
| 85 | * Errors you can expect: |
| 86 | * |
| 87 | * API specific: |
| 88 | * |
| 89 | * 0: success for sync, for async it means request was sent |
| 90 | * -EINVAL: invalid parameters or request |
| 91 | * -ENOENT: files not found |
| 92 | * |
| 93 | * System environment: |
| 94 | * |
| 95 | * -ENOMEM: memory pressure on system |
| 96 | * -ENODEV: out of number of devices to test |
| 97 | * -EINVAL: an unexpected error has occurred |
| 98 | * @req_firmware: if @sync_direct is true this is set to |
| 99 | * request_firmware_direct(), otherwise request_firmware() |
| 100 | */ |
| 101 | struct test_config { |
| 102 | char *name; |
| 103 | bool into_buf; |
| 104 | size_t buf_size; |
| 105 | size_t file_offset; |
| 106 | bool partial; |
| 107 | bool sync_direct; |
| 108 | bool send_uevent; |
| 109 | u8 num_requests; |
| 110 | u8 read_fw_idx; |
| 111 | char *upload_name; |
| 112 | |
| 113 | /* |
| 114 | * These below don't belong her but we'll move them once we create |
| 115 | * a struct fw_test_device and stuff the misc_dev under there later. |
| 116 | */ |
| 117 | struct test_batched_req *reqs; |
| 118 | int test_result; |
| 119 | int (*req_firmware)(const struct firmware **fw, const char *name, |
| 120 | struct device *device); |
| 121 | }; |
| 122 | |
| 123 | struct upload_inject_err { |
| 124 | const char *prog; |
| 125 | enum fw_upload_err err_code; |
| 126 | }; |
| 127 | |
| 128 | struct test_firmware_upload { |
| 129 | char *name; |
| 130 | struct list_head node; |
| 131 | char *buf; |
| 132 | size_t size; |
| 133 | bool cancel_request; |
| 134 | struct upload_inject_err inject; |
| 135 | struct fw_upload *fwl; |
| 136 | }; |
| 137 | |
| 138 | static struct test_config *test_fw_config; |
| 139 | |
| 140 | static struct test_firmware_upload *upload_lookup_name(const char *name) |
| 141 | { |
| 142 | struct test_firmware_upload *tst; |
| 143 | |
| 144 | list_for_each_entry(tst, &test_upload_list, node) |
| 145 | if (strncmp(name, tst->name, strlen(tst->name)) == 0) |
| 146 | return tst; |
| 147 | |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | static ssize_t test_fw_misc_read(struct file *f, char __user *buf, |
| 152 | size_t size, loff_t *offset) |
| 153 | { |
| 154 | ssize_t rc = 0; |
| 155 | |
| 156 | mutex_lock(&test_fw_mutex); |
| 157 | if (test_firmware) |
| 158 | rc = simple_read_from_buffer(to: buf, count: size, ppos: offset, |
| 159 | from: test_firmware->data, |
| 160 | available: test_firmware->size); |
| 161 | mutex_unlock(lock: &test_fw_mutex); |
| 162 | return rc; |
| 163 | } |
| 164 | |
| 165 | static const struct file_operations test_fw_fops = { |
| 166 | .owner = THIS_MODULE, |
| 167 | .read = test_fw_misc_read, |
| 168 | }; |
| 169 | |
| 170 | static void __test_release_all_firmware(void) |
| 171 | { |
| 172 | struct test_batched_req *req; |
| 173 | u8 i; |
| 174 | |
| 175 | if (!test_fw_config->reqs) |
| 176 | return; |
| 177 | |
| 178 | for (i = 0; i < test_fw_config->num_requests; i++) { |
| 179 | req = &test_fw_config->reqs[i]; |
| 180 | if (req->fw) { |
| 181 | if (req->fw_buf) { |
| 182 | kfree_const(x: req->fw_buf); |
| 183 | req->fw_buf = NULL; |
| 184 | } |
| 185 | release_firmware(fw: req->fw); |
| 186 | req->fw = NULL; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | vfree(addr: test_fw_config->reqs); |
| 191 | test_fw_config->reqs = NULL; |
| 192 | } |
| 193 | |
| 194 | static void test_release_all_firmware(void) |
| 195 | { |
| 196 | mutex_lock(&test_fw_mutex); |
| 197 | __test_release_all_firmware(); |
| 198 | mutex_unlock(lock: &test_fw_mutex); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static void __test_firmware_config_free(void) |
| 203 | { |
| 204 | __test_release_all_firmware(); |
| 205 | kfree_const(x: test_fw_config->name); |
| 206 | test_fw_config->name = NULL; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * XXX: move to kstrncpy() once merged. |
| 211 | * |
| 212 | * Users should use kfree_const() when freeing these. |
| 213 | */ |
| 214 | static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp) |
| 215 | { |
| 216 | *dst = kstrndup(s: name, len: count, gfp); |
| 217 | if (!*dst) |
| 218 | return -ENOMEM; |
| 219 | return count; |
| 220 | } |
| 221 | |
| 222 | static int __test_firmware_config_init(void) |
| 223 | { |
| 224 | int ret; |
| 225 | |
| 226 | ret = __kstrncpy(dst: &test_fw_config->name, TEST_FIRMWARE_NAME, |
| 227 | strlen(TEST_FIRMWARE_NAME), GFP_KERNEL); |
| 228 | if (ret < 0) |
| 229 | goto out; |
| 230 | |
| 231 | test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS; |
| 232 | test_fw_config->send_uevent = true; |
| 233 | test_fw_config->into_buf = false; |
| 234 | test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE; |
| 235 | test_fw_config->file_offset = 0; |
| 236 | test_fw_config->partial = false; |
| 237 | test_fw_config->sync_direct = false; |
| 238 | test_fw_config->req_firmware = request_firmware; |
| 239 | test_fw_config->test_result = 0; |
| 240 | test_fw_config->reqs = NULL; |
| 241 | test_fw_config->upload_name = NULL; |
| 242 | |
| 243 | return 0; |
| 244 | |
| 245 | out: |
| 246 | __test_firmware_config_free(); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static ssize_t reset_store(struct device *dev, |
| 251 | struct device_attribute *attr, |
| 252 | const char *buf, size_t count) |
| 253 | { |
| 254 | int ret; |
| 255 | |
| 256 | mutex_lock(&test_fw_mutex); |
| 257 | |
| 258 | __test_firmware_config_free(); |
| 259 | |
| 260 | ret = __test_firmware_config_init(); |
| 261 | if (ret < 0) { |
| 262 | ret = -ENOMEM; |
| 263 | pr_err("could not alloc settings for config trigger: %d\n" , |
| 264 | ret); |
| 265 | goto out; |
| 266 | } |
| 267 | |
| 268 | pr_info("reset\n" ); |
| 269 | ret = count; |
| 270 | |
| 271 | out: |
| 272 | mutex_unlock(lock: &test_fw_mutex); |
| 273 | |
| 274 | return ret; |
| 275 | } |
| 276 | static DEVICE_ATTR_WO(reset); |
| 277 | |
| 278 | static ssize_t config_show(struct device *dev, |
| 279 | struct device_attribute *attr, |
| 280 | char *buf) |
| 281 | { |
| 282 | int len = 0; |
| 283 | |
| 284 | mutex_lock(&test_fw_mutex); |
| 285 | |
| 286 | len += scnprintf(buf, PAGE_SIZE - len, |
| 287 | fmt: "Custom trigger configuration for: %s\n" , |
| 288 | dev_name(dev)); |
| 289 | |
| 290 | if (test_fw_config->name) |
| 291 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 292 | fmt: "name:\t%s\n" , |
| 293 | test_fw_config->name); |
| 294 | else |
| 295 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 296 | fmt: "name:\tEMPTY\n" ); |
| 297 | |
| 298 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 299 | fmt: "num_requests:\t%u\n" , test_fw_config->num_requests); |
| 300 | |
| 301 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 302 | fmt: "send_uevent:\t\t%s\n" , |
| 303 | test_fw_config->send_uevent ? |
| 304 | "FW_ACTION_UEVENT" : |
| 305 | "FW_ACTION_NOUEVENT" ); |
| 306 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 307 | fmt: "into_buf:\t\t%s\n" , |
| 308 | str_true_false(v: test_fw_config->into_buf)); |
| 309 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 310 | fmt: "buf_size:\t%zu\n" , test_fw_config->buf_size); |
| 311 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 312 | fmt: "file_offset:\t%zu\n" , test_fw_config->file_offset); |
| 313 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 314 | fmt: "partial:\t\t%s\n" , |
| 315 | str_true_false(v: test_fw_config->partial)); |
| 316 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 317 | fmt: "sync_direct:\t\t%s\n" , |
| 318 | str_true_false(v: test_fw_config->sync_direct)); |
| 319 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 320 | fmt: "read_fw_idx:\t%u\n" , test_fw_config->read_fw_idx); |
| 321 | if (test_fw_config->upload_name) |
| 322 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 323 | fmt: "upload_name:\t%s\n" , |
| 324 | test_fw_config->upload_name); |
| 325 | else |
| 326 | len += scnprintf(buf: buf + len, PAGE_SIZE - len, |
| 327 | fmt: "upload_name:\tEMPTY\n" ); |
| 328 | |
| 329 | mutex_unlock(lock: &test_fw_mutex); |
| 330 | |
| 331 | return len; |
| 332 | } |
| 333 | static DEVICE_ATTR_RO(config); |
| 334 | |
| 335 | static ssize_t config_name_store(struct device *dev, |
| 336 | struct device_attribute *attr, |
| 337 | const char *buf, size_t count) |
| 338 | { |
| 339 | int ret; |
| 340 | |
| 341 | mutex_lock(&test_fw_mutex); |
| 342 | kfree_const(x: test_fw_config->name); |
| 343 | ret = __kstrncpy(dst: &test_fw_config->name, name: buf, count, GFP_KERNEL); |
| 344 | mutex_unlock(lock: &test_fw_mutex); |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE. |
| 351 | */ |
| 352 | static ssize_t config_test_show_str(char *dst, |
| 353 | char *src) |
| 354 | { |
| 355 | int len; |
| 356 | |
| 357 | mutex_lock(&test_fw_mutex); |
| 358 | len = snprintf(buf: dst, PAGE_SIZE, fmt: "%s\n" , src); |
| 359 | mutex_unlock(lock: &test_fw_mutex); |
| 360 | |
| 361 | return len; |
| 362 | } |
| 363 | |
| 364 | static inline int __test_dev_config_update_bool(const char *buf, size_t size, |
| 365 | bool *cfg) |
| 366 | { |
| 367 | int ret; |
| 368 | |
| 369 | if (kstrtobool(s: buf, res: cfg) < 0) |
| 370 | ret = -EINVAL; |
| 371 | else |
| 372 | ret = size; |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static int test_dev_config_update_bool(const char *buf, size_t size, |
| 378 | bool *cfg) |
| 379 | { |
| 380 | int ret; |
| 381 | |
| 382 | mutex_lock(&test_fw_mutex); |
| 383 | ret = __test_dev_config_update_bool(buf, size, cfg); |
| 384 | mutex_unlock(lock: &test_fw_mutex); |
| 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | static ssize_t test_dev_config_show_bool(char *buf, bool val) |
| 390 | { |
| 391 | return snprintf(buf, PAGE_SIZE, fmt: "%d\n" , val); |
| 392 | } |
| 393 | |
| 394 | static int __test_dev_config_update_size_t( |
| 395 | const char *buf, |
| 396 | size_t size, |
| 397 | size_t *cfg) |
| 398 | { |
| 399 | int ret; |
| 400 | long new; |
| 401 | |
| 402 | ret = kstrtol(s: buf, base: 10, res: &new); |
| 403 | if (ret) |
| 404 | return ret; |
| 405 | |
| 406 | *(size_t *)cfg = new; |
| 407 | |
| 408 | /* Always return full write size even if we didn't consume all */ |
| 409 | return size; |
| 410 | } |
| 411 | |
| 412 | static ssize_t test_dev_config_show_size_t(char *buf, size_t val) |
| 413 | { |
| 414 | return snprintf(buf, PAGE_SIZE, fmt: "%zu\n" , val); |
| 415 | } |
| 416 | |
| 417 | static ssize_t test_dev_config_show_int(char *buf, int val) |
| 418 | { |
| 419 | return snprintf(buf, PAGE_SIZE, fmt: "%d\n" , val); |
| 420 | } |
| 421 | |
| 422 | static int __test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg) |
| 423 | { |
| 424 | u8 val; |
| 425 | int ret; |
| 426 | |
| 427 | ret = kstrtou8(s: buf, base: 10, res: &val); |
| 428 | if (ret) |
| 429 | return ret; |
| 430 | |
| 431 | *(u8 *)cfg = val; |
| 432 | |
| 433 | /* Always return full write size even if we didn't consume all */ |
| 434 | return size; |
| 435 | } |
| 436 | |
| 437 | static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg) |
| 438 | { |
| 439 | int ret; |
| 440 | |
| 441 | mutex_lock(&test_fw_mutex); |
| 442 | ret = __test_dev_config_update_u8(buf, size, cfg); |
| 443 | mutex_unlock(lock: &test_fw_mutex); |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | static ssize_t test_dev_config_show_u8(char *buf, u8 val) |
| 449 | { |
| 450 | return snprintf(buf, PAGE_SIZE, fmt: "%u\n" , val); |
| 451 | } |
| 452 | |
| 453 | static ssize_t config_name_show(struct device *dev, |
| 454 | struct device_attribute *attr, |
| 455 | char *buf) |
| 456 | { |
| 457 | return config_test_show_str(dst: buf, src: test_fw_config->name); |
| 458 | } |
| 459 | static DEVICE_ATTR_RW(config_name); |
| 460 | |
| 461 | static ssize_t config_upload_name_store(struct device *dev, |
| 462 | struct device_attribute *attr, |
| 463 | const char *buf, size_t count) |
| 464 | { |
| 465 | struct test_firmware_upload *tst; |
| 466 | int ret = count; |
| 467 | |
| 468 | mutex_lock(&test_fw_mutex); |
| 469 | tst = upload_lookup_name(name: buf); |
| 470 | if (tst) |
| 471 | test_fw_config->upload_name = tst->name; |
| 472 | else |
| 473 | ret = -EINVAL; |
| 474 | mutex_unlock(lock: &test_fw_mutex); |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | static ssize_t config_upload_name_show(struct device *dev, |
| 480 | struct device_attribute *attr, |
| 481 | char *buf) |
| 482 | { |
| 483 | return config_test_show_str(dst: buf, src: test_fw_config->upload_name); |
| 484 | } |
| 485 | static DEVICE_ATTR_RW(config_upload_name); |
| 486 | |
| 487 | static ssize_t config_num_requests_store(struct device *dev, |
| 488 | struct device_attribute *attr, |
| 489 | const char *buf, size_t count) |
| 490 | { |
| 491 | int rc; |
| 492 | |
| 493 | mutex_lock(&test_fw_mutex); |
| 494 | if (test_fw_config->reqs) { |
| 495 | pr_err("Must call release_all_firmware prior to changing config\n" ); |
| 496 | rc = -EINVAL; |
| 497 | mutex_unlock(lock: &test_fw_mutex); |
| 498 | goto out; |
| 499 | } |
| 500 | |
| 501 | rc = __test_dev_config_update_u8(buf, size: count, |
| 502 | cfg: &test_fw_config->num_requests); |
| 503 | mutex_unlock(lock: &test_fw_mutex); |
| 504 | |
| 505 | out: |
| 506 | return rc; |
| 507 | } |
| 508 | |
| 509 | static ssize_t config_num_requests_show(struct device *dev, |
| 510 | struct device_attribute *attr, |
| 511 | char *buf) |
| 512 | { |
| 513 | return test_dev_config_show_u8(buf, val: test_fw_config->num_requests); |
| 514 | } |
| 515 | static DEVICE_ATTR_RW(config_num_requests); |
| 516 | |
| 517 | static ssize_t config_into_buf_store(struct device *dev, |
| 518 | struct device_attribute *attr, |
| 519 | const char *buf, size_t count) |
| 520 | { |
| 521 | return test_dev_config_update_bool(buf, |
| 522 | size: count, |
| 523 | cfg: &test_fw_config->into_buf); |
| 524 | } |
| 525 | |
| 526 | static ssize_t config_into_buf_show(struct device *dev, |
| 527 | struct device_attribute *attr, |
| 528 | char *buf) |
| 529 | { |
| 530 | return test_dev_config_show_bool(buf, val: test_fw_config->into_buf); |
| 531 | } |
| 532 | static DEVICE_ATTR_RW(config_into_buf); |
| 533 | |
| 534 | static ssize_t config_buf_size_store(struct device *dev, |
| 535 | struct device_attribute *attr, |
| 536 | const char *buf, size_t count) |
| 537 | { |
| 538 | int rc; |
| 539 | |
| 540 | mutex_lock(&test_fw_mutex); |
| 541 | if (test_fw_config->reqs) { |
| 542 | pr_err("Must call release_all_firmware prior to changing config\n" ); |
| 543 | rc = -EINVAL; |
| 544 | mutex_unlock(lock: &test_fw_mutex); |
| 545 | goto out; |
| 546 | } |
| 547 | |
| 548 | rc = __test_dev_config_update_size_t(buf, size: count, |
| 549 | cfg: &test_fw_config->buf_size); |
| 550 | mutex_unlock(lock: &test_fw_mutex); |
| 551 | |
| 552 | out: |
| 553 | return rc; |
| 554 | } |
| 555 | |
| 556 | static ssize_t config_buf_size_show(struct device *dev, |
| 557 | struct device_attribute *attr, |
| 558 | char *buf) |
| 559 | { |
| 560 | return test_dev_config_show_size_t(buf, val: test_fw_config->buf_size); |
| 561 | } |
| 562 | static DEVICE_ATTR_RW(config_buf_size); |
| 563 | |
| 564 | static ssize_t config_file_offset_store(struct device *dev, |
| 565 | struct device_attribute *attr, |
| 566 | const char *buf, size_t count) |
| 567 | { |
| 568 | int rc; |
| 569 | |
| 570 | mutex_lock(&test_fw_mutex); |
| 571 | if (test_fw_config->reqs) { |
| 572 | pr_err("Must call release_all_firmware prior to changing config\n" ); |
| 573 | rc = -EINVAL; |
| 574 | mutex_unlock(lock: &test_fw_mutex); |
| 575 | goto out; |
| 576 | } |
| 577 | |
| 578 | rc = __test_dev_config_update_size_t(buf, size: count, |
| 579 | cfg: &test_fw_config->file_offset); |
| 580 | mutex_unlock(lock: &test_fw_mutex); |
| 581 | |
| 582 | out: |
| 583 | return rc; |
| 584 | } |
| 585 | |
| 586 | static ssize_t config_file_offset_show(struct device *dev, |
| 587 | struct device_attribute *attr, |
| 588 | char *buf) |
| 589 | { |
| 590 | return test_dev_config_show_size_t(buf, val: test_fw_config->file_offset); |
| 591 | } |
| 592 | static DEVICE_ATTR_RW(config_file_offset); |
| 593 | |
| 594 | static ssize_t config_partial_store(struct device *dev, |
| 595 | struct device_attribute *attr, |
| 596 | const char *buf, size_t count) |
| 597 | { |
| 598 | return test_dev_config_update_bool(buf, |
| 599 | size: count, |
| 600 | cfg: &test_fw_config->partial); |
| 601 | } |
| 602 | |
| 603 | static ssize_t config_partial_show(struct device *dev, |
| 604 | struct device_attribute *attr, |
| 605 | char *buf) |
| 606 | { |
| 607 | return test_dev_config_show_bool(buf, val: test_fw_config->partial); |
| 608 | } |
| 609 | static DEVICE_ATTR_RW(config_partial); |
| 610 | |
| 611 | static ssize_t config_sync_direct_store(struct device *dev, |
| 612 | struct device_attribute *attr, |
| 613 | const char *buf, size_t count) |
| 614 | { |
| 615 | int rc = test_dev_config_update_bool(buf, size: count, |
| 616 | cfg: &test_fw_config->sync_direct); |
| 617 | |
| 618 | if (rc == count) |
| 619 | test_fw_config->req_firmware = test_fw_config->sync_direct ? |
| 620 | request_firmware_direct : |
| 621 | request_firmware; |
| 622 | return rc; |
| 623 | } |
| 624 | |
| 625 | static ssize_t config_sync_direct_show(struct device *dev, |
| 626 | struct device_attribute *attr, |
| 627 | char *buf) |
| 628 | { |
| 629 | return test_dev_config_show_bool(buf, val: test_fw_config->sync_direct); |
| 630 | } |
| 631 | static DEVICE_ATTR_RW(config_sync_direct); |
| 632 | |
| 633 | static ssize_t config_send_uevent_store(struct device *dev, |
| 634 | struct device_attribute *attr, |
| 635 | const char *buf, size_t count) |
| 636 | { |
| 637 | return test_dev_config_update_bool(buf, size: count, |
| 638 | cfg: &test_fw_config->send_uevent); |
| 639 | } |
| 640 | |
| 641 | static ssize_t config_send_uevent_show(struct device *dev, |
| 642 | struct device_attribute *attr, |
| 643 | char *buf) |
| 644 | { |
| 645 | return test_dev_config_show_bool(buf, val: test_fw_config->send_uevent); |
| 646 | } |
| 647 | static DEVICE_ATTR_RW(config_send_uevent); |
| 648 | |
| 649 | static ssize_t config_read_fw_idx_store(struct device *dev, |
| 650 | struct device_attribute *attr, |
| 651 | const char *buf, size_t count) |
| 652 | { |
| 653 | return test_dev_config_update_u8(buf, size: count, |
| 654 | cfg: &test_fw_config->read_fw_idx); |
| 655 | } |
| 656 | |
| 657 | static ssize_t config_read_fw_idx_show(struct device *dev, |
| 658 | struct device_attribute *attr, |
| 659 | char *buf) |
| 660 | { |
| 661 | return test_dev_config_show_u8(buf, val: test_fw_config->read_fw_idx); |
| 662 | } |
| 663 | static DEVICE_ATTR_RW(config_read_fw_idx); |
| 664 | |
| 665 | |
| 666 | static ssize_t trigger_request_store(struct device *dev, |
| 667 | struct device_attribute *attr, |
| 668 | const char *buf, size_t count) |
| 669 | { |
| 670 | int rc; |
| 671 | char *name; |
| 672 | |
| 673 | name = kstrndup(s: buf, len: count, GFP_KERNEL); |
| 674 | if (!name) |
| 675 | return -ENOMEM; |
| 676 | |
| 677 | pr_info("loading '%s'\n" , name); |
| 678 | |
| 679 | mutex_lock(&test_fw_mutex); |
| 680 | release_firmware(fw: test_firmware); |
| 681 | if (test_fw_config->reqs) |
| 682 | __test_release_all_firmware(); |
| 683 | test_firmware = NULL; |
| 684 | rc = request_firmware(fw: &test_firmware, name, device: dev); |
| 685 | if (rc) { |
| 686 | pr_info("load of '%s' failed: %d\n" , name, rc); |
| 687 | goto out; |
| 688 | } |
| 689 | pr_info("loaded: %zu\n" , test_firmware->size); |
| 690 | rc = count; |
| 691 | |
| 692 | out: |
| 693 | mutex_unlock(lock: &test_fw_mutex); |
| 694 | |
| 695 | kfree(objp: name); |
| 696 | |
| 697 | return rc; |
| 698 | } |
| 699 | static DEVICE_ATTR_WO(trigger_request); |
| 700 | |
| 701 | #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE |
| 702 | extern struct list_head efi_embedded_fw_list; |
| 703 | extern bool efi_embedded_fw_checked; |
| 704 | |
| 705 | static ssize_t trigger_request_platform_store(struct device *dev, |
| 706 | struct device_attribute *attr, |
| 707 | const char *buf, size_t count) |
| 708 | { |
| 709 | static const u8 test_data[] = { |
| 710 | 0x55, 0xaa, 0x55, 0xaa, 0x01, 0x02, 0x03, 0x04, |
| 711 | 0x55, 0xaa, 0x55, 0xaa, 0x05, 0x06, 0x07, 0x08, |
| 712 | 0x55, 0xaa, 0x55, 0xaa, 0x10, 0x20, 0x30, 0x40, |
| 713 | 0x55, 0xaa, 0x55, 0xaa, 0x50, 0x60, 0x70, 0x80 |
| 714 | }; |
| 715 | struct efi_embedded_fw efi_embedded_fw; |
| 716 | const struct firmware *firmware = NULL; |
| 717 | bool saved_efi_embedded_fw_checked; |
| 718 | char *name; |
| 719 | int rc; |
| 720 | |
| 721 | name = kstrndup(s: buf, len: count, GFP_KERNEL); |
| 722 | if (!name) |
| 723 | return -ENOMEM; |
| 724 | |
| 725 | pr_info("inserting test platform fw '%s'\n" , name); |
| 726 | efi_embedded_fw.name = name; |
| 727 | efi_embedded_fw.data = (void *)test_data; |
| 728 | efi_embedded_fw.length = sizeof(test_data); |
| 729 | list_add(new: &efi_embedded_fw.list, head: &efi_embedded_fw_list); |
| 730 | saved_efi_embedded_fw_checked = efi_embedded_fw_checked; |
| 731 | efi_embedded_fw_checked = true; |
| 732 | |
| 733 | pr_info("loading '%s'\n" , name); |
| 734 | rc = firmware_request_platform(fw: &firmware, name, device: dev); |
| 735 | if (rc) { |
| 736 | pr_info("load of '%s' failed: %d\n" , name, rc); |
| 737 | goto out; |
| 738 | } |
| 739 | if (firmware->size != sizeof(test_data) || |
| 740 | memcmp(p: firmware->data, q: test_data, size: sizeof(test_data)) != 0) { |
| 741 | pr_info("firmware contents mismatch for '%s'\n" , name); |
| 742 | rc = -EINVAL; |
| 743 | goto out; |
| 744 | } |
| 745 | pr_info("loaded: %zu\n" , firmware->size); |
| 746 | rc = count; |
| 747 | |
| 748 | out: |
| 749 | efi_embedded_fw_checked = saved_efi_embedded_fw_checked; |
| 750 | release_firmware(fw: firmware); |
| 751 | list_del(entry: &efi_embedded_fw.list); |
| 752 | kfree(objp: name); |
| 753 | |
| 754 | return rc; |
| 755 | } |
| 756 | static DEVICE_ATTR_WO(trigger_request_platform); |
| 757 | #endif |
| 758 | |
| 759 | static DECLARE_COMPLETION(async_fw_done); |
| 760 | |
| 761 | static void trigger_async_request_cb(const struct firmware *fw, void *context) |
| 762 | { |
| 763 | test_firmware = fw; |
| 764 | complete(&async_fw_done); |
| 765 | } |
| 766 | |
| 767 | static ssize_t trigger_async_request_store(struct device *dev, |
| 768 | struct device_attribute *attr, |
| 769 | const char *buf, size_t count) |
| 770 | { |
| 771 | int rc; |
| 772 | char *name; |
| 773 | |
| 774 | name = kstrndup(s: buf, len: count, GFP_KERNEL); |
| 775 | if (!name) |
| 776 | return -ENOMEM; |
| 777 | |
| 778 | pr_info("loading '%s'\n" , name); |
| 779 | |
| 780 | mutex_lock(&test_fw_mutex); |
| 781 | release_firmware(fw: test_firmware); |
| 782 | test_firmware = NULL; |
| 783 | if (test_fw_config->reqs) |
| 784 | __test_release_all_firmware(); |
| 785 | rc = request_firmware_nowait(THIS_MODULE, uevent: 1, name, device: dev, GFP_KERNEL, |
| 786 | NULL, cont: trigger_async_request_cb); |
| 787 | if (rc) { |
| 788 | pr_info("async load of '%s' failed: %d\n" , name, rc); |
| 789 | kfree(objp: name); |
| 790 | goto out; |
| 791 | } |
| 792 | /* Free 'name' ASAP, to test for race conditions */ |
| 793 | kfree(objp: name); |
| 794 | |
| 795 | wait_for_completion(&async_fw_done); |
| 796 | |
| 797 | if (test_firmware) { |
| 798 | pr_info("loaded: %zu\n" , test_firmware->size); |
| 799 | rc = count; |
| 800 | } else { |
| 801 | pr_err("failed to async load firmware\n" ); |
| 802 | rc = -ENOMEM; |
| 803 | } |
| 804 | |
| 805 | out: |
| 806 | mutex_unlock(lock: &test_fw_mutex); |
| 807 | |
| 808 | return rc; |
| 809 | } |
| 810 | static DEVICE_ATTR_WO(trigger_async_request); |
| 811 | |
| 812 | static ssize_t trigger_custom_fallback_store(struct device *dev, |
| 813 | struct device_attribute *attr, |
| 814 | const char *buf, size_t count) |
| 815 | { |
| 816 | int rc; |
| 817 | char *name; |
| 818 | |
| 819 | name = kstrndup(s: buf, len: count, GFP_KERNEL); |
| 820 | if (!name) |
| 821 | return -ENOMEM; |
| 822 | |
| 823 | pr_info("loading '%s' using custom fallback mechanism\n" , name); |
| 824 | |
| 825 | mutex_lock(&test_fw_mutex); |
| 826 | release_firmware(fw: test_firmware); |
| 827 | if (test_fw_config->reqs) |
| 828 | __test_release_all_firmware(); |
| 829 | test_firmware = NULL; |
| 830 | rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOUEVENT, name, |
| 831 | device: dev, GFP_KERNEL, NULL, |
| 832 | cont: trigger_async_request_cb); |
| 833 | if (rc) { |
| 834 | pr_info("async load of '%s' failed: %d\n" , name, rc); |
| 835 | kfree(objp: name); |
| 836 | goto out; |
| 837 | } |
| 838 | /* Free 'name' ASAP, to test for race conditions */ |
| 839 | kfree(objp: name); |
| 840 | |
| 841 | wait_for_completion(&async_fw_done); |
| 842 | |
| 843 | if (test_firmware) { |
| 844 | pr_info("loaded: %zu\n" , test_firmware->size); |
| 845 | rc = count; |
| 846 | } else { |
| 847 | pr_err("failed to async load firmware\n" ); |
| 848 | rc = -ENODEV; |
| 849 | } |
| 850 | |
| 851 | out: |
| 852 | mutex_unlock(lock: &test_fw_mutex); |
| 853 | |
| 854 | return rc; |
| 855 | } |
| 856 | static DEVICE_ATTR_WO(trigger_custom_fallback); |
| 857 | |
| 858 | static int test_fw_run_batch_request(void *data) |
| 859 | { |
| 860 | struct test_batched_req *req = data; |
| 861 | |
| 862 | if (!req) { |
| 863 | test_fw_config->test_result = -EINVAL; |
| 864 | return -EINVAL; |
| 865 | } |
| 866 | |
| 867 | if (test_fw_config->into_buf) { |
| 868 | void *test_buf; |
| 869 | |
| 870 | test_buf = kzalloc(TEST_FIRMWARE_BUF_SIZE, GFP_KERNEL); |
| 871 | if (!test_buf) |
| 872 | return -ENOMEM; |
| 873 | |
| 874 | if (test_fw_config->partial) |
| 875 | req->rc = request_partial_firmware_into_buf |
| 876 | (firmware_p: &req->fw, |
| 877 | name: req->name, |
| 878 | device: req->dev, |
| 879 | buf: test_buf, |
| 880 | size: test_fw_config->buf_size, |
| 881 | offset: test_fw_config->file_offset); |
| 882 | else |
| 883 | req->rc = request_firmware_into_buf |
| 884 | (firmware_p: &req->fw, |
| 885 | name: req->name, |
| 886 | device: req->dev, |
| 887 | buf: test_buf, |
| 888 | size: test_fw_config->buf_size); |
| 889 | if (!req->fw) |
| 890 | kfree(objp: test_buf); |
| 891 | else |
| 892 | req->fw_buf = test_buf; |
| 893 | } else { |
| 894 | req->rc = test_fw_config->req_firmware(&req->fw, |
| 895 | req->name, |
| 896 | req->dev); |
| 897 | } |
| 898 | |
| 899 | if (req->rc) { |
| 900 | pr_info("#%u: batched sync load failed: %d\n" , |
| 901 | req->idx, req->rc); |
| 902 | if (!test_fw_config->test_result) |
| 903 | test_fw_config->test_result = req->rc; |
| 904 | } else if (req->fw) { |
| 905 | req->sent = true; |
| 906 | pr_info("#%u: batched sync loaded %zu\n" , |
| 907 | req->idx, req->fw->size); |
| 908 | } |
| 909 | complete(&req->completion); |
| 910 | |
| 911 | req->task = NULL; |
| 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * We use a kthread as otherwise the kernel serializes all our sync requests |
| 918 | * and we would not be able to mimic batched requests on a sync call. Batched |
| 919 | * requests on a sync call can for instance happen on a device driver when |
| 920 | * multiple cards are used and firmware loading happens outside of probe. |
| 921 | */ |
| 922 | static ssize_t trigger_batched_requests_store(struct device *dev, |
| 923 | struct device_attribute *attr, |
| 924 | const char *buf, size_t count) |
| 925 | { |
| 926 | struct test_batched_req *req; |
| 927 | int rc; |
| 928 | u8 i; |
| 929 | |
| 930 | mutex_lock(&test_fw_mutex); |
| 931 | |
| 932 | if (test_fw_config->reqs) { |
| 933 | rc = -EBUSY; |
| 934 | goto out_bail; |
| 935 | } |
| 936 | |
| 937 | test_fw_config->reqs = |
| 938 | vzalloc(array3_size(sizeof(struct test_batched_req), |
| 939 | test_fw_config->num_requests, 2)); |
| 940 | if (!test_fw_config->reqs) { |
| 941 | rc = -ENOMEM; |
| 942 | goto out_unlock; |
| 943 | } |
| 944 | |
| 945 | pr_info("batched sync firmware loading '%s' %u times\n" , |
| 946 | test_fw_config->name, test_fw_config->num_requests); |
| 947 | |
| 948 | for (i = 0; i < test_fw_config->num_requests; i++) { |
| 949 | req = &test_fw_config->reqs[i]; |
| 950 | req->fw = NULL; |
| 951 | req->idx = i; |
| 952 | req->name = test_fw_config->name; |
| 953 | req->fw_buf = NULL; |
| 954 | req->dev = dev; |
| 955 | init_completion(x: &req->completion); |
| 956 | req->task = kthread_run(test_fw_run_batch_request, req, |
| 957 | "%s-%u" , KBUILD_MODNAME, req->idx); |
| 958 | if (!req->task || IS_ERR(ptr: req->task)) { |
| 959 | pr_err("Setting up thread %u failed\n" , req->idx); |
| 960 | req->task = NULL; |
| 961 | rc = -ENOMEM; |
| 962 | goto out_bail; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | rc = count; |
| 967 | |
| 968 | /* |
| 969 | * We require an explicit release to enable more time and delay of |
| 970 | * calling release_firmware() to improve our chances of forcing a |
| 971 | * batched request. If we instead called release_firmware() right away |
| 972 | * then we might miss on an opportunity of having a successful firmware |
| 973 | * request pass on the opportunity to be come a batched request. |
| 974 | */ |
| 975 | |
| 976 | out_bail: |
| 977 | for (i = 0; i < test_fw_config->num_requests; i++) { |
| 978 | req = &test_fw_config->reqs[i]; |
| 979 | if (req->task || req->sent) |
| 980 | wait_for_completion(&req->completion); |
| 981 | } |
| 982 | |
| 983 | /* Override any worker error if we had a general setup error */ |
| 984 | if (rc < 0) |
| 985 | test_fw_config->test_result = rc; |
| 986 | |
| 987 | out_unlock: |
| 988 | mutex_unlock(lock: &test_fw_mutex); |
| 989 | |
| 990 | return rc; |
| 991 | } |
| 992 | static DEVICE_ATTR_WO(trigger_batched_requests); |
| 993 | |
| 994 | /* |
| 995 | * We wait for each callback to return with the lock held, no need to lock here |
| 996 | */ |
| 997 | static void trigger_batched_cb(const struct firmware *fw, void *context) |
| 998 | { |
| 999 | struct test_batched_req *req = context; |
| 1000 | |
| 1001 | if (!req) { |
| 1002 | test_fw_config->test_result = -EINVAL; |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | /* forces *some* batched requests to queue up */ |
| 1007 | if (!req->idx) |
| 1008 | ssleep(seconds: 2); |
| 1009 | |
| 1010 | req->fw = fw; |
| 1011 | |
| 1012 | /* |
| 1013 | * Unfortunately the firmware API gives us nothing other than a null FW |
| 1014 | * if the firmware was not found on async requests. Best we can do is |
| 1015 | * just assume -ENOENT. A better API would pass the actual return |
| 1016 | * value to the callback. |
| 1017 | */ |
| 1018 | if (!fw && !test_fw_config->test_result) |
| 1019 | test_fw_config->test_result = -ENOENT; |
| 1020 | |
| 1021 | complete(&req->completion); |
| 1022 | } |
| 1023 | |
| 1024 | static |
| 1025 | ssize_t trigger_batched_requests_async_store(struct device *dev, |
| 1026 | struct device_attribute *attr, |
| 1027 | const char *buf, size_t count) |
| 1028 | { |
| 1029 | struct test_batched_req *req; |
| 1030 | bool send_uevent; |
| 1031 | int rc; |
| 1032 | u8 i; |
| 1033 | |
| 1034 | mutex_lock(&test_fw_mutex); |
| 1035 | |
| 1036 | if (test_fw_config->reqs) { |
| 1037 | rc = -EBUSY; |
| 1038 | goto out_bail; |
| 1039 | } |
| 1040 | |
| 1041 | test_fw_config->reqs = |
| 1042 | vzalloc(array3_size(sizeof(struct test_batched_req), |
| 1043 | test_fw_config->num_requests, 2)); |
| 1044 | if (!test_fw_config->reqs) { |
| 1045 | rc = -ENOMEM; |
| 1046 | goto out; |
| 1047 | } |
| 1048 | |
| 1049 | pr_info("batched loading '%s' custom fallback mechanism %u times\n" , |
| 1050 | test_fw_config->name, test_fw_config->num_requests); |
| 1051 | |
| 1052 | send_uevent = test_fw_config->send_uevent ? FW_ACTION_UEVENT : |
| 1053 | FW_ACTION_NOUEVENT; |
| 1054 | |
| 1055 | for (i = 0; i < test_fw_config->num_requests; i++) { |
| 1056 | req = &test_fw_config->reqs[i]; |
| 1057 | req->name = test_fw_config->name; |
| 1058 | req->fw_buf = NULL; |
| 1059 | req->fw = NULL; |
| 1060 | req->idx = i; |
| 1061 | init_completion(x: &req->completion); |
| 1062 | rc = request_firmware_nowait(THIS_MODULE, uevent: send_uevent, |
| 1063 | name: req->name, |
| 1064 | device: dev, GFP_KERNEL, context: req, |
| 1065 | cont: trigger_batched_cb); |
| 1066 | if (rc) { |
| 1067 | pr_info("#%u: batched async load failed setup: %d\n" , |
| 1068 | i, rc); |
| 1069 | req->rc = rc; |
| 1070 | goto out_bail; |
| 1071 | } else |
| 1072 | req->sent = true; |
| 1073 | } |
| 1074 | |
| 1075 | rc = count; |
| 1076 | |
| 1077 | out_bail: |
| 1078 | |
| 1079 | /* |
| 1080 | * We require an explicit release to enable more time and delay of |
| 1081 | * calling release_firmware() to improve our chances of forcing a |
| 1082 | * batched request. If we instead called release_firmware() right away |
| 1083 | * then we might miss on an opportunity of having a successful firmware |
| 1084 | * request pass on the opportunity to be come a batched request. |
| 1085 | */ |
| 1086 | |
| 1087 | for (i = 0; i < test_fw_config->num_requests; i++) { |
| 1088 | req = &test_fw_config->reqs[i]; |
| 1089 | if (req->sent) |
| 1090 | wait_for_completion(&req->completion); |
| 1091 | } |
| 1092 | |
| 1093 | /* Override any worker error if we had a general setup error */ |
| 1094 | if (rc < 0) |
| 1095 | test_fw_config->test_result = rc; |
| 1096 | |
| 1097 | out: |
| 1098 | mutex_unlock(lock: &test_fw_mutex); |
| 1099 | |
| 1100 | return rc; |
| 1101 | } |
| 1102 | static DEVICE_ATTR_WO(trigger_batched_requests_async); |
| 1103 | |
| 1104 | static void upload_release(struct test_firmware_upload *tst) |
| 1105 | { |
| 1106 | firmware_upload_unregister(fw_upload: tst->fwl); |
| 1107 | kfree(objp: tst->buf); |
| 1108 | kfree(objp: tst->name); |
| 1109 | kfree(objp: tst); |
| 1110 | } |
| 1111 | |
| 1112 | static void upload_release_all(void) |
| 1113 | { |
| 1114 | struct test_firmware_upload *tst, *tmp; |
| 1115 | |
| 1116 | list_for_each_entry_safe(tst, tmp, &test_upload_list, node) { |
| 1117 | list_del(entry: &tst->node); |
| 1118 | upload_release(tst); |
| 1119 | } |
| 1120 | test_fw_config->upload_name = NULL; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * This table is replicated from .../firmware_loader/sysfs_upload.c |
| 1125 | * and needs to be kept in sync. |
| 1126 | */ |
| 1127 | static const char * const fw_upload_err_str[] = { |
| 1128 | [FW_UPLOAD_ERR_NONE] = "none" , |
| 1129 | [FW_UPLOAD_ERR_HW_ERROR] = "hw-error" , |
| 1130 | [FW_UPLOAD_ERR_TIMEOUT] = "timeout" , |
| 1131 | [FW_UPLOAD_ERR_CANCELED] = "user-abort" , |
| 1132 | [FW_UPLOAD_ERR_BUSY] = "device-busy" , |
| 1133 | [FW_UPLOAD_ERR_INVALID_SIZE] = "invalid-file-size" , |
| 1134 | [FW_UPLOAD_ERR_RW_ERROR] = "read-write-error" , |
| 1135 | [FW_UPLOAD_ERR_WEAROUT] = "flash-wearout" , |
| 1136 | [FW_UPLOAD_ERR_FW_INVALID] = "firmware-invalid" , |
| 1137 | }; |
| 1138 | |
| 1139 | static void upload_err_inject_error(struct test_firmware_upload *tst, |
| 1140 | const u8 *p, const char *prog) |
| 1141 | { |
| 1142 | enum fw_upload_err err; |
| 1143 | |
| 1144 | for (err = FW_UPLOAD_ERR_NONE + 1; err < FW_UPLOAD_ERR_MAX; err++) { |
| 1145 | if (strncmp(p, fw_upload_err_str[err], |
| 1146 | strlen(fw_upload_err_str[err])) == 0) { |
| 1147 | tst->inject.prog = prog; |
| 1148 | tst->inject.err_code = err; |
| 1149 | return; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | static void upload_err_inject_prog(struct test_firmware_upload *tst, |
| 1155 | const u8 *p) |
| 1156 | { |
| 1157 | static const char * const progs[] = { |
| 1158 | "preparing:" , "transferring:" , "programming:" |
| 1159 | }; |
| 1160 | int i; |
| 1161 | |
| 1162 | for (i = 0; i < ARRAY_SIZE(progs); i++) { |
| 1163 | if (strncmp(p, progs[i], strlen(progs[i])) == 0) { |
| 1164 | upload_err_inject_error(tst, p: p + strlen(progs[i]), |
| 1165 | prog: progs[i]); |
| 1166 | return; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | #define FIVE_MINUTES_MS (5 * 60 * 1000) |
| 1172 | static enum fw_upload_err |
| 1173 | fw_upload_wait_on_cancel(struct test_firmware_upload *tst) |
| 1174 | { |
| 1175 | int ms_delay; |
| 1176 | |
| 1177 | for (ms_delay = 0; ms_delay < FIVE_MINUTES_MS; ms_delay += 100) { |
| 1178 | msleep(msecs: 100); |
| 1179 | if (tst->cancel_request) |
| 1180 | return FW_UPLOAD_ERR_CANCELED; |
| 1181 | } |
| 1182 | return FW_UPLOAD_ERR_NONE; |
| 1183 | } |
| 1184 | |
| 1185 | static enum fw_upload_err test_fw_upload_prepare(struct fw_upload *fwl, |
| 1186 | const u8 *data, u32 size) |
| 1187 | { |
| 1188 | struct test_firmware_upload *tst = fwl->dd_handle; |
| 1189 | enum fw_upload_err ret = FW_UPLOAD_ERR_NONE; |
| 1190 | const char *progress = "preparing:" ; |
| 1191 | |
| 1192 | tst->cancel_request = false; |
| 1193 | |
| 1194 | if (!size || size > TEST_UPLOAD_MAX_SIZE) { |
| 1195 | ret = FW_UPLOAD_ERR_INVALID_SIZE; |
| 1196 | goto err_out; |
| 1197 | } |
| 1198 | |
| 1199 | if (strncmp(data, "inject:" , strlen("inject:" )) == 0) |
| 1200 | upload_err_inject_prog(tst, p: data + strlen("inject:" )); |
| 1201 | |
| 1202 | memset(tst->buf, 0, TEST_UPLOAD_MAX_SIZE); |
| 1203 | tst->size = size; |
| 1204 | |
| 1205 | if (tst->inject.err_code == FW_UPLOAD_ERR_NONE || |
| 1206 | strncmp(tst->inject.prog, progress, strlen(progress)) != 0) |
| 1207 | return FW_UPLOAD_ERR_NONE; |
| 1208 | |
| 1209 | if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED) |
| 1210 | ret = fw_upload_wait_on_cancel(tst); |
| 1211 | else |
| 1212 | ret = tst->inject.err_code; |
| 1213 | |
| 1214 | err_out: |
| 1215 | /* |
| 1216 | * The cleanup op only executes if the prepare op succeeds. |
| 1217 | * If the prepare op fails, it must do it's own clean-up. |
| 1218 | */ |
| 1219 | tst->inject.err_code = FW_UPLOAD_ERR_NONE; |
| 1220 | tst->inject.prog = NULL; |
| 1221 | |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl, |
| 1226 | const u8 *data, u32 offset, |
| 1227 | u32 size, u32 *written) |
| 1228 | { |
| 1229 | struct test_firmware_upload *tst = fwl->dd_handle; |
| 1230 | const char *progress = "transferring:" ; |
| 1231 | u32 blk_size; |
| 1232 | |
| 1233 | if (tst->cancel_request) |
| 1234 | return FW_UPLOAD_ERR_CANCELED; |
| 1235 | |
| 1236 | blk_size = min_t(u32, TEST_UPLOAD_BLK_SIZE, size); |
| 1237 | memcpy(tst->buf + offset, data + offset, blk_size); |
| 1238 | |
| 1239 | *written = blk_size; |
| 1240 | |
| 1241 | if (tst->inject.err_code == FW_UPLOAD_ERR_NONE || |
| 1242 | strncmp(tst->inject.prog, progress, strlen(progress)) != 0) |
| 1243 | return FW_UPLOAD_ERR_NONE; |
| 1244 | |
| 1245 | if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED) |
| 1246 | return fw_upload_wait_on_cancel(tst); |
| 1247 | |
| 1248 | return tst->inject.err_code; |
| 1249 | } |
| 1250 | |
| 1251 | static enum fw_upload_err test_fw_upload_complete(struct fw_upload *fwl) |
| 1252 | { |
| 1253 | struct test_firmware_upload *tst = fwl->dd_handle; |
| 1254 | const char *progress = "programming:" ; |
| 1255 | |
| 1256 | if (tst->cancel_request) |
| 1257 | return FW_UPLOAD_ERR_CANCELED; |
| 1258 | |
| 1259 | if (tst->inject.err_code == FW_UPLOAD_ERR_NONE || |
| 1260 | strncmp(tst->inject.prog, progress, strlen(progress)) != 0) |
| 1261 | return FW_UPLOAD_ERR_NONE; |
| 1262 | |
| 1263 | if (tst->inject.err_code == FW_UPLOAD_ERR_CANCELED) |
| 1264 | return fw_upload_wait_on_cancel(tst); |
| 1265 | |
| 1266 | return tst->inject.err_code; |
| 1267 | } |
| 1268 | |
| 1269 | static void test_fw_upload_cancel(struct fw_upload *fwl) |
| 1270 | { |
| 1271 | struct test_firmware_upload *tst = fwl->dd_handle; |
| 1272 | |
| 1273 | tst->cancel_request = true; |
| 1274 | } |
| 1275 | |
| 1276 | static void test_fw_cleanup(struct fw_upload *fwl) |
| 1277 | { |
| 1278 | struct test_firmware_upload *tst = fwl->dd_handle; |
| 1279 | |
| 1280 | tst->inject.err_code = FW_UPLOAD_ERR_NONE; |
| 1281 | tst->inject.prog = NULL; |
| 1282 | } |
| 1283 | |
| 1284 | static const struct fw_upload_ops upload_test_ops = { |
| 1285 | .prepare = test_fw_upload_prepare, |
| 1286 | .write = test_fw_upload_write, |
| 1287 | .poll_complete = test_fw_upload_complete, |
| 1288 | .cancel = test_fw_upload_cancel, |
| 1289 | .cleanup = test_fw_cleanup |
| 1290 | }; |
| 1291 | |
| 1292 | static ssize_t upload_register_store(struct device *dev, |
| 1293 | struct device_attribute *attr, |
| 1294 | const char *buf, size_t count) |
| 1295 | { |
| 1296 | struct test_firmware_upload *tst; |
| 1297 | struct fw_upload *fwl; |
| 1298 | char *name; |
| 1299 | int ret; |
| 1300 | |
| 1301 | name = kstrndup(s: buf, len: count, GFP_KERNEL); |
| 1302 | if (!name) |
| 1303 | return -ENOMEM; |
| 1304 | |
| 1305 | mutex_lock(&test_fw_mutex); |
| 1306 | tst = upload_lookup_name(name); |
| 1307 | if (tst) { |
| 1308 | ret = -EEXIST; |
| 1309 | goto free_name; |
| 1310 | } |
| 1311 | |
| 1312 | tst = kzalloc(sizeof(*tst), GFP_KERNEL); |
| 1313 | if (!tst) { |
| 1314 | ret = -ENOMEM; |
| 1315 | goto free_name; |
| 1316 | } |
| 1317 | |
| 1318 | tst->name = name; |
| 1319 | tst->buf = kzalloc(TEST_UPLOAD_MAX_SIZE, GFP_KERNEL); |
| 1320 | if (!tst->buf) { |
| 1321 | ret = -ENOMEM; |
| 1322 | goto free_tst; |
| 1323 | } |
| 1324 | |
| 1325 | fwl = firmware_upload_register(THIS_MODULE, parent: dev, name: tst->name, |
| 1326 | ops: &upload_test_ops, dd_handle: tst); |
| 1327 | if (IS_ERR(ptr: fwl)) { |
| 1328 | ret = PTR_ERR(ptr: fwl); |
| 1329 | goto free_buf; |
| 1330 | } |
| 1331 | |
| 1332 | tst->fwl = fwl; |
| 1333 | list_add_tail(new: &tst->node, head: &test_upload_list); |
| 1334 | mutex_unlock(lock: &test_fw_mutex); |
| 1335 | return count; |
| 1336 | |
| 1337 | free_buf: |
| 1338 | kfree(objp: tst->buf); |
| 1339 | |
| 1340 | free_tst: |
| 1341 | kfree(objp: tst); |
| 1342 | |
| 1343 | free_name: |
| 1344 | mutex_unlock(lock: &test_fw_mutex); |
| 1345 | kfree(objp: name); |
| 1346 | |
| 1347 | return ret; |
| 1348 | } |
| 1349 | static DEVICE_ATTR_WO(upload_register); |
| 1350 | |
| 1351 | static ssize_t upload_unregister_store(struct device *dev, |
| 1352 | struct device_attribute *attr, |
| 1353 | const char *buf, size_t count) |
| 1354 | { |
| 1355 | struct test_firmware_upload *tst; |
| 1356 | int ret = count; |
| 1357 | |
| 1358 | mutex_lock(&test_fw_mutex); |
| 1359 | tst = upload_lookup_name(name: buf); |
| 1360 | if (!tst) { |
| 1361 | ret = -EINVAL; |
| 1362 | goto out; |
| 1363 | } |
| 1364 | |
| 1365 | if (test_fw_config->upload_name == tst->name) |
| 1366 | test_fw_config->upload_name = NULL; |
| 1367 | |
| 1368 | list_del(entry: &tst->node); |
| 1369 | upload_release(tst); |
| 1370 | |
| 1371 | out: |
| 1372 | mutex_unlock(lock: &test_fw_mutex); |
| 1373 | return ret; |
| 1374 | } |
| 1375 | static DEVICE_ATTR_WO(upload_unregister); |
| 1376 | |
| 1377 | static ssize_t test_result_show(struct device *dev, |
| 1378 | struct device_attribute *attr, |
| 1379 | char *buf) |
| 1380 | { |
| 1381 | return test_dev_config_show_int(buf, val: test_fw_config->test_result); |
| 1382 | } |
| 1383 | static DEVICE_ATTR_RO(test_result); |
| 1384 | |
| 1385 | static ssize_t release_all_firmware_store(struct device *dev, |
| 1386 | struct device_attribute *attr, |
| 1387 | const char *buf, size_t count) |
| 1388 | { |
| 1389 | test_release_all_firmware(); |
| 1390 | return count; |
| 1391 | } |
| 1392 | static DEVICE_ATTR_WO(release_all_firmware); |
| 1393 | |
| 1394 | static ssize_t read_firmware_show(struct device *dev, |
| 1395 | struct device_attribute *attr, |
| 1396 | char *buf) |
| 1397 | { |
| 1398 | struct test_batched_req *req; |
| 1399 | u8 idx; |
| 1400 | ssize_t rc = 0; |
| 1401 | |
| 1402 | mutex_lock(&test_fw_mutex); |
| 1403 | |
| 1404 | idx = test_fw_config->read_fw_idx; |
| 1405 | if (idx >= test_fw_config->num_requests) { |
| 1406 | rc = -ERANGE; |
| 1407 | goto out; |
| 1408 | } |
| 1409 | |
| 1410 | if (!test_fw_config->reqs) { |
| 1411 | rc = -EINVAL; |
| 1412 | goto out; |
| 1413 | } |
| 1414 | |
| 1415 | req = &test_fw_config->reqs[idx]; |
| 1416 | if (!req->fw) { |
| 1417 | pr_err("#%u: failed to async load firmware\n" , idx); |
| 1418 | rc = -ENOENT; |
| 1419 | goto out; |
| 1420 | } |
| 1421 | |
| 1422 | pr_info("#%u: loaded %zu\n" , idx, req->fw->size); |
| 1423 | |
| 1424 | if (req->fw->size > PAGE_SIZE) { |
| 1425 | pr_err("Testing interface must use PAGE_SIZE firmware for now\n" ); |
| 1426 | rc = -EINVAL; |
| 1427 | goto out; |
| 1428 | } |
| 1429 | memcpy(buf, req->fw->data, req->fw->size); |
| 1430 | |
| 1431 | rc = req->fw->size; |
| 1432 | out: |
| 1433 | mutex_unlock(lock: &test_fw_mutex); |
| 1434 | |
| 1435 | return rc; |
| 1436 | } |
| 1437 | static DEVICE_ATTR_RO(read_firmware); |
| 1438 | |
| 1439 | static ssize_t upload_read_show(struct device *dev, |
| 1440 | struct device_attribute *attr, |
| 1441 | char *buf) |
| 1442 | { |
| 1443 | struct test_firmware_upload *tst = NULL; |
| 1444 | struct test_firmware_upload *tst_iter; |
| 1445 | int ret = -EINVAL; |
| 1446 | |
| 1447 | if (!test_fw_config->upload_name) { |
| 1448 | pr_err("Set config_upload_name before using upload_read\n" ); |
| 1449 | return -EINVAL; |
| 1450 | } |
| 1451 | |
| 1452 | mutex_lock(&test_fw_mutex); |
| 1453 | list_for_each_entry(tst_iter, &test_upload_list, node) |
| 1454 | if (tst_iter->name == test_fw_config->upload_name) { |
| 1455 | tst = tst_iter; |
| 1456 | break; |
| 1457 | } |
| 1458 | |
| 1459 | if (!tst) { |
| 1460 | pr_err("Firmware name not found: %s\n" , |
| 1461 | test_fw_config->upload_name); |
| 1462 | goto out; |
| 1463 | } |
| 1464 | |
| 1465 | if (tst->size > PAGE_SIZE) { |
| 1466 | pr_err("Testing interface must use PAGE_SIZE firmware for now\n" ); |
| 1467 | goto out; |
| 1468 | } |
| 1469 | |
| 1470 | memcpy(buf, tst->buf, tst->size); |
| 1471 | ret = tst->size; |
| 1472 | out: |
| 1473 | mutex_unlock(lock: &test_fw_mutex); |
| 1474 | return ret; |
| 1475 | } |
| 1476 | static DEVICE_ATTR_RO(upload_read); |
| 1477 | |
| 1478 | #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr |
| 1479 | |
| 1480 | static struct attribute *test_dev_attrs[] = { |
| 1481 | TEST_FW_DEV_ATTR(reset), |
| 1482 | |
| 1483 | TEST_FW_DEV_ATTR(config), |
| 1484 | TEST_FW_DEV_ATTR(config_name), |
| 1485 | TEST_FW_DEV_ATTR(config_num_requests), |
| 1486 | TEST_FW_DEV_ATTR(config_into_buf), |
| 1487 | TEST_FW_DEV_ATTR(config_buf_size), |
| 1488 | TEST_FW_DEV_ATTR(config_file_offset), |
| 1489 | TEST_FW_DEV_ATTR(config_partial), |
| 1490 | TEST_FW_DEV_ATTR(config_sync_direct), |
| 1491 | TEST_FW_DEV_ATTR(config_send_uevent), |
| 1492 | TEST_FW_DEV_ATTR(config_read_fw_idx), |
| 1493 | TEST_FW_DEV_ATTR(config_upload_name), |
| 1494 | |
| 1495 | /* These don't use the config at all - they could be ported! */ |
| 1496 | TEST_FW_DEV_ATTR(trigger_request), |
| 1497 | TEST_FW_DEV_ATTR(trigger_async_request), |
| 1498 | TEST_FW_DEV_ATTR(trigger_custom_fallback), |
| 1499 | #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE |
| 1500 | TEST_FW_DEV_ATTR(trigger_request_platform), |
| 1501 | #endif |
| 1502 | |
| 1503 | /* These use the config and can use the test_result */ |
| 1504 | TEST_FW_DEV_ATTR(trigger_batched_requests), |
| 1505 | TEST_FW_DEV_ATTR(trigger_batched_requests_async), |
| 1506 | |
| 1507 | TEST_FW_DEV_ATTR(release_all_firmware), |
| 1508 | TEST_FW_DEV_ATTR(test_result), |
| 1509 | TEST_FW_DEV_ATTR(read_firmware), |
| 1510 | TEST_FW_DEV_ATTR(upload_read), |
| 1511 | TEST_FW_DEV_ATTR(upload_register), |
| 1512 | TEST_FW_DEV_ATTR(upload_unregister), |
| 1513 | NULL, |
| 1514 | }; |
| 1515 | |
| 1516 | ATTRIBUTE_GROUPS(test_dev); |
| 1517 | |
| 1518 | static struct miscdevice test_fw_misc_device = { |
| 1519 | .minor = MISC_DYNAMIC_MINOR, |
| 1520 | .name = "test_firmware" , |
| 1521 | .fops = &test_fw_fops, |
| 1522 | .groups = test_dev_groups, |
| 1523 | }; |
| 1524 | |
| 1525 | static int __init test_firmware_init(void) |
| 1526 | { |
| 1527 | int rc; |
| 1528 | |
| 1529 | test_fw_config = kzalloc(sizeof(struct test_config), GFP_KERNEL); |
| 1530 | if (!test_fw_config) |
| 1531 | return -ENOMEM; |
| 1532 | |
| 1533 | rc = __test_firmware_config_init(); |
| 1534 | if (rc) { |
| 1535 | kfree(objp: test_fw_config); |
| 1536 | pr_err("could not init firmware test config: %d\n" , rc); |
| 1537 | return rc; |
| 1538 | } |
| 1539 | |
| 1540 | rc = misc_register(misc: &test_fw_misc_device); |
| 1541 | if (rc) { |
| 1542 | __test_firmware_config_free(); |
| 1543 | kfree(objp: test_fw_config); |
| 1544 | pr_err("could not register misc device: %d\n" , rc); |
| 1545 | return rc; |
| 1546 | } |
| 1547 | |
| 1548 | pr_warn("interface ready\n" ); |
| 1549 | |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | module_init(test_firmware_init); |
| 1554 | |
| 1555 | static void __exit test_firmware_exit(void) |
| 1556 | { |
| 1557 | mutex_lock(&test_fw_mutex); |
| 1558 | release_firmware(fw: test_firmware); |
| 1559 | misc_deregister(misc: &test_fw_misc_device); |
| 1560 | upload_release_all(); |
| 1561 | __test_firmware_config_free(); |
| 1562 | kfree(objp: test_fw_config); |
| 1563 | mutex_unlock(lock: &test_fw_mutex); |
| 1564 | |
| 1565 | pr_warn("removed interface\n" ); |
| 1566 | } |
| 1567 | |
| 1568 | module_exit(test_firmware_exit); |
| 1569 | |
| 1570 | MODULE_AUTHOR("Kees Cook <keescook@chromium.org>" ); |
| 1571 | MODULE_DESCRIPTION("interface to trigger and test firmware loading" ); |
| 1572 | MODULE_LICENSE("GPL" ); |
| 1573 | |