1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0+ 3# 4# Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> 5# 6# Sanity check for mkimage and dumpimage tools 7# 8# To run this: 9# 10# make O=sandbox sandbox_config 11# make O=sandbox 12# ./test/image/test-imagetools.sh 13 14BASEDIR=sandbox 15SRCDIR=${BASEDIR}/boot 16IMAGE_NAME="v1.0-test" 17IMAGE_MULTI=linux.img 18IMAGE_FIT_ITS=linux.its 19IMAGE_FIT_ITB=linux.itb 20DATAFILE0=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 38 rm -f ${IMAGE_MULTI} 39 rm -f ${DUMPIMAGE_LIST} 40 rm -f ${MKIMAGE_LIST} 41 rm -f ${TEST_OUT} 42 rmdir ${SRCDIR} 43} 44 45# Check that two files are the same 46assert_equal() 47{ 48 if ! diff -u $1 $2; then 49 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 89# Write files into an multi-file image 90create_multi_image() 91{ 92 local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}" 93 files+=":${SRCDIR}/${DATAFILE2}" 94 95 echo -e "\nBuilding multi-file image..." 96 do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \ 97 -d ${files} ${IMAGE_MULTI} 98 echo "done." 99} 100 101# Extract files from an multi-file image 102extract_multi_image() 103{ 104 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} 109 echo "done." 110} 111 112# 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 176# List the contents of a file 177# Args: 178# image filename 179list_image() 180{ 181 local image="$1" 182 183 echo -e "\nListing image contents..." 184 do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${image} 185 do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${image} 186 echo "done." 187} 188 189main() 190{ 191 local file 192 193 create_files 194 195 # Compress and extract multi-file images, compare the result 196 create_multi_image 197 extract_multi_image 198 for file in ${DATAFILES}; do 199 assert_equal ${file} ${SRCDIR}/${file} 200 done 201 assert_equal ${TEST_OUT} ${DATAFILE2} 202 203 # List contents of multi-file image and compares output from tools 204 list_image ${IMAGE_MULTI} 205 assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} 206 207 # 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 219 # Remove files created 220 cleanup 221 222 echo "Tests passed." 223} 224 225main 226