efivarfs.sh 3.6 KB

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