How to rebuild the libmem_crc32_direct CRC function in python?

0

I like to rebuild the libmem_crc32_direct function in python.

I used the crcmod python package before. So I like to setup the crc generator by using it.

the c-code looks like:

uint32_t crc_process_chunk(uint8_t* data, uint32_t len) {
  return ~libmem_crc32_direct(data, len, 0xFFFFFFFF);
}

my python code looks so far:

def bit_not(n, numbits=8):
    return (1 << numbits) - 1 - n

def getCRC(imageBA):
    crcGen = crcmod.mkCrcFun(0x104C11DB7, initCrc=0xFFFFFFFF)
    val = crcGen(imageBA)
    val = bit_not(val, 32)
    return val

The returned value of the python code is not equal of the one in c. So I guess I mad some error.

Any ideas?

python
c
stm32
crc
asked on Stack Overflow Jul 11, 2019 by Stefan Jaritz

2 Answers

1

Doesn't (1 << numbits) == 0? If this is two's complement math it should work as bit_not could be return 0-1-n. However, this isn't needed, since there is an optional xorOut parameter for crcmod. I'm thinking that since the optional rev parameter for reversed (reflected) input and output defaults to true, it needs to be set to false. I think the call to create the crc generator should be:

    crcGen = crcmod.mkCrcFun(0x104C11DB7, initCrc=0xFFFFFFF, rev=False, xorOut=0xFFFFFFFF)
answered on Stack Overflow Jul 16, 2019 by rcgldr • edited Jul 16, 2019 by rcgldr
0

B bit tricky because 64Bit arithmetic on PC vs 32Bit arithmetic on ARM STM32F4, but finally this solution works:

def libmem_crc32_direct_with_xor(im, startAddr, l):
    fw = im[startAddr:startAddr+l]
    crcGen =  crcmod.Crc(0x104C11DB7, initCrc=0xFFFFFFFF, rev = False)
    crcGen.update(fw)
    return (~crcGen.crcValue ) & 0xFFFFFFFF # 32bit xor
answered on Stack Overflow Jul 16, 2019 by Stefan Jaritz

User contributions licensed under CC BY-SA 3.0