diff mbox

[U-Boot,v2,3/3] test: ums: Add script for testing UMS gadget operation

Message ID 1408020064-18013-4-git-send-email-l.majewski@samsung.com
State Superseded
Delegated to: Marek Vasut
Headers show

Commit Message

Ɓukasz Majewski Aug. 14, 2014, 12:41 p.m. UTC
This commit adds new test for UMS USB gadget to u-boot mainline tree.
It is similar in operation to the one already available in test/dfu
directory.

Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>

---
Changes for v2:
- Add -f switch for optional FS creation on target UMS partition
- Remove need for passing FS type to script
- Remove syncs
- Use named variables instead of $1, $2, $3 ... etc
- Create temporary mount directory at /mnt/tmp-ums-test/
- Use target device's VID/PID to find exact mount device (/dev/sdX)
- Add support for Ctrl+C tap
- Rewrite README entry
---
 test/ums/README             |  26 +++++++
 test/ums/ums_gadget_test.sh | 169 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+)
 create mode 100644 test/ums/README
 create mode 100755 test/ums/ums_gadget_test.sh

Comments

Stephen Warren Aug. 15, 2014, 5:06 p.m. UTC | #1
On 08/14/2014 06:41 AM, Lukasz Majewski wrote:
> This commit adds new test for UMS USB gadget to u-boot mainline tree.
> It is similar in operation to the one already available in test/dfu
> directory.

This looks good, although I have a few small comments...

> diff --git a/test/ums/README b/test/ums/README

> +Example usage:
> +1. On the target:
> +   create UMS exportable partitions (with e.g. gpt write)

I'd like to avoid that requirement. Perhaps the script can operate on 
raw devices without a partition table too. Can we enhance the script so 
that if the user specifies - as the partition ID, then $PART_NUM gets 
set to '' and hence the whole device is used?

If you do that, I'd like to add the following to the line I quoted above:

, or specify a partition ID of - to use the entire device.

You might want to insert an extra step in the documentation here:

Create a filesystem on the partition or device, or pass the -f option to 
the test script.

> diff --git a/test/ums/ums_gadget_test.sh b/test/ums/ums_gadget_test.sh

> +idVendor=`find /sys -type f -name "idVendor" -exec grep -w $VID {} \;`
> +idProduct=`find /sys -type f -name "idProduct" -exec grep -w $PID {} \;`
> +if [ -z "$idVendor" ] || [ -z "$idProduct" ]; then
> +    echo "Device $VID:$PID not connected!"
> +    exit 0
> +fi

That ensures that the given VID/PID exist somewhere, but not necessarily 
even on the same USB device.

> +USB_DEV=`find /sys -type f -name "idProduct" -exec grep -l $PID {} \;`
 > +USB_DEV=`dirname $USB_DEV`

... and that simply finds a USB device with a matching PID, but ignores 
the VID.

This might work better:

#!/bin/bash

VID=0955
PID=701a

for f in `find /sys -type f -name idProduct`; do
     d=`dirname ${f}`
     if [ `cat ${d}/idVendor` != "${VID}" ]; then
         continue
     fi
     if [ `cat ${d}/idProduct` != "${PID}" ]; then
         continue
     fi
     USB_DEV=${d}
     break
done

if [ -z "${USB_DEV}" ]; then
     echo "Connect target"
     echo "e.g. ums 0 mmc 0"
     exit 1
fi

echo ${USB_DEV}

 > +MEM_DEV=`find $USB_DEV -name "sd[a-z]" | awk -F/ '{print $(NF)}' -`

"-type d" might be a good idea there.

That's probably OK, although it worries me slightly to rely on the 
naming convention of the device. If the code above doesn't work for some 
people, perhaps we could find a file named "dev", and extract the device 
major/minor from it. However, we'd need some complex logic to 
distinguish between the many files named "dev"; in ${USB_DEV}/ itself, 
the various partitions on the device, and the scsi_generic and "bsg" 
(whatever that is) devices. So, it's probably not worth changing that now.
diff mbox

Patch

