xref: /openbmc/u-boot/test/fs/fs-test.sh (revision 274bced8)
1#!/bin/bash
2#
3# (C) Copyright 2014 Suriyan Ramasami
4#
5#  SPDX-License-Identifier:	GPL-2.0+
6#
7
8# Invoke this test script from U-Boot base directory as ./test/fs/fs-test.sh
9# It currently tests the fs/sb and native commands for ext4 and fat partitions
10# Expected results are as follows:
11# EXT4 tests:
12# fs-test.sb.ext4.out: Summary: PASS: 23 FAIL: 0
13# fs-test.ext4.out: Summary: PASS: 23 FAIL: 0
14# fs-test.fs.ext4.out: Summary: PASS: 23 FAIL: 0
15# FAT tests:
16# fs-test.sb.fat.out: Summary: PASS: 23 FAIL: 0
17# fs-test.fat.out: Summary: PASS: 20 FAIL: 3
18# fs-test.fs.fat.out: Summary: PASS: 20 FAIL: 3
19# Total Summary: TOTAL PASS: 132 TOTAL FAIL: 6
20
21# pre-requisite binaries list.
22PREREQ_BINS="md5sum mkfs mount umount dd fallocate mkdir"
23
24# All generated output files from this test will be in $OUT_DIR
25# Hence everything is sandboxed.
26OUT_DIR="sandbox/test/fs"
27
28# Location of generated sandbox u-boot
29UBOOT="./sandbox/u-boot"
30
31# Our mount directory will be in the sandbox
32MOUNT_DIR="${OUT_DIR}/mnt"
33
34# The file system image we create will have the $IMG prefix.
35IMG="${OUT_DIR}/3GB"
36
37# $SMALL_FILE is the name of the 1MB file in the file system image
38SMALL_FILE="1MB.file"
39
40# $BIG_FILE is the name of the 2.5GB file in the file system image
41BIG_FILE="2.5GB.file"
42
43# $MD5_FILE will have the expected md5s when we do the test
44# They shall have a suffix which represents their file system (ext4/fat)
45MD5_FILE="${OUT_DIR}/md5s.list"
46
47# $OUT shall be the prefix of the test output. Their suffix will be .out
48OUT="${OUT_DIR}/fs-test"
49
50# Full Path of the 1 MB file that shall be created in the fs image.
51MB1="${MOUNT_DIR}/${SMALL_FILE}"
52GB2p5="${MOUNT_DIR}/${BIG_FILE}"
53
54# ************************
55# * Functions start here *
56# ************************
57
58# Check if the prereq binaries exist, or exit
59function check_prereq() {
60	for prereq in $PREREQ_BINS; do
61		if [ ! -x "`which $prereq`" ]; then
62			echo "Missing $prereq binary. Exiting!"
63			exit
64		fi
65	done
66
67	# We use /dev/urandom to create files. Check if it exists.
68	if [ ! -c /dev/urandom ]; then
69		echo "Missing character special /dev/urandom. Exiting!"
70		exit
71	fi
72}
73
74# If 1st param is "clean", then clean out the generated files and exit
75function check_clean() {
76	if [ "$1" = "clean" ]; then
77		rm -rf "$OUT_DIR"
78		echo "Cleaned up generated files. Exiting"
79		exit
80	fi
81}
82
83# Generate sandbox U-Boot - gleaned from /test/dm/test-dm.sh
84function compile_sandbox() {
85	unset CROSS_COMPILE
86	NUM_CPUS=$(cat /proc/cpuinfo |grep -c processor)
87	make O=sandbox sandbox_config
88	make O=sandbox -s -j${NUM_CPUS}
89
90	# Check if U-Boot exists
91	if [ ! -x "$UBOOT" ]; then
92		echo "$UBOOT does not exist or is not executable"
93		echo "Build error?"
94		echo "Please run this script as ./test/fs/`basename $0`"
95		exit
96	fi
97}
98
99# Clean out all generated files other than the file system images
100# We save time by not deleting and recreating the file system images
101function prepare_env() {
102	rm -f ${MD5_FILE}.* ${OUT}.*
103	mkdir -p ${OUT_DIR}
104}
105
106# 1st parameter is the name of the image file to be created
107# 2nd parameter is the filesystem - fat ext4 etc
108# -F cant be used with fat as it means something else.
109function create_image() {
110	# Create image if not already present - saves time, while debugging
111	if [ "$2" = "ext4" ]; then
112		MKFS_OPTION="-F"
113	else
114		MKFS_OPTION=""
115	fi
116	if [ ! -f "$1" ]; then
117		fallocate -l 3G "$1" &> /dev/null
118		if [ $? -ne 0 ]; then
119			echo fallocate failed - using dd instead
120			dd if=/dev/zero of=$1 bs=1024 count=$((3 * 1024 * 1024))
121			if [ $? -ne 0 ]; then
122				echo Could not create empty disk image
123				exit $?
124			fi
125		fi
126		mkfs -t "$2" $MKFS_OPTION "$1" &> /dev/null
127		if [ $? -ne 0 -a "$2" = "fat" ]; then
128			# If we fail and we did fat, try vfat.
129			mkfs -t vfat $MKFS_OPTION "$1" &> /dev/null
130		fi
131		if [ $? -ne 0 ]; then
132			echo Could not create filesystem
133			exit $?
134		fi
135	fi
136}
137
138# 1st parameter is image file
139# 2nd parameter is file system type - fat/ext4
140# 3rd parameter is name of small file
141# 4th parameter is name of big file
142# 5th parameter is fs/nonfs/sb - to dictate generic fs commands or
143# otherwise or sb hostfs
144# 6th parameter is the directory path for the files. Its "" for generic
145# fs and ext4/fat and full patch for sb hostfs
146# UBOOT is set in env
147function test_image() {
148	addr="0x01000008"
149	length="0x00100000"
150
151	case "$2" in
152		fat)
153		FPATH=""
154		PREFIX="fat"
155		WRITE="write"
156		;;
157
158		ext4)
159		# ext4 needs absolute path
160		FPATH="/"
161		PREFIX="ext4"
162		WRITE="write"
163		;;
164
165		*)
166		echo "Unhandled filesystem $2. Exiting!"
167		exit
168		;;
169	esac
170
171	case "$5" in
172		fs)
173		PREFIX=""
174		WRITE="save"
175		SUFFIX=" 0:0"
176		;;
177
178		nonfs)
179		SUFFIX=" 0:0"
180		;;
181
182		sb)
183		PREFIX="sb "
184		WRITE="save"
185		SUFFIX="fs -"
186		;;
187
188		*)
189		echo "Unhandled mode $5. Exiting!"
190		exit
191		;;
192
193	esac
194
195	# sb always uses full path to mointpoint, irrespective of filesystem
196	if [ "$5" = "sb" ]; then
197		FPATH=${6}/
198	fi
199
200	FILE_WRITE=${3}.w
201	FILE_SMALL=$3
202	FILE_BIG=$4
203
204	# In u-boot commands, <interface> stands for host or hostfs
205	# hostfs maps to the host fs.
206	# host maps to the "sb bind" that we do
207
208	$UBOOT << EOF
209sb=$5
210setenv bind 'if test "\$sb" != sb; then sb bind 0 "$1"; fi'
211run bind
212# Test Case 1 - ls
213${PREFIX}ls host${SUFFIX} $6
214#
215# We want ${PREFIX}size host 0:0 $3 for host commands and
216# sb size hostfs - $3 for hostfs commands.
217# 1MB is 0x0010 0000
218# Test Case 2 - size of small file
219${PREFIX}size host${SUFFIX} ${FPATH}$FILE_SMALL
220printenv filesize
221setenv filesize
222
223# 2.5GB (1024*1024*2500) is 0x9C40 0000
224# Test Case 3 - size of big file
225${PREFIX}size host${SUFFIX} ${FPATH}$FILE_BIG
226printenv filesize
227setenv filesize
228
229# Notes about load operation
230# If I use 0x01000000 I get DMA misaligned error message
231# Last two parameters are size and offset.
232
233# Test Case 4a - Read full 1MB of small file
234${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_SMALL
235printenv filesize
236# Test Case 4b - Read full 1MB of small file
237md5sum $addr \$filesize
238setenv filesize
239
240# Test Case 5a - First 1MB of big file
241${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x0
242printenv filesize
243# Test Case 5b - First 1MB of big file
244md5sum $addr \$filesize
245setenv filesize
246
247# fails for ext as no offset support
248# Test Case 6a - Last 1MB of big file
249${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x9C300000
250printenv filesize
251# Test Case 6b - Last 1MB of big file
252md5sum $addr \$filesize
253setenv filesize
254
255# fails for ext as no offset support
256# Test Case 7a - One from the last 1MB chunk of 2GB
257${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x7FF00000
258printenv filesize
259# Test Case 7b - One from the last 1MB chunk of 2GB
260md5sum $addr \$filesize
261setenv filesize
262
263# fails for ext as no offset support
264# Test Case 8a - One from the start 1MB chunk from 2GB
265${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x80000000
266printenv filesize
267# Test Case 8b - One from the start 1MB chunk from 2GB
268md5sum $addr \$filesize
269setenv filesize
270
271# fails for ext as no offset support
272# Test Case 9a - One 1MB chunk crossing the 2GB boundary
273${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x7FF80000
274printenv filesize
275# Test Case 9b - One 1MB chunk crossing the 2GB boundary
276md5sum $addr \$filesize
277setenv filesize
278
279# Generic failure case
280# Test Case 10 - 2MB chunk from the last 1MB of big file
281${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG 0x00200000 0x9C300000
282printenv filesize
283#
284
285# Read 1MB from small file
286${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_SMALL
287# Write it back to test the writes
288# Test Case 11a - Check that the write succeeded
289${PREFIX}${WRITE} host${SUFFIX} $addr ${FPATH}$FILE_WRITE \$filesize
290mw.b $addr 00 100
291${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_WRITE
292# Test Case 11b - Check md5 of written to is same as the one read from
293md5sum $addr \$filesize
294setenv filesize
295#
296
297# Next test case checks writing a file whose dirent
298# is the first in the block, which is always true for "."
299# The write should fail, but the lookup should work
300# Test Case 12 - Check directory traversal
301${PREFIX}${WRITE} host${SUFFIX} $addr ${FPATH}. 0x10
302
303# Read 1MB from small file
304${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_SMALL
305# Write it via "same directory", i.e. "." dirent
306# Test Case 13a - Check directory traversal
307${PREFIX}${WRITE} host${SUFFIX} $addr ${FPATH}./${FILE_WRITE}2 \$filesize
308mw.b $addr 00 100
309${PREFIX}load host${SUFFIX} $addr ${FPATH}./${FILE_WRITE}2
310# Test Case 13b - Check md5 of written to is same as the one read from
311md5sum $addr \$filesize
312setenv filesize
313mw.b $addr 00 100
314${PREFIX}load host${SUFFIX} $addr ${FPATH}${FILE_WRITE}2
315# Test Case 13c - Check md5 of written to is same as the one read from
316md5sum $addr \$filesize
317setenv filesize
318#
319reset
320
321EOF
322}
323
324# 1st argument is the name of the image file.
325# 2nd argument is the file where we generate the md5s of the files
326# generated with the appropriate start and length that we use to test.
327# It creates the necessary files in the image to test.
328# $GB2p5 is the path of the big file (2.5 GB)
329# $MB1 is the path of the small file (1 MB)
330# $MOUNT_DIR is the path we can use to mount the image file.
331function create_files() {
332	# Mount the image so we can populate it.
333	mkdir -p "$MOUNT_DIR"
334	sudo mount -o loop,rw "$1" "$MOUNT_DIR"
335
336	# Create big file in this image.
337	# Note that we work only on the start 1MB, couple MBs in the 2GB range
338	# and the last 1 MB of the huge 2.5GB file.
339	# So, just put random values only in those areas.
340	if [ ! -f "${GB2p5}" ]; then
341		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 \
342			&> /dev/null
343		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=2 seek=2047 \
344			&> /dev/null
345		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 seek=2499 \
346			&> /dev/null
347	fi
348
349	# Create a small file in this image.
350	if [ ! -f "${MB1}" ]; then
351		sudo dd if=/dev/urandom of="${MB1}" bs=1M count=1 \
352			&> /dev/null
353	fi
354
355	# Delete the small file copies which possibly are written as part of a
356	# previous test.
357	sudo rm -f "${MB1}.w"
358	sudo rm -f "${MB1}.w2"
359
360	# Generate the md5sums of reads that we will test against small file
361	dd if="${MB1}" bs=1M skip=0 count=1 2> /dev/null | md5sum > "$2"
362
363	# Generate the md5sums of reads that we will test against big file
364	# One from beginning of file.
365	dd if="${GB2p5}" bs=1M skip=0 count=1 \
366		2> /dev/null | md5sum >> "$2"
367
368	# One from end of file.
369	dd if="${GB2p5}" bs=1M skip=2499 count=1 \
370		2> /dev/null | md5sum >> "$2"
371
372	# One from the last 1MB chunk of 2GB
373	dd if="${GB2p5}" bs=1M skip=2047 count=1 \
374		2> /dev/null | md5sum >> "$2"
375
376	# One from the start 1MB chunk from 2GB
377	dd if="${GB2p5}" bs=1M skip=2048 count=1 \
378		2> /dev/null | md5sum >> "$2"
379
380	# One 1MB chunk crossing the 2GB boundary
381	dd if="${GB2p5}" bs=512K skip=4095 count=2 \
382		2> /dev/null | md5sum >> "$2"
383
384	sync
385	sudo umount "$MOUNT_DIR"
386	rmdir "$MOUNT_DIR"
387}
388
389# 1st parameter is the text to print
390# if $? is 0 its a pass, else a fail
391# As a side effect it shall update env variable PASS and FAIL
392function pass_fail() {
393	if [ $? -eq 0 ]; then
394		echo pass - "$1"
395		PASS=$((PASS + 1))
396	else
397		echo FAIL - "$1"
398		FAIL=$((FAIL + 1))
399	fi
400}
401
402# 1st parameter is the string which leads to an md5 generation
403# 2nd parameter is the file we grep, for that string
404# 3rd parameter is the name of the file which has md5s in it
405# 4th parameter is the line # in the md5 file that we match it against
406# This function checks if the md5 of the file in the sandbox matches
407# that calculated while generating the file
408# 5th parameter is the string to print with the result
409check_md5() {
410	# md5sum in u-boot has output of form:
411	# md5 for 01000008 ... 01100007 ==> <md5>
412	# the 7th field is the actual md5
413	md5_src=`grep -A2 "$1" "$2" | grep "md5 for" | tr -d '\r'`
414	md5_src=($md5_src)
415	md5_src=${md5_src[6]}
416
417	# The md5 list, each line is of the form:
418	# - <md5>
419	# the 2nd field is the actual md5
420	md5_dst=`sed -n $4p $3`
421	md5_dst=($md5_dst)
422	md5_dst=${md5_dst[0]}
423
424	# For a pass they should match.
425	[ "$md5_src" = "$md5_dst" ]
426	pass_fail "$5"
427}
428
429# 1st parameter is the name of the output file to check
430# 2nd parameter is the name of the file containing the md5 expected
431# 3rd parameter is the name of the small file
432# 4th parameter is the name of the big file
433# 5th paramter is the name of the written file
434# This function checks the output file for correct results.
435function check_results() {
436	echo "** Start $1"
437
438	PASS=0
439	FAIL=0
440
441	# Check if the ls is showing correct results for 2.5 gb file
442	grep -A6 "Test Case 1 " "$1" | egrep -iq "2621440000 *$4"
443	pass_fail "TC1: ls of $4"
444
445	# Check if the ls is showing correct results for 1 mb file
446	grep -A6 "Test Case 1 " "$1" | egrep -iq "1048576 *$3"
447	pass_fail "TC1: ls of $3"
448
449	# Check size command on 1MB.file
450	egrep -A3 "Test Case 2 " "$1" | grep -q "filesize=100000"
451	pass_fail "TC2: size of $3"
452
453	# Check size command on 2.5GB.file
454	egrep -A3 "Test Case 3 " "$1" | grep -q "filesize=9c400000"
455	pass_fail "TC3: size of $4"
456
457	# Check read full mb of 1MB.file
458	grep -A4 "Test Case 4a " "$1" | grep -q "filesize=100000"
459	pass_fail "TC4: load of $3 size"
460	check_md5 "Test Case 4b " "$1" "$2" 1 "TC4: load from $3"
461
462	# Check first mb of 2.5GB.file
463	grep -A4 "Test Case 5a " "$1" | grep -q "filesize=100000"
464	pass_fail "TC5: load of 1st MB from $4 size"
465	check_md5 "Test Case 5b " "$1" "$2" 2 "TC5: load of 1st MB from $4"
466
467	# Check last mb of 2.5GB.file
468	grep -A4 "Test Case 6a " "$1" | grep -q "filesize=100000"
469	pass_fail "TC6: load of last MB from $4 size"
470	check_md5 "Test Case 6b " "$1" "$2" 3 "TC6: load of last MB from $4"
471
472	# Check last 1mb chunk of 2gb from 2.5GB file
473	grep -A4 "Test Case 7a " "$1" | grep -q "filesize=100000"
474	pass_fail "TC7: load of last 1mb chunk of 2GB from $4 size"
475	check_md5 "Test Case 7b " "$1" "$2" 4 \
476		"TC7: load of last 1mb chunk of 2GB from $4"
477
478	# Check first 1mb chunk after 2gb from 2.5GB file
479	grep -A4 "Test Case 8a " "$1" | grep -q "filesize=100000"
480	pass_fail "TC8: load 1st MB chunk after 2GB from $4 size"
481	check_md5 "Test Case 8b " "$1" "$2" 5 \
482		"TC8: load 1st MB chunk after 2GB from $4"
483
484	# Check 1mb chunk crossing the 2gb boundary from 2.5GB file
485	grep -A4 "Test Case 9a " "$1" | grep -q "filesize=100000"
486	pass_fail "TC9: load 1MB chunk crossing 2GB boundary from $4 size"
487	check_md5 "Test Case 9b " "$1" "$2" 6 \
488		"TC9: load 1MB chunk crossing 2GB boundary from $4"
489
490	# Check 2mb chunk from the last 1MB of 2.5GB file loads 1MB
491	grep -A5 "Test Case 10 " "$1" | grep -q "filesize=100000"
492	pass_fail "TC10: load 2MB from the last 1MB of $4 loads 1MB"
493
494	# Check 1mb chunk write
495	grep -A2 "Test Case 11a " "$1" | grep -q '1048576 bytes written'
496	pass_fail "TC11: 1MB write to $3.w - write succeeded"
497	check_md5 "Test Case 11b " "$1" "$2" 1 \
498		"TC11: 1MB write to $3.w - content verified"
499
500	# Check lookup of 'dot' directory
501	grep -A4 "Test Case 12 " "$1" | grep -q 'Unable to write file'
502	pass_fail "TC12: 1MB write to . - write denied"
503
504	# Check directory traversal
505	grep -A2 "Test Case 13a " "$1" | grep -q '1048576 bytes written'
506	pass_fail "TC13: 1MB write to ./$3.w2 - write succeeded"
507	check_md5 "Test Case 13b " "$1" "$2" 1 \
508		"TC13: 1MB read from ./$3.w2 - content verified"
509	check_md5 "Test Case 13c " "$1" "$2" 1 \
510		"TC13: 1MB read from $3.w2 - content verified"
511
512	echo "** End $1"
513}
514
515# Takes in one parameter which is "fs" or "nonfs", which then dictates
516# if a fs test (size/load/save) or a nonfs test (fatread/extread) needs to
517# be performed.
518function test_fs_nonfs() {
519	echo "Creating files in $fs image if not already present."
520	create_files $IMAGE $MD5_FILE_FS
521
522	OUT_FILE="${OUT}.$1.${fs}.out"
523	test_image $IMAGE $fs $SMALL_FILE $BIG_FILE $1 "" \
524		> ${OUT_FILE} 2>&1
525	# strip out noise from fs code
526	grep -v -e "File System is consistent\|update journal finished" \
527		-e "reading .*\.file\|writing .*\.file.w" \
528		< ${OUT_FILE} > ${OUT_FILE}_clean
529	check_results ${OUT_FILE}_clean $MD5_FILE_FS $SMALL_FILE \
530		$BIG_FILE
531	TOTAL_FAIL=$((TOTAL_FAIL + FAIL))
532	TOTAL_PASS=$((TOTAL_PASS + PASS))
533	echo "Summary: PASS: $PASS FAIL: $FAIL"
534	echo "--------------------------------------------"
535}
536
537# ********************
538# * End of functions *
539# ********************
540
541check_clean "$1"
542check_prereq
543compile_sandbox
544prepare_env
545
546# Track TOTAL_FAIL and TOTAL_PASS
547TOTAL_FAIL=0
548TOTAL_PASS=0
549
550# In each loop, for a given file system image, we test both the
551# fs command, like load/size/write, the file system specific command
552# like: ext4load/ext4size/ext4write and the sb load/ls/save commands.
553for fs in ext4 fat; do
554
555	echo "Creating $fs image if not already present."
556	IMAGE=${IMG}.${fs}.img
557	MD5_FILE_FS="${MD5_FILE}.${fs}"
558	create_image $IMAGE $fs
559
560	# sb commands test
561	echo "Creating files in $fs image if not already present."
562	create_files $IMAGE $MD5_FILE_FS
563
564	# Lets mount the image and test sb hostfs commands
565	mkdir -p "$MOUNT_DIR"
566	if [ "$fs" = "fat" ]; then
567		uid="uid=`id -u`"
568	else
569		uid=""
570	fi
571	sudo mount -o loop,rw,$uid "$IMAGE" "$MOUNT_DIR"
572	sudo chmod 777 "$MOUNT_DIR"
573
574	OUT_FILE="${OUT}.sb.${fs}.out"
575	test_image $IMAGE $fs $SMALL_FILE $BIG_FILE sb `pwd`/$MOUNT_DIR \
576		> ${OUT_FILE} 2>&1
577	sudo umount "$MOUNT_DIR"
578	rmdir "$MOUNT_DIR"
579
580	check_results $OUT_FILE $MD5_FILE_FS $SMALL_FILE $BIG_FILE
581	TOTAL_FAIL=$((TOTAL_FAIL + FAIL))
582	TOTAL_PASS=$((TOTAL_PASS + PASS))
583	echo "Summary: PASS: $PASS FAIL: $FAIL"
584	echo "--------------------------------------------"
585
586	test_fs_nonfs nonfs
587	test_fs_nonfs fs
588done
589
590echo "Total Summary: TOTAL PASS: $TOTAL_PASS TOTAL FAIL: $TOTAL_FAIL"
591echo "--------------------------------------------"
592if [ $TOTAL_FAIL -eq 0 ]; then
593	echo "PASSED"
594	exit 0
595else
596	echo "FAILED"
597	exit 1
598fi
599