efivarfs.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_delete()
  58. {
  59. local attrs='\x07\x00\x00\x00'
  60. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  61. printf "$attrs\x00" > $file
  62. if [ ! -e $file ]; then
  63. echo "$file couldn't be created" >&2
  64. exit 1
  65. fi
  66. rm $file
  67. if [ -e $file ]; then
  68. echo "$file couldn't be deleted" >&2
  69. exit 1
  70. fi
  71. }
  72. # test that we can remove a variable by issuing a write with only
  73. # attributes specified
  74. test_zero_size_delete()
  75. {
  76. local attrs='\x07\x00\x00\x00'
  77. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  78. printf "$attrs\x00" > $file
  79. if [ ! -e $file ]; then
  80. echo "$file does not exist" >&2
  81. exit 1
  82. fi
  83. printf "$attrs" > $file
  84. if [ -e $file ]; then
  85. echo "$file should have been deleted" >&2
  86. exit 1
  87. fi
  88. }
  89. test_open_unlink()
  90. {
  91. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  92. ./open-unlink $file
  93. }
  94. check_prereqs
  95. rc=0
  96. run_test test_create
  97. run_test test_create_empty
  98. run_test test_delete
  99. run_test test_zero_size_delete
  100. run_test test_open_unlink
  101. exit $rc