Lukasz Majewski | 801123f | 2014-08-18 12:12:28 +0200 | [diff] [blame] | 1 | #! /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 | |
| 10 | clear |
| 11 | |
| 12 | COLOUR_RED="\33[31m" |
| 13 | COLOUR_GREEN="\33[32m" |
Lukasz Majewski | fbd3b79 | 2014-11-07 14:05:55 +0100 | [diff] [blame^] | 14 | COLOUR_ORANGE="\33[33m" |
Lukasz Majewski | 801123f | 2014-08-18 12:12:28 +0200 | [diff] [blame] | 15 | COLOUR_DEFAULT="\33[0m" |
| 16 | |
| 17 | DIR=./ |
| 18 | SUFFIX=img |
| 19 | RCV_DIR=rcv/ |
| 20 | LOG_FILE=./log/log-`date +%d-%m-%Y_%H-%M-%S` |
| 21 | |
| 22 | cd `dirname $0` |
| 23 | ../dfu/dfu_gadget_test_init.sh 33M 97M |
| 24 | |
| 25 | cleanup () { |
| 26 | rm -rf $RCV_DIR $MNT_DIR |
| 27 | } |
| 28 | |
| 29 | control_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) |
| 39 | trap control_c SIGINT |
| 40 | |
| 41 | die () { |
| 42 | printf " $COLOUR_RED FAILED $COLOUR_DEFAULT \n" |
| 43 | cleanup |
| 44 | exit 1 |
| 45 | } |
| 46 | |
| 47 | calculate_md5sum () { |
| 48 | MD5SUM=`md5sum $1` |
| 49 | MD5SUM=`echo $MD5SUM | cut -d ' ' -f1` |
| 50 | echo "md5sum:"$MD5SUM |
| 51 | } |
| 52 | |
| 53 | ums_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 Majewski | 801123f | 2014-08-18 12:12:28 +0200 | [diff] [blame] | 63 | |
Lukasz Majewski | fbd3b79 | 2014-11-07 14:05:55 +0100 | [diff] [blame^] | 64 | 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 Majewski | 801123f | 2014-08-18 12:12:28 +0200 | [diff] [blame] | 72 | |
| 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 | |
| 98 | printf "$COLOUR_GREEN========================================================================================= $COLOUR_DEFAULT\n" |
| 99 | echo "U-boot UMS test program" |
| 100 | |
| 101 | if [ $EUID -ne 0 ]; then |
| 102 | echo "You must be root to do this." 1>&2 |
| 103 | exit 100 |
| 104 | fi |
| 105 | |
| 106 | if [ $# -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 |
| 111 | fi |
| 112 | |
| 113 | MNT_DIR="/mnt/tmp-ums-test" |
| 114 | |
| 115 | VID=$1; shift |
| 116 | PID=$1; shift |
| 117 | PART_NUM=$1; shift |
| 118 | |
| 119 | if [ "$1" == "-f" ]; then |
| 120 | shift |
| 121 | FS_TO_FORMAT=$1; shift |
| 122 | fi |
| 123 | |
| 124 | TEST_FILE=$1 |
| 125 | |
| 126 | for 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 |
| 136 | done |
| 137 | |
| 138 | if [ -z "${USB_DEV}" ]; then |
| 139 | echo "Connect target" |
| 140 | echo "e.g. ums 0 mmc 0" |
| 141 | exit 1 |
| 142 | fi |
| 143 | |
| 144 | MEM_DEV=`find $USB_DEV -type d -name "sd[a-z]" | awk -F/ '{print $(NF)}' -` |
| 145 | |
| 146 | mkdir -p $RCV_DIR |
| 147 | if [ ! -d $MNT_DIR ]; then |
| 148 | mkdir -p $MNT_DIR |
| 149 | fi |
| 150 | |
| 151 | if [ "$PART_NUM" == "-" ]; then |
| 152 | PART_NUM="" |
| 153 | fi |
| 154 | MEM_DEV=$MEM_DEV$PART_NUM |
| 155 | |
| 156 | if [ -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 |
| 164 | fi |
| 165 | |
| 166 | printf "Mount: /dev/$MEM_DEV \n" |
| 167 | |
| 168 | if [ -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 |
| 174 | else |
| 175 | for file in $DIR*.$SUFFIX |
| 176 | do |
| 177 | ums_test_file $file |
| 178 | done |
| 179 | fi |
| 180 | |
| 181 | cleanup |
| 182 | |
| 183 | exit 0 |