code: def checksum_creation_action(target, source, env):
      
  
      # Important Notes:
      # There are apparently many ways to calculate a CRC-32 checksum, we use the following options
      # Initial seed value prepended to the input: 0xFFFFFFFF
      # Whether the input is fed into the shift register least-significant bit or most-significant bit first: LSB
      # Whether each data word is inverted: No
      # Whether the final CRC value is inverted: No
      # *These settings must agree between the executive and this function*
  
      import crcmod
      crc32_func = crcmod.mkCrcFun(0x104C11DB7, initCrc=0xFFFFFFFF, rev=False, xorOut=0)
  
      with open(str(source[0]), 'rb') as f:
          data = f.read()
  
          # Ignore the last four bytes of the file since that is where the checksum will go
          data = data[:-4]
  
          # Make sure the magic number is correct so that we're dealing with an actual firmware image
          magicbin = data[-4:]
          magic, = struct.unpack('<L', magicbin)
  
          if magic != 0xBAADDAAD:
              raise BuildError("Attempting to patch a file that is not a CDB binary or has the wrong size", reason="invalid magic number found", actual_magic=magic, desired_magic=0xBAADDAAD)
  
          # Calculate CRC32 in the same way as its done in the target microcontroller
          checksum = crc32_func(data) & 0xFFFFFFFF
  
      with open(str(target[0]), 'w') as f:
          # hex strings end with L on windows and possibly some other systems
          checkhex = hex(checksum)
          if checkhex[-1] == 'L':
              checkhex = checkhex[:-1]
  
          f.write("--defsym=__image_checksum=%s\n" % checkhex)
docs: Create a linker command file for patching an application checksum into a firmware image