On 2006-08-07, David Peters <[email protected]> wrote:

> Is it possible for the GCC tools to generate a
> checksum from the generated code and embed it into the
> hex file at a specific address?

I don't think so.  

It's fairly trivial to write a little utility to do that,
though.  There are also tons of pretty fancy (and free) hex
file utilities that do that sort of thing.

For exampmle, here's a Python program I use to pad a file to a
specified size (by adding 0xffs to the input file) and add a
16-bit checksum to the last two bytes:

------------------------------8<------------------------------
#!/usr/bin/python
import sys

def showHex(s):
    print " ".join(["%02x" % ord(c) for c in s])

def sum16(s):
    sum = 0
    i = 0
    while i < len(s):
        sum += ord(s[i])<<8 
        sum += ord(s[i+1])
        sum &= 0xffff
        i += 2
    return sum
    
requiredSize    = int(sys.argv[1])*1024
inputFilename   = sys.argv[2]
outputFilename  = sys.argv[3]

image = file(inputFilename,"rb").read()
print len(image)
padLength = (requiredSize-4) - len(image)
image += '\xff'*padLength

print len(image)

s = sum16(image)
c = -s & 0xffff

print hex(s),hex(c)

if c == 0 or c == 0xffff:
    c -= 0x1234
    c &= 0xffff
    image += '\x12\x34'
else:
    image += '\x00\x00'
    
image += (chr(c>>8) + chr(c&0xff))

file(outputFilename,"wb").write(image)
------------------------------8<------------------------------

The above program operates on binary files (which are much
easier to manipulate than hex files).  If you really want a hex
file when you're done, the objcopy utility can convert between
binary and various hex formats.

> I've done this in the past using the IAR compiler and need it
> for the bootloader to check the new code being loaded into
> flash.

-- 
Grant Edwards                   grante             Yow!  If you STAY in China,
                                  at               I'll give you 4,000 BUSHELS
                               visi.com            of "ATOMIC MOUSE" pencil
                                                   sharpeners!!


Reply via email to