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