blob: 56d46163760a43c160629060bf0627c09be28268 [file] [log] [blame]
Lukasz Majewski81b487a2014-08-18 12:12:28 +02001#! /bin/bash
2
3# Copyright (C) 2014 Samsung Electronics
4# Lukasz Majewski <l.majewski@samsung.com>
5#
6# UMS operation test script
7#
8# SPDX-License-Identifier: GPL-2.0+
9
10clear
11
12COLOUR_RED="\33[31m"
13COLOUR_GREEN="\33[32m"
14COLOUR_DEFAULT="\33[0m"
15
16DIR=./
17SUFFIX=img
18RCV_DIR=rcv/
19LOG_FILE=./log/log-`date +%d-%m-%Y_%H-%M-%S`
20
21cd `dirname $0`
22../dfu/dfu_gadget_test_init.sh 33M 97M
23
24cleanup () {
25 rm -rf $RCV_DIR $MNT_DIR
26}
27
28control_c()
29# run if user hits control-c
30{
31 echo -en "\n*** CTRL+C ***\n"
32 umount $MNT_DIR
33 cleanup
34 exit 0
35}
36
37# trap keyboard interrupt (control-c)
38trap control_c SIGINT
39
40die () {
41 printf " $COLOUR_RED FAILED $COLOUR_DEFAULT \n"
42 cleanup
43 exit 1
44}
45
46calculate_md5sum () {
47 MD5SUM=`md5sum $1`
48 MD5SUM=`echo $MD5SUM | cut -d ' ' -f1`
49 echo "md5sum:"$MD5SUM
50}
51
52ums_test_file () {
53 printf "$COLOUR_GREEN========================================================================================= $COLOUR_DEFAULT\n"
54 printf "File:$COLOUR_GREEN %s $COLOUR_DEFAULT\n" $1
55
56 mount /dev/$MEM_DEV $MNT_DIR
57 if [ -f $MNT_DIR/dat_* ]; then
58 rm $MNT_DIR/dat_*
59 fi
60
61 cp ./$1 $MNT_DIR
62 umount $MNT_DIR
63
64
65 echo -n "TX: "
66 calculate_md5sum $1
67
68 MD5_TX=$MD5SUM
69 sleep 1
70 N_FILE=$DIR$RCV_DIR${1:2}"_rcv"
71
72 mount /dev/$MEM_DEV $MNT_DIR
73 cp $MNT_DIR/$1 $N_FILE || die $?
74 rm $MNT_DIR/$1
75 umount $MNT_DIR
76
77 echo -n "RX: "
78 calculate_md5sum $N_FILE
79 MD5_RX=$MD5SUM
80
81 if [ "$MD5_TX" == "$MD5_RX" ]; then
82 printf " $COLOUR_GREEN -------> OK $COLOUR_DEFAULT \n"
83 else
84 printf " $COLOUR_RED -------> FAILED $COLOUR_DEFAULT \n"
85 cleanup
86 exit 1
87 fi
88}
89
90printf "$COLOUR_GREEN========================================================================================= $COLOUR_DEFAULT\n"
91echo "U-boot UMS test program"
92
93if [ $EUID -ne 0 ]; then
94 echo "You must be root to do this." 1>&2
95 exit 100
96fi
97
98if [ $# -lt 3 ]; then
99 echo "Wrong number of arguments"
100 echo "Example:"
101 echo "sudo ./ums_gadget_test.sh VID PID PART_NUM [-f ext4] [test_file]"
102 die
103fi
104
105MNT_DIR="/mnt/tmp-ums-test"
106
107VID=$1; shift
108PID=$1; shift
109PART_NUM=$1; shift
110
111if [ "$1" == "-f" ]; then
112 shift
113 FS_TO_FORMAT=$1; shift
114fi
115
116TEST_FILE=$1
117
118for f in `find /sys -type f -name idProduct`; do
119 d=`dirname ${f}`
120 if [ `cat ${d}/idVendor` != "${VID}" ]; then
121 continue
122 fi
123 if [ `cat ${d}/idProduct` != "${PID}" ]; then
124 continue
125 fi
126 USB_DEV=${d}
127 break
128done
129
130if [ -z "${USB_DEV}" ]; then
131 echo "Connect target"
132 echo "e.g. ums 0 mmc 0"
133 exit 1
134fi
135
136MEM_DEV=`find $USB_DEV -type d -name "sd[a-z]" | awk -F/ '{print $(NF)}' -`
137
138mkdir -p $RCV_DIR
139if [ ! -d $MNT_DIR ]; then
140 mkdir -p $MNT_DIR
141fi
142
143if [ "$PART_NUM" == "-" ]; then
144 PART_NUM=""
145fi
146MEM_DEV=$MEM_DEV$PART_NUM
147
148if [ -n "$FS_TO_FORMAT" ]; then
149 echo -n "Formatting partition /dev/$MEM_DEV to $FS_TO_FORMAT"
150 mkfs -t $FS_TO_FORMAT /dev/$MEM_DEV > /dev/null 2>&1
151 if [ $? -eq 0 ]; then
152 printf " $COLOUR_GREEN DONE $COLOUR_DEFAULT \n"
153 else
154 die
155 fi
156fi
157
158printf "Mount: /dev/$MEM_DEV \n"
159
160if [ -n "$TEST_FILE" ]; then
161 if [ ! -e $TEST_FILE ]; then
162 echo "No file: $TEST_FILE"
163 die
164 fi
165 ums_test_file $TEST_FILE
166else
167 for file in $DIR*.$SUFFIX
168 do
169 ums_test_file $file
170 done
171fi
172
173cleanup
174
175exit 0