fat-noncontig-test.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. # (C) Copyright 2015 Stephen Warren
  3. #
  4. # SPDX-License-Identifier: GPL-2.0+
  5. # This script tests U-Boot's FAT filesystem code's ability to read non-
  6. # contiguous files.
  7. # When porting the ff.c FAT parsing code into U-Boot, it was found that ff.c
  8. # always reads files cluster-by-cluster, which results in poor performance.
  9. # This was solved by adding a patch to ff.c to coalesce reads of adjacent
  10. # clusters. Since this patch needed to correctly handle non-contiguous files,
  11. # this test was written to validate that.
  12. #
  13. # To execute the test, simply run it from the U-Boot source root directory:
  14. #
  15. # cd u-boot
  16. # ./test/fs/fat-noncontig-test.sh
  17. #
  18. # The test will create a FAT filesystem image, record the CRC of a randomly
  19. # generated file in the image, build U-Boot sandbox, invoke U-Boot sandbox to
  20. # read the file and validate that the CRCs match. Expected output is shown
  21. # below. The important part of the log is the penultimate line that contains
  22. # either "PASS" or "FAILURE".
  23. #
  24. # mkfs.fat 3.0.26 (2014-03-07)
  25. #
  26. #
  27. # U-Boot 2015.10-rc4-00018-g4b22a3e5513f (Oct 03 2015 - 13:49:23 -0600)
  28. #
  29. # DRAM: 128 MiB
  30. # Using default environment
  31. #
  32. # In: serial
  33. # Out: lcd
  34. # Err: lcd
  35. # Net: No ethernet found.
  36. # => host bind 0 sandbox/fat-noncontig.img
  37. # => load host 0:0 1000 noncontig.img
  38. # 33584964 bytes read in 18 ms (1.7 GiB/s)
  39. # => crc32 1000 $filesize 0
  40. # crc32 for 00001000 ... 02008743 ==> 6a080523
  41. # => if itest.l *0 != 2305086a; then echo FAILURE; else echo PASS; fi
  42. # PASS
  43. # => reset
  44. #
  45. # All temporary files used by this script are created in ./sandbox to avoid
  46. # polluting the source tree. test/fs/fs-test.sh also uses this directory for
  47. # the same purpose.
  48. #
  49. # TODO: Integrate this (and many other corner-cases e.g. different types of
  50. # FAT) with fs-test.sh so that a single script tests everything filesystem-
  51. # related.
  52. odir=sandbox
  53. img=${odir}/fat-noncontig.img
  54. mnt=${odir}/mnt
  55. fill=/dev/urandom
  56. testfn=noncontig.img
  57. mnttestfn=${mnt}/${testfn}
  58. crcaddr=0
  59. loadaddr=1000
  60. for prereq in fallocate mkfs.fat dd crc32; do
  61. if [ ! -x "`which $prereq`" ]; then
  62. echo "Missing $prereq binary. Exiting!"
  63. exit 1
  64. fi
  65. done
  66. make O=${odir} -s sandbox_defconfig && make O=${odir} -s -j8
  67. mkdir -p ${mnt}
  68. if [ ! -f ${img} ]; then
  69. fallocate -l 40M ${img}
  70. if [ $? -ne 0 ]; then
  71. echo fallocate failed - using dd instead
  72. dd if=/dev/zero of=${img} bs=1024 count=$((40 * 1024))
  73. if [ $? -ne 0 ]; then
  74. echo Could not create empty disk image
  75. exit $?
  76. fi
  77. fi
  78. mkfs.fat ${img}
  79. if [ $? -ne 0 ]; then
  80. echo Could not create FAT filesystem
  81. exit $?
  82. fi
  83. sudo mount -o loop,uid=$(id -u) ${img} ${mnt}
  84. if [ $? -ne 0 ]; then
  85. echo Could not mount test filesystem
  86. exit $?
  87. fi
  88. for ((sects=8; sects < 512; sects += 8)); do
  89. fn=${mnt}/keep-${sects}.img
  90. dd if=${fill} of=${fn} bs=512 count=${sects} >/dev/null 2>&1
  91. fn=${mnt}/remove-${sects}.img
  92. dd if=${fill} of=${fn} bs=512 count=${sects} >/dev/null 2>&1
  93. done
  94. rm -f ${mnt}/remove-*.img
  95. # 511 deliberately to trigger a file size that's not a multiple of the
  96. # sector size (ignoring sizes that are multiples of both).
  97. dd if=${fill} of=${mnttestfn} bs=511 >/dev/null 2>&1
  98. sudo umount ${mnt}
  99. if [ $? -ne 0 ]; then
  100. echo Could not unmount test filesystem
  101. exit $?
  102. fi
  103. fi
  104. sudo mount -o ro,loop,uid=$(id -u) ${img} ${mnt}
  105. if [ $? -ne 0 ]; then
  106. echo Could not mount test filesystem
  107. exit $?
  108. fi
  109. crc=0x`crc32 ${mnttestfn}`
  110. sudo umount ${mnt}
  111. if [ $? -ne 0 ]; then
  112. echo Could not unmount test filesystem
  113. exit $?
  114. fi
  115. crc=`printf %02x%02x%02x%02x \
  116. $((${crc} & 0xff)) \
  117. $(((${crc} >> 8) & 0xff)) \
  118. $(((${crc} >> 16) & 0xff)) \
  119. $((${crc} >> 24))`
  120. ./sandbox/u-boot << EOF
  121. host bind 0 ${img}
  122. load host 0:0 ${loadaddr} ${testfn}
  123. crc32 ${loadaddr} \$filesize ${crcaddr}
  124. if itest.l *${crcaddr} != ${crc}; then echo FAILURE; else echo PASS; fi
  125. reset
  126. EOF
  127. if [ $? -ne 0 ]; then
  128. echo U-Boot exit status indicates an error
  129. exit $?
  130. fi