0846a17f5c0540052ab180f6280c6dcb33daa1fc
[oweals/u-boot_mod.git] / target_util / u-boot-upgrade
1 #!/bin/sh
2
3 #==================================================
4 #      Name: u-boot-upgrade
5 #   Purpose: Update U-Boot partition using mtd tool
6 #
7 #    Author: Piotr Dymacz <piotr@dymacz.pl>
8 # Copyright: Copyright (C) 2014 Piotr Dymacz
9 #   Created: 2014-11-19
10 #==================================================
11
12       MTD_DEVICE="mtd0"
13    NEW_UBOOT_DIR="/etc/u-boot_mod"
14  NEW_UBOOT_FNAME=
15 MTD_BACKUP_FNAME="mtd0_backup.bin"
16
17 echo_info() {
18         echo -e "[\e[93minfo\e[0m] $1"
19 }
20
21 echo_err() {
22         echo -e "[\e[31merro\e[0m] $1"
23 }
24
25 echo_ok() {
26         echo -e "[\e[92m ok \e[0m] $1"
27 }
28
29 show_disclaimer() {
30         local answer
31
32         echo ""
33         echo "================================================================="
34         echo -e "     \e[93mDISCLAIMER: you are using this script at your own risk!\e[0m"
35         echo ""
36         echo "     The author of U-Boot modification and this script takes"
37         echo "     no responsibility for any of the results of using them."
38         echo ""
39         echo -e "          \e[31mUpdating U-Boot is a very dangerous operation"
40         echo -e "        and may damage your device! You have been warned!\e[0m"
41         echo "================================================================="
42         read -p "   Are you sure you want to continue (type 'yes' or 'no')? " answer
43
44         while true; do
45                 case $answer in
46                         yes)
47                                 echo "================================================================="
48                                 echo ""
49                                 break
50                                 ;;
51                         no)
52                                 exit 0
53                                 ;;
54                         *)
55                                 read -p "   Please, type 'yes' or 'no': " answer
56                                 ;;
57                 esac
58         done
59 }
60
61 find_new_uboot_file() {
62         local answer
63         local filesqty
64         local new_ubot_md5
65
66         if [ ! -d $NEW_UBOOT_DIR ]; then
67                 echo_err "Directory $NEW_UBOOT_DIR does not exist"
68                 exit 1
69         fi
70
71         cd $NEW_UBOOT_DIR 2>/dev/null
72         if [ $? -ne 0 ]; then
73                 echo_err "Could not change directory to $NEW_UBOOT_DIR"
74                 exit 1
75         fi
76
77         filesqty=`ls -1 | grep "\.bin" | wc -l`
78
79         if [ $filesqty -eq 0 ]; then
80                 echo_err "Could not find any binary file in $NEW_UBOOT_DIR"
81                 exit 1
82         fi
83
84         if [ $filesqty -gt 1 ]; then
85                 echo_err "Found more than one binary file in $NEW_UBOOT_DIR"
86                 exit 1
87         fi
88
89         NEW_UBOOT_FNAME=`ls -1 | grep "\.bin"`
90         new_ubot_md5="`basename $NEW_UBOOT_FNAME .bin`.md5"
91
92         if [ ! -e $new_ubot_md5 ]; then
93                 echo_err "Checksum file $new_ubot_md5 does not exist"
94                 exit 1
95         fi
96
97         echo_ok "Found U-Boot image file: $NEW_UBOOT_FNAME"
98         read -p "       Do you want to use this file (type 'yes' or 'no')? " answer
99
100         while true; do
101                 case $answer in
102                         yes)
103                                 break
104                                 ;;
105                         no)
106                                 exit 0
107                                 ;;
108                         *)
109                                 read -p "       Please, type 'yes' or 'no': " answer
110                                 ;;
111                 esac
112         done
113
114         md5sum -cs $new_ubot_md5 &>/dev/null
115         if [ $? -ne 0 ]; then
116                 echo_err "MD5 checksum of new U-Boot image file is wrong"
117                 exit 1
118         fi
119
120         echo_ok "MD5 checksum of new U-Boot image file is correct"
121 }
122
123 backup_mtd_zero() {
124         local answer
125
126         if [ ! -c /dev/$MTD_DEVICE ]; then
127                 echo_err "Device /dev/$MTD_DEVICE does not exist"
128                 exit 1
129         fi
130
131         cd /tmp 2>/dev/null
132         if [ $? -ne 0 ]; then
133                 echo_err "Could not change directory to /tmp"
134                 exit 1
135         fi
136
137         if [ -e $MTD_BACKUP_FNAME ]; then
138                 rm -f $MTD_BACKUP_FNAME 2>/dev/null
139         fi
140
141         dd if=/dev/$MTD_DEVICE of=$MTD_BACKUP_FNAME &>/dev/null
142         if [ $? -ne 0 ]; then
143                 echo_err "Could not backup /dev/$MTD_DEVICE"
144                 exit 1
145         fi
146
147         echo_ok "Backup of /dev/$MTD_DEVICE successfully created"
148         read -p "       Do you want to store backup in $NEW_UBOOT_DIR/backup/ (recommended, type 'yes' or 'no')? " answer
149
150         while true; do
151                 case $answer in
152                         yes)
153                                 cp -f $MTD_BACKUP_FNAME $NEW_UBOOT_DIR/backup/$MTD_BACKUP_FNAME &>/dev/null
154                                 if [ $? -ne 0 ]; then
155                                         echo_err "Could not copy backup file $MTD_BACKUP_FNAME to $NEW_UBOOT_DIR/backup/"
156                                         exit 1
157                                 fi
158
159                                 echo_ok "Backup of /dev/$MTD_DEVICE successfully copied to $NEW_UBOOT_DIR/backup/"
160                                 break
161                                 ;;
162                         no)
163                                 break
164                                 ;;
165                         *)
166                                 read -p "       Please, type 'yes' or 'no': " answer
167                                 ;;
168                 esac
169         done
170 }
171
172 combine_images() {
173         local new_size
174         local old_size
175
176         new_size=`wc -c $NEW_UBOOT_DIR/$NEW_UBOOT_FNAME | awk '{print $1}'`
177         old_size=`wc -c /tmp/$MTD_BACKUP_FNAME | awk '{print $1}'`
178
179         if [ "x$old_size" = "x" ] || \
180            [ "x$new_size" = "x" ]; then
181                 echo_err "Could not get size of new U-Boot image and/or backup file"
182                 exit 1
183         fi
184
185         # Allow to use only images not bigger than mtd0 size
186         if [ $new_size -gt $old_size ]; then
187                 echo_err "New U-Boot image size ($new_size bytes) is bigger than $MTD_DEVICE partition size ($old_size bytes)"
188                 exit 1
189         fi
190
191         dd if=$NEW_UBOOT_DIR/$NEW_UBOOT_FNAME of=/tmp/$MTD_BACKUP_FNAME conv=notrunc &>/dev/null
192         if [ $? -ne 0 ]; then
193                 echo_err "Could not combine new U-Boot image with backup file"
194                 exit 1
195         fi
196
197         echo_ok "New U-Boot image successfully combined with backup file"
198 }
199
200 write_new_image() {
201         local answer
202         local verify_result
203
204         echo_info "New U-Boot image is ready to be written into FLASH"
205         read -p "       Are you sure you want to continue (type 'yes' or 'no')? " answer
206
207         while true; do
208                 case $answer in
209                         yes)
210                                 break
211                                 ;;
212                         no)
213                                 exit 0
214                                 ;;
215                         *)
216                                 read -p "       Please, type 'yes' or 'no': " answer
217                                 ;;
218                 esac
219         done
220
221         # Erase mtd0 and write new image...
222         mtd -e /dev/$MTD_DEVICE write /tmp/$MTD_BACKUP_FNAME /dev/$MTD_DEVICE &>/dev/null
223         if [ $? -ne 0 ]; then
224                 echo_err "FATAL ERROR: could not write new U-Boot image into FLASH"
225                 echo_err "DO NOT RESET YOUR DEVICE NOW AND TRY AGAIN!"
226                 exit 1
227         fi
228
229         echo_ok "New U-Boot image successfully written into FLASH"
230
231         # Verify MD5 of mtd0 and prepared image
232         verify_result=`mtd verify /tmp/$MTD_BACKUP_FNAME /dev/$MTD_DEVICE 2>&1 | grep "Success"`
233         if [ "x$verify_result" = "x" ]; then
234                 echo_err "FATAL ERROR: MD5 checksum of $MTD_DEVICE and new U-Boot image are not equal"
235                 echo_err "DO NOT RESET YOUR DEVICE NOW AND TRY AGAIN!"
236                 exit 1
237         fi
238
239         echo_ok "MD5 checksum of $MTD_DEVICE and new U-Boot image are equal"
240
241         echo_info "Done!"
242 }
243
244 #======================
245 # Execution begins here
246 #======================
247
248 show_disclaimer
249 find_new_uboot_file
250 backup_mtd_zero
251 combine_images
252 write_new_image
253
254 exit 0