cec: don't send an active source command when the physical address couldn't be set...
[deb_libcec.git] / support / cec-flash-device.sh
1 #!/bin/bash
2
3 _usage()
4 {
5 echo "Usage: $0 /path/to/firmware.hex"
6 }
7
8 _check_bootloader_device()
9 {
10 cec_adapter=`lsusb | grep "03eb:2ffa" | wc -l`
11 if [ $cec_adapter -eq 0 ]; then
12 _enter_bootloader
13 cec_adapter=`lsusb | grep "03eb:2ffa" | wc -l`
14 fi
15
16 if [ $cec_adapter -eq 0 ]; then
17 echo "ERROR: failed to find any CEC adapter in bootloader mode"
18 return 1
19 fi
20
21 return 0
22 }
23
24 _enter_bootloader()
25 {
26 echo "Instructing the CEC adapter to enter bootloader mode"
27 cec_adapter=`lsusb | grep "2548:1001" | wc -l`
28 if [ $cec_adapter -gt 0 ]; then
29 echo "bl" | cec-client --bootloader
30 echo "Waiting for the device to reinitialise"
31 sleep 5
32 fi
33 }
34
35 _flash()
36 {
37 file=$1
38
39 if [ ! -f "$file" ]; then
40 echo "ERROR: firmware file '$file' does not exist"
41 exit 1
42 fi
43
44 cat << EOB
45 Flash '$file' onto the CEC adapter
46
47 DISCONNECT THE HDMI CABLES BEFORE STARTING AND
48 DO NOT POWER OFF OR DISCONNECT THE DEVICE WHILE THIS OPERATION IS IN PROGRESS!
49
50
51 Are you sure you want to flash '$file' onto the CEC adapter?
52 Type 'do it!' if you're sure. Anything else will cancel the operation.
53
54 EOB
55 read confirmation
56 if [ ! "$confirmation" == "do it!" ]; then
57 echo "Exiting"
58 exit 0
59 fi
60
61 _prereq
62 if [ $? -eq 1 ]; then
63 exit 1
64 fi
65
66 _check_bootloader_device
67 if [ $? -eq 1 ]; then
68 exit 1
69 fi
70
71
72 echo "Erasing the previous firmware"
73 sudo dfu-programmer at90usb162 erase
74
75 echo "Flashing the new firmware"
76 sudo dfu-programmer at90usb162 flash "$file"
77
78 cat << EOB
79
80 ===============================================================================
81
82 Done!
83
84 Remove the USB cable from the device and reconnect it to use the new firmware.
85
86 EOB
87 exit 0
88 }
89
90 _prereq()
91 {
92 programmer=`which dfu-programmer`
93 if [ -z "$programmer" ]; then
94 echo "dfu-programmer was not found in your path, installing"
95 sudo apt-get install -y dfu-programmer
96 fi
97
98 programmer=`which dfu-programmer`
99 if [ -z "$programmer" ]; then
100 echo "ERROR: failed to find dfu-programmer"
101 return 1
102 fi
103 return 0
104 }
105
106
107 cat << EOB
108 ===============================================================================
109 Pulse-Eight CEC Adapter firmware flash tool
110 ===============================================================================
111
112 EOB
113
114 if [ -z "$1" ]; then
115 _usage
116 else
117 _flash $1
118 fi
119
120 exit 0