16496d00fSGuilherme Maciel Ferreira#!/bin/bash
283d290c5STom Rini# SPDX-License-Identifier: GPL-2.0+
36496d00fSGuilherme Maciel Ferreira#
46496d00fSGuilherme Maciel Ferreira# Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
56496d00fSGuilherme Maciel Ferreira#
66496d00fSGuilherme Maciel Ferreira# Sanity check for mkimage and dumpimage tools
76496d00fSGuilherme Maciel Ferreira#
86496d00fSGuilherme Maciel Ferreira# To run this:
96496d00fSGuilherme Maciel Ferreira#
106496d00fSGuilherme Maciel Ferreira# make O=sandbox sandbox_config
116496d00fSGuilherme Maciel Ferreira# make O=sandbox
126496d00fSGuilherme Maciel Ferreira# ./test/image/test-imagetools.sh
136496d00fSGuilherme Maciel Ferreira
146496d00fSGuilherme Maciel FerreiraBASEDIR=sandbox
15f41f5b7cSGuilherme Maciel FerreiraSRCDIR=${BASEDIR}/boot
166496d00fSGuilherme Maciel FerreiraIMAGE_NAME="v1.0-test"
17f41f5b7cSGuilherme Maciel FerreiraIMAGE_MULTI=linux.img
1839931f96SGuilherme Maciel FerreiraIMAGE_FIT_ITS=linux.its
1939931f96SGuilherme Maciel FerreiraIMAGE_FIT_ITB=linux.itb
206496d00fSGuilherme Maciel FerreiraDATAFILE0=vmlinuz
216496d00fSGuilherme Maciel FerreiraDATAFILE1=initrd.img
226496d00fSGuilherme Maciel FerreiraDATAFILE2=System.map
236496d00fSGuilherme Maciel FerreiraDATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}"
246496d00fSGuilherme Maciel FerreiraTEST_OUT=test_output
256496d00fSGuilherme Maciel FerreiraMKIMAGE=${BASEDIR}/tools/mkimage
266496d00fSGuilherme Maciel FerreiraDUMPIMAGE=${BASEDIR}/tools/dumpimage
276496d00fSGuilherme Maciel FerreiraMKIMAGE_LIST=mkimage.list
286496d00fSGuilherme Maciel FerreiraDUMPIMAGE_LIST=dumpimage.list
296496d00fSGuilherme Maciel Ferreira
306496d00fSGuilherme Maciel Ferreira# Remove all the files we created
316496d00fSGuilherme Maciel Ferreiracleanup()
326496d00fSGuilherme Maciel Ferreira{
336496d00fSGuilherme Maciel Ferreira	local file
346496d00fSGuilherme Maciel Ferreira
356496d00fSGuilherme Maciel Ferreira	for file in ${DATAFILES}; do
366496d00fSGuilherme Maciel Ferreira		rm -f ${file} ${SRCDIR}/${file}
376496d00fSGuilherme Maciel Ferreira	done
3839931f96SGuilherme Maciel Ferreira	rm -f ${IMAGE_MULTI}
3939931f96SGuilherme Maciel Ferreira	rm -f ${DUMPIMAGE_LIST}
4039931f96SGuilherme Maciel Ferreira	rm -f ${MKIMAGE_LIST}
4139931f96SGuilherme Maciel Ferreira	rm -f ${TEST_OUT}
426496d00fSGuilherme Maciel Ferreira	rmdir ${SRCDIR}
436496d00fSGuilherme Maciel Ferreira}
446496d00fSGuilherme Maciel Ferreira
456496d00fSGuilherme Maciel Ferreira# Check that two files are the same
466496d00fSGuilherme Maciel Ferreiraassert_equal()
476496d00fSGuilherme Maciel Ferreira{
48f41f5b7cSGuilherme Maciel Ferreira	if ! diff -u $1 $2; then
496496d00fSGuilherme Maciel Ferreira		echo "Failed."
506496d00fSGuilherme Maciel Ferreira		cleanup
516496d00fSGuilherme Maciel Ferreira		exit 1
526496d00fSGuilherme Maciel Ferreira	fi
536496d00fSGuilherme Maciel Ferreira}
546496d00fSGuilherme Maciel Ferreira
556496d00fSGuilherme Maciel Ferreira# Create some test files
566496d00fSGuilherme Maciel Ferreiracreate_files()
576496d00fSGuilherme Maciel Ferreira{
586496d00fSGuilherme Maciel Ferreira	local file
596496d00fSGuilherme Maciel Ferreira
606496d00fSGuilherme Maciel Ferreira	mkdir -p ${SRCDIR}
616496d00fSGuilherme Maciel Ferreira	for file in ${DATAFILES}; do
626496d00fSGuilherme Maciel Ferreira		head -c $RANDOM /dev/urandom >${SRCDIR}/${file}
636496d00fSGuilherme Maciel Ferreira	done
646496d00fSGuilherme Maciel Ferreira}
656496d00fSGuilherme Maciel Ferreira
666496d00fSGuilherme Maciel Ferreira# Run a command, echoing it first
676496d00fSGuilherme Maciel Ferreirado_cmd()
686496d00fSGuilherme Maciel Ferreira{
696496d00fSGuilherme Maciel Ferreira	local cmd="$@"
706496d00fSGuilherme Maciel Ferreira
716496d00fSGuilherme Maciel Ferreira	echo "# ${cmd}"
726496d00fSGuilherme Maciel Ferreira	${cmd} 2>&1
736496d00fSGuilherme Maciel Ferreira}
746496d00fSGuilherme Maciel Ferreira
756496d00fSGuilherme Maciel Ferreira# Run a command, redirecting output
766496d00fSGuilherme Maciel Ferreira# Args:
776496d00fSGuilherme Maciel Ferreira#    redirect_file
786496d00fSGuilherme Maciel Ferreira#    command...
796496d00fSGuilherme Maciel Ferreirado_cmd_redir()
806496d00fSGuilherme Maciel Ferreira{
816496d00fSGuilherme Maciel Ferreira	local redir="$1"
826496d00fSGuilherme Maciel Ferreira	shift
836496d00fSGuilherme Maciel Ferreira	local cmd="$@"
846496d00fSGuilherme Maciel Ferreira
856496d00fSGuilherme Maciel Ferreira	echo "# ${cmd}"
866496d00fSGuilherme Maciel Ferreira	${cmd} >${redir}
876496d00fSGuilherme Maciel Ferreira}
886496d00fSGuilherme Maciel Ferreira
89f41f5b7cSGuilherme Maciel Ferreira# Write files into an multi-file image
90f41f5b7cSGuilherme Maciel Ferreiracreate_multi_image()
916496d00fSGuilherme Maciel Ferreira{
926496d00fSGuilherme Maciel Ferreira	local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}"
936496d00fSGuilherme Maciel Ferreira	files+=":${SRCDIR}/${DATAFILE2}"
946496d00fSGuilherme Maciel Ferreira
95f41f5b7cSGuilherme Maciel Ferreira	echo -e "\nBuilding multi-file image..."
966496d00fSGuilherme Maciel Ferreira	do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \
97f41f5b7cSGuilherme Maciel Ferreira		-d ${files} ${IMAGE_MULTI}
986496d00fSGuilherme Maciel Ferreira	echo "done."
996496d00fSGuilherme Maciel Ferreira}
1006496d00fSGuilherme Maciel Ferreira
101f41f5b7cSGuilherme Maciel Ferreira# Extract files from an multi-file image
102f41f5b7cSGuilherme Maciel Ferreiraextract_multi_image()
1036496d00fSGuilherme Maciel Ferreira{
104f41f5b7cSGuilherme Maciel Ferreira	echo -e "\nExtracting multi-file image contents..."
105*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T multi -p 0 -o ${DATAFILE0} ${IMAGE_MULTI}
106*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T multi -p 1 -o ${DATAFILE1} ${IMAGE_MULTI}
107*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T multi -p 2 -o ${DATAFILE2} ${IMAGE_MULTI}
108*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T multi -p 2 -o ${TEST_OUT} ${IMAGE_MULTI}
1096496d00fSGuilherme Maciel Ferreira	echo "done."
1106496d00fSGuilherme Maciel Ferreira}
1116496d00fSGuilherme Maciel Ferreira
11239931f96SGuilherme Maciel Ferreira# Write files into a FIT image
11339931f96SGuilherme Maciel Ferreiracreate_fit_image()
11439931f96SGuilherme Maciel Ferreira{
11539931f96SGuilherme Maciel Ferreira	echo " \
11639931f96SGuilherme Maciel Ferreira	/dts-v1/; \
11739931f96SGuilherme Maciel Ferreira	/ { \
11839931f96SGuilherme Maciel Ferreira	    description = \"FIT image\"; \
11939931f96SGuilherme Maciel Ferreira	    #address-cells = <1>; \
12039931f96SGuilherme Maciel Ferreira	\
12139931f96SGuilherme Maciel Ferreira	    images { \
12239931f96SGuilherme Maciel Ferreira	        kernel@1 { \
12339931f96SGuilherme Maciel Ferreira	            description = \"kernel\"; \
12439931f96SGuilherme Maciel Ferreira	            data = /incbin/(\"${DATAFILE0}\"); \
12539931f96SGuilherme Maciel Ferreira	            type = \"kernel\"; \
12639931f96SGuilherme Maciel Ferreira	            arch = \"sandbox\"; \
12739931f96SGuilherme Maciel Ferreira	            os = \"linux\"; \
12839931f96SGuilherme Maciel Ferreira	            compression = \"gzip\"; \
12939931f96SGuilherme Maciel Ferreira	            load = <0x40000>; \
13039931f96SGuilherme Maciel Ferreira	            entry = <0x8>; \
13139931f96SGuilherme Maciel Ferreira	        }; \
13239931f96SGuilherme Maciel Ferreira	        ramdisk@1 { \
13339931f96SGuilherme Maciel Ferreira	            description = \"filesystem\"; \
13439931f96SGuilherme Maciel Ferreira	            data = /incbin/(\"${DATAFILE1}\"); \
13539931f96SGuilherme Maciel Ferreira	            type = \"ramdisk\"; \
13639931f96SGuilherme Maciel Ferreira	            arch = \"sandbox\"; \
13739931f96SGuilherme Maciel Ferreira	            os = \"linux\"; \
13839931f96SGuilherme Maciel Ferreira	            compression = \"none\"; \
13939931f96SGuilherme Maciel Ferreira	            load = <0x80000>; \
14039931f96SGuilherme Maciel Ferreira	            entry = <0x16>; \
14139931f96SGuilherme Maciel Ferreira	        }; \
14239931f96SGuilherme Maciel Ferreira	        fdt@1 { \
14339931f96SGuilherme Maciel Ferreira	            description = \"device tree\"; \
14439931f96SGuilherme Maciel Ferreira	            data = /incbin/(\"${DATAFILE2}\"); \
14539931f96SGuilherme Maciel Ferreira	            type = \"flat_dt\"; \
14639931f96SGuilherme Maciel Ferreira	            arch = \"sandbox\"; \
14739931f96SGuilherme Maciel Ferreira	            compression = \"none\"; \
14839931f96SGuilherme Maciel Ferreira	        }; \
14939931f96SGuilherme Maciel Ferreira	    }; \
15039931f96SGuilherme Maciel Ferreira	    configurations { \
15139931f96SGuilherme Maciel Ferreira	        default = \"conf@1\"; \
15239931f96SGuilherme Maciel Ferreira	        conf@1 { \
15339931f96SGuilherme Maciel Ferreira	            kernel = \"kernel@1\"; \
15439931f96SGuilherme Maciel Ferreira	            fdt = \"fdt@1\"; \
15539931f96SGuilherme Maciel Ferreira	        }; \
15639931f96SGuilherme Maciel Ferreira	    }; \
15739931f96SGuilherme Maciel Ferreira	}; \
15839931f96SGuilherme Maciel Ferreira	" > ${IMAGE_FIT_ITS}
15939931f96SGuilherme Maciel Ferreira
16039931f96SGuilherme Maciel Ferreira	echo -e "\nBuilding FIT image..."
16139931f96SGuilherme Maciel Ferreira	do_cmd ${MKIMAGE} -f ${IMAGE_FIT_ITS} ${IMAGE_FIT_ITB}
16239931f96SGuilherme Maciel Ferreira	echo "done."
16339931f96SGuilherme Maciel Ferreira}
16439931f96SGuilherme Maciel Ferreira
16539931f96SGuilherme Maciel Ferreira# Extract files from a FIT image
16639931f96SGuilherme Maciel Ferreiraextract_fit_image()
16739931f96SGuilherme Maciel Ferreira{
16839931f96SGuilherme Maciel Ferreira	echo -e "\nExtracting FIT image contents..."
169*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T flat_dt -p 0 -o ${DATAFILE0} ${IMAGE_FIT_ITB}
170*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T flat_dt -p 1 -o ${DATAFILE1} ${IMAGE_FIT_ITB}
171*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T flat_dt -p 2 -o ${DATAFILE2} ${IMAGE_FIT_ITB}
172*280fafffSMartyn Welch	do_cmd ${DUMPIMAGE} -T flat_dt -p 2 -o ${TEST_OUT} ${IMAGE_FIT_ITB}
17339931f96SGuilherme Maciel Ferreira	echo "done."
17439931f96SGuilherme Maciel Ferreira}
17539931f96SGuilherme Maciel Ferreira
1766496d00fSGuilherme Maciel Ferreira# List the contents of a file
177f41f5b7cSGuilherme Maciel Ferreira# Args:
178f41f5b7cSGuilherme Maciel Ferreira#    image filename
1796496d00fSGuilherme Maciel Ferreiralist_image()
1806496d00fSGuilherme Maciel Ferreira{
181f41f5b7cSGuilherme Maciel Ferreira	local image="$1"
182f41f5b7cSGuilherme Maciel Ferreira
1836496d00fSGuilherme Maciel Ferreira	echo -e "\nListing image contents..."
184f41f5b7cSGuilherme Maciel Ferreira	do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${image}
185f41f5b7cSGuilherme Maciel Ferreira	do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${image}
1866496d00fSGuilherme Maciel Ferreira	echo "done."
1876496d00fSGuilherme Maciel Ferreira}
1886496d00fSGuilherme Maciel Ferreira
1896496d00fSGuilherme Maciel Ferreiramain()
1906496d00fSGuilherme Maciel Ferreira{
1916496d00fSGuilherme Maciel Ferreira	local file
1926496d00fSGuilherme Maciel Ferreira
1936496d00fSGuilherme Maciel Ferreira	create_files
1946496d00fSGuilherme Maciel Ferreira
195f41f5b7cSGuilherme Maciel Ferreira	# Compress and extract multi-file images, compare the result
196f41f5b7cSGuilherme Maciel Ferreira	create_multi_image
197f41f5b7cSGuilherme Maciel Ferreira	extract_multi_image
1986496d00fSGuilherme Maciel Ferreira	for file in ${DATAFILES}; do
1996496d00fSGuilherme Maciel Ferreira		assert_equal ${file} ${SRCDIR}/${file}
2006496d00fSGuilherme Maciel Ferreira	done
2016496d00fSGuilherme Maciel Ferreira	assert_equal ${TEST_OUT} ${DATAFILE2}
2026496d00fSGuilherme Maciel Ferreira
203f41f5b7cSGuilherme Maciel Ferreira	# List contents of multi-file image and compares output from tools
204f41f5b7cSGuilherme Maciel Ferreira	list_image ${IMAGE_MULTI}
2056496d00fSGuilherme Maciel Ferreira	assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
2066496d00fSGuilherme Maciel Ferreira
20739931f96SGuilherme Maciel Ferreira	# Compress and extract FIT images, compare the result
20839931f96SGuilherme Maciel Ferreira	create_fit_image
20939931f96SGuilherme Maciel Ferreira	extract_fit_image
21039931f96SGuilherme Maciel Ferreira	for file in ${DATAFILES}; do
21139931f96SGuilherme Maciel Ferreira		assert_equal ${file} ${SRCDIR}/${file}
21239931f96SGuilherme Maciel Ferreira	done
21339931f96SGuilherme Maciel Ferreira	assert_equal ${TEST_OUT} ${DATAFILE2}
21439931f96SGuilherme Maciel Ferreira
21539931f96SGuilherme Maciel Ferreira	# List contents of FIT image and compares output from tools
21639931f96SGuilherme Maciel Ferreira	list_image ${IMAGE_FIT_ITB}
21739931f96SGuilherme Maciel Ferreira	assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
21839931f96SGuilherme Maciel Ferreira
2196496d00fSGuilherme Maciel Ferreira	# Remove files created
2206496d00fSGuilherme Maciel Ferreira	cleanup
2216496d00fSGuilherme Maciel Ferreira
2226496d00fSGuilherme Maciel Ferreira	echo "Tests passed."
2236496d00fSGuilherme Maciel Ferreira}
2246496d00fSGuilherme Maciel Ferreira
2256496d00fSGuilherme Maciel Ferreiramain
226