efivarfs.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. check_prereqs
  100. rc=0
  101. run_test test_create
  102. run_test test_create_empty
  103. run_test test_create_read
  104. run_test test_delete
  105. run_test test_zero_size_delete
  106. run_test test_open_unlink
  107. exit $rc