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"
16 MTD_BACKUP_FNAME="mtd0_backup.bin"
19 echo -e "[\e[96minfo\e[0m] $1"
23 echo -e "[\e[31merro\e[0m] $1"
27 echo -e "[\e[92m ok \e[0m] $1"
31 echo -e "[\e[93mwarn\e[0m] $1"
38 read -p "-----> $question (type 'yes' or 'no')? " answer
51 read -p "Please, type 'yes' or 'no': " answer
59 echo "======================================================="
61 echo "DISCLAIMER: you are using this script at your own risk!"
64 echo "The author of U-Boot modification and this script takes"
65 echo "no responsibility for any of the results of using them."
68 echo " Updating U-Boot is a very dangerous operation"
69 echo " and may damage your device! You have been warned!"
71 echo "======================================================="
73 wait_for_yesno "Do you want to continue" || return 1
76 find_new_uboot_file() {
80 [ -d "$NEW_UBOOT_DIR" ] || {
81 echo_err "Directory '$NEW_UBOOT_DIR' does not exist"
85 cd "$NEW_UBOOT_DIR" >/dev/null 2>&1 || {
86 echo_err "Could not change directory to '$NEW_UBOOT_DIR'"
90 filesqty="$(find . -maxdepth 1 -name "*.bin" | wc -l)"
92 [ "$filesqty" -eq "0" ] && {
93 echo_err "Could not find any binary file in '$NEW_UBOOT_DIR'"
97 [ "$filesqty" -gt "1" ] && {
98 echo_err "Found more than one binary file in '$NEW_UBOOT_DIR'"
102 NEW_UBOOT_FNAME="$(basename "$(find . -maxdepth 1 -name "*.bin")")"
103 new_ubot_md5="$(basename "$NEW_UBOOT_FNAME" .bin).md5"
105 [ -e "$new_ubot_md5" ] || {
106 [ -e "md5sums" ] && \
107 grep -q "$NEW_UBOOT_FNAME" "md5sums" && \
108 grep "$NEW_UBOOT_FNAME" "md5sums" > "$new_ubot_md5" && \
109 echo_info "Checksum file successfully created from 'md5sums'"
112 [ -e "$new_ubot_md5" ] || {
113 echo_err "Checksum file '$new_ubot_md5' does not exist"
117 echo_ok "Found U-Boot image file: '$NEW_UBOOT_FNAME'"
119 wait_for_yesno "Do you want to use this file" || return 1
121 md5sum -cs "$new_ubot_md5" >/dev/null 2>&1 || {
122 echo_err "MD5 checksum of new U-Boot image file is wrong"
126 echo_ok "MD5 checksum of new U-Boot image file is correct"
130 [ -c "/dev/$MTD_DEVICE" ] || {
131 echo_err "Device /dev/$MTD_DEVICE does not exist"
135 cd /tmp >/dev/null 2>&1 || {
136 echo_err "Could not change directory to '/tmp'"
140 [ -e "$MTD_BACKUP_FNAME" ] && rm -f "$MTD_BACKUP_FNAME" >/dev/null 2>&1
142 dd if=/dev/"$MTD_DEVICE" of="$MTD_BACKUP_FNAME" >/dev/null 2>&1 || {
143 echo_err "Could not backup '/dev/$MTD_DEVICE'"
147 MTD_BACKUP_MD5="$(md5sum "$MTD_BACKUP_FNAME" | awk '{print $1}')"
148 [ -n "$MTD_BACKUP_MD5" ] || {
149 echo_err "Could not calculate MD5 of backup file"
153 echo_ok "Backup of /dev/$MTD_DEVICE successfully created"
155 wait_for_yesno "Do you want to store backup (recommended) in '$NEW_UBOOT_DIR/backup/'" || return 0
157 mkdir -p "$NEW_UBOOT_DIR/backup" >/dev/null 2>&1 || {
158 echo_err "Could not create '$NEW_UBOOT_DIR/backup'"
162 cp -f "$MTD_BACKUP_FNAME" "$NEW_UBOOT_DIR/backup/$MTD_BACKUP_FNAME" >/dev/null 2>&1 || {
163 echo_err "Could not copy backup file '$MTD_BACKUP_FNAME' to '$NEW_UBOOT_DIR/backup/'"
167 echo_ok "Backup of '/dev/$MTD_DEVICE' successfully copied to '$NEW_UBOOT_DIR/backup/'"
171 local mtdrw="kmod-mtd-rw"
175 [ -e "/sys/class/mtd/$MTD_DEVICE/flags" ] || return 0
177 flags="$(cat /sys/class/mtd/$MTD_DEVICE/flags)"
178 [ "$((flags & rwfalg))" -gt "0" ] && {
179 echo_info "Partition '/dev/$MTD_DEVICE' is writable"
183 echo_warn "Partition '/dev/$MTD_DEVICE' is not writable"
185 opkg list-installed | grep -q "$mtdrw" || {
186 wait_for_yesno "Do you want to install '$mtdrw' and unclock '/dev/$MTD_DEVICE'" || return 1
188 echo_info "Updating packages..."
190 opkg update > /dev/null 2>&1 || {
191 echo_err "Could not update packages, check your Internet connection"
195 echo_ok "Packages successfully updated, installing '$mtdrw'..."
197 opkg install "$mtdrw" > /dev/null 2>&1 || {
198 echo_err "Could not install '$mtdrw' package"
202 echo_ok "Package '$mtdrw' successfully installed"
205 echo_info "Unlocking '/dev/$MTD_DEVICE' partition..."
207 insmod mtd-rw i_want_a_brick=1 >/dev/null 2>&1 || {
208 echo_err "Could not load 'mtd-rw' kernel module"
212 flags="$(cat /sys/class/mtd/$MTD_DEVICE/flags)"
213 [ "$((flags & rwfalg))" -gt "0" ] || {
214 echo_err "Could not unlock '/dev/$MTD_DEVICE' partition"
218 echo_ok "Partition '/dev/$MTD_DEVICE' successfully unlocked"
225 new_size="$(wc -c "$NEW_UBOOT_DIR/$NEW_UBOOT_FNAME" | awk '{print $1}')"
226 old_size="$(wc -c "/tmp/$MTD_BACKUP_FNAME" | awk '{print $1}')"
228 [ -n "$old_size" ] || [ -n "$new_size" ] || {
229 echo_err "Could not get size of new U-Boot image and/or backup file"
233 # Allow to use only images not bigger than mtd0 size
234 [ "$new_size" -gt "$old_size" ] && {
235 echo_err "New U-Boot image size ('$new_size' bytes) is bigger than '$MTD_DEVICE' partition size ('$old_size' bytes)"
239 dd if="$NEW_UBOOT_DIR/$NEW_UBOOT_FNAME" of="/tmp/$MTD_BACKUP_FNAME" conv=notrunc >/dev/null 2>&1 || {
240 echo_err "Could not combine new U-Boot image with backup file"
244 echo_ok "New U-Boot image successfully combined with backup file"
250 echo_info "New U-Boot image is ready to be written into FLASH"
252 wait_for_yesno "Do you want to continue" || return 1
254 # Erase mtd0 and write new image...
255 mtd -e "/dev/$MTD_DEVICE" write "/tmp/$MTD_BACKUP_FNAME" "/dev/$MTD_DEVICE" >/dev/null 2>&1 || {
256 echo_warn "Could not write new U-Boot image into FLASH"
258 newmd5="$(md5sum "/dev/$MTD_DEVICE" | awk '{print $1}')"
260 [ "$MTD_BACKUP_MD5" = "$newmd5" ] && {
261 echo_ok "Original '/dev/$MTD_DEVICE' partition content was not changed"
265 echo_err "FATAL ERROR: '/dev/$MTD_DEVICE' and old U-Boot image are not equal"
266 echo_err "DO NOT REBOOT OR POWER DOWN YOUR DEVICE NOW AND TRY AGAIN!"
270 echo_ok "New U-Boot image successfully written into FLASH"
272 # Verify MD5 of mtd0 and prepared image
273 mtd verify "/tmp/$MTD_BACKUP_FNAME" "/dev/$MTD_DEVICE" 2>&1 | grep -q "Success" || {
274 echo_err "FATAL ERROR: '/dev/$MTD_DEVICE' and new U-Boot image are not equal"
275 echo_err "DO NOT REBOOT OR POWER DOWN YOUR DEVICE NOW AND TRY AGAIN!"
279 echo_ok "'/dev/$MTD_DEVICE' and new U-Boot image are equal"
281 echo_info "Done! You can now reboot your device!"
285 #======================
286 # Execution begins here
287 #======================
290 find_new_uboot_file && \