9443b9744542bc8a57ee953987550ce5225edb23
[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 -s -d 2
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 DO NOT POWER OFF OR DISCONNECT THE DEVICE WHILE THIS OPERATION IS IN PROGRESS!
48
49
50 Are you sure you want to flash '$file' onto the CEC adapter?
51 Type 'do it!' if you're sure. Anything else will cancel the operation.
52
53 EOB
54 read confirmation
55 if [ ! "$confirmation" == "do it!" ]; then
56 echo "Exiting"
57 exit 0
58 fi
59
60 _prereq
61 if [ $? -eq 1 ]; then
62 exit 1
63 fi
64
65 _check_bootloader_device
66 if [ $? -eq 1 ]; then
67 exit 1
68 fi
69
70
71 echo "Erasing the previous firmware"
72 sudo dfu-programmer at90usb162 erase
73
74 echo "Flashing the new firmware"
75 sudo dfu-programmer at90usb162 flash "$file"
76
77 cat << EOB
78
79 ===============================================================================
80
81 Done!
82
83 Remove the USB cable from the device and reconnect it to use the new firmware.
84
85 EOB
86 exit 0
87 }
88
89 _prereq()
90 {
91 programmer=`which dfu-programmer`
92 if [ -z "$programmer" ]; then
93 echo "dfu-programmer was not found in your path, installing"
94 sudo apt-get install -y dfu-programmer
95 fi
96
97 programmer=`which dfu-programmer`
98 if [ -z "$programmer" ]; then
99 echo "ERROR: failed to find dfu-programmer"
100 return 1
101 fi
102 return 0
103 }
104
105
106 cat << EOB
107 ===============================================================================
108 Pulse-Eight CEC Adapter firmware flash tool
109 ===============================================================================
110
111 EOB
112
113 if [ -z "$1" ]; then
114 _usage
115 else
116 _flash $1
117 fi
118
119 exit 0