efivarfs.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_delete()
  49. {
  50. local attrs='\x07\x00\x00\x00'
  51. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  52. printf "$attrs\x00" > $file
  53. if [ ! -e $file ]; then
  54. echo "$file couldn't be created" >&2
  55. exit 1
  56. fi
  57. rm $file
  58. if [ -e $file ]; then
  59. echo "$file couldn't be deleted" >&2
  60. exit 1
  61. fi
  62. }
  63. # test that we can remove a variable by issuing a write with only
  64. # attributes specified
  65. test_zero_size_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 does not exist" >&2
  72. exit 1
  73. fi
  74. printf "$attrs" > $file
  75. if [ -e $file ]; then
  76. echo "$file should have been deleted" >&2
  77. exit 1
  78. fi
  79. }
  80. test_open_unlink()
  81. {
  82. local file=$efivarfs_mount/$FUNCNAME-$test_guid
  83. ./open-unlink $file
  84. }
  85. check_prereqs
  86. rc=0
  87. run_test test_create
  88. run_test test_delete
  89. run_test test_zero_size_delete
  90. run_test test_open_unlink
  91. exit $rc