flash-wrs 8.44 KB
Newer Older
1
#!/bin/sh
2 3
# A script to ease the use of the usb loader, possibly changing the mac address
#
4
# Authors:
5
#	- Benoit Rat (Seven Solutions, www.sevensols.com)
6
#	- Federico Vaga
7 8 9 10 11 12 13 14 15
# 	- Alessandro Rubini
#
# GNU Lesser General Public License Usage
# This file may be used under the terms of the GNU Lesser
# General Public License version 2.1 as published by the Free Software
# Foundation and appearing in the file LICENSE.LGPL included in the
# packaging of this file.  Please review the following information to
# ensure the GNU Lesser General Public License version 2.1 requirements
# will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16
#########################################################################
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#hack: allow some environment variables to change behavior, for developing
if [ "x$WRS_FAKE_FLASHER" = "x" ]; then
    fake=false
else
    fake=true
    echo "Using fake flasher (no action on switch is done)"
    sleep 1
fi

if [ "x$WRS_KEEP_TEMPORARIES" = "x" ]; then
    remove_temps=true
else
    remove_temps=false
fi

fake_flasher()
{
    echo "NOT FLASHING with command:"
    echo "   $*"
    echo ""
    sleep 3
}
40 41 42

showhelp()
{
43 44
	printf "Usage: $0 [options] [<firmware>.tar] [DEV]\n\n"
	printf "<firmware>.tar: Use the file in the firmware to flash the device\n"
45 46
	printf "DEV:\t The usb device (by default it is /dev/ttyACM0)\n"
	printf "Options: \n"
47
	printf "  -h|--help\t Show this help message\n"
48
	printf "  -m|--mode\t can be: default (df and nf), df (dataflash),\n\t\t nf (nandflash), ddr (ddr memories).\n"
49
	printf "  -g|--gateware\t Select the gateware: 18p (18 ports, default), 8p (8 ports)\n"
50
	printf "  -e \t\t Completely erase the memory (Can erase your configuration)\n"
51
	printf "  -b|--build\t Use files that you have built in the WRS_OUTPUT_DIR\n"
52 53 54 55 56 57
	exit 0
}


checkExit()
{
58 59 60 61
	# $1 == what, $2 == filename
	# (assumes $me is the script name)
	if [ -f $2 ]; then
		return 0;
62
	else
63 64
		echo "$me: Can't find file \"$2\" ($1)" >& 2;
		exit 1
65 66 67
	fi
}

68
# Go to the top directory
69 70
dir=$(dirname $0)/..
WRS_BASE_DIR=$(cd $dir && /bin/pwd)
71 72

# Sanity checks
73
if [ -d ${WRS_BASE_DIR}/usb-loader ]; then true; else
74
    echo "$0: ${WRS_BASE_DIR}/usb-loader: no such directory" >& 2
75 76 77
    exit 1
fi

78 79 80 81
if [ "x${WRS_OUTPUT_DIR}" = "x" ]; then
    export WRS_OUTPUT_DIR=$(/bin/pwd)
fi

82
# build flasher itself
83 84 85 86 87
FLASHER="${WRS_BASE_DIR}/usb-loader/mch_flasher"
if [ -x $FLASHER ]; then
    true
else
    if CC=cc make -s -C ${WRS_BASE_DIR}/usb-loader; then true; else
88 89
	echo "$0: Error compiling in directory ${WRS_BASE_DIR}/usb-loader" >&2;
	exit 1;
90
    fi
91 92
fi

93 94 95 96 97
# allow faking operations
if $fake;  then
    FLASHER="fake_flasher $FLASHER"
fi

98 99 100 101 102 103 104
# Default MAC address for the switch board ethernet
MAC1_DEF="02:34:56:78:9A:BC"
MAC1=$MAC1_DEF
# Default base MAC address for the 18 switch ports
MAC2_DEF="02:34:56:78:9A:00"
MAC2=$MAC2_DEF

105 106 107 108
# By default we select the gateware only for 18ports because 8ports are
# only used by developper.
gateware="18p*.bin"

109 110
DEV=""
FLAGS=""
111

112 113 114 115 116 117
# full path to the at91boot binary
at91bs=""
# full path to the barebox binary
barebox=""
# full path to the kernel image
kernel=""
118 119
# full path to the /usr.gz file system
usrtar=""
120
# full path to the cpio image
121
initramfs=""
122

