1#!/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 16SRCDIR=${BASEDIR}/boot 17IMAGE_NAME="v1.0-test" 18IMAGE_MULTI=linux.img 19IMAGE_FIT_ITS=linux.its 20IMAGE_FIT_ITB=linux.itb 21DATAFILE0=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 39 rm -f ${IMAGE_MULTI} 40 rm -f ${DUMPIMAGE_LIST} 41 rm -f ${MKIMAGE_LIST} 42 rm -f ${TEST_OUT} 43 rmdir ${SRCDIR} 44} 45 46# Check that two files are the same 47assert_equal() 48{ 49 if ! diff -u $1 $2; then 50 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 90# Write files into an multi-file image 91create_multi_image() 92{ 93 local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}" 94 files+=":${SRCDIR}/${DATAFILE2}" 95 96 echo -e "\nBuilding multi-file image..." 97 do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \ 98 -d ${files} ${IMAGE_MULTI} 99 echo "done." 100} 101 102# Extract files from an multi-file image 103extract_multi_image() 104{ 105 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} 110 echo "done." 111} 112 113# 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 177# List the contents of a file 178# Args: 179# image filename 180list_image() 181{ 182 local image="$1" 183 184 echo -e "\nListing image contents..." 185 do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${image} 186 do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${image} 187 echo "done." 188} 189 190main() 191{ 192 local file 193 194 create_files 195 196 # Compress and extract multi-file images, compare the result 197 create_multi_image 198 extract_multi_image 199 for file in ${DATAFILES}; do 200 assert_equal ${file} ${SRCDIR}/${file} 201 done 202 assert_equal ${TEST_OUT} ${DATAFILE2} 203 204 # List contents of multi-file image and compares output from tools 205 list_image ${IMAGE_MULTI} 206 assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} 207 208 # 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 220 # Remove files created 221 cleanup 222 223 echo "Tests passed." 224} 225 226main 227