blob: 907f46a7b51e65830f5a0875a266d8930553cd65 [file] [log] [blame]
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -07001#!/bin/bash
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Guilherme Maciel Ferreirac5c0c792013-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 Ferreirac5c0c792013-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 Ferreira40bf5632015-01-15 02:54:40 -020015SRCDIR=${BASEDIR}/boot
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -070016IMAGE_NAME="v1.0-test"
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020017IMAGE_MULTI=linux.img
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -020018IMAGE_FIT_ITS=linux.its
19IMAGE_FIT_ITB=linux.itb
Guilherme Maciel Ferreirac5c0c792013-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 Ferreira3c46bcd2015-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 Ferreirac5c0c792013-12-01 12:43:12 -070042 rmdir ${SRCDIR}
43}
44
45# Check that two files are the same
46assert_equal()
47{
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020048 if ! diff -u $1 $2; then
Guilherme Maciel Ferreirac5c0c792013-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 Ferreira40bf5632015-01-15 02:54:40 -020089# Write files into an multi-file image
90create_multi_image()
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -070091{
92 local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}"
93 files+=":${SRCDIR}/${DATAFILE2}"
94
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020095 echo -e "\nBuilding multi-file image..."
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -070096 do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020097 -d ${files} ${IMAGE_MULTI}
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -070098 echo "done."
99}
100
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -0200101# Extract files from an multi-file image
102extract_multi_image()
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -0700103{
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -0200104 echo -e "\nExtracting multi-file image contents..."
Martyn Welchfb5370b2019-02-14 13:11:35 +0000105 do_cmd ${DUMPIMAGE} -T multi -p 0 -o ${DATAFILE0} ${IMAGE_MULTI}
106 do_cmd ${DUMPIMAGE} -T multi -p 1 -o ${DATAFILE1} ${IMAGE_MULTI}
107 do_cmd ${DUMPIMAGE} -T multi -p 2 -o ${DATAFILE2} ${IMAGE_MULTI}
108 do_cmd ${DUMPIMAGE} -T multi -p 2 -o ${TEST_OUT} ${IMAGE_MULTI}
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -0700109 echo "done."
110}
111
Guilherme Maciel Ferreira3c46bcd2015-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..."
Martyn Welchfb5370b2019-02-14 13:11:35 +0000169 do_cmd ${DUMPIMAGE} -T flat_dt -p 0 -o ${DATAFILE0} ${IMAGE_FIT_ITB}
170 do_cmd ${DUMPIMAGE} -T flat_dt -p 1 -o ${DATAFILE1} ${IMAGE_FIT_ITB}
171 do_cmd ${DUMPIMAGE} -T flat_dt -p 2 -o ${DATAFILE2} ${IMAGE_FIT_ITB}
172 do_cmd ${DUMPIMAGE} -T flat_dt -p 2 -o ${TEST_OUT} ${IMAGE_FIT_ITB}
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200173 echo "done."
174}
175
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -0700176# List the contents of a file
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -0200177# Args:
178# image filename
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -0700179list_image()
180{
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -0200181 local image="$1"
182
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -0700183 echo -e "\nListing image contents..."
Guilherme Maciel Ferreira40bf5632015-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 Ferreirac5c0c792013-12-01 12:43:12 -0700186 echo "done."
187}
188
189main()
190{
191 local file
192
193 create_files
194
Guilherme Maciel Ferreira40bf5632015-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 Ferreirac5c0c792013-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 Ferreira40bf5632015-01-15 02:54:40 -0200203 # List contents of multi-file image and compares output from tools
204 list_image ${IMAGE_MULTI}
Guilherme Maciel Ferreirac5c0c792013-12-01 12:43:12 -0700205 assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
206
Guilherme Maciel Ferreira3c46bcd2015-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 Ferreirac5c0c792013-12-01 12:43:12 -0700219 # Remove files created
220 cleanup
221
222 echo "Tests passed."
223}
224
225main