blob: 256af71d42ab19a2ab1843f0ec27fd07b316cc1d [file] [log] [blame]
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -07001#!/bin/bash
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -07003#
4# Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
5#
6# Sanity check for mkimage and dumpimage tools
7#
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -07008# To run this:
9#
10# make O=sandbox sandbox_config
11# make O=sandbox
12# ./test/image/test-imagetools.sh
13
14BASEDIR=sandbox
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020015SRCDIR=${BASEDIR}/boot
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070016IMAGE_NAME="v1.0-test"
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020017IMAGE_MULTI=linux.img
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -020018IMAGE_FIT_ITS=linux.its
19IMAGE_FIT_ITB=linux.itb
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070020DATAFILE0=vmlinuz
21DATAFILE1=initrd.img
22DATAFILE2=System.map
23DATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}"
24TEST_OUT=test_output
25MKIMAGE=${BASEDIR}/tools/mkimage
26DUMPIMAGE=${BASEDIR}/tools/dumpimage
27MKIMAGE_LIST=mkimage.list
28DUMPIMAGE_LIST=dumpimage.list
29
30# Remove all the files we created
31cleanup()
32{
33 local file
34
35 for file in ${DATAFILES}; do
36 rm -f ${file} ${SRCDIR}/${file}
37 done
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -020038 rm -f ${IMAGE_MULTI}
39 rm -f ${DUMPIMAGE_LIST}
40 rm -f ${MKIMAGE_LIST}
41 rm -f ${TEST_OUT}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070042 rmdir ${SRCDIR}
43}
44
45# Check that two files are the same
46assert_equal()
47{
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020048 if ! diff -u $1 $2; then
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070049 echo "Failed."
50 cleanup
51 exit 1
52 fi
53}
54
55# Create some test files
56create_files()
57{
58 local file
59
60 mkdir -p ${SRCDIR}
61 for file in ${DATAFILES}; do
62 head -c $RANDOM /dev/urandom >${SRCDIR}/${file}
63 done
64}
65
66# Run a command, echoing it first
67do_cmd()
68{
69 local cmd="$@"
70
71 echo "# ${cmd}"
72 ${cmd} 2>&1
73}
74
75# Run a command, redirecting output
76# Args:
77# redirect_file
78# command...
79do_cmd_redir()
80{
81 local redir="$1"
82 shift
83 local cmd="$@"
84
85 echo "# ${cmd}"
86 ${cmd} >${redir}
87}
88
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020089# Write files into an multi-file image
90create_multi_image()
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070091{
92 local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}"
93 files+=":${SRCDIR}/${DATAFILE2}"
94
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020095 echo -e "\nBuilding multi-file image..."
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070096 do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020097 -d ${files} ${IMAGE_MULTI}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070098 echo "done."
99}
100
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200101# Extract files from an multi-file image
102extract_multi_image()
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700103{
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200104 echo -e "\nExtracting multi-file image contents..."
105 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 0 ${DATAFILE0}
106 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 1 ${DATAFILE1}
107 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 2 ${DATAFILE2}
108 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 2 ${DATAFILE2} -o ${TEST_OUT}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700109 echo "done."
110}
111
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200112# Write files into a FIT image
113create_fit_image()
114{
115 echo " \
116 /dts-v1/; \
117 / { \
118 description = \"FIT image\"; \
119 #address-cells = <1>; \
120 \
121 images { \
122 kernel@1 { \
123 description = \"kernel\"; \
124 data = /incbin/(\"${DATAFILE0}\"); \
125 type = \"kernel\"; \
126 arch = \"sandbox\"; \
127 os = \"linux\"; \
128 compression = \"gzip\"; \
129 load = <0x40000>; \
130 entry = <0x8>; \
131 }; \
132 ramdisk@1 { \
133 description = \"filesystem\"; \
134 data = /incbin/(\"${DATAFILE1}\"); \
135 type = \"ramdisk\"; \
136 arch = \"sandbox\"; \
137 os = \"linux\"; \
138 compression = \"none\"; \
139 load = <0x80000>; \
140 entry = <0x16>; \
141 }; \
142 fdt@1 { \
143 description = \"device tree\"; \
144 data = /incbin/(\"${DATAFILE2}\"); \
145 type = \"flat_dt\"; \
146 arch = \"sandbox\"; \
147 compression = \"none\"; \
148 }; \
149 }; \
150 configurations { \
151 default = \"conf@1\"; \
152 conf@1 { \
153 kernel = \"kernel@1\"; \
154 fdt = \"fdt@1\"; \
155 }; \
156 }; \
157 }; \
158 " > ${IMAGE_FIT_ITS}
159
160 echo -e "\nBuilding FIT image..."
161 do_cmd ${MKIMAGE} -f ${IMAGE_FIT_ITS} ${IMAGE_FIT_ITB}
162 echo "done."
163}
164
165# Extract files from a FIT image
166extract_fit_image()
167{
168 echo -e "\nExtracting FIT image contents..."
169 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 0 ${DATAFILE0}
170 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 1 ${DATAFILE1}
171 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 2 ${DATAFILE2}
172 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 2 ${DATAFILE2} -o ${TEST_OUT}
173 echo "done."
174}
175
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700176# List the contents of a file
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200177# Args:
178# image filename
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700179list_image()
180{
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200181 local image="$1"
182
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700183 echo -e "\nListing image contents..."
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200184 do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${image}
185 do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${image}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700186 echo "done."
187}
188
189main()
190{
191 local file
192
193 create_files
194
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200195 # Compress and extract multi-file images, compare the result
196 create_multi_image
197 extract_multi_image
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700198 for file in ${DATAFILES}; do
199 assert_equal ${file} ${SRCDIR}/${file}
200 done
201 assert_equal ${TEST_OUT} ${DATAFILE2}
202
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200203 # List contents of multi-file image and compares output from tools
204 list_image ${IMAGE_MULTI}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700205 assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
206
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200207 # Compress and extract FIT images, compare the result
208 create_fit_image
209 extract_fit_image
210 for file in ${DATAFILES}; do
211 assert_equal ${file} ${SRCDIR}/${file}
212 done
213 assert_equal ${TEST_OUT} ${DATAFILE2}
214
215 # List contents of FIT image and compares output from tools
216 list_image ${IMAGE_FIT_ITB}
217 assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
218
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700219 # Remove files created
220 cleanup
221
222 echo "Tests passed."
223}
224
225main