14a282742SStephen Warren#!/bin/bash 2*83d290c5STom Rini# SPDX-License-Identifier: GPL-2.0+ 34a282742SStephen Warren 44a282742SStephen Warren# (C) Copyright 2015 Stephen Warren 54a282742SStephen Warren 64a282742SStephen Warren# This script tests U-Boot's FAT filesystem code's ability to read non- 74a282742SStephen Warren# contiguous files. 84a282742SStephen Warren 94a282742SStephen Warren# When porting the ff.c FAT parsing code into U-Boot, it was found that ff.c 104a282742SStephen Warren# always reads files cluster-by-cluster, which results in poor performance. 114a282742SStephen Warren# This was solved by adding a patch to ff.c to coalesce reads of adjacent 124a282742SStephen Warren# clusters. Since this patch needed to correctly handle non-contiguous files, 134a282742SStephen Warren# this test was written to validate that. 144a282742SStephen Warren# 154a282742SStephen Warren# To execute the test, simply run it from the U-Boot source root directory: 164a282742SStephen Warren# 174a282742SStephen Warren# cd u-boot 184a282742SStephen Warren# ./test/fs/fat-noncontig-test.sh 194a282742SStephen Warren# 204a282742SStephen Warren# The test will create a FAT filesystem image, record the CRC of a randomly 214a282742SStephen Warren# generated file in the image, build U-Boot sandbox, invoke U-Boot sandbox to 224a282742SStephen Warren# read the file and validate that the CRCs match. Expected output is shown 234a282742SStephen Warren# below. The important part of the log is the penultimate line that contains 244a282742SStephen Warren# either "PASS" or "FAILURE". 254a282742SStephen Warren# 264a282742SStephen Warren# mkfs.fat 3.0.26 (2014-03-07) 274a282742SStephen Warren# 284a282742SStephen Warren# 294a282742SStephen Warren# U-Boot 2015.10-rc4-00018-g4b22a3e5513f (Oct 03 2015 - 13:49:23 -0600) 304a282742SStephen Warren# 314a282742SStephen Warren# DRAM: 128 MiB 324a282742SStephen Warren# Using default environment 334a282742SStephen Warren# 344a282742SStephen Warren# In: serial 354a282742SStephen Warren# Out: lcd 364a282742SStephen Warren# Err: lcd 374a282742SStephen Warren# Net: No ethernet found. 384a282742SStephen Warren# => host bind 0 sandbox/fat-noncontig.img 394a282742SStephen Warren# => load host 0:0 1000 noncontig.img 404a282742SStephen Warren# 33584964 bytes read in 18 ms (1.7 GiB/s) 414a282742SStephen Warren# => crc32 1000 $filesize 0 424a282742SStephen Warren# crc32 for 00001000 ... 02008743 ==> 6a080523 434a282742SStephen Warren# => if itest.l *0 != 2305086a; then echo FAILURE; else echo PASS; fi 444a282742SStephen Warren# PASS 454a282742SStephen Warren# => reset 464a282742SStephen Warren# 474a282742SStephen Warren# All temporary files used by this script are created in ./sandbox to avoid 484a282742SStephen Warren# polluting the source tree. test/fs/fs-test.sh also uses this directory for 494a282742SStephen Warren# the same purpose. 504a282742SStephen Warren# 514a282742SStephen Warren# TODO: Integrate this (and many other corner-cases e.g. different types of 524a282742SStephen Warren# FAT) with fs-test.sh so that a single script tests everything filesystem- 534a282742SStephen Warren# related. 544a282742SStephen Warren 554a282742SStephen Warrenodir=sandbox 564a282742SStephen Warrenimg=${odir}/fat-noncontig.img 574a282742SStephen Warrenmnt=${odir}/mnt 584a282742SStephen Warrenfill=/dev/urandom 594a282742SStephen Warrentestfn=noncontig.img 604a282742SStephen Warrenmnttestfn=${mnt}/${testfn} 614a282742SStephen Warrencrcaddr=0 624a282742SStephen Warrenloadaddr=1000 634a282742SStephen Warren 644a282742SStephen Warrenfor prereq in fallocate mkfs.fat dd crc32; do 654a282742SStephen Warren if [ ! -x "`which $prereq`" ]; then 664a282742SStephen Warren echo "Missing $prereq binary. Exiting!" 674a282742SStephen Warren exit 1 684a282742SStephen Warren fi 694a282742SStephen Warrendone 704a282742SStephen Warren 714a282742SStephen Warrenmake O=${odir} -s sandbox_defconfig && make O=${odir} -s -j8 724a282742SStephen Warren 734a282742SStephen Warrenmkdir -p ${mnt} 744a282742SStephen Warrenif [ ! -f ${img} ]; then 754a282742SStephen Warren fallocate -l 40M ${img} 7634a60d9bSStephen Warren if [ $? -ne 0 ]; then 7734a60d9bSStephen Warren echo fallocate failed - using dd instead 7834a60d9bSStephen Warren dd if=/dev/zero of=${img} bs=1024 count=$((40 * 1024)) 7934a60d9bSStephen Warren if [ $? -ne 0 ]; then 8034a60d9bSStephen Warren echo Could not create empty disk image 8134a60d9bSStephen Warren exit $? 8234a60d9bSStephen Warren fi 8334a60d9bSStephen Warren fi 844a282742SStephen Warren mkfs.fat ${img} 8534a60d9bSStephen Warren if [ $? -ne 0 ]; then 8634a60d9bSStephen Warren echo Could not create FAT filesystem 8734a60d9bSStephen Warren exit $? 8834a60d9bSStephen Warren fi 894a282742SStephen Warren 904a282742SStephen Warren sudo mount -o loop,uid=$(id -u) ${img} ${mnt} 9134a60d9bSStephen Warren if [ $? -ne 0 ]; then 9234a60d9bSStephen Warren echo Could not mount test filesystem 9334a60d9bSStephen Warren exit $? 9434a60d9bSStephen Warren fi 954a282742SStephen Warren 964a282742SStephen Warren for ((sects=8; sects < 512; sects += 8)); do 974a282742SStephen Warren fn=${mnt}/keep-${sects}.img 984a282742SStephen Warren dd if=${fill} of=${fn} bs=512 count=${sects} >/dev/null 2>&1 994a282742SStephen Warren fn=${mnt}/remove-${sects}.img 1004a282742SStephen Warren dd if=${fill} of=${fn} bs=512 count=${sects} >/dev/null 2>&1 1014a282742SStephen Warren done 1024a282742SStephen Warren 1034a282742SStephen Warren rm -f ${mnt}/remove-*.img 1044a282742SStephen Warren 1054a282742SStephen Warren # 511 deliberately to trigger a file size that's not a multiple of the 1064a282742SStephen Warren # sector size (ignoring sizes that are multiples of both). 1074a282742SStephen Warren dd if=${fill} of=${mnttestfn} bs=511 >/dev/null 2>&1 1084a282742SStephen Warren 1094a282742SStephen Warren sudo umount ${mnt} 11034a60d9bSStephen Warren if [ $? -ne 0 ]; then 11134a60d9bSStephen Warren echo Could not unmount test filesystem 11234a60d9bSStephen Warren exit $? 11334a60d9bSStephen Warren fi 1144a282742SStephen Warrenfi 1154a282742SStephen Warren 1164a282742SStephen Warrensudo mount -o ro,loop,uid=$(id -u) ${img} ${mnt} 11734a60d9bSStephen Warrenif [ $? -ne 0 ]; then 11834a60d9bSStephen Warren echo Could not mount test filesystem 11934a60d9bSStephen Warren exit $? 12034a60d9bSStephen Warrenfi 1214a282742SStephen Warrencrc=0x`crc32 ${mnttestfn}` 1224a282742SStephen Warrensudo umount ${mnt} 12334a60d9bSStephen Warrenif [ $? -ne 0 ]; then 12434a60d9bSStephen Warren echo Could not unmount test filesystem 12534a60d9bSStephen Warren exit $? 12634a60d9bSStephen Warrenfi 1274a282742SStephen Warren 1284a282742SStephen Warrencrc=`printf %02x%02x%02x%02x \ 1294a282742SStephen Warren $((${crc} & 0xff)) \ 1304a282742SStephen Warren $(((${crc} >> 8) & 0xff)) \ 1314a282742SStephen Warren $(((${crc} >> 16) & 0xff)) \ 1324a282742SStephen Warren $((${crc} >> 24))` 1334a282742SStephen Warren 1344a282742SStephen Warren./sandbox/u-boot << EOF 1354a282742SStephen Warrenhost bind 0 ${img} 1364a282742SStephen Warrenload host 0:0 ${loadaddr} ${testfn} 1374a282742SStephen Warrencrc32 ${loadaddr} \$filesize ${crcaddr} 1384a282742SStephen Warrenif itest.l *${crcaddr} != ${crc}; then echo FAILURE; else echo PASS; fi 1394a282742SStephen Warrenreset 1404a282742SStephen WarrenEOF 14134a60d9bSStephen Warrenif [ $? -ne 0 ]; then 14234a60d9bSStephen Warren echo U-Boot exit status indicates an error 14334a60d9bSStephen Warren exit $? 14434a60d9bSStephen Warrenfi 145