fw_filesystem.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # This validates that the kernel will load firmware out of its list of
  4. # firmware locations on disk. Since the user helper does similar work,
  5. # we reset the custom load directory to a location the user helper doesn't
  6. # know so we can be sure we're not accidentally testing the user helper.
  7. set -e
  8. TEST_REQS_FW_SYSFS_FALLBACK="no"
  9. TEST_REQS_FW_SET_CUSTOM_PATH="yes"
  10. TEST_DIR=$(dirname $0)
  11. source $TEST_DIR/fw_lib.sh
  12. RUN_XZ="xz -C crc32 --lzma2=dict=2MiB"
  13. RUN_ZSTD="zstd -q"
  14. check_mods
  15. check_setup
  16. verify_reqs
  17. setup_tmp_file
  18. trap "test_finish" EXIT
  19. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  20. # Turn down the timeout so failures don't take so long.
  21. echo 1 >/sys/class/firmware/timeout
  22. fi
  23. if printf '\000' >"$DIR"/trigger_request 2> /dev/null; then
  24. echo "$0: empty filename should not succeed" >&2
  25. exit 1
  26. fi
  27. if [ ! -e "$DIR"/trigger_async_request ]; then
  28. echo "$0: empty filename: async trigger not present, ignoring test" >&2
  29. exit $ksft_skip
  30. else
  31. if printf '\000' >"$DIR"/trigger_async_request 2> /dev/null; then
  32. echo "$0: empty filename should not succeed (async)" >&2
  33. exit 1
  34. fi
  35. fi
  36. # Request a firmware that doesn't exist, it should fail.
  37. if echo -n "nope-$NAME" >"$DIR"/trigger_request 2> /dev/null; then
  38. echo "$0: firmware shouldn't have loaded" >&2
  39. exit 1
  40. fi
  41. if diff -q "$FW" /dev/test_firmware >/dev/null ; then
  42. echo "$0: firmware was not expected to match" >&2
  43. exit 1
  44. else
  45. if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then
  46. echo "$0: timeout works"
  47. fi
  48. fi
  49. # This should succeed via kernel load or will fail after 1 second after
  50. # being handed over to the user helper, which won't find the fw either.
  51. if ! echo -n "$NAME" >"$DIR"/trigger_request ; then
  52. echo "$0: could not trigger request" >&2
  53. exit 1
  54. fi
  55. # Verify the contents are what we expect.
  56. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  57. echo "$0: firmware was not loaded" >&2
  58. exit 1
  59. else
  60. echo "$0: filesystem loading works"
  61. fi
  62. # Try the asynchronous version too
  63. if [ ! -e "$DIR"/trigger_async_request ]; then
  64. echo "$0: firmware loading: async trigger not present, ignoring test" >&2
  65. exit $ksft_skip
  66. else
  67. if ! echo -n "$NAME" >"$DIR"/trigger_async_request ; then
  68. echo "$0: could not trigger async request" >&2
  69. exit 1
  70. fi
  71. # Verify the contents are what we expect.
  72. if ! diff -q "$FW" /dev/test_firmware >/dev/null ; then
  73. echo "$0: firmware was not loaded (async)" >&2
  74. exit 1
  75. else
  76. echo "$0: async filesystem loading works"
  77. fi
  78. fi
  79. # Try platform (EFI embedded fw) loading too
  80. if [ ! -e "$DIR"/trigger_request_platform ]; then
  81. echo "$0: firmware loading: platform trigger not present, ignoring test" >&2
  82. else
  83. if printf '\000' >"$DIR"/trigger_request_platform 2> /dev/null; then
  84. echo "$0: empty filename should not succeed (platform)" >&2
  85. exit 1
  86. fi
  87. # Note we echo a non-existing name, since files on the file-system
  88. # are preferred over firmware embedded inside the platform's firmware
  89. # The test adds a fake entry with the requested name to the platform's
  90. # fw list, so the name does not matter as long as it does not exist
  91. if ! echo -n "nope-$NAME" >"$DIR"/trigger_request_platform ; then
  92. echo "$0: could not trigger request platform" >&2
  93. exit 1
  94. fi
  95. # The test verifies itself that the loaded firmware contents matches
  96. # the contents for the fake platform fw entry it added.
  97. echo "$0: platform loading works"
  98. fi
  99. ### Batched requests tests
  100. test_config_present()
  101. {
  102. if [ ! -f $DIR/reset ]; then
  103. echo "Configuration triggers not present, ignoring test"
  104. exit $ksft_skip
  105. fi
  106. }
  107. # Defaults :
  108. #
  109. # send_uevent: 1
  110. # sync_direct: 0
  111. # name: test-firmware.bin
  112. # num_requests: 4
  113. config_reset()
  114. {
  115. echo 1 > $DIR/reset
  116. }
  117. release_all_firmware()
  118. {
  119. echo 1 > $DIR/release_all_firmware
  120. }
  121. config_set_name()
  122. {
  123. echo -n $1 > $DIR/config_name
  124. }
  125. config_set_into_buf()
  126. {
  127. echo 1 > $DIR/config_into_buf
  128. }
  129. config_unset_into_buf()
  130. {
  131. echo 0 > $DIR/config_into_buf
  132. }
  133. config_set_buf_size()
  134. {
  135. echo $1 > $DIR/config_buf_size
  136. }
  137. config_set_file_offset()
  138. {
  139. echo $1 > $DIR/config_file_offset
  140. }
  141. config_set_partial()
  142. {
  143. echo 1 > $DIR/config_partial
  144. }
  145. config_unset_partial()
  146. {
  147. echo 0 > $DIR/config_partial
  148. }
  149. config_set_sync_direct()
  150. {
  151. echo 1 > $DIR/config_sync_direct
  152. }
  153. config_unset_sync_direct()
  154. {
  155. echo 0 > $DIR/config_sync_direct
  156. }
  157. config_set_uevent()
  158. {
  159. echo 1 > $DIR/config_send_uevent
  160. }
  161. config_unset_uevent()
  162. {
  163. echo 0 > $DIR/config_send_uevent
  164. }
  165. config_trigger_sync()
  166. {
  167. echo -n 1 > $DIR/trigger_batched_requests 2>/dev/null
  168. }
  169. config_trigger_async()
  170. {
  171. echo -n 1 > $DIR/trigger_batched_requests_async 2> /dev/null
  172. }
  173. config_set_read_fw_idx()
  174. {
  175. echo -n $1 > $DIR/config_read_fw_idx 2> /dev/null
  176. }
  177. read_firmwares()
  178. {
  179. if [ "$(cat $DIR/config_into_buf)" == "1" ]; then
  180. fwfile="$FW_INTO_BUF"
  181. else
  182. fwfile="$FW"
  183. fi
  184. if [ "$1" = "componly" ]; then
  185. fwfile="${fwfile}-orig"
  186. fi
  187. for i in $(seq 0 3); do
  188. config_set_read_fw_idx $i
  189. # Verify the contents are what we expect.
  190. # -Z required for now -- check for yourself, md5sum
  191. # on $FW and DIR/read_firmware will yield the same. Even
  192. # cmp agrees, so something is off.
  193. if ! diff -q -Z "$fwfile" $DIR/read_firmware 2>/dev/null ; then
  194. echo "request #$i: firmware was not loaded" >&2
  195. exit 1
  196. fi
  197. done
  198. }
  199. read_partial_firmwares()
  200. {
  201. if [ "$(cat $DIR/config_into_buf)" == "1" ]; then
  202. fwfile="${FW_INTO_BUF}"
  203. else
  204. fwfile="${FW}"
  205. fi
  206. if [ "$1" = "componly" ]; then
  207. fwfile="${fwfile}-orig"
  208. fi
  209. # Strip fwfile down to match partial offset and length
  210. partial_data="$(cat $fwfile)"
  211. partial_data="${partial_data:$2:$3}"
  212. for i in $(seq 0 3); do
  213. config_set_read_fw_idx $i
  214. read_firmware="$(cat $DIR/read_firmware)"
  215. # Verify the contents are what we expect.
  216. if [ $read_firmware != $partial_data ]; then
  217. echo "request #$i: partial firmware was not loaded" >&2
  218. exit 1
  219. fi
  220. done
  221. }
  222. read_firmwares_expect_nofile()
  223. {
  224. for i in $(seq 0 3); do
  225. config_set_read_fw_idx $i
  226. # Ensures contents differ
  227. if diff -q -Z "$FW" $DIR/read_firmware 2>/dev/null ; then
  228. echo "request $i: file was not expected to match" >&2
  229. exit 1
  230. fi
  231. done
  232. }
  233. test_batched_request_firmware_nofile()
  234. {
  235. echo -n "Batched request_firmware() nofile try #$1: "
  236. config_reset
  237. config_set_name nope-test-firmware.bin
  238. config_trigger_sync
  239. read_firmwares_expect_nofile
  240. release_all_firmware
  241. echo "OK"
  242. }
  243. test_batched_request_firmware_into_buf_nofile()
  244. {
  245. echo -n "Batched request_firmware_into_buf() nofile try #$1: "
  246. config_reset
  247. config_set_name nope-test-firmware.bin
  248. config_set_into_buf
  249. config_trigger_sync
  250. read_firmwares_expect_nofile
  251. release_all_firmware
  252. echo "OK"
  253. }
  254. test_request_partial_firmware_into_buf_nofile()
  255. {
  256. echo -n "Test request_partial_firmware_into_buf() off=$1 size=$2 nofile: "
  257. config_reset
  258. config_set_name nope-test-firmware.bin
  259. config_set_into_buf
  260. config_set_partial
  261. config_set_buf_size $2
  262. config_set_file_offset $1
  263. config_trigger_sync
  264. read_firmwares_expect_nofile
  265. release_all_firmware
  266. echo "OK"
  267. }
  268. test_batched_request_firmware_direct_nofile()
  269. {
  270. echo -n "Batched request_firmware_direct() nofile try #$1: "
  271. config_reset
  272. config_set_name nope-test-firmware.bin
  273. config_set_sync_direct
  274. config_trigger_sync
  275. release_all_firmware
  276. echo "OK"
  277. }
  278. test_request_firmware_nowait_uevent_nofile()
  279. {
  280. echo -n "Batched request_firmware_nowait(uevent=true) nofile try #$1: "
  281. config_reset
  282. config_set_name nope-test-firmware.bin
  283. config_trigger_async
  284. release_all_firmware
  285. echo "OK"
  286. }
  287. test_wait_and_cancel_custom_load()
  288. {
  289. if [ "$HAS_FW_LOADER_USER_HELPER" != "yes" ]; then
  290. return
  291. fi
  292. local timeout=10
  293. name=$1
  294. while [ ! -e "$DIR"/"$name"/loading ]; do
  295. sleep 0.1
  296. timeout=$(( $timeout - 1 ))
  297. if [ "$timeout" -eq 0 ]; then
  298. echo "firmware interface never appeared:" >&2
  299. echo "$DIR/$name/loading" >&2
  300. exit 1
  301. fi
  302. done
  303. echo -1 >"$DIR"/"$name"/loading
  304. }
  305. test_request_firmware_nowait_custom_nofile()
  306. {
  307. echo -n "Batched request_firmware_nowait(uevent=false) nofile try #$1: "
  308. config_reset
  309. config_unset_uevent
  310. RANDOM_FILE_PATH=$(setup_random_file_fake)
  311. RANDOM_FILE="$(basename $RANDOM_FILE_PATH)"
  312. config_set_name $RANDOM_FILE
  313. config_trigger_async &
  314. test_wait_and_cancel_custom_load $RANDOM_FILE
  315. wait
  316. release_all_firmware
  317. echo "OK"
  318. }
  319. test_batched_request_firmware()
  320. {
  321. echo -n "Batched request_firmware() $2 try #$1: "
  322. config_reset
  323. config_trigger_sync
  324. read_firmwares $2
  325. release_all_firmware
  326. echo "OK"
  327. }
  328. test_batched_request_firmware_into_buf()
  329. {
  330. echo -n "Batched request_firmware_into_buf() $2 try #$1: "
  331. config_reset
  332. config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME
  333. config_set_into_buf
  334. config_trigger_sync
  335. read_firmwares $2
  336. release_all_firmware
  337. echo "OK"
  338. }
  339. test_batched_request_firmware_direct()
  340. {
  341. echo -n "Batched request_firmware_direct() $2 try #$1: "
  342. config_reset
  343. config_set_sync_direct
  344. config_trigger_sync
  345. release_all_firmware
  346. echo "OK"
  347. }
  348. test_request_firmware_nowait_uevent()
  349. {
  350. echo -n "Batched request_firmware_nowait(uevent=true) $2 try #$1: "
  351. config_reset
  352. config_trigger_async
  353. release_all_firmware
  354. echo "OK"
  355. }
  356. test_request_firmware_nowait_custom()
  357. {
  358. echo -n "Batched request_firmware_nowait(uevent=false) $2 try #$1: "
  359. config_reset
  360. config_unset_uevent
  361. RANDOM_FILE_PATH=$(setup_random_file)
  362. RANDOM_FILE="$(basename $RANDOM_FILE_PATH)"
  363. if [ -n "$2" -a "$2" != "normal" ]; then
  364. compress_"$2"_"$COMPRESS_FORMAT" $RANDOM_FILE_PATH
  365. fi
  366. config_set_name $RANDOM_FILE
  367. config_trigger_async
  368. release_all_firmware
  369. echo "OK"
  370. }
  371. test_request_partial_firmware_into_buf()
  372. {
  373. echo -n "Test request_partial_firmware_into_buf() off=$1 size=$2: "
  374. config_reset
  375. config_set_name $TEST_FIRMWARE_INTO_BUF_FILENAME
  376. config_set_into_buf
  377. config_set_partial
  378. config_set_buf_size $2
  379. config_set_file_offset $1
  380. config_trigger_sync
  381. read_partial_firmwares normal $1 $2
  382. release_all_firmware
  383. echo "OK"
  384. }
  385. do_tests ()
  386. {
  387. mode="$1"
  388. suffix="$2"
  389. for i in $(seq 1 5); do
  390. test_batched_request_firmware$suffix $i $mode
  391. done
  392. for i in $(seq 1 5); do
  393. test_batched_request_firmware_into_buf$suffix $i $mode
  394. done
  395. for i in $(seq 1 5); do
  396. test_batched_request_firmware_direct$suffix $i $mode
  397. done
  398. for i in $(seq 1 5); do
  399. test_request_firmware_nowait_uevent$suffix $i $mode
  400. done
  401. for i in $(seq 1 5); do
  402. test_request_firmware_nowait_custom$suffix $i $mode
  403. done
  404. }
  405. # Only continue if batched request triggers are present on the
  406. # test-firmware driver
  407. test_config_present
  408. # test with the file present
  409. echo
  410. echo "Testing with the file present..."
  411. do_tests normal
  412. # Partial loads cannot use fallback, so do not repeat tests.
  413. test_request_partial_firmware_into_buf 0 10
  414. test_request_partial_firmware_into_buf 0 5
  415. test_request_partial_firmware_into_buf 1 6
  416. test_request_partial_firmware_into_buf 2 10
  417. # Test for file not found, errors are expected, the failure would be
  418. # a hung task, which would require a hard reset.
  419. echo
  420. echo "Testing with the file missing..."
  421. do_tests nofile _nofile
  422. # Partial loads cannot use fallback, so do not repeat tests.
  423. test_request_partial_firmware_into_buf_nofile 0 10
  424. test_request_partial_firmware_into_buf_nofile 0 5
  425. test_request_partial_firmware_into_buf_nofile 1 6
  426. test_request_partial_firmware_into_buf_nofile 2 10
  427. test_request_firmware_compressed ()
  428. {
  429. export COMPRESS_FORMAT="$1"
  430. # test with both files present
  431. compress_both_"$COMPRESS_FORMAT" $FW
  432. compress_both_"$COMPRESS_FORMAT" $FW_INTO_BUF
  433. config_set_name $NAME
  434. echo
  435. echo "Testing with both plain and $COMPRESS_FORMAT files present..."
  436. do_tests both
  437. # test with only compressed file present
  438. mv "$FW" "${FW}-orig"
  439. mv "$FW_INTO_BUF" "${FW_INTO_BUF}-orig"
  440. config_set_name $NAME
  441. echo
  442. echo "Testing with only $COMPRESS_FORMAT file present..."
  443. do_tests componly
  444. mv "${FW}-orig" "$FW"
  445. mv "${FW_INTO_BUF}-orig" "$FW_INTO_BUF"
  446. }
  447. compress_both_XZ ()
  448. {
  449. $RUN_XZ -k "$@"
  450. }
  451. compress_componly_XZ ()
  452. {
  453. $RUN_XZ "$@"
  454. }
  455. compress_both_ZSTD ()
  456. {
  457. $RUN_ZSTD -k "$@"
  458. }
  459. compress_componly_ZSTD ()
  460. {
  461. $RUN_ZSTD --rm "$@"
  462. }
  463. if test "$HAS_FW_LOADER_COMPRESS_XZ" = "yes"; then
  464. test_request_firmware_compressed XZ
  465. fi
  466. if test "$HAS_FW_LOADER_COMPRESS_ZSTD" = "yes"; then
  467. test_request_firmware_compressed ZSTD
  468. fi
  469. exit 0