test_firmware.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/printk.h>
  14. #include <linux/completion.h>
  15. #include <linux/firmware.h>
  16. #include <linux/device.h>
  17. #include <linux/fs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/sizes.h>
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/delay.h>
  23. #include <linux/kthread.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/efi_embedded_fw.h>
  26. MODULE_IMPORT_NS(TEST_FIRMWARE);
  27. #define TEST_FIRMWARE_NAME "test-firmware.bin"
  28. #define TEST_FIRMWARE_NUM_REQS 4
  29. #define TEST_FIRMWARE_BUF_SIZE SZ_1K
  30. static DEFINE_MUTEX(test_fw_mutex);
  31. static const struct firmware *test_firmware;
  32. struct test_batched_req {
  33. u8 idx;
  34. int rc;
  35. bool sent;
  36. const struct firmware *fw;
  37. const char *name;
  38. struct completion completion;
  39. struct task_struct *task;
  40. struct device *dev;
  41. };
  42. /**
  43. * test_config - represents configuration for the test for different triggers
  44. *
  45. * @name: the name of the firmware file to look for
  46. * @into_buf: when the into_buf is used if this is true
  47. * request_firmware_into_buf() will be used instead.
  48. * @buf_size: size of buf to allocate when into_buf is true
  49. * @file_offset: file offset to request when calling request_firmware_into_buf
  50. * @partial: partial read opt when calling request_firmware_into_buf
  51. * @sync_direct: when the sync trigger is used if this is true
  52. * request_firmware_direct() will be used instead.
  53. * @send_uevent: whether or not to send a uevent for async requests
  54. * @num_requests: number of requests to try per test case. This is trigger
  55. * specific.
  56. * @reqs: stores all requests information
  57. * @read_fw_idx: index of thread from which we want to read firmware results
  58. * from through the read_fw trigger.
  59. * @test_result: a test may use this to collect the result from the call
  60. * of the request_firmware*() calls used in their tests. In order of
  61. * priority we always keep first any setup error. If no setup errors were
  62. * found then we move on to the first error encountered while running the
  63. * API. Note that for async calls this typically will be a successful
  64. * result (0) unless of course you've used bogus parameters, or the system
  65. * is out of memory. In the async case the callback is expected to do a
  66. * bit more homework to figure out what happened, unfortunately the only
  67. * information passed today on error is the fact that no firmware was
  68. * found so we can only assume -ENOENT on async calls if the firmware is
  69. * NULL.
  70. *
  71. * Errors you can expect:
  72. *
  73. * API specific:
  74. *
  75. * 0: success for sync, for async it means request was sent
  76. * -EINVAL: invalid parameters or request
  77. * -ENOENT: files not found
  78. *
  79. * System environment:
  80. *
  81. * -ENOMEM: memory pressure on system
  82. * -ENODEV: out of number of devices to test
  83. * -EINVAL: an unexpected error has occurred
  84. * @req_firmware: if @sync_direct is true this is set to
  85. * request_firmware_direct(), otherwise request_firmware()
  86. */
  87. struct test_config {
  88. char *name;
  89. bool into_buf;
  90. size_t buf_size;
  91. size_t file_offset;
  92. bool partial;
  93. bool sync_direct;
  94. bool send_uevent;
  95. u8 num_requests;
  96. u8 read_fw_idx;
  97. /*
  98. * These below don't belong her but we'll move them once we create
  99. * a struct fw_test_device and stuff the misc_dev under there later.
  100. */
  101. struct test_batched_req *reqs;
  102. int test_result;
  103. int (*req_firmware)(const struct firmware **fw, const char *name,
  104. struct device *device);
  105. };
  106. static struct test_config *test_fw_config;
  107. static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
  108. size_t size, loff_t *offset)
  109. {
  110. ssize_t rc = 0;
  111. mutex_lock(&test_fw_mutex);
  112. if (test_firmware)
  113. rc = simple_read_from_buffer(buf, size, offset,
  114. test_firmware->data,
  115. test_firmware->size);
  116. mutex_unlock(&test_fw_mutex);
  117. return rc;
  118. }
  119. static const struct file_operations test_fw_fops = {
  120. .owner = THIS_MODULE,
  121. .read = test_fw_misc_read,
  122. };
  123. static void __test_release_all_firmware(void)
  124. {
  125. struct test_batched_req *req;
  126. u8 i;
  127. if (!test_fw_config->reqs)
  128. return;
  129. for (i = 0; i < test_fw_config->num_requests; i++) {
  130. req = &test_fw_config->reqs[i];
  131. if (req->fw)
  132. release_firmware(req->fw);
  133. }
  134. vfree(test_fw_config->reqs);
  135. test_fw_config->reqs = NULL;
  136. }
  137. static void test_release_all_firmware(void)
  138. {
  139. mutex_lock(&test_fw_mutex);
  140. __test_release_all_firmware();
  141. mutex_unlock(&test_fw_mutex);
  142. }
  143. static void __test_firmware_config_free(void)
  144. {
  145. __test_release_all_firmware();
  146. kfree_const(test_fw_config->name);
  147. test_fw_config->name = NULL;
  148. }
  149. /*
  150. * XXX: move to kstrncpy() once merged.
  151. *
  152. * Users should use kfree_const() when freeing these.
  153. */
  154. static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
  155. {
  156. *dst = kstrndup(name, count, gfp);
  157. if (!*dst)
  158. return -ENOSPC;
  159. return count;
  160. }
  161. static int __test_firmware_config_init(void)
  162. {
  163. int ret;
  164. ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME,
  165. strlen(TEST_FIRMWARE_NAME), GFP_KERNEL);
  166. if (ret < 0)
  167. goto out;
  168. test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS;
  169. test_fw_config->send_uevent = true;
  170. test_fw_config->into_buf = false;
  171. test_fw_config->buf_size = TEST_FIRMWARE_BUF_SIZE;
  172. test_fw_config->file_offset = 0;
  173. test_fw_config->partial = false;
  174. test_fw_config->sync_direct = false;
  175. test_fw_config->req_firmware = request_firmware;
  176. test_fw_config->test_result = 0;
  177. test_fw_config->reqs = NULL;
  178. return 0;
  179. out:
  180. __test_firmware_config_free();
  181. return ret;
  182. }
  183. static ssize_t reset_store(struct device *dev,
  184. struct device_attribute *attr,
  185. const char *buf, size_t count)
  186. {
  187. int ret;
  188. mutex_lock(&test_fw_mutex);
  189. __test_firmware_config_free();
  190. ret = __test_firmware_config_init();
  191. if (ret < 0) {
  192. ret = -ENOMEM;
  193. pr_err("could not alloc settings for config trigger: %d\n",
  194. ret);
  195. goto out;
  196. }
  197. pr_info("reset\n");
  198. ret = count;
  199. out:
  200. mutex_unlock(&test_fw_mutex);
  201. return ret;
  202. }
  203. static DEVICE_ATTR_WO(reset);
  204. static ssize_t config_show(struct device *dev,
  205. struct device_attribute *attr,
  206. char *buf)
  207. {
  208. int len = 0;
  209. mutex_lock(&test_fw_mutex);
  210. len += scnprintf(buf, PAGE_SIZE - len,
  211. "Custom trigger configuration for: %s\n",
  212. dev_name(dev));
  213. if (test_fw_config->name)
  214. len += scnprintf(buf + len, PAGE_SIZE - len,
  215. "name:\t%s\n",
  216. test_fw_config->name);
  217. else
  218. len += scnprintf(buf + len, PAGE_SIZE - len,
  219. "name:\tEMTPY\n");
  220. len += scnprintf(buf + len, PAGE_SIZE - len,
  221. "num_requests:\t%u\n", test_fw_config->num_requests);
  222. len += scnprintf(buf + len, PAGE_SIZE - len,
  223. "send_uevent:\t\t%s\n",
  224. test_fw_config->send_uevent ?
  225. "FW_ACTION_HOTPLUG" :
  226. "FW_ACTION_NOHOTPLUG");
  227. len += scnprintf(buf + len, PAGE_SIZE - len,
  228. "into_buf:\t\t%s\n",
  229. test_fw_config->into_buf ? "true" : "false");
  230. len += scnprintf(buf + len, PAGE_SIZE - len,
  231. "buf_size:\t%zu\n", test_fw_config->buf_size);
  232. len += scnprintf(buf + len, PAGE_SIZE - len,
  233. "file_offset:\t%zu\n", test_fw_config->file_offset);
  234. len += scnprintf(buf + len, PAGE_SIZE - len,
  235. "partial:\t\t%s\n",
  236. test_fw_config->partial ? "true" : "false");
  237. len += scnprintf(buf + len, PAGE_SIZE - len,
  238. "sync_direct:\t\t%s\n",
  239. test_fw_config->sync_direct ? "true" : "false");
  240. len += scnprintf(buf + len, PAGE_SIZE - len,
  241. "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx);
  242. mutex_unlock(&test_fw_mutex);
  243. return len;
  244. }
  245. static DEVICE_ATTR_RO(config);
  246. static ssize_t config_name_store(struct device *dev,
  247. struct device_attribute *attr,
  248. const char *buf, size_t count)
  249. {
  250. int ret;
  251. mutex_lock(&test_fw_mutex);
  252. kfree_const(test_fw_config->name);
  253. ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL);
  254. mutex_unlock(&test_fw_mutex);
  255. return ret;
  256. }
  257. /*
  258. * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
  259. */
  260. static ssize_t config_test_show_str(char *dst,
  261. char *src)
  262. {
  263. int len;
  264. mutex_lock(&test_fw_mutex);
  265. len = snprintf(dst, PAGE_SIZE, "%s\n", src);
  266. mutex_unlock(&test_fw_mutex);
  267. return len;
  268. }
  269. static int test_dev_config_update_bool(const char *buf, size_t size,
  270. bool *cfg)
  271. {
  272. int ret;
  273. mutex_lock(&test_fw_mutex);
  274. if (strtobool(buf, cfg) < 0)
  275. ret = -EINVAL;
  276. else
  277. ret = size;
  278. mutex_unlock(&test_fw_mutex);
  279. return ret;
  280. }
  281. static ssize_t test_dev_config_show_bool(char *buf, bool val)
  282. {
  283. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  284. }
  285. static int test_dev_config_update_size_t(const char *buf,
  286. size_t size,
  287. size_t *cfg)
  288. {
  289. int ret;
  290. long new;
  291. ret = kstrtol(buf, 10, &new);
  292. if (ret)
  293. return ret;
  294. mutex_lock(&test_fw_mutex);
  295. *(size_t *)cfg = new;
  296. mutex_unlock(&test_fw_mutex);
  297. /* Always return full write size even if we didn't consume all */
  298. return size;
  299. }
  300. static ssize_t test_dev_config_show_size_t(char *buf, size_t val)
  301. {
  302. return snprintf(buf, PAGE_SIZE, "%zu\n", val);
  303. }
  304. static ssize_t test_dev_config_show_int(char *buf, int val)
  305. {
  306. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  307. }
  308. static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
  309. {
  310. int ret;
  311. long new;
  312. ret = kstrtol(buf, 10, &new);
  313. if (ret)
  314. return ret;
  315. if (new > U8_MAX)
  316. return -EINVAL;
  317. mutex_lock(&test_fw_mutex);
  318. *(u8 *)cfg = new;
  319. mutex_unlock(&test_fw_mutex);
  320. /* Always return full write size even if we didn't consume all */
  321. return size;
  322. }
  323. static ssize_t test_dev_config_show_u8(char *buf, u8 val)
  324. {
  325. return snprintf(buf, PAGE_SIZE, "%u\n", val);
  326. }
  327. static ssize_t config_name_show(struct device *dev,
  328. struct device_attribute *attr,
  329. char *buf)
  330. {
  331. return config_test_show_str(buf, test_fw_config->name);
  332. }
  333. static DEVICE_ATTR_RW(config_name);
  334. static ssize_t config_num_requests_store(struct device *dev,
  335. struct device_attribute *attr,
  336. const char *buf, size_t count)
  337. {
  338. int rc;
  339. mutex_lock(&test_fw_mutex);
  340. if (test_fw_config->reqs) {
  341. pr_err("Must call release_all_firmware prior to changing config\n");
  342. rc = -EINVAL;
  343. mutex_unlock(&test_fw_mutex);
  344. goto out;
  345. }
  346. mutex_unlock(&test_fw_mutex);
  347. rc = test_dev_config_update_u8(buf, count,
  348. &test_fw_config->num_requests);
  349. out:
  350. return rc;
  351. }
  352. static ssize_t config_num_requests_show(struct device *dev,
  353. struct device_attribute *attr,
  354. char *buf)
  355. {
  356. return test_dev_config_show_u8(buf, test_fw_config->num_requests);
  357. }
  358. static DEVICE_ATTR_RW(config_num_requests);
  359. static ssize_t config_into_buf_store(struct device *dev,
  360. struct device_attribute *attr,
  361. const char *buf, size_t count)
  362. {
  363. return test_dev_config_update_bool(buf,
  364. count,
  365. &test_fw_config->into_buf);
  366. }
  367. static ssize_t config_into_buf_show(struct device *dev,
  368. struct device_attribute *attr,
  369. char *buf)
  370. {
  371. return test_dev_config_show_bool(buf, test_fw_config->into_buf);
  372. }
  373. static DEVICE_ATTR_RW(config_into_buf);
  374. static ssize_t config_buf_size_store(struct device *dev,
  375. struct device_attribute *attr,
  376. const char *buf, size_t count)
  377. {
  378. int rc;
  379. mutex_lock(&test_fw_mutex);
  380. if (test_fw_config->reqs) {
  381. pr_err("Must call release_all_firmware prior to changing config\n");
  382. rc = -EINVAL;
  383. mutex_unlock(&test_fw_mutex);
  384. goto out;
  385. }
  386. mutex_unlock(&test_fw_mutex);
  387. rc = test_dev_config_update_size_t(buf, count,
  388. &test_fw_config->buf_size);
  389. out:
  390. return rc;
  391. }
  392. static ssize_t config_buf_size_show(struct device *dev,
  393. struct device_attribute *attr,
  394. char *buf)
  395. {
  396. return test_dev_config_show_size_t(buf, test_fw_config->buf_size);
  397. }
  398. static DEVICE_ATTR_RW(config_buf_size);
  399. static ssize_t config_file_offset_store(struct device *dev,
  400. struct device_attribute *attr,
  401. const char *buf, size_t count)
  402. {
  403. int rc;
  404. mutex_lock(&test_fw_mutex);
  405. if (test_fw_config->reqs) {
  406. pr_err("Must call release_all_firmware prior to changing config\n");
  407. rc = -EINVAL;
  408. mutex_unlock(&test_fw_mutex);
  409. goto out;
  410. }
  411. mutex_unlock(&test_fw_mutex);
  412. rc = test_dev_config_update_size_t(buf, count,
  413. &test_fw_config->file_offset);
  414. out:
  415. return rc;
  416. }
  417. static ssize_t config_file_offset_show(struct device *dev,
  418. struct device_attribute *attr,
  419. char *buf)
  420. {
  421. return test_dev_config_show_size_t(buf, test_fw_config->file_offset);
  422. }
  423. static DEVICE_ATTR_RW(config_file_offset);
  424. static ssize_t config_partial_store(struct device *dev,
  425. struct device_attribute *attr,
  426. const char *buf, size_t count)
  427. {
  428. return test_dev_config_update_bool(buf,
  429. count,
  430. &test_fw_config->partial);
  431. }
  432. static ssize_t config_partial_show(struct device *dev,
  433. struct device_attribute *attr,
  434. char *buf)
  435. {
  436. return test_dev_config_show_bool(buf, test_fw_config->partial);
  437. }
  438. static DEVICE_ATTR_RW(config_partial);
  439. static ssize_t config_sync_direct_store(struct device *dev,
  440. struct device_attribute *attr,
  441. const char *buf, size_t count)
  442. {
  443. int rc = test_dev_config_update_bool(buf, count,
  444. &test_fw_config->sync_direct);
  445. if (rc == count)
  446. test_fw_config->req_firmware = test_fw_config->sync_direct ?
  447. request_firmware_direct :
  448. request_firmware;
  449. return rc;
  450. }
  451. static ssize_t config_sync_direct_show(struct device *dev,
  452. struct device_attribute *attr,
  453. char *buf)
  454. {
  455. return test_dev_config_show_bool(buf, test_fw_config->sync_direct);
  456. }
  457. static DEVICE_ATTR_RW(config_sync_direct);
  458. static ssize_t config_send_uevent_store(struct device *dev,
  459. struct device_attribute *attr,
  460. const char *buf, size_t count)
  461. {
  462. return test_dev_config_update_bool(buf, count,
  463. &test_fw_config->send_uevent);
  464. }
  465. static ssize_t config_send_uevent_show(struct device *dev,
  466. struct device_attribute *attr,
  467. char *buf)
  468. {
  469. return test_dev_config_show_bool(buf, test_fw_config->send_uevent);
  470. }
  471. static DEVICE_ATTR_RW(config_send_uevent);
  472. static ssize_t config_read_fw_idx_store(struct device *dev,
  473. struct device_attribute *attr,
  474. const char *buf, size_t count)
  475. {
  476. return test_dev_config_update_u8(buf, count,
  477. &test_fw_config->read_fw_idx);
  478. }
  479. static ssize_t config_read_fw_idx_show(struct device *dev,
  480. struct device_attribute *attr,
  481. char *buf)
  482. {
  483. return test_dev_config_show_u8(buf, test_fw_config->read_fw_idx);
  484. }
  485. static DEVICE_ATTR_RW(config_read_fw_idx);
  486. static ssize_t trigger_request_store(struct device *dev,
  487. struct device_attribute *attr,
  488. const char *buf, size_t count)
  489. {
  490. int rc;
  491. char *name;
  492. name = kstrndup(buf, count, GFP_KERNEL);
  493. if (!name)
  494. return -ENOSPC;
  495. pr_info("loading '%s'\n", name);
  496. mutex_lock(&test_fw_mutex);
  497. release_firmware(test_firmware);
  498. test_firmware = NULL;
  499. rc = request_firmware(&test_firmware, name, dev);
  500. if (rc) {
  501. pr_info("load of '%s' failed: %d\n", name, rc);
  502. goto out;
  503. }
  504. pr_info("loaded: %zu\n", test_firmware->size);
  505. rc = count;
  506. out:
  507. mutex_unlock(&test_fw_mutex);
  508. kfree(name);
  509. return rc;
  510. }
  511. static DEVICE_ATTR_WO(trigger_request);
  512. #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
  513. extern struct list_head efi_embedded_fw_list;
  514. extern bool efi_embedded_fw_checked;
  515. static ssize_t trigger_request_platform_store(struct device *dev,
  516. struct device_attribute *attr,
  517. const char *buf, size_t count)
  518. {
  519. static const u8 test_data[] = {
  520. 0x55, 0xaa, 0x55, 0xaa, 0x01, 0x02, 0x03, 0x04,
  521. 0x55, 0xaa, 0x55, 0xaa, 0x05, 0x06, 0x07, 0x08,
  522. 0x55, 0xaa, 0x55, 0xaa, 0x10, 0x20, 0x30, 0x40,
  523. 0x55, 0xaa, 0x55, 0xaa, 0x50, 0x60, 0x70, 0x80
  524. };
  525. struct efi_embedded_fw efi_embedded_fw;
  526. const struct firmware *firmware = NULL;
  527. bool saved_efi_embedded_fw_checked;
  528. char *name;
  529. int rc;
  530. name = kstrndup(buf, count, GFP_KERNEL);
  531. if (!name)
  532. return -ENOSPC;
  533. pr_info("inserting test platform fw '%s'\n", name);
  534. efi_embedded_fw.name = name;
  535. efi_embedded_fw.data = (void *)test_data;
  536. efi_embedded_fw.length = sizeof(test_data);
  537. list_add(&efi_embedded_fw.list, &efi_embedded_fw_list);
  538. saved_efi_embedded_fw_checked = efi_embedded_fw_checked;
  539. efi_embedded_fw_checked = true;
  540. pr_info("loading '%s'\n", name);
  541. rc = firmware_request_platform(&firmware, name, dev);
  542. if (rc) {
  543. pr_info("load of '%s' failed: %d\n", name, rc);
  544. goto out;
  545. }
  546. if (firmware->size != sizeof(test_data) ||
  547. memcmp(firmware->data, test_data, sizeof(test_data)) != 0) {
  548. pr_info("firmware contents mismatch for '%s'\n", name);
  549. rc = -EINVAL;
  550. goto out;
  551. }
  552. pr_info("loaded: %zu\n", firmware->size);
  553. rc = count;
  554. out:
  555. efi_embedded_fw_checked = saved_efi_embedded_fw_checked;
  556. release_firmware(firmware);
  557. list_del(&efi_embedded_fw.list);
  558. kfree(name);
  559. return rc;
  560. }
  561. static DEVICE_ATTR_WO(trigger_request_platform);
  562. #endif
  563. static DECLARE_COMPLETION(async_fw_done);
  564. static void trigger_async_request_cb(const struct firmware *fw, void *context)
  565. {
  566. test_firmware = fw;
  567. complete(&async_fw_done);
  568. }
  569. static ssize_t trigger_async_request_store(struct device *dev,
  570. struct device_attribute *attr,
  571. const char *buf, size_t count)
  572. {
  573. int rc;
  574. char *name;
  575. name = kstrndup(buf, count, GFP_KERNEL);
  576. if (!name)
  577. return -ENOSPC;
  578. pr_info("loading '%s'\n", name);
  579. mutex_lock(&test_fw_mutex);
  580. release_firmware(test_firmware);
  581. test_firmware = NULL;
  582. rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
  583. NULL, trigger_async_request_cb);
  584. if (rc) {
  585. pr_info("async load of '%s' failed: %d\n", name, rc);
  586. kfree(name);
  587. goto out;
  588. }
  589. /* Free 'name' ASAP, to test for race conditions */
  590. kfree(name);
  591. wait_for_completion(&async_fw_done);
  592. if (test_firmware) {
  593. pr_info("loaded: %zu\n", test_firmware->size);
  594. rc = count;
  595. } else {
  596. pr_err("failed to async load firmware\n");
  597. rc = -ENOMEM;
  598. }
  599. out:
  600. mutex_unlock(&test_fw_mutex);
  601. return rc;
  602. }
  603. static DEVICE_ATTR_WO(trigger_async_request);
  604. static ssize_t trigger_custom_fallback_store(struct device *dev,
  605. struct device_attribute *attr,
  606. const char *buf, size_t count)
  607. {
  608. int rc;
  609. char *name;
  610. name = kstrndup(buf, count, GFP_KERNEL);
  611. if (!name)
  612. return -ENOSPC;
  613. pr_info("loading '%s' using custom fallback mechanism\n", name);
  614. mutex_lock(&test_fw_mutex);
  615. release_firmware(test_firmware);
  616. test_firmware = NULL;
  617. rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, name,
  618. dev, GFP_KERNEL, NULL,
  619. trigger_async_request_cb);
  620. if (rc) {
  621. pr_info("async load of '%s' failed: %d\n", name, rc);
  622. kfree(name);
  623. goto out;
  624. }
  625. /* Free 'name' ASAP, to test for race conditions */
  626. kfree(name);
  627. wait_for_completion(&async_fw_done);
  628. if (test_firmware) {
  629. pr_info("loaded: %zu\n", test_firmware->size);
  630. rc = count;
  631. } else {
  632. pr_err("failed to async load firmware\n");
  633. rc = -ENODEV;
  634. }
  635. out:
  636. mutex_unlock(&test_fw_mutex);
  637. return rc;
  638. }
  639. static DEVICE_ATTR_WO(trigger_custom_fallback);
  640. static int test_fw_run_batch_request(void *data)
  641. {
  642. struct test_batched_req *req = data;
  643. if (!req) {
  644. test_fw_config->test_result = -EINVAL;
  645. return -EINVAL;
  646. }
  647. if (test_fw_config->into_buf) {
  648. void *test_buf;
  649. test_buf = kzalloc(TEST_FIRMWARE_BUF_SIZE, GFP_KERNEL);
  650. if (!test_buf)
  651. return -ENOSPC;
  652. if (test_fw_config->partial)
  653. req->rc = request_partial_firmware_into_buf
  654. (&req->fw,
  655. req->name,
  656. req->dev,
  657. test_buf,
  658. test_fw_config->buf_size,
  659. test_fw_config->file_offset);
  660. else
  661. req->rc = request_firmware_into_buf
  662. (&req->fw,
  663. req->name,
  664. req->dev,
  665. test_buf,
  666. test_fw_config->buf_size);
  667. if (!req->fw)
  668. kfree(test_buf);
  669. } else {
  670. req->rc = test_fw_config->req_firmware(&req->fw,
  671. req->name,
  672. req->dev);
  673. }
  674. if (req->rc) {
  675. pr_info("#%u: batched sync load failed: %d\n",
  676. req->idx, req->rc);
  677. if (!test_fw_config->test_result)
  678. test_fw_config->test_result = req->rc;
  679. } else if (req->fw) {
  680. req->sent = true;
  681. pr_info("#%u: batched sync loaded %zu\n",
  682. req->idx, req->fw->size);
  683. }
  684. complete(&req->completion);
  685. req->task = NULL;
  686. return 0;
  687. }
  688. /*
  689. * We use a kthread as otherwise the kernel serializes all our sync requests
  690. * and we would not be able to mimic batched requests on a sync call. Batched
  691. * requests on a sync call can for instance happen on a device driver when
  692. * multiple cards are used and firmware loading happens outside of probe.
  693. */
  694. static ssize_t trigger_batched_requests_store(struct device *dev,
  695. struct device_attribute *attr,
  696. const char *buf, size_t count)
  697. {
  698. struct test_batched_req *req;
  699. int rc;
  700. u8 i;
  701. mutex_lock(&test_fw_mutex);
  702. test_fw_config->reqs =
  703. vzalloc(array3_size(sizeof(struct test_batched_req),
  704. test_fw_config->num_requests, 2));
  705. if (!test_fw_config->reqs) {
  706. rc = -ENOMEM;
  707. goto out_unlock;
  708. }
  709. pr_info("batched sync firmware loading '%s' %u times\n",
  710. test_fw_config->name, test_fw_config->num_requests);
  711. for (i = 0; i < test_fw_config->num_requests; i++) {
  712. req = &test_fw_config->reqs[i];
  713. req->fw = NULL;
  714. req->idx = i;
  715. req->name = test_fw_config->name;
  716. req->dev = dev;
  717. init_completion(&req->completion);
  718. req->task = kthread_run(test_fw_run_batch_request, req,
  719. "%s-%u", KBUILD_MODNAME, req->idx);
  720. if (!req->task || IS_ERR(req->task)) {
  721. pr_err("Setting up thread %u failed\n", req->idx);
  722. req->task = NULL;
  723. rc = -ENOMEM;
  724. goto out_bail;
  725. }
  726. }
  727. rc = count;
  728. /*
  729. * We require an explicit release to enable more time and delay of
  730. * calling release_firmware() to improve our chances of forcing a
  731. * batched request. If we instead called release_firmware() right away
  732. * then we might miss on an opportunity of having a successful firmware
  733. * request pass on the opportunity to be come a batched request.
  734. */
  735. out_bail:
  736. for (i = 0; i < test_fw_config->num_requests; i++) {
  737. req = &test_fw_config->reqs[i];
  738. if (req->task || req->sent)
  739. wait_for_completion(&req->completion);
  740. }
  741. /* Override any worker error if we had a general setup error */
  742. if (rc < 0)
  743. test_fw_config->test_result = rc;
  744. out_unlock:
  745. mutex_unlock(&test_fw_mutex);
  746. return rc;
  747. }
  748. static DEVICE_ATTR_WO(trigger_batched_requests);
  749. /*
  750. * We wait for each callback to return with the lock held, no need to lock here
  751. */
  752. static void trigger_batched_cb(const struct firmware *fw, void *context)
  753. {
  754. struct test_batched_req *req = context;
  755. if (!req) {
  756. test_fw_config->test_result = -EINVAL;
  757. return;
  758. }
  759. /* forces *some* batched requests to queue up */
  760. if (!req->idx)
  761. ssleep(2);
  762. req->fw = fw;
  763. /*
  764. * Unfortunately the firmware API gives us nothing other than a null FW
  765. * if the firmware was not found on async requests. Best we can do is
  766. * just assume -ENOENT. A better API would pass the actual return
  767. * value to the callback.
  768. */
  769. if (!fw && !test_fw_config->test_result)
  770. test_fw_config->test_result = -ENOENT;
  771. complete(&req->completion);
  772. }
  773. static
  774. ssize_t trigger_batched_requests_async_store(struct device *dev,
  775. struct device_attribute *attr,
  776. const char *buf, size_t count)
  777. {
  778. struct test_batched_req *req;
  779. bool send_uevent;
  780. int rc;
  781. u8 i;
  782. mutex_lock(&test_fw_mutex);
  783. test_fw_config->reqs =
  784. vzalloc(array3_size(sizeof(struct test_batched_req),
  785. test_fw_config->num_requests, 2));
  786. if (!test_fw_config->reqs) {
  787. rc = -ENOMEM;
  788. goto out;
  789. }
  790. pr_info("batched loading '%s' custom fallback mechanism %u times\n",
  791. test_fw_config->name, test_fw_config->num_requests);
  792. send_uevent = test_fw_config->send_uevent ? FW_ACTION_HOTPLUG :
  793. FW_ACTION_NOHOTPLUG;
  794. for (i = 0; i < test_fw_config->num_requests; i++) {
  795. req = &test_fw_config->reqs[i];
  796. req->name = test_fw_config->name;
  797. req->fw = NULL;
  798. req->idx = i;
  799. init_completion(&req->completion);
  800. rc = request_firmware_nowait(THIS_MODULE, send_uevent,
  801. req->name,
  802. dev, GFP_KERNEL, req,
  803. trigger_batched_cb);
  804. if (rc) {
  805. pr_info("#%u: batched async load failed setup: %d\n",
  806. i, rc);
  807. req->rc = rc;
  808. goto out_bail;
  809. } else
  810. req->sent = true;
  811. }
  812. rc = count;
  813. out_bail:
  814. /*
  815. * We require an explicit release to enable more time and delay of
  816. * calling release_firmware() to improve our chances of forcing a
  817. * batched request. If we instead called release_firmware() right away
  818. * then we might miss on an opportunity of having a successful firmware
  819. * request pass on the opportunity to be come a batched request.
  820. */
  821. for (i = 0; i < test_fw_config->num_requests; i++) {
  822. req = &test_fw_config->reqs[i];
  823. if (req->sent)
  824. wait_for_completion(&req->completion);
  825. }
  826. /* Override any worker error if we had a general setup error */
  827. if (rc < 0)
  828. test_fw_config->test_result = rc;
  829. out:
  830. mutex_unlock(&test_fw_mutex);
  831. return rc;
  832. }
  833. static DEVICE_ATTR_WO(trigger_batched_requests_async);
  834. static ssize_t test_result_show(struct device *dev,
  835. struct device_attribute *attr,
  836. char *buf)
  837. {
  838. return test_dev_config_show_int(buf, test_fw_config->test_result);
  839. }
  840. static DEVICE_ATTR_RO(test_result);
  841. static ssize_t release_all_firmware_store(struct device *dev,
  842. struct device_attribute *attr,
  843. const char *buf, size_t count)
  844. {
  845. test_release_all_firmware();
  846. return count;
  847. }
  848. static DEVICE_ATTR_WO(release_all_firmware);
  849. static ssize_t read_firmware_show(struct device *dev,
  850. struct device_attribute *attr,
  851. char *buf)
  852. {
  853. struct test_batched_req *req;
  854. u8 idx;
  855. ssize_t rc = 0;
  856. mutex_lock(&test_fw_mutex);
  857. idx = test_fw_config->read_fw_idx;
  858. if (idx >= test_fw_config->num_requests) {
  859. rc = -ERANGE;
  860. goto out;
  861. }
  862. if (!test_fw_config->reqs) {
  863. rc = -EINVAL;
  864. goto out;
  865. }
  866. req = &test_fw_config->reqs[idx];
  867. if (!req->fw) {
  868. pr_err("#%u: failed to async load firmware\n", idx);
  869. rc = -ENOENT;
  870. goto out;
  871. }
  872. pr_info("#%u: loaded %zu\n", idx, req->fw->size);
  873. if (req->fw->size > PAGE_SIZE) {
  874. pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
  875. rc = -EINVAL;
  876. goto out;
  877. }
  878. memcpy(buf, req->fw->data, req->fw->size);
  879. rc = req->fw->size;
  880. out:
  881. mutex_unlock(&test_fw_mutex);
  882. return rc;
  883. }
  884. static DEVICE_ATTR_RO(read_firmware);
  885. #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
  886. static struct attribute *test_dev_attrs[] = {
  887. TEST_FW_DEV_ATTR(reset),
  888. TEST_FW_DEV_ATTR(config),
  889. TEST_FW_DEV_ATTR(config_name),
  890. TEST_FW_DEV_ATTR(config_num_requests),
  891. TEST_FW_DEV_ATTR(config_into_buf),
  892. TEST_FW_DEV_ATTR(config_buf_size),
  893. TEST_FW_DEV_ATTR(config_file_offset),
  894. TEST_FW_DEV_ATTR(config_partial),
  895. TEST_FW_DEV_ATTR(config_sync_direct),
  896. TEST_FW_DEV_ATTR(config_send_uevent),
  897. TEST_FW_DEV_ATTR(config_read_fw_idx),
  898. /* These don't use the config at all - they could be ported! */
  899. TEST_FW_DEV_ATTR(trigger_request),
  900. TEST_FW_DEV_ATTR(trigger_async_request),
  901. TEST_FW_DEV_ATTR(trigger_custom_fallback),
  902. #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE
  903. TEST_FW_DEV_ATTR(trigger_request_platform),
  904. #endif
  905. /* These use the config and can use the test_result */
  906. TEST_FW_DEV_ATTR(trigger_batched_requests),
  907. TEST_FW_DEV_ATTR(trigger_batched_requests_async),
  908. TEST_FW_DEV_ATTR(release_all_firmware),
  909. TEST_FW_DEV_ATTR(test_result),
  910. TEST_FW_DEV_ATTR(read_firmware),
  911. NULL,
  912. };
  913. ATTRIBUTE_GROUPS(test_dev);
  914. static struct miscdevice test_fw_misc_device = {
  915. .minor = MISC_DYNAMIC_MINOR,
  916. .name = "test_firmware",
  917. .fops = &test_fw_fops,
  918. .groups = test_dev_groups,
  919. };
  920. static int __init test_firmware_init(void)
  921. {
  922. int rc;
  923. test_fw_config = kzalloc(sizeof(struct test_config), GFP_KERNEL);
  924. if (!test_fw_config)
  925. return -ENOMEM;
  926. rc = __test_firmware_config_init();
  927. if (rc) {
  928. kfree(test_fw_config);
  929. pr_err("could not init firmware test config: %d\n", rc);
  930. return rc;
  931. }
  932. rc = misc_register(&test_fw_misc_device);
  933. if (rc) {
  934. kfree(test_fw_config);
  935. pr_err("could not register misc device: %d\n", rc);
  936. return rc;
  937. }
  938. pr_warn("interface ready\n");
  939. return 0;
  940. }
  941. module_init(test_firmware_init);
  942. static void __exit test_firmware_exit(void)
  943. {
  944. mutex_lock(&test_fw_mutex);
  945. release_firmware(test_firmware);
  946. misc_deregister(&test_fw_misc_device);
  947. __test_firmware_config_free();
  948. kfree(test_fw_config);
  949. mutex_unlock(&test_fw_mutex);
  950. pr_warn("removed interface\n");
  951. }
  952. module_exit(test_firmware_exit);
  953. MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
  954. MODULE_LICENSE("GPL");