Skip to content

Commit

Permalink
fix: checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
edy555 committed Feb 23, 2020
1 parent 681272c commit c89cd36
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void flash_unlock(void)
FLASH->KEYR = 0xCDEF89AB;
}

#define rotate(x) ((x<<1) | (x&(1<<31)?1:0))
#define rotate(x) (((x)<<1) | ((x)&(1<<31)?1:0))

static uint32_t
checksum(const void *start, size_t len)
Expand All @@ -73,7 +73,7 @@ checksum(const void *start, size_t len)
uint32_t *tail = (uint32_t*)(start + len);
uint32_t value = 0;
while (p < tail)
value = rotate(value) | *p++;
value = rotate(value) + *p++;
return value;
}

Expand Down Expand Up @@ -142,8 +142,7 @@ caldata_save(int id)
dst = (uint16_t*)saveareas[id];

current_props.magic = CONFIG_MAGIC;
current_props.checksum = 0;
current_props.checksum = checksum(&current_props, sizeof current_props);
current_props.checksum = checksum(&current_props, sizeof current_props - sizeof current_props.checksum);

flash_unlock();

Expand Down

0 comments on commit c89cd36

Please sign in to comment.