Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/testing/selftests/efivarfs/efivarfs.sh
26285 views
1
#!/bin/bash
2
# SPDX-License-Identifier: GPL-2.0
3
4
efivarfs_mount=/sys/firmware/efi/efivars
5
test_guid=210be57c-9849-4fc7-a635-e6382d1aec27
6
7
# Kselftest framework requirement - SKIP code is 4.
8
ksft_skip=4
9
10
file_cleanup()
11
{
12
chattr -i $1
13
rm -f $1
14
}
15
16
check_prereqs()
17
{
18
local msg="skip all tests:"
19
20
if [ $UID != 0 ]; then
21
echo $msg must be run as root >&2
22
exit $ksft_skip
23
fi
24
25
if ! grep -q "^\S\+ $efivarfs_mount efivarfs" /proc/mounts; then
26
echo $msg efivarfs is not mounted on $efivarfs_mount >&2
27
exit $ksft_skip
28
fi
29
}
30
31
run_test()
32
{
33
local test="$1"
34
35
echo "--------------------"
36
echo "running $test"
37
echo "--------------------"
38
39
if [ "$(type -t $test)" = 'function' ]; then
40
( $test )
41
else
42
( ./$test )
43
fi
44
45
if [ $? -ne 0 ]; then
46
echo " [FAIL]"
47
rc=1
48
else
49
echo " [PASS]"
50
fi
51
}
52
53
test_create()
54
{
55
local attrs='\x07\x00\x00\x00'
56
local file=$efivarfs_mount/$FUNCNAME-$test_guid
57
58
printf "$attrs\x00" > $file
59
60
if [ ! -e $file ]; then
61
echo "$file couldn't be created" >&2
62
exit 1
63
fi
64
65
if [ $(stat -c %s $file) -ne 5 ]; then
66
echo "$file has invalid size" >&2
67
file_cleanup $file
68
exit 1
69
fi
70
file_cleanup $file
71
}
72
73
test_create_empty()
74
{
75
local file=$efivarfs_mount/$FUNCNAME-$test_guid
76
77
: > $file
78
79
if [ -e $file ]; then
80
echo "$file can be created without writing" >&2
81
file_cleanup $file
82
exit 1
83
fi
84
}
85
86
test_create_read()
87
{
88
local file=$efivarfs_mount/$FUNCNAME-$test_guid
89
./create-read $file
90
if [ $? -ne 0 ]; then
91
echo "create and read $file failed"
92
exit 1
93
fi
94
if [ -e $file ]; then
95
echo "file still exists and should not"
96
file_cleanup $file
97
exit 1
98
fi
99
}
100
101
test_delete()
102
{
103
local attrs='\x07\x00\x00\x00'
104
local file=$efivarfs_mount/$FUNCNAME-$test_guid
105
106
printf "$attrs\x00" > $file
107
108
if [ ! -e $file ]; then
109
echo "$file couldn't be created" >&2
110
exit 1
111
fi
112
113
file_cleanup $file
114
115
if [ -e $file ]; then
116
echo "$file couldn't be deleted" >&2
117
exit 1
118
fi
119
120
}
121
122
# test that we can remove a variable by issuing a write with only
123
# attributes specified
124
test_zero_size_delete()
125
{
126
local attrs='\x07\x00\x00\x00'
127
local file=$efivarfs_mount/$FUNCNAME-$test_guid
128
129
printf "$attrs\x00" > $file
130
131
if [ ! -e $file ]; then
132
echo "$file does not exist" >&2
133
exit 1
134
fi
135
136
chattr -i $file
137
printf "$attrs" > $file
138
139
if [ -e $file ]; then
140
echo "$file should have been deleted" >&2
141
exit 1
142
fi
143
}
144
145
test_open_unlink()
146
{
147
local file=$efivarfs_mount/$FUNCNAME-$test_guid
148
./open-unlink $file
149
}
150
151
# test that we can create a range of filenames
152
test_valid_filenames()
153
{
154
local attrs='\x07\x00\x00\x00'
155
local ret=0
156
157
local file_list="abc dump-type0-11-1-1362436005 1234 -"
158
for f in $file_list; do
159
local file=$efivarfs_mount/$f-$test_guid
160
161
printf "$attrs\x00" > $file
162
163
if [ ! -e $file ]; then
164
echo "$file could not be created" >&2
165
ret=1
166
else
167
file_cleanup $file
168
fi
169
done
170
171
exit $ret
172
}
173
174
test_invalid_filenames()
175
{
176
local attrs='\x07\x00\x00\x00'
177
local ret=0
178
179
local file_list="
180
-1234-1234-1234-123456789abc
181
foo
182
foo-bar
183
-foo-
184
foo-barbazba-foob-foob-foob-foobarbazfoo
185
foo-------------------------------------
186
-12345678-1234-1234-1234-123456789abc
187
a-12345678=1234-1234-1234-123456789abc
188
a-12345678-1234=1234-1234-123456789abc
189
a-12345678-1234-1234=1234-123456789abc
190
a-12345678-1234-1234-1234=123456789abc
191
1112345678-1234-1234-1234-123456789abc"
192
193
for f in $file_list; do
194
local file=$efivarfs_mount/$f
195
196
printf "$attrs\x00" 2>/dev/null > $file
197
198
if [ -e $file ]; then
199
echo "Creating $file should have failed" >&2
200
file_cleanup $file
201
ret=1
202
fi
203
done
204
205
exit $ret
206
}
207
208
test_no_set_size()
209
{
210
local attrs='\x07\x00\x00\x00'
211
local file=$efivarfs_mount/$FUNCNAME-$test_guid
212
local ret=0
213
214
printf "$attrs\x00" > $file
215
[ -e $file -a -s $file ] || exit 1
216
chattr -i $file
217
: > $file
218
if [ $? != 0 ]; then
219
echo "variable file failed to accept truncation"
220
ret=1
221
elif [ -e $file -a ! -s $file ]; then
222
echo "file can be truncated to zero size"
223
ret=1
224
fi
225
rm $file || exit 1
226
227
exit $ret
228
}
229
230
setup_test_multiple()
231
{
232
##
233
# we're going to do multi-threaded tests, so create a set of
234
# pipes for synchronization. We use pipes 1..3 to start the
235
# stalled shell job and pipes 4..6 as indicators that the job
236
# has started. If you need more than 3 jobs the two +3's below
237
# need increasing
238
##
239
240
declare -ag p
241
242
# empty is because arrays number from 0 but jobs number from 1
243
p[0]=""
244
245
for f in 1 2 3 4 5 6; do
246
p[$f]=/tmp/efivarfs_pipe${f}
247
mknod ${p[$f]} p
248
done
249
250
declare -g var=$efivarfs_mount/test_multiple-$test_guid
251
252
cleanup() {
253
for f in ${p[@]}; do
254
rm -f ${f}
255
done
256
if [ -e $var ]; then
257
file_cleanup $var
258
fi
259
}
260
trap cleanup exit
261
262
waitstart() {
263
cat ${p[$[$1+3]]} > /dev/null
264
}
265
266
waitpipe() {
267
echo 1 > ${p[$[$1+3]]}
268
cat ${p[$1]} > /dev/null
269
}
270
271
endjob() {
272
echo 1 > ${p[$1]}
273
wait -n %$1
274
}
275
}
276
277
test_multiple_zero_size()
278
{
279
##
280
# check for remove on last close, set up three threads all
281
# holding the variable (one write and two reads) and then
282
# close them sequentially (waiting for completion) and check
283
# the state of the variable
284
##
285
286
{ waitpipe 1; echo 1; } > $var 2> /dev/null &
287
waitstart 1
288
# zero length file should exist
289
[ -e $var ] || exit 1
290
# second and third delayed close
291
{ waitpipe 2; } < $var &
292
waitstart 2
293
{ waitpipe 3; } < $var &
294
waitstart 3
295
# close first fd
296
endjob 1
297
# var should only be deleted on last close
298
[ -e $var ] || exit 1
299
# close second fd
300
endjob 2
301
[ -e $var ] || exit 1
302
# file should go on last close
303
endjob 3
304
[ ! -e $var ] || exit 1
305
}
306
307
test_multiple_create()
308
{
309
##
310
# set multiple threads to access the variable but delay
311
# the final write to check the close of 2 and 3. The
312
# final write should succeed in creating the variable
313
##
314
{ waitpipe 1; printf '\x07\x00\x00\x00\x54'; } > $var &
315
waitstart 1
316
[ -e $var -a ! -s $var ] || exit 1
317
{ waitpipe 2; } < $var &
318
waitstart 2
319
{ waitpipe 3; } < $var &
320
waitstart 3
321
# close second and third fds
322
endjob 2
323
# var should only be created (have size) on last close
324
[ -e $var -a ! -s $var ] || exit 1
325
endjob 3
326
[ -e $var -a ! -s $var ] || exit 1
327
# close first fd
328
endjob 1
329
# variable should still exist
330
[ -s $var ] || exit 1
331
file_cleanup $var
332
}
333
334
test_multiple_delete_on_write() {
335
##
336
# delete the variable on final write; seqencing similar
337
# to test_multiple_create()
338
##
339
printf '\x07\x00\x00\x00\x54' > $var
340
chattr -i $var
341
{ waitpipe 1; printf '\x07\x00\x00\x00'; } > $var &
342
waitstart 1
343
[ -e $var -a -s $var ] || exit 1
344
{ waitpipe 2; } < $var &
345
waitstart 2
346
{ waitpipe 3; } < $var &
347
waitstart 3
348
# close first fd; write should set variable size to zero
349
endjob 1
350
# var should only be deleted on last close
351
[ -e $var -a ! -s $var ] || exit 1
352
endjob 2
353
[ -e $var ] || exit 1
354
# close last fd
355
endjob 3
356
# variable should now be removed
357
[ ! -e $var ] || exit 1
358
}
359
360
check_prereqs
361
362
rc=0
363
364
run_test test_create
365
run_test test_create_empty
366
run_test test_create_read
367
run_test test_delete
368
run_test test_zero_size_delete
369
run_test test_open_unlink
370
run_test test_valid_filenames
371
run_test test_invalid_filenames
372
run_test test_no_set_size
373
setup_test_multiple
374
run_test test_multiple_zero_size
375
run_test test_multiple_create
376
run_test test_multiple_delete_on_write
377
378
exit $rc
379
380