2 # SPDX-License-Identifier: GPL-2.0+
4 # (C) Copyright 2014 Suriyan Ramasami
6 # Invoke this test script from U-Boot base directory as ./test/fs/fs-test.sh
7 # It currently tests the fs/sb and native commands for ext4 and fat partitions
8 # Expected results are as follows:
10 # fs-test.sb.ext4.out: Summary: PASS: 24 FAIL: 0
11 # fs-test.ext4.out: Summary: PASS: 24 FAIL: 0
12 # fs-test.fs.ext4.out: Summary: PASS: 24 FAIL: 0
14 # fs-test.sb.fat16.out: Summary: PASS: 24 FAIL: 0
15 # fs-test.fat16.out: Summary: PASS: 21 FAIL: 3
16 # fs-test.fs.fat16.out: Summary: PASS: 21 FAIL: 3
18 # fs-test.sb.fat32.out: Summary: PASS: 24 FAIL: 0
19 # fs-test.fat32.out: Summary: PASS: 21 FAIL: 3
20 # fs-test.fs.fat32.out: Summary: PASS: 21 FAIL: 3
21 # Total Summary: TOTAL PASS: 204 TOTAL FAIL: 12
23 # pre-requisite binaries list.
24 PREREQ_BINS="md5sum mkfs mount umount dd fallocate mkdir"
26 # All generated output files from this test will be in $OUT_DIR
27 # Hence everything is sandboxed.
28 OUT_DIR="sandbox/test/fs"
30 # Location of generated sandbox u-boot
31 UBOOT="./sandbox/u-boot"
33 # Our mount directory will be in the sandbox
34 MOUNT_DIR="${OUT_DIR}/mnt"
36 # The file system image we create will have the $IMG prefix.
39 # $SMALL_FILE is the name of the 1MB file in the file system image
42 # $BIG_FILE is the name of the 2.5GB file in the file system image
45 # $MD5_FILE will have the expected md5s when we do the test
46 # They shall have a suffix which represents their file system (ext4/fat16/...)
47 MD5_FILE="${OUT_DIR}/md5s.list"
49 # $OUT shall be the prefix of the test output. Their suffix will be .out
50 OUT="${OUT_DIR}/fs-test"
52 # Full Path of the 1 MB file that shall be created in the fs image.
53 MB1="${MOUNT_DIR}/${SMALL_FILE}"
54 GB2p5="${MOUNT_DIR}/${BIG_FILE}"
56 # ************************
57 # * Functions start here *
58 # ************************
60 # Check if the prereq binaries exist, or exit
61 function check_prereq() {
62 for prereq in $PREREQ_BINS; do
63 if [ ! -x "`which $prereq`" ]; then
64 echo "Missing $prereq binary. Exiting!"
69 # We use /dev/urandom to create files. Check if it exists.
70 if [ ! -c /dev/urandom ]; then
71 echo "Missing character special /dev/urandom. Exiting!"
76 # If 1st param is "clean", then clean out the generated files and exit
77 function check_clean() {
78 if [ "$1" = "clean" ]; then
80 echo "Cleaned up generated files. Exiting"
85 # Generate sandbox U-Boot - gleaned from /test/dm/test-dm.sh
86 function compile_sandbox() {
88 NUM_CPUS=$(cat /proc/cpuinfo |grep -c processor)
89 make O=sandbox sandbox_config
90 make O=sandbox -s -j${NUM_CPUS}
92 # Check if U-Boot exists
93 if [ ! -x "$UBOOT" ]; then
94 echo "$UBOOT does not exist or is not executable"
96 echo "Please run this script as ./test/fs/`basename $0`"
101 # Clean out all generated files other than the file system images
102 # We save time by not deleting and recreating the file system images
103 function prepare_env() {
104 rm -f ${MD5_FILE}.* ${OUT}.*
108 # 1st parameter is the name of the image file to be created
109 # 2nd parameter is the filesystem - fat16 ext4 etc
110 # -F cant be used with fat as it means something else.
111 function create_image() {
112 # Create image if not already present - saves time, while debugging
128 if [ ! -f "$1" ]; then
129 fallocate -l 3G "$1" &> /dev/null
130 if [ $? -ne 0 ]; then
131 echo fallocate failed - using dd instead
132 dd if=/dev/zero of=$1 bs=1024 count=$((3 * 1024 * 1024))
133 if [ $? -ne 0 ]; then
134 echo Could not create empty disk image
138 mkfs -t "$FS_TYPE" $MKFS_OPTION "$1" &> /dev/null
139 if [ $? -ne 0 -a "$FS_TYPE" = "fat" ]; then
140 # If we fail and we did fat, try vfat.
141 mkfs -t vfat $MKFS_OPTION "$1" &> /dev/null
143 if [ $? -ne 0 ]; then
144 echo Could not create filesystem
150 # 1st parameter is image file
151 # 2nd parameter is file system type - fat16/ext4/...
152 # 3rd parameter is name of small file
153 # 4th parameter is name of big file
154 # 5th parameter is fs/nonfs/sb - to dictate generic fs commands or
155 # otherwise or sb hostfs
156 # 6th parameter is the directory path for the files. Its "" for generic
157 # fs and ext4/fat and full patch for sb hostfs
158 # UBOOT is set in env
159 function test_image() {
171 # ext4 needs absolute path
178 echo "Unhandled filesystem $2. Exiting!"
201 echo "Unhandled mode $5. Exiting!"
207 # sb always uses full path to mointpoint, irrespective of filesystem
208 if [ "$5" = "sb" ]; then
216 # In u-boot commands, <interface> stands for host or hostfs
217 # hostfs maps to the host fs.
218 # host maps to the "sb bind" that we do
222 setenv bind 'if test "\$sb" != sb; then sb bind 0 "$1"; fi'
225 ${PREFIX}ls host${SUFFIX} $6
226 # In addition, test with a nonexistent directory to see if we crash.
227 ${PREFIX}ls host${SUFFIX} invalid_d
229 # We want ${PREFIX}size host 0:0 $3 for host commands and
230 # sb size hostfs - $3 for hostfs commands.
232 # Test Case 2a - size of small file
233 ${PREFIX}size host${SUFFIX} ${FPATH}$FILE_SMALL
236 # Test Case 2b - size of small file via a path using '..'
237 ${PREFIX}size host${SUFFIX} ${FPATH}SUBDIR/../$FILE_SMALL
241 # 2.5GB (1024*1024*2500) is 0x9C40 0000
242 # Test Case 3 - size of big file
243 ${PREFIX}size host${SUFFIX} ${FPATH}$FILE_BIG
247 # Notes about load operation
248 # If I use 0x01000000 I get DMA misaligned error message
249 # Last two parameters are size and offset.
251 # Test Case 4a - Read full 1MB of small file
252 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_SMALL
254 # Test Case 4b - Read full 1MB of small file
255 md5sum $addr \$filesize
258 # Test Case 5a - First 1MB of big file
259 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x0
261 # Test Case 5b - First 1MB of big file
262 md5sum $addr \$filesize
265 # fails for ext as no offset support
266 # Test Case 6a - Last 1MB of big file
267 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x9C300000
269 # Test Case 6b - Last 1MB of big file
270 md5sum $addr \$filesize
273 # fails for ext as no offset support
274 # Test Case 7a - One from the last 1MB chunk of 2GB
275 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x7FF00000
277 # Test Case 7b - One from the last 1MB chunk of 2GB
278 md5sum $addr \$filesize
281 # fails for ext as no offset support
282 # Test Case 8a - One from the start 1MB chunk from 2GB
283 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x80000000
285 # Test Case 8b - One from the start 1MB chunk from 2GB
286 md5sum $addr \$filesize
289 # fails for ext as no offset support
290 # Test Case 9a - One 1MB chunk crossing the 2GB boundary
291 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG $length 0x7FF80000
293 # Test Case 9b - One 1MB chunk crossing the 2GB boundary
294 md5sum $addr \$filesize
297 # Generic failure case
298 # Test Case 10 - 2MB chunk from the last 1MB of big file
299 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_BIG 0x00200000 0x9C300000
303 # Read 1MB from small file
304 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_SMALL
305 # Write it back to test the writes
306 # Test Case 11a - Check that the write succeeded
307 ${PREFIX}${WRITE} host${SUFFIX} $addr ${FPATH}$FILE_WRITE \$filesize
309 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_WRITE
310 # Test Case 11b - Check md5 of written to is same as the one read from
311 md5sum $addr \$filesize
315 # Next test case checks writing a file whose dirent
316 # is the first in the block, which is always true for "."
317 # The write should fail, but the lookup should work
318 # Test Case 12 - Check directory traversal
319 ${PREFIX}${WRITE} host${SUFFIX} $addr ${FPATH}. 0x10
321 # Read 1MB from small file
322 ${PREFIX}load host${SUFFIX} $addr ${FPATH}$FILE_SMALL
323 # Write it via "same directory", i.e. "." dirent
324 # Test Case 13a - Check directory traversal
325 ${PREFIX}${WRITE} host${SUFFIX} $addr ${FPATH}./${FILE_WRITE}2 \$filesize
327 ${PREFIX}load host${SUFFIX} $addr ${FPATH}./${FILE_WRITE}2
328 # Test Case 13b - Check md5 of written to is same as the one read from
329 md5sum $addr \$filesize
332 ${PREFIX}load host${SUFFIX} $addr ${FPATH}${FILE_WRITE}2
333 # Test Case 13c - Check md5 of written to is same as the one read from
334 md5sum $addr \$filesize
342 # 1st argument is the name of the image file.
343 # 2nd argument is the file where we generate the md5s of the files
344 # generated with the appropriate start and length that we use to test.
345 # It creates the necessary files in the image to test.
346 # $GB2p5 is the path of the big file (2.5 GB)
347 # $MB1 is the path of the small file (1 MB)
348 # $MOUNT_DIR is the path we can use to mount the image file.
349 function create_files() {
350 # Mount the image so we can populate it.
351 mkdir -p "$MOUNT_DIR"
352 sudo mount -o loop,rw "$1" "$MOUNT_DIR"
354 # Create a subdirectory.
355 sudo mkdir -p "$MOUNT_DIR/SUBDIR"
357 # Create big file in this image.
358 # Note that we work only on the start 1MB, couple MBs in the 2GB range
359 # and the last 1 MB of the huge 2.5GB file.
360 # So, just put random values only in those areas.
361 if [ ! -f "${GB2p5}" ]; then
362 sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 \
364 sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=2 seek=2047 \
366 sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 seek=2499 \
370 # Create a small file in this image.
371 if [ ! -f "${MB1}" ]; then
372 sudo dd if=/dev/urandom of="${MB1}" bs=1M count=1 \
376 # Delete the small file copies which possibly are written as part of a
378 sudo rm -f "${MB1}.w"
379 sudo rm -f "${MB1}.w2"
381 # Generate the md5sums of reads that we will test against small file
382 dd if="${MB1}" bs=1M skip=0 count=1 2> /dev/null | md5sum > "$2"
384 # Generate the md5sums of reads that we will test against big file
385 # One from beginning of file.
386 dd if="${GB2p5}" bs=1M skip=0 count=1 \
387 2> /dev/null | md5sum >> "$2"
389 # One from end of file.
390 dd if="${GB2p5}" bs=1M skip=2499 count=1 \
391 2> /dev/null | md5sum >> "$2"
393 # One from the last 1MB chunk of 2GB
394 dd if="${GB2p5}" bs=1M skip=2047 count=1 \
395 2> /dev/null | md5sum >> "$2"
397 # One from the start 1MB chunk from 2GB
398 dd if="${GB2p5}" bs=1M skip=2048 count=1 \
399 2> /dev/null | md5sum >> "$2"
401 # One 1MB chunk crossing the 2GB boundary
402 dd if="${GB2p5}" bs=512K skip=4095 count=2 \
403 2> /dev/null | md5sum >> "$2"
406 sudo umount "$MOUNT_DIR"
410 # 1st parameter is the text to print
411 # if $? is 0 its a pass, else a fail
412 # As a side effect it shall update env variable PASS and FAIL
413 function pass_fail() {
414 if [ $? -eq 0 ]; then
423 # 1st parameter is the string which leads to an md5 generation
424 # 2nd parameter is the file we grep, for that string
425 # 3rd parameter is the name of the file which has md5s in it
426 # 4th parameter is the line # in the md5 file that we match it against
427 # This function checks if the md5 of the file in the sandbox matches
428 # that calculated while generating the file
429 # 5th parameter is the string to print with the result
431 # md5sum in u-boot has output of form:
432 # md5 for 01000008 ... 01100007 ==> <md5>
433 # the 7th field is the actual md5
434 md5_src=`grep -A2 "$1" "$2" | grep "md5 for" | tr -d '\r'`
436 md5_src=${md5_src[6]}
438 # The md5 list, each line is of the form:
440 # the 2nd field is the actual md5
441 md5_dst=`sed -n $4p $3`
443 md5_dst=${md5_dst[0]}
445 # For a pass they should match.
446 [ "$md5_src" = "$md5_dst" ]
450 # 1st parameter is the name of the output file to check
451 # 2nd parameter is the name of the file containing the md5 expected
452 # 3rd parameter is the name of the small file
453 # 4th parameter is the name of the big file
454 # 5th paramter is the name of the written file
455 # This function checks the output file for correct results.
456 function check_results() {
462 # Check if the ls is showing correct results for 2.5 gb file
463 grep -A7 "Test Case 1 " "$1" | egrep -iq "2621440000 *$4"
464 pass_fail "TC1: ls of $4"
466 # Check if the ls is showing correct results for 1 mb file
467 grep -A7 "Test Case 1 " "$1" | egrep -iq "1048576 *$3"
468 pass_fail "TC1: ls of $3"
470 # Check size command on 1MB.file
471 egrep -A3 "Test Case 2a " "$1" | grep -q "filesize=100000"
472 pass_fail "TC2: size of $3"
473 # Check size command on 1MB.file via a path using '..'
474 egrep -A3 "Test Case 2b " "$1" | grep -q "filesize=100000"
475 pass_fail "TC2: size of $3 via a path using '..'"
477 # Check size command on 2.5GB.file
478 egrep -A3 "Test Case 3 " "$1" | grep -q "filesize=9c400000"
479 pass_fail "TC3: size of $4"
481 # Check read full mb of 1MB.file
482 grep -A4 "Test Case 4a " "$1" | grep -q "filesize=100000"
483 pass_fail "TC4: load of $3 size"
484 check_md5 "Test Case 4b " "$1" "$2" 1 "TC4: load from $3"
486 # Check first mb of 2.5GB.file
487 grep -A4 "Test Case 5a " "$1" | grep -q "filesize=100000"
488 pass_fail "TC5: load of 1st MB from $4 size"
489 check_md5 "Test Case 5b " "$1" "$2" 2 "TC5: load of 1st MB from $4"
491 # Check last mb of 2.5GB.file
492 grep -A4 "Test Case 6a " "$1" | grep -q "filesize=100000"
493 pass_fail "TC6: load of last MB from $4 size"
494 check_md5 "Test Case 6b " "$1" "$2" 3 "TC6: load of last MB from $4"
496 # Check last 1mb chunk of 2gb from 2.5GB file
497 grep -A4 "Test Case 7a " "$1" | grep -q "filesize=100000"
498 pass_fail "TC7: load of last 1mb chunk of 2GB from $4 size"
499 check_md5 "Test Case 7b " "$1" "$2" 4 \
500 "TC7: load of last 1mb chunk of 2GB from $4"
502 # Check first 1mb chunk after 2gb from 2.5GB file
503 grep -A4 "Test Case 8a " "$1" | grep -q "filesize=100000"
504 pass_fail "TC8: load 1st MB chunk after 2GB from $4 size"
505 check_md5 "Test Case 8b " "$1" "$2" 5 \
506 "TC8: load 1st MB chunk after 2GB from $4"
508 # Check 1mb chunk crossing the 2gb boundary from 2.5GB file
509 grep -A4 "Test Case 9a " "$1" | grep -q "filesize=100000"
510 pass_fail "TC9: load 1MB chunk crossing 2GB boundary from $4 size"
511 check_md5 "Test Case 9b " "$1" "$2" 6 \
512 "TC9: load 1MB chunk crossing 2GB boundary from $4"
514 # Check 2mb chunk from the last 1MB of 2.5GB file loads 1MB
515 grep -A5 "Test Case 10 " "$1" | grep -q "filesize=100000"
516 pass_fail "TC10: load 2MB from the last 1MB of $4 loads 1MB"
518 # Check 1mb chunk write
519 grep -A2 "Test Case 11a " "$1" | grep -q '1048576 bytes written'
520 pass_fail "TC11: 1MB write to $3.w - write succeeded"
521 check_md5 "Test Case 11b " "$1" "$2" 1 \
522 "TC11: 1MB write to $3.w - content verified"
524 # Check lookup of 'dot' directory
525 grep -A4 "Test Case 12 " "$1" | grep -q 'Unable to write file'
526 pass_fail "TC12: 1MB write to . - write denied"
528 # Check directory traversal
529 grep -A2 "Test Case 13a " "$1" | grep -q '1048576 bytes written'
530 pass_fail "TC13: 1MB write to ./$3.w2 - write succeeded"
531 check_md5 "Test Case 13b " "$1" "$2" 1 \
532 "TC13: 1MB read from ./$3.w2 - content verified"
533 check_md5 "Test Case 13c " "$1" "$2" 1 \
534 "TC13: 1MB read from $3.w2 - content verified"
539 # Takes in one parameter which is "fs" or "nonfs", which then dictates
540 # if a fs test (size/load/save) or a nonfs test (fatread/extread) needs to
542 function test_fs_nonfs() {
543 echo "Creating files in $fs image if not already present."
544 create_files $IMAGE $MD5_FILE_FS
546 OUT_FILE="${OUT}.$1.${fs}.out"
547 test_image $IMAGE $fs $SMALL_FILE $BIG_FILE $1 "" \
549 # strip out noise from fs code
550 grep -v -e "File System is consistent\|update journal finished" \
551 -e "reading .*\.file\|writing .*\.file.w" \
552 < ${OUT_FILE} > ${OUT_FILE}_clean
553 check_results ${OUT_FILE}_clean $MD5_FILE_FS $SMALL_FILE \
555 TOTAL_FAIL=$((TOTAL_FAIL + FAIL))
556 TOTAL_PASS=$((TOTAL_PASS + PASS))
557 echo "Summary: PASS: $PASS FAIL: $FAIL"
558 echo "--------------------------------------------"
561 # ********************
562 # * End of functions *
563 # ********************
570 # Track TOTAL_FAIL and TOTAL_PASS
574 # In each loop, for a given file system image, we test both the
575 # fs command, like load/size/write, the file system specific command
576 # like: ext4load/ext4size/ext4write and the sb load/ls/save commands.
577 for fs in ext4 fat16 fat32; do
579 echo "Creating $fs image if not already present."
580 IMAGE=${IMG}.${fs}.img
581 MD5_FILE_FS="${MD5_FILE}.${fs}"
582 create_image $IMAGE $fs
585 echo "Creating files in $fs image if not already present."
586 create_files $IMAGE $MD5_FILE_FS
588 # Lets mount the image and test sb hostfs commands
589 mkdir -p "$MOUNT_DIR"
598 sudo mount -o loop,rw,$uid "$IMAGE" "$MOUNT_DIR"
599 sudo chmod 777 "$MOUNT_DIR"
601 OUT_FILE="${OUT}.sb.${fs}.out"
602 test_image $IMAGE $fs $SMALL_FILE $BIG_FILE sb `pwd`/$MOUNT_DIR \
604 sudo umount "$MOUNT_DIR"
607 check_results $OUT_FILE $MD5_FILE_FS $SMALL_FILE $BIG_FILE
608 TOTAL_FAIL=$((TOTAL_FAIL + FAIL))
609 TOTAL_PASS=$((TOTAL_PASS + PASS))
610 echo "Summary: PASS: $PASS FAIL: $FAIL"
611 echo "--------------------------------------------"
617 echo "Total Summary: TOTAL PASS: $TOTAL_PASS TOTAL FAIL: $TOTAL_FAIL"
618 echo "--------------------------------------------"
619 if [ $TOTAL_FAIL -eq 0 ]; then