123 124 125
if [ "x$TMPDIR" = "x" ]; then TMPDIR="/tmp"; fi

WRSTMPDIR=$(mktemp -d $TMPDIR/flash-wrs-XXXXXX)
126

127
while [ $# -ge 1 ]; do
128 129
	case $1 in
	-b|--build)
130 131 132
		at91bs=${WRS_OUTPUT_DIR}/images/at91bootstrap.bin;
		barebox=${WRS_OUTPUT_DIR}/images/barebox.bin
		kernel=${WRS_OUTPUT_DIR}/images/zImage
133 134
		initramfs=${WRS_OUTPUT_DIR}/images/wrs-initramfs.gz
		usrtar=${WRS_OUTPUT_DIR}/images/wrs-usr.tar.gz
135
		tarball=${WRS_OUTPUT_DIR}/images/wrs-firmware.tar
136
		shift;;
137

138
	*.tar)
139
		checkExit "tar-file" $1
140
		tar --directory $WRSTMPDIR -xf $1
141 142 143
		at91bs=$WRSTMPDIR/at91bootstrap.bin;
		barebox=$WRSTMPDIR/barebox.bin
		kernel=$WRSTMPDIR/zImage
144 145
		initramfs=$WRSTMPDIR/wrs-initramfs.gz
		usrtar=$WRSTMPDIR/wrs-usr.tar.gz
146
		tarball=$1
147
		shift;;
148

149
	-h|--help) 	showhelp; shift;;
150

151
	-m|--memmode)
152 153
		# verify memory mode
		if [ "$2" != "nf" ] && [ "$2" != "df" ] && [ "$2" != "ddr" ] && [ "$2" != "default" ]; then
154
			echo "Error: Invalid memory mode \"$2\""
155 156 157 158 159
			showhelp
			exit 1
		fi
		memmode="$2"
		shift; shift;;
160 161 162 163 164 165 166 167 168 169 170
		
	-g|--gateware)
		# Obtain which gateware type we want to keep for flash
		if [ "$2" = "18p" ] || [ "$2" = "8p" ]; then
			gateware="$2*.bin";
		else
			echo "Error: Invalid gateware selection \"$2\""
			showhelp
			exit 1
		fi
		shift; shift;;
171

