summaryrefslogtreecommitdiff
path: root/test/ums/ums_gadget_test.sh
blob: 9da486b266ce079cd0b8621c66a8baf4f6b20b7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#! /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_ORANGE="\33[33m"
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

    while true; do
	umount $MNT_DIR > /dev/null 2>&1
	if [ $? -eq 0 ]; then
	    break
	fi
	printf "$COLOUR_ORANGE\tSleeping to wait for umount...$COLOUR_DEFAULT\n"
	sleep 1
    done

    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

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

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

mkdir -p $RCV_DIR
if [ ! -d $MNT_DIR ]; then
    mkdir -p $MNT_DIR
fi

if [ "$PART_NUM" == "-" ]; then
    PART_NUM=""
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