5 # Copyright 2008, 2009, 2010 Fred Cooke
7 # This file is part of the FreeEMS project.
9 # FreeEMS software is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # FreeEMS software is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
22 # We ask that if you make any changes to this file you send them upstream to us at admin@diyefi.org
24 # Thank you for choosing FreeEMS to run your engine!
27 # Set up the variables
28 export EXPECTED_ARGS
="2"
29 export NUMBER_OF_ARGS
="$#"
30 export SERIAL_DEVICE
="$1"
31 export FIRMWARE_S19_PATH
="$2"
32 export SERIAL_BAUD
="115200"
33 export XDP512
="mc9s12xdp512"
34 export XEP100
="mc9s12xep100"
35 export DEVICE_TYPE
=$XDP512
36 export LOAD_TOOL
=`which hcs12mem`
37 export HCS12MEM_PATH
="/usr/local/share/hcs12mem"
38 #export ADDRESS_TYPE="non-banked"
39 export ADDRESS_TYPE
="banked-ppage"
40 #export ADDRESS_TYPE="banked-linear"
41 # Address type banked linear should be used WITHOUT lma/ppage references in the objcopy section of the Makefile
44 export TEMP_BASE
="/tmp/"`date +date_%F_time_%Hh%Mm%Ss_%p`
45 export TEMP_S19_COPY
=$TEMP_BASE".freeEMS.firmware.copy.s19"
46 export TEMP_S19_DIFF
=$TEMP_BASE".freeEMS.firmware.s19.diff"
49 export GOODBYE
="Thank you for choosing the FreeEMS to run your engine, goodbye."
50 export USAGE
="Usage : freeEMSloader.bash /dev/yourTTY new-firmware.s19"
52 # Formalise the error codes
53 export WRONG_EMS
="-10"
55 export WRONG_NUMBER_OF_ARGS
="-20"
56 export SERIAL_DEVICE_DOES_NOT_EXIST
="-21"
57 export SERIAL_DEVICE_NOT_READABLE
="-22"
58 export SERIAL_DEVICE_NOT_WRITABLE
="-23"
59 export SERIAL_DEVICE_NOT_CHARACTER_DEVICE
="-24"
61 export S19_FILE_DOES_NOT_EXIST
="-30"
62 export S19_FILE_NOT_READABLE
="-31"
63 export S19_FILE_NOT_REGULAR_FILE
="-32"
65 export HCS12MEM_EXECUTABLE_DOES_NOT_EXIST
="-40"
66 export HCS12MEM_EXECUTABLE_NOT_READABLE
="-41"
67 export HCS12MEM_EXECUTABLE_NOT_EXECUTABLE
="-42"
68 export HCS12MEM_DAT_NOT_FOUND
="-43"
70 export ABORT_PRE_ERASE
="-50"
71 export ERASE_FAILED
="-51"
73 export ABORT_PRE_LOAD
="-60"
74 export LOAD_FAILED
="-61"
76 export VERIFY_FAILED
="-70"
77 export ABORT_PRE_DELETE_TMP
="-71"
78 export EXIT_WITHOUT_CLEANING_UP
="-72"
81 # continue() takes three arguments : affirmative meaning continue, negative meaning exit, and exit code
88 if [ "$ANSWER" == "$1" ]
92 elif [ "$ANSWER" == "$2" ]
97 echo "'$ANSWER' not recognised, $1 and $2 are valid, please re-enter"
103 # Display splash screen
106 echo "Welcome to the FreeEMS firmware loader"
108 echo "This program is released under the GPL V3"
109 echo "Please see http://www.gnu.org/licenses/ for details"
111 echo "This version is for $DEVICE_TYPE based FreeEMS's"
113 # echo "Do you have a $DEVICE_TYPE based FreeEMS? [ yes | quit ]"
115 # continue "yes" "quit" "$WRONG_EMS"
119 # Ensure all is in order
122 echo "Checking configuration and arguments..."
124 # Test for the correct number of arguments
125 if [ $EXPECTED_ARGS -ne $NUMBER_OF_ARGS ]
128 echo "Expected $EXPECTED_ARGS parameter(s), got $NUMBER_OF_ARGS, exiting..."
129 exit "$WRONG_NUMBER_OF_ARGS"
131 # Ensure serial device exists
132 elif [ ! -e "$SERIAL_DEVICE" ]
134 echo "Serial device $SERIAL_DEVICE does NOT exist! exiting..."
135 exit "$SERIAL_DEVICE_DOES_NOT_EXIST"
137 # Ensure serial device is readable
138 elif [ ! -r "$SERIAL_DEVICE" ]
140 echo "Serial device $SERIAL_DEVICE is NOT readable! exiting..."
141 exit "$SERIAL_DEVICE_NOT_READABLE"
143 # Ensure serial device is writable
144 elif [ ! -w "$SERIAL_DEVICE" ]
146 echo "Serial device $SERIAL_DEVICE is NOT writable! exiting..."
147 exit "$SERIAL_DEVICE_NOT_WRITABLE"
149 # Ensure serial device is a character device or symlink
150 elif [ ! -c "$SERIAL_DEVICE" ] && [ ! -L "$SERIAL_DEVICE" ]
152 echo "Serial device $SERIAL_DEVICE is NOT a valid character device or symlink! exiting..."
153 exit "$SERIAL_DEVICE_NOT_CHARACTER_DEVICE"
155 # Ensure the s19 file exists
156 elif [ ! -e "$FIRMWARE_S19_PATH" ]
158 echo "S19 file $FIRMWARE_S19_PATH does NOT exist! exiting..."
159 exit "$S19_FILE_DOES_NOT_EXIST"
161 # Ensure the s19 is readable
162 elif [ ! -r "$FIRMWARE_S19_PATH" ]
164 echo "S19 file $FIRMWARE_S19_PATH is NOT readable! exiting..."
165 exit "$S19_FILE_NOT_READABLE"
167 # Ensure the s19 is a regular file or symlink
168 elif [ ! -f "$FIRMWARE_S19_PATH" ] && [ ! -L "$FIRMWARE_S19_PATH" ]
170 echo "S19 file $FIRMWARE_S19_PATH is NOT a regular file or symlink! exiting..."
171 exit "$S19_FILE_NOT_REGULAR_FILE"
173 # Ensure the hcs12mem tool is available
174 elif [ ! -e "$LOAD_TOOL" ]
176 echo "hcs12mem does NOT appear to be available in the current path! exiting..."
177 exit "$HCS12MEM_EXECUTABLE_DOES_NOT_EXIST"
179 # Ensure the hcs12mem tool is readable
180 elif [ ! -r "$LOAD_TOOL" ]
182 echo "$LOAD_TOOL is NOT readable! exiting..."
183 exit "$HCS12MEM_EXECUTABLE_NOT_READABLE"
185 # Ensure the hcs12mem tool is executable
186 elif [ ! -x "$LOAD_TOOL" ]
188 echo "$LOAD_TOOL is NOT executable! exiting..."
189 exit "$HCS12MEM_EXECUTABLE_NOT_EXECUTABLE"
191 # Ensure the needed dat file has been put in place
192 elif [ ! -e "$HCS12MEM_PATH/${XDP512}.dat" ]
194 echo "$HCS12MEM_PATH/${XDP512}.dat is missing, please copy ../bin/${XDP512}.dat to $HCS12MEM_PATH and retry, exiting..."
195 exit "$HCS12MEM_DAT_NOT_FOUND"
198 echo "Everything looks good!"
202 # Erase the flash blocks before trying to write our S19 in (this is required)
205 echo "Erase the flash on your FreeEMS? (This is necessary to load new firmware) [ yes | abort ]"
206 continue "yes" "abort" "$ABORT_PRE_ERASE"
208 echo "Erasing the FreeEMS flash without verification..."
209 $LOAD_TOOL -b $SERIAL_BAUD -i sm
-p $SERIAL_DEVICE -t $DEVICE_TYPE --flash-erase
213 echo "Erasing flash failed! exiting..."
217 echo "Erasing flash was successful!"
221 # Load the s19 into the FreeEMS without verification (verify unimplemented in hcs12mem)
224 echo "Load the firmware to your FreeEMS? [ yes | abort ]"
225 continue "yes" "abort" "$ABORT_PRE_LOAD"
227 echo "Loading $FIRMWARE_S19_PATH to FreeEMS flash without verification..."
228 $LOAD_TOOL -b $SERIAL_BAUD -i sm
-p $SERIAL_DEVICE -t $DEVICE_TYPE -a $ADDRESS_TYPE --flash-write "$FIRMWARE_S19_PATH"
232 echo "Firmware load failed! exiting..."
236 echo "Firmware load was successful!"
240 # Verify the load to flash manually
243 echo "Verifying firmware loaded to flash correctly..."
245 echo "Reading contents of FreeEMS flash back to $TEMP_S19_COPY ..."
246 $LOAD_TOOL -b $SERIAL_BAUD -i sm
-p $SERIAL_DEVICE -t $DEVICE_TYPE -a $ADDRESS_TYPE --flash-read "$TEMP_S19_COPY"
248 echo "Comparing $TEMP_S19_COPY with $FIRMWARE_S19_PATH , redirecting output to $TEMP_S19_DIFF ..."
249 diff $FIRMWARE_S19_PATH $TEMP_S19_COPY > $TEMP_S19_DIFF
253 echo "Verification failure! :-("
254 echo "The retrieved S19 can be found here : $TEMP_S19_COPY"
255 echo "The difference between that and the original can be found here : $TEMP_S19_DIFF"
258 echo "Verification successful!"
260 echo "Removing $TEMP_S19_COPY ..."
263 echo "Removing $TEMP_S19_DIFF ..."
273 # verify currently doesn't work. needs more finesse.
275 echo "It is strongly recommended that you re-extract the firmware and do a visual diff to confirm that the only change is the serial monitor being included."