Path: blob/master/tools/testing/selftests/efivarfs/efivarfs.sh
26285 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.023efivarfs_mount=/sys/firmware/efi/efivars4test_guid=210be57c-9849-4fc7-a635-e6382d1aec2756# Kselftest framework requirement - SKIP code is 4.7ksft_skip=489file_cleanup()10{11chattr -i $112rm -f $113}1415check_prereqs()16{17local msg="skip all tests:"1819if [ $UID != 0 ]; then20echo $msg must be run as root >&221exit $ksft_skip22fi2324if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then25echo $msg efivarfs is not mounted on $efivarfs_mount >&226exit $ksft_skip27fi28}2930run_test()31{32local test="$1"3334echo "--------------------"35echo "running $test"36echo "--------------------"3738if [ "$(type -t $test)" = 'function' ]; then39( $test )40else41( ./$test )42fi4344if [ $? -ne 0 ]; then45echo " [FAIL]"46rc=147else48echo " [PASS]"49fi50}5152test_create()53{54local attrs='\x07\x00\x00\x00'55local file=$efivarfs_mount/$FUNCNAME-$test_guid5657printf "$attrs\x00" > $file5859if [ ! -e $file ]; then60echo "$file couldn't be created" >&261exit 162fi6364if [ $(stat -c %s $file) -ne 5 ]; then65echo "$file has invalid size" >&266file_cleanup $file67exit 168fi69file_cleanup $file70}7172test_create_empty()73{74local file=$efivarfs_mount/$FUNCNAME-$test_guid7576: > $file7778if [ -e $file ]; then79echo "$file can be created without writing" >&280file_cleanup $file81exit 182fi83}8485test_create_read()86{87local file=$efivarfs_mount/$FUNCNAME-$test_guid88./create-read $file89if [ $? -ne 0 ]; then90echo "create and read $file failed"91exit 192fi93if [ -e $file ]; then94echo "file still exists and should not"95file_cleanup $file96exit 197fi98}99100test_delete()101{102local attrs='\x07\x00\x00\x00'103local file=$efivarfs_mount/$FUNCNAME-$test_guid104105printf "$attrs\x00" > $file106107if [ ! -e $file ]; then108echo "$file couldn't be created" >&2109exit 1110fi111112file_cleanup $file113114if [ -e $file ]; then115echo "$file couldn't be deleted" >&2116exit 1117fi118119}120121# test that we can remove a variable by issuing a write with only122# attributes specified123test_zero_size_delete()124{125local attrs='\x07\x00\x00\x00'126local file=$efivarfs_mount/$FUNCNAME-$test_guid127128printf "$attrs\x00" > $file129130if [ ! -e $file ]; then131echo "$file does not exist" >&2132exit 1133fi134135chattr -i $file136printf "$attrs" > $file137138if [ -e $file ]; then139echo "$file should have been deleted" >&2140exit 1141fi142}143144test_open_unlink()145{146local file=$efivarfs_mount/$FUNCNAME-$test_guid147./open-unlink $file148}149150# test that we can create a range of filenames151test_valid_filenames()152{153local attrs='\x07\x00\x00\x00'154local ret=0155156local file_list="abc dump-type0-11-1-1362436005 1234 -"157for f in $file_list; do158local file=$efivarfs_mount/$f-$test_guid159160printf "$attrs\x00" > $file161162if [ ! -e $file ]; then163echo "$file could not be created" >&2164ret=1165else166file_cleanup $file167fi168done169170exit $ret171}172173test_invalid_filenames()174{175local attrs='\x07\x00\x00\x00'176local ret=0177178local file_list="179-1234-1234-1234-123456789abc180foo181foo-bar182-foo-183foo-barbazba-foob-foob-foob-foobarbazfoo184foo-------------------------------------185-12345678-1234-1234-1234-123456789abc186a-12345678=1234-1234-1234-123456789abc187a-12345678-1234=1234-1234-123456789abc188a-12345678-1234-1234=1234-123456789abc189a-12345678-1234-1234-1234=123456789abc1901112345678-1234-1234-1234-123456789abc"191192for f in $file_list; do193local file=$efivarfs_mount/$f194195printf "$attrs\x00" 2>/dev/null > $file196197if [ -e $file ]; then198echo "Creating $file should have failed" >&2199file_cleanup $file200ret=1201fi202done203204exit $ret205}206207test_no_set_size()208{209local attrs='\x07\x00\x00\x00'210local file=$efivarfs_mount/$FUNCNAME-$test_guid211local ret=0212213printf "$attrs\x00" > $file214[ -e $file -a -s $file ] || exit 1215chattr -i $file216: > $file217if [ $? != 0 ]; then218echo "variable file failed to accept truncation"219ret=1220elif [ -e $file -a ! -s $file ]; then221echo "file can be truncated to zero size"222ret=1223fi224rm $file || exit 1225226exit $ret227}228229setup_test_multiple()230{231##232# we're going to do multi-threaded tests, so create a set of233# pipes for synchronization. We use pipes 1..3 to start the234# stalled shell job and pipes 4..6 as indicators that the job235# has started. If you need more than 3 jobs the two +3's below236# need increasing237##238239declare -ag p240241# empty is because arrays number from 0 but jobs number from 1242p[0]=""243244for f in 1 2 3 4 5 6; do245p[$f]=/tmp/efivarfs_pipe${f}246mknod ${p[$f]} p247done248249declare -g var=$efivarfs_mount/test_multiple-$test_guid250251cleanup() {252for f in ${p[@]}; do253rm -f ${f}254done255if [ -e $var ]; then256file_cleanup $var257fi258}259trap cleanup exit260261waitstart() {262cat ${p[$[$1+3]]} > /dev/null263}264265waitpipe() {266echo 1 > ${p[$[$1+3]]}267cat ${p[$1]} > /dev/null268}269270endjob() {271echo 1 > ${p[$1]}272wait -n %$1273}274}275276test_multiple_zero_size()277{278##279# check for remove on last close, set up three threads all280# holding the variable (one write and two reads) and then281# close them sequentially (waiting for completion) and check282# the state of the variable283##284285{ waitpipe 1; echo 1; } > $var 2> /dev/null &286waitstart 1287# zero length file should exist288[ -e $var ] || exit 1289# second and third delayed close290{ waitpipe 2; } < $var &291waitstart 2292{ waitpipe 3; } < $var &293waitstart 3294# close first fd295endjob 1296# var should only be deleted on last close297[ -e $var ] || exit 1298# close second fd299endjob 2300[ -e $var ] || exit 1301# file should go on last close302endjob 3303[ ! -e $var ] || exit 1304}305306test_multiple_create()307{308##309# set multiple threads to access the variable but delay310# the final write to check the close of 2 and 3. The311# final write should succeed in creating the variable312##313{ waitpipe 1; printf '\x07\x00\x00\x00\x54'; } > $var &314waitstart 1315[ -e $var -a ! -s $var ] || exit 1316{ waitpipe 2; } < $var &317waitstart 2318{ waitpipe 3; } < $var &319waitstart 3320# close second and third fds321endjob 2322# var should only be created (have size) on last close323[ -e $var -a ! -s $var ] || exit 1324endjob 3325[ -e $var -a ! -s $var ] || exit 1326# close first fd327endjob 1328# variable should still exist329[ -s $var ] || exit 1330file_cleanup $var331}332333test_multiple_delete_on_write() {334##335# delete the variable on final write; seqencing similar336# to test_multiple_create()337##338printf '\x07\x00\x00\x00\x54' > $var339chattr -i $var340{ waitpipe 1; printf '\x07\x00\x00\x00'; } > $var &341waitstart 1342[ -e $var -a -s $var ] || exit 1343{ waitpipe 2; } < $var &344waitstart 2345{ waitpipe 3; } < $var &346waitstart 3347# close first fd; write should set variable size to zero348endjob 1349# var should only be deleted on last close350[ -e $var -a ! -s $var ] || exit 1351endjob 2352[ -e $var ] || exit 1353# close last fd354endjob 3355# variable should now be removed356[ ! -e $var ] || exit 1357}358359check_prereqs360361rc=0362363run_test test_create364run_test test_create_empty365run_test test_create_read366run_test test_delete367run_test test_zero_size_delete368run_test test_open_unlink369run_test test_valid_filenames370run_test test_invalid_filenames371run_test test_no_set_size372setup_test_multiple373run_test test_multiple_zero_size374run_test test_multiple_create375run_test test_multiple_delete_on_write376377exit $rc378379380