Guilherme Maciel Ferreira | c5c0c79 | 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 |
| 16 | SRCDIR=sandbox/boot |
| 17 | IMAGE_NAME="v1.0-test" |
| 18 | IMAGE=linux.img |
| 19 | DATAFILE0=vmlinuz |
| 20 | DATAFILE1=initrd.img |
| 21 | DATAFILE2=System.map |
| 22 | DATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}" |
| 23 | TEST_OUT=test_output |
| 24 | MKIMAGE=${BASEDIR}/tools/mkimage |
| 25 | DUMPIMAGE=${BASEDIR}/tools/dumpimage |
| 26 | MKIMAGE_LIST=mkimage.list |
| 27 | DUMPIMAGE_LIST=dumpimage.list |
| 28 | |
| 29 | # Remove all the files we created |
| 30 | cleanup() |
| 31 | { |
| 32 | local file |
| 33 | |
| 34 | for file in ${DATAFILES}; do |
| 35 | rm -f ${file} ${SRCDIR}/${file} |
| 36 | done |
| 37 | rm -f ${IMAGE} ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} ${TEST_OUT} |
| 38 | rmdir ${SRCDIR} |
| 39 | } |
| 40 | |
| 41 | # Check that two files are the same |
| 42 | assert_equal() |
| 43 | { |
| 44 | if ! diff $1 $2; then |
| 45 | echo "Failed." |
| 46 | cleanup |
| 47 | exit 1 |
| 48 | fi |
| 49 | } |
| 50 | |
| 51 | # Create some test files |
| 52 | create_files() |
| 53 | { |
| 54 | local file |
| 55 | |
| 56 | mkdir -p ${SRCDIR} |
| 57 | for file in ${DATAFILES}; do |
| 58 | head -c $RANDOM /dev/urandom >${SRCDIR}/${file} |
| 59 | done |
| 60 | } |
| 61 | |
| 62 | # Run a command, echoing it first |
| 63 | do_cmd() |
| 64 | { |
| 65 | local cmd="$@" |
| 66 | |
| 67 | echo "# ${cmd}" |
| 68 | ${cmd} 2>&1 |
| 69 | } |
| 70 | |
| 71 | # Run a command, redirecting output |
| 72 | # Args: |
| 73 | # redirect_file |
| 74 | # command... |
| 75 | do_cmd_redir() |
| 76 | { |
| 77 | local redir="$1" |
| 78 | shift |
| 79 | local cmd="$@" |
| 80 | |
| 81 | echo "# ${cmd}" |
| 82 | ${cmd} >${redir} |
| 83 | } |
| 84 | |
| 85 | # Write files into an image |
| 86 | create_image() |
| 87 | { |
| 88 | local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}" |
| 89 | files+=":${SRCDIR}/${DATAFILE2}" |
| 90 | |
| 91 | echo -e "\nBuilding image..." |
| 92 | do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \ |
| 93 | -d ${files} ${IMAGE} |
| 94 | echo "done." |
| 95 | } |
| 96 | |
| 97 | # Extract files from an image |
| 98 | extract_image() |
| 99 | { |
| 100 | echo -e "\nExtracting image contents..." |
| 101 | do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 0 ${DATAFILE0} |
| 102 | do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 1 ${DATAFILE1} |
| 103 | do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 2 ${DATAFILE2} |
| 104 | do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 2 ${DATAFILE2} -o ${TEST_OUT} |
| 105 | echo "done." |
| 106 | } |
| 107 | |
| 108 | # List the contents of a file |
| 109 | list_image() |
| 110 | { |
| 111 | echo -e "\nListing image contents..." |
| 112 | do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${IMAGE} |
| 113 | do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${IMAGE} |
| 114 | echo "done." |
| 115 | } |
| 116 | |
| 117 | main() |
| 118 | { |
| 119 | local file |
| 120 | |
| 121 | create_files |
| 122 | |
| 123 | # Compress and extract multifile images, compare the result |
| 124 | create_image |
| 125 | extract_image |
| 126 | for file in ${DATAFILES}; do |
| 127 | assert_equal ${file} ${SRCDIR}/${file} |
| 128 | done |
| 129 | assert_equal ${TEST_OUT} ${DATAFILE2} |
| 130 | |
| 131 | # List contents and compares output fro tools |
| 132 | list_image |
| 133 | assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} |
| 134 | |
| 135 | # Remove files created |
| 136 | cleanup |
| 137 | |
| 138 | echo "Tests passed." |
| 139 | } |
| 140 | |
| 141 | main |