efivarfs.sh 3.3 KB

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