diff --git a/test/ums/README b/test/ums/README
new file mode 100644
index 0000000..84906be
--- /dev/null
+++ b/test/ums/README
@@ -0,0 +1,26 @@ 
+UMS test script.
+
+ums_gadget_test.sh
+==================
+
+Example usage:
+1. On the target:
+   create UMS exportable partitions (with e.g. gpt write)
+   ums 0 mmc 0
+2. On the host:
+   sudo test/ums/ums_gadget_test.sh VID PID PART_NUM [-f FILE_SYSTEM] [test_file]
+   e.g. sudo test/ums/ums_gadget_test.sh 04e8 6601 6 -f vfat ./dat_14M.img
+
+... where:
+    VID - UMS device USB Vendor ID
+    PID - UMS device USB Product ID
+    PART_NUM - is the partition number on which UMS operates
+
+Information about available partitions on the target one can read with using
+the 'mmc part' or 'part list' commands.
+
+The [-f FILE_SYSTEM] optional switch allows for formatting target partition to
+FILE_SYSTEM.
+
+The last, optional [test_file] parameter is for specifying the exact test file
+to use.
diff --git a/test/ums/ums_gadget_test.sh b/test/ums/ums_gadget_test.sh
new file mode 100755
index 0000000..2cfa208
--- /dev/null
+++ b/test/ums/ums_gadget_test.sh
@@ -0,0 +1,169 @@ 
+#! /bin/bash
+
+# Copyright (C) 2014 Samsung Electronics
+# Lukasz Majewski <l.majewski@samsung.com>
+#
+# UMS operation test script
+#
+# SPDX-License-Identifier:	GPL-2.0+
+
+clear
+
+COLOUR_RED="\33[31m"
+COLOUR_GREEN="\33[32m"
+COLOUR_DEFAULT="\33[0m"
+
+DIR=./
+SUFFIX=img
+RCV_DIR=rcv/
+LOG_FILE=./log/log-`date +%d-%m-%Y_%H-%M-%S`
+
+cd `dirname $0`
+../dfu/dfu_gadget_test_init.sh 33M 97M
+
+cleanup () {
+    rm -rf $RCV_DIR $MNT_DIR
+}
+
+control_c()
+# run if user hits control-c
+{
+	echo -en "\n*** CTRL+C ***\n"
+	umount $MNT_DIR
+	cleanup
+	exit 0
+}
+
+# trap keyboard interrupt (control-c)
+trap control_c SIGINT
+
+die () {
+    printf "   $COLOUR_RED FAILED $COLOUR_DEFAULT \n"
+    cleanup
+    exit 1
+}
+
+calculate_md5sum () {
+    MD5SUM=`md5sum $1`
+    MD5SUM=`echo $MD5SUM | cut -d ' ' -f1`
+    echo "md5sum:"$MD5SUM
+}
+
+ums_test_file () {
+    printf "$COLOUR_GREEN========================================================================================= $COLOUR_DEFAULT\n"
+    printf "File:$COLOUR_GREEN %s $COLOUR_DEFAULT\n" $1
+
+    mount /dev/$MEM_DEV $MNT_DIR
+    if [ -f $MNT_DIR/dat_* ]; then
+	rm $MNT_DIR/dat_*
+    fi
+
+    cp ./$1 $MNT_DIR
+    umount $MNT_DIR
+
+
+    echo -n "TX: "
+    calculate_md5sum $1
+
+    MD5_TX=$MD5SUM
+    sleep 1
+    N_FILE=$DIR$RCV_DIR${1:2}"_rcv"
+
+    mount /dev/$MEM_DEV $MNT_DIR
+    cp $MNT_DIR/$1 $N_FILE || die $?
+    rm $MNT_DIR/$1
+    umount $MNT_DIR
+
+    echo -n "RX: "
+    calculate_md5sum $N_FILE
+    MD5_RX=$MD5SUM
+
+    if [ "$MD5_TX" == "$MD5_RX" ]; then
+	printf "   $COLOUR_GREEN -------> OK $COLOUR_DEFAULT \n"
+    else
+	printf "   $COLOUR_RED -------> FAILED $COLOUR_DEFAULT \n"
+	cleanup
+	exit 1
+    fi
+}
+
+printf "$COLOUR_GREEN========================================================================================= $COLOUR_DEFAULT\n"
+echo "U-boot UMS test program"
+
+if [ $EUID -ne 0 ]; then
+   echo "You must be root to do this." 1>&2
+   exit 100
+fi
+
+if [ $# -lt 3 ]; then
+    echo "Wrong number of arguments"
+    echo "Example:"
+    echo "sudo ./ums_gadget_test.sh VID PID PART_NUM [-f ext4] [test_file]"
+    die
+fi
+
+MNT_DIR="/mnt/tmp-ums-test"
+
+VID=$1; shift
+PID=$1; shift
+PART_NUM=$1; shift
+
+if [ "$1" == "-f" ]; then
+    shift
+    FS_TO_FORMAT=$1; shift
+fi
+
+TEST_FILE=$1
+
+idVendor=`find /sys -type f -name "idVendor" -exec grep -w $VID {} \;`
+idProduct=`find /sys -type f -name "idProduct" -exec grep -w $PID {} \;`
+if [ -z "$idVendor" ] || [ -z "$idProduct" ]; then
+    echo "Device $VID:$PID not connected!"
+    exit 0
+fi
+
+USB_DEV=`find /sys -type f -name "idProduct" -exec grep -l $PID {} \;`
+USB_DEV=`dirname $USB_DEV`
+
+MEM_DEV=`find $USB_DEV -name "sd[a-z]" | awk -F/ '{print $(NF)}' -`
+if [ -z $MEM_DEV ]; then
+    echo "Connect target"
+    echo "e.g. ums 0 mmc 0"
+    die
+fi
+
+mkdir -p $RCV_DIR
+if [ ! -d $MNT_DIR ]; then
+    mkdir -p $MNT_DIR
+fi
+
+MEM_DEV=$MEM_DEV$PART_NUM
+
+if [ -n "$FS_TO_FORMAT" ]; then
+    echo -n "Formatting partition /dev/$MEM_DEV to $FS_TO_FORMAT"
+    mkfs -t $FS_TO_FORMAT /dev/$MEM_DEV > /dev/null 2>&1
+    if [ $? -eq 0 ]; then
+	printf " $COLOUR_GREEN DONE $COLOUR_DEFAULT \n"
+    else
+	die
+    fi
+fi
+
+printf "Mount: /dev/$MEM_DEV \n"
+
+if [ -n "$TEST_FILE" ]; then
+    if [ ! -e $TEST_FILE ]; then
+	echo "No file: $TEST_FILE"
+	die
+    fi
+    ums_test_file $TEST_FILE
+else
+    for file in $DIR*.$SUFFIX
+    do
+	ums_test_file $file
+    done
+fi
+
+cleanup
+
+exit 0