xref: /openbmc/u-boot/test/fs/fs-test.sh (revision 41582e2e)
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: 17 FAIL: 2
13# fs-test.ext4.out: Summary: PASS: 10 FAIL: 9
14# fs-test.fs.ext4.out: Summary: PASS: 10 FAIL: 9
15# FAT tests:
16# fs-test.sb.fat.out: Summary: PASS: 17 FAIL: 2
17# fs-test.fat.out: Summary: PASS: 19 FAIL: 0
18# fs-test.fs.fat.out: Summary: PASS: 19 FAIL: 0
19# Total Summary: TOTAL PASS: 92 TOTAL FAIL: 22
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 the FS type: fat/ext4
139# 2nd parameter is the name of small file
140# Returns filename which can be used for fat or ext4 for writing
141function fname_for_write() {
142	case $1 in
143		ext4)
144			# ext4 needs absolute path name of file
145			echo /${2}.w
146			;;
147
148		*)
149			echo ${2}.w
150			;;
151	esac
152}
153
154# 1st parameter is image file
155# 2nd parameter is file system type - fat/ext4
156# 3rd parameter is name of small file
157# 4th parameter is name of big file
158# 5th parameter is fs/nonfs/sb - to dictate generic fs commands or
159# otherwise or sb hostfs
160# 6th parameter is the directory path for the files. Its "" for generic
161# fs and ext4/fat and full patch for sb hostfs
162# UBOOT is set in env
163function test_image() {
164	addr="0x01000008"
165	length="0x00100000"
166
167	case "$2" in
168		fat)
169		PREFIX="fat"
170		WRITE="write"
171		;;
172
173		ext4)
174		PREFIX="ext4"
175		WRITE="write"
176		;;
177
178		*)
179		echo "Unhandled filesystem $2. Exiting!"
180		exit
181		;;
182	esac
183
184	case "$5" in
185		fs)
186		PREFIX=""
187		WRITE="save"
188		SUFFIX=" 0:0"
189		;;
190
191		nonfs)
192		SUFFIX=" 0:0"
193		;;
194
195		sb)
196		PREFIX="sb "
197		WRITE="save"
198		SUFFIX="fs -"
199		;;
200
201		*)
202		echo "Unhandled mode $5. Exiting!"
203		exit
204		;;
205
206	esac
207
208	if [ -z "$6" ]; then
209		FILE_WRITE=`fname_for_write $2 $3`
210		FILE_SMALL=$3
211		FILE_BIG=$4
212	else
213		FILE_WRITE=$6/`fname_for_write $2 $3`
214		FILE_SMALL=$6/$3
215		FILE_BIG=$6/$4
216	fi
217
218	# In u-boot commands, <interface> stands for host or hostfs
219	# hostfs maps to the host fs.
220	# host maps to the "sb bind" that we do
221
222	$UBOOT << EOF
223sb=$5
224setenv bind 'if test "\$sb" != sb; then sb bind 0 "$1"; fi'
225run bind
226# Test Case 1 - ls
227${PREFIX}ls host${SUFFIX} $6
228#
229# We want ${PREFIX}size host 0:0 $3 for host commands and
230# sb size hostfs - $3 for hostfs commands.
231# 1MB is 0x0010 0000
232# Test Case 2 - size of small file
233${PREFIX}size host${SUFFIX} $FILE_SMALL
234printenv filesize
235setenv filesize
236
237# 2.5GB (1024*1024*2500) is 0x9C40 0000
238# Test Case 3 - size of big file
239${PREFIX}size host${SUFFIX} $FILE_BIG
240printenv filesize
241setenv filesize
242
243# Notes about load operation
244# If I use 0x01000000 I get DMA misaligned error message
245# Last two parameters are size and offset.
246
247# Test Case 4a - Read full 1MB of small file
248${PREFIX}load host${SUFFIX} $addr $FILE_SMALL
249printenv filesize
250# Test Case 4b - Read full 1MB of small file
251md5sum $addr \$filesize
252setenv filesize
253
254# Test Case 5a - First 1MB of big file
255${PREFIX}load host${SUFFIX} $addr $FILE_BIG $length 0x0
256printenv filesize
257# Test Case 5b - First 1MB of big file
258md5sum $addr \$filesize
259setenv filesize
260
261# fails for ext as no offset support
262# Test Case 6a - Last 1MB of big file
263${PREFIX}load host${SUFFIX} $addr $FILE_BIG $length 0x9C300000
264printenv filesize
265# Test Case 6b - Last 1MB of big file
266md5sum $addr \$filesize
267setenv filesize
268
269# fails for ext as no offset support
270# Test Case 7a - One from the last 1MB chunk of 2GB
271${PREFIX}load host${SUFFIX} $addr $FILE_BIG $length 0x7FF00000
272printenv filesize
273# Test Case 7b - One from the last 1MB chunk of 2GB
274md5sum $addr \$filesize
275setenv filesize
276
277# fails for ext as no offset support
278# Test Case 8a - One from the start 1MB chunk from 2GB
279${PREFIX}load host${SUFFIX} $addr $FILE_BIG $length 0x80000000
280printenv filesize
281# Test Case 8b - One from the start 1MB chunk from 2GB
282md5sum $addr \$filesize
283setenv filesize
284
285# fails for ext as no offset support
286# Test Case 9a - One 1MB chunk crossing the 2GB boundary
287${PREFIX}load host${SUFFIX} $addr $FILE_BIG $length 0x7FF80000
288printenv filesize
289# Test Case 9b - One 1MB chunk crossing the 2GB boundary
290md5sum $addr \$filesize
291setenv filesize
292
293# Generic failure case
294# Test Case 10 - 2MB chunk from the last 1MB of big file
295${PREFIX}load host${SUFFIX} $addr $FILE_BIG 0x00200000 0x9C300000
296printenv filesize
297#
298
299# Read 1MB from small file
300${PREFIX}load host${SUFFIX} $addr $FILE_SMALL
301# Write it back to test the writes
302# Test Case 11a - Check that the write succeeded
303${PREFIX}${WRITE} host${SUFFIX} $addr $FILE_WRITE \$filesize
304mw.b $addr 00 100
305${PREFIX}load host${SUFFIX} $addr $FILE_WRITE
306# Test Case 11b - Check md5 of written to is same as the one read from
307md5sum $addr \$filesize
308setenv filesize
309#
310reset
311
312EOF
313}
314
315# 1st argument is the name of the image file.
316# 2nd argument is the file where we generate the md5s of the files
317# generated with the appropriate start and length that we use to test.
318# It creates the necessary files in the image to test.
319# $GB2p5 is the path of the big file (2.5 GB)
320# $MB1 is the path of the small file (1 MB)
321# $MOUNT_DIR is the path we can use to mount the image file.
322function create_files() {
323	# Mount the image so we can populate it.
324	mkdir -p "$MOUNT_DIR"
325	sudo mount -o loop,rw "$1" "$MOUNT_DIR"
326
327	# Create big file in this image.
328	# Note that we work only on the start 1MB, couple MBs in the 2GB range
329	# and the last 1 MB of the huge 2.5GB file.
330	# So, just put random values only in those areas.
331	if [ ! -f "${GB2p5}" ]; then
332		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 \
333			&> /dev/null
334		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=2 seek=2047 \
335			&> /dev/null
336		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 seek=2499 \
337			&> /dev/null
338	fi
339
340	# Create a small file in this image.
341	if [ ! -f "${MB1}" ]; then
342		sudo dd if=/dev/urandom of="${MB1}" bs=1M count=1 \
343			&> /dev/null
344	fi
345
346	# Delete the small file which possibly is written as part of a
347	# previous test.
348	sudo rm -f "${MB1}.w"
349
350	# Generate the md5sums of reads that we will test against small file
351	dd if="${MB1}" bs=1M skip=0 count=1 2> /dev/null | md5sum > "$2"
352
353	# Generate the md5sums of reads that we will test against big file
354	# One from beginning of file.
355	dd if="${GB2p5}" bs=1M skip=0 count=1 \
356		2> /dev/null | md5sum >> "$2"
357
358	# One from end of file.
359	dd if="${GB2p5}" bs=1M skip=2499 count=1 \
360		2> /dev/null | md5sum >> "$2"
361
362	# One from the last 1MB chunk of 2GB
363	dd if="${GB2p5}" bs=1M skip=2047 count=1 \
364		2> /dev/null | md5sum >> "$2"
365
366	# One from the start 1MB chunk from 2GB
367	dd if="${GB2p5}" bs=1M skip=2048 count=1 \
368		2> /dev/null | md5sum >> "$2"
369
370	# One 1MB chunk crossing the 2GB boundary
371	dd if="${GB2p5}" bs=512K skip=4095 count=2 \
372		2> /dev/null | md5sum >> "$2"
373
374	sync
375	sudo umount "$MOUNT_DIR"
376	rmdir "$MOUNT_DIR"
377}
378
379# 1st parameter is the text to print
380# if $? is 0 its a pass, else a fail
381# As a side effect it shall update env variable PASS and FAIL
382function pass_fail() {
383	if [ $? -eq 0 ]; then
384		echo pass - "$1"
385		PASS=$((PASS + 1))
386	else
387		echo FAIL - "$1"
388		FAIL=$((FAIL + 1))
389	fi
390}
391
392# 1st parameter is the string which leads to an md5 generation
393# 2nd parameter is the file we grep, for that string
394# 3rd parameter is the name of the file which has md5s in it
395# 4th parameter is the line # in the md5 file that we match it against
396# This function checks if the md5 of the file in the sandbox matches
397# that calculated while generating the file
398# 5th parameter is the string to print with the result
399check_md5() {
400	# md5sum in u-boot has output of form:
401	# md5 for 01000008 ... 01100007 ==> <md5>
402	# the 7th field is the actual md5
403	md5_src=`grep -A3 "$1" "$2" | grep "md5 for"`
404	md5_src=($md5_src)
405	md5_src=${md5_src[6]}
406
407	# The md5 list, each line is of the form:
408	# - <md5>
409	# the 2nd field is the actual md5
410	md5_dst=`sed -n $4p $3`
411	md5_dst=($md5_dst)
412	md5_dst=${md5_dst[0]}
413
414	# For a pass they should match.
415	[ "$md5_src" = "$md5_dst" ]
416	pass_fail "$5"
417}
418
419# 1st parameter is the name of the output file to check
420# 2nd parameter is the name of the file containing the md5 expected
421# 3rd parameter is the name of the small file
422# 4th parameter is the name of the big file
423# 5th paramter is the name of the written file
424# This function checks the output file for correct results.
425function check_results() {
426	echo "** Start $1"
427
428	PASS=0
429	FAIL=0
430
431	# Check if the ls is showing correct results for 2.5 gb file
432	grep -A6 "Test Case 1 " "$1" | egrep -iq "2621440000 *$4"
433	pass_fail "TC1: ls of $4"
434
435	# Check if the ls is showing correct results for 1 mb file
436	grep -A6 "Test Case 1 " "$1" | egrep -iq "1048576 *$3"
437	pass_fail "TC1: ls of $3"
438
439	# Check size command on 1MB.file
440	egrep -A3 "Test Case 2 " "$1" | grep -q "filesize=100000"
441	pass_fail "TC2: size of $3"
442
443	# Check size command on 2.5GB.file
444	egrep -A3 "Test Case 3 " "$1" | grep -q "filesize=9c400000"
445	pass_fail "TC3: size of $4"
446
447	# Check read full mb of 1MB.file
448	grep -A6 "Test Case 4a " "$1" | grep -q "filesize=100000"
449	pass_fail "TC4: load of $3 size"
450	check_md5 "Test Case 4b " "$1" "$2" 1 "TC4: load from $3"
451
452	# Check first mb of 2.5GB.file
453	grep -A6 "Test Case 5a " "$1" | grep -q "filesize=100000"
454	pass_fail "TC5: load of 1st MB from $4 size"
455	check_md5 "Test Case 5b " "$1" "$2" 2 "TC5: load of 1st MB from $4"
456
457	# Check last mb of 2.5GB.file
458	grep -A6 "Test Case 6a " "$1" | grep -q "filesize=100000"
459	pass_fail "TC6: load of last MB from $4 size"
460	check_md5 "Test Case 6b " "$1" "$2" 3 "TC6: load of last MB from $4"
461
462	# Check last 1mb chunk of 2gb from 2.5GB file
463	grep -A6 "Test Case 7a " "$1" | grep -q "filesize=100000"
464	pass_fail "TC7: load of last 1mb chunk of 2GB from $4 size"
465	check_md5 "Test Case 7b " "$1" "$2" 4 \
466		"TC7: load of last 1mb chunk of 2GB from $4"
467
468	# Check first 1mb chunk after 2gb from 2.5GB file
469	grep -A6 "Test Case 8a " "$1" | grep -q "filesize=100000"
470	pass_fail "TC8: load 1st MB chunk after 2GB from $4 size"
471	check_md5 "Test Case 8b " "$1" "$2" 5 \
472		"TC8: load 1st MB chunk after 2GB from $4"
473
474	# Check 1mb chunk crossing the 2gb boundary from 2.5GB file
475	grep -A6 "Test Case 9a " "$1" | grep -q "filesize=100000"
476	pass_fail "TC9: load 1MB chunk crossing 2GB boundary from $4 size"
477	check_md5 "Test Case 9b " "$1" "$2" 6 \
478		"TC9: load 1MB chunk crossing 2GB boundary from $4"
479
480	# Check 2mb chunk from the last 1MB of 2.5GB file loads 1MB
481	grep -A6 "Test Case 10 " "$1" | grep -q "filesize=100000"
482	pass_fail "TC10: load 2MB from the last 1MB of $4 loads 1MB"
483
484	# Check 1mb chunk write
485	grep -A3 "Test Case 11a " "$1" | \
486		egrep -q '1048576 bytes written|update journal'
487	pass_fail "TC11: 1MB write to $5 - write succeeded"
488	check_md5 "Test Case 11b " "$1" "$2" 1 \
489		"TC11: 1MB write to $5 - content verified"
490	echo "** End $1"
491}
492
493# Takes in one parameter which is "fs" or "nonfs", which then dictates
494# if a fs test (size/load/save) or a nonfs test (fatread/extread) needs to
495# be performed.
496function test_fs_nonfs() {
497	echo "Creating files in $fs image if not already present."
498	create_files $IMAGE $MD5_FILE_FS
499
500	OUT_FILE="${OUT}.$1.${fs}.out"
501	test_image $IMAGE $fs $SMALL_FILE $BIG_FILE $1 "" \
502		> ${OUT_FILE} 2>&1
503	check_results $OUT_FILE $MD5_FILE_FS $SMALL_FILE $BIG_FILE \
504		$WRITE_FILE
505	TOTAL_FAIL=$((TOTAL_FAIL + FAIL))
506	TOTAL_PASS=$((TOTAL_PASS + PASS))
507	echo "Summary: PASS: $PASS FAIL: $FAIL"
508	echo "--------------------------------------------"
509}
510
511# ********************
512# * End of functions *
513# ********************
514
515check_clean "$1"
516check_prereq
517compile_sandbox
518prepare_env
519
520# Track TOTAL_FAIL and TOTAL_PASS
521TOTAL_FAIL=0
522TOTAL_PASS=0
523
524# In each loop, for a given file system image, we test both the
525# fs command, like load/size/write, the file system specific command
526# like: ext4load/ext4size/ext4write and the sb load/ls/save commands.
527for fs in ext4 fat; do
528
529	echo "Creating $fs image if not already present."
530	IMAGE=${IMG}.${fs}.img
531	MD5_FILE_FS="${MD5_FILE}.${fs}"
532	create_image $IMAGE $fs
533
534	# sb commands test
535	echo "Creating files in $fs image if not already present."
536	create_files $IMAGE $MD5_FILE_FS
537
538	# Lets mount the image and test sb hostfs commands
539	mkdir -p "$MOUNT_DIR"
540	if [ "$fs" = "fat" ]; then
541		uid="uid=`id -u`"
542	else
543		uid=""
544	fi
545	sudo mount -o loop,rw,$uid "$IMAGE" "$MOUNT_DIR"
546	sudo chmod 777 "$MOUNT_DIR"
547
548	OUT_FILE="${OUT}.sb.${fs}.out"
549	test_image $IMAGE $fs $SMALL_FILE $BIG_FILE sb `pwd`/$MOUNT_DIR \
550		> ${OUT_FILE} 2>&1
551	sudo umount "$MOUNT_DIR"
552	rmdir "$MOUNT_DIR"
553
554	check_results $OUT_FILE $MD5_FILE_FS $SMALL_FILE $BIG_FILE \
555		$WRITE_FILE
556	TOTAL_FAIL=$((TOTAL_FAIL + FAIL))
557	TOTAL_PASS=$((TOTAL_PASS + PASS))
558	echo "Summary: PASS: $PASS FAIL: $FAIL"
559	echo "--------------------------------------------"
560
561	test_fs_nonfs nonfs
562	test_fs_nonfs fs
563done
564
565echo "Total Summary: TOTAL PASS: $TOTAL_PASS TOTAL FAIL: $TOTAL_FAIL"
566echo "--------------------------------------------"
567if [ $TOTAL_FAIL -eq 0 ]; then
568	echo "PASSED"
569	exit 0
570else
571	echo "FAILED"
572	exit 1
573fi
574