1if itest.s a$uboot_defconfig == a; then
2        echo "Please set uboot_defconfig to the appropriate value"
3        exit
4fi
5
6offset=0x400
7erase_size=0xC0000
8qspi_offset=0x0
9a_base=0x12000000
10block_size=0x200
11
12#grab 1st 2/3 characters of string
13setexpr cpu2 sub "^(..?).*" "\\1" "${imx_cpu}"
14setexpr cpu3 sub "^(..?.?).*" "\\1" "${imx_cpu}"
15if itest.s x51 == "x${cpu2}"; then
16	a_base=0x92000000
17elif itest.s x53 == "x${cpu2}"; then
18	a_base=0x72000000
19elif itest.s x6SX == "x${cpu3}" || itest.s x6U == "x${cpu2}" || itest.s x7D == "x${cpu2}"; then
20	a_base=0x82000000
21elif itest.s x8M == "x${cpu2}"; then
22	a_base=0x42000000
23	offset=0x8400
24	if itest.s x8MN == "x${cpu3}" || itest.s x8MP == "x${cpu3}"; then
25		offset=0x8000
26		if itest ${env_part} != 0 ; then
27			offset=0x0
28		fi
29	fi
30fi
31
32qspi_match=1
33setexpr a_qspi1 ${a_base}
34setexpr a_qspi2 ${a_qspi1} + 0x400000
35setexpr a_uImage1 ${a_qspi1} + 0x400
36setexpr a_uImage2 ${a_qspi2} + 0x400
37setexpr a_script ${a_base}
38
39if itest.s "x${vidconsole}" == "x" ; then
40	vidconsole=vga
41fi
42setenv stdout serial,${vidconsole}
43
44if itest.s "x${sfname}" == "x" ; then
45# U-Boot resides in (e)MMC
46if itest.s "x${env_dev}" == "x" || itest.s "x${env_part}" == "x"; then
47        echo "Please set env_dev/part to the appropriate values"
48        exit
49fi
50
51# Load bootloader binary for this board
52if ${fs}load ${devtype} ${devnum}:${distro_bootpart} ${a_uImage1} u-boot.$uboot_defconfig ; then
53else
54	echo "File u-boot.$uboot_defconfig not found on SD card" ;
55	exit
56fi
57
58# Compute block count for filesize and offset
59setexpr cntoffset ${offset} / ${block_size}
60setexpr cntfile ${filesize} / ${block_size}
61# Add 1 in case the $filesize is not a multiple of $block_size
62setexpr cntfile ${cntfile} + 1
63
64# Select media partition (if different from main partition)
65mmc dev ${env_dev} ${env_part}
66
67# Read and compare current U-Boot
68mmc read ${a_uImage2} ${cntoffset} ${cntfile}
69if cmp.b ${a_uImage1} ${a_uImage2} ${filesize} ; then
70	echo "------- U-Boot versions match" ;
71	echo "------- U-Boot upgrade NOT needed" ;
72	exit ;
73fi
74
75echo "Need U-Boot upgrade" ;
76echo "Program in 5 seconds" ;
77for n in 5 4 3 2 1 ; do
78	echo $n ;
79	sleep 1 ;
80done
81mmc write ${a_uImage1} ${cntoffset} ${cntfile}
82
83# Make sure to boot from the proper partition
84if itest ${env_part} != 0 ; then
85	mmc partconf ${env_dev} 1 ${env_part} 0
86fi
87
88# Switch back to main eMMC partition (to avoid confusion)
89mmc dev ${env_dev}
90
91else
92# U-Boot resides in NOR flash
93if sf probe || sf probe || sf probe 1 27000000 || sf probe 1 27000000 ; then
94	echo "probed SPI ROM" ;
95else
96	echo "Error initializing EEPROM"
97	exit
98fi
99
100if itest.s "x${sfname}" == "xat45db041d" ; then
101	erase_size=0x7e000
102fi
103
104if itest.s x7D == "x${imx_cpu}"; then
105	echo "check qspi parameter block" ;
106	if ${fs}load ${devtype} ${devnum}:${distro_bootpart} ${a_qspi1} qspi-${sfname}.${uboot_defconfig} ; then
107	else
108		echo "parameter file qspi-${sfname}.${uboot_defconfig} not found on SD card"
109		exit
110	fi
111	if itest ${filesize} != 0x200 ; then
112		echo "------- qspi-${sfname}.${uboot_defconfig} 0x${filesize} != 0x200 bytes" ;
113		exit
114	fi
115	setexpr a_marker ${a_qspi1} + 0x1fc
116	if itest *${a_marker} != c0ffee01 ; then
117		echo "------- qspi-${sfname}.${uboot_defconfig} c0ffee01 marker missing" ;
118		exit
119	fi
120	if sf read ${a_qspi2} ${qspi_offset} 0x200 ; then
121	else
122		echo "Error reading qspi parameter from EEPROM"
123		exit
124	fi
125	if cmp.b ${a_qspi1} ${a_qspi2} 0x200 ; then
126		echo "------- qspi parameters match"
127	else
128		echo "------- qspi parameters mismatch"
129		qspi_match=0
130	fi
131fi
132
133echo "check U-Boot" ;
134
135if ${fs}load ${devtype} ${devnum}:${distro_bootpart} ${a_uImage1} u-boot.$uboot_defconfig ; then
136else
137	echo "File u-boot.$uboot_defconfig not found on SD card" ;
138	exit
139fi
140echo "read $filesize bytes from SD card" ;
141if sf read ${a_uImage2} $offset $filesize ; then
142else
143	echo "Error reading boot loader from EEPROM" ;
144	exit
145fi
146
147if cmp.b ${a_uImage1} ${a_uImage2} $filesize ; then
148	echo "------- U-Boot versions match" ;
149	if itest.s "${qspi_match}" == "1" ; then
150		echo "------- U-Boot upgrade NOT needed" ;
151		if itest.s "x" != "x${next}" ; then
152			if ${fs}load ${devtype} ${devnum}:${distro_bootpart} ${a_script} ${next} ; then
153				source ${a_script}
154			else
155				echo "${next} not found on SD card"
156			fi
157		fi
158		exit
159	fi
160	erase_size=0x1000
161	if itest.s xMX25L6405D == "x${sfname}"; then
162		erase_size=0x10000
163	fi
164	setexpr filesize ${erase_size} - ${offset}
165fi
166
167echo "Need U-Boot upgrade" ;
168echo "Program in 5 seconds" ;
169for n in 5 4 3 2 1 ; do
170	echo $n ;
171	sleep 1 ;
172done
173echo "erasing" ;
174sf erase 0 ${erase_size} ;
175
176# two steps to prevent bricking
177echo "programming" ;
178setexpr a1 ${a_uImage1} + 0x400
179setexpr o1 ${offset} + 0x400
180setexpr s1 ${filesize} - 0x400
181sf write ${a1} ${o1} ${s1} ;
182sf write ${a_uImage1} $offset 0x400 ;
183
184if itest.s x7D == "x${imx_cpu}"; then
185	sf write ${a_qspi1} ${qspi_offset} 0x200
186fi
187
188echo "verifying" ;
189if sf read ${a_uImage2} $offset $filesize ; then
190else
191	echo "Error re-reading EEPROM" ;
192	exit
193fi
194if cmp.b ${a_uImage1} ${a_uImage2} $filesize ; then
195else
196	echo "Read verification error" ;
197	exit
198fi
199
200if itest.s x7D == "x${imx_cpu}"; then
201	if sf read ${a_qspi2} ${qspi_offset} 0x200 ; then
202	else
203		echo "Error re-reading qspi" ;
204		exit
205	fi
206	if cmp.b ${a_qspi1} ${a_qspi2} 0x200 ; then
207	else
208		echo "qspi parameter block verification error" ;
209		exit
210	fi
211fi
212
213if itest.s "x" != "x${next}" ; then
214	if ${fs}load ${devtype} ${devnum}:${distro_bootpart} ${a_script} ${next} ; then
215		source ${a_script}
216	else
217		echo "${next} not found on ${devtype} ${devnum}:${distro_bootpart}"
218	fi
219fi
220fi
221
222if itest.s "xno" == "x${reset}" ; then
223	while echo "---- U-Boot upgraded. Please reset the board" ; do
224		sleep 120
225	done
226fi
227echo "---- U-Boot upgraded. The board will now reset."
228sleep 1
229reset
230done
231