Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 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 | |
| 15 | BASEDIR=sandbox |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 16 | SRCDIR=${BASEDIR}/boot |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 17 | IMAGE_NAME="v1.0-test" |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 18 | IMAGE_MULTI=linux.img |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 19 | IMAGE_FIT_ITS=linux.its |
| 20 | IMAGE_FIT_ITB=linux.itb |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 21 | DATAFILE0=vmlinuz |
| 22 | DATAFILE1=initrd.img |
| 23 | DATAFILE2=System.map |
| 24 | DATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}" |
| 25 | TEST_OUT=test_output |
| 26 | MKIMAGE=${BASEDIR}/tools/mkimage |
| 27 | DUMPIMAGE=${BASEDIR}/tools/dumpimage |
| 28 | MKIMAGE_LIST=mkimage.list |
| 29 | DUMPIMAGE_LIST=dumpimage.list |
| 30 | |
| 31 | # Remove all the files we created |
| 32 | cleanup() |
| 33 | { |
| 34 | local file |
| 35 | |
| 36 | for file in ${DATAFILES}; do |
| 37 | rm -f ${file} ${SRCDIR}/${file} |
| 38 | done |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 39 | rm -f ${IMAGE_MULTI} |
| 40 | rm -f ${DUMPIMAGE_LIST} |
| 41 | rm -f ${MKIMAGE_LIST} |
| 42 | rm -f ${TEST_OUT} |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 43 | rmdir ${SRCDIR} |
| 44 | } |
| 45 | |
| 46 | # Check that two files are the same |
| 47 | assert_equal() |
| 48 | { |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 49 | if ! diff -u $1 $2; then |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 50 | echo "Failed." |
| 51 | cleanup |
| 52 | exit 1 |
| 53 | fi |
| 54 | } |
| 55 | |
| 56 | # Create some test files |
| 57 | create_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 |
| 68 | do_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... |
| 80 | do_cmd_redir() |
| 81 | { |
| 82 | local redir="$1" |
| 83 | shift |
| 84 | local cmd="$@" |
| 85 | |
| 86 | echo "# ${cmd}" |
| 87 | ${cmd} >${redir} |
| 88 | } |
| 89 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 90 | # Write files into an multi-file image |
| 91 | create_multi_image() |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 92 | { |
| 93 | local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}" |
| 94 | files+=":${SRCDIR}/${DATAFILE2}" |
| 95 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 96 | echo -e "\nBuilding multi-file image..." |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 97 | do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \ |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 98 | -d ${files} ${IMAGE_MULTI} |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 99 | echo "done." |
| 100 | } |
| 101 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 102 | # Extract files from an multi-file image |
| 103 | extract_multi_image() |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 104 | { |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 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} |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 110 | echo "done." |
| 111 | } |
| 112 | |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 113 | # Write files into a FIT image |
| 114 | create_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 |
| 167 | extract_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 Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 177 | # List the contents of a file |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 178 | # Args: |
| 179 | # image filename |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 180 | list_image() |
| 181 | { |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 182 | local image="$1" |
| 183 | |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 184 | echo -e "\nListing image contents..." |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 185 | do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${image} |
| 186 | do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${image} |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 187 | echo "done." |
| 188 | } |
| 189 | |
| 190 | main() |
| 191 | { |
| 192 | local file |
| 193 | |
| 194 | create_files |
| 195 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 196 | # Compress and extract multi-file images, compare the result |
| 197 | create_multi_image |
| 198 | extract_multi_image |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 199 | for file in ${DATAFILES}; do |
| 200 | assert_equal ${file} ${SRCDIR}/${file} |
| 201 | done |
| 202 | assert_equal ${TEST_OUT} ${DATAFILE2} |
| 203 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 204 | # List contents of multi-file image and compares output from tools |
| 205 | list_image ${IMAGE_MULTI} |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 206 | assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} |
| 207 | |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 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 | |
Guilherme Maciel Ferreira | 6496d00 | 2013-12-01 12:43:12 -0700 | [diff] [blame] | 220 | # Remove files created |
| 221 | cleanup |
| 222 | |
| 223 | echo "Tests passed." |
| 224 | } |
| 225 | |
| 226 | main |