5 # Copyright 2008 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 ADDRESS_TYPE="non-banked"
38 export ADDRESS_TYPE
="banked-ppage"
39 #export ADDRESS_TYPE="banked-linear"
40 # Address type banked linear should be used WITHOUT lma/ppage references in the objcopy section of the Makefile
43 export TEMP_BASE
="/tmp/"`date +date_%F_time_%Hh%Mm%Ss_%p`
44 export TEMP_S19_COPY
=$TEMP_BASE".freeEMS.firmware.copy.s19"
45 export TEMP_S19_DIFF
=$TEMP_BASE".freeEMS.firmware.s19.diff"
48 export GOODBYE
="Thank you for choosing the FreeEMS to run your engine, goodbye."
49 export USAGE
="Usage : freeEMSloader.bash /dev/yourTTY new-firmware.s19"
51 # Formalise the error codes
52 export WRONG_EMS
="-10"
54 export WRONG_NUMBER_OF_ARGS
="-20"
55 export SERIAL_DEVICE_DOES_NOT_EXIST
="-21"
56 export SERIAL_DEVICE_NOT_READABLE
="-22"
57 export SERIAL_DEVICE_NOT_WRITABLE
="-23"
58 export SERIAL_DEVICE_NOT_CHARACTER_DEVICE
="-24"
60 export S19_FILE_DOES_NOT_EXIST
="-30"
61 export S19_FILE_NOT_READABLE
="-31"
62 export S19_FILE_NOT_REGULAR_FILE
="-32"
64 export HCS12MEM_EXECUTABLE_DOES_NOT_EXIST
="-40"
65 export HCS12MEM_EXECUTABLE_NOT_READABLE
="-41"
66 export HCS12MEM_EXECUTABLE_NOT_EXECUTABLE
="-42"
68 export ABORT_PRE_ERASE
="-50"
69 export ERASE_FAILED
="-51"
71 export ABORT_PRE_LOAD
="-60"
72 export LOAD_FAILED
="-61"
74 export VERIFY_FAILED
="-70"
75 export ABORT_PRE_DELETE_TMP
="-71"
76 export EXIT_WITHOUT_CLEANING_UP
="-72"
79 # continue() takes three arguments : affirmative meaning continue, negative meaning exit, and exit code
86 if [ "$ANSWER" == "$1" ]
90 elif [ "$ANSWER" == "$2" ]
95 echo "'$ANSWER' not recognised, $1 and $2 are valid, please re-enter"
101 # Display splash screen
104 echo "Welcome to the FreeEMS firmware loader"
106 echo "This program is released under the GPL V3"
107 echo "Please see http://www.gnu.org/licenses/ for details"
109 echo "This version is for $DEVICE_TYPE based FreeEMS's"
111 echo "Do you have a $DEVICE_TYPE based FreeEMS? [ yes | quit ]"
113 continue "yes" "quit" "$WRONG_EMS"
117 # Ensure all is in order
120 echo "Checking configuration and arguments..."
122 # Test for the correct number of arguments
123 if [ $EXPECTED_ARGS -ne $NUMBER_OF_ARGS ]
126 echo "Expected $EXPECTED_ARGS parameter(s), got $NUMBER_OF_ARGS, exiting..."
127 exit "$WRONG_NUMBER_OF_ARGS"
129 # Ensure serial device exists
130 elif [ ! -e "$SERIAL_DEVICE" ]
132 echo "Serial device $SERIAL_DEVICE does NOT exist! exiting..."
133 exit "$SERIAL_DEVICE_DOES_NOT_EXIST"
135 # Ensure serial device is readable
136 elif [ ! -r "$SERIAL_DEVICE" ]
138 echo "Serial device $SERIAL_DEVICE is NOT readable! exiting..."
139 exit "$SERIAL_DEVICE_NOT_READABLE"
141 # Ensure serial device is writable
142 elif [ ! -w "$SERIAL_DEVICE" ]
144 echo "Serial device $SERIAL_DEVICE is NOT writable! exiting..."
145 exit "$SERIAL_DEVICE_NOT_WRITABLE"
147 # Ensure serial device is a character device or symlink
148 elif [ ! -c "$SERIAL_DEVICE" ] && [ ! -L "$SERIAL_DEVICE" ]
150 echo "Serial device $SERIAL_DEVICE is NOT a valid character device or symlink! exiting..."
151 exit "$SERIAL_DEVICE_NOT_CHARACTER_DEVICE"
153 # Ensure the s19 file exists
154 elif [ ! -e "$FIRMWARE_S19_PATH" ]
156 echo "S19 file $FIRMWARE_S19_PATH does NOT exist! exiting..."
157 exit "$S19_FILE_DOES_NOT_EXIST"
159 # Ensure the s19 is readable
160 elif [ ! -r "$FIRMWARE_S19_PATH" ]
162 echo "S19 file $FIRMWARE_S19_PATH is NOT readable! exiting..."
163 exit "$S19_FILE_NOT_READABLE"
165 # Ensure the s19 is a regular file or symlink
166 elif [ ! -f "$FIRMWARE_S19_PATH" ] && [ ! -L "$FIRMWARE_S19_PATH" ]
168 echo "S19 file $FIRMWARE_S19_PATH is NOT a regular file or symlink! exiting..."
169 exit "$S19_FILE_NOT_REGULAR_FILE"
171 # Ensure the hcs12mem tool is available
172 elif [ ! -e "$LOAD_TOOL" ]
174 echo "hcs12mem does NOT appear to be available in the current path! exiting..."
175 exit "$HCS12MEM_EXECUTABLE_DOES_NOT_EXIST"
177 # Ensure the hcs12mem tool is readable
178 elif [ ! -r "$LOAD_TOOL" ]
180 echo "$LOAD_TOOL is NOT readable! exiting..."
181 exit "$HCS12MEM_EXECUTABLE_NOT_READABLE"
183 # Ensure the hcs12mem tool is executable
184 elif [ ! -x "$LOAD_TOOL" ]
186 echo "$LOAD_TOOL is NOT executable! exiting..."
187 exit "$HCS12MEM_EXECUTABLE_NOT_EXECUTABLE"
190 echo "Everything looks good!"
194 # Erase the flash blocks before trying to write our S19 in (this is required)
197 echo "Erase the flash on your FreeEMS? (This is necessary to load new firmware) [ yes | abort ]"
198 continue "yes" "abort" "$ABORT_PRE_ERASE"
200 echo "Erasing the FreeEMS flash without verification..."
201 $LOAD_TOOL -b $SERIAL_BAUD -i sm
-p $SERIAL_DEVICE -t $DEVICE_TYPE --flash-erase
205 echo "Erasing flash failed! exiting..."
209 echo "Erasing flash was successful!"
213 # Load the s19 into the FreeEMS without verification (verify unimplemented in hcs12mem)
216 echo "Load the firmware to your FreeEMS? [ yes | abort ]"
217 continue "yes" "abort" "$ABORT_PRE_LOAD"
219 echo "Loading $FIRMWARE_S19_PATH to FreeEMS flash without verification..."
220 $LOAD_TOOL -b $SERIAL_BAUD -i sm
-p $SERIAL_DEVICE -t $DEVICE_TYPE -a $ADDRESS_TYPE --flash-write "$FIRMWARE_S19_PATH"
224 echo "Firmware load failed! exiting..."
228 echo "Firmware load was successful!"
232 # Verify the load to flash manually
235 echo "Verifying firmware loaded to flash correctly..."
237 echo "Reading contents of FreeEMS flash back to $TEMP_S19_COPY ..."
238 $LOAD_TOOL -b $SERIAL_BAUD -i sm
-p $SERIAL_DEVICE -t $DEVICE_TYPE -a $ADDRESS_TYPE --flash-read "$TEMP_S19_COPY"
240 echo "Comparing $TEMP_S19_COPY with $FIRMWARE_S19_PATH , redirecting output to $TEMP_S19_DIFF ..."
241 diff $FIRMWARE_S19_PATH $TEMP_S19_COPY > $TEMP_S19_DIFF
245 echo "Verification failure! :-("
246 echo "The retrieved S19 can be found here : $TEMP_S19_COPY"
247 echo "The difference between that and the original can be found here : $TEMP_S19_DIFF"
250 echo "Verification successful!"
252 echo "Removing $TEMP_S19_COPY ..."
255 echo "Removing $TEMP_S19_DIFF ..."
265 # verify currently doesn't work. needs more finesse.
267 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."