172
	/* ) DEV="-s $1"; shift ;;
173

174 175
	-m1|--mac1|-m2|--mac2)
		echo "Error: can't change mac1 and mac2 any more. See docs" >& 2
176
		shift; shift;;
177

178 179 180
	-[ecs])
		# e == erase, c == check, s == scrub
		FLAGS="${FLAGS} $1"; shift;;
181

182 183 184 185
	* ) echo "$0: Invalid argument \"$1\"" >&2; exit 1;;
    esac
done

186
if [ "x$usrtar" = "x" ]; then
187 188 189
    showhelp
fi

190 191 192 193 194 195 196 197 198 199 200 201

# Check that there is a tftpboot place with the right thing...
tartftp=wrs-firmware.tar
tarsum=$(md5sum < $tarball)
for n in /tftpboot /home/tftpboot $HOME/tftpboot $HOME/[wW][rR]/tftpboot; do
    if [ ! -d $n ]; then continue; fi
    if [ ! -f $n/$tartftp ]; then continue; fi
    sum2=$(md5sum < $n/$tartftp)
    if [ "$tarsum" != "$sum2" ]; then
	echo "Warning: File $n/$tartftp not up to date";
	echo "Maybe you want to run the following command:"
	echo ""
202
	echo "  cp $tarball $n/$tartftp"
203 204 205 206 207 208
	echo ""
	echo "Press enter to continue"
	read a
    fi
done

209 210
# Even if not all files are always needed, check all of them immediately
me=$(basename $0)
211
echo "${me}: Working in $WRSTMPDIR"
212 213 214 215 216
checkExit "AT91Bootstrap IPL"           $at91bs
checkExit "BareBox boot loader"         $barebox
checkExit "Linux kernel"                $kernel
checkExit "initial RAM disk"            $rootfsgz
checkExit "archive for /usr filesystem" $usrtar
217 218


219 220
## MAC1 and MAC2 are really recommended
if [ "x$MAC1" = "x$MAC1_DEF" ]; then
221
    echo "Warning: you did not set the MAC1 value; using \"$MAC1_DEF\""
222 223
fi
if [ "x$MAC2" = "x$MAC2_DEF" ]; then
224
    echo "Warning: you did not set the MAC2 value; using \"$MAC2_DEF\""
225
fi
226

227
## Selecting the running memmode
228
df=false; nf=false; ddr=false
229
if [ "x$memmode" = "xdf" ]; then
230
	df=true
231
elif [ "x$memmode" = "xnf" ]; then
232
	nf=true
233
elif [ "x$memmode" = "xddr" ]; then
234
	ddr=true
235
else
236 237
	df=true
	nf=true
238
fi
239

240
# Check if atmel sam-ba is found by lsusb
241
if lsusb | grep -q "at91sam" || $fake; then
242 243 244 245
	true;
else
	echo ""
	echo "$me: Waiting for at91sam SAMBA bootloader on usb."
Alessandro Rubini's avatar
Alessandro Rubini committed
246
	echo "    Please check the Management USB cable is connected "
247 248
	echo "    and keep pressed the Flash button while" 
	echo "    resetting/powering the switch."
249 250 251
	while true; do
		if lsusb | grep -q "at91sam"; then
			break
252 253
		fi
		sleep 1
254 255 256
		echo -n "."
	done
	echo " Ok"
257 258
fi

259
# Remove the jumper now, before proceeding
260 261 262 263 264
if $fake; then
    echo "$me: fake operations, let's continue withouth a WR switch;"
else
    echo "$me: I'm talking with the switch;"
fi
265
echo -n "   please release the flash button and press Enter to start flashing: "
266 267
read unused

Alessandro Rubini's avatar
Alessandro Rubini committed
268
## Flashing DataFlash: at91bootstrap at 0, then barebox at 0x8400 (33792)
269
if $df; then
270
	${FLASHER} -m df $FLAGS $DEV ${at91bs} 0 ${barebox} 33792
271 272
fi

273
## Prepare for the NAND flashing procedure (kernel, initramfs, /usr)
274
if $nf; then
275

276 277 278 279 280 281 282 283 284 285 286
	# A this poinit, we used to create a special filesystem but that
	# step is not needed any more: the standard initramfs works for
	# installation too.

	# So, the only things that remains is setting up the magic string
	# before the initramfs itself:
	MAGICSTR=$WRSTMPDIR/magicstr
	echo -n paperino > $MAGICSTR

	# Also, we don't erase nand: the initramfs does it at install time
	#$FLASHER -e -m nand
287

288
	# Start nand flashing procedure
289
	$FLASHER -m ddr $FLAGS $DEV \
290
	    ${barebox}   0x0000000 \
291 292 293
	    ${kernel}    0x1000000 \
	    ${MAGICSTR}  0x17FFFF8 \
	    ${initramfs} 0x1800000
294

295
	if $remove_temps; then
296
	    rm -rf $WRSTMPDIR
297
	else
298
	    echo "Keeping temporary directory: ${WRSTMPDIR}"
299
	fi
300 301 302 303 304 305 306 307 308 309

	# Now, flashing proceeds by booting a kernel, get its messages

	# Wait for the device to disappear
	while [ -e /dev/ttyACM0 ]; do sleep 0.1; done
	# Wait for the device to appear again
	while [ ! -e /dev/ttyACM0 ]; do sleep 0.1; done
	# Report install messages until the device disappears again
	cat /dev/ttyACM0

310 311
fi

312
## Loading in DDR. This is used to boot without booting (i.e. no at91boot)
313
if $ddr; then
314 315 316

	# barebox would be enough, but place kernel and initramfs too. it costs nothing
	$FLASHER -m ddr $FLAGS $DEV \
317
	    ${barebox}   0x0000000 \
318 319 320 321 322 323 324 325 326 327 328 329 330
	    ${kernel}    0x1000000 \
	    ${initramfs} 0x1800000

	# It looks like this does it all. We are done

	if $remove_temps; then
	    rm -rf $WRSTMPDIR
	else
	    echo "Keeping temporary directory: ${WRSTMPDIR}"
	fi



331
fi
332

333
echo -e "\n\nDone\n"