2 # restart using a Tcl shell \
3 exec sh -c 'for tclshell in tclsh tclsh83 cygtclsh80 ; do \
4 ( echo | $tclshell ) 2> /dev/null && exec $tclshell "`( cygpath -w \"$0\" ) 2> /dev/null || echo $0`" "$@" ; \
6 echo "flash_cksum.tcl: cannot find Tcl shell" ; exit 1' "$0" "$@"
8 #===============================================================================
12 # Compute the checksum for a AEB-1 flash module
14 #===============================================================================
15 #####ECOSGPLCOPYRIGHTBEGIN####
16 ## -------------------------------------------
17 ## This file is part of eCos, the Embedded Configurable Operating System.
18 ## Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
20 ## eCos is free software; you can redistribute it and/or modify it under
21 ## the terms of the GNU General Public License as published by the Free
22 ## Software Foundation; either version 2 or (at your option) any later version.
24 ## eCos is distributed in the hope that it will be useful, but WITHOUT ANY
25 ## WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 ## You should have received a copy of the GNU General Public License along
30 ## with eCos; if not, write to the Free Software Foundation, Inc.,
31 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
33 ## As a special exception, if other files instantiate templates or use macros
34 ## or inline functions from this file, or you compile this file and link it
35 ## with other works to produce a work based on this file, this file does not
36 ## by itself cause the resulting work to be covered by the GNU General Public
37 ## License. However the source code for this file must still be made available
38 ## in accordance with section (3) of the GNU General Public License.
40 ## This exception does not invalidate any other reasons why a work based on
41 ## this file might be covered by the GNU General Public License.
43 ## Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
44 ## at http://sources.redhat.com/ecos/ecos-license/
45 ## -------------------------------------------
46 #####ECOSGPLCOPYRIGHTEND####
47 #===============================================================================
48 ######DESCRIPTIONBEGIN####
53 # Version: 0.01 $RcsVersion$
54 # Purpose: Compute the checksum for a FLASH ROM module.
56 # Requires: A working Tcl interpreter, V8.0 or later.
57 # Provides: Command line operation only.
61 # See also: 'Notes' which describes the process of creating the GDB stubs
66 #####DESCRIPTIONEND####
67 #===============================================================================
70 # This ckecksum is the 32-bit XOR of all 32 bit words in the file.
71 # The output of the program is a single number, suitable for use in
72 # a Makefile/script used to create an AEB-1 module for FLASH ROM.
75 proc checksum { name } {
78 set fd [open $name "r"]
79 fconfigure $fd -translation binary
85 puts "Unable to read file $name: $message"
90 set length [string length $data]
91 for { set i 0 } { $i < $length } { incr i 4 } {
92 # Fetch the next 32 bit word. The funky tests are to handle the case
94 set char0 [string index $data [expr $i + 0]]
95 if { "$char0" != "" } {
96 scan $char0 "%c" ascii0
100 set char1 [string index $data [expr $i + 1]]
101 if { "$char1" != "" } {
102 scan $char1 "%c" ascii1
106 set char2 [string index $data [expr $i + 2]]
107 if { "$char2" != "" } {
108 scan $char2 "%c" ascii2
112 set char3 [string index $data [expr $i + 3]]
113 if { "$char3" != "" } {
114 scan $char3 "%c" ascii3
118 set word [expr $ascii0 << 24]
119 set word [expr $word | [expr $ascii1 << 16]]
120 set word [expr $word | [expr $ascii2 << 8]]
121 set word [expr $word | $ascii3]
122 set sum [expr $sum ^ $word]
123 # puts "sum: [format %8.8X $sum], word: [format %8.8X $word]"
129 proc bswap { native } {
130 set byte0 [expr [expr $native >> 24] & 255]
131 set byte1 [expr [expr $native >> 16] & 255]
132 set byte2 [expr [expr $native >> 8] & 255]
133 set byte3 [expr [expr $native >> 0] & 255]
134 set swapped [expr $byte3 << 24]
135 set swapped [expr $swapped | [expr $byte2 << 16]]
136 set swapped [expr $swapped | [expr $byte1 << 8]]
137 set swapped [expr $swapped | [expr $byte0 << 0]]
138 # puts "native: [format %8.8X $native], swapped: [format %8.8X $swapped]"
143 set byteOrder $tcl_platform(byteOrder)
144 # puts "platform byte order = $byteOrder"
146 # Parse command line arguments
150 set args([incr argc]) $arg
153 if { "$argc" != "1" } {
154 puts "usage: flash_cksum <file>"
158 set cksum [checksum $args(1)]
160 # if { "$byteOrder" == "littleEndian" } {
162 set cksum [bswap $cksum]
165 # Tcl up to at least version 8.3 has problems on alphas,
166 # Tcl_FormatObjCmd() invokes Tcl_GetIntFromObj() which
167 # ends up doing dodgy conversions between int and long.
168 # This results in an error if bit 32 is set. Rewriting
169 # the format string to output one byte at a time is
172 # puts "[format 0x%8.8X $cksum]"
174 puts [format "0x%02X%02X%02X%02X" \
175 [expr (($cksum >> 24) & 0x0FF)] \
176 [expr (($cksum >> 16) & 0x0FF)] \
177 [expr (($cksum >> 8) & 0x0FF)] \
178 [expr (($cksum ) & 0x0FF)]]