-
Notifications
You must be signed in to change notification settings - Fork 5
/
buspirate.bash
executable file
·97 lines (78 loc) · 2.41 KB
/
buspirate.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Bus Pirate setup script
# Used for flashing and erasing Kinetis (SWD) devices
# NOTE: Only tested with a buspirate on Linux
# Expected exported variables
# TOOLCHAIN - Path to McHCK ruby programmer toolchain (all)
# CLASS - Verifies detected class with expected (all)
# FIRMWARE - Path to firmware image to flash (flash)
# ADDRESS - Starting address to flash firmware from (flash)
# Modes
# flash - Flashes given firmware
# erase - Erases chip
# NOTE: This requires a udev rule
# See: https://wiki.archlinux.org/index.php/Bus_pirate
BUSPIRATE=/dev/buspirate
# Convenience debug/error checker
debug() {
# Error occurred, show debug
if [ "$RETVAL" -ne "0" ]; then
echo ""
# Debug info
echo "TOOLCHAIN: '$TOOLCHAIN'"
echo "FIRMWARE: '$FIRMWARE'"
echo "ADDRESS: '$ADDRESS'"
echo "CWD: '$(pwd)'"
exit $RETVAL
fi
}
# Find actual directory of this script and enter it
ORIG_PATH=$(pwd)
FIRMWARE=${ORIG_PATH}/${FIRMWARE}
cd "$(dirname $(realpath "$0"))"
# First check for toolchain
if [ ! -d "${TOOLCHAIN}" ]; then
echo "ERROR: Could not find toolchain directory '${TOOLCHAIN}'"
exit 1
fi
# Check for a bus pirate
if [ ! -e "$BUSPIRATE" ]; then
echo "ERROR: Could not find a bus pirate '${BUSPIRATE}'"
exit 1
fi
# Detect which class of chip has been connected to the bus pirate
# Udev rules have been applied to name the buspirate as /dev/buspirate (instead of something like /dev/ttyUSB0)
# By default only root can access serial devices on Linux
if [ "${1}" != "erase" ]; then
DETECTED_CLASS=$(ruby "${TOOLCHAIN}"/flash.rb name=buspirate:dev=/dev/buspirate --detect)
RETVAL=$?
# Check if bus flashing cable is attached correctly
if [ "$RETVAL" -ne "0" ]; then
echo "ERROR: Cannot detect device. Check flashing cable. Possibly a soldering issue."
fi
debug
# Make sure the detected class matches the expected class
if [ "${DETECTED_CLASS}" != "${FAMILY}" ]; then
echo "ERROR: Invalid microcontroller detected: '${DETECTED_CLASS}'. Expected: '${FAMILY}'"
exit 5
fi
fi
# Check mode
case "$1" in
"flash")
# Attempt to flash
ruby "${TOOLCHAIN}"/flash.rb name=buspirate:dev=/dev/buspirate "$FIRMWARE" "$ADDRESS"
RETVAL=$?
sleep 0.5 # Wait for Device/USB to initialize before continuing
;;
"erase")
# Attempt to erase
ruby "${TOOLCHAIN}"/flash.rb name=buspirate:dev=/dev/buspirate --mass-erase
RETVAL=$?
;;
*)
echo "ERROR: '$1' is an invalid mode"
RETVAL=1
esac
debug
exit $RETVAL