1#!/bin/bash
2
3#
4# Copyright (C) 2007 Karel Zak <kzak@redhat.com>
5#
6# This file is part of util-linux.
7#
8# This file is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This file is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18TS_TOPDIR="${0%/*}/../.."
19TS_DESC="fsck bad header"
20
21. $TS_TOPDIR/functions.sh
22ts_init "$*"
23
24ts_check_test_command "$TS_CMD_MKCRAMFS"
25ts_check_test_command "$TS_CMD_FSCKCRAMFS"
26ts_check_prog "dd"
27
28function num2binary()
29{
30	local num=$1
31	local endian=$2
32
33	test "$num" -ge 0 -a "$num" -le 4294967295 || return 1
34	test "$endian" = "be" -o "$endian" = "le" || return 1
35
36	# how to do that easier?
37	if test "$endian" = "be"; then
38		echo -en "$(printf "%08x" "$1" | sed 's/\(..\)/\\x\1/g')"
39	else
40		echo -en "$(printf "%08x" "$1" | sed 's/^\(..\)\(..\)\(..\)\(..\)$/\\x\4\\x\3\\x\2\\x\1/')"
41	fi
42}
43
44function fsck_loop_sizes()
45{
46	local endian=$1  # be, le
47	local seek=$2    # 4 for nopad, 516 for pad
48	shift 2          # the rest are sizes to loop over
49
50	for size in "$@"; do
51		ts_log_both  "## size: $size"
52		cp -a "$IMAGE_FILE" "$IMAGE_FILE.tmp"
53		num2binary "$size" $endian |
54			dd of="$IMAGE_FILE.tmp" bs=1 seek="$seek" count=4 conv=notrunc &> /dev/null
55		$TS_CMD_FSCKCRAMFS "$IMAGE_FILE.tmp" >> $TS_OUTPUT 2>> $TS_ERRLOG
56		ts_log "ret: $?
57"
58	done
59	rm -f "$IMAGE_FILE"
60}
61
62
63IMAGE_SOURCE="$TS_OUTDIR/${TS_TESTNAME}-data"
64IMAGE_FILE="$TS_OUTDIR/${TS_TESTNAME}-cramfs.img"
65
66mkdir -p "${IMAGE_SOURCE}/subdir" &> /dev/null
67
68ts_init_subtest "nopad-4K-be"
69$TS_CMD_MKCRAMFS -N big -b 4096 $IMAGE_SOURCE $IMAGE_FILE &> /dev/null
70fsck_loop_sizes be 4  0 75 76 4095 4096 4097 4294967295
71rm -f "$IMAGE_FILE"
72ts_finalize_subtest
73
74ts_init_subtest "nopad-4K-le"
75$TS_CMD_MKCRAMFS -N little -b 4096 $IMAGE_SOURCE $IMAGE_FILE &> /dev/null
76fsck_loop_sizes le 4  0 75 76 4095 4096 4097 4294967295
77ts_finalize_subtest
78
79ts_init_subtest "pad-4K-be"
80$TS_CMD_MKCRAMFS -p -N big -b 4096 $IMAGE_SOURCE $IMAGE_FILE &> /dev/null
81fsck_loop_sizes be 516  76 587 588 4095 4096 4097 4294967295
82ts_finalize_subtest
83
84ts_init_subtest "pad-4K-le"
85$TS_CMD_MKCRAMFS -p -N little -b 4096 $IMAGE_SOURCE $IMAGE_FILE &> /dev/null
86fsck_loop_sizes le 516  76 587 588 4095 4096 4097 4294967295
87ts_finalize_subtest
88
89ts_init_subtest "pad-64K-be"
90$TS_CMD_MKCRAMFS -p -N big -b 65536 $IMAGE_SOURCE $IMAGE_FILE &> /dev/null
91fsck_loop_sizes be 516  76 587 588 65535 65536 65537 4294967295
92ts_finalize_subtest
93
94ts_init_subtest "pad-64K-le"
95$TS_CMD_MKCRAMFS -p -N little -b 65536 $IMAGE_SOURCE $IMAGE_FILE &> /dev/null
96fsck_loop_sizes le 516  76 587 588 65535 65536 65537 4294967295
97ts_finalize_subtest
98
99rm -rf "$IMAGE_SOURCE" "$IMAGE_FILE.tmp"
100
101ts_finalize
102
103