xref: /freebsd/usr.bin/mkimg/tests/mkimg_test.sh (revision 3ac62888fc211e92785751c9bb6e73868101f65c)
1458cbf0aSEnji Cooper# $FreeBSD$
2458cbf0aSEnji Cooper
3458cbf0aSEnji Coopermkimg_blksz_list="512 4096"
4c78cd98bSOleksandr Tymoshenkomkimg_format_list="qcow qcow2 raw vhd vhdf vhdx vmdk"
5458cbf0aSEnji Coopermkimg_geom_list="1x1 63x255"
6458cbf0aSEnji Coopermkimg_scheme_list="apm bsd ebr gpt mbr vtoc8"
7458cbf0aSEnji Cooper
8458cbf0aSEnji Cooperbootcode()
9458cbf0aSEnji Cooper{
10458cbf0aSEnji Cooper    case $1 in
11458cbf0aSEnji Cooper      bsd)	echo 8192 ;;
12458cbf0aSEnji Cooper      gpt|mbr)	echo 512 ;;
13458cbf0aSEnji Cooper      *)	echo 0 ;;
14458cbf0aSEnji Cooper    esac
15458cbf0aSEnji Cooper    return 0
16458cbf0aSEnji Cooper}
17458cbf0aSEnji Cooper
18458cbf0aSEnji Coopermkcontents()
19458cbf0aSEnji Cooper{
20458cbf0aSEnji Cooper    local byte count name
21458cbf0aSEnji Cooper
22458cbf0aSEnji Cooper    byte=$1
23458cbf0aSEnji Cooper    count=$2
24458cbf0aSEnji Cooper
25458cbf0aSEnji Cooper    name=_tmp-$byte-$count.bin
26458cbf0aSEnji Cooper    jot -b $byte $(($count/2)) > $name
27458cbf0aSEnji Cooper    echo $name
28458cbf0aSEnji Cooper    return 0
29458cbf0aSEnji Cooper}
30458cbf0aSEnji Cooper
31458cbf0aSEnji Coopermakeimage()
32458cbf0aSEnji Cooper{
33458cbf0aSEnji Cooper    local blksz bootarg bootsz format geom nhds nsecs partarg pfx scheme
34458cbf0aSEnji Cooper
35458cbf0aSEnji Cooper    format=$1
36458cbf0aSEnji Cooper    scheme=$2
37458cbf0aSEnji Cooper    blksz=$3
38458cbf0aSEnji Cooper    geom=$4
39458cbf0aSEnji Cooper    pfx=$5
40458cbf0aSEnji Cooper    shift 5
41458cbf0aSEnji Cooper
42458cbf0aSEnji Cooper    nsecs=${geom%x*}
43458cbf0aSEnji Cooper    nhds=${geom#*x}
44458cbf0aSEnji Cooper
45458cbf0aSEnji Cooper    bootsz=`bootcode $scheme`
46458cbf0aSEnji Cooper    if test $bootsz -gt 0; then
47458cbf0aSEnji Cooper	bootarg="-b `mkcontents B $bootsz`"
48458cbf0aSEnji Cooper    else
49458cbf0aSEnji Cooper	bootarg=""
50458cbf0aSEnji Cooper    fi
51458cbf0aSEnji Cooper
52458cbf0aSEnji Cooper    partarg=""
53458cbf0aSEnji Cooper    for P in $*; do
54458cbf0aSEnji Cooper	partarg="$partarg -p $P"
55458cbf0aSEnji Cooper    done
56458cbf0aSEnji Cooper    if test -z "$partarg"; then
57458cbf0aSEnji Cooper	local swap ufs
58458cbf0aSEnji Cooper	swap="-p freebsd-swap::128K"
59*3ac62888SAlex Richardson	ufs="-p freebsd-ufs:=$(atf_get_srcdir)/partition_data_4M.bin"
60458cbf0aSEnji Cooper	partarg="$ufs $swap"
61458cbf0aSEnji Cooper    fi
62458cbf0aSEnji Cooper
63458cbf0aSEnji Cooper    imagename=$pfx-$geom-$blksz-$scheme.$format
64458cbf0aSEnji Cooper
65458cbf0aSEnji Cooper    mkimg -y -f $format -o $imagename -s $scheme -P $blksz -H $nhds -T $nsecs \
66458cbf0aSEnji Cooper	    $bootarg $partarg
67458cbf0aSEnji Cooper    echo $imagename
68458cbf0aSEnji Cooper    return 0
69458cbf0aSEnji Cooper}
70458cbf0aSEnji Cooper
71458cbf0aSEnji Coopermkimg_rebase()
72458cbf0aSEnji Cooper{
73458cbf0aSEnji Cooper    local baseline image result tmpfile update
74458cbf0aSEnji Cooper
75458cbf0aSEnji Cooper    image=$1
76458cbf0aSEnji Cooper    result=$2
77458cbf0aSEnji Cooper
78370d612fSMarcel Moolenaar    baseline=$image.hex
79458cbf0aSEnji Cooper    update=yes
80458cbf0aSEnji Cooper
81458cbf0aSEnji Cooper    if test -f $baseline; then
82458cbf0aSEnji Cooper	tmpfile=_tmp-baseline
83370d612fSMarcel Moolenaar	sed -e '/^#.*/D' < $baseline > $tmpfile
84370d612fSMarcel Moolenaar	if diff -u $tmpfile $result; then
85458cbf0aSEnji Cooper	    update=no
86458cbf0aSEnji Cooper	fi
87458cbf0aSEnji Cooper    fi
88458cbf0aSEnji Cooper
89458cbf0aSEnji Cooper    if test $update = yes; then
90458cbf0aSEnji Cooper	# Prevent keyword expansion when writing the keyword.
91458cbf0aSEnji Cooper	(echo -n '# $'; echo -n FreeBSD; echo '$') > $baseline
92370d612fSMarcel Moolenaar	cat $result >> $baseline
93458cbf0aSEnji Cooper    fi
94458cbf0aSEnji Cooper
95458cbf0aSEnji Cooper    rm $image $result _tmp-*
96458cbf0aSEnji Cooper    return 0
97458cbf0aSEnji Cooper}
98458cbf0aSEnji Cooper
99458cbf0aSEnji Coopermkimg_test()
100458cbf0aSEnji Cooper{
101458cbf0aSEnji Cooper    local blksz format geom scheme
102458cbf0aSEnji Cooper
103458cbf0aSEnji Cooper    geom=$1
104458cbf0aSEnji Cooper    blksz=$2
105458cbf0aSEnji Cooper    scheme=$3
106458cbf0aSEnji Cooper    format=$4
107458cbf0aSEnji Cooper
108458cbf0aSEnji Cooper    case $scheme in
109458cbf0aSEnji Cooper      ebr|mbr)
110458cbf0aSEnji Cooper	bsd=`makeimage raw bsd $blksz $geom _tmp`
111458cbf0aSEnji Cooper	partinfo="freebsd:=$bsd"
112458cbf0aSEnji Cooper	;;
113458cbf0aSEnji Cooper      *)
114458cbf0aSEnji Cooper	partinfo=""
115458cbf0aSEnji Cooper	;;
116458cbf0aSEnji Cooper    esac
117458cbf0aSEnji Cooper    image=`makeimage $format $scheme $blksz $geom img $partinfo`
118458cbf0aSEnji Cooper    result=$image.out
119458cbf0aSEnji Cooper    hexdump -C $image > $result
120458cbf0aSEnji Cooper    if test "x$mkimg_update_baseline" = "xyes"; then
121458cbf0aSEnji Cooper	mkimg_rebase $image $result
122458cbf0aSEnji Cooper    else
123458cbf0aSEnji Cooper	baseline=`atf_get_srcdir`/$image
124370d612fSMarcel Moolenaar	atf_check -s exit:0 diff -u $baseline $result
125458cbf0aSEnji Cooper    fi
126458cbf0aSEnji Cooper    return 0
127458cbf0aSEnji Cooper}
128458cbf0aSEnji Cooper
129458cbf0aSEnji Cooperatf_test_case rebase
130458cbf0aSEnji Cooperrebase_body()
131458cbf0aSEnji Cooper{
132458cbf0aSEnji Cooper    local nm
133458cbf0aSEnji Cooper
134458cbf0aSEnji Cooper    mkimg_update_baseline=yes
135458cbf0aSEnji Cooper    for nm in $mkimg_tests; do
136458cbf0aSEnji Cooper	${nm}_body
137458cbf0aSEnji Cooper    done
138458cbf0aSEnji Cooper    return 0
139458cbf0aSEnji Cooper}
140458cbf0aSEnji Cooper
141458cbf0aSEnji Cooperatf_init_test_cases()
142458cbf0aSEnji Cooper{
143458cbf0aSEnji Cooper    local B F G S nm
144458cbf0aSEnji Cooper
145458cbf0aSEnji Cooper    for G in $mkimg_geom_list; do
146458cbf0aSEnji Cooper	for B in $mkimg_blksz_list; do
147458cbf0aSEnji Cooper	    for S in $mkimg_scheme_list; do
148458cbf0aSEnji Cooper		for F in $mkimg_format_list; do
149458cbf0aSEnji Cooper		    nm="${S}_${G}_${B}_${F}"
150458cbf0aSEnji Cooper		    atf_test_case $nm
151458cbf0aSEnji Cooper		    eval "${nm}_body() { mkimg_test $G $B $S $F; }"
152458cbf0aSEnji Cooper		    mkimg_tests="${mkimg_tests} ${nm}"
153458cbf0aSEnji Cooper		    atf_add_test_case $nm
154458cbf0aSEnji Cooper		done
155458cbf0aSEnji Cooper	    done
156458cbf0aSEnji Cooper	done
157458cbf0aSEnji Cooper    done
158458cbf0aSEnji Cooper
159458cbf0aSEnji Cooper    # XXX hack to make updating the baseline easier
160458cbf0aSEnji Cooper    if test "${__RUNNING_INSIDE_ATF_RUN}" != "internal-yes-value"; then
161458cbf0aSEnji Cooper	atf_add_test_case rebase
162458cbf0aSEnji Cooper    fi
163458cbf0aSEnji Cooper}
164458cbf0aSEnji Cooper
165