3 #==================================================
5 # Purpose: Update U-Boot partition using mtd tool
7 # Author: Piotr Dymacz <piotr@dymacz.pl>
8 # Copyright: Copyright (C) 2014 Piotr Dymacz
10 #==================================================
13 NEW_UBOOT_DIR="/etc/u-boot_mod"
15 MTD_BACKUP_FNAME="mtd0_backup.bin"
18 echo -e "[\e[93minfo\e[0m] $1"
22 echo -e "[\e[31merro\e[0m] $1"
26 echo -e "[\e[92m ok \e[0m] $1"
33 echo "================================================================="
34 echo -e " \e[93mDISCLAIMER: you are using this script at your own risk!\e[0m"
36 echo " The author of U-Boot modification and this script takes"
37 echo " no responsibility for any of the results of using them."
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
47 echo "================================================================="
55 read -p " Please, type 'yes' or 'no': " answer
61 find_new_uboot_file() {
66 if [ ! -d $NEW_UBOOT_DIR ]; then
67 echo_err "Directory $NEW_UBOOT_DIR does not exist"
71 cd $NEW_UBOOT_DIR 2>/dev/null
73 echo_err "Could not change directory to $NEW_UBOOT_DIR"
77 filesqty=`ls -1 | grep "\.bin" | wc -l`
79 if [ $filesqty -eq 0 ]; then
80 echo_err "Could not find any binary file in $NEW_UBOOT_DIR"
84 if [ $filesqty -gt 1 ]; then
85 echo_err "Found more than one binary file in $NEW_UBOOT_DIR"
89 NEW_UBOOT_FNAME=`ls -1 | grep "\.bin"`
90 new_ubot_md5="`basename $NEW_UBOOT_FNAME .bin`.md5"
92 if [ ! -e $new_ubot_md5 ]; then
93 echo_err "Checksum file $new_ubot_md5 does not exist"
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
109 read -p " Please, type 'yes' or 'no': " answer
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"
120 echo_ok "MD5 checksum of new U-Boot image file is correct"
126 if [ ! -c /dev/$MTD_DEVICE ]; then
127 echo_err "Device /dev/$MTD_DEVICE does not exist"
132 if [ $? -ne 0 ]; then
133 echo_err "Could not change directory to /tmp"
137 if [ -e $MTD_BACKUP_FNAME ]; then
138 rm -f $MTD_BACKUP_FNAME 2>/dev/null
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"
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
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/"
159 echo_ok "Backup of /dev/$MTD_DEVICE successfully copied to $NEW_UBOOT_DIR/backup/"
166 read -p " Please, type 'yes' or 'no': " answer
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}'`
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"
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)"
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"
197 echo_ok "New U-Boot image successfully combined with backup file"
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
216 read -p " Please, type 'yes' or 'no': " answer
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!"
229 echo_ok "New U-Boot image successfully written into FLASH"
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!"
239 echo_ok "MD5 checksum of $MTD_DEVICE and new U-Boot image are equal"
244 #======================
245 # Execution begins here
246 #======================