How to Extract an uImage

About
This article provides a Linux script which extracts the data of an uImage, so it can be re-used for other compatible devices. A script is used instead of separate instructions, so that it can be re-used easily for several different uImages.

Extract an uImage
According to the U-Boot header definition an uImage begins with the hex byte sequence 27 05 19 56 and the header is 64 bytes long.

The data is directly following the header and could be in compressed form (gzip or bzip2), but this is untypical for embedded devices. It is normally a zImage (=compressed Linux kernel plus decompressor for low memory). To get the data only the uImage header has to be removed.

For ARM devices an additional 8-byte header with the machine type is in front of a zImage. In this case the additional 8 bytes have to be removed too.

extract_uImage.sh
The script also tries to determine if the data is a zImage, otherwise it assumes an Image. The script is also available here, but most not be up-to-date all the time.
 * 1) !/bin/sh


 * 1) Copyright (C) 2010 Matthias Buecher (http://www.maddes.net/)
 * 2) This program is free software; you can redistribute it and/or modify
 * 3) it under the terms of the GNU General Public License as published by
 * 4) the Free Software Foundation; either version 2 of the License, or
 * 5) (at your option) any later version.
 * 6) http://www.gnu.org/licenses/gpl-2.0.txt
 * 7) This program is distributed in the hope that it will be useful,
 * 8) but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 9) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 10) GNU General Public License for more details.
 * 11) You should have received a copy of the GNU General Public License
 * 12) along with this program; if not, write to the Free Software
 * 13) Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 1) You should have received a copy of the GNU General Public License
 * 2) along with this program; if not, write to the Free Software
 * 3) Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 1) Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

UIMAGE=$1

echo 'Checking for uImage magic word...' MAGIC=`dd if="${UIMAGE}" ibs=4 count=1 | hexdump -v -e '1/1 "%02X"'` [ '27051956' != "${MAGIC}" ] && { echo 'Not an uImage.' ; exit 1 ; }
 * 1) check for uImage magic word
 * 2) http://git.denx.de/cgi-bin/gitweb.cgi?p=u-boot.git;a=blob;f=include/image.h

echo 'uImage recognized.' echo 'Extracting data...' DATAFILE='uImage.data' dd if="${UIMAGE}" of="${DATAFILE}" ibs=64 skip=1
 * 1) extract data from uImage

echo 'Checking for ARM mach-type...' MAGIC=`dd if="${DATAFILE}" ibs=1 skip=1 count=3 | hexdump -v -e '1/1 "%02X"'` [ '1CA0E3' = "${MAGIC}" ] && { MAGIC=`dd if="${DATAFILE}" ibs=1 skip=5 count=3 | hexdump -v -e '1/1 "%02X"'` [ '1081E3' = "${MAGIC}" ] && { echo 'ARM mach-type header recognized.' echo 'Extracting mach-type header...' dd if="${DATAFILE}" of="uImage.mach-type" ibs=8 count=1 ARCH=$(hexdump -v -e '1/1 "%02X "' uImage.mach-type); echo "The mach-type is: $ARCH" echo 'Stripping mach-type header...' TMPFILE='uImage.tmp' dd if="${DATAFILE}" of="${TMPFILE}" ibs=8 skip=1 rm -f "${DATAFILE}" mv "${TMPFILE}" "${DATAFILE}" } }
 * 1) check for ARM mach type ( xx 1C A0 E3 xx 10 81 E3 )
 * 2) http://www.simtec.co.uk/products/SWLINUX/files/booting_article.html#d0e600

TMPFILE='Image' echo 'Checking for zImage...' MAGIC=`dd if="${DATAFILE}" ibs=4 skip=9 count=1 | hexdump -v -e '1/1 "%02X"'` [ '18286F01' = "${MAGIC}" ] && { START=`dd if="${DATAFILE}" ibs=4 skip=10 count=1 | hexdump -v -e '1/4 "%08X"'` END=`dd if="${DATAFILE}" ibs=4 skip=11 count=1 | hexdump -v -e '1/4 "%08X"'` SIZE=$(( 0x${END} - 0x${START} )) echo "zImage recognized with start 0x${START}, end 0x${END} and size ${SIZE}." TMPFILE='zImage' } mv "${DATAFILE}" "${TMPFILE}"
 * 1) check for zImage, otherwise assume Image
 * 2) http://www.simtec.co.uk/products/SWLINUX/files/booting_article.html#d0e309

echo ">>> ${UIMAGE} extracted to ${TMPFILE}"