efivarfs.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. efivarfs_mount=/sys/firmware/efi/efivars
  4. test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
  5. # Kselftest framework requirement - SKIP code is 4.
  6. ksft_skip=4
  7. file_cleanup()
  8. {
  9. chattr -i $1
  10. rm -f $1
  11. }
  12. check_prereqs()
  13. {
  14. local msg="skip all tests:"
  15. if [ $UID != 0 ]; then
  16. echo $msg must be run as root >&2
  17. exit $ksft_skip
  18. fi
  19. if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then
  20. echo $msg efivarfs is not mounted on $efivarfs_mount >&2
  21. exit $ksft_skip
  22. fi
  23. }
  24. run_test()
  25. {
  26. local test="$1"
  27. echo "--------------------"
  28. echo "running $test"
  29. echo "--------------------"
  30. if [ "$(type -t $test)" = 'function' ]; then
  31. ( $test )
  32. else
  33. ( ./$test )
  34. fi
  35. if [ $? -ne 0 ]; then
  36. echo " [FAIL]"
  37. rc=1
  38. else
  39. echo " [PASS]"
  40. fi
  41. }
  42. test_create()
  43. {
  44. local attrs='\x07\x00\x00\x00'
  45. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  46. printf "$attrs\x00" > $file
  47. if [ ! -e $file ]; then
  48. echo "$file couldn't be created" >&2
  49. exit 1
  50. fi
  51. if [ $(stat -c %s $file) -ne 5 ]; then
  52. echo "$file has invalid size" >&2
  53. file_cleanup $file
  54. exit 1
  55. fi
  56. file_cleanup $file
  57. }
  58. test_create_empty()
  59. {
  60. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  61. : > $file
  62. if [ ! -e $file ]; then
  63. echo "$file can not be created without writing" >&2
  64. exit 1
  65. fi
  66. file_cleanup $file
  67. }
  68. test_create_read()
  69. {
  70. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  71. ./create-read $file
  72. if [ $? -ne 0 ]; then
  73. echo "create and read $file failed"
  74. file_cleanup $file
  75. exit 1
  76. fi
  77. file_cleanup $file
  78. }
  79. test_delete()
  80. {
  81. local attrs='\x07\x00\x00\x00'
  82. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  83. printf "$attrs\x00" > $file
  84. if [ ! -e $file ]; then
  85. echo "$file couldn't be created" >&2
  86. exit 1
  87. fi
  88. file_cleanup $file
  89. if [ -e $file ]; then
  90. echo "$file couldn't be deleted" >&2
  91. exit 1
  92. fi
  93. }
  94. # test that we can remove a variable by issuing a write with only
  95. # attributes specified
  96. test_zero_size_delete()
  97. {
  98. local attrs='\x07\x00\x00\x00'
  99. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  100. printf "$attrs\x00" > $file
  101. if [ ! -e $file ]; then
  102. echo "$file does not exist" >&2
  103. exit 1
  104. fi
  105. chattr -i $file
  106. printf "$attrs" > $file
  107. if [ -e $file ]; then
  108. echo "$file should have been deleted" >&2
  109. exit 1
  110. fi
  111. }
  112. test_open_unlink()
  113. {
  114. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  115. ./open-unlink $file
  116. }
  117. # test that we can create a range of filenames
  118. test_valid_filenames()
  119. {
  120. local attrs='\x07\x00\x00\x00'
  121. local ret=0
  122. local file_list="abc dump-type0-11-1-1362436005 1234 -"
  123. for f in $file_list; do
  124. local file=$efivarfs_mount/$f-$test_guid
  125. printf "$attrs\x00" > $file
  126. if [ ! -e $file ]; then
  127. echo "$file could not be created" >&2
  128. ret=1
  129. else
  130. file_cleanup $file
  131. fi
  132. done
  133. exit $ret
  134. }
  135. test_invalid_filenames()
  136. {
  137. local attrs='\x07\x00\x00\x00'
  138. local ret=0
  139. local file_list="
  140. -1234-1234-1234-123456789abc
  141. foo
  142. foo-bar
  143. -foo-
  144. foo-barbazba-foob-foob-foob-foobarbazfoo
  145. foo-------------------------------------
  146. -12345678-1234-1234-1234-123456789abc
  147. a-12345678=1234-1234-1234-123456789abc
  148. a-12345678-1234=1234-1234-123456789abc
  149. a-12345678-1234-1234=1234-123456789abc
  150. a-12345678-1234-1234-1234=123456789abc
  151. 1112345678-1234-1234-1234-123456789abc"
  152. for f in $file_list; do
  153. local file=$efivarfs_mount/$f
  154. printf "$attrs\x00" 2>/dev/null > $file
  155. if [ -e $file ]; then
  156. echo "Creating $file should have failed" >&2
  157. file_cleanup $file
  158. ret=1
  159. fi
  160. done
  161. exit $ret
  162. }
  163. check_prereqs
  164. rc=0
  165. run_test test_create
  166. run_test test_create_empty
  167. run_test test_create_read
  168. run_test test_delete
  169. run_test test_zero_size_delete
  170. run_test test_open_unlink
  171. run_test test_valid_filenames
  172. run_test test_invalid_filenames
  173. exit $rc