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