Setting up repository
[linux-libre-firmware.git] / b43-tools / assembler / selftest.sh
1 #!/bin/bash
2
3 ASM="b43-asm"
4 DASM="b43-dasm"
5 SUM="sha1sum"
6 TMPDIR="/tmp"
7
8
9 if [ $# -lt 1 ]; then
10         echo "b43-(d)asm trivial selftest"
11         echo "This selftest will take the binary input file, disassemble"
12         echo "it, assemble it and compare the two binaries."
13         echo
14         echo "Usage: $0 /path/to/binary [arch] [format]"
15         exit 1
16 fi
17 arch="5"
18 format="b43"
19 infile="$1" && shift
20 [ $# -eq 0 ] || arch="$1" && shift
21 [ $# -eq 0 ] || format="$1" && shift
22
23
24 if ! [ -r "$infile" ]; then
25         echo "Can not read input binary $infile"
26         exit 1
27 fi
28
29 rnd="$RANDOM"
30 asmfile="$TMPDIR/b43-asm-selftest-$rnd.asm"
31 outfile="$TMPDIR/b43-asm-selftest-$rnd.bin"
32
33 function cleanup
34 {
35         rm -f "$asmfile"
36         rm -f "$outfile"
37 }
38 cleanup
39
40
41 $DASM "$infile" "$asmfile" --arch $arch --format $format
42 err=$?
43 if [ $err -ne 0 ]; then
44         echo "Disassembling FAILED: $err"
45         cleanup
46         exit 1
47 fi
48 $ASM "$asmfile" "$outfile" --format $format
49 err=$?
50 if [ $err -ne 0 ]; then
51         echo "Assembling FAILED: $err"
52         cleanup
53         exit 1
54 fi
55
56 orig_sum="$($SUM "$infile" | cut -f1 -d ' ')"
57 err=$?
58 if [ $err -ne 0 ] || [ -z "$orig_sum" ]; then
59         echo "Checksumming of input file failed: $err"
60         cleanup
61         exit 1
62 fi
63 out_sum="$($SUM "$outfile" | cut -f1 -d ' ')"
64 err=$?
65 if [ $err -ne 0 ] || [ -z "$out_sum" ]; then
66         echo "Checksumming of reassembled file failed: $err"
67         cleanup
68         exit 1
69 fi
70 cleanup
71
72 echo "Input file checksum:    $orig_sum"
73 echo "Re-assembled checksum:  $out_sum"
74 echo
75
76 if [ "$orig_sum" != "$out_sum" ]; then
77         echo "FAILURE: Checksums don't match!"
78         exit 1
79 fi
80 echo "Checksums match"
81
82 exit 0