1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4######################################################
5# Copyright (C) 2016 Marvell International Ltd.
6#
7# https://spdx.org/licenses
8#
9# Author: Konstantin Porotchkin kostap@marvell.com
10#
11# Version 0.3
12#
13# UART recovery downloader for Armada SoCs
14#
15######################################################
16
17port=$1
18file=$2
19speed=$3
20
21pattern_repeat=1500
22default_baudrate=115200
23tmpfile=/tmp/xmodem.pattern
24tools=( dd stty sx minicom )
25
26case "$3" in
27    2)
28        fast_baudrate=230400
29        prefix="\xF2"
30        ;;
31    4)
32        fast_baudrate=460800
33        prefix="\xF4"
34        ;;
35    8)
36    	fast_baudrate=921600
37        prefix="\xF8"
38        ;;
39    *)
40    	fast_baudrate=$default_baudrate
41        prefix="\xBB"
42esac
43
44if [[ -z "$port" || -z "$file" ]]
45then
46    echo -e "\nMarvell recovery image downloader for Armada SoC family."
47    echo -e "Command syntax:"
48    echo -e "\t$(basename $0) <port> <file> [2|4|8]"
49    echo -e "\tport  - serial port the target board is connected to"
50    echo -e "\tfile  - recovery boot image for target download"
51    echo -e "\t2|4|8 - times to increase the default serial port speed by"
52    echo -e "For example - load the image over ttyUSB0 @ 460800 baud:"
53    echo -e "$(basename $0) /dev/ttyUSB0 /tmp/flash-image.bin 4\n"
54    echo -e "=====WARNING====="
55    echo -e "- The speed-up option is not available in SoC families prior to A8K+"
56    echo -e "- This utility is not compatible with Armada 37xx SoC family\n"
57fi
58
59# Sanity checks
60if [ -c "$port" ]
61then
62   echo -e "Using device connected on serial port \"$port\""
63else
64   echo "Wrong serial port name!"
65   exit 1
66fi
67
68if [ -f "$file" ]
69then
70   echo -e "Loading flash image file \"$file\""
71else
72   echo "File $file does not exist!"
73   exit 1
74fi
75
76# Verify required tools installation
77for tool in ${tools[@]}
78do
79    toolname=`which $tool`
80    if [ -z "$toolname" ]
81    then
82        echo -e "Missing installation of \"$tool\" --> Exiting"
83        exit 1
84    fi
85done
86
87
88echo -e "Recovery will run at $fast_baudrate baud"
89echo -e "========================================"
90
91if [ -f "$tmpfile" ]
92then
93    rm -f $tmpfile
94fi
95
96# Send the escape sequence to target board using default debug port speed
97stty -F $port raw ignbrk time 5 $default_baudrate
98counter=0
99while [ $counter -lt $pattern_repeat ]; do
100    echo -n -e "$prefix\x11\x22\x33\x44\x55\x66\x77" >> $tmpfile
101    let counter=counter+1
102done
103
104echo -en "Press the \"Reset\" button on the target board and "
105echo -en "the \"Enter\" key on the host keyboard simultaneously"
106read
107dd if=$tmpfile of=$port &>/dev/null
108
109# Speed up the binary image transfer
110stty -F $port raw ignbrk time 5 $fast_baudrate
111sx -vv $file > $port < $port
112#sx-at91 $port $file
113
114# Return the port to the default speed
115stty -F $port raw ignbrk time 5 $default_baudrate
116
117# Optional - fire up Minicom
118minicom -D $port -b $default_baudrate
119
120