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"
18 echo " PLATFORM: Either of --j721e-evm or --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 }
23 init() {
24 board=$1
25 # Customize this as required
26 if [ "$board" = "j721e-evm" ]; then
27 uart_dev=/dev/ttyUSB0
28 switch=0
29 elif [ "$board" = "am65xx-evm" ]; then
30 uart_dev=/dev/ttyUSB4
31 switch=3
32 else
33 echo "Invalid board"
34 usage
35 exit 1
36 fi
38 prebuilt=$SCRIPTPATH/bin/$board
39 boot_select=$SCRIPTPATH/boot_select/$board
40 }
42 toggle_power()
43 {
44 switch=$1
45 echo " >>>> Toggling phidget..."
46 (phidget-switch $switch 0 && sleep 0.5 && phidget-switch $switch 1 && sleep 0.1) >/dev/null 2>&1
47 if [ $? -ne 0 ]; then
48 echo -n "ERROR: phidget not found, Reboot manually and press enter.. "
49 read DUMMY
50 fi
51 }
53 # Bootloader takes time to initialize
54 # wait till PC detects a dfu device
55 wait_till_ready() {
56 msg=$1
57 for i in `seq 30`; do
58 dfu-util -l | grep "Found DFU" >/dev/null 2>&1
59 if [ $? -eq "0" ]; then
60 >&2 echo " >>>> dfu ready $msg after $i tries"
61 return
62 fi
63 sleep 0.2
64 done
65 >&2 echo " >>>> ERROR: Timeout waiting for dfu"
66 >&2 echo " >>>> Make sure to connect USB cable from EVM to host machine"
67 >&2 echo " >>>> Refer to readme for correct switch settings for DFU bootmode:"
68 exit 1
69 }
71 # Use dfu to send prebuilt binaries till you get to the
72 # Cortex-A u-boot prompt
73 boot_till_uboot() {
74 wait_till_ready "for tiboot3.bin"
75 2>&1 dfu-util -R -a bootloader -D $prebuilt/tiboot3.bin
76 wait_till_ready "for sysfw.itb"
77 2>&1 dfu-util -R -a sysfw.itb -D $prebuilt/sysfw.itb
78 wait_till_ready "for tispl.bin"
79 2>&1 dfu-util -R -a tispl.bin -D $prebuilt/tispl.bin
80 wait_till_ready "for u-boot.img"
81 2>&1 dfu-util -R -a u-boot.img -D $prebuilt/u-boot.img
82 }
84 # Detect and mount the partitions
85 try_mount() {
86 uart_dev=$1
87 mdev=$2
88 for i in `seq 1 100`; do
89 echo "ums 0 mmc $mdev" > $uart_dev
90 sleep 0.1
91 if [ -b $UMS_part1 ] && [ -b $UMS_part2 ]; then
92 mkdir -p /media/$user/UMS-boot
93 mkdir -p /media/$user/UMS-rootfs
94 mount $UMS_part1 /media/$user/UMS-boot
95 mount $UMS_part2 /media/$user/UMS-rootfs
96 echo " >>>> Mounted partions at /media/$user/UMS-boot and /media/$user/UMS-rootfs"
97 return
98 fi
99 done
100 >&2 echo " >>>> ERROR: Could not find partitions $UMS_part1"
101 exit 1
102 }
104 # Send a boot_select binary
105 change_bootmode() {
106 bootmode=$1
107 if [ ! -f $boot_select/spl.$bootmode ]; then
108 echo "Invalid bootmode $bootmode"
109 options=`ls $boot_select/spl* | awk -F"." 'BEGIN{ORS=" "} { print $2 }'`
110 echo "Supported bootmodes for $board are: $options"
111 exit 1
112 fi
114 wait_till_ready
115 echo " >>>> Selecting bootmode: $bootmode"
116 dfu-util -R -a bootloader -D $boot_select/spl.$bootmode >/dev/null 2>&1
117 if [ $? -eq 0 ]; then
118 echo " >>>> SUCCESS"
119 else
120 echo " >>>> FAILED"
121 fi
122 }
124 # Main script starts from here
125 if [ `whoami` != "root" ]; then
126 echo "This script should be called with sudo!!"
127 usage
128 exit 1
129 fi
131 while [[ $# -gt 0 ]]
132 do
133 case $1 in
134 --j7|--j721e|--j721e-evm)
135 init "j721e-evm"
136 shift
137 ;;
138 --am6|--am654|--am65x-evm|--am654-idk|--am65xx-evm)
139 init "am65xx-evm"
140 shift
141 ;;
142 -m|--mount)
143 mdev=$2
144 shift
145 shift
146 ;;
147 -b|--bootmode)
148 bootmode=$2
149 shift
150 shift
151 ;;
152 -h|--help)
153 usage
154 exit 0
155 ;;
156 *)
157 echo "Invalid argument $1"
158 usage
159 exit 1
160 ;;
161 esac
162 done
164 init $board
165 if [ ! -z $bootmode ]; then
166 # Reboot the board in specified bootmode
167 toggle_power $switch
168 change_bootmode $bootmode
170 elif [ ! -z $mdev ]; then
171 # Reboot the board and mount the specified device
172 toggle_power $switch
173 boot_till_uboot >/dev/null
174 try_mount $uart_dev $mdev
175 else
176 echo "Invalid usage!!"
177 usage
178 fi