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