blob: 952f975af11ec9eeb5ff30c63381b80fc8fc7d77 [file] [log] [blame]
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -07001#!/bin/bash
2#
3# Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
4#
5# Sanity check for mkimage and dumpimage tools
6#
7# SPDX-License-Identifier: GPL-2.0+
8#
9# To run this:
10#
11# make O=sandbox sandbox_config
12# make O=sandbox
13# ./test/image/test-imagetools.sh
14
15BASEDIR=sandbox
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020016SRCDIR=${BASEDIR}/boot
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070017IMAGE_NAME="v1.0-test"
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020018IMAGE_MULTI=linux.img
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -020019IMAGE_FIT_ITS=linux.its
20IMAGE_FIT_ITB=linux.itb
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070021DATAFILE0=vmlinuz
22DATAFILE1=initrd.img
23DATAFILE2=System.map
24DATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}"
25TEST_OUT=test_output
26MKIMAGE=${BASEDIR}/tools/mkimage
27DUMPIMAGE=${BASEDIR}/tools/dumpimage
28MKIMAGE_LIST=mkimage.list
29DUMPIMAGE_LIST=dumpimage.list
30
31# Remove all the files we created
32cleanup()
33{
34 local file
35
36 for file in ${DATAFILES}; do
37 rm -f ${file} ${SRCDIR}/${file}
38 done
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -020039 rm -f ${IMAGE_MULTI}
40 rm -f ${DUMPIMAGE_LIST}
41 rm -f ${MKIMAGE_LIST}
42 rm -f ${TEST_OUT}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070043 rmdir ${SRCDIR}
44}
45
46# Check that two files are the same
47assert_equal()
48{
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020049 if ! diff -u $1 $2; then
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070050 echo "Failed."
51 cleanup
52 exit 1
53 fi
54}
55
56# Create some test files
57create_files()
58{
59 local file
60
61 mkdir -p ${SRCDIR}
62 for file in ${DATAFILES}; do
63 head -c $RANDOM /dev/urandom >${SRCDIR}/${file}
64 done
65}
66
67# Run a command, echoing it first
68do_cmd()
69{
70 local cmd="$@"
71
72 echo "# ${cmd}"
73 ${cmd} 2>&1
74}
75
76# Run a command, redirecting output
77# Args:
78# redirect_file
79# command...
80do_cmd_redir()
81{
82 local redir="$1"
83 shift
84 local cmd="$@"
85
86 echo "# ${cmd}"
87 ${cmd} >${redir}
88}
89
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020090# Write files into an multi-file image
91create_multi_image()
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070092{
93 local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}"
94 files+=":${SRCDIR}/${DATAFILE2}"
95
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020096 echo -e "\nBuilding multi-file image..."
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070097 do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020098 -d ${files} ${IMAGE_MULTI}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -070099 echo "done."
100}
101
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200102# Extract files from an multi-file image
103extract_multi_image()
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700104{
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200105 echo -e "\nExtracting multi-file image contents..."
106 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 0 ${DATAFILE0}
107 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 1 ${DATAFILE1}
108 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 2 ${DATAFILE2}
109 do_cmd ${DUMPIMAGE} -T multi -i ${IMAGE_MULTI} -p 2 ${DATAFILE2} -o ${TEST_OUT}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700110 echo "done."
111}
112
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200113# Write files into a FIT image
114create_fit_image()
115{
116 echo " \
117 /dts-v1/; \
118 / { \
119 description = \"FIT image\"; \
120 #address-cells = <1>; \
121 \
122 images { \
123 kernel@1 { \
124 description = \"kernel\"; \
125 data = /incbin/(\"${DATAFILE0}\"); \
126 type = \"kernel\"; \
127 arch = \"sandbox\"; \
128 os = \"linux\"; \
129 compression = \"gzip\"; \
130 load = <0x40000>; \
131 entry = <0x8>; \
132 }; \
133 ramdisk@1 { \
134 description = \"filesystem\"; \
135 data = /incbin/(\"${DATAFILE1}\"); \
136 type = \"ramdisk\"; \
137 arch = \"sandbox\"; \
138 os = \"linux\"; \
139 compression = \"none\"; \
140 load = <0x80000>; \
141 entry = <0x16>; \
142 }; \
143 fdt@1 { \
144 description = \"device tree\"; \
145 data = /incbin/(\"${DATAFILE2}\"); \
146 type = \"flat_dt\"; \
147 arch = \"sandbox\"; \
148 compression = \"none\"; \
149 }; \
150 }; \
151 configurations { \
152 default = \"conf@1\"; \
153 conf@1 { \
154 kernel = \"kernel@1\"; \
155 fdt = \"fdt@1\"; \
156 }; \
157 }; \
158 }; \
159 " > ${IMAGE_FIT_ITS}
160
161 echo -e "\nBuilding FIT image..."
162 do_cmd ${MKIMAGE} -f ${IMAGE_FIT_ITS} ${IMAGE_FIT_ITB}
163 echo "done."
164}
165
166# Extract files from a FIT image
167extract_fit_image()
168{
169 echo -e "\nExtracting FIT image contents..."
170 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 0 ${DATAFILE0}
171 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 1 ${DATAFILE1}
172 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 2 ${DATAFILE2}
173 do_cmd ${DUMPIMAGE} -T flat_dt -i ${IMAGE_FIT_ITB} -p 2 ${DATAFILE2} -o ${TEST_OUT}
174 echo "done."
175}
176
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700177# List the contents of a file
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200178# Args:
179# image filename
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700180list_image()
181{
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200182 local image="$1"
183
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700184 echo -e "\nListing image contents..."
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200185 do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${image}
186 do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${image}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700187 echo "done."
188}
189
190main()
191{
192 local file
193
194 create_files
195
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200196 # Compress and extract multi-file images, compare the result
197 create_multi_image
198 extract_multi_image
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700199 for file in ${DATAFILES}; do
200 assert_equal ${file} ${SRCDIR}/${file}
201 done
202 assert_equal ${TEST_OUT} ${DATAFILE2}
203
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200204 # List contents of multi-file image and compares output from tools
205 list_image ${IMAGE_MULTI}
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700206 assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
207
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200208 # Compress and extract FIT images, compare the result
209 create_fit_image
210 extract_fit_image
211 for file in ${DATAFILES}; do
212 assert_equal ${file} ${SRCDIR}/${file}
213 done
214 assert_equal ${TEST_OUT} ${DATAFILE2}
215
216 # List contents of FIT image and compares output from tools
217 list_image ${IMAGE_FIT_ITB}
218 assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST}
219
Guilherme Maciel Ferreira6496d002013-12-01 12:43:12 -0700220 # Remove files created
221 cleanup
222
223 echo "Tests passed."
224}
225
226main