]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/aeb/v2_0/src/flash_cksum.tcl
Initial revision
[karo-tx-redboot.git] / packages / hal / arm / aeb / v2_0 / src / flash_cksum.tcl
1 #!/bin/bash
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`" "$@" ; \
5         done ; \
6         echo "flash_cksum.tcl: cannot find Tcl shell" ; exit 1' "$0" "$@"
7
8 #===============================================================================
9 #
10 #    flash_cksum.tcl
11 #
12 #    Compute the checksum for a AEB-1 flash module
13 #
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.
19 ##
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.
23 ##
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
27 ## for more details.
28 ##
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.
32 ##
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.
39 ##
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.
42 ##
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####
49 #
50 # Author(s):    gthomas
51 # Contact(s):   gthomas
52 # Date:         1999/01/06
53 # Version:      0.01 $RcsVersion$
54 # Purpose:      Compute the checksum for a FLASH ROM module.
55 # Description:
56 # Requires:     A working Tcl interpreter, V8.0 or later.
57 # Provides:     Command line operation only.
58 # Inputs:       
59 # Side FX:      
60 # Outputs:
61 # See also:     'Notes' which describes the process of creating the GDB stubs
62 #               module.
63 # Known bugs:   
64 # Usage:
65 #
66 #####DESCRIPTIONEND####
67 #===============================================================================
68
69 #
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.
73 #
74
75 proc checksum { name } {
76
77     set status [ catch {
78         set fd [open $name "r"]
79     fconfigure $fd -translation binary
80         set data [read $fd]
81         close $fd
82     } message]
83
84     if { $status != 0 } {
85         puts "Unable to read file $name: $message"
86         exit 1
87     }
88
89     set sum 0
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
93         # of zero bytes.
94         set char0 [string index $data [expr $i + 0]]
95         if { "$char0" != "" } {
96             scan $char0 "%c" ascii0
97         } else {
98             set ascii0 0
99         }
100         set char1 [string index $data [expr $i + 1]]
101         if { "$char1" != "" } {
102             scan $char1 "%c" ascii1
103         } else {
104             set ascii1 0
105         }
106         set char2 [string index $data [expr $i + 2]]
107         if { "$char2" != "" } {
108             scan $char2 "%c" ascii2
109         } else {
110             set ascii2 0
111         }
112         set char3 [string index $data [expr $i + 3]]
113         if { "$char3" != "" } {
114             scan $char3 "%c" ascii3
115         } else {
116             set ascii3 0
117         }
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]"
124     }
125
126     return $sum
127 }
128
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]"
139     return $swapped
140 }
141
142 global tcl_platform
143 set byteOrder $tcl_platform(byteOrder)
144 # puts "platform byte order = $byteOrder"
145
146 # Parse command line arguments
147 set argc 0
148 array set args { }
149 foreach arg $argv {
150     set args([incr argc]) $arg
151 }
152
153 if { "$argc" != "1" } {
154         puts "usage: flash_cksum <file>"
155         exit 1
156 }
157
158 set cksum [checksum $args(1)]
159
160 # if { "$byteOrder" == "littleEndian" } {
161     # puts "Swap bytes!"
162     set cksum [bswap $cksum]
163 # }
164
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
170 # safer.
171
172 # puts "[format 0x%8.8X $cksum]"
173
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)]]
179