Lukasz Majewski | 81b487a | 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" |
| 14 | COLOUR_DEFAULT="\33[0m" |
| 15 | |
| 16 | DIR=./ |
| 17 | SUFFIX=img |
| 18 | RCV_DIR=rcv/ |
| 19 | LOG_FILE=./log/log-`date +%d-%m-%Y_%H-%M-%S` |
| 20 | |
| 21 | cd `dirname $0` |
| 22 | ../dfu/dfu_gadget_test_init.sh 33M 97M |
| 23 | |
| 24 | cleanup () { |
| 25 | rm -rf $RCV_DIR $MNT_DIR |
| 26 | } |
| 27 | |
| 28 | control_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) |
| 38 | trap control_c SIGINT |
| 39 | |
| 40 | die () { |
| 41 | printf " $COLOUR_RED FAILED $COLOUR_DEFAULT \n" |
| 42 | cleanup |
| 43 | exit 1 |
| 44 | } |
| 45 | |
| 46 | calculate_md5sum () { |
| 47 | MD5SUM=`md5sum $1` |
| 48 | MD5SUM=`echo $MD5SUM | cut -d ' ' -f1` |
| 49 | echo "md5sum:"$MD5SUM |
| 50 | } |
| 51 | |
| 52 | ums_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 | |
| 90 | printf "$COLOUR_GREEN========================================================================================= $COLOUR_DEFAULT\n" |
| 91 | echo "U-boot UMS test program" |
| 92 | |
| 93 | if [ $EUID -ne 0 ]; then |
| 94 | echo "You must be root to do this." 1>&2 |
| 95 | exit 100 |
| 96 | fi |
| 97 | |
| 98 | if [ $# -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 |
| 103 | fi |
| 104 | |
| 105 | MNT_DIR="/mnt/tmp-ums-test" |
| 106 | |
| 107 | VID=$1; shift |
| 108 | PID=$1; shift |
| 109 | PART_NUM=$1; shift |
| 110 | |
| 111 | if [ "$1" == "-f" ]; then |
| 112 | shift |
| 113 | FS_TO_FORMAT=$1; shift |
| 114 | fi |
| 115 | |
| 116 | TEST_FILE=$1 |
| 117 | |
| 118 | for 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 |
| 128 | done |
| 129 | |
| 130 | if [ -z "${USB_DEV}" ]; then |
| 131 | echo "Connect target" |
| 132 | echo "e.g. ums 0 mmc 0" |
| 133 | exit 1 |
| 134 | fi |
| 135 | |
| 136 | MEM_DEV=`find $USB_DEV -type d -name "sd[a-z]" | awk -F/ '{print $(NF)}' -` |
| 137 | |
| 138 | mkdir -p $RCV_DIR |
| 139 | if [ ! -d $MNT_DIR ]; then |
| 140 | mkdir -p $MNT_DIR |
| 141 | fi |
| 142 | |
| 143 | if [ "$PART_NUM" == "-" ]; then |
| 144 | PART_NUM="" |
| 145 | fi |
| 146 | MEM_DEV=$MEM_DEV$PART_NUM |
| 147 | |
| 148 | if [ -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 |
| 156 | fi |
| 157 | |
| 158 | printf "Mount: /dev/$MEM_DEV \n" |
| 159 | |
| 160 | if [ -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 |
| 166 | else |
| 167 | for file in $DIR*.$SUFFIX |
| 168 | do |
| 169 | ums_test_file $file |
| 170 | done |
| 171 | fi |
| 172 | |
| 173 | cleanup |
| 174 | |
| 175 | exit 0 |