2013-12-01 19:43:12 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
|
|
|
|
#
|
|
|
|
# Sanity check for mkimage and dumpimage tools
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
|
|
|
#
|
|
|
|
# To run this:
|
|
|
|
#
|
|
|
|
# make O=sandbox sandbox_config
|
|
|
|
# make O=sandbox
|
|
|
|
# ./test/image/test-imagetools.sh
|
|
|
|
|
|
|
|
BASEDIR=sandbox
|
2015-01-15 04:54:40 +00:00
|
|
|
SRCDIR=${BASEDIR}/boot
|
2013-12-01 19:43:12 +00:00
|
|
|
IMAGE_NAME="v1.0-test"
|
2015-01-15 04:54:40 +00:00
|
|
|
IMAGE_MULTI=linux.img
|
2015-01-15 04:54:42 +00:00
|
|
|
IMAGE_FIT_ITS=linux.its
|
|
|
|
IMAGE_FIT_ITB=linux.itb
|
2013-12-01 19:43:12 +00:00
|
|
|
DATAFILE0=vmlinuz
|
|
|
|
DATAFILE1=initrd.img
|
|
|
|
DATAFILE2=System.map
|
|
|
|
DATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}"
|
|
|
|
TEST_OUT=test_output
|
|
|
|
MKIMAGE=${BASEDIR}/tools/mkimage
|
|
|
|
DUMPIMAGE=${BASEDIR}/tools/dumpimage
|
|
|
|
MKIMAGE_LIST=mkimage.list
|
|
|
|
DUMPIMAGE_LIST=dumpimage.list
|
|
|
|
|
|
|
|
# Remove all the files we created
|
|
|
|
cleanup()
|
|
|
|
{
|
|
|
|
local file
|
|
|
|
|
|
|
|
for file in ${DATAFILES}; do
|
|
|
|
rm -f ${file} ${SRCDIR}/${file}
|
|
|
|
done
|
2015-01-15 04:54:42 +00:00
|
|
|
rm -f ${IMAGE_MULTI}
|
|
|
|
rm -f ${DUMPIMAGE_LIST}
|
|
|
|
rm -f ${MKIMAGE_LIST}
|
|
|
|
rm -f ${TEST_OUT}
|
2013-12-01 19:43:12 +00:00
|
|
|
rmdir ${SRCDIR}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check that two files are the same
|
|
|
|
assert_equal()
|
|
|
|
{
|
2015-01-15 04:54:40 +00:00
|
|
|
if ! diff -u $1 $2; then
|
2013-12-01 19:43:12 +00:00
|
|
|
echo "Failed."
|
|
|
|
cleanup
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create some test files
|
|
|
|
create_files()
|
|
|
|
{
|
|
|
|
local file
|
|
|
|
|
|
|
|
mkdir -p ${SRCDIR}
|
|
|
|
for file in ${DATAFILES}; do
|
|
|
|
head -c $RANDOM /dev/urandom >${SRCDIR}/${file}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run a command, echoing it first
|
|
|
|
do_cmd()
|
|
|
|
{
|
|
|
|
local cmd="$@"
|
|
|
|
|
|
|
|
echo "# ${cmd}"
|
|
|
|
${cmd} 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run a command, redirecting output
|
|
|
|
# Args:
|
|
|
|
# redirect_file
|
|
|
|
# command...
|
|
|
|
do_cmd_redir()
|
|
|
|
{
|
|
|
|
local redir="$1"
|
|
|
|
shift
|
|
|
|
local cmd="$@"
|
|
|
|
|
|
|
|
echo "# ${cmd}"
|
|
|
|
${cmd} >${redir}
|
|
|
|
}
|
|
|
|
|
2015-01-15 04:54:40 +00:00
|
|
|
# Write files into an multi-file image
|
|
|
|
create_multi_image()
|
2013-12-01 19:43:12 +00:00
|
|
|
{
|
|
|
|
local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}"
|
|
|
|
files+=":${SRCDIR}/${DATAFILE2}"
|
|
|
|
|
2015-01-15 04:54:40 +00:00
|
|
|
echo -e "\nBuilding multi-file image..."
|
2013-12-01 19:43:12 +00:00
|
|
|
do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \
|
2015-01-15 04:54:40 +00:00
|
|
|
-d ${files} ${IMAGE_MULTI}
|
2013-12-01 19:43:12 +00:00
|
|
|
echo "done."
|
|
|
|
}
|
|
|
|
|
2015-01-15 04:54:40 +00:00
|
|
|
# Extract files from an multi-file image
|
|
|
|
extract_multi_image()
|
2013-12-01 19:43:12 +00:00
|
|
|
{
|
2015-01-15 04:54:40 +00:00
|
|
|
echo -e "\nExtracting multi-file image contents..."
|
|
|
|
do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 0 ${DATAFILE0}
|
|
|
|
do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 1 ${DATAFILE1}
|
|
|
|
do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 2 ${DATAFILE2}
|
|
|
|
do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 2 ${DATAFILE2} -o ${TEST_OUT}
|
2013-12-01 19:43:12 +00:00
|
|
|
echo "done."
|
|
|
|
}
|
|
|
|
|
2015-01-15 04:54:42 +00:00
|
|
|
# Write files into a FIT image
|
|
|
|
create_fit_image()
|
|
|
|
{
|
|
|
|
echo " \
|
|
|
|
/dts-v1/; \
|
|
|
|
/ { \
|
|
|
|
description = \"FIT image\"; \
|
|
|
|
#address-cells = <1>; \
|
|
|
|
\
|
|
|
|
images { \
|
|
|
|
kernel@1 { \
|
|
|
|
description = \"kernel\"; \
|
|
|
|
data = /incbin/(\"${DATAFILE0}\"); \
|
|
|
|
type = \"kernel\"; \
|
|
|
|
arch = \"sandbox\"; \
|
|
|
|
os = \"linux\"; \
|
|
|
|
compression = \"gzip\"; \
|
|
|
|
load = <0x40000>; \
|
|
|
|
entry = <0x8>; \
|
|
|
|
}; \
|
|
|
|
ramdisk@1 { \
|
|
|
|
description = \"filesystem\"; \
|
|
|
|
data = /incbin/(\"${DATAFILE1}\"); \
|
|
|
|
type = \"ramdisk\"; \
|
|
|
|
arch = \"sandbox\"; \
|
|
|
|
os = \"linux\"; \
|
|
|
|
compression = \"none\"; \
|
|
|
|
load = <0x80000>; \
|
|
|
|
entry = <0x16>; \
|
|
|
|
}; \
|
|
|
|
fdt@1 { \
|
|
|
|
description = \"device tree\"; \
|
|
|
|
data = /incbin/(\"${DATAFILE2}\"); \
|
|
|
|
type = \"flat_dt\"; \
|
|
|
|
arch = \"sandbox\"; \
|
|
|
|
compression = \"none\"; \
|
|
|
|
}; \
|
|
|
|
}; \
|
|
|
|
configurations { \
|
|
|
|
default = \"conf@1\"; \
|
|
|
|
conf@1 { \
|
|
|
|
kernel = \"kernel@1\"; \
|
|
|
|
fdt = \"fdt@1\"; \
|
|
|
|
}; \
|
|
|
|
}; \
|
|
|
|
}; \
|
|
|
|
" > ${IMAGE_FIT_ITS}
|
|
|
|
|
|
|
|
echo -e "\nBuilding FIT image..."
|
|
|
|
do_cmd ${MKIMAGE} -f ${IMAGE_FIT_ITS} ${IMAGE_FIT_ITB}
|
|
|
|
echo "done."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Extract files from a FIT image
|
|
|
|
extract_fit_image()
|
|
|
|
{
|
|
|
|
echo -e "\nExtracting FIT image contents..."
|
|
|
|
do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 0 ${DATAFILE0}
|
|
|
|
do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 1 ${DATAFILE1}
|
|
|
|
do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 2 ${DATAFILE2}
|
|
|
|
do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 2 ${DATAFILE2} -o ${TEST_OUT}
|
|
|
|
echo "done."
|
|
|
|
}
|
|
|
|
|
2013-12-01 19:43:12 +00:00
|
|
|
# List the contents of a file
|
2015-01-15 04:54:40 +00:00
|
|
|
# Args:
|
|
|
|
# image filename
|
2013-12-01 19:43:12 +00:00
|
|
|
list_image()
|
|
|
|
{
|
2015-01-15 04:54:40 +00:00
|
|
|
local image="$1"
|
|
|
|
|
2013-12-01 19:43:12 +00:00
|
|
|
echo -e "\nListing image contents..."
|
2015-01-15 04:54:40 +00:00
|
|
|
do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${image}
|
|
|
|
do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${image}
|
2013-12-01 19:43:12 +00:00
|
|
|
echo "done."
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
local file
|
|
|
|
|
|
|
|
create_files
|
|
|
|
|
2015-01-15 04:54:40 +00:00
|
|
|
# Compress and extract multi-file images, compare the result
|
|
|
|
create_multi_image
|
|
|
|
extract_multi_image
|
2013-12-01 19:43:12 +00:00
|
|
|
for file in ${DATAFILES}; do
|
|
|
|
assert_equal ${file} ${SRCDIR}/${file}
|
|
|
|
done
|
|
|
|
assert_equal ${TEST_OUT} ${DATAFILE2}
|
|
|
|
|
2015-01-15 04:54:40 +00:00
|
|
|
# List contents of multi-file image and compares output from tools
|
|
|
|
list_image ${IMAGE_MULTI}
|
2013-12-01 19:43:12 +00:00
|
|
|
assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
|
|
|
|
|
2015-01-15 04:54:42 +00:00
|
|
|
# Compress and extract FIT images, compare the result
|
|
|
|
create_fit_image
|
|
|
|
extract_fit_image
|
|
|
|
for file in ${DATAFILES}; do
|
|
|
|
assert_equal ${file} ${SRCDIR}/${file}
|
|
|
|
done
|
|
|
|
assert_equal ${TEST_OUT} ${DATAFILE2}
|
|
|
|
|
|
|
|
# List contents of FIT image and compares output from tools
|
|
|
|
list_image ${IMAGE_FIT_ITB}
|
|
|
|
assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
|
|
|
|
|
2013-12-01 19:43:12 +00:00
|
|
|
# Remove files created
|
|
|
|
cleanup
|
|
|
|
|
|
|
|
echo "Tests passed."
|
|
|
|
}
|
|
|
|
|
|
|
|
main
|