248c5b913ca614b951a8782d015822d62ff9fef1
1 #!/bin/bash
2 # Utility script to select the bootmode from command line
3 # Author: Nikhil Devshatwar
5 # No need to change anything below this line
6 UMS_part1=/dev/disk/by-id/usb-Linux_UMS_disk*part1
7 UMS_part2=/dev/disk/by-id/usb-Linux_UMS_disk*part2
8 SCRIPT=$(readlink -f $0)
9 SCRIPTPATH=`dirname $SCRIPT`
10 user=`logname`
12 usage()
13 {
14 echo
15 echo "dfu-boot.sh => Utility script to select bootmode and mount MMC to PC"
16 echo "Usage:"
17 echo " sudo ./dfu-boot.sh --PLATFORM [--mount DEV | --bootmode MODE | --tftp ADDRESS]"
18 echo " PLATFORM: j721e-evm, j7200-evm, am65xx-evm"
19 echo " DEV: specify the device to mount => 1 for MMC, 0 for eMMC"
20 echo " MODE: specify the bootmode to use"
21 echo " ADDRESS: specify the IP address for tftp/NFS boot"
22 }
24 read_config() {
25 configfile=$HOME/.config/k3bootswitch.conf
26 section=$1
27 param=$2
29 python3 -c "
30 import configparser;
31 import sys;
32 config = configparser.ConfigParser();
33 config.read('$configfile');
34 print (config.get('$section','$param'));
35 "
36 }
38 init() {
39 board=$1
41 prebuilt=$SCRIPTPATH/bin/$board
42 boot_select=$SCRIPTPATH/boot_select/$board
44 uart_dev=`read_config $board uart_dev`
45 nfspath=`read_config $board nfspath`
46 switch=`read_config $board switch`
48 ipaddr=`read_config core ipaddr`
49 }
51 toggle_power()
52 {
53 switch=$1
54 echo " >>>> Toggling phidget..."
55 (phidget-switch $switch 0 && sleep 1 && phidget-switch $switch 1 && sleep 0.1) >/dev/null 2>&1
56 if [ $? -ne 0 ]; then
57 echo -n "ERROR: phidget not found, Reboot manually and press enter.. "
58 read DUMMY
59 fi
60 }
62 # Bootloader takes time to initialize
63 # wait till PC detects a dfu device
64 wait_till_ready() {
65 msg=$1
66 for i in `seq 30`; do
67 dfu-util -l | grep "Found DFU" >/dev/null 2>&1
68 if [ $? -eq "0" ]; then
69 >&2 echo " >>>> dfu ready $msg after $i tries"
70 return
71 fi
72 sleep 0.2
73 done
74 >&2 echo " >>>> ERROR: Timeout waiting for dfu"
75 >&2 echo " >>>> Make sure to connect USB cable from EVM to host machine"
76 >&2 echo " >>>> Refer to readme for correct switch settings for DFU bootmode:"
77 exit 1
78 }
80 # Use dfu to send prebuilt binaries till you get to the
81 # Cortex-A u-boot prompt
82 boot_till_uboot() {
83 wait_till_ready "for tiboot3.bin"
84 2>&1 dfu-util -R -a bootloader -D $prebuilt/tiboot3.bin
85 # Skip the sysfe.itb where combined boot flow is used
86 if [ $board != "am64xx-evm" ]; then
87 wait_till_ready "for sysfw.itb"
88 2>&1 dfu-util -R -a sysfw.itb -D $prebuilt/sysfw.itb
89 fi
90 wait_till_ready "for tispl.bin"
91 2>&1 dfu-util -R -a tispl.bin -D $prebuilt/tispl.bin
92 wait_till_ready "for u-boot.img"
93 2>&1 dfu-util -R -a u-boot.img -D $prebuilt/u-boot.img
94 }
96 # Detect and mount the partitions
97 try_mount() {
98 uart_dev=$1
99 mdev=$2
100 for i in `seq 1 100`; do
101 echo "ums 0 mmc $mdev" > $uart_dev
102 sleep 0.1
103 if [ -b $UMS_part1 ] && [ -b $UMS_part2 ]; then
104 mkdir -p /media/$user/UMS-boot
105 mkdir -p /media/$user/UMS-rootfs
106 mount $UMS_part1 /media/$user/UMS-boot -o uid=`id | cut -d'(' -f1 | cut -d'=' -f2`,gid=`id | cut -d'(' -f2 | cut -d'=' -f2`
107 mount $UMS_part2 /media/$user/UMS-rootfs
108 echo " >>>> Mounted partions at /media/$user/UMS-boot and /media/$user/UMS-rootfs"
109 return
110 fi
111 done
112 >&2 echo " >>>> ERROR: Could not find partitions $UMS_part1"
113 exit 1
114 }
116 change_bootmode_slow() {
117 uart_dev=$1
118 sleep 5
119 for i in `seq 1 100`; do
120 echo "mw.w 0x43000030 0x0d3b" > $uart_dev
121 sleep 0.1
122 echo "reset" > $uart_dev
123 sleep 0.1
124 done
125 }
128 # Send a boot_select binary
129 change_bootmode() {
130 bootmode=$1
131 if [ ! -f $boot_select/spl.$bootmode ]; then
132 echo "Invalid bootmode $bootmode"
133 options=`ls $boot_select/spl* | awk -F"." 'BEGIN{ORS=" "} { print $2 }'`
134 echo "Supported bootmodes for $board are: $options"
135 exit 1
136 fi
138 wait_till_ready
139 echo " >>>> Selecting bootmode: $bootmode"
140 dfu-util -R -a bootloader -D $boot_select/spl.$bootmode >/dev/null 2>&1
141 if [ $? -eq 0 ]; then
142 echo " >>>> SUCCESS"
143 else
144 echo " >>>> FAILED"
145 fi
146 }
148 # Send commands to do a tftp boot
149 tftp_boot() {
150 ipaddr=$1
151 nfspath=$2
153 for i in `seq 1 30`; do
154 echo "" > $uart_dev
155 sleep 0.3
156 done
158 cat >>$uart_dev << EOF
159 run findfdt
160 setenv bootargs console=\$console \$optargs root=/dev/nfs rw nfsroot=$ipaddr:$nfspath,nolock,v3,tcp,rsize=4096,wsize=4096 ip=dhcp sysrq_always_enabled loglevel=8 earlycon=ns16550a,mmio32,0x02800000
161 setenv bootcmd 'run args_all; setenv autoload no; dhcp; setenv serverip $ipaddr; run findfdt; tftp \${loadaddr} Image; tftp \${fdtaddr} \${name_fdt}; fdt address \${fdtaddr}; fdt resize 0x100000; booti \${loadaddr} - \${fdtaddr}'
162 boot
163 EOF
164 }
166 # Main script starts from here
167 if [ `whoami` != "root" ]; then
168 echo "This script should be called with sudo!!"
169 usage
170 exit 1
171 fi
173 while [[ $# -gt 0 ]]
174 do
175 case $1 in
176 --j7|--j7es|--j721e|--j721e-evm)
177 init "j721e-evm"
178 shift
179 ;;
180 --vcl|--j7vcl|--j7200|--j7200-evm)
181 init "j7200-evm"
182 shift
183 ;;
184 --am65|--am65x-evm|--am654-idk|--am65xx-evm)
185 init "am65xx-evm"
186 shift
187 ;;
188 --am64|--am64xx-evm)
189 init "am64xx-evm"
190 shift
191 ;;
192 -t|--tftp)
193 ipaddr=$2
194 shift
195 shift
196 ;;
197 -m|--mount)
198 mdev=$2
199 shift
200 shift
201 ;;
202 -b|--bootmode)
203 bootmode=$2
204 shift
205 shift
206 ;;
207 -h|--help)
208 usage
209 exit 0
210 ;;
211 *)
212 echo "Invalid argument $1"
213 usage
214 exit 1
215 ;;
216 esac
217 done
219 init $board
221 if [ ! -z $bootmode ]; then
222 # Reboot the board in specified bootmode
223 toggle_power $switch
224 if [ $board == "am64x-evm" ] && [ $bootmode == "uart" ]; then
225 boot_till_uboot >/dev/null
226 change_bootmode_slow $uart_dev
227 else
228 change_bootmode $bootmode
229 fi
230 elif [ ! -z $mdev ]; then
231 # Reboot the board and mount the specified device
232 toggle_power $switch
233 boot_till_uboot >/dev/null
234 try_mount $uart_dev $mdev
235 elif [ ! -z $ipaddr ]; then
236 # Reboot the board and mount the specified device
237 toggle_power $switch
238 boot_till_uboot >/dev/null
239 tftp_boot $ipaddr $nfspath
240 else
241 echo "Invalid usage!!"
242 usage
243 fi