1#!/bin/sh
2
3
4# Tests that the "require-path-at-offset" feature works. This
5# allows one to update firmware images based on where the filesystem is
6# mounted AND at the specified block offset.
7#
8
9. "$(cd "$(dirname "$0")" && pwd)/common.sh"
10
11case $HOST_OS in
12    Darwin|Windows)
13        # require-path-on-device not implemented due to not
14	# expecting it to be needed.
15        exit 77
16        ;;
17    FreeBSD|NetBSD|OpenBSD|DragonFly)
18        # find_rdev doesn't work due to stat calls.
19        exit 77
20        ;;
21    *)
22        ;;
23esac
24
25if [ "$CC" = "x86_64-w64-mingw32-gcc" -o "$MODE" = "windows" ]; then
26    # Try to detect the cross-compile for Windows case since
27    # this really doesn't make sense for this test.
28    exit 77
29fi
30
31# Figure out where the root filesystem is. Sadly, we can't
32# depend on Busybox's rdev, since it's not compiled into
33# Busybox on Linux.
34find_rdev() {
35    WANT=$(stat / -c "%04D")
36    FILES=$(find /dev 2>/dev/null || true)
37    for FILE in $FILES; do
38        GOT=$(stat $FILE -c "%02t%02T")
39        if [ $WANT = $GOT ]; then
40            echo $FILE
41            break
42        fi
43    done
44}
45RDEV=$(find_rdev)
46if [ -z $RDEV ]; then
47    echo "The root filesystem doesn't map to one device file, so can't test for rdev"
48    exit 77
49fi
50
51# Get the major and minor number in decimal
52RDEV_MAJOR=$(printf "%d" 0x$(stat $RDEV -c "%t"))
53RDEV_MINOR=$(printf "%d" 0x$(stat $RDEV -c "%T"))
54
55RDEV_START_FILE=/sys/dev/block/$RDEV_MAJOR:$RDEV_MINOR/start
56if [ ! -e "$RDEV_START_FILE" ]; then
57    echo "Couldn't open $RDEV_START_FILE so likely non-trivial rootfs. Skipping test"
58    exit 77
59fi
60
61# Get the start block offset
62RDEV_START=$(cat $RDEV_START_FILE)
63if [ "$RDEV_START" = "0" ]; then
64    echo "Didn't expect the start offset to be 0. That seems wrong, but it's possible so skipping this test"
65    echo 0
66fi
67
68cat >$CONFIG <<EOF
69task upgrade.zero {
70    require-path-at-offset("/", 0)
71    on-init { info("zero") }
72}
73task upgrade.right {
74    require-path-at-offset("/", "$RDEV_START")
75    on-init { info("correct") }
76}
77task upgrade.catchall {
78    on-init { info("catchall") }
79}
80EOF
81
82# Create the firmware file the normal way
83$FWUP_CREATE -c -f $CONFIG -o $FWFILE
84
85$FWUP_APPLY -a -q -d $IMGFILE -i $FWFILE -t upgrade > $WORK/actual_output.txt
86cat >$WORK/expected_output.txt <<EOF
87fwup: correct
88EOF
89diff -w $WORK/expected_output.txt $WORK/actual_output.txt
90
91# Check that the verify logic works on this file
92$FWUP_VERIFY -V -i $FWFILE
93